blob: dd615ab89ef7f5f5c663d288d25952f26697d5cf [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 {
237typedef std::map<std::string, StatsReport> StatsMap;
238
239std::string StatsId(const std::string& type, const std::string& id) {
240 return type + "_" + id;
241}
242
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000243std::string StatsId(const std::string& type, const std::string& id,
244 StatsCollector::TrackDirection direction) {
245 ASSERT(direction == StatsCollector::kSending ||
246 direction == StatsCollector::kReceiving);
247
248 // Strings for the direction of the track.
249 const char kSendDirection[] = "send";
250 const char kRecvDirection[] = "recv";
251
252 const std::string direction_id = (direction == StatsCollector::kSending) ?
253 kSendDirection : kRecvDirection;
254 return type + "_" + id + "_" + direction_id;
255}
256
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000257bool ExtractValueFromReport(
258 const StatsReport& report,
tommi@webrtc.org242068d2014-07-14 20:19:56 +0000259 StatsReport::StatsValueName name,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000260 std::string* value) {
261 StatsReport::Values::const_iterator it = report.values.begin();
262 for (; it != report.values.end(); ++it) {
263 if (it->name == name) {
264 *value = it->value;
265 return true;
266 }
267 }
268 return false;
269}
270
xians@webrtc.org01bda202014-07-09 07:38:38 +0000271void AddTrackReport(StatsMap* reports, const std::string& track_id) {
272 // Adds an empty track report.
273 StatsReport report;
274 report.type = StatsReport::kStatsReportTypeTrack;
275 report.id = StatsId(StatsReport::kStatsReportTypeTrack, track_id);
276 report.AddValue(StatsReport::kStatsValueNameTrackId, track_id);
277 (*reports)[report.id] = report;
278}
279
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000280template <class TrackVector>
281void CreateTrackReports(const TrackVector& tracks, StatsMap* reports) {
282 for (size_t j = 0; j < tracks.size(); ++j) {
283 webrtc::MediaStreamTrackInterface* track = tracks[j];
xians@webrtc.org01bda202014-07-09 07:38:38 +0000284 AddTrackReport(reports, track->id());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000285 }
286}
287
288void ExtractStats(const cricket::VoiceReceiverInfo& info, StatsReport* report) {
289 report->AddValue(StatsReport::kStatsValueNameAudioOutputLevel,
290 info.audio_level);
291 report->AddValue(StatsReport::kStatsValueNameBytesReceived,
292 info.bytes_rcvd);
293 report->AddValue(StatsReport::kStatsValueNameJitterReceived,
294 info.jitter_ms);
jiayl@webrtc.org11aab0e2014-03-07 18:56:26 +0000295 report->AddValue(StatsReport::kStatsValueNameJitterBufferMs,
296 info.jitter_buffer_ms);
297 report->AddValue(StatsReport::kStatsValueNamePreferredJitterBufferMs,
298 info.jitter_buffer_preferred_ms);
299 report->AddValue(StatsReport::kStatsValueNameCurrentDelayMs,
300 info.delay_estimate_ms);
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000301 report->AddValue(StatsReport::kStatsValueNameExpandRate,
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000302 talk_base::ToString<float>(info.expand_rate));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000303 report->AddValue(StatsReport::kStatsValueNamePacketsReceived,
304 info.packets_rcvd);
305 report->AddValue(StatsReport::kStatsValueNamePacketsLost,
306 info.packets_lost);
buildbot@webrtc.org3e01e0b2014-05-13 17:54:10 +0000307 report->AddValue(StatsReport::kStatsValueNameDecodingCTSG,
308 info.decoding_calls_to_silence_generator);
309 report->AddValue(StatsReport::kStatsValueNameDecodingCTN,
310 info.decoding_calls_to_neteq);
311 report->AddValue(StatsReport::kStatsValueNameDecodingNormal,
312 info.decoding_normal);
313 report->AddValue(StatsReport::kStatsValueNameDecodingPLC,
314 info.decoding_plc);
315 report->AddValue(StatsReport::kStatsValueNameDecodingCNG,
316 info.decoding_cng);
317 report->AddValue(StatsReport::kStatsValueNameDecodingPLCCNG,
318 info.decoding_plc_cng);
buildbot@webrtc.orgb525a9d2014-06-03 09:42:15 +0000319 report->AddValue(StatsReport::kStatsValueNameCaptureStartNtpTimeMs,
320 info.capture_start_ntp_time_ms);
buildbot@webrtc.org7e71b772014-06-13 01:14:01 +0000321 report->AddValue(StatsReport::kStatsValueNameCodecName, info.codec_name);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000322}
323
324void ExtractStats(const cricket::VoiceSenderInfo& info, StatsReport* report) {
325 report->AddValue(StatsReport::kStatsValueNameAudioInputLevel,
326 info.audio_level);
327 report->AddValue(StatsReport::kStatsValueNameBytesSent,
328 info.bytes_sent);
329 report->AddValue(StatsReport::kStatsValueNamePacketsSent,
330 info.packets_sent);
henrike@webrtc.orgffe26202014-03-19 22:20:10 +0000331 report->AddValue(StatsReport::kStatsValueNamePacketsLost,
332 info.packets_lost);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000333 report->AddValue(StatsReport::kStatsValueNameJitterReceived,
334 info.jitter_ms);
335 report->AddValue(StatsReport::kStatsValueNameRtt, info.rtt_ms);
336 report->AddValue(StatsReport::kStatsValueNameEchoCancellationQualityMin,
337 talk_base::ToString<float>(info.aec_quality_min));
338 report->AddValue(StatsReport::kStatsValueNameEchoDelayMedian,
339 info.echo_delay_median_ms);
340 report->AddValue(StatsReport::kStatsValueNameEchoDelayStdDev,
341 info.echo_delay_std_ms);
342 report->AddValue(StatsReport::kStatsValueNameEchoReturnLoss,
343 info.echo_return_loss);
344 report->AddValue(StatsReport::kStatsValueNameEchoReturnLossEnhancement,
345 info.echo_return_loss_enhancement);
346 report->AddValue(StatsReport::kStatsValueNameCodecName, info.codec_name);
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000347 report->AddBoolean(StatsReport::kStatsValueNameTypingNoiseState,
348 info.typing_noise_detected);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000349}
350
351void ExtractStats(const cricket::VideoReceiverInfo& info, StatsReport* report) {
352 report->AddValue(StatsReport::kStatsValueNameBytesReceived,
353 info.bytes_rcvd);
354 report->AddValue(StatsReport::kStatsValueNamePacketsReceived,
355 info.packets_rcvd);
356 report->AddValue(StatsReport::kStatsValueNamePacketsLost,
357 info.packets_lost);
358
359 report->AddValue(StatsReport::kStatsValueNameFirsSent,
360 info.firs_sent);
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +0000361 report->AddValue(StatsReport::kStatsValueNamePlisSent,
362 info.plis_sent);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000363 report->AddValue(StatsReport::kStatsValueNameNacksSent,
364 info.nacks_sent);
365 report->AddValue(StatsReport::kStatsValueNameFrameWidthReceived,
366 info.frame_width);
367 report->AddValue(StatsReport::kStatsValueNameFrameHeightReceived,
368 info.frame_height);
369 report->AddValue(StatsReport::kStatsValueNameFrameRateReceived,
370 info.framerate_rcvd);
371 report->AddValue(StatsReport::kStatsValueNameFrameRateDecoded,
372 info.framerate_decoded);
373 report->AddValue(StatsReport::kStatsValueNameFrameRateOutput,
374 info.framerate_output);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000375
376 report->AddValue(StatsReport::kStatsValueNameDecodeMs,
377 info.decode_ms);
378 report->AddValue(StatsReport::kStatsValueNameMaxDecodeMs,
379 info.max_decode_ms);
380 report->AddValue(StatsReport::kStatsValueNameCurrentDelayMs,
381 info.current_delay_ms);
382 report->AddValue(StatsReport::kStatsValueNameTargetDelayMs,
383 info.target_delay_ms);
384 report->AddValue(StatsReport::kStatsValueNameJitterBufferMs,
385 info.jitter_buffer_ms);
386 report->AddValue(StatsReport::kStatsValueNameMinPlayoutDelayMs,
387 info.min_playout_delay_ms);
388 report->AddValue(StatsReport::kStatsValueNameRenderDelayMs,
389 info.render_delay_ms);
buildbot@webrtc.org0581f0b2014-05-06 21:36:31 +0000390
391 report->AddValue(StatsReport::kStatsValueNameCaptureStartNtpTimeMs,
392 info.capture_start_ntp_time_ms);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000393}
394
395void ExtractStats(const cricket::VideoSenderInfo& info, StatsReport* report) {
396 report->AddValue(StatsReport::kStatsValueNameBytesSent,
397 info.bytes_sent);
398 report->AddValue(StatsReport::kStatsValueNamePacketsSent,
399 info.packets_sent);
henrike@webrtc.orgffe26202014-03-19 22:20:10 +0000400 report->AddValue(StatsReport::kStatsValueNamePacketsLost,
401 info.packets_lost);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000402
403 report->AddValue(StatsReport::kStatsValueNameFirsReceived,
404 info.firs_rcvd);
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +0000405 report->AddValue(StatsReport::kStatsValueNamePlisReceived,
406 info.plis_rcvd);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000407 report->AddValue(StatsReport::kStatsValueNameNacksReceived,
408 info.nacks_rcvd);
wu@webrtc.org987f2c92014-03-28 16:22:19 +0000409 report->AddValue(StatsReport::kStatsValueNameFrameWidthInput,
410 info.input_frame_width);
411 report->AddValue(StatsReport::kStatsValueNameFrameHeightInput,
412 info.input_frame_height);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000413 report->AddValue(StatsReport::kStatsValueNameFrameWidthSent,
wu@webrtc.org987f2c92014-03-28 16:22:19 +0000414 info.send_frame_width);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000415 report->AddValue(StatsReport::kStatsValueNameFrameHeightSent,
wu@webrtc.org987f2c92014-03-28 16:22:19 +0000416 info.send_frame_height);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000417 report->AddValue(StatsReport::kStatsValueNameFrameRateInput,
418 info.framerate_input);
419 report->AddValue(StatsReport::kStatsValueNameFrameRateSent,
420 info.framerate_sent);
421 report->AddValue(StatsReport::kStatsValueNameRtt, info.rtt_ms);
422 report->AddValue(StatsReport::kStatsValueNameCodecName, info.codec_name);
mallinath@webrtc.org62451dc2013-12-13 12:29:34 +0000423 report->AddBoolean(StatsReport::kStatsValueNameCpuLimitedResolution,
424 (info.adapt_reason & 0x1) > 0);
425 report->AddBoolean(StatsReport::kStatsValueNameBandwidthLimitedResolution,
426 (info.adapt_reason & 0x2) > 0);
427 report->AddBoolean(StatsReport::kStatsValueNameViewLimitedResolution,
428 (info.adapt_reason & 0x4) > 0);
buildbot@webrtc.org71dffb72014-06-24 07:24:49 +0000429 report->AddValue(StatsReport::kStatsValueNameAdaptationChanges,
430 info.adapt_changes);
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000431 report->AddValue(StatsReport::kStatsValueNameAvgEncodeMs, info.avg_encode_ms);
432 report->AddValue(StatsReport::kStatsValueNameCaptureJitterMs,
433 info.capture_jitter_ms);
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000434 report->AddValue(StatsReport::kStatsValueNameCaptureQueueDelayMsPerS,
435 info.capture_queue_delay_ms_per_s);
436 report->AddValue(StatsReport::kStatsValueNameEncodeUsagePercent,
437 info.encode_usage_percent);
buildbot@webrtc.orgc800c1c2014-06-13 07:56:17 +0000438 report->AddValue(StatsReport::kStatsValueNameEncodeRelStdDev,
439 info.encode_rsd);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000440}
441
442void ExtractStats(const cricket::BandwidthEstimationInfo& info,
443 double stats_gathering_started,
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000444 PeerConnectionInterface::StatsOutputLevel level,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000445 StatsReport* report) {
446 report->id = StatsReport::kStatsReportVideoBweId;
447 report->type = StatsReport::kStatsReportTypeBwe;
448
449 // Clear out stats from previous GatherStats calls if any.
450 if (report->timestamp != stats_gathering_started) {
451 report->values.clear();
452 report->timestamp = stats_gathering_started;
453 }
454
455 report->AddValue(StatsReport::kStatsValueNameAvailableSendBandwidth,
456 info.available_send_bandwidth);
457 report->AddValue(StatsReport::kStatsValueNameAvailableReceiveBandwidth,
458 info.available_recv_bandwidth);
459 report->AddValue(StatsReport::kStatsValueNameTargetEncBitrate,
460 info.target_enc_bitrate);
461 report->AddValue(StatsReport::kStatsValueNameActualEncBitrate,
462 info.actual_enc_bitrate);
463 report->AddValue(StatsReport::kStatsValueNameRetransmitBitrate,
464 info.retransmit_bitrate);
465 report->AddValue(StatsReport::kStatsValueNameTransmitBitrate,
466 info.transmit_bitrate);
467 report->AddValue(StatsReport::kStatsValueNameBucketDelay,
468 info.bucket_delay);
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000469 if (level >= PeerConnectionInterface::kStatsOutputLevelDebug) {
470 report->AddValue(
471 StatsReport::kStatsValueNameRecvPacketGroupPropagationDeltaSumDebug,
472 info.total_received_propagation_delta_ms);
473 if (info.recent_received_propagation_delta_ms.size() > 0) {
474 report->AddValue(
475 StatsReport::kStatsValueNameRecvPacketGroupPropagationDeltaDebug,
476 info.recent_received_propagation_delta_ms);
477 report->AddValue(
478 StatsReport::kStatsValueNameRecvPacketGroupArrivalTimeDebug,
479 info.recent_received_packet_group_arrival_time_ms);
480 }
481 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000482}
483
wu@webrtc.org97077a32013-10-25 21:18:33 +0000484void ExtractRemoteStats(const cricket::MediaSenderInfo& info,
485 StatsReport* report) {
486 report->timestamp = info.remote_stats[0].timestamp;
487 // TODO(hta): Extract some stats here.
488}
489
490void ExtractRemoteStats(const cricket::MediaReceiverInfo& info,
491 StatsReport* report) {
492 report->timestamp = info.remote_stats[0].timestamp;
493 // TODO(hta): Extract some stats here.
494}
495
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000496// Template to extract stats from a data vector.
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000497// In order to use the template, the functions that are called from it,
498// ExtractStats and ExtractRemoteStats, must be defined and overloaded
499// for each type.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000500template<typename T>
501void ExtractStatsFromList(const std::vector<T>& data,
502 const std::string& transport_id,
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000503 StatsCollector* collector,
504 StatsCollector::TrackDirection direction) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000505 typename std::vector<T>::const_iterator it = data.begin();
506 for (; it != data.end(); ++it) {
507 std::string id;
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000508 uint32 ssrc = it->ssrc();
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000509 // Each track can have stats for both local and remote objects.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000510 // TODO(hta): Handle the case of multiple SSRCs per object.
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000511 StatsReport* report = collector->PrepareLocalReport(ssrc, transport_id,
512 direction);
513 if (report)
514 ExtractStats(*it, report);
515
wu@webrtc.org97077a32013-10-25 21:18:33 +0000516 if (it->remote_stats.size() > 0) {
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000517 report = collector->PrepareRemoteReport(ssrc, transport_id,
518 direction);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000519 if (!report) {
520 continue;
521 }
522 ExtractRemoteStats(*it, report);
523 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000524 }
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000525}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000526
527} // namespace
528
tommi@webrtc.org03505bc2014-07-14 20:15:26 +0000529StatsCollector::StatsCollector(WebRtcSession* session)
530 : session_(session), stats_gathering_started_(0) {
531 ASSERT(session_);
532}
533
534StatsCollector::~StatsCollector() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000535}
536
537// Adds a MediaStream with tracks that can be used as a |selector| in a call
538// to GetStats.
539void StatsCollector::AddStream(MediaStreamInterface* stream) {
540 ASSERT(stream != NULL);
541
542 CreateTrackReports<AudioTrackVector>(stream->GetAudioTracks(),
543 &reports_);
544 CreateTrackReports<VideoTrackVector>(stream->GetVideoTracks(),
545 &reports_);
546}
547
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000548void StatsCollector::AddLocalAudioTrack(AudioTrackInterface* audio_track,
549 uint32 ssrc) {
550 ASSERT(audio_track != NULL);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000551 for (LocalAudioTrackVector::iterator it = local_audio_tracks_.begin();
552 it != local_audio_tracks_.end(); ++it) {
553 ASSERT(it->first != audio_track || it->second != ssrc);
554 }
xians@webrtc.org01bda202014-07-09 07:38:38 +0000555
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000556 local_audio_tracks_.push_back(std::make_pair(audio_track, ssrc));
xians@webrtc.org01bda202014-07-09 07:38:38 +0000557
558 // Create the kStatsReportTypeTrack report for the new track if there is no
559 // report yet.
560 StatsMap::iterator it = reports_.find(
561 StatsId(StatsReport::kStatsReportTypeTrack, audio_track->id()));
562 if (it == reports_.end())
563 AddTrackReport(&reports_, audio_track->id());
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000564}
565
566void StatsCollector::RemoveLocalAudioTrack(AudioTrackInterface* audio_track,
567 uint32 ssrc) {
568 ASSERT(audio_track != NULL);
569 for (LocalAudioTrackVector::iterator it = local_audio_tracks_.begin();
570 it != local_audio_tracks_.end(); ++it) {
571 if (it->first == audio_track && it->second == ssrc) {
572 local_audio_tracks_.erase(it);
573 return;
574 }
575 }
576
577 ASSERT(false);
578}
579
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000580bool StatsCollector::GetStats(MediaStreamTrackInterface* track,
581 StatsReports* reports) {
582 ASSERT(reports != NULL);
583 reports->clear();
584
585 StatsMap::iterator it;
586 if (!track) {
587 for (it = reports_.begin(); it != reports_.end(); ++it) {
588 reports->push_back(it->second);
589 }
590 return true;
591 }
592
593 it = reports_.find(StatsId(StatsReport::kStatsReportTypeSession,
594 session_->id()));
595 if (it != reports_.end()) {
596 reports->push_back(it->second);
597 }
598
599 it = reports_.find(StatsId(StatsReport::kStatsReportTypeTrack, track->id()));
600
601 if (it == reports_.end()) {
602 LOG(LS_WARNING) << "No StatsReport is available for "<< track->id();
603 return false;
604 }
605
606 reports->push_back(it->second);
607
608 std::string track_id;
609 for (it = reports_.begin(); it != reports_.end(); ++it) {
610 if (it->second.type != StatsReport::kStatsReportTypeSsrc) {
611 continue;
612 }
613 if (ExtractValueFromReport(it->second,
614 StatsReport::kStatsValueNameTrackId,
615 &track_id)) {
616 if (track_id == track->id()) {
617 reports->push_back(it->second);
618 }
619 }
620 }
621
622 return true;
623}
624
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000625void
626StatsCollector::UpdateStats(PeerConnectionInterface::StatsOutputLevel level) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000627 double time_now = GetTimeNow();
628 // Calls to UpdateStats() that occur less than kMinGatherStatsPeriod number of
629 // ms apart will be ignored.
630 const double kMinGatherStatsPeriod = 50;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000631 if (stats_gathering_started_ != 0 &&
632 stats_gathering_started_ + kMinGatherStatsPeriod > time_now) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000633 return;
634 }
635 stats_gathering_started_ = time_now;
636
637 if (session_) {
638 ExtractSessionInfo();
639 ExtractVoiceInfo();
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000640 ExtractVideoInfo(level);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000641 }
642}
643
wu@webrtc.org97077a32013-10-25 21:18:33 +0000644StatsReport* StatsCollector::PrepareLocalReport(
645 uint32 ssrc,
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000646 const std::string& transport_id,
647 TrackDirection direction) {
648 const std::string ssrc_id = talk_base::ToString<uint32>(ssrc);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000649 StatsMap::iterator it = reports_.find(StatsId(
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000650 StatsReport::kStatsReportTypeSsrc, ssrc_id, direction));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000651
xians@webrtc.org01bda202014-07-09 07:38:38 +0000652 // Use the ID of the track that is currently mapped to the SSRC, if any.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000653 std::string track_id;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000654 if (!GetTrackIdBySsrc(ssrc, &track_id, direction)) {
655 if (it == reports_.end()) {
656 // The ssrc is not used by any track or existing report, return NULL
657 // in such case to indicate no report is prepared for the ssrc.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000658 return NULL;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000659 }
660
661 // The ssrc is not used by any existing track. Keeps the old track id
662 // since we want to report the stats for inactive ssrc.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000663 ExtractValueFromReport(it->second,
664 StatsReport::kStatsValueNameTrackId,
665 &track_id);
666 }
667
wu@webrtc.org97077a32013-10-25 21:18:33 +0000668 StatsReport* report = GetOrCreateReport(StatsReport::kStatsReportTypeSsrc,
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000669 ssrc_id, direction);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000670
671 // Clear out stats from previous GatherStats calls if any.
xians@webrtc.org01bda202014-07-09 07:38:38 +0000672 // This is required since the report will be returned for the new values.
673 // Having the old values in the report will lead to multiple values with
674 // the same name.
675 // TODO(xians): Consider changing StatsReport to use map instead of vector.
676 report->values.clear();
677 report->timestamp = stats_gathering_started_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000678
679 report->AddValue(StatsReport::kStatsValueNameSsrc, ssrc_id);
680 report->AddValue(StatsReport::kStatsValueNameTrackId, track_id);
681 // Add the mapping of SSRC to transport.
682 report->AddValue(StatsReport::kStatsValueNameTransportId,
683 transport_id);
684 return report;
685}
686
wu@webrtc.org97077a32013-10-25 21:18:33 +0000687StatsReport* StatsCollector::PrepareRemoteReport(
688 uint32 ssrc,
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000689 const std::string& transport_id,
690 TrackDirection direction) {
691 const std::string ssrc_id = talk_base::ToString<uint32>(ssrc);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000692 StatsMap::iterator it = reports_.find(StatsId(
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000693 StatsReport::kStatsReportTypeRemoteSsrc, ssrc_id, direction));
wu@webrtc.org97077a32013-10-25 21:18:33 +0000694
xians@webrtc.org01bda202014-07-09 07:38:38 +0000695 // Use the ID of the track that is currently mapped to the SSRC, if any.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000696 std::string track_id;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000697 if (!GetTrackIdBySsrc(ssrc, &track_id, direction)) {
698 if (it == reports_.end()) {
699 // The ssrc is not used by any track or existing report, return NULL
700 // in such case to indicate no report is prepared for the ssrc.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000701 return NULL;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000702 }
703
704 // The ssrc is not used by any existing track. Keeps the old track id
705 // since we want to report the stats for inactive ssrc.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000706 ExtractValueFromReport(it->second,
707 StatsReport::kStatsValueNameTrackId,
708 &track_id);
709 }
710
711 StatsReport* report = GetOrCreateReport(
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000712 StatsReport::kStatsReportTypeRemoteSsrc, ssrc_id, direction);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000713
714 // Clear out stats from previous GatherStats calls if any.
715 // The timestamp will be added later. Zero it for debugging.
716 report->values.clear();
717 report->timestamp = 0;
718
719 report->AddValue(StatsReport::kStatsValueNameSsrc, ssrc_id);
720 report->AddValue(StatsReport::kStatsValueNameTrackId, track_id);
721 // Add the mapping of SSRC to transport.
722 report->AddValue(StatsReport::kStatsValueNameTransportId,
723 transport_id);
724 return report;
725}
726
wu@webrtc.org4551b792013-10-09 15:37:36 +0000727std::string StatsCollector::AddOneCertificateReport(
728 const talk_base::SSLCertificate* cert, const std::string& issuer_id) {
729 // TODO(bemasc): Move this computation to a helper class that caches these
730 // values to reduce CPU use in GetStats. This will require adding a fast
731 // SSLCertificate::Equals() method to detect certificate changes.
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000732
733 std::string digest_algorithm;
734 if (!cert->GetSignatureDigestAlgorithm(&digest_algorithm))
735 return std::string();
736
wu@webrtc.org4551b792013-10-09 15:37:36 +0000737 talk_base::scoped_ptr<talk_base::SSLFingerprint> ssl_fingerprint(
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000738 talk_base::SSLFingerprint::Create(digest_algorithm, cert));
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000739
740 // SSLFingerprint::Create can fail if the algorithm returned by
741 // SSLCertificate::GetSignatureDigestAlgorithm is not supported by the
742 // implementation of SSLCertificate::ComputeDigest. This currently happens
743 // with MD5- and SHA-224-signed certificates when linked to libNSS.
744 if (!ssl_fingerprint)
745 return std::string();
746
wu@webrtc.org4551b792013-10-09 15:37:36 +0000747 std::string fingerprint = ssl_fingerprint->GetRfc4572Fingerprint();
748
749 talk_base::Buffer der_buffer;
750 cert->ToDER(&der_buffer);
751 std::string der_base64;
752 talk_base::Base64::EncodeFromArray(
753 der_buffer.data(), der_buffer.length(), &der_base64);
754
755 StatsReport report;
756 report.type = StatsReport::kStatsReportTypeCertificate;
757 report.id = StatsId(report.type, fingerprint);
758 report.timestamp = stats_gathering_started_;
759 report.AddValue(StatsReport::kStatsValueNameFingerprint, fingerprint);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000760 report.AddValue(StatsReport::kStatsValueNameFingerprintAlgorithm,
761 digest_algorithm);
wu@webrtc.org4551b792013-10-09 15:37:36 +0000762 report.AddValue(StatsReport::kStatsValueNameDer, der_base64);
763 if (!issuer_id.empty())
764 report.AddValue(StatsReport::kStatsValueNameIssuerId, issuer_id);
765 reports_[report.id] = report;
766 return report.id;
767}
768
769std::string StatsCollector::AddCertificateReports(
770 const talk_base::SSLCertificate* cert) {
771 // Produces a chain of StatsReports representing this certificate and the rest
772 // of its chain, and adds those reports to |reports_|. The return value is
773 // the id of the leaf report. The provided cert must be non-null, so at least
774 // one report will always be provided and the returned string will never be
775 // empty.
776 ASSERT(cert != NULL);
777
778 std::string issuer_id;
779 talk_base::scoped_ptr<talk_base::SSLCertChain> chain;
780 if (cert->GetChain(chain.accept())) {
781 // This loop runs in reverse, i.e. from root to leaf, so that each
782 // certificate's issuer's report ID is known before the child certificate's
783 // report is generated. The root certificate does not have an issuer ID
784 // value.
785 for (ptrdiff_t i = chain->GetSize() - 1; i >= 0; --i) {
786 const talk_base::SSLCertificate& cert_i = chain->Get(i);
787 issuer_id = AddOneCertificateReport(&cert_i, issuer_id);
788 }
789 }
790 // Add the leaf certificate.
791 return AddOneCertificateReport(cert, issuer_id);
792}
793
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000794void StatsCollector::ExtractSessionInfo() {
795 // Extract information from the base session.
796 StatsReport report;
797 report.id = StatsId(StatsReport::kStatsReportTypeSession, session_->id());
798 report.type = StatsReport::kStatsReportTypeSession;
799 report.timestamp = stats_gathering_started_;
800 report.values.clear();
801 report.AddBoolean(StatsReport::kStatsValueNameInitiator,
802 session_->initiator());
803
804 reports_[report.id] = report;
805
806 cricket::SessionStats stats;
807 if (session_->GetStats(&stats)) {
808 // Store the proxy map away for use in SSRC reporting.
809 proxy_to_transport_ = stats.proxy_to_transport;
810
811 for (cricket::TransportStatsMap::iterator transport_iter
812 = stats.transport_stats.begin();
813 transport_iter != stats.transport_stats.end(); ++transport_iter) {
wu@webrtc.org4551b792013-10-09 15:37:36 +0000814 // Attempt to get a copy of the certificates from the transport and
815 // expose them in stats reports. All channels in a transport share the
816 // same local and remote certificates.
817 std::string local_cert_report_id, remote_cert_report_id;
818 cricket::Transport* transport =
819 session_->GetTransport(transport_iter->second.content_name);
820 if (transport) {
821 talk_base::scoped_ptr<talk_base::SSLIdentity> identity;
822 if (transport->GetIdentity(identity.accept()))
823 local_cert_report_id = AddCertificateReports(
824 &(identity->certificate()));
825
826 talk_base::scoped_ptr<talk_base::SSLCertificate> cert;
827 if (transport->GetRemoteCertificate(cert.accept()))
828 remote_cert_report_id = AddCertificateReports(cert.get());
829 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000830 for (cricket::TransportChannelStatsList::iterator channel_iter
831 = transport_iter->second.channel_stats.begin();
832 channel_iter != transport_iter->second.channel_stats.end();
833 ++channel_iter) {
834 StatsReport channel_report;
835 std::ostringstream ostc;
836 ostc << "Channel-" << transport_iter->second.content_name
837 << "-" << channel_iter->component;
838 channel_report.id = ostc.str();
839 channel_report.type = StatsReport::kStatsReportTypeComponent;
840 channel_report.timestamp = stats_gathering_started_;
841 channel_report.AddValue(StatsReport::kStatsValueNameComponent,
842 channel_iter->component);
wu@webrtc.org4551b792013-10-09 15:37:36 +0000843 if (!local_cert_report_id.empty())
844 channel_report.AddValue(
845 StatsReport::kStatsValueNameLocalCertificateId,
846 local_cert_report_id);
847 if (!remote_cert_report_id.empty())
848 channel_report.AddValue(
849 StatsReport::kStatsValueNameRemoteCertificateId,
850 remote_cert_report_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000851 reports_[channel_report.id] = channel_report;
852 for (size_t i = 0;
853 i < channel_iter->connection_infos.size();
854 ++i) {
855 StatsReport report;
856 const cricket::ConnectionInfo& info
857 = channel_iter->connection_infos[i];
858 std::ostringstream ost;
859 ost << "Conn-" << transport_iter->first << "-"
860 << channel_iter->component << "-" << i;
861 report.id = ost.str();
862 report.type = StatsReport::kStatsReportTypeCandidatePair;
863 report.timestamp = stats_gathering_started_;
864 // Link from connection to its containing channel.
865 report.AddValue(StatsReport::kStatsValueNameChannelId,
866 channel_report.id);
867 report.AddValue(StatsReport::kStatsValueNameBytesSent,
868 info.sent_total_bytes);
869 report.AddValue(StatsReport::kStatsValueNameBytesReceived,
870 info.recv_total_bytes);
871 report.AddBoolean(StatsReport::kStatsValueNameWritable,
872 info.writable);
873 report.AddBoolean(StatsReport::kStatsValueNameReadable,
874 info.readable);
875 report.AddBoolean(StatsReport::kStatsValueNameActiveConnection,
876 info.best_connection);
877 report.AddValue(StatsReport::kStatsValueNameLocalAddress,
878 info.local_candidate.address().ToString());
879 report.AddValue(StatsReport::kStatsValueNameRemoteAddress,
880 info.remote_candidate.address().ToString());
wu@webrtc.org97077a32013-10-25 21:18:33 +0000881 report.AddValue(StatsReport::kStatsValueNameRtt, info.rtt);
882 report.AddValue(StatsReport::kStatsValueNameTransportType,
883 info.local_candidate.protocol());
wu@webrtc.org364f2042013-11-20 21:49:41 +0000884 report.AddValue(StatsReport::kStatsValueNameLocalCandidateType,
885 info.local_candidate.type());
886 report.AddValue(StatsReport::kStatsValueNameRemoteCandidateType,
887 info.remote_candidate.type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000888 reports_[report.id] = report;
889 }
890 }
891 }
892 }
893}
894
895void StatsCollector::ExtractVoiceInfo() {
896 if (!session_->voice_channel()) {
897 return;
898 }
899 cricket::VoiceMediaInfo voice_info;
900 if (!session_->voice_channel()->GetStats(&voice_info)) {
901 LOG(LS_ERROR) << "Failed to get voice channel stats.";
902 return;
903 }
904 std::string transport_id;
905 if (!GetTransportIdFromProxy(session_->voice_channel()->content_name(),
906 &transport_id)) {
907 LOG(LS_ERROR) << "Failed to get transport name for proxy "
908 << session_->voice_channel()->content_name();
909 return;
910 }
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000911 ExtractStatsFromList(voice_info.receivers, transport_id, this, kReceiving);
912 ExtractStatsFromList(voice_info.senders, transport_id, this, kSending);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000913
914 UpdateStatsFromExistingLocalAudioTracks();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000915}
916
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000917void StatsCollector::ExtractVideoInfo(
918 PeerConnectionInterface::StatsOutputLevel level) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000919 if (!session_->video_channel()) {
920 return;
921 }
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000922 cricket::StatsOptions options;
923 options.include_received_propagation_stats =
924 (level >= PeerConnectionInterface::kStatsOutputLevelDebug) ?
925 true : false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000926 cricket::VideoMediaInfo video_info;
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000927 if (!session_->video_channel()->GetStats(options, &video_info)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000928 LOG(LS_ERROR) << "Failed to get video channel stats.";
929 return;
930 }
931 std::string transport_id;
932 if (!GetTransportIdFromProxy(session_->video_channel()->content_name(),
933 &transport_id)) {
934 LOG(LS_ERROR) << "Failed to get transport name for proxy "
935 << session_->video_channel()->content_name();
936 return;
937 }
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000938 ExtractStatsFromList(video_info.receivers, transport_id, this, kReceiving);
939 ExtractStatsFromList(video_info.senders, transport_id, this, kSending);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000940 if (video_info.bw_estimations.size() != 1) {
941 LOG(LS_ERROR) << "BWEs count: " << video_info.bw_estimations.size();
942 } else {
943 StatsReport* report = &reports_[StatsReport::kStatsReportVideoBweId];
944 ExtractStats(
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000945 video_info.bw_estimations[0], stats_gathering_started_, level, report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000946 }
947}
948
949double StatsCollector::GetTimeNow() {
tommi@webrtc.orgecb87232014-07-08 12:48:29 +0000950 return talk_base::Timing::WallTimeNow() * talk_base::kNumMillisecsPerSec;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000951}
952
953bool StatsCollector::GetTransportIdFromProxy(const std::string& proxy,
954 std::string* transport) {
955 // TODO(hta): Remove handling of empty proxy name once tests do not use it.
956 if (proxy.empty()) {
957 transport->clear();
958 return true;
959 }
960 if (proxy_to_transport_.find(proxy) == proxy_to_transport_.end()) {
961 LOG(LS_ERROR) << "No transport ID mapping for " << proxy;
962 return false;
963 }
964 std::ostringstream ost;
965 // Component 1 is always used for RTP.
966 ost << "Channel-" << proxy_to_transport_[proxy] << "-1";
967 *transport = ost.str();
968 return true;
969}
970
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000971StatsReport* StatsCollector::GetReport(const std::string& type,
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000972 const std::string& id,
973 TrackDirection direction) {
974 ASSERT(type == StatsReport::kStatsReportTypeSsrc ||
975 type == StatsReport::kStatsReportTypeRemoteSsrc);
976 std::string statsid = StatsId(type, id, direction);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000977 StatsReport* report = NULL;
978 std::map<std::string, StatsReport>::iterator it = reports_.find(statsid);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000979 if (it != reports_.end())
980 report = &(it->second);
981
982 return report;
983}
984
985StatsReport* StatsCollector::GetOrCreateReport(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);
990 StatsReport* report = GetReport(type, id, direction);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000991 if (report == NULL) {
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000992 std::string statsid = StatsId(type, id, direction);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000993 report = &reports_[statsid]; // Create new element.
994 report->id = statsid;
995 report->type = type;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000996 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000997
wu@webrtc.org97077a32013-10-25 21:18:33 +0000998 return report;
999}
1000
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001001void StatsCollector::UpdateStatsFromExistingLocalAudioTracks() {
1002 // Loop through the existing local audio tracks.
1003 for (LocalAudioTrackVector::const_iterator it = local_audio_tracks_.begin();
1004 it != local_audio_tracks_.end(); ++it) {
1005 AudioTrackInterface* track = it->first;
1006 uint32 ssrc = it->second;
1007 std::string ssrc_id = talk_base::ToString<uint32>(ssrc);
1008 StatsReport* report = GetReport(StatsReport::kStatsReportTypeSsrc,
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001009 ssrc_id,
1010 kSending);
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +00001011 if (report == NULL) {
1012 // This can happen if a local audio track is added to a stream on the
1013 // fly and the report has not been set up yet. Do nothing in this case.
1014 LOG(LS_ERROR) << "Stats report does not exist for ssrc " << ssrc;
1015 continue;
1016 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +00001017
1018 // The same ssrc can be used by both local and remote audio tracks.
1019 std::string track_id;
1020 if (!ExtractValueFromReport(*report,
1021 StatsReport::kStatsValueNameTrackId,
1022 &track_id) ||
1023 track_id != track->id()) {
1024 continue;
1025 }
1026
1027 UpdateReportFromAudioTrack(track, report);
1028 }
1029}
1030
1031void StatsCollector::UpdateReportFromAudioTrack(AudioTrackInterface* track,
1032 StatsReport* report) {
1033 ASSERT(track != NULL);
1034 if (report == NULL)
1035 return;
1036
1037 int signal_level = 0;
1038 if (track->GetSignalLevel(&signal_level)) {
1039 report->ReplaceValue(StatsReport::kStatsValueNameAudioInputLevel,
1040 talk_base::ToString<int>(signal_level));
1041 }
1042
1043 talk_base::scoped_refptr<AudioProcessorInterface> audio_processor(
1044 track->GetAudioProcessor());
1045 if (audio_processor.get() == NULL)
1046 return;
1047
1048 AudioProcessorInterface::AudioProcessorStats stats;
1049 audio_processor->GetStats(&stats);
1050 report->ReplaceValue(StatsReport::kStatsValueNameTypingNoiseState,
1051 stats.typing_noise_detected ? "true" : "false");
1052 report->ReplaceValue(StatsReport::kStatsValueNameEchoReturnLoss,
1053 talk_base::ToString<int>(stats.echo_return_loss));
1054 report->ReplaceValue(
1055 StatsReport::kStatsValueNameEchoReturnLossEnhancement,
1056 talk_base::ToString<int>(stats.echo_return_loss_enhancement));
1057 report->ReplaceValue(StatsReport::kStatsValueNameEchoDelayMedian,
1058 talk_base::ToString<int>(stats.echo_delay_median_ms));
1059 report->ReplaceValue(StatsReport::kStatsValueNameEchoCancellationQualityMin,
1060 talk_base::ToString<float>(stats.aec_quality_min));
1061 report->ReplaceValue(StatsReport::kStatsValueNameEchoDelayStdDev,
1062 talk_base::ToString<int>(stats.echo_delay_std_ms));
1063}
1064
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001065bool StatsCollector::GetTrackIdBySsrc(uint32 ssrc, std::string* track_id,
1066 TrackDirection direction) {
1067 if (direction == kSending) {
tommi@webrtc.org03505bc2014-07-14 20:15:26 +00001068 if (!session_->GetLocalTrackIdBySsrc(ssrc, track_id)) {
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001069 LOG(LS_WARNING) << "The SSRC " << ssrc
1070 << " is not associated with a sending track";
1071 return false;
1072 }
1073 } else {
1074 ASSERT(direction == kReceiving);
tommi@webrtc.org03505bc2014-07-14 20:15:26 +00001075 if (!session_->GetRemoteTrackIdBySsrc(ssrc, track_id)) {
xians@webrtc.org4cb01282014-06-12 14:57:05 +00001076 LOG(LS_WARNING) << "The SSRC " << ssrc
1077 << " is not associated with a receiving track";
1078 return false;
1079 }
1080 }
1081
1082 return true;
1083}
1084
xians@webrtc.org01bda202014-07-09 07:38:38 +00001085void StatsCollector::ClearUpdateStatsCache() {
1086 stats_gathering_started_ = 0;
1087}
1088
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001089} // namespace webrtc