blob: 94586fda7c2d4a6b73c00c86b12a591797c5dca2 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
3 * Copyright 2012, Google Inc.
4 *
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
wu@webrtc.org4551b792013-10-09 15:37:36 +000033#include "talk/base/base64.h"
34#include "talk/base/scoped_ptr.h"
tommi@webrtc.orgecb87232014-07-08 12:48:29 +000035#include "talk/base/timing.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000036#include "talk/session/media/channel.h"
37
38namespace webrtc {
39
40// The items below are in alphabetical order.
41const char StatsReport::kStatsValueNameActiveConnection[] =
42 "googActiveConnection";
43const char StatsReport::kStatsValueNameActualEncBitrate[] =
44 "googActualEncBitrate";
45const char StatsReport::kStatsValueNameAudioOutputLevel[] = "audioOutputLevel";
46const char StatsReport::kStatsValueNameAudioInputLevel[] = "audioInputLevel";
47const char StatsReport::kStatsValueNameAvailableReceiveBandwidth[] =
48 "googAvailableReceiveBandwidth";
49const char StatsReport::kStatsValueNameAvailableSendBandwidth[] =
50 "googAvailableSendBandwidth";
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +000051const char StatsReport::kStatsValueNameAvgEncodeMs[] = "googAvgEncodeMs";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000052const char StatsReport::kStatsValueNameBucketDelay[] = "googBucketDelay";
53const char StatsReport::kStatsValueNameBytesReceived[] = "bytesReceived";
54const char StatsReport::kStatsValueNameBytesSent[] = "bytesSent";
mallinath@webrtc.org62451dc2013-12-13 12:29:34 +000055const char StatsReport::kStatsValueNameBandwidthLimitedResolution[] =
56 "googBandwidthLimitedResolution";
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +000057const char StatsReport::kStatsValueNameCaptureJitterMs[] =
58 "googCaptureJitterMs";
wu@webrtc.org9caf2762013-12-11 18:25:07 +000059const char StatsReport::kStatsValueNameCaptureQueueDelayMsPerS[] =
60 "googCaptureQueueDelayMsPerS";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000061const char StatsReport::kStatsValueNameChannelId[] = "googChannelId";
62const char StatsReport::kStatsValueNameCodecName[] = "googCodecName";
63const char StatsReport::kStatsValueNameComponent[] = "googComponent";
64const char StatsReport::kStatsValueNameContentName[] = "googContentName";
mallinath@webrtc.org62451dc2013-12-13 12:29:34 +000065const char StatsReport::kStatsValueNameCpuLimitedResolution[] =
66 "googCpuLimitedResolution";
buildbot@webrtc.org3e01e0b2014-05-13 17:54:10 +000067const char StatsReport::kStatsValueNameDecodingCTSG[] =
68 "googDecodingCTSG";
69const char StatsReport::kStatsValueNameDecodingCTN[] =
70 "googDecodingCTN";
71const char StatsReport::kStatsValueNameDecodingNormal[] =
72 "googDecodingNormal";
73const char StatsReport::kStatsValueNameDecodingPLC[] =
74 "googDecodingPLC";
75const char StatsReport::kStatsValueNameDecodingCNG[] =
76 "googDecodingCNG";
77const char StatsReport::kStatsValueNameDecodingPLCCNG[] =
78 "googDecodingPLCCNG";
wu@webrtc.org4551b792013-10-09 15:37:36 +000079const char StatsReport::kStatsValueNameDer[] = "googDerBase64";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000080// Echo metrics from the audio processing module.
81const char StatsReport::kStatsValueNameEchoCancellationQualityMin[] =
82 "googEchoCancellationQualityMin";
83const char StatsReport::kStatsValueNameEchoDelayMedian[] =
84 "googEchoCancellationEchoDelayMedian";
85const char StatsReport::kStatsValueNameEchoDelayStdDev[] =
86 "googEchoCancellationEchoDelayStdDev";
87const char StatsReport::kStatsValueNameEchoReturnLoss[] =
88 "googEchoCancellationReturnLoss";
89const char StatsReport::kStatsValueNameEchoReturnLossEnhancement[] =
90 "googEchoCancellationReturnLossEnhancement";
91
buildbot@webrtc.orgc800c1c2014-06-13 07:56:17 +000092const char StatsReport::kStatsValueNameEncodeRelStdDev[] =
93 "googEncodeRelStdDev";
wu@webrtc.org9caf2762013-12-11 18:25:07 +000094const char StatsReport::kStatsValueNameEncodeUsagePercent[] =
95 "googEncodeUsagePercent";
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000096const char StatsReport::kStatsValueNameExpandRate[] = "googExpandRate";
wu@webrtc.org4551b792013-10-09 15:37:36 +000097const char StatsReport::kStatsValueNameFingerprint[] = "googFingerprint";
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +000098const char StatsReport::kStatsValueNameFingerprintAlgorithm[] =
99 "googFingerprintAlgorithm";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000100const char StatsReport::kStatsValueNameFirsReceived[] = "googFirsReceived";
101const char StatsReport::kStatsValueNameFirsSent[] = "googFirsSent";
wu@webrtc.org987f2c92014-03-28 16:22:19 +0000102const char StatsReport::kStatsValueNameFrameHeightInput[] =
103 "googFrameHeightInput";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000104const char StatsReport::kStatsValueNameFrameHeightReceived[] =
105 "googFrameHeightReceived";
106const char StatsReport::kStatsValueNameFrameHeightSent[] =
107 "googFrameHeightSent";
108const char StatsReport::kStatsValueNameFrameRateReceived[] =
109 "googFrameRateReceived";
110const char StatsReport::kStatsValueNameFrameRateDecoded[] =
111 "googFrameRateDecoded";
112const char StatsReport::kStatsValueNameFrameRateOutput[] =
113 "googFrameRateOutput";
wu@webrtc.org97077a32013-10-25 21:18:33 +0000114const char StatsReport::kStatsValueNameDecodeMs[] = "googDecodeMs";
115const char StatsReport::kStatsValueNameMaxDecodeMs[] = "googMaxDecodeMs";
116const char StatsReport::kStatsValueNameCurrentDelayMs[] = "googCurrentDelayMs";
117const char StatsReport::kStatsValueNameTargetDelayMs[] = "googTargetDelayMs";
118const char StatsReport::kStatsValueNameJitterBufferMs[] = "googJitterBufferMs";
119const char StatsReport::kStatsValueNameMinPlayoutDelayMs[] =
120 "googMinPlayoutDelayMs";
121const char StatsReport::kStatsValueNameRenderDelayMs[] = "googRenderDelayMs";
122
buildbot@webrtc.org0581f0b2014-05-06 21:36:31 +0000123const char StatsReport::kStatsValueNameCaptureStartNtpTimeMs[] =
124 "googCaptureStartNtpTimeMs";
125
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000126const char StatsReport::kStatsValueNameFrameRateInput[] = "googFrameRateInput";
127const char StatsReport::kStatsValueNameFrameRateSent[] = "googFrameRateSent";
wu@webrtc.org987f2c92014-03-28 16:22:19 +0000128const char StatsReport::kStatsValueNameFrameWidthInput[] =
129 "googFrameWidthInput";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000130const char StatsReport::kStatsValueNameFrameWidthReceived[] =
131 "googFrameWidthReceived";
132const char StatsReport::kStatsValueNameFrameWidthSent[] = "googFrameWidthSent";
133const char StatsReport::kStatsValueNameInitiator[] = "googInitiator";
wu@webrtc.org4551b792013-10-09 15:37:36 +0000134const char StatsReport::kStatsValueNameIssuerId[] = "googIssuerId";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000135const char StatsReport::kStatsValueNameJitterReceived[] = "googJitterReceived";
136const char StatsReport::kStatsValueNameLocalAddress[] = "googLocalAddress";
wu@webrtc.org364f2042013-11-20 21:49:41 +0000137const char StatsReport::kStatsValueNameLocalCandidateType[] =
138 "googLocalCandidateType";
wu@webrtc.org4551b792013-10-09 15:37:36 +0000139const char StatsReport::kStatsValueNameLocalCertificateId[] =
140 "googLocalCertificateId";
buildbot@webrtc.org71dffb72014-06-24 07:24:49 +0000141const char StatsReport::kStatsValueNameAdaptationChanges[] =
142 "googAdaptationChanges";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000143const char StatsReport::kStatsValueNameNacksReceived[] = "googNacksReceived";
144const char StatsReport::kStatsValueNameNacksSent[] = "googNacksSent";
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +0000145const char StatsReport::kStatsValueNamePlisReceived[] = "googPlisReceived";
146const char StatsReport::kStatsValueNamePlisSent[] = "googPlisSent";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000147const char StatsReport::kStatsValueNamePacketsReceived[] = "packetsReceived";
148const char StatsReport::kStatsValueNamePacketsSent[] = "packetsSent";
149const char StatsReport::kStatsValueNamePacketsLost[] = "packetsLost";
jiayl@webrtc.org11aab0e2014-03-07 18:56:26 +0000150const char StatsReport::kStatsValueNamePreferredJitterBufferMs[] =
151 "googPreferredJitterBufferMs";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000152const char StatsReport::kStatsValueNameReadable[] = "googReadable";
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000153const char StatsReport::kStatsValueNameRecvPacketGroupArrivalTimeDebug[] =
154 "googReceivedPacketGroupArrivalTimeDebug";
155const char StatsReport::kStatsValueNameRecvPacketGroupPropagationDeltaDebug[] =
156 "googReceivedPacketGroupPropagationDeltaDebug";
157const char
158StatsReport::kStatsValueNameRecvPacketGroupPropagationDeltaSumDebug[] =
159 "googReceivedPacketGroupPropagationDeltaSumDebug";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000160const char StatsReport::kStatsValueNameRemoteAddress[] = "googRemoteAddress";
wu@webrtc.org364f2042013-11-20 21:49:41 +0000161const char StatsReport::kStatsValueNameRemoteCandidateType[] =
162 "googRemoteCandidateType";
wu@webrtc.org4551b792013-10-09 15:37:36 +0000163const char StatsReport::kStatsValueNameRemoteCertificateId[] =
164 "googRemoteCertificateId";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000165const char StatsReport::kStatsValueNameRetransmitBitrate[] =
166 "googRetransmitBitrate";
167const char StatsReport::kStatsValueNameRtt[] = "googRtt";
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000168const char StatsReport::kStatsValueNameSsrc[] = "ssrc";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000169const char StatsReport::kStatsValueNameTargetEncBitrate[] =
170 "googTargetEncBitrate";
171const char StatsReport::kStatsValueNameTransmitBitrate[] =
172 "googTransmitBitrate";
173const char StatsReport::kStatsValueNameTransportId[] = "transportId";
174const char StatsReport::kStatsValueNameTransportType[] = "googTransportType";
175const char StatsReport::kStatsValueNameTrackId[] = "googTrackId";
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000176const char StatsReport::kStatsValueNameTypingNoiseState[] =
177 "googTypingNoiseState";
mallinath@webrtc.org62451dc2013-12-13 12:29:34 +0000178const char StatsReport::kStatsValueNameViewLimitedResolution[] =
179 "googViewLimitedResolution";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000180const char StatsReport::kStatsValueNameWritable[] = "googWritable";
181
182const char StatsReport::kStatsReportTypeSession[] = "googLibjingleSession";
183const char StatsReport::kStatsReportTypeBwe[] = "VideoBwe";
wu@webrtc.org97077a32013-10-25 21:18:33 +0000184const char StatsReport::kStatsReportTypeRemoteSsrc[] = "remoteSsrc";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000185const char StatsReport::kStatsReportTypeSsrc[] = "ssrc";
186const char StatsReport::kStatsReportTypeTrack[] = "googTrack";
187const char StatsReport::kStatsReportTypeIceCandidate[] = "iceCandidate";
188const char StatsReport::kStatsReportTypeTransport[] = "googTransport";
189const char StatsReport::kStatsReportTypeComponent[] = "googComponent";
190const char StatsReport::kStatsReportTypeCandidatePair[] = "googCandidatePair";
wu@webrtc.org4551b792013-10-09 15:37:36 +0000191const char StatsReport::kStatsReportTypeCertificate[] = "googCertificate";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000192
193const char StatsReport::kStatsReportVideoBweId[] = "bweforvideo";
194
195// Implementations of functions in statstypes.h
tommi@webrtc.org242068d2014-07-14 20:19:56 +0000196void StatsReport::AddValue(StatsReport::StatsValueName name,
197 const std::string& value) {
198 values.push_back(Value(name, value));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000199}
200
tommi@webrtc.org242068d2014-07-14 20:19:56 +0000201void StatsReport::AddValue(StatsReport::StatsValueName name, int64 value) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000202 AddValue(name, talk_base::ToString<int64>(value));
203}
204
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000205template <typename T>
tommi@webrtc.org242068d2014-07-14 20:19:56 +0000206void StatsReport::AddValue(StatsReport::StatsValueName name,
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000207 const std::vector<T>& value) {
208 std::ostringstream oss;
209 oss << "[";
210 for (size_t i = 0; i < value.size(); ++i) {
211 oss << talk_base::ToString<T>(value[i]);
212 if (i != value.size() - 1)
213 oss << ", ";
214 }
215 oss << "]";
216 AddValue(name, oss.str());
217}
218
tommi@webrtc.org242068d2014-07-14 20:19:56 +0000219void StatsReport::AddBoolean(StatsReport::StatsValueName name, bool value) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000220 AddValue(name, value ? "true" : "false");
221}
222
tommi@webrtc.org242068d2014-07-14 20:19:56 +0000223void StatsReport::ReplaceValue(StatsReport::StatsValueName name,
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000224 const std::string& value) {
225 for (Values::iterator it = values.begin(); it != values.end(); ++it) {
226 if ((*it).name == name) {
227 it->value = value;
228 return;
229 }
230 }
231 // It is not reachable here, add an ASSERT to make sure the overwriting is
232 // always a success.
233 ASSERT(false);
234}
235
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000236namespace {
henrike@webrtc.org39f831f2014-07-24 14:20:52 +0000237typedef std::map<std::string, StatsReport> StatsMap;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000238
tommi@webrtc.org47218952014-07-15 19:22:37 +0000239double GetTimeNow() {
240 return talk_base::Timing::WallTimeNow() * talk_base::kNumMillisecsPerSec;
241}
242
243bool GetTransportIdFromProxy(const cricket::ProxyTransportMap& map,
244 const std::string& proxy,
245 std::string* transport) {
246 // TODO(hta): Remove handling of empty proxy name once tests do not use it.
247 if (proxy.empty()) {
248 transport->clear();
249 return true;
250 }
251
252 cricket::ProxyTransportMap::const_iterator found = map.find(proxy);
253 if (found == map.end()) {
254 LOG(LS_ERROR) << "No transport ID mapping for " << proxy;
255 return false;
256 }
257
258 std::ostringstream ost;
259 // Component 1 is always used for RTP.
260 ost << "Channel-" << found->second << "-1";
261 *transport = ost.str();
262 return true;
263}
264
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000265std::string StatsId(const std::string& type, const std::string& id) {
266 return type + "_" + id;
267}
268
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000269std::string StatsId(const std::string& type, const std::string& id,
270 StatsCollector::TrackDirection direction) {
271 ASSERT(direction == StatsCollector::kSending ||
272 direction == StatsCollector::kReceiving);
273
274 // Strings for the direction of the track.
275 const char kSendDirection[] = "send";
276 const char kRecvDirection[] = "recv";
277
278 const std::string direction_id = (direction == StatsCollector::kSending) ?
279 kSendDirection : kRecvDirection;
280 return type + "_" + id + "_" + direction_id;
281}
282
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000283bool ExtractValueFromReport(
284 const StatsReport& report,
tommi@webrtc.org242068d2014-07-14 20:19:56 +0000285 StatsReport::StatsValueName name,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000286 std::string* value) {
287 StatsReport::Values::const_iterator it = report.values.begin();
288 for (; it != report.values.end(); ++it) {
289 if (it->name == name) {
290 *value = it->value;
291 return true;
292 }
293 }
294 return false;
295}
296
henrike@webrtc.org39f831f2014-07-24 14:20:52 +0000297void AddTrackReport(StatsMap* reports, const std::string& track_id) {
xians@webrtc.org01bda202014-07-09 07:38:38 +0000298 // Adds an empty track report.
henrike@webrtc.org39f831f2014-07-24 14:20:52 +0000299 StatsReport report;
300 report.type = StatsReport::kStatsReportTypeTrack;
301 report.id = StatsId(StatsReport::kStatsReportTypeTrack, track_id);
302 report.AddValue(StatsReport::kStatsValueNameTrackId, track_id);
303 (*reports)[report.id] = report;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000304}
305
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000306template <class TrackVector>
henrike@webrtc.org39f831f2014-07-24 14:20:52 +0000307void CreateTrackReports(const TrackVector& tracks, StatsMap* reports) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000308 for (size_t j = 0; j < tracks.size(); ++j) {
309 webrtc::MediaStreamTrackInterface* track = tracks[j];
xians@webrtc.org01bda202014-07-09 07:38:38 +0000310 AddTrackReport(reports, track->id());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000311 }
312}
313
314void ExtractStats(const cricket::VoiceReceiverInfo& info, StatsReport* report) {
315 report->AddValue(StatsReport::kStatsValueNameAudioOutputLevel,
316 info.audio_level);
317 report->AddValue(StatsReport::kStatsValueNameBytesReceived,
318 info.bytes_rcvd);
319 report->AddValue(StatsReport::kStatsValueNameJitterReceived,
320 info.jitter_ms);
jiayl@webrtc.org11aab0e2014-03-07 18:56:26 +0000321 report->AddValue(StatsReport::kStatsValueNameJitterBufferMs,
322 info.jitter_buffer_ms);
323 report->AddValue(StatsReport::kStatsValueNamePreferredJitterBufferMs,
324 info.jitter_buffer_preferred_ms);
325 report->AddValue(StatsReport::kStatsValueNameCurrentDelayMs,
326 info.delay_estimate_ms);
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000327 report->AddValue(StatsReport::kStatsValueNameExpandRate,
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000328 talk_base::ToString<float>(info.expand_rate));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000329 report->AddValue(StatsReport::kStatsValueNamePacketsReceived,
330 info.packets_rcvd);
331 report->AddValue(StatsReport::kStatsValueNamePacketsLost,
332 info.packets_lost);
buildbot@webrtc.org3e01e0b2014-05-13 17:54:10 +0000333 report->AddValue(StatsReport::kStatsValueNameDecodingCTSG,
334 info.decoding_calls_to_silence_generator);
335 report->AddValue(StatsReport::kStatsValueNameDecodingCTN,
336 info.decoding_calls_to_neteq);
337 report->AddValue(StatsReport::kStatsValueNameDecodingNormal,
338 info.decoding_normal);
339 report->AddValue(StatsReport::kStatsValueNameDecodingPLC,
340 info.decoding_plc);
341 report->AddValue(StatsReport::kStatsValueNameDecodingCNG,
342 info.decoding_cng);
343 report->AddValue(StatsReport::kStatsValueNameDecodingPLCCNG,
344 info.decoding_plc_cng);
buildbot@webrtc.orgb525a9d2014-06-03 09:42:15 +0000345 report->AddValue(StatsReport::kStatsValueNameCaptureStartNtpTimeMs,
346 info.capture_start_ntp_time_ms);
buildbot@webrtc.org7e71b772014-06-13 01:14:01 +0000347 report->AddValue(StatsReport::kStatsValueNameCodecName, info.codec_name);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000348}
349
350void ExtractStats(const cricket::VoiceSenderInfo& info, StatsReport* report) {
351 report->AddValue(StatsReport::kStatsValueNameAudioInputLevel,
352 info.audio_level);
353 report->AddValue(StatsReport::kStatsValueNameBytesSent,
354 info.bytes_sent);
355 report->AddValue(StatsReport::kStatsValueNamePacketsSent,
356 info.packets_sent);
henrike@webrtc.orgffe26202014-03-19 22:20:10 +0000357 report->AddValue(StatsReport::kStatsValueNamePacketsLost,
358 info.packets_lost);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000359 report->AddValue(StatsReport::kStatsValueNameJitterReceived,
360 info.jitter_ms);
361 report->AddValue(StatsReport::kStatsValueNameRtt, info.rtt_ms);
362 report->AddValue(StatsReport::kStatsValueNameEchoCancellationQualityMin,
363 talk_base::ToString<float>(info.aec_quality_min));
364 report->AddValue(StatsReport::kStatsValueNameEchoDelayMedian,
365 info.echo_delay_median_ms);
366 report->AddValue(StatsReport::kStatsValueNameEchoDelayStdDev,
367 info.echo_delay_std_ms);
368 report->AddValue(StatsReport::kStatsValueNameEchoReturnLoss,
369 info.echo_return_loss);
370 report->AddValue(StatsReport::kStatsValueNameEchoReturnLossEnhancement,
371 info.echo_return_loss_enhancement);
372 report->AddValue(StatsReport::kStatsValueNameCodecName, info.codec_name);
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000373 report->AddBoolean(StatsReport::kStatsValueNameTypingNoiseState,
374 info.typing_noise_detected);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000375}
376
377void ExtractStats(const cricket::VideoReceiverInfo& info, StatsReport* report) {
378 report->AddValue(StatsReport::kStatsValueNameBytesReceived,
379 info.bytes_rcvd);
380 report->AddValue(StatsReport::kStatsValueNamePacketsReceived,
381 info.packets_rcvd);
382 report->AddValue(StatsReport::kStatsValueNamePacketsLost,
383 info.packets_lost);
384
385 report->AddValue(StatsReport::kStatsValueNameFirsSent,
386 info.firs_sent);
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +0000387 report->AddValue(StatsReport::kStatsValueNamePlisSent,
388 info.plis_sent);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000389 report->AddValue(StatsReport::kStatsValueNameNacksSent,
390 info.nacks_sent);
391 report->AddValue(StatsReport::kStatsValueNameFrameWidthReceived,
392 info.frame_width);
393 report->AddValue(StatsReport::kStatsValueNameFrameHeightReceived,
394 info.frame_height);
395 report->AddValue(StatsReport::kStatsValueNameFrameRateReceived,
396 info.framerate_rcvd);
397 report->AddValue(StatsReport::kStatsValueNameFrameRateDecoded,
398 info.framerate_decoded);
399 report->AddValue(StatsReport::kStatsValueNameFrameRateOutput,
400 info.framerate_output);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000401
402 report->AddValue(StatsReport::kStatsValueNameDecodeMs,
403 info.decode_ms);
404 report->AddValue(StatsReport::kStatsValueNameMaxDecodeMs,
405 info.max_decode_ms);
406 report->AddValue(StatsReport::kStatsValueNameCurrentDelayMs,
407 info.current_delay_ms);
408 report->AddValue(StatsReport::kStatsValueNameTargetDelayMs,
409 info.target_delay_ms);
410 report->AddValue(StatsReport::kStatsValueNameJitterBufferMs,
411 info.jitter_buffer_ms);
412 report->AddValue(StatsReport::kStatsValueNameMinPlayoutDelayMs,
413 info.min_playout_delay_ms);
414 report->AddValue(StatsReport::kStatsValueNameRenderDelayMs,
415 info.render_delay_ms);
buildbot@webrtc.org0581f0b2014-05-06 21:36:31 +0000416
417 report->AddValue(StatsReport::kStatsValueNameCaptureStartNtpTimeMs,
418 info.capture_start_ntp_time_ms);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000419}
420
421void ExtractStats(const cricket::VideoSenderInfo& info, StatsReport* report) {
422 report->AddValue(StatsReport::kStatsValueNameBytesSent,
423 info.bytes_sent);
424 report->AddValue(StatsReport::kStatsValueNamePacketsSent,
425 info.packets_sent);
henrike@webrtc.orgffe26202014-03-19 22:20:10 +0000426 report->AddValue(StatsReport::kStatsValueNamePacketsLost,
427 info.packets_lost);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000428
429 report->AddValue(StatsReport::kStatsValueNameFirsReceived,
430 info.firs_rcvd);
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +0000431 report->AddValue(StatsReport::kStatsValueNamePlisReceived,
432 info.plis_rcvd);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000433 report->AddValue(StatsReport::kStatsValueNameNacksReceived,
434 info.nacks_rcvd);
wu@webrtc.org987f2c92014-03-28 16:22:19 +0000435 report->AddValue(StatsReport::kStatsValueNameFrameWidthInput,
436 info.input_frame_width);
437 report->AddValue(StatsReport::kStatsValueNameFrameHeightInput,
438 info.input_frame_height);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000439 report->AddValue(StatsReport::kStatsValueNameFrameWidthSent,
wu@webrtc.org987f2c92014-03-28 16:22:19 +0000440 info.send_frame_width);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000441 report->AddValue(StatsReport::kStatsValueNameFrameHeightSent,
wu@webrtc.org987f2c92014-03-28 16:22:19 +0000442 info.send_frame_height);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000443 report->AddValue(StatsReport::kStatsValueNameFrameRateInput,
444 info.framerate_input);
445 report->AddValue(StatsReport::kStatsValueNameFrameRateSent,
446 info.framerate_sent);
447 report->AddValue(StatsReport::kStatsValueNameRtt, info.rtt_ms);
448 report->AddValue(StatsReport::kStatsValueNameCodecName, info.codec_name);
mallinath@webrtc.org62451dc2013-12-13 12:29:34 +0000449 report->AddBoolean(StatsReport::kStatsValueNameCpuLimitedResolution,
450 (info.adapt_reason & 0x1) > 0);
451 report->AddBoolean(StatsReport::kStatsValueNameBandwidthLimitedResolution,
452 (info.adapt_reason & 0x2) > 0);
453 report->AddBoolean(StatsReport::kStatsValueNameViewLimitedResolution,
454 (info.adapt_reason & 0x4) > 0);
buildbot@webrtc.org71dffb72014-06-24 07:24:49 +0000455 report->AddValue(StatsReport::kStatsValueNameAdaptationChanges,
456 info.adapt_changes);
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000457 report->AddValue(StatsReport::kStatsValueNameAvgEncodeMs, info.avg_encode_ms);
458 report->AddValue(StatsReport::kStatsValueNameCaptureJitterMs,
459 info.capture_jitter_ms);
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000460 report->AddValue(StatsReport::kStatsValueNameCaptureQueueDelayMsPerS,
461 info.capture_queue_delay_ms_per_s);
462 report->AddValue(StatsReport::kStatsValueNameEncodeUsagePercent,
463 info.encode_usage_percent);
buildbot@webrtc.orgc800c1c2014-06-13 07:56:17 +0000464 report->AddValue(StatsReport::kStatsValueNameEncodeRelStdDev,
465 info.encode_rsd);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000466}
467
468void ExtractStats(const cricket::BandwidthEstimationInfo& info,
469 double stats_gathering_started,
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000470 PeerConnectionInterface::StatsOutputLevel level,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000471 StatsReport* report) {
henrike@webrtc.org39f831f2014-07-24 14:20:52 +0000472 report->id = StatsReport::kStatsReportVideoBweId;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000473 report->type = StatsReport::kStatsReportTypeBwe;
474
475 // Clear out stats from previous GatherStats calls if any.
476 if (report->timestamp != stats_gathering_started) {
477 report->values.clear();
478 report->timestamp = stats_gathering_started;
479 }
480
481 report->AddValue(StatsReport::kStatsValueNameAvailableSendBandwidth,
482 info.available_send_bandwidth);
483 report->AddValue(StatsReport::kStatsValueNameAvailableReceiveBandwidth,
484 info.available_recv_bandwidth);
485 report->AddValue(StatsReport::kStatsValueNameTargetEncBitrate,
486 info.target_enc_bitrate);
487 report->AddValue(StatsReport::kStatsValueNameActualEncBitrate,
488 info.actual_enc_bitrate);
489 report->AddValue(StatsReport::kStatsValueNameRetransmitBitrate,
490 info.retransmit_bitrate);
491 report->AddValue(StatsReport::kStatsValueNameTransmitBitrate,
492 info.transmit_bitrate);
493 report->AddValue(StatsReport::kStatsValueNameBucketDelay,
494 info.bucket_delay);
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000495 if (level >= PeerConnectionInterface::kStatsOutputLevelDebug) {
496 report->AddValue(
497 StatsReport::kStatsValueNameRecvPacketGroupPropagationDeltaSumDebug,
498 info.total_received_propagation_delta_ms);
499 if (info.recent_received_propagation_delta_ms.size() > 0) {
500 report->AddValue(
501 StatsReport::kStatsValueNameRecvPacketGroupPropagationDeltaDebug,
502 info.recent_received_propagation_delta_ms);
503 report->AddValue(
504 StatsReport::kStatsValueNameRecvPacketGroupArrivalTimeDebug,
505 info.recent_received_packet_group_arrival_time_ms);
506 }
507 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000508}
509
wu@webrtc.org97077a32013-10-25 21:18:33 +0000510void ExtractRemoteStats(const cricket::MediaSenderInfo& info,
511 StatsReport* report) {
512 report->timestamp = info.remote_stats[0].timestamp;
513 // TODO(hta): Extract some stats here.
514}
515
516void ExtractRemoteStats(const cricket::MediaReceiverInfo& info,
517 StatsReport* report) {
518 report->timestamp = info.remote_stats[0].timestamp;
519 // TODO(hta): Extract some stats here.
520}
521
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000522// Template to extract stats from a data vector.
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000523// In order to use the template, the functions that are called from it,
524// ExtractStats and ExtractRemoteStats, must be defined and overloaded
525// for each type.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000526template<typename T>
527void ExtractStatsFromList(const std::vector<T>& data,
528 const std::string& transport_id,
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000529 StatsCollector* collector,
530 StatsCollector::TrackDirection direction) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000531 typename std::vector<T>::const_iterator it = data.begin();
532 for (; it != data.end(); ++it) {
533 std::string id;
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000534 uint32 ssrc = it->ssrc();
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000535 // Each track can have stats for both local and remote objects.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000536 // TODO(hta): Handle the case of multiple SSRCs per object.
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000537 StatsReport* report = collector->PrepareLocalReport(ssrc, transport_id,
538 direction);
539 if (report)
540 ExtractStats(*it, report);
541
wu@webrtc.org97077a32013-10-25 21:18:33 +0000542 if (it->remote_stats.size() > 0) {
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000543 report = collector->PrepareRemoteReport(ssrc, transport_id,
544 direction);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000545 if (!report) {
546 continue;
547 }
548 ExtractRemoteStats(*it, report);
549 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000550 }
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000551}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000552
553} // namespace
554
tommi@webrtc.org03505bc2014-07-14 20:15:26 +0000555StatsCollector::StatsCollector(WebRtcSession* session)
556 : session_(session), stats_gathering_started_(0) {
557 ASSERT(session_);
558}
559
560StatsCollector::~StatsCollector() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000561}
562
563// Adds a MediaStream with tracks that can be used as a |selector| in a call
564// to GetStats.
565void StatsCollector::AddStream(MediaStreamInterface* stream) {
566 ASSERT(stream != NULL);
567
568 CreateTrackReports<AudioTrackVector>(stream->GetAudioTracks(),
569 &reports_);
570 CreateTrackReports<VideoTrackVector>(stream->GetVideoTracks(),
571 &reports_);
572}
573
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000574void StatsCollector::AddLocalAudioTrack(AudioTrackInterface* audio_track,
575 uint32 ssrc) {
576 ASSERT(audio_track != NULL);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000577 for (LocalAudioTrackVector::iterator it = local_audio_tracks_.begin();
578 it != local_audio_tracks_.end(); ++it) {
579 ASSERT(it->first != audio_track || it->second != ssrc);
580 }
xians@webrtc.org01bda202014-07-09 07:38:38 +0000581
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000582 local_audio_tracks_.push_back(std::make_pair(audio_track, ssrc));
xians@webrtc.org01bda202014-07-09 07:38:38 +0000583
584 // Create the kStatsReportTypeTrack report for the new track if there is no
585 // report yet.
henrike@webrtc.org39f831f2014-07-24 14:20:52 +0000586 StatsMap::iterator it = reports_.find(
xians@webrtc.org01bda202014-07-09 07:38:38 +0000587 StatsId(StatsReport::kStatsReportTypeTrack, audio_track->id()));
henrike@webrtc.org39f831f2014-07-24 14:20:52 +0000588 if (it == reports_.end())
xians@webrtc.org01bda202014-07-09 07:38:38 +0000589 AddTrackReport(&reports_, audio_track->id());
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000590}
591
592void StatsCollector::RemoveLocalAudioTrack(AudioTrackInterface* audio_track,
593 uint32 ssrc) {
594 ASSERT(audio_track != NULL);
595 for (LocalAudioTrackVector::iterator it = local_audio_tracks_.begin();
596 it != local_audio_tracks_.end(); ++it) {
597 if (it->first == audio_track && it->second == ssrc) {
598 local_audio_tracks_.erase(it);
599 return;
600 }
601 }
602
603 ASSERT(false);
604}
605
henrike@webrtc.org39f831f2014-07-24 14:20:52 +0000606bool StatsCollector::GetStats(MediaStreamTrackInterface* track,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000607 StatsReports* reports) {
608 ASSERT(reports != NULL);
henrike@webrtc.org39f831f2014-07-24 14:20:52 +0000609 reports->clear();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000610
henrike@webrtc.org39f831f2014-07-24 14:20:52 +0000611 StatsMap::iterator it;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000612 if (!track) {
henrike@webrtc.org39f831f2014-07-24 14:20:52 +0000613 for (it = reports_.begin(); it != reports_.end(); ++it) {
614 reports->push_back(it->second);
615 }
616 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000617 }
618
henrike@webrtc.org39f831f2014-07-24 14:20:52 +0000619 it = reports_.find(StatsId(StatsReport::kStatsReportTypeSession,
620 session_->id()));
621 if (it != reports_.end()) {
622 reports->push_back(it->second);
623 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000624
henrike@webrtc.org39f831f2014-07-24 14:20:52 +0000625 it = reports_.find(StatsId(StatsReport::kStatsReportTypeTrack, track->id()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000626
henrike@webrtc.org39f831f2014-07-24 14:20:52 +0000627 if (it == reports_.end()) {
628 LOG(LS_WARNING) << "No StatsReport is available for "<< track->id();
629 return false;
630 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000631
henrike@webrtc.org39f831f2014-07-24 14:20:52 +0000632 reports->push_back(it->second);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000633
634 std::string track_id;
henrike@webrtc.org39f831f2014-07-24 14:20:52 +0000635 for (it = reports_.begin(); it != reports_.end(); ++it) {
636 if (it->second.type != StatsReport::kStatsReportTypeSsrc) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000637 continue;
henrike@webrtc.org39f831f2014-07-24 14:20:52 +0000638 }
639 if (ExtractValueFromReport(it->second,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000640 StatsReport::kStatsValueNameTrackId,
641 &track_id)) {
642 if (track_id == track->id()) {
henrike@webrtc.org39f831f2014-07-24 14:20:52 +0000643 reports->push_back(it->second);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000644 }
645 }
646 }
henrike@webrtc.org39f831f2014-07-24 14:20:52 +0000647
648 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000649}
650
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000651void
652StatsCollector::UpdateStats(PeerConnectionInterface::StatsOutputLevel level) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000653 double time_now = GetTimeNow();
654 // Calls to UpdateStats() that occur less than kMinGatherStatsPeriod number of
655 // ms apart will be ignored.
656 const double kMinGatherStatsPeriod = 50;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000657 if (stats_gathering_started_ != 0 &&
658 stats_gathering_started_ + kMinGatherStatsPeriod > time_now) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000659 return;
660 }
661 stats_gathering_started_ = time_now;
662
663 if (session_) {
664 ExtractSessionInfo();
665 ExtractVoiceInfo();
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000666 ExtractVideoInfo(level);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000667 }
668}
669
wu@webrtc.org97077a32013-10-25 21:18:33 +0000670StatsReport* StatsCollector::PrepareLocalReport(
671 uint32 ssrc,
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000672 const std::string& transport_id,
673 TrackDirection direction) {
674 const std::string ssrc_id = talk_base::ToString<uint32>(ssrc);
henrike@webrtc.org39f831f2014-07-24 14:20:52 +0000675 StatsMap::iterator it = reports_.find(StatsId(
676 StatsReport::kStatsReportTypeSsrc, ssrc_id, direction));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000677
xians@webrtc.org01bda202014-07-09 07:38:38 +0000678 // Use the ID of the track that is currently mapped to the SSRC, if any.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000679 std::string track_id;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000680 if (!GetTrackIdBySsrc(ssrc, &track_id, direction)) {
henrike@webrtc.org39f831f2014-07-24 14:20:52 +0000681 if (it == reports_.end()) {
xians@webrtc.org01bda202014-07-09 07:38:38 +0000682 // The ssrc is not used by any track or existing report, return NULL
683 // in such case to indicate no report is prepared for the ssrc.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000684 return NULL;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000685 }
686
687 // The ssrc is not used by any existing track. Keeps the old track id
688 // since we want to report the stats for inactive ssrc.
henrike@webrtc.org39f831f2014-07-24 14:20:52 +0000689 ExtractValueFromReport(it->second,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000690 StatsReport::kStatsValueNameTrackId,
691 &track_id);
692 }
693
henrike@webrtc.org39f831f2014-07-24 14:20:52 +0000694 StatsReport* report = GetOrCreateReport(StatsReport::kStatsReportTypeSsrc,
695 ssrc_id, direction);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000696
697 // Clear out stats from previous GatherStats calls if any.
xians@webrtc.org01bda202014-07-09 07:38:38 +0000698 // This is required since the report will be returned for the new values.
699 // Having the old values in the report will lead to multiple values with
700 // the same name.
701 // TODO(xians): Consider changing StatsReport to use map instead of vector.
702 report->values.clear();
703 report->timestamp = stats_gathering_started_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000704
705 report->AddValue(StatsReport::kStatsValueNameSsrc, ssrc_id);
706 report->AddValue(StatsReport::kStatsValueNameTrackId, track_id);
707 // Add the mapping of SSRC to transport.
708 report->AddValue(StatsReport::kStatsValueNameTransportId,
709 transport_id);
710 return report;
711}
712
wu@webrtc.org97077a32013-10-25 21:18:33 +0000713StatsReport* StatsCollector::PrepareRemoteReport(
714 uint32 ssrc,
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000715 const std::string& transport_id,
716 TrackDirection direction) {
717 const std::string ssrc_id = talk_base::ToString<uint32>(ssrc);
henrike@webrtc.org39f831f2014-07-24 14:20:52 +0000718 StatsMap::iterator it = reports_.find(StatsId(
719 StatsReport::kStatsReportTypeRemoteSsrc, ssrc_id, direction));
wu@webrtc.org97077a32013-10-25 21:18:33 +0000720
xians@webrtc.org01bda202014-07-09 07:38:38 +0000721 // Use the ID of the track that is currently mapped to the SSRC, if any.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000722 std::string track_id;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000723 if (!GetTrackIdBySsrc(ssrc, &track_id, direction)) {
henrike@webrtc.org39f831f2014-07-24 14:20:52 +0000724 if (it == reports_.end()) {
xians@webrtc.org01bda202014-07-09 07:38:38 +0000725 // The ssrc is not used by any track or existing report, return NULL
726 // in such case to indicate no report is prepared for the ssrc.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000727 return NULL;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000728 }
729
730 // The ssrc is not used by any existing track. Keeps the old track id
731 // since we want to report the stats for inactive ssrc.
henrike@webrtc.org39f831f2014-07-24 14:20:52 +0000732 ExtractValueFromReport(it->second,
wu@webrtc.org97077a32013-10-25 21:18:33 +0000733 StatsReport::kStatsValueNameTrackId,
734 &track_id);
735 }
736
henrike@webrtc.org39f831f2014-07-24 14:20:52 +0000737 StatsReport* report = GetOrCreateReport(
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000738 StatsReport::kStatsReportTypeRemoteSsrc, ssrc_id, direction);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000739
740 // Clear out stats from previous GatherStats calls if any.
741 // The timestamp will be added later. Zero it for debugging.
742 report->values.clear();
743 report->timestamp = 0;
744
745 report->AddValue(StatsReport::kStatsValueNameSsrc, ssrc_id);
746 report->AddValue(StatsReport::kStatsValueNameTrackId, track_id);
747 // Add the mapping of SSRC to transport.
748 report->AddValue(StatsReport::kStatsValueNameTransportId,
749 transport_id);
750 return report;
751}
752
wu@webrtc.org4551b792013-10-09 15:37:36 +0000753std::string StatsCollector::AddOneCertificateReport(
754 const talk_base::SSLCertificate* cert, const std::string& issuer_id) {
755 // TODO(bemasc): Move this computation to a helper class that caches these
756 // values to reduce CPU use in GetStats. This will require adding a fast
757 // SSLCertificate::Equals() method to detect certificate changes.
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000758
759 std::string digest_algorithm;
760 if (!cert->GetSignatureDigestAlgorithm(&digest_algorithm))
761 return std::string();
762
wu@webrtc.org4551b792013-10-09 15:37:36 +0000763 talk_base::scoped_ptr<talk_base::SSLFingerprint> ssl_fingerprint(
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000764 talk_base::SSLFingerprint::Create(digest_algorithm, cert));
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000765
766 // SSLFingerprint::Create can fail if the algorithm returned by
767 // SSLCertificate::GetSignatureDigestAlgorithm is not supported by the
768 // implementation of SSLCertificate::ComputeDigest. This currently happens
769 // with MD5- and SHA-224-signed certificates when linked to libNSS.
770 if (!ssl_fingerprint)
771 return std::string();
772
wu@webrtc.org4551b792013-10-09 15:37:36 +0000773 std::string fingerprint = ssl_fingerprint->GetRfc4572Fingerprint();
774
775 talk_base::Buffer der_buffer;
776 cert->ToDER(&der_buffer);
777 std::string der_base64;
778 talk_base::Base64::EncodeFromArray(
779 der_buffer.data(), der_buffer.length(), &der_base64);
780
henrike@webrtc.org39f831f2014-07-24 14:20:52 +0000781 StatsReport report;
782 report.type = StatsReport::kStatsReportTypeCertificate;
783 report.id = StatsId(report.type, fingerprint);
784 report.timestamp = stats_gathering_started_;
785 report.AddValue(StatsReport::kStatsValueNameFingerprint, fingerprint);
786 report.AddValue(StatsReport::kStatsValueNameFingerprintAlgorithm,
787 digest_algorithm);
788 report.AddValue(StatsReport::kStatsValueNameDer, der_base64);
wu@webrtc.org4551b792013-10-09 15:37:36 +0000789 if (!issuer_id.empty())
henrike@webrtc.org39f831f2014-07-24 14:20:52 +0000790 report.AddValue(StatsReport::kStatsValueNameIssuerId, issuer_id);
791 reports_[report.id] = report;
792 return report.id;
wu@webrtc.org4551b792013-10-09 15:37:36 +0000793}
794
795std::string StatsCollector::AddCertificateReports(
796 const talk_base::SSLCertificate* cert) {
797 // Produces a chain of StatsReports representing this certificate and the rest
798 // of its chain, and adds those reports to |reports_|. The return value is
799 // the id of the leaf report. The provided cert must be non-null, so at least
800 // one report will always be provided and the returned string will never be
801 // empty.
802 ASSERT(cert != NULL);
803
804 std::string issuer_id;
805 talk_base::scoped_ptr<talk_base::SSLCertChain> chain;
806 if (cert->GetChain(chain.accept())) {
807 // This loop runs in reverse, i.e. from root to leaf, so that each
808 // certificate's issuer's report ID is known before the child certificate's
809 // report is generated. The root certificate does not have an issuer ID
810 // value.
811 for (ptrdiff_t i = chain->GetSize() - 1; i >= 0; --i) {
812 const talk_base::SSLCertificate& cert_i = chain->Get(i);
813 issuer_id = AddOneCertificateReport(&cert_i, issuer_id);
814 }
815 }
816 // Add the leaf certificate.
817 return AddOneCertificateReport(cert, issuer_id);
818}
819
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000820void StatsCollector::ExtractSessionInfo() {
821 // Extract information from the base session.
henrike@webrtc.org39f831f2014-07-24 14:20:52 +0000822 StatsReport report;
823 report.id = StatsId(StatsReport::kStatsReportTypeSession, session_->id());
824 report.type = StatsReport::kStatsReportTypeSession;
825 report.timestamp = stats_gathering_started_;
826 report.values.clear();
827 report.AddBoolean(StatsReport::kStatsValueNameInitiator,
828 session_->initiator());
829
830 reports_[report.id] = report;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000831
832 cricket::SessionStats stats;
833 if (session_->GetStats(&stats)) {
834 // Store the proxy map away for use in SSRC reporting.
835 proxy_to_transport_ = stats.proxy_to_transport;
836
837 for (cricket::TransportStatsMap::iterator transport_iter
838 = stats.transport_stats.begin();
839 transport_iter != stats.transport_stats.end(); ++transport_iter) {
wu@webrtc.org4551b792013-10-09 15:37:36 +0000840 // Attempt to get a copy of the certificates from the transport and
841 // expose them in stats reports. All channels in a transport share the
842 // same local and remote certificates.
jiayl@webrtc.org06b04ec2014-07-24 20:41:20 +0000843 //
844 // Note that Transport::GetIdentity and Transport::GetRemoteCertificate
845 // invoke method calls on the worker thread and block this thread, but
846 // messages are still processed on this thread, which may blow way the
847 // existing transports. So we cannot reuse |transport| after these calls.
wu@webrtc.org4551b792013-10-09 15:37:36 +0000848 std::string local_cert_report_id, remote_cert_report_id;
jiayl@webrtc.org06b04ec2014-07-24 20:41:20 +0000849
wu@webrtc.org4551b792013-10-09 15:37:36 +0000850 cricket::Transport* transport =
851 session_->GetTransport(transport_iter->second.content_name);
jiayl@webrtc.org06b04ec2014-07-24 20:41:20 +0000852 talk_base::scoped_ptr<talk_base::SSLIdentity> identity;
853 if (transport && transport->GetIdentity(identity.accept())) {
854 local_cert_report_id =
855 AddCertificateReports(&(identity->certificate()));
wu@webrtc.org4551b792013-10-09 15:37:36 +0000856 }
jiayl@webrtc.org06b04ec2014-07-24 20:41:20 +0000857
858 transport = session_->GetTransport(transport_iter->second.content_name);
859 talk_base::scoped_ptr<talk_base::SSLCertificate> cert;
860 if (transport && transport->GetRemoteCertificate(cert.accept())) {
861 remote_cert_report_id = AddCertificateReports(cert.get());
862 }
863
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000864 for (cricket::TransportChannelStatsList::iterator channel_iter
865 = transport_iter->second.channel_stats.begin();
866 channel_iter != transport_iter->second.channel_stats.end();
867 ++channel_iter) {
henrike@webrtc.org39f831f2014-07-24 14:20:52 +0000868 StatsReport channel_report;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000869 std::ostringstream ostc;
870 ostc << "Channel-" << transport_iter->second.content_name
871 << "-" << channel_iter->component;
henrike@webrtc.org39f831f2014-07-24 14:20:52 +0000872 channel_report.id = ostc.str();
873 channel_report.type = StatsReport::kStatsReportTypeComponent;
874 channel_report.timestamp = stats_gathering_started_;
875 channel_report.AddValue(StatsReport::kStatsValueNameComponent,
876 channel_iter->component);
wu@webrtc.org4551b792013-10-09 15:37:36 +0000877 if (!local_cert_report_id.empty())
henrike@webrtc.org39f831f2014-07-24 14:20:52 +0000878 channel_report.AddValue(
wu@webrtc.org4551b792013-10-09 15:37:36 +0000879 StatsReport::kStatsValueNameLocalCertificateId,
880 local_cert_report_id);
881 if (!remote_cert_report_id.empty())
henrike@webrtc.org39f831f2014-07-24 14:20:52 +0000882 channel_report.AddValue(
wu@webrtc.org4551b792013-10-09 15:37:36 +0000883 StatsReport::kStatsValueNameRemoteCertificateId,
884 remote_cert_report_id);
henrike@webrtc.org39f831f2014-07-24 14:20:52 +0000885 reports_[channel_report.id] = channel_report;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000886 for (size_t i = 0;
887 i < channel_iter->connection_infos.size();
888 ++i) {
henrike@webrtc.org39f831f2014-07-24 14:20:52 +0000889 StatsReport report;
890 const cricket::ConnectionInfo& info
891 = channel_iter->connection_infos[i];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000892 std::ostringstream ost;
893 ost << "Conn-" << transport_iter->first << "-"
894 << channel_iter->component << "-" << i;
henrike@webrtc.org39f831f2014-07-24 14:20:52 +0000895 report.id = ost.str();
896 report.type = StatsReport::kStatsReportTypeCandidatePair;
897 report.timestamp = stats_gathering_started_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000898 // Link from connection to its containing channel.
henrike@webrtc.org39f831f2014-07-24 14:20:52 +0000899 report.AddValue(StatsReport::kStatsValueNameChannelId,
900 channel_report.id);
901 report.AddValue(StatsReport::kStatsValueNameBytesSent,
902 info.sent_total_bytes);
903 report.AddValue(StatsReport::kStatsValueNameBytesReceived,
904 info.recv_total_bytes);
905 report.AddBoolean(StatsReport::kStatsValueNameWritable,
906 info.writable);
907 report.AddBoolean(StatsReport::kStatsValueNameReadable,
908 info.readable);
909 report.AddBoolean(StatsReport::kStatsValueNameActiveConnection,
910 info.best_connection);
911 report.AddValue(StatsReport::kStatsValueNameLocalAddress,
912 info.local_candidate.address().ToString());
913 report.AddValue(StatsReport::kStatsValueNameRemoteAddress,
914 info.remote_candidate.address().ToString());
915 report.AddValue(StatsReport::kStatsValueNameRtt, info.rtt);
916 report.AddValue(StatsReport::kStatsValueNameTransportType,
917 info.local_candidate.protocol());
918 report.AddValue(StatsReport::kStatsValueNameLocalCandidateType,
919 info.local_candidate.type());
920 report.AddValue(StatsReport::kStatsValueNameRemoteCandidateType,
921 info.remote_candidate.type());
922 reports_[report.id] = report;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000923 }
924 }
925 }
926 }
927}
928
929void StatsCollector::ExtractVoiceInfo() {
930 if (!session_->voice_channel()) {
931 return;
932 }
933 cricket::VoiceMediaInfo voice_info;
934 if (!session_->voice_channel()->GetStats(&voice_info)) {
935 LOG(LS_ERROR) << "Failed to get voice channel stats.";
936 return;
937 }
938 std::string transport_id;
tommi@webrtc.org47218952014-07-15 19:22:37 +0000939 if (!GetTransportIdFromProxy(proxy_to_transport_,
940 session_->voice_channel()->content_name(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000941 &transport_id)) {
942 LOG(LS_ERROR) << "Failed to get transport name for proxy "
943 << session_->voice_channel()->content_name();
944 return;
945 }
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000946 ExtractStatsFromList(voice_info.receivers, transport_id, this, kReceiving);
947 ExtractStatsFromList(voice_info.senders, transport_id, this, kSending);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000948
949 UpdateStatsFromExistingLocalAudioTracks();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000950}
951
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000952void StatsCollector::ExtractVideoInfo(
953 PeerConnectionInterface::StatsOutputLevel level) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000954 if (!session_->video_channel()) {
955 return;
956 }
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000957 cricket::StatsOptions options;
958 options.include_received_propagation_stats =
959 (level >= PeerConnectionInterface::kStatsOutputLevelDebug) ?
960 true : false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000961 cricket::VideoMediaInfo video_info;
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000962 if (!session_->video_channel()->GetStats(options, &video_info)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000963 LOG(LS_ERROR) << "Failed to get video channel stats.";
964 return;
965 }
966 std::string transport_id;
tommi@webrtc.org47218952014-07-15 19:22:37 +0000967 if (!GetTransportIdFromProxy(proxy_to_transport_,
968 session_->video_channel()->content_name(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000969 &transport_id)) {
970 LOG(LS_ERROR) << "Failed to get transport name for proxy "
971 << session_->video_channel()->content_name();
972 return;
973 }
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000974 ExtractStatsFromList(video_info.receivers, transport_id, this, kReceiving);
975 ExtractStatsFromList(video_info.senders, transport_id, this, kSending);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000976 if (video_info.bw_estimations.size() != 1) {
977 LOG(LS_ERROR) << "BWEs count: " << video_info.bw_estimations.size();
978 } else {
henrike@webrtc.org39f831f2014-07-24 14:20:52 +0000979 StatsReport* report = &reports_[StatsReport::kStatsReportVideoBweId];
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000980 ExtractStats(
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000981 video_info.bw_estimations[0], stats_gathering_started_, level, report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000982 }
983}
984
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000985StatsReport* StatsCollector::GetReport(const std::string& type,
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000986 const std::string& id,
987 TrackDirection direction) {
988 ASSERT(type == StatsReport::kStatsReportTypeSsrc ||
989 type == StatsReport::kStatsReportTypeRemoteSsrc);
henrike@webrtc.org39f831f2014-07-24 14:20:52 +0000990 std::string statsid = StatsId(type, id, direction);
991 StatsReport* report = NULL;
992 std::map<std::string, StatsReport>::iterator it = reports_.find(statsid);
993 if (it != reports_.end())
994 report = &(it->second);
995
996 return report;
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000997}
998
999StatsReport* StatsCollector::GetOrCreateReport(const std::string& type,
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001000 const std::string& id,
1001 TrackDirection direction) {
1002 ASSERT(type == StatsReport::kStatsReportTypeSsrc ||
1003 type == StatsReport::kStatsReportTypeRemoteSsrc);
1004 StatsReport* report = GetReport(type, id, direction);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001005 if (report == NULL) {
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001006 std::string statsid = StatsId(type, id, direction);
henrike@webrtc.org39f831f2014-07-24 14:20:52 +00001007 report = &reports_[statsid]; // Create new element.
1008 report->id = statsid;
wu@webrtc.org97077a32013-10-25 21:18:33 +00001009 report->type = type;
wu@webrtc.org97077a32013-10-25 21:18:33 +00001010 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001011
wu@webrtc.org97077a32013-10-25 21:18:33 +00001012 return report;
1013}
1014
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001015void StatsCollector::UpdateStatsFromExistingLocalAudioTracks() {
1016 // Loop through the existing local audio tracks.
1017 for (LocalAudioTrackVector::const_iterator it = local_audio_tracks_.begin();
1018 it != local_audio_tracks_.end(); ++it) {
1019 AudioTrackInterface* track = it->first;
1020 uint32 ssrc = it->second;
1021 std::string ssrc_id = talk_base::ToString<uint32>(ssrc);
1022 StatsReport* report = GetReport(StatsReport::kStatsReportTypeSsrc,
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001023 ssrc_id,
1024 kSending);
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +00001025 if (report == NULL) {
1026 // This can happen if a local audio track is added to a stream on the
1027 // fly and the report has not been set up yet. Do nothing in this case.
1028 LOG(LS_ERROR) << "Stats report does not exist for ssrc " << ssrc;
1029 continue;
1030 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001031
1032 // The same ssrc can be used by both local and remote audio tracks.
1033 std::string track_id;
1034 if (!ExtractValueFromReport(*report,
1035 StatsReport::kStatsValueNameTrackId,
1036 &track_id) ||
1037 track_id != track->id()) {
1038 continue;
1039 }
1040
1041 UpdateReportFromAudioTrack(track, report);
1042 }
1043}
1044
1045void StatsCollector::UpdateReportFromAudioTrack(AudioTrackInterface* track,
1046 StatsReport* report) {
1047 ASSERT(track != NULL);
1048 if (report == NULL)
1049 return;
1050
1051 int signal_level = 0;
1052 if (track->GetSignalLevel(&signal_level)) {
1053 report->ReplaceValue(StatsReport::kStatsValueNameAudioInputLevel,
1054 talk_base::ToString<int>(signal_level));
1055 }
1056
1057 talk_base::scoped_refptr<AudioProcessorInterface> audio_processor(
1058 track->GetAudioProcessor());
1059 if (audio_processor.get() == NULL)
1060 return;
1061
1062 AudioProcessorInterface::AudioProcessorStats stats;
1063 audio_processor->GetStats(&stats);
1064 report->ReplaceValue(StatsReport::kStatsValueNameTypingNoiseState,
1065 stats.typing_noise_detected ? "true" : "false");
1066 report->ReplaceValue(StatsReport::kStatsValueNameEchoReturnLoss,
1067 talk_base::ToString<int>(stats.echo_return_loss));
1068 report->ReplaceValue(
1069 StatsReport::kStatsValueNameEchoReturnLossEnhancement,
1070 talk_base::ToString<int>(stats.echo_return_loss_enhancement));
1071 report->ReplaceValue(StatsReport::kStatsValueNameEchoDelayMedian,
1072 talk_base::ToString<int>(stats.echo_delay_median_ms));
1073 report->ReplaceValue(StatsReport::kStatsValueNameEchoCancellationQualityMin,
1074 talk_base::ToString<float>(stats.aec_quality_min));
1075 report->ReplaceValue(StatsReport::kStatsValueNameEchoDelayStdDev,
1076 talk_base::ToString<int>(stats.echo_delay_std_ms));
1077}
1078
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001079bool StatsCollector::GetTrackIdBySsrc(uint32 ssrc, std::string* track_id,
1080 TrackDirection direction) {
1081 if (direction == kSending) {
tommi@webrtc.org03505bc2014-07-14 20:15:26 +00001082 if (!session_->GetLocalTrackIdBySsrc(ssrc, track_id)) {
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001083 LOG(LS_WARNING) << "The SSRC " << ssrc
1084 << " is not associated with a sending track";
1085 return false;
1086 }
1087 } else {
1088 ASSERT(direction == kReceiving);
tommi@webrtc.org03505bc2014-07-14 20:15:26 +00001089 if (!session_->GetRemoteTrackIdBySsrc(ssrc, track_id)) {
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001090 LOG(LS_WARNING) << "The SSRC " << ssrc
1091 << " is not associated with a receiving track";
1092 return false;
1093 }
1094 }
1095
1096 return true;
1097}
1098
xians@webrtc.org01bda202014-07-09 07:38:38 +00001099void StatsCollector::ClearUpdateStatsCache() {
1100 stats_gathering_started_ = 0;
1101}
1102
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001103} // namespace webrtc