blob: a900bba37e58571afe3705420ff8f096b2356cbc [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"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000035#include "talk/session/media/channel.h"
36
37namespace webrtc {
38
39// The items below are in alphabetical order.
40const char StatsReport::kStatsValueNameActiveConnection[] =
41 "googActiveConnection";
42const char StatsReport::kStatsValueNameActualEncBitrate[] =
43 "googActualEncBitrate";
44const char StatsReport::kStatsValueNameAudioOutputLevel[] = "audioOutputLevel";
45const char StatsReport::kStatsValueNameAudioInputLevel[] = "audioInputLevel";
46const char StatsReport::kStatsValueNameAvailableReceiveBandwidth[] =
47 "googAvailableReceiveBandwidth";
48const char StatsReport::kStatsValueNameAvailableSendBandwidth[] =
49 "googAvailableSendBandwidth";
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +000050const char StatsReport::kStatsValueNameAvgEncodeMs[] = "googAvgEncodeMs";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000051const char StatsReport::kStatsValueNameBucketDelay[] = "googBucketDelay";
52const char StatsReport::kStatsValueNameBytesReceived[] = "bytesReceived";
53const char StatsReport::kStatsValueNameBytesSent[] = "bytesSent";
mallinath@webrtc.org62451dc2013-12-13 12:29:34 +000054const char StatsReport::kStatsValueNameBandwidthLimitedResolution[] =
55 "googBandwidthLimitedResolution";
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +000056const char StatsReport::kStatsValueNameCaptureJitterMs[] =
57 "googCaptureJitterMs";
wu@webrtc.org9caf2762013-12-11 18:25:07 +000058const char StatsReport::kStatsValueNameCaptureQueueDelayMsPerS[] =
59 "googCaptureQueueDelayMsPerS";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000060const char StatsReport::kStatsValueNameChannelId[] = "googChannelId";
61const char StatsReport::kStatsValueNameCodecName[] = "googCodecName";
62const char StatsReport::kStatsValueNameComponent[] = "googComponent";
63const char StatsReport::kStatsValueNameContentName[] = "googContentName";
mallinath@webrtc.org62451dc2013-12-13 12:29:34 +000064const char StatsReport::kStatsValueNameCpuLimitedResolution[] =
65 "googCpuLimitedResolution";
wu@webrtc.org4551b792013-10-09 15:37:36 +000066const char StatsReport::kStatsValueNameDer[] = "googDerBase64";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000067// Echo metrics from the audio processing module.
68const char StatsReport::kStatsValueNameEchoCancellationQualityMin[] =
69 "googEchoCancellationQualityMin";
70const char StatsReport::kStatsValueNameEchoDelayMedian[] =
71 "googEchoCancellationEchoDelayMedian";
72const char StatsReport::kStatsValueNameEchoDelayStdDev[] =
73 "googEchoCancellationEchoDelayStdDev";
74const char StatsReport::kStatsValueNameEchoReturnLoss[] =
75 "googEchoCancellationReturnLoss";
76const char StatsReport::kStatsValueNameEchoReturnLossEnhancement[] =
77 "googEchoCancellationReturnLossEnhancement";
78
wu@webrtc.org9caf2762013-12-11 18:25:07 +000079const char StatsReport::kStatsValueNameEncodeUsagePercent[] =
80 "googEncodeUsagePercent";
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +000081const char StatsReport::kStatsValueNameExpandRate[] = "googExpandRate";
wu@webrtc.org4551b792013-10-09 15:37:36 +000082const char StatsReport::kStatsValueNameFingerprint[] = "googFingerprint";
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +000083const char StatsReport::kStatsValueNameFingerprintAlgorithm[] =
84 "googFingerprintAlgorithm";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000085const char StatsReport::kStatsValueNameFirsReceived[] = "googFirsReceived";
86const char StatsReport::kStatsValueNameFirsSent[] = "googFirsSent";
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +000087const char StatsReport::kStatsValueNameFrameHeightInput[] =
88 "googFrameHeightInput";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000089const char StatsReport::kStatsValueNameFrameHeightReceived[] =
90 "googFrameHeightReceived";
91const char StatsReport::kStatsValueNameFrameHeightSent[] =
92 "googFrameHeightSent";
93const char StatsReport::kStatsValueNameFrameRateReceived[] =
94 "googFrameRateReceived";
95const char StatsReport::kStatsValueNameFrameRateDecoded[] =
96 "googFrameRateDecoded";
97const char StatsReport::kStatsValueNameFrameRateOutput[] =
98 "googFrameRateOutput";
wu@webrtc.org97077a32013-10-25 21:18:33 +000099const char StatsReport::kStatsValueNameDecodeMs[] = "googDecodeMs";
100const char StatsReport::kStatsValueNameMaxDecodeMs[] = "googMaxDecodeMs";
101const char StatsReport::kStatsValueNameCurrentDelayMs[] = "googCurrentDelayMs";
102const char StatsReport::kStatsValueNameTargetDelayMs[] = "googTargetDelayMs";
103const char StatsReport::kStatsValueNameJitterBufferMs[] = "googJitterBufferMs";
104const char StatsReport::kStatsValueNameMinPlayoutDelayMs[] =
105 "googMinPlayoutDelayMs";
106const char StatsReport::kStatsValueNameRenderDelayMs[] = "googRenderDelayMs";
107
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000108const char StatsReport::kStatsValueNameFrameRateInput[] = "googFrameRateInput";
109const char StatsReport::kStatsValueNameFrameRateSent[] = "googFrameRateSent";
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000110const char StatsReport::kStatsValueNameFrameWidthInput[] =
111 "googFrameWidthInput";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000112const char StatsReport::kStatsValueNameFrameWidthReceived[] =
113 "googFrameWidthReceived";
114const char StatsReport::kStatsValueNameFrameWidthSent[] = "googFrameWidthSent";
115const char StatsReport::kStatsValueNameInitiator[] = "googInitiator";
wu@webrtc.org4551b792013-10-09 15:37:36 +0000116const char StatsReport::kStatsValueNameIssuerId[] = "googIssuerId";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000117const char StatsReport::kStatsValueNameJitterReceived[] = "googJitterReceived";
118const char StatsReport::kStatsValueNameLocalAddress[] = "googLocalAddress";
wu@webrtc.org364f2042013-11-20 21:49:41 +0000119const char StatsReport::kStatsValueNameLocalCandidateType[] =
120 "googLocalCandidateType";
wu@webrtc.org4551b792013-10-09 15:37:36 +0000121const char StatsReport::kStatsValueNameLocalCertificateId[] =
122 "googLocalCertificateId";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000123const char StatsReport::kStatsValueNameNacksReceived[] = "googNacksReceived";
124const char StatsReport::kStatsValueNameNacksSent[] = "googNacksSent";
125const char StatsReport::kStatsValueNamePacketsReceived[] = "packetsReceived";
126const char StatsReport::kStatsValueNamePacketsSent[] = "packetsSent";
127const char StatsReport::kStatsValueNamePacketsLost[] = "packetsLost";
128const char StatsReport::kStatsValueNameReadable[] = "googReadable";
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000129const char StatsReport::kStatsValueNameRecvPacketGroupArrivalTimeDebug[] =
130 "googReceivedPacketGroupArrivalTimeDebug";
131const char StatsReport::kStatsValueNameRecvPacketGroupPropagationDeltaDebug[] =
132 "googReceivedPacketGroupPropagationDeltaDebug";
133const char
134StatsReport::kStatsValueNameRecvPacketGroupPropagationDeltaSumDebug[] =
135 "googReceivedPacketGroupPropagationDeltaSumDebug";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000136const char StatsReport::kStatsValueNameRemoteAddress[] = "googRemoteAddress";
wu@webrtc.org364f2042013-11-20 21:49:41 +0000137const char StatsReport::kStatsValueNameRemoteCandidateType[] =
138 "googRemoteCandidateType";
wu@webrtc.org4551b792013-10-09 15:37:36 +0000139const char StatsReport::kStatsValueNameRemoteCertificateId[] =
140 "googRemoteCertificateId";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000141const char StatsReport::kStatsValueNameRetransmitBitrate[] =
142 "googRetransmitBitrate";
143const char StatsReport::kStatsValueNameRtt[] = "googRtt";
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000144const char StatsReport::kStatsValueNameSsrc[] = "ssrc";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000145const char StatsReport::kStatsValueNameTargetEncBitrate[] =
146 "googTargetEncBitrate";
147const char StatsReport::kStatsValueNameTransmitBitrate[] =
148 "googTransmitBitrate";
149const char StatsReport::kStatsValueNameTransportId[] = "transportId";
150const char StatsReport::kStatsValueNameTransportType[] = "googTransportType";
151const char StatsReport::kStatsValueNameTrackId[] = "googTrackId";
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000152const char StatsReport::kStatsValueNameTypingNoiseState[] =
153 "googTypingNoiseState";
mallinath@webrtc.org62451dc2013-12-13 12:29:34 +0000154const char StatsReport::kStatsValueNameViewLimitedResolution[] =
155 "googViewLimitedResolution";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000156const char StatsReport::kStatsValueNameWritable[] = "googWritable";
157
158const char StatsReport::kStatsReportTypeSession[] = "googLibjingleSession";
159const char StatsReport::kStatsReportTypeBwe[] = "VideoBwe";
wu@webrtc.org97077a32013-10-25 21:18:33 +0000160const char StatsReport::kStatsReportTypeRemoteSsrc[] = "remoteSsrc";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000161const char StatsReport::kStatsReportTypeSsrc[] = "ssrc";
162const char StatsReport::kStatsReportTypeTrack[] = "googTrack";
163const char StatsReport::kStatsReportTypeIceCandidate[] = "iceCandidate";
164const char StatsReport::kStatsReportTypeTransport[] = "googTransport";
165const char StatsReport::kStatsReportTypeComponent[] = "googComponent";
166const char StatsReport::kStatsReportTypeCandidatePair[] = "googCandidatePair";
wu@webrtc.org4551b792013-10-09 15:37:36 +0000167const char StatsReport::kStatsReportTypeCertificate[] = "googCertificate";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000168
169const char StatsReport::kStatsReportVideoBweId[] = "bweforvideo";
170
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000171
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000172// Implementations of functions in statstypes.h
173void StatsReport::AddValue(const std::string& name, const std::string& value) {
174 Value temp;
175 temp.name = name;
176 temp.value = value;
177 values.push_back(temp);
178}
179
180void StatsReport::AddValue(const std::string& name, int64 value) {
181 AddValue(name, talk_base::ToString<int64>(value));
182}
183
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000184template <typename T>
185void StatsReport::AddValue(const std::string& name,
186 const std::vector<T>& value) {
187 std::ostringstream oss;
188 oss << "[";
189 for (size_t i = 0; i < value.size(); ++i) {
190 oss << talk_base::ToString<T>(value[i]);
191 if (i != value.size() - 1)
192 oss << ", ";
193 }
194 oss << "]";
195 AddValue(name, oss.str());
196}
197
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000198void StatsReport::AddBoolean(const std::string& name, bool value) {
199 AddValue(name, value ? "true" : "false");
200}
201
202namespace {
203typedef std::map<std::string, StatsReport> StatsMap;
204
205std::string StatsId(const std::string& type, const std::string& id) {
206 return type + "_" + id;
207}
208
209bool ExtractValueFromReport(
210 const StatsReport& report,
211 const std::string& name,
212 std::string* value) {
213 StatsReport::Values::const_iterator it = report.values.begin();
214 for (; it != report.values.end(); ++it) {
215 if (it->name == name) {
216 *value = it->value;
217 return true;
218 }
219 }
220 return false;
221}
222
223template <class TrackVector>
224void CreateTrackReports(const TrackVector& tracks, StatsMap* reports) {
225 for (size_t j = 0; j < tracks.size(); ++j) {
226 webrtc::MediaStreamTrackInterface* track = tracks[j];
227 // Adds an empty track report.
228 StatsReport report;
229 report.type = StatsReport::kStatsReportTypeTrack;
230 report.id = StatsId(StatsReport::kStatsReportTypeTrack, track->id());
231 report.AddValue(StatsReport::kStatsValueNameTrackId,
232 track->id());
233 (*reports)[report.id] = report;
234 }
235}
236
237void ExtractStats(const cricket::VoiceReceiverInfo& info, StatsReport* report) {
238 report->AddValue(StatsReport::kStatsValueNameAudioOutputLevel,
239 info.audio_level);
240 report->AddValue(StatsReport::kStatsValueNameBytesReceived,
241 info.bytes_rcvd);
242 report->AddValue(StatsReport::kStatsValueNameJitterReceived,
243 info.jitter_ms);
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000244 report->AddValue(StatsReport::kStatsValueNameExpandRate,
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000245 talk_base::ToString<float>(info.expand_rate));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000246 report->AddValue(StatsReport::kStatsValueNamePacketsReceived,
247 info.packets_rcvd);
248 report->AddValue(StatsReport::kStatsValueNamePacketsLost,
249 info.packets_lost);
250}
251
252void ExtractStats(const cricket::VoiceSenderInfo& info, StatsReport* report) {
253 report->AddValue(StatsReport::kStatsValueNameAudioInputLevel,
254 info.audio_level);
255 report->AddValue(StatsReport::kStatsValueNameBytesSent,
256 info.bytes_sent);
257 report->AddValue(StatsReport::kStatsValueNamePacketsSent,
258 info.packets_sent);
259 report->AddValue(StatsReport::kStatsValueNameJitterReceived,
260 info.jitter_ms);
261 report->AddValue(StatsReport::kStatsValueNameRtt, info.rtt_ms);
262 report->AddValue(StatsReport::kStatsValueNameEchoCancellationQualityMin,
263 talk_base::ToString<float>(info.aec_quality_min));
264 report->AddValue(StatsReport::kStatsValueNameEchoDelayMedian,
265 info.echo_delay_median_ms);
266 report->AddValue(StatsReport::kStatsValueNameEchoDelayStdDev,
267 info.echo_delay_std_ms);
268 report->AddValue(StatsReport::kStatsValueNameEchoReturnLoss,
269 info.echo_return_loss);
270 report->AddValue(StatsReport::kStatsValueNameEchoReturnLossEnhancement,
271 info.echo_return_loss_enhancement);
272 report->AddValue(StatsReport::kStatsValueNameCodecName, info.codec_name);
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000273 report->AddBoolean(StatsReport::kStatsValueNameTypingNoiseState,
274 info.typing_noise_detected);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000275}
276
277void ExtractStats(const cricket::VideoReceiverInfo& info, StatsReport* report) {
278 report->AddValue(StatsReport::kStatsValueNameBytesReceived,
279 info.bytes_rcvd);
280 report->AddValue(StatsReport::kStatsValueNamePacketsReceived,
281 info.packets_rcvd);
282 report->AddValue(StatsReport::kStatsValueNamePacketsLost,
283 info.packets_lost);
284
285 report->AddValue(StatsReport::kStatsValueNameFirsSent,
286 info.firs_sent);
287 report->AddValue(StatsReport::kStatsValueNameNacksSent,
288 info.nacks_sent);
289 report->AddValue(StatsReport::kStatsValueNameFrameWidthReceived,
290 info.frame_width);
291 report->AddValue(StatsReport::kStatsValueNameFrameHeightReceived,
292 info.frame_height);
293 report->AddValue(StatsReport::kStatsValueNameFrameRateReceived,
294 info.framerate_rcvd);
295 report->AddValue(StatsReport::kStatsValueNameFrameRateDecoded,
296 info.framerate_decoded);
297 report->AddValue(StatsReport::kStatsValueNameFrameRateOutput,
298 info.framerate_output);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000299
300 report->AddValue(StatsReport::kStatsValueNameDecodeMs,
301 info.decode_ms);
302 report->AddValue(StatsReport::kStatsValueNameMaxDecodeMs,
303 info.max_decode_ms);
304 report->AddValue(StatsReport::kStatsValueNameCurrentDelayMs,
305 info.current_delay_ms);
306 report->AddValue(StatsReport::kStatsValueNameTargetDelayMs,
307 info.target_delay_ms);
308 report->AddValue(StatsReport::kStatsValueNameJitterBufferMs,
309 info.jitter_buffer_ms);
310 report->AddValue(StatsReport::kStatsValueNameMinPlayoutDelayMs,
311 info.min_playout_delay_ms);
312 report->AddValue(StatsReport::kStatsValueNameRenderDelayMs,
313 info.render_delay_ms);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000314}
315
316void ExtractStats(const cricket::VideoSenderInfo& info, StatsReport* report) {
317 report->AddValue(StatsReport::kStatsValueNameBytesSent,
318 info.bytes_sent);
319 report->AddValue(StatsReport::kStatsValueNamePacketsSent,
320 info.packets_sent);
321
322 report->AddValue(StatsReport::kStatsValueNameFirsReceived,
323 info.firs_rcvd);
324 report->AddValue(StatsReport::kStatsValueNameNacksReceived,
325 info.nacks_rcvd);
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000326 report->AddValue(StatsReport::kStatsValueNameFrameWidthInput,
327 info.input_frame_width);
328 report->AddValue(StatsReport::kStatsValueNameFrameHeightInput,
329 info.input_frame_height);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000330 report->AddValue(StatsReport::kStatsValueNameFrameWidthSent,
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000331 info.send_frame_width);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000332 report->AddValue(StatsReport::kStatsValueNameFrameHeightSent,
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000333 info.send_frame_height);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000334 report->AddValue(StatsReport::kStatsValueNameFrameRateInput,
335 info.framerate_input);
336 report->AddValue(StatsReport::kStatsValueNameFrameRateSent,
337 info.framerate_sent);
338 report->AddValue(StatsReport::kStatsValueNameRtt, info.rtt_ms);
339 report->AddValue(StatsReport::kStatsValueNameCodecName, info.codec_name);
mallinath@webrtc.org62451dc2013-12-13 12:29:34 +0000340 report->AddBoolean(StatsReport::kStatsValueNameCpuLimitedResolution,
341 (info.adapt_reason & 0x1) > 0);
342 report->AddBoolean(StatsReport::kStatsValueNameBandwidthLimitedResolution,
343 (info.adapt_reason & 0x2) > 0);
344 report->AddBoolean(StatsReport::kStatsValueNameViewLimitedResolution,
345 (info.adapt_reason & 0x4) > 0);
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000346 report->AddValue(StatsReport::kStatsValueNameAvgEncodeMs, info.avg_encode_ms);
347 report->AddValue(StatsReport::kStatsValueNameCaptureJitterMs,
348 info.capture_jitter_ms);
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000349 report->AddValue(StatsReport::kStatsValueNameCaptureQueueDelayMsPerS,
350 info.capture_queue_delay_ms_per_s);
351 report->AddValue(StatsReport::kStatsValueNameEncodeUsagePercent,
352 info.encode_usage_percent);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000353}
354
355void ExtractStats(const cricket::BandwidthEstimationInfo& info,
356 double stats_gathering_started,
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000357 PeerConnectionInterface::StatsOutputLevel level,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000358 StatsReport* report) {
359 report->id = StatsReport::kStatsReportVideoBweId;
360 report->type = StatsReport::kStatsReportTypeBwe;
361
362 // Clear out stats from previous GatherStats calls if any.
363 if (report->timestamp != stats_gathering_started) {
364 report->values.clear();
365 report->timestamp = stats_gathering_started;
366 }
367
368 report->AddValue(StatsReport::kStatsValueNameAvailableSendBandwidth,
369 info.available_send_bandwidth);
370 report->AddValue(StatsReport::kStatsValueNameAvailableReceiveBandwidth,
371 info.available_recv_bandwidth);
372 report->AddValue(StatsReport::kStatsValueNameTargetEncBitrate,
373 info.target_enc_bitrate);
374 report->AddValue(StatsReport::kStatsValueNameActualEncBitrate,
375 info.actual_enc_bitrate);
376 report->AddValue(StatsReport::kStatsValueNameRetransmitBitrate,
377 info.retransmit_bitrate);
378 report->AddValue(StatsReport::kStatsValueNameTransmitBitrate,
379 info.transmit_bitrate);
380 report->AddValue(StatsReport::kStatsValueNameBucketDelay,
381 info.bucket_delay);
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000382 if (level >= PeerConnectionInterface::kStatsOutputLevelDebug) {
383 report->AddValue(
384 StatsReport::kStatsValueNameRecvPacketGroupPropagationDeltaSumDebug,
385 info.total_received_propagation_delta_ms);
386 if (info.recent_received_propagation_delta_ms.size() > 0) {
387 report->AddValue(
388 StatsReport::kStatsValueNameRecvPacketGroupPropagationDeltaDebug,
389 info.recent_received_propagation_delta_ms);
390 report->AddValue(
391 StatsReport::kStatsValueNameRecvPacketGroupArrivalTimeDebug,
392 info.recent_received_packet_group_arrival_time_ms);
393 }
394 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000395}
396
wu@webrtc.org97077a32013-10-25 21:18:33 +0000397void ExtractRemoteStats(const cricket::MediaSenderInfo& info,
398 StatsReport* report) {
399 report->timestamp = info.remote_stats[0].timestamp;
400 // TODO(hta): Extract some stats here.
401}
402
403void ExtractRemoteStats(const cricket::MediaReceiverInfo& info,
404 StatsReport* report) {
405 report->timestamp = info.remote_stats[0].timestamp;
406 // TODO(hta): Extract some stats here.
407}
408
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000409// Template to extract stats from a data vector.
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000410// In order to use the template, the functions that are called from it,
411// ExtractStats and ExtractRemoteStats, must be defined and overloaded
412// for each type.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000413template<typename T>
414void ExtractStatsFromList(const std::vector<T>& data,
415 const std::string& transport_id,
416 StatsCollector* collector) {
417 typename std::vector<T>::const_iterator it = data.begin();
418 for (; it != data.end(); ++it) {
419 std::string id;
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000420 uint32 ssrc = it->ssrc();
wu@webrtc.org97077a32013-10-25 21:18:33 +0000421 // Each object can result in 2 objects, a local and a remote object.
422 // TODO(hta): Handle the case of multiple SSRCs per object.
423 StatsReport* report = collector->PrepareLocalReport(ssrc, transport_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000424 if (!report) {
425 continue;
426 }
427 ExtractStats(*it, report);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000428 if (it->remote_stats.size() > 0) {
429 report = collector->PrepareRemoteReport(ssrc, transport_id);
430 if (!report) {
431 continue;
432 }
433 ExtractRemoteStats(*it, report);
434 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000435 }
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000436}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000437
438} // namespace
439
440StatsCollector::StatsCollector()
441 : session_(NULL), stats_gathering_started_(0) {
442}
443
444// Adds a MediaStream with tracks that can be used as a |selector| in a call
445// to GetStats.
446void StatsCollector::AddStream(MediaStreamInterface* stream) {
447 ASSERT(stream != NULL);
448
449 CreateTrackReports<AudioTrackVector>(stream->GetAudioTracks(),
450 &reports_);
451 CreateTrackReports<VideoTrackVector>(stream->GetVideoTracks(),
452 &reports_);
453}
454
455bool StatsCollector::GetStats(MediaStreamTrackInterface* track,
456 StatsReports* reports) {
457 ASSERT(reports != NULL);
458 reports->clear();
459
460 StatsMap::iterator it;
461 if (!track) {
462 for (it = reports_.begin(); it != reports_.end(); ++it) {
463 reports->push_back(it->second);
464 }
465 return true;
466 }
467
468 it = reports_.find(StatsId(StatsReport::kStatsReportTypeSession,
469 session_->id()));
470 if (it != reports_.end()) {
471 reports->push_back(it->second);
472 }
473
474 it = reports_.find(StatsId(StatsReport::kStatsReportTypeTrack, track->id()));
475
476 if (it == reports_.end()) {
477 LOG(LS_WARNING) << "No StatsReport is available for "<< track->id();
478 return false;
479 }
480
481 reports->push_back(it->second);
482
483 std::string track_id;
484 for (it = reports_.begin(); it != reports_.end(); ++it) {
485 if (it->second.type != StatsReport::kStatsReportTypeSsrc) {
486 continue;
487 }
488 if (ExtractValueFromReport(it->second,
489 StatsReport::kStatsValueNameTrackId,
490 &track_id)) {
491 if (track_id == track->id()) {
492 reports->push_back(it->second);
493 }
494 }
495 }
496
497 return true;
498}
499
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000500void
501StatsCollector::UpdateStats(PeerConnectionInterface::StatsOutputLevel level) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000502 double time_now = GetTimeNow();
503 // Calls to UpdateStats() that occur less than kMinGatherStatsPeriod number of
504 // ms apart will be ignored.
505 const double kMinGatherStatsPeriod = 50;
506 if (stats_gathering_started_ + kMinGatherStatsPeriod > time_now) {
507 return;
508 }
509 stats_gathering_started_ = time_now;
510
511 if (session_) {
512 ExtractSessionInfo();
513 ExtractVoiceInfo();
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000514 ExtractVideoInfo(level);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000515 }
516}
517
wu@webrtc.org97077a32013-10-25 21:18:33 +0000518StatsReport* StatsCollector::PrepareLocalReport(
519 uint32 ssrc,
520 const std::string& transport_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000521 std::string ssrc_id = talk_base::ToString<uint32>(ssrc);
522 StatsMap::iterator it = reports_.find(StatsId(
523 StatsReport::kStatsReportTypeSsrc, ssrc_id));
524
525 std::string track_id;
526 if (it == reports_.end()) {
527 if (!session()->GetTrackIdBySsrc(ssrc, &track_id)) {
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000528 LOG(LS_WARNING) << "The SSRC " << ssrc
529 << " is not associated with a track";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000530 return NULL;
531 }
532 } else {
533 // Keeps the old track id since we want to report the stats for inactive
534 // tracks.
535 ExtractValueFromReport(it->second,
536 StatsReport::kStatsValueNameTrackId,
537 &track_id);
538 }
539
wu@webrtc.org97077a32013-10-25 21:18:33 +0000540 StatsReport* report = GetOrCreateReport(StatsReport::kStatsReportTypeSsrc,
541 ssrc_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000542
543 // Clear out stats from previous GatherStats calls if any.
544 if (report->timestamp != stats_gathering_started_) {
545 report->values.clear();
546 report->timestamp = stats_gathering_started_;
547 }
548
549 report->AddValue(StatsReport::kStatsValueNameSsrc, ssrc_id);
550 report->AddValue(StatsReport::kStatsValueNameTrackId, track_id);
551 // Add the mapping of SSRC to transport.
552 report->AddValue(StatsReport::kStatsValueNameTransportId,
553 transport_id);
554 return report;
555}
556
wu@webrtc.org97077a32013-10-25 21:18:33 +0000557StatsReport* StatsCollector::PrepareRemoteReport(
558 uint32 ssrc,
559 const std::string& transport_id) {
560 std::string ssrc_id = talk_base::ToString<uint32>(ssrc);
561 StatsMap::iterator it = reports_.find(StatsId(
562 StatsReport::kStatsReportTypeRemoteSsrc, ssrc_id));
563
564 std::string track_id;
565 if (it == reports_.end()) {
566 if (!session()->GetTrackIdBySsrc(ssrc, &track_id)) {
567 LOG(LS_WARNING) << "The SSRC " << ssrc
568 << " is not associated with a track";
569 return NULL;
570 }
571 } else {
572 // Keeps the old track id since we want to report the stats for inactive
573 // tracks.
574 ExtractValueFromReport(it->second,
575 StatsReport::kStatsValueNameTrackId,
576 &track_id);
577 }
578
579 StatsReport* report = GetOrCreateReport(
580 StatsReport::kStatsReportTypeRemoteSsrc, ssrc_id);
581
582 // Clear out stats from previous GatherStats calls if any.
583 // The timestamp will be added later. Zero it for debugging.
584 report->values.clear();
585 report->timestamp = 0;
586
587 report->AddValue(StatsReport::kStatsValueNameSsrc, ssrc_id);
588 report->AddValue(StatsReport::kStatsValueNameTrackId, track_id);
589 // Add the mapping of SSRC to transport.
590 report->AddValue(StatsReport::kStatsValueNameTransportId,
591 transport_id);
592 return report;
593}
594
wu@webrtc.org4551b792013-10-09 15:37:36 +0000595std::string StatsCollector::AddOneCertificateReport(
596 const talk_base::SSLCertificate* cert, const std::string& issuer_id) {
597 // TODO(bemasc): Move this computation to a helper class that caches these
598 // values to reduce CPU use in GetStats. This will require adding a fast
599 // SSLCertificate::Equals() method to detect certificate changes.
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000600
601 std::string digest_algorithm;
602 if (!cert->GetSignatureDigestAlgorithm(&digest_algorithm))
603 return std::string();
604
wu@webrtc.org4551b792013-10-09 15:37:36 +0000605 talk_base::scoped_ptr<talk_base::SSLFingerprint> ssl_fingerprint(
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000606 talk_base::SSLFingerprint::Create(digest_algorithm, cert));
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000607
608 // SSLFingerprint::Create can fail if the algorithm returned by
609 // SSLCertificate::GetSignatureDigestAlgorithm is not supported by the
610 // implementation of SSLCertificate::ComputeDigest. This currently happens
611 // with MD5- and SHA-224-signed certificates when linked to libNSS.
612 if (!ssl_fingerprint)
613 return std::string();
614
wu@webrtc.org4551b792013-10-09 15:37:36 +0000615 std::string fingerprint = ssl_fingerprint->GetRfc4572Fingerprint();
616
617 talk_base::Buffer der_buffer;
618 cert->ToDER(&der_buffer);
619 std::string der_base64;
620 talk_base::Base64::EncodeFromArray(
621 der_buffer.data(), der_buffer.length(), &der_base64);
622
623 StatsReport report;
624 report.type = StatsReport::kStatsReportTypeCertificate;
625 report.id = StatsId(report.type, fingerprint);
626 report.timestamp = stats_gathering_started_;
627 report.AddValue(StatsReport::kStatsValueNameFingerprint, fingerprint);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000628 report.AddValue(StatsReport::kStatsValueNameFingerprintAlgorithm,
629 digest_algorithm);
wu@webrtc.org4551b792013-10-09 15:37:36 +0000630 report.AddValue(StatsReport::kStatsValueNameDer, der_base64);
631 if (!issuer_id.empty())
632 report.AddValue(StatsReport::kStatsValueNameIssuerId, issuer_id);
633 reports_[report.id] = report;
634 return report.id;
635}
636
637std::string StatsCollector::AddCertificateReports(
638 const talk_base::SSLCertificate* cert) {
639 // Produces a chain of StatsReports representing this certificate and the rest
640 // of its chain, and adds those reports to |reports_|. The return value is
641 // the id of the leaf report. The provided cert must be non-null, so at least
642 // one report will always be provided and the returned string will never be
643 // empty.
644 ASSERT(cert != NULL);
645
646 std::string issuer_id;
647 talk_base::scoped_ptr<talk_base::SSLCertChain> chain;
648 if (cert->GetChain(chain.accept())) {
649 // This loop runs in reverse, i.e. from root to leaf, so that each
650 // certificate's issuer's report ID is known before the child certificate's
651 // report is generated. The root certificate does not have an issuer ID
652 // value.
653 for (ptrdiff_t i = chain->GetSize() - 1; i >= 0; --i) {
654 const talk_base::SSLCertificate& cert_i = chain->Get(i);
655 issuer_id = AddOneCertificateReport(&cert_i, issuer_id);
656 }
657 }
658 // Add the leaf certificate.
659 return AddOneCertificateReport(cert, issuer_id);
660}
661
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000662void StatsCollector::ExtractSessionInfo() {
663 // Extract information from the base session.
664 StatsReport report;
665 report.id = StatsId(StatsReport::kStatsReportTypeSession, session_->id());
666 report.type = StatsReport::kStatsReportTypeSession;
667 report.timestamp = stats_gathering_started_;
668 report.values.clear();
669 report.AddBoolean(StatsReport::kStatsValueNameInitiator,
670 session_->initiator());
671
672 reports_[report.id] = report;
673
674 cricket::SessionStats stats;
675 if (session_->GetStats(&stats)) {
676 // Store the proxy map away for use in SSRC reporting.
677 proxy_to_transport_ = stats.proxy_to_transport;
678
679 for (cricket::TransportStatsMap::iterator transport_iter
680 = stats.transport_stats.begin();
681 transport_iter != stats.transport_stats.end(); ++transport_iter) {
wu@webrtc.org4551b792013-10-09 15:37:36 +0000682 // Attempt to get a copy of the certificates from the transport and
683 // expose them in stats reports. All channels in a transport share the
684 // same local and remote certificates.
685 std::string local_cert_report_id, remote_cert_report_id;
686 cricket::Transport* transport =
687 session_->GetTransport(transport_iter->second.content_name);
688 if (transport) {
689 talk_base::scoped_ptr<talk_base::SSLIdentity> identity;
690 if (transport->GetIdentity(identity.accept()))
691 local_cert_report_id = AddCertificateReports(
692 &(identity->certificate()));
693
694 talk_base::scoped_ptr<talk_base::SSLCertificate> cert;
695 if (transport->GetRemoteCertificate(cert.accept()))
696 remote_cert_report_id = AddCertificateReports(cert.get());
697 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000698 for (cricket::TransportChannelStatsList::iterator channel_iter
699 = transport_iter->second.channel_stats.begin();
700 channel_iter != transport_iter->second.channel_stats.end();
701 ++channel_iter) {
702 StatsReport channel_report;
703 std::ostringstream ostc;
704 ostc << "Channel-" << transport_iter->second.content_name
705 << "-" << channel_iter->component;
706 channel_report.id = ostc.str();
707 channel_report.type = StatsReport::kStatsReportTypeComponent;
708 channel_report.timestamp = stats_gathering_started_;
709 channel_report.AddValue(StatsReport::kStatsValueNameComponent,
710 channel_iter->component);
wu@webrtc.org4551b792013-10-09 15:37:36 +0000711 if (!local_cert_report_id.empty())
712 channel_report.AddValue(
713 StatsReport::kStatsValueNameLocalCertificateId,
714 local_cert_report_id);
715 if (!remote_cert_report_id.empty())
716 channel_report.AddValue(
717 StatsReport::kStatsValueNameRemoteCertificateId,
718 remote_cert_report_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000719 reports_[channel_report.id] = channel_report;
720 for (size_t i = 0;
721 i < channel_iter->connection_infos.size();
722 ++i) {
723 StatsReport report;
724 const cricket::ConnectionInfo& info
725 = channel_iter->connection_infos[i];
726 std::ostringstream ost;
727 ost << "Conn-" << transport_iter->first << "-"
728 << channel_iter->component << "-" << i;
729 report.id = ost.str();
730 report.type = StatsReport::kStatsReportTypeCandidatePair;
731 report.timestamp = stats_gathering_started_;
732 // Link from connection to its containing channel.
733 report.AddValue(StatsReport::kStatsValueNameChannelId,
734 channel_report.id);
735 report.AddValue(StatsReport::kStatsValueNameBytesSent,
736 info.sent_total_bytes);
737 report.AddValue(StatsReport::kStatsValueNameBytesReceived,
738 info.recv_total_bytes);
739 report.AddBoolean(StatsReport::kStatsValueNameWritable,
740 info.writable);
741 report.AddBoolean(StatsReport::kStatsValueNameReadable,
742 info.readable);
743 report.AddBoolean(StatsReport::kStatsValueNameActiveConnection,
744 info.best_connection);
745 report.AddValue(StatsReport::kStatsValueNameLocalAddress,
746 info.local_candidate.address().ToString());
747 report.AddValue(StatsReport::kStatsValueNameRemoteAddress,
748 info.remote_candidate.address().ToString());
wu@webrtc.org97077a32013-10-25 21:18:33 +0000749 report.AddValue(StatsReport::kStatsValueNameRtt, info.rtt);
750 report.AddValue(StatsReport::kStatsValueNameTransportType,
751 info.local_candidate.protocol());
wu@webrtc.org364f2042013-11-20 21:49:41 +0000752 report.AddValue(StatsReport::kStatsValueNameLocalCandidateType,
753 info.local_candidate.type());
754 report.AddValue(StatsReport::kStatsValueNameRemoteCandidateType,
755 info.remote_candidate.type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000756 reports_[report.id] = report;
757 }
758 }
759 }
760 }
761}
762
763void StatsCollector::ExtractVoiceInfo() {
764 if (!session_->voice_channel()) {
765 return;
766 }
767 cricket::VoiceMediaInfo voice_info;
768 if (!session_->voice_channel()->GetStats(&voice_info)) {
769 LOG(LS_ERROR) << "Failed to get voice channel stats.";
770 return;
771 }
772 std::string transport_id;
773 if (!GetTransportIdFromProxy(session_->voice_channel()->content_name(),
774 &transport_id)) {
775 LOG(LS_ERROR) << "Failed to get transport name for proxy "
776 << session_->voice_channel()->content_name();
777 return;
778 }
779 ExtractStatsFromList(voice_info.receivers, transport_id, this);
780 ExtractStatsFromList(voice_info.senders, transport_id, this);
781}
782
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000783void StatsCollector::ExtractVideoInfo(
784 PeerConnectionInterface::StatsOutputLevel level) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000785 if (!session_->video_channel()) {
786 return;
787 }
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000788 cricket::StatsOptions options;
789 options.include_received_propagation_stats =
790 (level >= PeerConnectionInterface::kStatsOutputLevelDebug) ?
791 true : false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000792 cricket::VideoMediaInfo video_info;
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000793 if (!session_->video_channel()->GetStats(options, &video_info)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000794 LOG(LS_ERROR) << "Failed to get video channel stats.";
795 return;
796 }
797 std::string transport_id;
798 if (!GetTransportIdFromProxy(session_->video_channel()->content_name(),
799 &transport_id)) {
800 LOG(LS_ERROR) << "Failed to get transport name for proxy "
801 << session_->video_channel()->content_name();
802 return;
803 }
804 ExtractStatsFromList(video_info.receivers, transport_id, this);
805 ExtractStatsFromList(video_info.senders, transport_id, this);
806 if (video_info.bw_estimations.size() != 1) {
807 LOG(LS_ERROR) << "BWEs count: " << video_info.bw_estimations.size();
808 } else {
809 StatsReport* report = &reports_[StatsReport::kStatsReportVideoBweId];
810 ExtractStats(
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000811 video_info.bw_estimations[0], stats_gathering_started_, level, report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000812 }
813}
814
815double StatsCollector::GetTimeNow() {
816 return timing_.WallTimeNow() * talk_base::kNumMillisecsPerSec;
817}
818
819bool StatsCollector::GetTransportIdFromProxy(const std::string& proxy,
820 std::string* transport) {
821 // TODO(hta): Remove handling of empty proxy name once tests do not use it.
822 if (proxy.empty()) {
823 transport->clear();
824 return true;
825 }
826 if (proxy_to_transport_.find(proxy) == proxy_to_transport_.end()) {
827 LOG(LS_ERROR) << "No transport ID mapping for " << proxy;
828 return false;
829 }
830 std::ostringstream ost;
831 // Component 1 is always used for RTP.
832 ost << "Channel-" << proxy_to_transport_[proxy] << "-1";
833 *transport = ost.str();
834 return true;
835}
836
wu@webrtc.org97077a32013-10-25 21:18:33 +0000837StatsReport* StatsCollector::GetOrCreateReport(const std::string& type,
838 const std::string& id) {
839 std::string statsid = StatsId(type, id);
840 StatsReport* report = NULL;
841 std::map<std::string, StatsReport>::iterator it = reports_.find(statsid);
842 if (it == reports_.end()) {
843 report = &reports_[statsid]; // Create new element.
844 report->id = statsid;
845 report->type = type;
846 } else {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000847 report = &(it->second);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000848 }
849 return report;
850}
851
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000852} // namespace webrtc