blob: db7cac433d6e8494bf278c894fd67c7795c4b07b [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";
50const char StatsReport::kStatsValueNameBucketDelay[] = "googBucketDelay";
51const char StatsReport::kStatsValueNameBytesReceived[] = "bytesReceived";
52const char StatsReport::kStatsValueNameBytesSent[] = "bytesSent";
53const char StatsReport::kStatsValueNameChannelId[] = "googChannelId";
54const char StatsReport::kStatsValueNameCodecName[] = "googCodecName";
55const char StatsReport::kStatsValueNameComponent[] = "googComponent";
56const char StatsReport::kStatsValueNameContentName[] = "googContentName";
wu@webrtc.org4551b792013-10-09 15:37:36 +000057const char StatsReport::kStatsValueNameDer[] = "googDerBase64";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000058// Echo metrics from the audio processing module.
59const char StatsReport::kStatsValueNameEchoCancellationQualityMin[] =
60 "googEchoCancellationQualityMin";
61const char StatsReport::kStatsValueNameEchoDelayMedian[] =
62 "googEchoCancellationEchoDelayMedian";
63const char StatsReport::kStatsValueNameEchoDelayStdDev[] =
64 "googEchoCancellationEchoDelayStdDev";
65const char StatsReport::kStatsValueNameEchoReturnLoss[] =
66 "googEchoCancellationReturnLoss";
67const char StatsReport::kStatsValueNameEchoReturnLossEnhancement[] =
68 "googEchoCancellationReturnLossEnhancement";
69
wu@webrtc.org4551b792013-10-09 15:37:36 +000070const char StatsReport::kStatsValueNameFingerprint[] = "googFingerprint";
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +000071const char StatsReport::kStatsValueNameFingerprintAlgorithm[] =
72 "googFingerprintAlgorithm";
henrike@webrtc.org28e20752013-07-10 00:45:36 +000073const char StatsReport::kStatsValueNameFirsReceived[] = "googFirsReceived";
74const char StatsReport::kStatsValueNameFirsSent[] = "googFirsSent";
75const char StatsReport::kStatsValueNameFrameHeightReceived[] =
76 "googFrameHeightReceived";
77const char StatsReport::kStatsValueNameFrameHeightSent[] =
78 "googFrameHeightSent";
79const char StatsReport::kStatsValueNameFrameRateReceived[] =
80 "googFrameRateReceived";
81const char StatsReport::kStatsValueNameFrameRateDecoded[] =
82 "googFrameRateDecoded";
83const char StatsReport::kStatsValueNameFrameRateOutput[] =
84 "googFrameRateOutput";
wu@webrtc.org97077a32013-10-25 21:18:33 +000085const char StatsReport::kStatsValueNameDecodeMs[] = "googDecodeMs";
86const char StatsReport::kStatsValueNameMaxDecodeMs[] = "googMaxDecodeMs";
87const char StatsReport::kStatsValueNameCurrentDelayMs[] = "googCurrentDelayMs";
88const char StatsReport::kStatsValueNameTargetDelayMs[] = "googTargetDelayMs";
89const char StatsReport::kStatsValueNameJitterBufferMs[] = "googJitterBufferMs";
90const char StatsReport::kStatsValueNameMinPlayoutDelayMs[] =
91 "googMinPlayoutDelayMs";
92const char StatsReport::kStatsValueNameRenderDelayMs[] = "googRenderDelayMs";
93
henrike@webrtc.org28e20752013-07-10 00:45:36 +000094const char StatsReport::kStatsValueNameFrameRateInput[] = "googFrameRateInput";
95const char StatsReport::kStatsValueNameFrameRateSent[] = "googFrameRateSent";
96const char StatsReport::kStatsValueNameFrameWidthReceived[] =
97 "googFrameWidthReceived";
98const char StatsReport::kStatsValueNameFrameWidthSent[] = "googFrameWidthSent";
99const char StatsReport::kStatsValueNameInitiator[] = "googInitiator";
wu@webrtc.org4551b792013-10-09 15:37:36 +0000100const char StatsReport::kStatsValueNameIssuerId[] = "googIssuerId";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000101const char StatsReport::kStatsValueNameJitterReceived[] = "googJitterReceived";
102const char StatsReport::kStatsValueNameLocalAddress[] = "googLocalAddress";
wu@webrtc.org4551b792013-10-09 15:37:36 +0000103const char StatsReport::kStatsValueNameLocalCertificateId[] =
104 "googLocalCertificateId";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000105const char StatsReport::kStatsValueNameNacksReceived[] = "googNacksReceived";
106const char StatsReport::kStatsValueNameNacksSent[] = "googNacksSent";
107const char StatsReport::kStatsValueNamePacketsReceived[] = "packetsReceived";
108const char StatsReport::kStatsValueNamePacketsSent[] = "packetsSent";
109const char StatsReport::kStatsValueNamePacketsLost[] = "packetsLost";
110const char StatsReport::kStatsValueNameReadable[] = "googReadable";
111const char StatsReport::kStatsValueNameRemoteAddress[] = "googRemoteAddress";
wu@webrtc.org4551b792013-10-09 15:37:36 +0000112const char StatsReport::kStatsValueNameRemoteCertificateId[] =
113 "googRemoteCertificateId";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000114const char StatsReport::kStatsValueNameRetransmitBitrate[] =
115 "googRetransmitBitrate";
116const char StatsReport::kStatsValueNameRtt[] = "googRtt";
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000117const char StatsReport::kStatsValueNameSsrc[] = "ssrc";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000118const char StatsReport::kStatsValueNameTargetEncBitrate[] =
119 "googTargetEncBitrate";
120const char StatsReport::kStatsValueNameTransmitBitrate[] =
121 "googTransmitBitrate";
122const char StatsReport::kStatsValueNameTransportId[] = "transportId";
123const char StatsReport::kStatsValueNameTransportType[] = "googTransportType";
124const char StatsReport::kStatsValueNameTrackId[] = "googTrackId";
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000125const char StatsReport::kStatsValueNameTypingNoiseState[] =
126 "googTypingNoiseState";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000127const char StatsReport::kStatsValueNameWritable[] = "googWritable";
128
129const char StatsReport::kStatsReportTypeSession[] = "googLibjingleSession";
130const char StatsReport::kStatsReportTypeBwe[] = "VideoBwe";
wu@webrtc.org97077a32013-10-25 21:18:33 +0000131const char StatsReport::kStatsReportTypeRemoteSsrc[] = "remoteSsrc";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000132const char StatsReport::kStatsReportTypeSsrc[] = "ssrc";
133const char StatsReport::kStatsReportTypeTrack[] = "googTrack";
134const char StatsReport::kStatsReportTypeIceCandidate[] = "iceCandidate";
135const char StatsReport::kStatsReportTypeTransport[] = "googTransport";
136const char StatsReport::kStatsReportTypeComponent[] = "googComponent";
137const char StatsReport::kStatsReportTypeCandidatePair[] = "googCandidatePair";
wu@webrtc.org4551b792013-10-09 15:37:36 +0000138const char StatsReport::kStatsReportTypeCertificate[] = "googCertificate";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000139
140const char StatsReport::kStatsReportVideoBweId[] = "bweforvideo";
141
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000142
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000143// Implementations of functions in statstypes.h
144void StatsReport::AddValue(const std::string& name, const std::string& value) {
145 Value temp;
146 temp.name = name;
147 temp.value = value;
148 values.push_back(temp);
149}
150
151void StatsReport::AddValue(const std::string& name, int64 value) {
152 AddValue(name, talk_base::ToString<int64>(value));
153}
154
155void StatsReport::AddBoolean(const std::string& name, bool value) {
156 AddValue(name, value ? "true" : "false");
157}
158
159namespace {
160typedef std::map<std::string, StatsReport> StatsMap;
161
162std::string StatsId(const std::string& type, const std::string& id) {
163 return type + "_" + id;
164}
165
166bool ExtractValueFromReport(
167 const StatsReport& report,
168 const std::string& name,
169 std::string* value) {
170 StatsReport::Values::const_iterator it = report.values.begin();
171 for (; it != report.values.end(); ++it) {
172 if (it->name == name) {
173 *value = it->value;
174 return true;
175 }
176 }
177 return false;
178}
179
180template <class TrackVector>
181void CreateTrackReports(const TrackVector& tracks, StatsMap* reports) {
182 for (size_t j = 0; j < tracks.size(); ++j) {
183 webrtc::MediaStreamTrackInterface* track = tracks[j];
184 // Adds an empty track report.
185 StatsReport report;
186 report.type = StatsReport::kStatsReportTypeTrack;
187 report.id = StatsId(StatsReport::kStatsReportTypeTrack, track->id());
188 report.AddValue(StatsReport::kStatsValueNameTrackId,
189 track->id());
190 (*reports)[report.id] = report;
191 }
192}
193
194void ExtractStats(const cricket::VoiceReceiverInfo& info, StatsReport* report) {
195 report->AddValue(StatsReport::kStatsValueNameAudioOutputLevel,
196 info.audio_level);
197 report->AddValue(StatsReport::kStatsValueNameBytesReceived,
198 info.bytes_rcvd);
199 report->AddValue(StatsReport::kStatsValueNameJitterReceived,
200 info.jitter_ms);
201 report->AddValue(StatsReport::kStatsValueNamePacketsReceived,
202 info.packets_rcvd);
203 report->AddValue(StatsReport::kStatsValueNamePacketsLost,
204 info.packets_lost);
205}
206
207void ExtractStats(const cricket::VoiceSenderInfo& info, StatsReport* report) {
208 report->AddValue(StatsReport::kStatsValueNameAudioInputLevel,
209 info.audio_level);
210 report->AddValue(StatsReport::kStatsValueNameBytesSent,
211 info.bytes_sent);
212 report->AddValue(StatsReport::kStatsValueNamePacketsSent,
213 info.packets_sent);
214 report->AddValue(StatsReport::kStatsValueNameJitterReceived,
215 info.jitter_ms);
216 report->AddValue(StatsReport::kStatsValueNameRtt, info.rtt_ms);
217 report->AddValue(StatsReport::kStatsValueNameEchoCancellationQualityMin,
218 talk_base::ToString<float>(info.aec_quality_min));
219 report->AddValue(StatsReport::kStatsValueNameEchoDelayMedian,
220 info.echo_delay_median_ms);
221 report->AddValue(StatsReport::kStatsValueNameEchoDelayStdDev,
222 info.echo_delay_std_ms);
223 report->AddValue(StatsReport::kStatsValueNameEchoReturnLoss,
224 info.echo_return_loss);
225 report->AddValue(StatsReport::kStatsValueNameEchoReturnLossEnhancement,
226 info.echo_return_loss_enhancement);
227 report->AddValue(StatsReport::kStatsValueNameCodecName, info.codec_name);
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000228 report->AddBoolean(StatsReport::kStatsValueNameTypingNoiseState,
229 info.typing_noise_detected);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000230}
231
232void ExtractStats(const cricket::VideoReceiverInfo& info, StatsReport* report) {
233 report->AddValue(StatsReport::kStatsValueNameBytesReceived,
234 info.bytes_rcvd);
235 report->AddValue(StatsReport::kStatsValueNamePacketsReceived,
236 info.packets_rcvd);
237 report->AddValue(StatsReport::kStatsValueNamePacketsLost,
238 info.packets_lost);
239
240 report->AddValue(StatsReport::kStatsValueNameFirsSent,
241 info.firs_sent);
242 report->AddValue(StatsReport::kStatsValueNameNacksSent,
243 info.nacks_sent);
244 report->AddValue(StatsReport::kStatsValueNameFrameWidthReceived,
245 info.frame_width);
246 report->AddValue(StatsReport::kStatsValueNameFrameHeightReceived,
247 info.frame_height);
248 report->AddValue(StatsReport::kStatsValueNameFrameRateReceived,
249 info.framerate_rcvd);
250 report->AddValue(StatsReport::kStatsValueNameFrameRateDecoded,
251 info.framerate_decoded);
252 report->AddValue(StatsReport::kStatsValueNameFrameRateOutput,
253 info.framerate_output);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000254
255 report->AddValue(StatsReport::kStatsValueNameDecodeMs,
256 info.decode_ms);
257 report->AddValue(StatsReport::kStatsValueNameMaxDecodeMs,
258 info.max_decode_ms);
259 report->AddValue(StatsReport::kStatsValueNameCurrentDelayMs,
260 info.current_delay_ms);
261 report->AddValue(StatsReport::kStatsValueNameTargetDelayMs,
262 info.target_delay_ms);
263 report->AddValue(StatsReport::kStatsValueNameJitterBufferMs,
264 info.jitter_buffer_ms);
265 report->AddValue(StatsReport::kStatsValueNameMinPlayoutDelayMs,
266 info.min_playout_delay_ms);
267 report->AddValue(StatsReport::kStatsValueNameRenderDelayMs,
268 info.render_delay_ms);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000269}
270
271void ExtractStats(const cricket::VideoSenderInfo& info, StatsReport* report) {
272 report->AddValue(StatsReport::kStatsValueNameBytesSent,
273 info.bytes_sent);
274 report->AddValue(StatsReport::kStatsValueNamePacketsSent,
275 info.packets_sent);
276
277 report->AddValue(StatsReport::kStatsValueNameFirsReceived,
278 info.firs_rcvd);
279 report->AddValue(StatsReport::kStatsValueNameNacksReceived,
280 info.nacks_rcvd);
281 report->AddValue(StatsReport::kStatsValueNameFrameWidthSent,
282 info.frame_width);
283 report->AddValue(StatsReport::kStatsValueNameFrameHeightSent,
284 info.frame_height);
285 report->AddValue(StatsReport::kStatsValueNameFrameRateInput,
286 info.framerate_input);
287 report->AddValue(StatsReport::kStatsValueNameFrameRateSent,
288 info.framerate_sent);
289 report->AddValue(StatsReport::kStatsValueNameRtt, info.rtt_ms);
290 report->AddValue(StatsReport::kStatsValueNameCodecName, info.codec_name);
291}
292
293void ExtractStats(const cricket::BandwidthEstimationInfo& info,
294 double stats_gathering_started,
295 StatsReport* report) {
296 report->id = StatsReport::kStatsReportVideoBweId;
297 report->type = StatsReport::kStatsReportTypeBwe;
298
299 // Clear out stats from previous GatherStats calls if any.
300 if (report->timestamp != stats_gathering_started) {
301 report->values.clear();
302 report->timestamp = stats_gathering_started;
303 }
304
305 report->AddValue(StatsReport::kStatsValueNameAvailableSendBandwidth,
306 info.available_send_bandwidth);
307 report->AddValue(StatsReport::kStatsValueNameAvailableReceiveBandwidth,
308 info.available_recv_bandwidth);
309 report->AddValue(StatsReport::kStatsValueNameTargetEncBitrate,
310 info.target_enc_bitrate);
311 report->AddValue(StatsReport::kStatsValueNameActualEncBitrate,
312 info.actual_enc_bitrate);
313 report->AddValue(StatsReport::kStatsValueNameRetransmitBitrate,
314 info.retransmit_bitrate);
315 report->AddValue(StatsReport::kStatsValueNameTransmitBitrate,
316 info.transmit_bitrate);
317 report->AddValue(StatsReport::kStatsValueNameBucketDelay,
318 info.bucket_delay);
319}
320
wu@webrtc.org97077a32013-10-25 21:18:33 +0000321void ExtractRemoteStats(const cricket::MediaSenderInfo& info,
322 StatsReport* report) {
323 report->timestamp = info.remote_stats[0].timestamp;
324 // TODO(hta): Extract some stats here.
325}
326
327void ExtractRemoteStats(const cricket::MediaReceiverInfo& info,
328 StatsReport* report) {
329 report->timestamp = info.remote_stats[0].timestamp;
330 // TODO(hta): Extract some stats here.
331}
332
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000333uint32 ExtractSsrc(const cricket::VoiceReceiverInfo& info) {
334 return info.ssrc;
335}
336
337uint32 ExtractSsrc(const cricket::VoiceSenderInfo& info) {
338 return info.ssrc;
339}
340
341uint32 ExtractSsrc(const cricket::VideoReceiverInfo& info) {
342 return info.ssrcs[0];
343}
344
345uint32 ExtractSsrc(const cricket::VideoSenderInfo& info) {
346 return info.ssrcs[0];
347}
348
349// Template to extract stats from a data vector.
350// ExtractSsrc and ExtractStats must be defined and overloaded for each type.
351template<typename T>
352void ExtractStatsFromList(const std::vector<T>& data,
353 const std::string& transport_id,
354 StatsCollector* collector) {
355 typename std::vector<T>::const_iterator it = data.begin();
356 for (; it != data.end(); ++it) {
357 std::string id;
358 uint32 ssrc = ExtractSsrc(*it);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000359 // Each object can result in 2 objects, a local and a remote object.
360 // TODO(hta): Handle the case of multiple SSRCs per object.
361 StatsReport* report = collector->PrepareLocalReport(ssrc, transport_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000362 if (!report) {
363 continue;
364 }
365 ExtractStats(*it, report);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000366 if (it->remote_stats.size() > 0) {
367 report = collector->PrepareRemoteReport(ssrc, transport_id);
368 if (!report) {
369 continue;
370 }
371 ExtractRemoteStats(*it, report);
372 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000373 }
374};
375
376} // namespace
377
378StatsCollector::StatsCollector()
379 : session_(NULL), stats_gathering_started_(0) {
380}
381
382// Adds a MediaStream with tracks that can be used as a |selector| in a call
383// to GetStats.
384void StatsCollector::AddStream(MediaStreamInterface* stream) {
385 ASSERT(stream != NULL);
386
387 CreateTrackReports<AudioTrackVector>(stream->GetAudioTracks(),
388 &reports_);
389 CreateTrackReports<VideoTrackVector>(stream->GetVideoTracks(),
390 &reports_);
391}
392
393bool StatsCollector::GetStats(MediaStreamTrackInterface* track,
394 StatsReports* reports) {
395 ASSERT(reports != NULL);
396 reports->clear();
397
398 StatsMap::iterator it;
399 if (!track) {
400 for (it = reports_.begin(); it != reports_.end(); ++it) {
401 reports->push_back(it->second);
402 }
403 return true;
404 }
405
406 it = reports_.find(StatsId(StatsReport::kStatsReportTypeSession,
407 session_->id()));
408 if (it != reports_.end()) {
409 reports->push_back(it->second);
410 }
411
412 it = reports_.find(StatsId(StatsReport::kStatsReportTypeTrack, track->id()));
413
414 if (it == reports_.end()) {
415 LOG(LS_WARNING) << "No StatsReport is available for "<< track->id();
416 return false;
417 }
418
419 reports->push_back(it->second);
420
421 std::string track_id;
422 for (it = reports_.begin(); it != reports_.end(); ++it) {
423 if (it->second.type != StatsReport::kStatsReportTypeSsrc) {
424 continue;
425 }
426 if (ExtractValueFromReport(it->second,
427 StatsReport::kStatsValueNameTrackId,
428 &track_id)) {
429 if (track_id == track->id()) {
430 reports->push_back(it->second);
431 }
432 }
433 }
434
435 return true;
436}
437
438void StatsCollector::UpdateStats() {
439 double time_now = GetTimeNow();
440 // Calls to UpdateStats() that occur less than kMinGatherStatsPeriod number of
441 // ms apart will be ignored.
442 const double kMinGatherStatsPeriod = 50;
443 if (stats_gathering_started_ + kMinGatherStatsPeriod > time_now) {
444 return;
445 }
446 stats_gathering_started_ = time_now;
447
448 if (session_) {
449 ExtractSessionInfo();
450 ExtractVoiceInfo();
451 ExtractVideoInfo();
452 }
453}
454
wu@webrtc.org97077a32013-10-25 21:18:33 +0000455StatsReport* StatsCollector::PrepareLocalReport(
456 uint32 ssrc,
457 const std::string& transport_id) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000458 std::string ssrc_id = talk_base::ToString<uint32>(ssrc);
459 StatsMap::iterator it = reports_.find(StatsId(
460 StatsReport::kStatsReportTypeSsrc, ssrc_id));
461
462 std::string track_id;
463 if (it == reports_.end()) {
464 if (!session()->GetTrackIdBySsrc(ssrc, &track_id)) {
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000465 LOG(LS_WARNING) << "The SSRC " << ssrc
466 << " is not associated with a track";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000467 return NULL;
468 }
469 } else {
470 // Keeps the old track id since we want to report the stats for inactive
471 // tracks.
472 ExtractValueFromReport(it->second,
473 StatsReport::kStatsValueNameTrackId,
474 &track_id);
475 }
476
wu@webrtc.org97077a32013-10-25 21:18:33 +0000477 StatsReport* report = GetOrCreateReport(StatsReport::kStatsReportTypeSsrc,
478 ssrc_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000479
480 // Clear out stats from previous GatherStats calls if any.
481 if (report->timestamp != stats_gathering_started_) {
482 report->values.clear();
483 report->timestamp = stats_gathering_started_;
484 }
485
486 report->AddValue(StatsReport::kStatsValueNameSsrc, ssrc_id);
487 report->AddValue(StatsReport::kStatsValueNameTrackId, track_id);
488 // Add the mapping of SSRC to transport.
489 report->AddValue(StatsReport::kStatsValueNameTransportId,
490 transport_id);
491 return report;
492}
493
wu@webrtc.org97077a32013-10-25 21:18:33 +0000494StatsReport* StatsCollector::PrepareRemoteReport(
495 uint32 ssrc,
496 const std::string& transport_id) {
497 std::string ssrc_id = talk_base::ToString<uint32>(ssrc);
498 StatsMap::iterator it = reports_.find(StatsId(
499 StatsReport::kStatsReportTypeRemoteSsrc, ssrc_id));
500
501 std::string track_id;
502 if (it == reports_.end()) {
503 if (!session()->GetTrackIdBySsrc(ssrc, &track_id)) {
504 LOG(LS_WARNING) << "The SSRC " << ssrc
505 << " is not associated with a track";
506 return NULL;
507 }
508 } else {
509 // Keeps the old track id since we want to report the stats for inactive
510 // tracks.
511 ExtractValueFromReport(it->second,
512 StatsReport::kStatsValueNameTrackId,
513 &track_id);
514 }
515
516 StatsReport* report = GetOrCreateReport(
517 StatsReport::kStatsReportTypeRemoteSsrc, ssrc_id);
518
519 // Clear out stats from previous GatherStats calls if any.
520 // The timestamp will be added later. Zero it for debugging.
521 report->values.clear();
522 report->timestamp = 0;
523
524 report->AddValue(StatsReport::kStatsValueNameSsrc, ssrc_id);
525 report->AddValue(StatsReport::kStatsValueNameTrackId, track_id);
526 // Add the mapping of SSRC to transport.
527 report->AddValue(StatsReport::kStatsValueNameTransportId,
528 transport_id);
529 return report;
530}
531
wu@webrtc.org4551b792013-10-09 15:37:36 +0000532std::string StatsCollector::AddOneCertificateReport(
533 const talk_base::SSLCertificate* cert, const std::string& issuer_id) {
534 // TODO(bemasc): Move this computation to a helper class that caches these
535 // values to reduce CPU use in GetStats. This will require adding a fast
536 // SSLCertificate::Equals() method to detect certificate changes.
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000537
538 std::string digest_algorithm;
539 if (!cert->GetSignatureDigestAlgorithm(&digest_algorithm))
540 return std::string();
541
wu@webrtc.org4551b792013-10-09 15:37:36 +0000542 talk_base::scoped_ptr<talk_base::SSLFingerprint> ssl_fingerprint(
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000543 talk_base::SSLFingerprint::Create(digest_algorithm, cert));
wu@webrtc.org4551b792013-10-09 15:37:36 +0000544 std::string fingerprint = ssl_fingerprint->GetRfc4572Fingerprint();
545
546 talk_base::Buffer der_buffer;
547 cert->ToDER(&der_buffer);
548 std::string der_base64;
549 talk_base::Base64::EncodeFromArray(
550 der_buffer.data(), der_buffer.length(), &der_base64);
551
552 StatsReport report;
553 report.type = StatsReport::kStatsReportTypeCertificate;
554 report.id = StatsId(report.type, fingerprint);
555 report.timestamp = stats_gathering_started_;
556 report.AddValue(StatsReport::kStatsValueNameFingerprint, fingerprint);
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000557 report.AddValue(StatsReport::kStatsValueNameFingerprintAlgorithm,
558 digest_algorithm);
wu@webrtc.org4551b792013-10-09 15:37:36 +0000559 report.AddValue(StatsReport::kStatsValueNameDer, der_base64);
560 if (!issuer_id.empty())
561 report.AddValue(StatsReport::kStatsValueNameIssuerId, issuer_id);
562 reports_[report.id] = report;
563 return report.id;
564}
565
566std::string StatsCollector::AddCertificateReports(
567 const talk_base::SSLCertificate* cert) {
568 // Produces a chain of StatsReports representing this certificate and the rest
569 // of its chain, and adds those reports to |reports_|. The return value is
570 // the id of the leaf report. The provided cert must be non-null, so at least
571 // one report will always be provided and the returned string will never be
572 // empty.
573 ASSERT(cert != NULL);
574
575 std::string issuer_id;
576 talk_base::scoped_ptr<talk_base::SSLCertChain> chain;
577 if (cert->GetChain(chain.accept())) {
578 // This loop runs in reverse, i.e. from root to leaf, so that each
579 // certificate's issuer's report ID is known before the child certificate's
580 // report is generated. The root certificate does not have an issuer ID
581 // value.
582 for (ptrdiff_t i = chain->GetSize() - 1; i >= 0; --i) {
583 const talk_base::SSLCertificate& cert_i = chain->Get(i);
584 issuer_id = AddOneCertificateReport(&cert_i, issuer_id);
585 }
586 }
587 // Add the leaf certificate.
588 return AddOneCertificateReport(cert, issuer_id);
589}
590
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000591void StatsCollector::ExtractSessionInfo() {
592 // Extract information from the base session.
593 StatsReport report;
594 report.id = StatsId(StatsReport::kStatsReportTypeSession, session_->id());
595 report.type = StatsReport::kStatsReportTypeSession;
596 report.timestamp = stats_gathering_started_;
597 report.values.clear();
598 report.AddBoolean(StatsReport::kStatsValueNameInitiator,
599 session_->initiator());
600
601 reports_[report.id] = report;
602
603 cricket::SessionStats stats;
604 if (session_->GetStats(&stats)) {
605 // Store the proxy map away for use in SSRC reporting.
606 proxy_to_transport_ = stats.proxy_to_transport;
607
608 for (cricket::TransportStatsMap::iterator transport_iter
609 = stats.transport_stats.begin();
610 transport_iter != stats.transport_stats.end(); ++transport_iter) {
wu@webrtc.org4551b792013-10-09 15:37:36 +0000611 // Attempt to get a copy of the certificates from the transport and
612 // expose them in stats reports. All channels in a transport share the
613 // same local and remote certificates.
614 std::string local_cert_report_id, remote_cert_report_id;
615 cricket::Transport* transport =
616 session_->GetTransport(transport_iter->second.content_name);
617 if (transport) {
618 talk_base::scoped_ptr<talk_base::SSLIdentity> identity;
619 if (transport->GetIdentity(identity.accept()))
620 local_cert_report_id = AddCertificateReports(
621 &(identity->certificate()));
622
623 talk_base::scoped_ptr<talk_base::SSLCertificate> cert;
624 if (transport->GetRemoteCertificate(cert.accept()))
625 remote_cert_report_id = AddCertificateReports(cert.get());
626 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000627 for (cricket::TransportChannelStatsList::iterator channel_iter
628 = transport_iter->second.channel_stats.begin();
629 channel_iter != transport_iter->second.channel_stats.end();
630 ++channel_iter) {
631 StatsReport channel_report;
632 std::ostringstream ostc;
633 ostc << "Channel-" << transport_iter->second.content_name
634 << "-" << channel_iter->component;
635 channel_report.id = ostc.str();
636 channel_report.type = StatsReport::kStatsReportTypeComponent;
637 channel_report.timestamp = stats_gathering_started_;
638 channel_report.AddValue(StatsReport::kStatsValueNameComponent,
639 channel_iter->component);
wu@webrtc.org4551b792013-10-09 15:37:36 +0000640 if (!local_cert_report_id.empty())
641 channel_report.AddValue(
642 StatsReport::kStatsValueNameLocalCertificateId,
643 local_cert_report_id);
644 if (!remote_cert_report_id.empty())
645 channel_report.AddValue(
646 StatsReport::kStatsValueNameRemoteCertificateId,
647 remote_cert_report_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000648 reports_[channel_report.id] = channel_report;
649 for (size_t i = 0;
650 i < channel_iter->connection_infos.size();
651 ++i) {
652 StatsReport report;
653 const cricket::ConnectionInfo& info
654 = channel_iter->connection_infos[i];
655 std::ostringstream ost;
656 ost << "Conn-" << transport_iter->first << "-"
657 << channel_iter->component << "-" << i;
658 report.id = ost.str();
659 report.type = StatsReport::kStatsReportTypeCandidatePair;
660 report.timestamp = stats_gathering_started_;
661 // Link from connection to its containing channel.
662 report.AddValue(StatsReport::kStatsValueNameChannelId,
663 channel_report.id);
664 report.AddValue(StatsReport::kStatsValueNameBytesSent,
665 info.sent_total_bytes);
666 report.AddValue(StatsReport::kStatsValueNameBytesReceived,
667 info.recv_total_bytes);
668 report.AddBoolean(StatsReport::kStatsValueNameWritable,
669 info.writable);
670 report.AddBoolean(StatsReport::kStatsValueNameReadable,
671 info.readable);
672 report.AddBoolean(StatsReport::kStatsValueNameActiveConnection,
673 info.best_connection);
674 report.AddValue(StatsReport::kStatsValueNameLocalAddress,
675 info.local_candidate.address().ToString());
676 report.AddValue(StatsReport::kStatsValueNameRemoteAddress,
677 info.remote_candidate.address().ToString());
wu@webrtc.org97077a32013-10-25 21:18:33 +0000678 report.AddValue(StatsReport::kStatsValueNameRtt, info.rtt);
679 report.AddValue(StatsReport::kStatsValueNameTransportType,
680 info.local_candidate.protocol());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000681 reports_[report.id] = report;
682 }
683 }
684 }
685 }
686}
687
688void StatsCollector::ExtractVoiceInfo() {
689 if (!session_->voice_channel()) {
690 return;
691 }
692 cricket::VoiceMediaInfo voice_info;
693 if (!session_->voice_channel()->GetStats(&voice_info)) {
694 LOG(LS_ERROR) << "Failed to get voice channel stats.";
695 return;
696 }
697 std::string transport_id;
698 if (!GetTransportIdFromProxy(session_->voice_channel()->content_name(),
699 &transport_id)) {
700 LOG(LS_ERROR) << "Failed to get transport name for proxy "
701 << session_->voice_channel()->content_name();
702 return;
703 }
704 ExtractStatsFromList(voice_info.receivers, transport_id, this);
705 ExtractStatsFromList(voice_info.senders, transport_id, this);
706}
707
708void StatsCollector::ExtractVideoInfo() {
709 if (!session_->video_channel()) {
710 return;
711 }
712 cricket::VideoMediaInfo video_info;
713 if (!session_->video_channel()->GetStats(&video_info)) {
714 LOG(LS_ERROR) << "Failed to get video channel stats.";
715 return;
716 }
717 std::string transport_id;
718 if (!GetTransportIdFromProxy(session_->video_channel()->content_name(),
719 &transport_id)) {
720 LOG(LS_ERROR) << "Failed to get transport name for proxy "
721 << session_->video_channel()->content_name();
722 return;
723 }
724 ExtractStatsFromList(video_info.receivers, transport_id, this);
725 ExtractStatsFromList(video_info.senders, transport_id, this);
726 if (video_info.bw_estimations.size() != 1) {
727 LOG(LS_ERROR) << "BWEs count: " << video_info.bw_estimations.size();
728 } else {
729 StatsReport* report = &reports_[StatsReport::kStatsReportVideoBweId];
730 ExtractStats(
731 video_info.bw_estimations[0], stats_gathering_started_, report);
732 }
733}
734
735double StatsCollector::GetTimeNow() {
736 return timing_.WallTimeNow() * talk_base::kNumMillisecsPerSec;
737}
738
739bool StatsCollector::GetTransportIdFromProxy(const std::string& proxy,
740 std::string* transport) {
741 // TODO(hta): Remove handling of empty proxy name once tests do not use it.
742 if (proxy.empty()) {
743 transport->clear();
744 return true;
745 }
746 if (proxy_to_transport_.find(proxy) == proxy_to_transport_.end()) {
747 LOG(LS_ERROR) << "No transport ID mapping for " << proxy;
748 return false;
749 }
750 std::ostringstream ost;
751 // Component 1 is always used for RTP.
752 ost << "Channel-" << proxy_to_transport_[proxy] << "-1";
753 *transport = ost.str();
754 return true;
755}
756
wu@webrtc.org97077a32013-10-25 21:18:33 +0000757StatsReport* StatsCollector::GetOrCreateReport(const std::string& type,
758 const std::string& id) {
759 std::string statsid = StatsId(type, id);
760 StatsReport* report = NULL;
761 std::map<std::string, StatsReport>::iterator it = reports_.find(statsid);
762 if (it == reports_.end()) {
763 report = &reports_[statsid]; // Create new element.
764 report->id = statsid;
765 report->type = type;
766 } else {
767 report = &reports_[statsid];
768 }
769 return report;
770}
771
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000772} // namespace webrtc