blob: 0458e82cc805462d295459f2ba3559c324f8d2aa [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellanderb24317b2016-02-10 07:54:43 -08002 * Copyright 2012 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellanderb24317b2016-02-10 07:54:43 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
ossu7bb87ee2017-01-23 04:56:25 -080011#include "webrtc/pc/statscollector.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000012
jbauch555604a2016-04-26 03:13:22 -070013#include <memory>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000014#include <utility>
15#include <vector>
16
kjellander@webrtc.org9b8df252016-02-12 06:47:59 +010017#include "webrtc/pc/channel.h"
ossu7bb87ee2017-01-23 04:56:25 -080018#include "webrtc/pc/peerconnection.h"
Edward Lemurc20978e2017-07-06 19:44:34 +020019#include "webrtc/rtc_base/base64.h"
20#include "webrtc/rtc_base/checks.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000021
22namespace webrtc {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000023namespace {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000024
guoweis@webrtc.org950c5182014-12-16 23:01:31 +000025// The following is the enum RTCStatsIceCandidateType from
26// http://w3c.github.io/webrtc-stats/#rtcstatsicecandidatetype-enum such that
27// our stats report for ice candidate type could conform to that.
28const char STATSREPORT_LOCAL_PORT_TYPE[] = "host";
29const char STATSREPORT_STUN_PORT_TYPE[] = "serverreflexive";
30const char STATSREPORT_PRFLX_PORT_TYPE[] = "peerreflexive";
31const char STATSREPORT_RELAY_PORT_TYPE[] = "relayed";
32
33// Strings used by the stats collector to report adapter types. This fits the
34// general stype of http://w3c.github.io/webrtc-stats than what
35// AdapterTypeToString does.
36const char* STATSREPORT_ADAPTER_TYPE_ETHERNET = "lan";
37const char* STATSREPORT_ADAPTER_TYPE_WIFI = "wlan";
38const char* STATSREPORT_ADAPTER_TYPE_WWAN = "wwan";
39const char* STATSREPORT_ADAPTER_TYPE_VPN = "vpn";
phoglund@webrtc.org006521d2015-02-12 09:23:59 +000040const char* STATSREPORT_ADAPTER_TYPE_LOOPBACK = "loopback";
guoweis@webrtc.org950c5182014-12-16 23:01:31 +000041
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000042template<typename ValueType>
43struct TypeForAdd {
tommi@webrtc.org92f40182015-03-04 15:25:19 +000044 const StatsReport::StatsValueName name;
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000045 const ValueType& value;
tommi@webrtc.org92f40182015-03-04 15:25:19 +000046};
47
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000048typedef TypeForAdd<bool> BoolForAdd;
49typedef TypeForAdd<float> FloatForAdd;
Peter Boström0c4e06b2015-10-07 12:23:21 +020050typedef TypeForAdd<int64_t> Int64ForAdd;
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000051typedef TypeForAdd<int> IntForAdd;
tommi@webrtc.org92f40182015-03-04 15:25:19 +000052
deadbeefd59daf82015-10-14 15:02:44 -070053StatsReport::Id GetTransportIdFromProxy(const ProxyTransportMap& map,
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000054 const std::string& proxy) {
henrikg91d6ede2015-09-17 00:24:34 -070055 RTC_DCHECK(!proxy.empty());
deadbeefd59daf82015-10-14 15:02:44 -070056 auto found = map.find(proxy);
57 if (found == map.end()) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000058 return StatsReport::Id();
deadbeefd59daf82015-10-14 15:02:44 -070059 }
tommi@webrtc.org47218952014-07-15 19:22:37 +000060
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000061 return StatsReport::NewComponentId(
62 found->second, cricket::ICE_CANDIDATE_COMPONENT_RTP);
tommi@webrtc.org47218952014-07-15 19:22:37 +000063}
64
jbauchbe24c942015-06-22 15:06:43 -070065StatsReport* AddTrackReport(StatsCollection* reports,
66 const std::string& track_id) {
xians@webrtc.org01bda202014-07-09 07:38:38 +000067 // Adds an empty track report.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000068 StatsReport::Id id(
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +000069 StatsReport::NewTypedId(StatsReport::kStatsReportTypeTrack, track_id));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +000070 StatsReport* report = reports->ReplaceOrAddNew(id);
tommi@webrtc.org92f40182015-03-04 15:25:19 +000071 report->AddString(StatsReport::kStatsValueNameTrackId, track_id);
jbauchbe24c942015-06-22 15:06:43 -070072 return report;
xians@webrtc.org01bda202014-07-09 07:38:38 +000073}
74
henrike@webrtc.org28e20752013-07-10 00:45:36 +000075template <class TrackVector>
jbauchbe24c942015-06-22 15:06:43 -070076void CreateTrackReports(const TrackVector& tracks, StatsCollection* reports,
77 TrackIdMap& track_ids) {
78 for (const auto& track : tracks) {
79 const std::string& track_id = track->id();
80 StatsReport* report = AddTrackReport(reports, track_id);
henrikg91d6ede2015-09-17 00:24:34 -070081 RTC_DCHECK(report != nullptr);
jbauchbe24c942015-06-22 15:06:43 -070082 track_ids[track_id] = report;
83 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +000084}
85
tommi@webrtc.org92f40182015-03-04 15:25:19 +000086void ExtractCommonSendProperties(const cricket::MediaSenderInfo& info,
87 StatsReport* report) {
88 report->AddString(StatsReport::kStatsValueNameCodecName, info.codec_name);
89 report->AddInt64(StatsReport::kStatsValueNameBytesSent, info.bytes_sent);
zhihuang6ba3b192016-05-13 11:46:35 -070090 if (info.rtt_ms >= 0) {
91 report->AddInt64(StatsReport::kStatsValueNameRtt, info.rtt_ms);
92 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +000093}
94
pbosf42376c2015-08-28 07:35:32 -070095void ExtractCommonReceiveProperties(const cricket::MediaReceiverInfo& info,
96 StatsReport* report) {
97 report->AddString(StatsReport::kStatsValueNameCodecName, info.codec_name);
98}
99
andrew2fe1cb02015-11-27 17:27:35 -0800100void SetAudioProcessingStats(StatsReport* report,
101 bool typing_noise_detected,
102 int echo_return_loss,
103 int echo_return_loss_enhancement,
104 int echo_delay_median_ms,
105 float aec_quality_min,
ivoc8c63a822016-10-21 04:10:03 -0700106 int echo_delay_std_ms,
ivoc4e477a12017-01-15 08:29:46 -0800107 float residual_echo_likelihood,
108 float residual_echo_likelihood_recent_max) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000109 report->AddBoolean(StatsReport::kStatsValueNameTypingNoiseState,
110 typing_noise_detected);
zhihuang6ba3b192016-05-13 11:46:35 -0700111 if (aec_quality_min >= 0.0f) {
112 report->AddFloat(StatsReport::kStatsValueNameEchoCancellationQualityMin,
113 aec_quality_min);
114 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000115 const IntForAdd ints[] = {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000116 { StatsReport::kStatsValueNameEchoDelayMedian, echo_delay_median_ms },
117 { StatsReport::kStatsValueNameEchoDelayStdDev, echo_delay_std_ms },
118 };
zhihuang6ba3b192016-05-13 11:46:35 -0700119 for (const auto& i : ints) {
120 if (i.value >= 0) {
121 report->AddInt(i.name, i.value);
122 }
123 }
124 // These can take on valid negative values.
125 report->AddInt(StatsReport::kStatsValueNameEchoReturnLoss, echo_return_loss);
126 report->AddInt(StatsReport::kStatsValueNameEchoReturnLossEnhancement,
127 echo_return_loss_enhancement);
ivoc8c63a822016-10-21 04:10:03 -0700128 if (residual_echo_likelihood >= 0.0f) {
129 report->AddFloat(StatsReport::kStatsValueNameResidualEchoLikelihood,
130 residual_echo_likelihood);
henrik.lundin04a057b2017-01-16 23:53:59 -0800131 }
132 if (residual_echo_likelihood_recent_max >= 0.0f) {
ivoc4e477a12017-01-15 08:29:46 -0800133 report->AddFloat(
134 StatsReport::kStatsValueNameResidualEchoLikelihoodRecentMax,
135 residual_echo_likelihood_recent_max);
ivoc8c63a822016-10-21 04:10:03 -0700136 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000137}
138
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000139void ExtractStats(const cricket::VoiceReceiverInfo& info, StatsReport* report) {
pbosf42376c2015-08-28 07:35:32 -0700140 ExtractCommonReceiveProperties(info, report);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000141 const FloatForAdd floats[] = {
142 { StatsReport::kStatsValueNameExpandRate, info.expand_rate },
143 { StatsReport::kStatsValueNameSecondaryDecodedRate,
144 info.secondary_decoded_rate },
minyue-webrtc0e320ec2017-08-28 13:51:27 +0200145 { StatsReport::kStatsValueNameSecondaryDiscardedRate,
146 info.secondary_discarded_rate },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000147 { StatsReport::kStatsValueNameSpeechExpandRate, info.speech_expand_rate },
Henrik Lundin8e6fd462015-06-02 09:24:52 +0200148 { StatsReport::kStatsValueNameAccelerateRate, info.accelerate_rate },
149 { StatsReport::kStatsValueNamePreemptiveExpandRate,
150 info.preemptive_expand_rate },
zsteine76bd3a2017-07-14 12:17:49 -0700151 { StatsReport::kStatsValueNameTotalAudioEnergy, info.total_output_energy },
152 { StatsReport::kStatsValueNameTotalSamplesDuration,
153 info.total_output_duration }
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000154 };
155
156 const IntForAdd ints[] = {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000157 { StatsReport::kStatsValueNameCurrentDelayMs, info.delay_estimate_ms },
158 { StatsReport::kStatsValueNameDecodingCNG, info.decoding_cng },
159 { StatsReport::kStatsValueNameDecodingCTN, info.decoding_calls_to_neteq },
160 { StatsReport::kStatsValueNameDecodingCTSG,
161 info.decoding_calls_to_silence_generator },
henrik.lundin63489782016-09-20 01:47:12 -0700162 { StatsReport::kStatsValueNameDecodingMutedOutput,
163 info.decoding_muted_output },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000164 { StatsReport::kStatsValueNameDecodingNormal, info.decoding_normal },
165 { StatsReport::kStatsValueNameDecodingPLC, info.decoding_plc },
166 { StatsReport::kStatsValueNameDecodingPLCCNG, info.decoding_plc_cng },
167 { StatsReport::kStatsValueNameJitterBufferMs, info.jitter_buffer_ms },
168 { StatsReport::kStatsValueNameJitterReceived, info.jitter_ms },
169 { StatsReport::kStatsValueNamePacketsLost, info.packets_lost },
170 { StatsReport::kStatsValueNamePacketsReceived, info.packets_rcvd },
171 { StatsReport::kStatsValueNamePreferredJitterBufferMs,
172 info.jitter_buffer_preferred_ms },
173 };
174
175 for (const auto& f : floats)
176 report->AddFloat(f.name, f.value);
177
178 for (const auto& i : ints)
179 report->AddInt(i.name, i.value);
zhihuang6ba3b192016-05-13 11:46:35 -0700180 if (info.audio_level >= 0) {
181 report->AddInt(StatsReport::kStatsValueNameAudioOutputLevel,
182 info.audio_level);
183 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000184
185 report->AddInt64(StatsReport::kStatsValueNameBytesReceived,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000186 info.bytes_rcvd);
zhihuang6ba3b192016-05-13 11:46:35 -0700187 if (info.capture_start_ntp_time_ms >= 0) {
188 report->AddInt64(StatsReport::kStatsValueNameCaptureStartNtpTimeMs,
189 info.capture_start_ntp_time_ms);
190 }
fippobec70ab2016-01-28 01:27:15 -0800191 report->AddString(StatsReport::kStatsValueNameMediaType, "audio");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000192}
193
194void ExtractStats(const cricket::VoiceSenderInfo& info, StatsReport* report) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000195 ExtractCommonSendProperties(info, report);
196
andrew2fe1cb02015-11-27 17:27:35 -0800197 SetAudioProcessingStats(
198 report, info.typing_noise_detected, info.echo_return_loss,
199 info.echo_return_loss_enhancement, info.echo_delay_median_ms,
ivoc8c63a822016-10-21 04:10:03 -0700200 info.aec_quality_min, info.echo_delay_std_ms,
ivoc4e477a12017-01-15 08:29:46 -0800201 info.residual_echo_likelihood, info.residual_echo_likelihood_recent_max);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000202
zsteine76bd3a2017-07-14 12:17:49 -0700203 const FloatForAdd floats[] = {
204 { StatsReport::kStatsValueNameTotalAudioEnergy, info.total_input_energy },
205 { StatsReport::kStatsValueNameTotalSamplesDuration,
206 info.total_input_duration }
207 };
208
andrew2fe1cb02015-11-27 17:27:35 -0800209 RTC_DCHECK_GE(info.audio_level, 0);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000210 const IntForAdd ints[] = {
andrew2fe1cb02015-11-27 17:27:35 -0800211 { StatsReport::kStatsValueNameAudioInputLevel, info.audio_level},
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000212 { StatsReport::kStatsValueNameJitterReceived, info.jitter_ms },
213 { StatsReport::kStatsValueNamePacketsLost, info.packets_lost },
214 { StatsReport::kStatsValueNamePacketsSent, info.packets_sent },
215 };
216
zsteine76bd3a2017-07-14 12:17:49 -0700217 for (const auto& f : floats) {
218 report->AddFloat(f.name, f.value);
219 }
220
zhihuang6ba3b192016-05-13 11:46:35 -0700221 for (const auto& i : ints) {
222 if (i.value >= 0) {
223 report->AddInt(i.name, i.value);
224 }
225 }
fippobec70ab2016-01-28 01:27:15 -0800226 report->AddString(StatsReport::kStatsValueNameMediaType, "audio");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000227}
228
229void ExtractStats(const cricket::VideoReceiverInfo& info, StatsReport* report) {
pbosf42376c2015-08-28 07:35:32 -0700230 ExtractCommonReceiveProperties(info, report);
Peter Boströmb7d9a972015-12-18 16:01:11 +0100231 report->AddString(StatsReport::kStatsValueNameCodecImplementationName,
232 info.decoder_implementation_name);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000233 report->AddInt64(StatsReport::kStatsValueNameBytesReceived,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000234 info.bytes_rcvd);
zhihuang6ba3b192016-05-13 11:46:35 -0700235 if (info.capture_start_ntp_time_ms >= 0) {
236 report->AddInt64(StatsReport::kStatsValueNameCaptureStartNtpTimeMs,
237 info.capture_start_ntp_time_ms);
238 }
sakalcc452e12017-02-09 04:53:45 -0800239 if (info.qp_sum)
240 report->AddInt64(StatsReport::kStatsValueNameQpSum, *info.qp_sum);
241
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000242 const IntForAdd ints[] = {
243 { StatsReport::kStatsValueNameCurrentDelayMs, info.current_delay_ms },
244 { StatsReport::kStatsValueNameDecodeMs, info.decode_ms },
245 { StatsReport::kStatsValueNameFirsSent, info.firs_sent },
246 { StatsReport::kStatsValueNameFrameHeightReceived, info.frame_height },
247 { StatsReport::kStatsValueNameFrameRateDecoded, info.framerate_decoded },
248 { StatsReport::kStatsValueNameFrameRateOutput, info.framerate_output },
249 { StatsReport::kStatsValueNameFrameRateReceived, info.framerate_rcvd },
250 { StatsReport::kStatsValueNameFrameWidthReceived, info.frame_width },
251 { StatsReport::kStatsValueNameJitterBufferMs, info.jitter_buffer_ms },
252 { StatsReport::kStatsValueNameMaxDecodeMs, info.max_decode_ms },
253 { StatsReport::kStatsValueNameMinPlayoutDelayMs,
254 info.min_playout_delay_ms },
255 { StatsReport::kStatsValueNameNacksSent, info.nacks_sent },
256 { StatsReport::kStatsValueNamePacketsLost, info.packets_lost },
257 { StatsReport::kStatsValueNamePacketsReceived, info.packets_rcvd },
258 { StatsReport::kStatsValueNamePlisSent, info.plis_sent },
259 { StatsReport::kStatsValueNameRenderDelayMs, info.render_delay_ms },
260 { StatsReport::kStatsValueNameTargetDelayMs, info.target_delay_ms },
sakale5ba44e2016-10-26 07:09:24 -0700261 { StatsReport::kStatsValueNameFramesDecoded, info.frames_decoded },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000262 };
263
264 for (const auto& i : ints)
265 report->AddInt(i.name, i.value);
fippobec70ab2016-01-28 01:27:15 -0800266 report->AddString(StatsReport::kStatsValueNameMediaType, "video");
ilnikf04afde2017-07-07 01:26:24 -0700267
ilnik2edc6842017-07-06 03:06:50 -0700268 if (info.timing_frame_info) {
269 report->AddString(StatsReport::kStatsValueNameTimingFrameInfo,
270 info.timing_frame_info->ToString());
271 }
ilnikf04afde2017-07-07 01:26:24 -0700272
ilnika79cc282017-08-23 05:24:10 -0700273 report->AddInt64(StatsReport::kStatsValueNameInterframeDelayMaxMs,
274 info.interframe_delay_max_ms);
ilnik2e1b40b2017-09-04 07:57:17 -0700275
276 report->AddString(
277 StatsReport::kStatsValueNameContentType,
278 webrtc::videocontenttypehelpers::ToString(info.content_type));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000279}
280
281void ExtractStats(const cricket::VideoSenderInfo& info, StatsReport* report) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000282 ExtractCommonSendProperties(info, report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000283
Peter Boströmb7d9a972015-12-18 16:01:11 +0100284 report->AddString(StatsReport::kStatsValueNameCodecImplementationName,
285 info.encoder_implementation_name);
mallinath@webrtc.org62451dc2013-12-13 12:29:34 +0000286 report->AddBoolean(StatsReport::kStatsValueNameBandwidthLimitedResolution,
287 (info.adapt_reason & 0x2) > 0);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000288 report->AddBoolean(StatsReport::kStatsValueNameCpuLimitedResolution,
289 (info.adapt_reason & 0x1) > 0);
sakal87da4042016-10-31 06:53:47 -0700290 if (info.qp_sum)
291 report->AddInt(StatsReport::kStatsValueNameQpSum, *info.qp_sum);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000292
293 const IntForAdd ints[] = {
294 { StatsReport::kStatsValueNameAdaptationChanges, info.adapt_changes },
295 { StatsReport::kStatsValueNameAvgEncodeMs, info.avg_encode_ms },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000296 { StatsReport::kStatsValueNameEncodeUsagePercent,
297 info.encode_usage_percent },
298 { StatsReport::kStatsValueNameFirsReceived, info.firs_rcvd },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000299 { StatsReport::kStatsValueNameFrameHeightSent, info.send_frame_height },
300 { StatsReport::kStatsValueNameFrameRateInput, info.framerate_input },
301 { StatsReport::kStatsValueNameFrameRateSent, info.framerate_sent },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000302 { StatsReport::kStatsValueNameFrameWidthSent, info.send_frame_width },
303 { StatsReport::kStatsValueNameNacksReceived, info.nacks_rcvd },
304 { StatsReport::kStatsValueNamePacketsLost, info.packets_lost },
305 { StatsReport::kStatsValueNamePacketsSent, info.packets_sent },
306 { StatsReport::kStatsValueNamePlisReceived, info.plis_rcvd },
sakal43536c32016-10-24 01:46:43 -0700307 { StatsReport::kStatsValueNameFramesEncoded, info.frames_encoded },
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000308 };
309
310 for (const auto& i : ints)
311 report->AddInt(i.name, i.value);
fippobec70ab2016-01-28 01:27:15 -0800312 report->AddString(StatsReport::kStatsValueNameMediaType, "video");
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000313}
314
315void ExtractStats(const cricket::BandwidthEstimationInfo& info,
316 double stats_gathering_started,
317 StatsReport* report) {
henrikg91d6ede2015-09-17 00:24:34 -0700318 RTC_DCHECK(report->type() == StatsReport::kStatsReportTypeBwe);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000319
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000320 report->set_timestamp(stats_gathering_started);
321 const IntForAdd ints[] = {
322 { StatsReport::kStatsValueNameAvailableSendBandwidth,
323 info.available_send_bandwidth },
324 { StatsReport::kStatsValueNameAvailableReceiveBandwidth,
325 info.available_recv_bandwidth },
326 { StatsReport::kStatsValueNameTargetEncBitrate, info.target_enc_bitrate },
327 { StatsReport::kStatsValueNameActualEncBitrate, info.actual_enc_bitrate },
328 { StatsReport::kStatsValueNameRetransmitBitrate, info.retransmit_bitrate },
329 { StatsReport::kStatsValueNameTransmitBitrate, info.transmit_bitrate },
330 };
331 for (const auto& i : ints)
332 report->AddInt(i.name, i.value);
333 report->AddInt64(StatsReport::kStatsValueNameBucketDelay, info.bucket_delay);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000334}
335
wu@webrtc.org97077a32013-10-25 21:18:33 +0000336void ExtractRemoteStats(const cricket::MediaSenderInfo& info,
337 StatsReport* report) {
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000338 report->set_timestamp(info.remote_stats[0].timestamp);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000339 // TODO(hta): Extract some stats here.
340}
341
342void ExtractRemoteStats(const cricket::MediaReceiverInfo& info,
343 StatsReport* report) {
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000344 report->set_timestamp(info.remote_stats[0].timestamp);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000345 // TODO(hta): Extract some stats here.
346}
347
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000348// Template to extract stats from a data vector.
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000349// In order to use the template, the functions that are called from it,
350// ExtractStats and ExtractRemoteStats, must be defined and overloaded
351// for each type.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000352template<typename T>
353void ExtractStatsFromList(const std::vector<T>& data,
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000354 const StatsReport::Id& transport_id,
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000355 StatsCollector* collector,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000356 StatsReport::Direction direction) {
357 for (const auto& d : data) {
Peter Boström0c4e06b2015-10-07 12:23:21 +0200358 uint32_t ssrc = d.ssrc();
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000359 // Each track can have stats for both local and remote objects.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000360 // TODO(hta): Handle the case of multiple SSRCs per object.
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000361 StatsReport* report = collector->PrepareReport(true, ssrc, transport_id,
362 direction);
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000363 if (report)
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000364 ExtractStats(d, report);
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000365
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000366 if (!d.remote_stats.empty()) {
367 report = collector->PrepareReport(false, ssrc, transport_id, direction);
368 if (report)
369 ExtractRemoteStats(d, report);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000370 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000371 }
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000372}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000373
374} // namespace
375
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000376const char* IceCandidateTypeToStatsType(const std::string& candidate_type) {
377 if (candidate_type == cricket::LOCAL_PORT_TYPE) {
378 return STATSREPORT_LOCAL_PORT_TYPE;
379 }
380 if (candidate_type == cricket::STUN_PORT_TYPE) {
381 return STATSREPORT_STUN_PORT_TYPE;
382 }
383 if (candidate_type == cricket::PRFLX_PORT_TYPE) {
384 return STATSREPORT_PRFLX_PORT_TYPE;
385 }
386 if (candidate_type == cricket::RELAY_PORT_TYPE) {
387 return STATSREPORT_RELAY_PORT_TYPE;
388 }
nisseeb4ca4e2017-01-12 02:24:27 -0800389 RTC_NOTREACHED();
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000390 return "unknown";
391}
392
393const char* AdapterTypeToStatsType(rtc::AdapterType type) {
394 switch (type) {
395 case rtc::ADAPTER_TYPE_UNKNOWN:
396 return "unknown";
397 case rtc::ADAPTER_TYPE_ETHERNET:
398 return STATSREPORT_ADAPTER_TYPE_ETHERNET;
399 case rtc::ADAPTER_TYPE_WIFI:
400 return STATSREPORT_ADAPTER_TYPE_WIFI;
401 case rtc::ADAPTER_TYPE_CELLULAR:
402 return STATSREPORT_ADAPTER_TYPE_WWAN;
403 case rtc::ADAPTER_TYPE_VPN:
404 return STATSREPORT_ADAPTER_TYPE_VPN;
phoglund@webrtc.org006521d2015-02-12 09:23:59 +0000405 case rtc::ADAPTER_TYPE_LOOPBACK:
406 return STATSREPORT_ADAPTER_TYPE_LOOPBACK;
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000407 default:
nisseeb4ca4e2017-01-12 02:24:27 -0800408 RTC_NOTREACHED();
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000409 return "";
410 }
411}
412
deadbeefab9b2d12015-10-14 11:33:11 -0700413StatsCollector::StatsCollector(PeerConnection* pc)
414 : pc_(pc), stats_gathering_started_(0) {
415 RTC_DCHECK(pc_);
tommi@webrtc.org03505bc2014-07-14 20:15:26 +0000416}
417
418StatsCollector::~StatsCollector() {
deadbeefab9b2d12015-10-14 11:33:11 -0700419 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000420}
421
nissecdf37a92016-09-13 23:41:47 -0700422// Wallclock time in ms.
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000423double StatsCollector::GetTimeNow() {
nissecdf37a92016-09-13 23:41:47 -0700424 return rtc::TimeUTCMicros() /
425 static_cast<double>(rtc::kNumMicrosecsPerMillisec);
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000426}
427
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000428// Adds a MediaStream with tracks that can be used as a |selector| in a call
429// to GetStats.
430void StatsCollector::AddStream(MediaStreamInterface* stream) {
deadbeefab9b2d12015-10-14 11:33:11 -0700431 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700432 RTC_DCHECK(stream != NULL);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000433
434 CreateTrackReports<AudioTrackVector>(stream->GetAudioTracks(),
jbauchbe24c942015-06-22 15:06:43 -0700435 &reports_, track_ids_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000436 CreateTrackReports<VideoTrackVector>(stream->GetVideoTracks(),
jbauchbe24c942015-06-22 15:06:43 -0700437 &reports_, track_ids_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000438}
439
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000440void StatsCollector::AddLocalAudioTrack(AudioTrackInterface* audio_track,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200441 uint32_t ssrc) {
deadbeefab9b2d12015-10-14 11:33:11 -0700442 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700443 RTC_DCHECK(audio_track != NULL);
kwiberg5377bc72016-10-04 13:46:56 -0700444#if RTC_DCHECK_IS_ON
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000445 for (const auto& track : local_audio_tracks_)
henrikg91d6ede2015-09-17 00:24:34 -0700446 RTC_DCHECK(track.first != audio_track || track.second != ssrc);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000447#endif
xians@webrtc.org01bda202014-07-09 07:38:38 +0000448
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000449 local_audio_tracks_.push_back(std::make_pair(audio_track, ssrc));
xians@webrtc.org01bda202014-07-09 07:38:38 +0000450
451 // Create the kStatsReportTypeTrack report for the new track if there is no
452 // report yet.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000453 StatsReport::Id id(StatsReport::NewTypedId(StatsReport::kStatsReportTypeTrack,
454 audio_track->id()));
455 StatsReport* report = reports_.Find(id);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000456 if (!report) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000457 report = reports_.InsertNew(id);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000458 report->AddString(StatsReport::kStatsValueNameTrackId, audio_track->id());
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000459 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000460}
461
462void StatsCollector::RemoveLocalAudioTrack(AudioTrackInterface* audio_track,
Peter Boström0c4e06b2015-10-07 12:23:21 +0200463 uint32_t ssrc) {
henrikg91d6ede2015-09-17 00:24:34 -0700464 RTC_DCHECK(audio_track != NULL);
deadbeef19b3a552017-06-16 20:19:08 -0700465 local_audio_tracks_.erase(
466 std::remove_if(
467 local_audio_tracks_.begin(), local_audio_tracks_.end(),
468 [audio_track, ssrc](const LocalAudioTrackVector::value_type& track) {
469 return track.first == audio_track && track.second == ssrc;
470 }),
471 local_audio_tracks_.end());
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000472}
473
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000474void StatsCollector::GetStats(MediaStreamTrackInterface* track,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000475 StatsReports* reports) {
deadbeefab9b2d12015-10-14 11:33:11 -0700476 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700477 RTC_DCHECK(reports != NULL);
478 RTC_DCHECK(reports->empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000479
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000480 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
481
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000482 if (!track) {
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000483 reports->reserve(reports_.size());
484 for (auto* r : reports_)
485 reports->push_back(r);
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000486 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000487 }
488
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000489 StatsReport* report = reports_.Find(StatsReport::NewTypedId(
deadbeefab9b2d12015-10-14 11:33:11 -0700490 StatsReport::kStatsReportTypeSession, pc_->session()->id()));
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000491 if (report)
492 reports->push_back(report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000493
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000494 report = reports_.Find(StatsReport::NewTypedId(
495 StatsReport::kStatsReportTypeTrack, track->id()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000496
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000497 if (!report)
498 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000499
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000500 reports->push_back(report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000501
502 std::string track_id;
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000503 for (const auto* r : reports_) {
504 if (r->type() != StatsReport::kStatsReportTypeSsrc)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000505 continue;
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000506
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000507 const StatsReport::Value* v =
508 r->FindValue(StatsReport::kStatsValueNameTrackId);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000509 if (v && v->string_val() == track->id())
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000510 reports->push_back(r);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000511 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000512}
513
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000514void
515StatsCollector::UpdateStats(PeerConnectionInterface::StatsOutputLevel level) {
deadbeefab9b2d12015-10-14 11:33:11 -0700516 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000517 double time_now = GetTimeNow();
518 // Calls to UpdateStats() that occur less than kMinGatherStatsPeriod number of
519 // ms apart will be ignored.
520 const double kMinGatherStatsPeriod = 50;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000521 if (stats_gathering_started_ != 0 &&
522 stats_gathering_started_ + kMinGatherStatsPeriod > time_now) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000523 return;
524 }
525 stats_gathering_started_ = time_now;
526
solenberg03d6d572016-03-01 12:42:03 -0800527 // TODO(pthatcher): Merge PeerConnection and WebRtcSession so there is no
528 // pc_->session().
deadbeefab9b2d12015-10-14 11:33:11 -0700529 if (pc_->session()) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000530 // TODO(tommi): All of these hop over to the worker thread to fetch
531 // information. We could use an AsyncInvoker to run all of these and post
532 // the information back to the signaling thread where we can create and
533 // update stats reports. That would also clean up the threading story a bit
534 // since we'd be creating/updating the stats report objects consistently on
535 // the same thread (this class has no locks right now).
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000536 ExtractSessionInfo();
stefanf79ade12017-06-02 06:44:03 -0700537 ExtractBweInfo();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000538 ExtractVoiceInfo();
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000539 ExtractVideoInfo(level);
nissefcc640f2016-04-01 01:10:42 -0700540 ExtractSenderInfo();
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000541 ExtractDataInfo();
jbauchbe24c942015-06-22 15:06:43 -0700542 UpdateTrackReports();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000543 }
544}
545
deadbeefab9b2d12015-10-14 11:33:11 -0700546StatsReport* StatsCollector::PrepareReport(
547 bool local,
548 uint32_t ssrc,
549 const StatsReport::Id& transport_id,
550 StatsReport::Direction direction) {
551 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000552 StatsReport::Id id(StatsReport::NewIdWithDirection(
Peter Boström0c4e06b2015-10-07 12:23:21 +0200553 local ? StatsReport::kStatsReportTypeSsrc
554 : StatsReport::kStatsReportTypeRemoteSsrc,
555 rtc::ToString<uint32_t>(ssrc), direction));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000556 StatsReport* report = reports_.Find(id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000557
xians@webrtc.org01bda202014-07-09 07:38:38 +0000558 // Use the ID of the track that is currently mapped to the SSRC, if any.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000559 std::string track_id;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000560 if (!GetTrackIdBySsrc(ssrc, &track_id, direction)) {
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000561 if (!report) {
xians@webrtc.org01bda202014-07-09 07:38:38 +0000562 // The ssrc is not used by any track or existing report, return NULL
563 // in such case to indicate no report is prepared for the ssrc.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000564 return NULL;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000565 }
566
567 // The ssrc is not used by any existing track. Keeps the old track id
568 // since we want to report the stats for inactive ssrc.
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000569 const StatsReport::Value* v =
570 report->FindValue(StatsReport::kStatsValueNameTrackId);
571 if (v)
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000572 track_id = v->string_val();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000573 }
574
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000575 if (!report)
576 report = reports_.InsertNew(id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000577
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000578 // FYI - for remote reports, the timestamp will be overwritten later.
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000579 report->set_timestamp(stats_gathering_started_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000580
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000581 report->AddInt64(StatsReport::kStatsValueNameSsrc, ssrc);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000582 report->AddString(StatsReport::kStatsValueNameTrackId, track_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000583 // Add the mapping of SSRC to transport.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000584 report->AddId(StatsReport::kStatsValueNameTransportId, transport_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000585 return report;
586}
587
zhihuange9e94c32016-11-04 11:38:15 -0700588bool StatsCollector::IsValidTrack(const std::string& track_id) {
589 return reports_.Find(StatsReport::NewTypedId(
590 StatsReport::kStatsReportTypeTrack, track_id)) != nullptr;
591}
592
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000593StatsReport* StatsCollector::AddCertificateReports(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000594 const rtc::SSLCertificate* cert) {
deadbeefab9b2d12015-10-14 11:33:11 -0700595 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700596 RTC_DCHECK(cert != NULL);
wu@webrtc.org4551b792013-10-09 15:37:36 +0000597
hbose29352b2016-08-25 03:52:38 -0700598 std::unique_ptr<rtc::SSLCertificateStats> first_stats = cert->GetStats();
599 StatsReport* first_report = nullptr;
600 StatsReport* prev_report = nullptr;
601 for (rtc::SSLCertificateStats* stats = first_stats.get(); stats;
602 stats = stats->issuer.get()) {
603 StatsReport::Id id(StatsReport::NewTypedId(
604 StatsReport::kStatsReportTypeCertificate, stats->fingerprint));
605
606 StatsReport* report = reports_.ReplaceOrAddNew(id);
607 report->set_timestamp(stats_gathering_started_);
608 report->AddString(StatsReport::kStatsValueNameFingerprint,
609 stats->fingerprint);
610 report->AddString(StatsReport::kStatsValueNameFingerprintAlgorithm,
611 stats->fingerprint_algorithm);
612 report->AddString(StatsReport::kStatsValueNameDer,
613 stats->base64_certificate);
614 if (!first_report)
615 first_report = report;
616 else
617 prev_report->AddId(StatsReport::kStatsValueNameIssuerId, id);
618 prev_report = report;
wu@webrtc.org4551b792013-10-09 15:37:36 +0000619 }
hbose29352b2016-08-25 03:52:38 -0700620 return first_report;
wu@webrtc.org4551b792013-10-09 15:37:36 +0000621}
622
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000623StatsReport* StatsCollector::AddConnectionInfoReport(
624 const std::string& content_name, int component, int connection_id,
625 const StatsReport::Id& channel_report_id,
626 const cricket::ConnectionInfo& info) {
627 StatsReport::Id id(StatsReport::NewCandidatePairId(content_name, component,
628 connection_id));
629 StatsReport* report = reports_.ReplaceOrAddNew(id);
630 report->set_timestamp(stats_gathering_started_);
631
632 const BoolForAdd bools[] = {
Peter Thatcher04ac81f2015-09-21 11:48:28 -0700633 {StatsReport::kStatsValueNameActiveConnection, info.best_connection},
634 {StatsReport::kStatsValueNameReceiving, info.receiving},
635 {StatsReport::kStatsValueNameWritable, info.writable},
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000636 };
637 for (const auto& b : bools)
638 report->AddBoolean(b.name, b.value);
639
640 report->AddId(StatsReport::kStatsValueNameChannelId, channel_report_id);
641 report->AddId(StatsReport::kStatsValueNameLocalCandidateId,
642 AddCandidateReport(info.local_candidate, true)->id());
643 report->AddId(StatsReport::kStatsValueNameRemoteCandidateId,
644 AddCandidateReport(info.remote_candidate, false)->id());
645
646 const Int64ForAdd int64s[] = {
zhihuang5ecf16c2016-06-01 17:09:15 -0700647 {StatsReport::kStatsValueNameBytesReceived, info.recv_total_bytes},
648 {StatsReport::kStatsValueNameBytesSent, info.sent_total_bytes},
649 {StatsReport::kStatsValueNamePacketsSent, info.sent_total_packets},
650 {StatsReport::kStatsValueNameRtt, info.rtt},
651 {StatsReport::kStatsValueNameSendPacketsDiscarded,
652 info.sent_discarded_packets},
653 {StatsReport::kStatsValueNameSentPingRequestsTotal,
654 info.sent_ping_requests_total},
655 {StatsReport::kStatsValueNameSentPingRequestsBeforeFirstResponse,
656 info.sent_ping_requests_before_first_response},
657 {StatsReport::kStatsValueNameSentPingResponses, info.sent_ping_responses},
658 {StatsReport::kStatsValueNameRecvPingRequests, info.recv_ping_requests},
659 {StatsReport::kStatsValueNameRecvPingResponses, info.recv_ping_responses},
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000660 };
661 for (const auto& i : int64s)
662 report->AddInt64(i.name, i.value);
663
664 report->AddString(StatsReport::kStatsValueNameLocalAddress,
665 info.local_candidate.address().ToString());
666 report->AddString(StatsReport::kStatsValueNameLocalCandidateType,
667 info.local_candidate.type());
668 report->AddString(StatsReport::kStatsValueNameRemoteAddress,
669 info.remote_candidate.address().ToString());
670 report->AddString(StatsReport::kStatsValueNameRemoteCandidateType,
671 info.remote_candidate.type());
672 report->AddString(StatsReport::kStatsValueNameTransportType,
673 info.local_candidate.protocol());
674
675 return report;
676}
677
678StatsReport* StatsCollector::AddCandidateReport(
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000679 const cricket::Candidate& candidate,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000680 bool local) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000681 StatsReport::Id id(StatsReport::NewCandidateId(local, candidate.id()));
682 StatsReport* report = reports_.Find(id);
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000683 if (!report) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000684 report = reports_.InsertNew(id);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000685 report->set_timestamp(stats_gathering_started_);
686 if (local) {
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000687 report->AddString(StatsReport::kStatsValueNameCandidateNetworkType,
688 AdapterTypeToStatsType(candidate.network_type()));
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000689 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000690 report->AddString(StatsReport::kStatsValueNameCandidateIPAddress,
691 candidate.address().ipaddr().ToString());
692 report->AddString(StatsReport::kStatsValueNameCandidatePortNumber,
693 candidate.address().PortAsString());
694 report->AddInt(StatsReport::kStatsValueNameCandidatePriority,
695 candidate.priority());
696 report->AddString(StatsReport::kStatsValueNameCandidateType,
697 IceCandidateTypeToStatsType(candidate.type()));
698 report->AddString(StatsReport::kStatsValueNameCandidateTransportType,
699 candidate.protocol());
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000700 }
701
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000702 return report;
guoweis@webrtc.org950c5182014-12-16 23:01:31 +0000703}
704
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000705void StatsCollector::ExtractSessionInfo() {
deadbeefab9b2d12015-10-14 11:33:11 -0700706 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000707
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000708 // Extract information from the base session.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000709 StatsReport::Id id(StatsReport::NewTypedId(
deadbeefab9b2d12015-10-14 11:33:11 -0700710 StatsReport::kStatsReportTypeSession, pc_->session()->id()));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000711 StatsReport* report = reports_.ReplaceOrAddNew(id);
tommi@webrtc.org8e327c42015-01-19 20:41:26 +0000712 report->set_timestamp(stats_gathering_started_);
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000713 report->AddBoolean(StatsReport::kStatsValueNameInitiator,
deadbeefd59daf82015-10-14 15:02:44 -0700714 pc_->session()->initial_offerer());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000715
hbosdf6075a2016-12-19 04:58:02 -0800716 std::unique_ptr<SessionStats> stats = pc_->session()->GetStats_s();
717 if (!stats) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000718 return;
719 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000720
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000721 // Store the proxy map away for use in SSRC reporting.
722 // TODO(tommi): This shouldn't be necessary if we post the stats back to the
723 // signaling thread after fetching them on the worker thread, then just use
724 // the proxy map directly from the session stats.
725 // As is, if GetStats() failed, we could be using old (incorrect?) proxy
726 // data.
hbosdf6075a2016-12-19 04:58:02 -0800727 proxy_to_transport_ = stats->proxy_to_transport;
jiayl@webrtc.org06b04ec2014-07-24 20:41:20 +0000728
hbosdf6075a2016-12-19 04:58:02 -0800729 for (const auto& transport_iter : stats->transport_stats) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000730 // Attempt to get a copy of the certificates from the transport and
731 // expose them in stats reports. All channels in a transport share the
732 // same local and remote certificates.
733 //
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000734 StatsReport::Id local_cert_report_id, remote_cert_report_id;
Henrik Boströmd8281982015-08-27 10:12:24 +0200735 rtc::scoped_refptr<rtc::RTCCertificate> certificate;
deadbeefab9b2d12015-10-14 11:33:11 -0700736 if (pc_->session()->GetLocalCertificate(
737 transport_iter.second.transport_name, &certificate)) {
Henrik Boströmd8281982015-08-27 10:12:24 +0200738 StatsReport* r = AddCertificateReports(&(certificate->ssl_certificate()));
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000739 if (r)
740 local_cert_report_id = r->id();
741 }
742
jbauch555604a2016-04-26 03:13:22 -0700743 std::unique_ptr<rtc::SSLCertificate> cert =
kwibergb4d01c42016-04-06 05:15:06 -0700744 pc_->session()->GetRemoteSSLCertificate(
745 transport_iter.second.transport_name);
746 if (cert) {
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000747 StatsReport* r = AddCertificateReports(cert.get());
748 if (r)
749 remote_cert_report_id = r->id();
750 }
751
752 for (const auto& channel_iter : transport_iter.second.channel_stats) {
753 StatsReport::Id id(StatsReport::NewComponentId(
deadbeefcbecd352015-09-23 11:50:27 -0700754 transport_iter.second.transport_name, channel_iter.component));
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000755 StatsReport* channel_report = reports_.ReplaceOrAddNew(id);
756 channel_report->set_timestamp(stats_gathering_started_);
757 channel_report->AddInt(StatsReport::kStatsValueNameComponent,
758 channel_iter.component);
759 if (local_cert_report_id.get()) {
760 channel_report->AddId(StatsReport::kStatsValueNameLocalCertificateId,
761 local_cert_report_id);
762 }
763 if (remote_cert_report_id.get()) {
764 channel_report->AddId(StatsReport::kStatsValueNameRemoteCertificateId,
765 remote_cert_report_id);
766 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800767 int srtp_crypto_suite = channel_iter.srtp_crypto_suite;
768 if (srtp_crypto_suite != rtc::SRTP_INVALID_CRYPTO_SUITE &&
769 rtc::SrtpCryptoSuiteToName(srtp_crypto_suite).length()) {
770 channel_report->AddString(
771 StatsReport::kStatsValueNameSrtpCipher,
772 rtc::SrtpCryptoSuiteToName(srtp_crypto_suite));
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000773 }
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800774 int ssl_cipher_suite = channel_iter.ssl_cipher_suite;
775 if (ssl_cipher_suite != rtc::TLS_NULL_WITH_NULL_NULL &&
776 rtc::SSLStreamAdapter::SslCipherSuiteToName(ssl_cipher_suite)
777 .length()) {
Guo-wei Shieh456696a2015-09-30 21:48:54 -0700778 channel_report->AddString(
779 StatsReport::kStatsValueNameDtlsCipher,
Guo-wei Shieh521ed7b2015-11-18 19:41:53 -0800780 rtc::SSLStreamAdapter::SslCipherSuiteToName(ssl_cipher_suite));
wu@webrtc.org4551b792013-10-09 15:37:36 +0000781 }
jiayl@webrtc.org06b04ec2014-07-24 20:41:20 +0000782
pthatcher@webrtc.orgc04a97f2015-03-16 19:31:40 +0000783 int connection_id = 0;
784 for (const cricket::ConnectionInfo& info :
785 channel_iter.connection_infos) {
786 StatsReport* connection_report = AddConnectionInfoReport(
787 transport_iter.first, channel_iter.component, connection_id++,
788 channel_report->id(), info);
789 if (info.best_connection) {
790 channel_report->AddId(
791 StatsReport::kStatsValueNameSelectedCandidatePairId,
792 connection_report->id());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000793 }
794 }
795 }
796 }
797}
798
stefanf79ade12017-06-02 06:44:03 -0700799void StatsCollector::ExtractBweInfo() {
800 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
801
802 if (pc_->session()->state() == WebRtcSession::State::STATE_CLOSED)
803 return;
804
805 webrtc::Call::Stats call_stats = pc_->session()->GetCallStats();
806 cricket::BandwidthEstimationInfo bwe_info;
807 bwe_info.available_send_bandwidth = call_stats.send_bandwidth_bps;
808 bwe_info.available_recv_bandwidth = call_stats.recv_bandwidth_bps;
809 bwe_info.bucket_delay = call_stats.pacer_delay_ms;
810 // Fill in target encoder bitrate, actual encoder bitrate, rtx bitrate, etc.
811 // TODO(holmer): Also fill this in for audio.
Alex Narest42308f62017-06-19 17:58:12 +0200812 if (pc_->session()->video_channel()) {
813 pc_->session()->video_channel()->FillBitrateInfo(&bwe_info);
stefanf79ade12017-06-02 06:44:03 -0700814 }
stefanf79ade12017-06-02 06:44:03 -0700815 StatsReport::Id report_id(StatsReport::NewBandwidthEstimationId());
816 StatsReport* report = reports_.FindOrAddNew(report_id);
817 ExtractStats(bwe_info, stats_gathering_started_, report);
818}
819
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000820void StatsCollector::ExtractVoiceInfo() {
deadbeefab9b2d12015-10-14 11:33:11 -0700821 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000822
deadbeefab9b2d12015-10-14 11:33:11 -0700823 if (!pc_->session()->voice_channel()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000824 return;
825 }
826 cricket::VoiceMediaInfo voice_info;
deadbeefab9b2d12015-10-14 11:33:11 -0700827 if (!pc_->session()->voice_channel()->GetStats(&voice_info)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000828 LOG(LS_ERROR) << "Failed to get voice channel stats.";
829 return;
830 }
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000831
832 // TODO(tommi): The above code should run on the worker thread and post the
833 // results back to the signaling thread, where we can add data to the reports.
834 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
835
deadbeefab9b2d12015-10-14 11:33:11 -0700836 StatsReport::Id transport_id(GetTransportIdFromProxy(
837 proxy_to_transport_, pc_->session()->voice_channel()->content_name()));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000838 if (!transport_id.get()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000839 LOG(LS_ERROR) << "Failed to get transport name for proxy "
deadbeefab9b2d12015-10-14 11:33:11 -0700840 << pc_->session()->voice_channel()->content_name();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000841 return;
842 }
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000843
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000844 ExtractStatsFromList(voice_info.receivers, transport_id, this,
845 StatsReport::kReceive);
846 ExtractStatsFromList(voice_info.senders, transport_id, this,
847 StatsReport::kSend);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000848
849 UpdateStatsFromExistingLocalAudioTracks();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000850}
851
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000852void StatsCollector::ExtractVideoInfo(
853 PeerConnectionInterface::StatsOutputLevel level) {
deadbeefab9b2d12015-10-14 11:33:11 -0700854 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000855
deadbeefab9b2d12015-10-14 11:33:11 -0700856 if (!pc_->session()->video_channel())
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000857 return;
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +0000858
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000859 cricket::VideoMediaInfo video_info;
deadbeefab9b2d12015-10-14 11:33:11 -0700860 if (!pc_->session()->video_channel()->GetStats(&video_info)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000861 LOG(LS_ERROR) << "Failed to get video channel stats.";
862 return;
863 }
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000864
865 // TODO(tommi): The above code should run on the worker thread and post the
866 // results back to the signaling thread, where we can add data to the reports.
867 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
868
deadbeefab9b2d12015-10-14 11:33:11 -0700869 StatsReport::Id transport_id(GetTransportIdFromProxy(
870 proxy_to_transport_, pc_->session()->video_channel()->content_name()));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000871 if (!transport_id.get()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000872 LOG(LS_ERROR) << "Failed to get transport name for proxy "
deadbeefab9b2d12015-10-14 11:33:11 -0700873 << pc_->session()->video_channel()->content_name();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000874 return;
875 }
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000876 ExtractStatsFromList(video_info.receivers, transport_id, this,
877 StatsReport::kReceive);
878 ExtractStatsFromList(video_info.senders, transport_id, this,
879 StatsReport::kSend);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000880}
881
nissefcc640f2016-04-01 01:10:42 -0700882void StatsCollector::ExtractSenderInfo() {
883 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
884
885 for (const auto& sender : pc_->GetSenders()) {
886 // TODO(nisse): SSRC == 0 currently means none. Delete check when
887 // that is fixed.
888 if (!sender->ssrc()) {
889 continue;
890 }
891 const rtc::scoped_refptr<MediaStreamTrackInterface> track(sender->track());
892 if (!track || track->kind() != MediaStreamTrackInterface::kVideoKind) {
893 continue;
894 }
895 // Safe, because kind() == kVideoKind implies a subclass of
896 // VideoTrackInterface; see mediastreaminterface.h.
897 VideoTrackSourceInterface* source =
898 static_cast<VideoTrackInterface*>(track.get())->GetSource();
899
900 VideoTrackSourceInterface::Stats stats;
901 if (!source->GetStats(&stats)) {
902 continue;
903 }
904 const StatsReport::Id stats_id = StatsReport::NewIdWithDirection(
905 StatsReport::kStatsReportTypeSsrc,
906 rtc::ToString<uint32_t>(sender->ssrc()), StatsReport::kSend);
907 StatsReport* report = reports_.FindOrAddNew(stats_id);
908 report->AddInt(StatsReport::kStatsValueNameFrameWidthInput,
909 stats.input_width);
910 report->AddInt(StatsReport::kStatsValueNameFrameHeightInput,
911 stats.input_height);
912 }
913}
914
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000915void StatsCollector::ExtractDataInfo() {
deadbeefab9b2d12015-10-14 11:33:11 -0700916 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000917
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000918 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
919
deadbeefab9b2d12015-10-14 11:33:11 -0700920 for (const auto& dc : pc_->sctp_data_channels()) {
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000921 StatsReport::Id id(StatsReport::NewTypedIntId(
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000922 StatsReport::kStatsReportTypeDataChannel, dc->id()));
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000923 StatsReport* report = reports_.ReplaceOrAddNew(id);
decurtis@webrtc.org322a5642015-02-03 22:09:37 +0000924 report->set_timestamp(stats_gathering_started_);
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000925 report->AddString(StatsReport::kStatsValueNameLabel, dc->label());
zhihuang6ba3b192016-05-13 11:46:35 -0700926 // Filter out the initial id (-1).
927 if (dc->id() >= 0) {
928 report->AddInt(StatsReport::kStatsValueNameDataChannelId, dc->id());
929 }
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000930 report->AddString(StatsReport::kStatsValueNameProtocol, dc->protocol());
931 report->AddString(StatsReport::kStatsValueNameState,
932 DataChannelInterface::DataStateString(dc->state()));
decurtis@webrtc.org487a4442015-01-15 22:55:07 +0000933 }
934}
935
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000936StatsReport* StatsCollector::GetReport(const StatsReport::StatsType& type,
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000937 const std::string& id,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000938 StatsReport::Direction direction) {
deadbeefab9b2d12015-10-14 11:33:11 -0700939 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700940 RTC_DCHECK(type == StatsReport::kStatsReportTypeSsrc ||
941 type == StatsReport::kStatsReportTypeRemoteSsrc);
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000942 return reports_.Find(StatsReport::NewIdWithDirection(type, id, direction));
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000943}
944
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000945void StatsCollector::UpdateStatsFromExistingLocalAudioTracks() {
deadbeefab9b2d12015-10-14 11:33:11 -0700946 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000947 // Loop through the existing local audio tracks.
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000948 for (const auto& it : local_audio_tracks_) {
949 AudioTrackInterface* track = it.first;
Peter Boström0c4e06b2015-10-07 12:23:21 +0200950 uint32_t ssrc = it.second;
951 StatsReport* report =
952 GetReport(StatsReport::kStatsReportTypeSsrc,
953 rtc::ToString<uint32_t>(ssrc), StatsReport::kSend);
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +0000954 if (report == NULL) {
955 // This can happen if a local audio track is added to a stream on the
956 // fly and the report has not been set up yet. Do nothing in this case.
957 LOG(LS_ERROR) << "Stats report does not exist for ssrc " << ssrc;
958 continue;
959 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000960
961 // The same ssrc can be used by both local and remote audio tracks.
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +0000962 const StatsReport::Value* v =
963 report->FindValue(StatsReport::kStatsValueNameTrackId);
tommi@webrtc.orgd3900292015-03-12 16:35:55 +0000964 if (!v || v->string_val() != track->id())
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000965 continue;
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000966
jbauchbe24c942015-06-22 15:06:43 -0700967 report->set_timestamp(stats_gathering_started_);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000968 UpdateReportFromAudioTrack(track, report);
969 }
970}
971
972void StatsCollector::UpdateReportFromAudioTrack(AudioTrackInterface* track,
973 StatsReport* report) {
deadbeefab9b2d12015-10-14 11:33:11 -0700974 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
henrikg91d6ede2015-09-17 00:24:34 -0700975 RTC_DCHECK(track != NULL);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000976
andrew2fe1cb02015-11-27 17:27:35 -0800977 // Don't overwrite report values if they're not available.
978 int signal_level;
979 if (track->GetSignalLevel(&signal_level)) {
980 RTC_DCHECK_GE(signal_level, 0);
981 report->AddInt(StatsReport::kStatsValueNameAudioInputLevel, signal_level);
982 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000983
andrew2fe1cb02015-11-27 17:27:35 -0800984 auto audio_processor(track->GetAudioProcessor());
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000985
andrew2fe1cb02015-11-27 17:27:35 -0800986 if (audio_processor.get()) {
987 AudioProcessorInterface::AudioProcessorStats stats;
tommi@webrtc.org92f40182015-03-04 15:25:19 +0000988 audio_processor->GetStats(&stats);
989
andrew2fe1cb02015-11-27 17:27:35 -0800990 SetAudioProcessingStats(
991 report, stats.typing_noise_detected, stats.echo_return_loss,
992 stats.echo_return_loss_enhancement, stats.echo_delay_median_ms,
ivoc8c63a822016-10-21 04:10:03 -0700993 stats.aec_quality_min, stats.echo_delay_std_ms,
ivoc4e477a12017-01-15 08:29:46 -0800994 stats.residual_echo_likelihood,
995 stats.residual_echo_likelihood_recent_max);
Minyue2a8a78c2016-04-07 16:48:15 +0200996
997 report->AddFloat(StatsReport::kStatsValueNameAecDivergentFilterFraction,
998 stats.aec_divergent_filter_fraction);
andrew2fe1cb02015-11-27 17:27:35 -0800999 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001000}
1001
Peter Boström0c4e06b2015-10-07 12:23:21 +02001002bool StatsCollector::GetTrackIdBySsrc(uint32_t ssrc,
1003 std::string* track_id,
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +00001004 StatsReport::Direction direction) {
deadbeefab9b2d12015-10-14 11:33:11 -07001005 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
tommi@webrtc.org4fb7e252015-01-21 11:36:18 +00001006 if (direction == StatsReport::kSend) {
deadbeefab9b2d12015-10-14 11:33:11 -07001007 if (!pc_->session()->GetLocalTrackIdBySsrc(ssrc, track_id)) {
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001008 LOG(LS_WARNING) << "The SSRC " << ssrc
1009 << " is not associated with a sending track";
1010 return false;
1011 }
1012 } else {
henrikg91d6ede2015-09-17 00:24:34 -07001013 RTC_DCHECK(direction == StatsReport::kReceive);
deadbeefab9b2d12015-10-14 11:33:11 -07001014 if (!pc_->session()->GetRemoteTrackIdBySsrc(ssrc, track_id)) {
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001015 LOG(LS_WARNING) << "The SSRC " << ssrc
1016 << " is not associated with a receiving track";
1017 return false;
1018 }
1019 }
1020
1021 return true;
1022}
1023
jbauchbe24c942015-06-22 15:06:43 -07001024void StatsCollector::UpdateTrackReports() {
deadbeefab9b2d12015-10-14 11:33:11 -07001025 RTC_DCHECK(pc_->session()->signaling_thread()->IsCurrent());
jbauchbe24c942015-06-22 15:06:43 -07001026
1027 rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls;
1028
1029 for (const auto& entry : track_ids_) {
1030 StatsReport* report = entry.second;
1031 report->set_timestamp(stats_gathering_started_);
1032 }
jbauchbe24c942015-06-22 15:06:43 -07001033}
1034
tommi@webrtc.org69bc5a32014-12-15 09:44:48 +00001035void StatsCollector::ClearUpdateStatsCacheForTest() {
xians@webrtc.org01bda202014-07-09 07:38:38 +00001036 stats_gathering_started_ = 0;
1037}
1038
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001039} // namespace webrtc