blob: 883766a36c0909d4bb764f807eb364e425481726 [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);
fippobec70ab2016-01-28 01:27:15 -0800180 report->AddString(StatsReport::kStatsValueNameMediaType, "audio");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000181}
182
183void ExtractStats(const cricket::VoiceSenderInfo& info, StatsReport* report) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000184 ExtractCommonSendProperties(info, report);
185
andrew2fe1cb02015-11-27 17:27:35 -0800186 SetAudioProcessingStats(
187 report, info.typing_noise_detected, info.echo_return_loss,
188 info.echo_return_loss_enhancement, info.echo_delay_median_ms,
189 info.aec_quality_min, info.echo_delay_std_ms);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000190
andrew2fe1cb02015-11-27 17:27:35 -0800191 RTC_DCHECK_GE(info.audio_level, 0);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000192 const IntForAdd ints[] = {
andrew2fe1cb02015-11-27 17:27:35 -0800193 { StatsReport::kStatsValueNameAudioInputLevel, info.audio_level},
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000194 { StatsReport::kStatsValueNameJitterReceived, info.jitter_ms },
195 { StatsReport::kStatsValueNamePacketsLost, info.packets_lost },
196 { StatsReport::kStatsValueNamePacketsSent, info.packets_sent },
197 };
198
199 for (const auto& i : ints)
200 report->AddInt(i.name, i.value);
fippobec70ab2016-01-28 01:27:15 -0800201 report->AddString(StatsReport::kStatsValueNameMediaType, "audio");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000202}
203
204void ExtractStats(const cricket::VideoReceiverInfo& info, StatsReport* report) {
pbosf42376c2015-08-28 07:35:32 -0700205 ExtractCommonReceiveProperties(info, report);
Peter Boströmb7d9a972015-12-18 16:01:11 +0100206 report->AddString(StatsReport::kStatsValueNameCodecImplementationName,
207 info.decoder_implementation_name);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000208 report->AddInt64(StatsReport::kStatsValueNameBytesReceived,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000209 info.bytes_rcvd);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000210 report->AddInt64(StatsReport::kStatsValueNameCaptureStartNtpTimeMs,
buildbot@webrtc.org0581f0b2014-05-06 21:36:31 +0000211 info.capture_start_ntp_time_ms);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000212 const IntForAdd ints[] = {
213 { StatsReport::kStatsValueNameCurrentDelayMs, info.current_delay_ms },
214 { StatsReport::kStatsValueNameDecodeMs, info.decode_ms },
215 { StatsReport::kStatsValueNameFirsSent, info.firs_sent },
216 { StatsReport::kStatsValueNameFrameHeightReceived, info.frame_height },
217 { StatsReport::kStatsValueNameFrameRateDecoded, info.framerate_decoded },
218 { StatsReport::kStatsValueNameFrameRateOutput, info.framerate_output },
219 { StatsReport::kStatsValueNameFrameRateReceived, info.framerate_rcvd },
220 { StatsReport::kStatsValueNameFrameWidthReceived, info.frame_width },
221 { StatsReport::kStatsValueNameJitterBufferMs, info.jitter_buffer_ms },
222 { StatsReport::kStatsValueNameMaxDecodeMs, info.max_decode_ms },
223 { StatsReport::kStatsValueNameMinPlayoutDelayMs,
224 info.min_playout_delay_ms },
225 { StatsReport::kStatsValueNameNacksSent, info.nacks_sent },
226 { StatsReport::kStatsValueNamePacketsLost, info.packets_lost },
227 { StatsReport::kStatsValueNamePacketsReceived, info.packets_rcvd },
228 { StatsReport::kStatsValueNamePlisSent, info.plis_sent },
229 { StatsReport::kStatsValueNameRenderDelayMs, info.render_delay_ms },
230 { StatsReport::kStatsValueNameTargetDelayMs, info.target_delay_ms },
231 };
232
233 for (const auto& i : ints)
234 report->AddInt(i.name, i.value);
fippobec70ab2016-01-28 01:27:15 -0800235 report->AddString(StatsReport::kStatsValueNameMediaType, "video");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000236}
237
238void ExtractStats(const cricket::VideoSenderInfo& info, StatsReport* report) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000239 ExtractCommonSendProperties(info, report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000240
Peter Boströmb7d9a972015-12-18 16:01:11 +0100241 report->AddString(StatsReport::kStatsValueNameCodecImplementationName,
242 info.encoder_implementation_name);
mallinath@webrtc.org62451dc2013-12-13 12:29:34 +0000243 report->AddBoolean(StatsReport::kStatsValueNameBandwidthLimitedResolution,
244 (info.adapt_reason & 0x2) > 0);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000245 report->AddBoolean(StatsReport::kStatsValueNameCpuLimitedResolution,
246 (info.adapt_reason & 0x1) > 0);
mallinath@webrtc.org62451dc2013-12-13 12:29:34 +0000247 report->AddBoolean(StatsReport::kStatsValueNameViewLimitedResolution,
248 (info.adapt_reason & 0x4) > 0);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000249
250 const IntForAdd ints[] = {
251 { StatsReport::kStatsValueNameAdaptationChanges, info.adapt_changes },
252 { StatsReport::kStatsValueNameAvgEncodeMs, info.avg_encode_ms },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000253 { StatsReport::kStatsValueNameEncodeUsagePercent,
254 info.encode_usage_percent },
255 { StatsReport::kStatsValueNameFirsReceived, info.firs_rcvd },
256 { StatsReport::kStatsValueNameFrameHeightInput, info.input_frame_height },
257 { StatsReport::kStatsValueNameFrameHeightSent, info.send_frame_height },
258 { StatsReport::kStatsValueNameFrameRateInput, info.framerate_input },
259 { StatsReport::kStatsValueNameFrameRateSent, info.framerate_sent },
260 { StatsReport::kStatsValueNameFrameWidthInput, info.input_frame_width },
261 { StatsReport::kStatsValueNameFrameWidthSent, info.send_frame_width },
262 { StatsReport::kStatsValueNameNacksReceived, info.nacks_rcvd },
263 { StatsReport::kStatsValueNamePacketsLost, info.packets_lost },
264 { StatsReport::kStatsValueNamePacketsSent, info.packets_sent },
265 { StatsReport::kStatsValueNamePlisReceived, info.plis_rcvd },
266 };
267
268 for (const auto& i : ints)
269 report->AddInt(i.name, i.value);
fippobec70ab2016-01-28 01:27:15 -0800270 report->AddString(StatsReport::kStatsValueNameMediaType, "video");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000271}
272
273void ExtractStats(const cricket::BandwidthEstimationInfo& info,
274 double stats_gathering_started,
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000275 PeerConnectionInterface::StatsOutputLevel level,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000276 StatsReport* report) {
henrikg91d6ede2015-09-17 00:24:34 -0700277 RTC_DCHECK(report->type() == StatsReport::kStatsReportTypeBwe);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000278
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000279 report->set_timestamp(stats_gathering_started);
280 const IntForAdd ints[] = {
281 { StatsReport::kStatsValueNameAvailableSendBandwidth,
282 info.available_send_bandwidth },
283 { StatsReport::kStatsValueNameAvailableReceiveBandwidth,
284 info.available_recv_bandwidth },
285 { StatsReport::kStatsValueNameTargetEncBitrate, info.target_enc_bitrate },
286 { StatsReport::kStatsValueNameActualEncBitrate, info.actual_enc_bitrate },
287 { StatsReport::kStatsValueNameRetransmitBitrate, info.retransmit_bitrate },
288 { StatsReport::kStatsValueNameTransmitBitrate, info.transmit_bitrate },
289 };
290 for (const auto& i : ints)
291 report->AddInt(i.name, i.value);
292 report->AddInt64(StatsReport::kStatsValueNameBucketDelay, info.bucket_delay);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000293}
294
wu@webrtc.org97077a32013-10-25 21:18:33 +0000295void ExtractRemoteStats(const cricket::MediaSenderInfo& info,
296 StatsReport* report) {
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000297 report->set_timestamp(info.remote_stats[0].timestamp);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000298 // TODO(hta): Extract some stats here.
299}
300
301void ExtractRemoteStats(const cricket::MediaReceiverInfo& info,
302 StatsReport* report) {
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000303 report->set_timestamp(info.remote_stats[0].timestamp);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000304 // TODO(hta): Extract some stats here.
305}
306
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000307// Template to extract stats from a data vector.
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000308// In order to use the template, the functions that are called from it,
309// ExtractStats and ExtractRemoteStats, must be defined and overloaded
310// for each type.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000311template<typename T>
312void ExtractStatsFromList(const std::vector<T>& data,
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000313 const StatsReport::Id& transport_id,
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000314 StatsCollector* collector,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000315 StatsReport::Direction direction) {
316 for (const auto& d : data) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200317 uint32_t ssrc = d.ssrc();
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000318 // Each track can have stats for both local and remote objects.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000319 // TODO(hta): Handle the case of multiple SSRCs per object.
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000320 StatsReport* report = collector->PrepareReport(true, ssrc, transport_id,
321 direction);
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000322 if (report)
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000323 ExtractStats(d, report);
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000324
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000325 if (!d.remote_stats.empty()) {
326 report = collector->PrepareReport(false, ssrc, transport_id, direction);
327 if (report)
328 ExtractRemoteStats(d, report);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000329 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000330 }
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000331}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000332
333} // namespace
334
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000335const char* IceCandidateTypeToStatsType(const std::string& candidate_type) {
336 if (candidate_type == cricket::LOCAL_PORT_TYPE) {
337 return STATSREPORT_LOCAL_PORT_TYPE;
338 }
339 if (candidate_type == cricket::STUN_PORT_TYPE) {
340 return STATSREPORT_STUN_PORT_TYPE;
341 }
342 if (candidate_type == cricket::PRFLX_PORT_TYPE) {
343 return STATSREPORT_PRFLX_PORT_TYPE;
344 }
345 if (candidate_type == cricket::RELAY_PORT_TYPE) {
346 return STATSREPORT_RELAY_PORT_TYPE;
347 }
henrikg91d6ede2015-09-17 00:24:34 -0700348 RTC_DCHECK(false);
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000349 return "unknown";
350}
351
352const char* AdapterTypeToStatsType(rtc::AdapterType type) {
353 switch (type) {
354 case rtc::ADAPTER_TYPE_UNKNOWN:
355 return "unknown";
356 case rtc::ADAPTER_TYPE_ETHERNET:
357 return STATSREPORT_ADAPTER_TYPE_ETHERNET;
358 case rtc::ADAPTER_TYPE_WIFI:
359 return STATSREPORT_ADAPTER_TYPE_WIFI;
360 case rtc::ADAPTER_TYPE_CELLULAR:
361 return STATSREPORT_ADAPTER_TYPE_WWAN;
362 case rtc::ADAPTER_TYPE_VPN:
363 return STATSREPORT_ADAPTER_TYPE_VPN;
phoglund@webrtc.org006521d2015-02-12 09:23:59 +0000364 case rtc::ADAPTER_TYPE_LOOPBACK:
365 return STATSREPORT_ADAPTER_TYPE_LOOPBACK;
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000366 default:
henrikg91d6ede2015-09-17 00:24:34 -0700367 RTC_DCHECK(false);
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000368 return "";
369 }
370}
371
deadbeefab9b2d12015-10-14 11:33:11 -0700372StatsCollector::StatsCollector(PeerConnection* pc)
373 : pc_(pc), stats_gathering_started_(0) {
374 RTC_DCHECK(pc_);
tommi@webrtc.org03505bc2014-07-14 20:15:26 +0000375}
376
377StatsCollector::~StatsCollector() {
deadbeefab9b2d12015-10-14 11:33:11 -0700378 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000379}
380
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000381double StatsCollector::GetTimeNow() {
382 return rtc::Timing::WallTimeNow() * rtc::kNumMillisecsPerSec;
383}
384
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000385// Adds a MediaStream with tracks that can be used as a |selector| in a call
386// to GetStats.
387void StatsCollector::AddStream(MediaStreamInterface* stream) {
deadbeefab9b2d12015-10-14 11:33:11 -0700388 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700389 RTC_DCHECK(stream != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000390
391 CreateTrackReports<AudioTrackVector>(stream->GetAudioTracks(),
jbauchbe24c942015-06-22 15:06:43 -0700392 &reports_, track_ids_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000393 CreateTrackReports<VideoTrackVector>(stream->GetVideoTracks(),
jbauchbe24c942015-06-22 15:06:43 -0700394 &reports_, track_ids_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000395}
396
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000397void StatsCollector::AddLocalAudioTrack(AudioTrackInterface* audio_track,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200398 uint32_t ssrc) {
deadbeefab9b2d12015-10-14 11:33:11 -0700399 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700400 RTC_DCHECK(audio_track != NULL);
tommi@webrtc.org4b89aa02015-03-16 09:52:30 +0000401#if (!defined(NDEBUG) || defined(DCHECK_ALWAYS_ON))
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000402 for (const auto& track : local_audio_tracks_)
henrikg91d6ede2015-09-17 00:24:34 -0700403 RTC_DCHECK(track.first != audio_track || track.second != ssrc);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000404#endif
xians@webrtc.org01bda202014-07-09 07:38:38 +0000405
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000406 local_audio_tracks_.push_back(std::make_pair(audio_track, ssrc));
xians@webrtc.org01bda202014-07-09 07:38:38 +0000407
408 // Create the kStatsReportTypeTrack report for the new track if there is no
409 // report yet.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000410 StatsReport::Id id(StatsReport::NewTypedId(StatsReport::kStatsReportTypeTrack,
411 audio_track->id()));
412 StatsReport* report = reports_.Find(id);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000413 if (!report) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000414 report = reports_.InsertNew(id);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000415 report->AddString(StatsReport::kStatsValueNameTrackId, audio_track->id());
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000416 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000417}
418
419void StatsCollector::RemoveLocalAudioTrack(AudioTrackInterface* audio_track,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200420 uint32_t ssrc) {
henrikg91d6ede2015-09-17 00:24:34 -0700421 RTC_DCHECK(audio_track != NULL);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000422 local_audio_tracks_.erase(std::remove_if(local_audio_tracks_.begin(),
423 local_audio_tracks_.end(),
424 [audio_track, ssrc](const LocalAudioTrackVector::value_type& track) {
425 return track.first == audio_track && track.second == ssrc;
426 }));
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000427}
428
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000429void StatsCollector::GetStats(MediaStreamTrackInterface* track,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000430 StatsReports* reports) {
deadbeefab9b2d12015-10-14 11:33:11 -0700431 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700432 RTC_DCHECK(reports != NULL);
433 RTC_DCHECK(reports->empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000434
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000435 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
436
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000437 if (!track) {
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000438 reports->reserve(reports_.size());
439 for (auto* r : reports_)
440 reports->push_back(r);
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000441 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000442 }
443
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000444 StatsReport* report = reports_.Find(StatsReport::NewTypedId(
deadbeefab9b2d12015-10-14 11:33:11 -0700445 StatsReport::kStatsReportTypeSession, pc_->session()->id()));
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000446 if (report)
447 reports->push_back(report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000448
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000449 report = reports_.Find(StatsReport::NewTypedId(
450 StatsReport::kStatsReportTypeTrack, track->id()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000451
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000452 if (!report)
453 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000454
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000455 reports->push_back(report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000456
457 std::string track_id;
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000458 for (const auto* r : reports_) {
459 if (r->type() != StatsReport::kStatsReportTypeSsrc)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000460 continue;
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000461
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000462 const StatsReport::Value* v =
463 r->FindValue(StatsReport::kStatsValueNameTrackId);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000464 if (v && v->string_val() == track->id())
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000465 reports->push_back(r);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000466 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000467}
468
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000469void
470StatsCollector::UpdateStats(PeerConnectionInterface::StatsOutputLevel level) {
deadbeefab9b2d12015-10-14 11:33:11 -0700471 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000472 double time_now = GetTimeNow();
473 // Calls to UpdateStats() that occur less than kMinGatherStatsPeriod number of
474 // ms apart will be ignored.
475 const double kMinGatherStatsPeriod = 50;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000476 if (stats_gathering_started_ != 0 &&
477 stats_gathering_started_ + kMinGatherStatsPeriod > time_now) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000478 return;
479 }
480 stats_gathering_started_ = time_now;
481
deadbeefab9b2d12015-10-14 11:33:11 -0700482 if (pc_->session()) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000483 // TODO(tommi): All of these hop over to the worker thread to fetch
484 // information. We could use an AsyncInvoker to run all of these and post
485 // the information back to the signaling thread where we can create and
486 // update stats reports. That would also clean up the threading story a bit
487 // since we'd be creating/updating the stats report objects consistently on
488 // the same thread (this class has no locks right now).
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000489 ExtractSessionInfo();
490 ExtractVoiceInfo();
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000491 ExtractVideoInfo(level);
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000492 ExtractDataInfo();
jbauchbe24c942015-06-22 15:06:43 -0700493 UpdateTrackReports();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000494 }
495}
496
deadbeefab9b2d12015-10-14 11:33:11 -0700497StatsReport* StatsCollector::PrepareReport(
498 bool local,
499 uint32_t ssrc,
500 const StatsReport::Id& transport_id,
501 StatsReport::Direction direction) {
502 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000503 StatsReport::Id id(StatsReport::NewIdWithDirection(
Peter Boström0c4e06b2015-10-07 12:23:21 +0200504 local ? StatsReport::kStatsReportTypeSsrc
505 : StatsReport::kStatsReportTypeRemoteSsrc,
506 rtc::ToString<uint32_t>(ssrc), direction));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000507 StatsReport* report = reports_.Find(id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000508
xians@webrtc.org01bda202014-07-09 07:38:38 +0000509 // Use the ID of the track that is currently mapped to the SSRC, if any.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000510 std::string track_id;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000511 if (!GetTrackIdBySsrc(ssrc, &track_id, direction)) {
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000512 if (!report) {
xians@webrtc.org01bda202014-07-09 07:38:38 +0000513 // The ssrc is not used by any track or existing report, return NULL
514 // in such case to indicate no report is prepared for the ssrc.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000515 return NULL;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000516 }
517
518 // The ssrc is not used by any existing track. Keeps the old track id
519 // since we want to report the stats for inactive ssrc.
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000520 const StatsReport::Value* v =
521 report->FindValue(StatsReport::kStatsValueNameTrackId);
522 if (v)
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000523 track_id = v->string_val();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000524 }
525
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000526 if (!report)
527 report = reports_.InsertNew(id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000528
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000529 // FYI - for remote reports, the timestamp will be overwritten later.
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000530 report->set_timestamp(stats_gathering_started_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000531
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000532 report->AddInt64(StatsReport::kStatsValueNameSsrc, ssrc);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000533 report->AddString(StatsReport::kStatsValueNameTrackId, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000534 // Add the mapping of SSRC to transport.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000535 report->AddId(StatsReport::kStatsValueNameTransportId, transport_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000536 return report;
537}
538
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000539StatsReport* StatsCollector::AddOneCertificateReport(
540 const rtc::SSLCertificate* cert, const StatsReport* issuer) {
deadbeefab9b2d12015-10-14 11:33:11 -0700541 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000542
wu@webrtc.org4551b792013-10-09 15:37:36 +0000543 // TODO(bemasc): Move this computation to a helper class that caches these
544 // values to reduce CPU use in GetStats. This will require adding a fast
545 // SSLCertificate::Equals() method to detect certificate changes.
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000546
547 std::string digest_algorithm;
548 if (!cert->GetSignatureDigestAlgorithm(&digest_algorithm))
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000549 return nullptr;
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000550
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000551 rtc::scoped_ptr<rtc::SSLFingerprint> ssl_fingerprint(
552 rtc::SSLFingerprint::Create(digest_algorithm, cert));
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000553
554 // SSLFingerprint::Create can fail if the algorithm returned by
555 // SSLCertificate::GetSignatureDigestAlgorithm is not supported by the
556 // implementation of SSLCertificate::ComputeDigest. This currently happens
557 // with MD5- and SHA-224-signed certificates when linked to libNSS.
558 if (!ssl_fingerprint)
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000559 return nullptr;
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000560
wu@webrtc.org4551b792013-10-09 15:37:36 +0000561 std::string fingerprint = ssl_fingerprint->GetRfc4572Fingerprint();
562
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000563 rtc::Buffer der_buffer;
wu@webrtc.org4551b792013-10-09 15:37:36 +0000564 cert->ToDER(&der_buffer);
565 std::string der_base64;
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +0000566 rtc::Base64::EncodeFromArray(der_buffer.data(), der_buffer.size(),
567 &der_base64);
wu@webrtc.org4551b792013-10-09 15:37:36 +0000568
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000569 StatsReport::Id id(StatsReport::NewTypedId(
570 StatsReport::kStatsReportTypeCertificate, fingerprint));
571 StatsReport* report = reports_.ReplaceOrAddNew(id);
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000572 report->set_timestamp(stats_gathering_started_);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000573 report->AddString(StatsReport::kStatsValueNameFingerprint, fingerprint);
574 report->AddString(StatsReport::kStatsValueNameFingerprintAlgorithm,
575 digest_algorithm);
576 report->AddString(StatsReport::kStatsValueNameDer, der_base64);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000577 if (issuer)
578 report->AddId(StatsReport::kStatsValueNameIssuerId, issuer->id());
579 return report;
wu@webrtc.org4551b792013-10-09 15:37:36 +0000580}
581
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000582StatsReport* StatsCollector::AddCertificateReports(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000583 const rtc::SSLCertificate* cert) {
deadbeefab9b2d12015-10-14 11:33:11 -0700584 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
wu@webrtc.org4551b792013-10-09 15:37:36 +0000585 // Produces a chain of StatsReports representing this certificate and the rest
586 // of its chain, and adds those reports to |reports_|. The return value is
587 // the id of the leaf report. The provided cert must be non-null, so at least
588 // one report will always be provided and the returned string will never be
589 // empty.
henrikg91d6ede2015-09-17 00:24:34 -0700590 RTC_DCHECK(cert != NULL);
wu@webrtc.org4551b792013-10-09 15:37:36 +0000591
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000592 StatsReport* issuer = nullptr;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000593 rtc::scoped_ptr<rtc::SSLCertChain> chain;
wu@webrtc.org4551b792013-10-09 15:37:36 +0000594 if (cert->GetChain(chain.accept())) {
595 // This loop runs in reverse, i.e. from root to leaf, so that each
596 // certificate's issuer's report ID is known before the child certificate's
597 // report is generated. The root certificate does not have an issuer ID
598 // value.
599 for (ptrdiff_t i = chain->GetSize() - 1; i >= 0; --i) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000600 const rtc::SSLCertificate& cert_i = chain->Get(i);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000601 issuer = AddOneCertificateReport(&cert_i, issuer);
wu@webrtc.org4551b792013-10-09 15:37:36 +0000602 }
603 }
604 // Add the leaf certificate.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000605 return AddOneCertificateReport(cert, issuer);
wu@webrtc.org4551b792013-10-09 15:37:36 +0000606}
607
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000608StatsReport* StatsCollector::AddConnectionInfoReport(
609 const std::string& content_name, int component, int connection_id,
610 const StatsReport::Id& channel_report_id,
611 const cricket::ConnectionInfo& info) {
612 StatsReport::Id id(StatsReport::NewCandidatePairId(content_name, component,
613 connection_id));
614 StatsReport* report = reports_.ReplaceOrAddNew(id);
615 report->set_timestamp(stats_gathering_started_);
616
617 const BoolForAdd bools[] = {
Peter Thatcher04ac81f2015-09-21 11:48:28 -0700618 {StatsReport::kStatsValueNameActiveConnection, info.best_connection},
619 {StatsReport::kStatsValueNameReceiving, info.receiving},
620 {StatsReport::kStatsValueNameWritable, info.writable},
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000621 };
622 for (const auto& b : bools)
623 report->AddBoolean(b.name, b.value);
624
625 report->AddId(StatsReport::kStatsValueNameChannelId, channel_report_id);
626 report->AddId(StatsReport::kStatsValueNameLocalCandidateId,
627 AddCandidateReport(info.local_candidate, true)->id());
628 report->AddId(StatsReport::kStatsValueNameRemoteCandidateId,
629 AddCandidateReport(info.remote_candidate, false)->id());
630
631 const Int64ForAdd int64s[] = {
632 { StatsReport::kStatsValueNameBytesReceived, info.recv_total_bytes },
633 { StatsReport::kStatsValueNameBytesSent, info.sent_total_bytes },
634 { StatsReport::kStatsValueNamePacketsSent, info.sent_total_packets },
635 { StatsReport::kStatsValueNameRtt, info.rtt },
636 { StatsReport::kStatsValueNameSendPacketsDiscarded,
637 info.sent_discarded_packets },
638 };
639 for (const auto& i : int64s)
640 report->AddInt64(i.name, i.value);
641
642 report->AddString(StatsReport::kStatsValueNameLocalAddress,
643 info.local_candidate.address().ToString());
644 report->AddString(StatsReport::kStatsValueNameLocalCandidateType,
645 info.local_candidate.type());
646 report->AddString(StatsReport::kStatsValueNameRemoteAddress,
647 info.remote_candidate.address().ToString());
648 report->AddString(StatsReport::kStatsValueNameRemoteCandidateType,
649 info.remote_candidate.type());
650 report->AddString(StatsReport::kStatsValueNameTransportType,
651 info.local_candidate.protocol());
652
653 return report;
654}
655
656StatsReport* StatsCollector::AddCandidateReport(
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000657 const cricket::Candidate& candidate,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000658 bool local) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000659 StatsReport::Id id(StatsReport::NewCandidateId(local, candidate.id()));
660 StatsReport* report = reports_.Find(id);
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000661 if (!report) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000662 report = reports_.InsertNew(id);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000663 report->set_timestamp(stats_gathering_started_);
664 if (local) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000665 report->AddString(StatsReport::kStatsValueNameCandidateNetworkType,
666 AdapterTypeToStatsType(candidate.network_type()));
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000667 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000668 report->AddString(StatsReport::kStatsValueNameCandidateIPAddress,
669 candidate.address().ipaddr().ToString());
670 report->AddString(StatsReport::kStatsValueNameCandidatePortNumber,
671 candidate.address().PortAsString());
672 report->AddInt(StatsReport::kStatsValueNameCandidatePriority,
673 candidate.priority());
674 report->AddString(StatsReport::kStatsValueNameCandidateType,
675 IceCandidateTypeToStatsType(candidate.type()));
676 report->AddString(StatsReport::kStatsValueNameCandidateTransportType,
677 candidate.protocol());
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000678 }
679
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000680 return report;
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000681}
682
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000683void StatsCollector::ExtractSessionInfo() {
deadbeefab9b2d12015-10-14 11:33:11 -0700684 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000685
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000686 // Extract information from the base session.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000687 StatsReport::Id id(StatsReport::NewTypedId(
deadbeefab9b2d12015-10-14 11:33:11 -0700688 StatsReport::kStatsReportTypeSession, pc_->session()->id()));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000689 StatsReport* report = reports_.ReplaceOrAddNew(id);
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000690 report->set_timestamp(stats_gathering_started_);
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000691 report->AddBoolean(StatsReport::kStatsValueNameInitiator,
deadbeefd59daf82015-10-14 15:02:44 -0700692 pc_->session()->initial_offerer());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000693
deadbeefd59daf82015-10-14 15:02:44 -0700694 SessionStats stats;
deadbeefab9b2d12015-10-14 11:33:11 -0700695 if (!pc_->session()->GetTransportStats(&stats)) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000696 return;
697 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000698
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000699 // Store the proxy map away for use in SSRC reporting.
700 // TODO(tommi): This shouldn't be necessary if we post the stats back to the
701 // signaling thread after fetching them on the worker thread, then just use
702 // the proxy map directly from the session stats.
703 // As is, if GetStats() failed, we could be using old (incorrect?) proxy
704 // data.
705 proxy_to_transport_ = stats.proxy_to_transport;
jiayl@webrtc.org06b04ec2014-07-24 20:41:20 +0000706
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000707 for (const auto& transport_iter : stats.transport_stats) {
708 // Attempt to get a copy of the certificates from the transport and
709 // expose them in stats reports. All channels in a transport share the
710 // same local and remote certificates.
711 //
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000712 StatsReport::Id local_cert_report_id, remote_cert_report_id;
Henrik Boströmd8281982015-08-27 10:12:24 +0200713 rtc::scoped_refptr<rtc::RTCCertificate> certificate;
deadbeefab9b2d12015-10-14 11:33:11 -0700714 if (pc_->session()->GetLocalCertificate(
715 transport_iter.second.transport_name, &certificate)) {
Henrik Boströmd8281982015-08-27 10:12:24 +0200716 StatsReport* r = AddCertificateReports(&(certificate->ssl_certificate()));
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000717 if (r)
718 local_cert_report_id = r->id();
719 }
720
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000721 rtc::scoped_ptr<rtc::SSLCertificate> cert;
deadbeefab9b2d12015-10-14 11:33:11 -0700722 if (pc_->session()->GetRemoteSSLCertificate(
723 transport_iter.second.transport_name, cert.accept())) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000724 StatsReport* r = AddCertificateReports(cert.get());
725 if (r)
726 remote_cert_report_id = r->id();
727 }
728
729 for (const auto& channel_iter : transport_iter.second.channel_stats) {
730 StatsReport::Id id(StatsReport::NewComponentId(
deadbeefcbecd352015-09-23 11:50:27 -0700731 transport_iter.second.transport_name, channel_iter.component));
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000732 StatsReport* channel_report = reports_.ReplaceOrAddNew(id);
733 channel_report->set_timestamp(stats_gathering_started_);
734 channel_report->AddInt(StatsReport::kStatsValueNameComponent,
735 channel_iter.component);
736 if (local_cert_report_id.get()) {
737 channel_report->AddId(StatsReport::kStatsValueNameLocalCertificateId,
738 local_cert_report_id);
739 }
740 if (remote_cert_report_id.get()) {
741 channel_report->AddId(StatsReport::kStatsValueNameRemoteCertificateId,
742 remote_cert_report_id);
743 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800744 int srtp_crypto_suite = channel_iter.srtp_crypto_suite;
745 if (srtp_crypto_suite != rtc::SRTP_INVALID_CRYPTO_SUITE &&
746 rtc::SrtpCryptoSuiteToName(srtp_crypto_suite).length()) {
747 channel_report->AddString(
748 StatsReport::kStatsValueNameSrtpCipher,
749 rtc::SrtpCryptoSuiteToName(srtp_crypto_suite));
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000750 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800751 int ssl_cipher_suite = channel_iter.ssl_cipher_suite;
752 if (ssl_cipher_suite != rtc::TLS_NULL_WITH_NULL_NULL &&
753 rtc::SSLStreamAdapter::SslCipherSuiteToName(ssl_cipher_suite)
754 .length()) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -0700755 channel_report->AddString(
756 StatsReport::kStatsValueNameDtlsCipher,
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800757 rtc::SSLStreamAdapter::SslCipherSuiteToName(ssl_cipher_suite));
wu@webrtc.org4551b792013-10-09 15:37:36 +0000758 }
jiayl@webrtc.org06b04ec2014-07-24 20:41:20 +0000759
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000760 int connection_id = 0;
761 for (const cricket::ConnectionInfo& info :
762 channel_iter.connection_infos) {
763 StatsReport* connection_report = AddConnectionInfoReport(
764 transport_iter.first, channel_iter.component, connection_id++,
765 channel_report->id(), info);
766 if (info.best_connection) {
767 channel_report->AddId(
768 StatsReport::kStatsValueNameSelectedCandidatePairId,
769 connection_report->id());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000770 }
771 }
772 }
773 }
774}
775
776void StatsCollector::ExtractVoiceInfo() {
deadbeefab9b2d12015-10-14 11:33:11 -0700777 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000778
deadbeefab9b2d12015-10-14 11:33:11 -0700779 if (!pc_->session()->voice_channel()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000780 return;
781 }
782 cricket::VoiceMediaInfo voice_info;
deadbeefab9b2d12015-10-14 11:33:11 -0700783 if (!pc_->session()->voice_channel()->GetStats(&voice_info)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000784 LOG(LS_ERROR) << "Failed to get voice channel stats.";
785 return;
786 }
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000787
788 // TODO(tommi): The above code should run on the worker thread and post the
789 // results back to the signaling thread, where we can add data to the reports.
790 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
791
deadbeefab9b2d12015-10-14 11:33:11 -0700792 StatsReport::Id transport_id(GetTransportIdFromProxy(
793 proxy_to_transport_, pc_->session()->voice_channel()->content_name()));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000794 if (!transport_id.get()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000795 LOG(LS_ERROR) << "Failed to get transport name for proxy "
deadbeefab9b2d12015-10-14 11:33:11 -0700796 << pc_->session()->voice_channel()->content_name();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000797 return;
798 }
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000799
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000800 ExtractStatsFromList(voice_info.receivers, transport_id, this,
801 StatsReport::kReceive);
802 ExtractStatsFromList(voice_info.senders, transport_id, this,
803 StatsReport::kSend);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000804
805 UpdateStatsFromExistingLocalAudioTracks();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000806}
807
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000808void StatsCollector::ExtractVideoInfo(
809 PeerConnectionInterface::StatsOutputLevel level) {
deadbeefab9b2d12015-10-14 11:33:11 -0700810 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000811
deadbeefab9b2d12015-10-14 11:33:11 -0700812 if (!pc_->session()->video_channel())
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000813 return;
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000814
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000815 cricket::VideoMediaInfo video_info;
deadbeefab9b2d12015-10-14 11:33:11 -0700816 if (!pc_->session()->video_channel()->GetStats(&video_info)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000817 LOG(LS_ERROR) << "Failed to get video channel stats.";
818 return;
819 }
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000820
821 // TODO(tommi): The above code should run on the worker thread and post the
822 // results back to the signaling thread, where we can add data to the reports.
823 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
824
deadbeefab9b2d12015-10-14 11:33:11 -0700825 StatsReport::Id transport_id(GetTransportIdFromProxy(
826 proxy_to_transport_, pc_->session()->video_channel()->content_name()));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000827 if (!transport_id.get()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000828 LOG(LS_ERROR) << "Failed to get transport name for proxy "
deadbeefab9b2d12015-10-14 11:33:11 -0700829 << pc_->session()->video_channel()->content_name();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000830 return;
831 }
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000832 ExtractStatsFromList(video_info.receivers, transport_id, this,
833 StatsReport::kReceive);
834 ExtractStatsFromList(video_info.senders, transport_id, this,
835 StatsReport::kSend);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000836 if (video_info.bw_estimations.size() != 1) {
837 LOG(LS_ERROR) << "BWEs count: " << video_info.bw_estimations.size();
838 } else {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000839 StatsReport::Id report_id(StatsReport::NewBandwidthEstimationId());
840 StatsReport* report = reports_.FindOrAddNew(report_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000841 ExtractStats(
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000842 video_info.bw_estimations[0], stats_gathering_started_, level, report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000843 }
844}
845
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000846void StatsCollector::ExtractDataInfo() {
deadbeefab9b2d12015-10-14 11:33:11 -0700847 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000848
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000849 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
850
deadbeefab9b2d12015-10-14 11:33:11 -0700851 for (const auto& dc : pc_->sctp_data_channels()) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000852 StatsReport::Id id(StatsReport::NewTypedIntId(
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000853 StatsReport::kStatsReportTypeDataChannel, dc->id()));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000854 StatsReport* report = reports_.ReplaceOrAddNew(id);
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000855 report->set_timestamp(stats_gathering_started_);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000856 report->AddString(StatsReport::kStatsValueNameLabel, dc->label());
857 report->AddInt(StatsReport::kStatsValueNameDataChannelId, dc->id());
858 report->AddString(StatsReport::kStatsValueNameProtocol, dc->protocol());
859 report->AddString(StatsReport::kStatsValueNameState,
860 DataChannelInterface::DataStateString(dc->state()));
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000861 }
862}
863
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000864StatsReport* StatsCollector::GetReport(const StatsReport::StatsType& type,
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000865 const std::string& id,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000866 StatsReport::Direction direction) {
deadbeefab9b2d12015-10-14 11:33:11 -0700867 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700868 RTC_DCHECK(type == StatsReport::kStatsReportTypeSsrc ||
869 type == StatsReport::kStatsReportTypeRemoteSsrc);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000870 return reports_.Find(StatsReport::NewIdWithDirection(type, id, direction));
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000871}
872
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000873void StatsCollector::UpdateStatsFromExistingLocalAudioTracks() {
deadbeefab9b2d12015-10-14 11:33:11 -0700874 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000875 // Loop through the existing local audio tracks.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000876 for (const auto& it : local_audio_tracks_) {
877 AudioTrackInterface* track = it.first;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200878 uint32_t ssrc = it.second;
879 StatsReport* report =
880 GetReport(StatsReport::kStatsReportTypeSsrc,
881 rtc::ToString<uint32_t>(ssrc), StatsReport::kSend);
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +0000882 if (report == NULL) {
883 // This can happen if a local audio track is added to a stream on the
884 // fly and the report has not been set up yet. Do nothing in this case.
885 LOG(LS_ERROR) << "Stats report does not exist for ssrc " << ssrc;
886 continue;
887 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000888
889 // The same ssrc can be used by both local and remote audio tracks.
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000890 const StatsReport::Value* v =
891 report->FindValue(StatsReport::kStatsValueNameTrackId);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000892 if (!v || v->string_val() != track->id())
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000893 continue;
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000894
jbauchbe24c942015-06-22 15:06:43 -0700895 report->set_timestamp(stats_gathering_started_);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000896 UpdateReportFromAudioTrack(track, report);
897 }
898}
899
900void StatsCollector::UpdateReportFromAudioTrack(AudioTrackInterface* track,
901 StatsReport* report) {
deadbeefab9b2d12015-10-14 11:33:11 -0700902 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700903 RTC_DCHECK(track != NULL);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000904
andrew2fe1cb02015-11-27 17:27:35 -0800905 // Don't overwrite report values if they're not available.
906 int signal_level;
907 if (track->GetSignalLevel(&signal_level)) {
908 RTC_DCHECK_GE(signal_level, 0);
909 report->AddInt(StatsReport::kStatsValueNameAudioInputLevel, signal_level);
910 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000911
andrew2fe1cb02015-11-27 17:27:35 -0800912 auto audio_processor(track->GetAudioProcessor());
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000913
andrew2fe1cb02015-11-27 17:27:35 -0800914 if (audio_processor.get()) {
915 AudioProcessorInterface::AudioProcessorStats stats;
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000916 audio_processor->GetStats(&stats);
917
andrew2fe1cb02015-11-27 17:27:35 -0800918 SetAudioProcessingStats(
919 report, stats.typing_noise_detected, stats.echo_return_loss,
920 stats.echo_return_loss_enhancement, stats.echo_delay_median_ms,
921 stats.aec_quality_min, stats.echo_delay_std_ms);
922 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000923}
924
Peter Boström0c4e06b2015-10-07 12:23:21 +0200925bool StatsCollector::GetTrackIdBySsrc(uint32_t ssrc,
926 std::string* track_id,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000927 StatsReport::Direction direction) {
deadbeefab9b2d12015-10-14 11:33:11 -0700928 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000929 if (direction == StatsReport::kSend) {
deadbeefab9b2d12015-10-14 11:33:11 -0700930 if (!pc_->session()->GetLocalTrackIdBySsrc(ssrc, track_id)) {
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000931 LOG(LS_WARNING) << "The SSRC " << ssrc
932 << " is not associated with a sending track";
933 return false;
934 }
935 } else {
henrikg91d6ede2015-09-17 00:24:34 -0700936 RTC_DCHECK(direction == StatsReport::kReceive);
deadbeefab9b2d12015-10-14 11:33:11 -0700937 if (!pc_->session()->GetRemoteTrackIdBySsrc(ssrc, track_id)) {
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000938 LOG(LS_WARNING) << "The SSRC " << ssrc
939 << " is not associated with a receiving track";
940 return false;
941 }
942 }
943
944 return true;
945}
946
jbauchbe24c942015-06-22 15:06:43 -0700947void StatsCollector::UpdateTrackReports() {
deadbeefab9b2d12015-10-14 11:33:11 -0700948 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
jbauchbe24c942015-06-22 15:06:43 -0700949
950 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
951
952 for (const auto& entry : track_ids_) {
953 StatsReport* report = entry.second;
954 report->set_timestamp(stats_gathering_started_);
955 }
jbauchbe24c942015-06-22 15:06:43 -0700956}
957
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000958void StatsCollector::ClearUpdateStatsCacheForTest() {
xians@webrtc.org01bda202014-07-09 07:38:38 +0000959 stats_gathering_started_ = 0;
960}
961
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000962} // namespace webrtc