blob: 296c9cbffacedf1e7a7cc46ac1b2acf281fd4c27 [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
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000033#include "talk/session/media/channel.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000034#include "webrtc/base/base64.h"
35#include "webrtc/base/scoped_ptr.h"
36#include "webrtc/base/timing.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000037
38namespace webrtc {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000039namespace {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000040
tommi@webrtc.org47218952014-07-15 19:22:37 +000041double GetTimeNow() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000042 return rtc::Timing::WallTimeNow() * rtc::kNumMillisecsPerSec;
tommi@webrtc.org47218952014-07-15 19:22:37 +000043}
44
45bool GetTransportIdFromProxy(const cricket::ProxyTransportMap& map,
46 const std::string& proxy,
47 std::string* transport) {
48 // TODO(hta): Remove handling of empty proxy name once tests do not use it.
49 if (proxy.empty()) {
50 transport->clear();
51 return true;
52 }
53
54 cricket::ProxyTransportMap::const_iterator found = map.find(proxy);
55 if (found == map.end()) {
56 LOG(LS_ERROR) << "No transport ID mapping for " << proxy;
57 return false;
58 }
59
60 std::ostringstream ost;
61 // Component 1 is always used for RTP.
62 ost << "Channel-" << found->second << "-1";
63 *transport = ost.str();
64 return true;
65}
66
henrike@webrtc.org28e20752013-07-10 00:45:36 +000067std::string StatsId(const std::string& type, const std::string& id) {
68 return type + "_" + id;
69}
70
xians@webrtc.org4cb01282014-06-12 14:57:05 +000071std::string StatsId(const std::string& type, const std::string& id,
72 StatsCollector::TrackDirection direction) {
73 ASSERT(direction == StatsCollector::kSending ||
74 direction == StatsCollector::kReceiving);
75
76 // Strings for the direction of the track.
77 const char kSendDirection[] = "send";
78 const char kRecvDirection[] = "recv";
79
80 const std::string direction_id = (direction == StatsCollector::kSending) ?
81 kSendDirection : kRecvDirection;
82 return type + "_" + id + "_" + direction_id;
83}
84
henrike@webrtc.org28e20752013-07-10 00:45:36 +000085bool ExtractValueFromReport(
86 const StatsReport& report,
tommi@webrtc.org242068d2014-07-14 20:19:56 +000087 StatsReport::StatsValueName name,
henrike@webrtc.org28e20752013-07-10 00:45:36 +000088 std::string* value) {
89 StatsReport::Values::const_iterator it = report.values.begin();
90 for (; it != report.values.end(); ++it) {
91 if (it->name == name) {
92 *value = it->value;
93 return true;
94 }
95 }
96 return false;
97}
98
tommi@webrtc.org5b06b062014-08-15 08:38:30 +000099void AddTrackReport(StatsSet* reports, const std::string& track_id) {
xians@webrtc.org01bda202014-07-09 07:38:38 +0000100 // Adds an empty track report.
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000101 StatsReport* report = reports->ReplaceOrAddNew(
102 StatsId(StatsReport::kStatsReportTypeTrack, track_id));
103 report->type = StatsReport::kStatsReportTypeTrack;
104 report->AddValue(StatsReport::kStatsValueNameTrackId, track_id);
xians@webrtc.org01bda202014-07-09 07:38:38 +0000105}
106
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000107template <class TrackVector>
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000108void CreateTrackReports(const TrackVector& tracks, StatsSet* reports) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000109 for (size_t j = 0; j < tracks.size(); ++j) {
110 webrtc::MediaStreamTrackInterface* track = tracks[j];
xians@webrtc.org01bda202014-07-09 07:38:38 +0000111 AddTrackReport(reports, track->id());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000112 }
113}
114
115void ExtractStats(const cricket::VoiceReceiverInfo& info, StatsReport* report) {
116 report->AddValue(StatsReport::kStatsValueNameAudioOutputLevel,
117 info.audio_level);
118 report->AddValue(StatsReport::kStatsValueNameBytesReceived,
119 info.bytes_rcvd);
120 report->AddValue(StatsReport::kStatsValueNameJitterReceived,
121 info.jitter_ms);
jiayl@webrtc.org11aab0e2014-03-07 18:56:26 +0000122 report->AddValue(StatsReport::kStatsValueNameJitterBufferMs,
123 info.jitter_buffer_ms);
124 report->AddValue(StatsReport::kStatsValueNamePreferredJitterBufferMs,
125 info.jitter_buffer_preferred_ms);
126 report->AddValue(StatsReport::kStatsValueNameCurrentDelayMs,
127 info.delay_estimate_ms);
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000128 report->AddValue(StatsReport::kStatsValueNameExpandRate,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000129 rtc::ToString<float>(info.expand_rate));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000130 report->AddValue(StatsReport::kStatsValueNamePacketsReceived,
131 info.packets_rcvd);
132 report->AddValue(StatsReport::kStatsValueNamePacketsLost,
133 info.packets_lost);
buildbot@webrtc.org3e01e0b2014-05-13 17:54:10 +0000134 report->AddValue(StatsReport::kStatsValueNameDecodingCTSG,
135 info.decoding_calls_to_silence_generator);
136 report->AddValue(StatsReport::kStatsValueNameDecodingCTN,
137 info.decoding_calls_to_neteq);
138 report->AddValue(StatsReport::kStatsValueNameDecodingNormal,
139 info.decoding_normal);
140 report->AddValue(StatsReport::kStatsValueNameDecodingPLC,
141 info.decoding_plc);
142 report->AddValue(StatsReport::kStatsValueNameDecodingCNG,
143 info.decoding_cng);
144 report->AddValue(StatsReport::kStatsValueNameDecodingPLCCNG,
145 info.decoding_plc_cng);
buildbot@webrtc.orgb525a9d2014-06-03 09:42:15 +0000146 report->AddValue(StatsReport::kStatsValueNameCaptureStartNtpTimeMs,
147 info.capture_start_ntp_time_ms);
buildbot@webrtc.org7e71b772014-06-13 01:14:01 +0000148 report->AddValue(StatsReport::kStatsValueNameCodecName, info.codec_name);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000149}
150
151void ExtractStats(const cricket::VoiceSenderInfo& info, StatsReport* report) {
152 report->AddValue(StatsReport::kStatsValueNameAudioInputLevel,
153 info.audio_level);
154 report->AddValue(StatsReport::kStatsValueNameBytesSent,
155 info.bytes_sent);
156 report->AddValue(StatsReport::kStatsValueNamePacketsSent,
157 info.packets_sent);
henrike@webrtc.orgffe26202014-03-19 22:20:10 +0000158 report->AddValue(StatsReport::kStatsValueNamePacketsLost,
159 info.packets_lost);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000160 report->AddValue(StatsReport::kStatsValueNameJitterReceived,
161 info.jitter_ms);
162 report->AddValue(StatsReport::kStatsValueNameRtt, info.rtt_ms);
163 report->AddValue(StatsReport::kStatsValueNameEchoCancellationQualityMin,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000164 rtc::ToString<float>(info.aec_quality_min));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000165 report->AddValue(StatsReport::kStatsValueNameEchoDelayMedian,
166 info.echo_delay_median_ms);
167 report->AddValue(StatsReport::kStatsValueNameEchoDelayStdDev,
168 info.echo_delay_std_ms);
169 report->AddValue(StatsReport::kStatsValueNameEchoReturnLoss,
170 info.echo_return_loss);
171 report->AddValue(StatsReport::kStatsValueNameEchoReturnLossEnhancement,
172 info.echo_return_loss_enhancement);
173 report->AddValue(StatsReport::kStatsValueNameCodecName, info.codec_name);
wu@webrtc.org967bfff2013-09-19 05:49:50 +0000174 report->AddBoolean(StatsReport::kStatsValueNameTypingNoiseState,
175 info.typing_noise_detected);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000176}
177
178void ExtractStats(const cricket::VideoReceiverInfo& info, StatsReport* report) {
179 report->AddValue(StatsReport::kStatsValueNameBytesReceived,
180 info.bytes_rcvd);
181 report->AddValue(StatsReport::kStatsValueNamePacketsReceived,
182 info.packets_rcvd);
183 report->AddValue(StatsReport::kStatsValueNamePacketsLost,
184 info.packets_lost);
185
186 report->AddValue(StatsReport::kStatsValueNameFirsSent,
187 info.firs_sent);
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +0000188 report->AddValue(StatsReport::kStatsValueNamePlisSent,
189 info.plis_sent);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000190 report->AddValue(StatsReport::kStatsValueNameNacksSent,
191 info.nacks_sent);
192 report->AddValue(StatsReport::kStatsValueNameFrameWidthReceived,
193 info.frame_width);
194 report->AddValue(StatsReport::kStatsValueNameFrameHeightReceived,
195 info.frame_height);
196 report->AddValue(StatsReport::kStatsValueNameFrameRateReceived,
197 info.framerate_rcvd);
198 report->AddValue(StatsReport::kStatsValueNameFrameRateDecoded,
199 info.framerate_decoded);
200 report->AddValue(StatsReport::kStatsValueNameFrameRateOutput,
201 info.framerate_output);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000202
203 report->AddValue(StatsReport::kStatsValueNameDecodeMs,
204 info.decode_ms);
205 report->AddValue(StatsReport::kStatsValueNameMaxDecodeMs,
206 info.max_decode_ms);
207 report->AddValue(StatsReport::kStatsValueNameCurrentDelayMs,
208 info.current_delay_ms);
209 report->AddValue(StatsReport::kStatsValueNameTargetDelayMs,
210 info.target_delay_ms);
211 report->AddValue(StatsReport::kStatsValueNameJitterBufferMs,
212 info.jitter_buffer_ms);
213 report->AddValue(StatsReport::kStatsValueNameMinPlayoutDelayMs,
214 info.min_playout_delay_ms);
215 report->AddValue(StatsReport::kStatsValueNameRenderDelayMs,
216 info.render_delay_ms);
buildbot@webrtc.org0581f0b2014-05-06 21:36:31 +0000217
218 report->AddValue(StatsReport::kStatsValueNameCaptureStartNtpTimeMs,
219 info.capture_start_ntp_time_ms);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000220}
221
222void ExtractStats(const cricket::VideoSenderInfo& info, StatsReport* report) {
223 report->AddValue(StatsReport::kStatsValueNameBytesSent,
224 info.bytes_sent);
225 report->AddValue(StatsReport::kStatsValueNamePacketsSent,
226 info.packets_sent);
henrike@webrtc.orgffe26202014-03-19 22:20:10 +0000227 report->AddValue(StatsReport::kStatsValueNamePacketsLost,
228 info.packets_lost);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000229
230 report->AddValue(StatsReport::kStatsValueNameFirsReceived,
231 info.firs_rcvd);
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +0000232 report->AddValue(StatsReport::kStatsValueNamePlisReceived,
233 info.plis_rcvd);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000234 report->AddValue(StatsReport::kStatsValueNameNacksReceived,
235 info.nacks_rcvd);
wu@webrtc.org987f2c92014-03-28 16:22:19 +0000236 report->AddValue(StatsReport::kStatsValueNameFrameWidthInput,
237 info.input_frame_width);
238 report->AddValue(StatsReport::kStatsValueNameFrameHeightInput,
239 info.input_frame_height);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000240 report->AddValue(StatsReport::kStatsValueNameFrameWidthSent,
wu@webrtc.org987f2c92014-03-28 16:22:19 +0000241 info.send_frame_width);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000242 report->AddValue(StatsReport::kStatsValueNameFrameHeightSent,
wu@webrtc.org987f2c92014-03-28 16:22:19 +0000243 info.send_frame_height);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000244 report->AddValue(StatsReport::kStatsValueNameFrameRateInput,
245 info.framerate_input);
246 report->AddValue(StatsReport::kStatsValueNameFrameRateSent,
247 info.framerate_sent);
248 report->AddValue(StatsReport::kStatsValueNameRtt, info.rtt_ms);
249 report->AddValue(StatsReport::kStatsValueNameCodecName, info.codec_name);
mallinath@webrtc.org62451dc2013-12-13 12:29:34 +0000250 report->AddBoolean(StatsReport::kStatsValueNameCpuLimitedResolution,
251 (info.adapt_reason & 0x1) > 0);
252 report->AddBoolean(StatsReport::kStatsValueNameBandwidthLimitedResolution,
253 (info.adapt_reason & 0x2) > 0);
254 report->AddBoolean(StatsReport::kStatsValueNameViewLimitedResolution,
255 (info.adapt_reason & 0x4) > 0);
buildbot@webrtc.org71dffb72014-06-24 07:24:49 +0000256 report->AddValue(StatsReport::kStatsValueNameAdaptationChanges,
257 info.adapt_changes);
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000258 report->AddValue(StatsReport::kStatsValueNameAvgEncodeMs, info.avg_encode_ms);
259 report->AddValue(StatsReport::kStatsValueNameCaptureJitterMs,
260 info.capture_jitter_ms);
wu@webrtc.org9caf2762013-12-11 18:25:07 +0000261 report->AddValue(StatsReport::kStatsValueNameCaptureQueueDelayMsPerS,
262 info.capture_queue_delay_ms_per_s);
263 report->AddValue(StatsReport::kStatsValueNameEncodeUsagePercent,
264 info.encode_usage_percent);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000265}
266
267void ExtractStats(const cricket::BandwidthEstimationInfo& info,
268 double stats_gathering_started,
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000269 PeerConnectionInterface::StatsOutputLevel level,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000270 StatsReport* report) {
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000271 ASSERT(report->id == StatsReport::kStatsReportVideoBweId);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000272 report->type = StatsReport::kStatsReportTypeBwe;
273
274 // Clear out stats from previous GatherStats calls if any.
275 if (report->timestamp != stats_gathering_started) {
276 report->values.clear();
277 report->timestamp = stats_gathering_started;
278 }
279
280 report->AddValue(StatsReport::kStatsValueNameAvailableSendBandwidth,
281 info.available_send_bandwidth);
282 report->AddValue(StatsReport::kStatsValueNameAvailableReceiveBandwidth,
283 info.available_recv_bandwidth);
284 report->AddValue(StatsReport::kStatsValueNameTargetEncBitrate,
285 info.target_enc_bitrate);
286 report->AddValue(StatsReport::kStatsValueNameActualEncBitrate,
287 info.actual_enc_bitrate);
288 report->AddValue(StatsReport::kStatsValueNameRetransmitBitrate,
289 info.retransmit_bitrate);
290 report->AddValue(StatsReport::kStatsValueNameTransmitBitrate,
291 info.transmit_bitrate);
292 report->AddValue(StatsReport::kStatsValueNameBucketDelay,
293 info.bucket_delay);
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000294 if (level >= PeerConnectionInterface::kStatsOutputLevelDebug) {
295 report->AddValue(
296 StatsReport::kStatsValueNameRecvPacketGroupPropagationDeltaSumDebug,
297 info.total_received_propagation_delta_ms);
298 if (info.recent_received_propagation_delta_ms.size() > 0) {
299 report->AddValue(
300 StatsReport::kStatsValueNameRecvPacketGroupPropagationDeltaDebug,
301 info.recent_received_propagation_delta_ms);
302 report->AddValue(
303 StatsReport::kStatsValueNameRecvPacketGroupArrivalTimeDebug,
304 info.recent_received_packet_group_arrival_time_ms);
305 }
306 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000307}
308
wu@webrtc.org97077a32013-10-25 21:18:33 +0000309void ExtractRemoteStats(const cricket::MediaSenderInfo& info,
310 StatsReport* report) {
311 report->timestamp = info.remote_stats[0].timestamp;
312 // TODO(hta): Extract some stats here.
313}
314
315void ExtractRemoteStats(const cricket::MediaReceiverInfo& info,
316 StatsReport* report) {
317 report->timestamp = info.remote_stats[0].timestamp;
318 // TODO(hta): Extract some stats here.
319}
320
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000321// Template to extract stats from a data vector.
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000322// In order to use the template, the functions that are called from it,
323// ExtractStats and ExtractRemoteStats, must be defined and overloaded
324// for each type.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000325template<typename T>
326void ExtractStatsFromList(const std::vector<T>& data,
327 const std::string& transport_id,
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000328 StatsCollector* collector,
329 StatsCollector::TrackDirection direction) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000330 typename std::vector<T>::const_iterator it = data.begin();
331 for (; it != data.end(); ++it) {
332 std::string id;
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000333 uint32 ssrc = it->ssrc();
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000334 // Each track can have stats for both local and remote objects.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000335 // TODO(hta): Handle the case of multiple SSRCs per object.
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000336 StatsReport* report = collector->PrepareLocalReport(ssrc, transport_id,
337 direction);
338 if (report)
339 ExtractStats(*it, report);
340
wu@webrtc.org97077a32013-10-25 21:18:33 +0000341 if (it->remote_stats.size() > 0) {
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000342 report = collector->PrepareRemoteReport(ssrc, transport_id,
343 direction);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000344 if (!report) {
345 continue;
346 }
347 ExtractRemoteStats(*it, report);
348 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000349 }
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000350}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000351
352} // namespace
353
tommi@webrtc.org03505bc2014-07-14 20:15:26 +0000354StatsCollector::StatsCollector(WebRtcSession* session)
355 : session_(session), stats_gathering_started_(0) {
356 ASSERT(session_);
357}
358
359StatsCollector::~StatsCollector() {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000360}
361
362// Adds a MediaStream with tracks that can be used as a |selector| in a call
363// to GetStats.
364void StatsCollector::AddStream(MediaStreamInterface* stream) {
365 ASSERT(stream != NULL);
366
367 CreateTrackReports<AudioTrackVector>(stream->GetAudioTracks(),
368 &reports_);
369 CreateTrackReports<VideoTrackVector>(stream->GetVideoTracks(),
370 &reports_);
371}
372
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000373void StatsCollector::AddLocalAudioTrack(AudioTrackInterface* audio_track,
374 uint32 ssrc) {
375 ASSERT(audio_track != NULL);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000376 for (LocalAudioTrackVector::iterator it = local_audio_tracks_.begin();
377 it != local_audio_tracks_.end(); ++it) {
378 ASSERT(it->first != audio_track || it->second != ssrc);
379 }
xians@webrtc.org01bda202014-07-09 07:38:38 +0000380
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000381 local_audio_tracks_.push_back(std::make_pair(audio_track, ssrc));
xians@webrtc.org01bda202014-07-09 07:38:38 +0000382
383 // Create the kStatsReportTypeTrack report for the new track if there is no
384 // report yet.
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000385 StatsReport* found = reports_.Find(
xians@webrtc.org01bda202014-07-09 07:38:38 +0000386 StatsId(StatsReport::kStatsReportTypeTrack, audio_track->id()));
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000387 if (!found)
xians@webrtc.org01bda202014-07-09 07:38:38 +0000388 AddTrackReport(&reports_, audio_track->id());
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000389}
390
391void StatsCollector::RemoveLocalAudioTrack(AudioTrackInterface* audio_track,
392 uint32 ssrc) {
393 ASSERT(audio_track != NULL);
394 for (LocalAudioTrackVector::iterator it = local_audio_tracks_.begin();
395 it != local_audio_tracks_.end(); ++it) {
396 if (it->first == audio_track && it->second == ssrc) {
397 local_audio_tracks_.erase(it);
398 return;
399 }
400 }
401
402 ASSERT(false);
403}
404
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000405void StatsCollector::GetStats(MediaStreamTrackInterface* track,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000406 StatsReports* reports) {
407 ASSERT(reports != NULL);
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000408 ASSERT(reports->empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000409
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000410 if (!track) {
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000411 StatsSet::const_iterator it;
412 for (it = reports_.begin(); it != reports_.end(); ++it)
413 reports->push_back(&(*it));
414 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000415 }
416
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000417 StatsReport* report =
418 reports_.Find(StatsId(StatsReport::kStatsReportTypeSession,
419 session_->id()));
420 if (report)
421 reports->push_back(report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000422
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000423 report = reports_.Find(
424 StatsId(StatsReport::kStatsReportTypeTrack, track->id()));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000425
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000426 if (!report)
427 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000428
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000429 reports->push_back(report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000430
431 std::string track_id;
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000432 for (StatsSet::const_iterator it = reports_.begin(); it != reports_.end();
433 ++it) {
434 if (it->type != StatsReport::kStatsReportTypeSsrc)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000435 continue;
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000436
437 if (ExtractValueFromReport(*it,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000438 StatsReport::kStatsValueNameTrackId,
439 &track_id)) {
440 if (track_id == track->id()) {
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000441 reports->push_back(&(*it));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000442 }
443 }
444 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000445}
446
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000447void
448StatsCollector::UpdateStats(PeerConnectionInterface::StatsOutputLevel level) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000449 double time_now = GetTimeNow();
450 // Calls to UpdateStats() that occur less than kMinGatherStatsPeriod number of
451 // ms apart will be ignored.
452 const double kMinGatherStatsPeriod = 50;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000453 if (stats_gathering_started_ != 0 &&
454 stats_gathering_started_ + kMinGatherStatsPeriod > time_now) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000455 return;
456 }
457 stats_gathering_started_ = time_now;
458
459 if (session_) {
460 ExtractSessionInfo();
461 ExtractVoiceInfo();
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000462 ExtractVideoInfo(level);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000463 }
464}
465
wu@webrtc.org97077a32013-10-25 21:18:33 +0000466StatsReport* StatsCollector::PrepareLocalReport(
467 uint32 ssrc,
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000468 const std::string& transport_id,
469 TrackDirection direction) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000470 const std::string ssrc_id = rtc::ToString<uint32>(ssrc);
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000471 StatsReport* report = reports_.Find(
472 StatsId(StatsReport::kStatsReportTypeSsrc, ssrc_id, direction));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000473
xians@webrtc.org01bda202014-07-09 07:38:38 +0000474 // Use the ID of the track that is currently mapped to the SSRC, if any.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000475 std::string track_id;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000476 if (!GetTrackIdBySsrc(ssrc, &track_id, direction)) {
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000477 if (!report) {
xians@webrtc.org01bda202014-07-09 07:38:38 +0000478 // The ssrc is not used by any track or existing report, return NULL
479 // in such case to indicate no report is prepared for the ssrc.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000480 return NULL;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000481 }
482
483 // The ssrc is not used by any existing track. Keeps the old track id
484 // since we want to report the stats for inactive ssrc.
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000485 ExtractValueFromReport(*report,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000486 StatsReport::kStatsValueNameTrackId,
487 &track_id);
488 }
489
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000490 report = GetOrCreateReport(
491 StatsReport::kStatsReportTypeSsrc, ssrc_id, direction);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000492
493 // Clear out stats from previous GatherStats calls if any.
xians@webrtc.org01bda202014-07-09 07:38:38 +0000494 // This is required since the report will be returned for the new values.
495 // Having the old values in the report will lead to multiple values with
496 // the same name.
497 // TODO(xians): Consider changing StatsReport to use map instead of vector.
498 report->values.clear();
499 report->timestamp = stats_gathering_started_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000500
501 report->AddValue(StatsReport::kStatsValueNameSsrc, ssrc_id);
502 report->AddValue(StatsReport::kStatsValueNameTrackId, track_id);
503 // Add the mapping of SSRC to transport.
504 report->AddValue(StatsReport::kStatsValueNameTransportId,
505 transport_id);
506 return report;
507}
508
wu@webrtc.org97077a32013-10-25 21:18:33 +0000509StatsReport* StatsCollector::PrepareRemoteReport(
510 uint32 ssrc,
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000511 const std::string& transport_id,
512 TrackDirection direction) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000513 const std::string ssrc_id = rtc::ToString<uint32>(ssrc);
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000514 StatsReport* report = reports_.Find(
515 StatsId(StatsReport::kStatsReportTypeRemoteSsrc, ssrc_id, direction));
wu@webrtc.org97077a32013-10-25 21:18:33 +0000516
xians@webrtc.org01bda202014-07-09 07:38:38 +0000517 // Use the ID of the track that is currently mapped to the SSRC, if any.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000518 std::string track_id;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000519 if (!GetTrackIdBySsrc(ssrc, &track_id, direction)) {
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000520 if (!report) {
xians@webrtc.org01bda202014-07-09 07:38:38 +0000521 // The ssrc is not used by any track or existing report, return NULL
522 // in such case to indicate no report is prepared for the ssrc.
wu@webrtc.org97077a32013-10-25 21:18:33 +0000523 return NULL;
xians@webrtc.org01bda202014-07-09 07:38:38 +0000524 }
525
526 // The ssrc is not used by any existing track. Keeps the old track id
527 // since we want to report the stats for inactive ssrc.
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000528 ExtractValueFromReport(*report,
wu@webrtc.org97077a32013-10-25 21:18:33 +0000529 StatsReport::kStatsValueNameTrackId,
530 &track_id);
531 }
532
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000533 report = GetOrCreateReport(
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000534 StatsReport::kStatsReportTypeRemoteSsrc, ssrc_id, direction);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000535
536 // Clear out stats from previous GatherStats calls if any.
537 // The timestamp will be added later. Zero it for debugging.
538 report->values.clear();
539 report->timestamp = 0;
540
541 report->AddValue(StatsReport::kStatsValueNameSsrc, ssrc_id);
542 report->AddValue(StatsReport::kStatsValueNameTrackId, track_id);
543 // Add the mapping of SSRC to transport.
544 report->AddValue(StatsReport::kStatsValueNameTransportId,
545 transport_id);
546 return report;
547}
548
wu@webrtc.org4551b792013-10-09 15:37:36 +0000549std::string StatsCollector::AddOneCertificateReport(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000550 const rtc::SSLCertificate* cert, const std::string& issuer_id) {
wu@webrtc.org4551b792013-10-09 15:37:36 +0000551 // TODO(bemasc): Move this computation to a helper class that caches these
552 // values to reduce CPU use in GetStats. This will require adding a fast
553 // SSLCertificate::Equals() method to detect certificate changes.
mallinath@webrtc.org19f27e62013-10-13 17:18:27 +0000554
555 std::string digest_algorithm;
556 if (!cert->GetSignatureDigestAlgorithm(&digest_algorithm))
557 return std::string();
558
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000559 rtc::scoped_ptr<rtc::SSLFingerprint> ssl_fingerprint(
560 rtc::SSLFingerprint::Create(digest_algorithm, cert));
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000561
562 // SSLFingerprint::Create can fail if the algorithm returned by
563 // SSLCertificate::GetSignatureDigestAlgorithm is not supported by the
564 // implementation of SSLCertificate::ComputeDigest. This currently happens
565 // with MD5- and SHA-224-signed certificates when linked to libNSS.
566 if (!ssl_fingerprint)
567 return std::string();
568
wu@webrtc.org4551b792013-10-09 15:37:36 +0000569 std::string fingerprint = ssl_fingerprint->GetRfc4572Fingerprint();
570
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000571 rtc::Buffer der_buffer;
wu@webrtc.org4551b792013-10-09 15:37:36 +0000572 cert->ToDER(&der_buffer);
573 std::string der_base64;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000574 rtc::Base64::EncodeFromArray(
wu@webrtc.org4551b792013-10-09 15:37:36 +0000575 der_buffer.data(), der_buffer.length(), &der_base64);
576
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000577 StatsReport* report = reports_.ReplaceOrAddNew(
578 StatsId(StatsReport::kStatsReportTypeCertificate, fingerprint));
579 report->type = StatsReport::kStatsReportTypeCertificate;
580 report->timestamp = stats_gathering_started_;
581 report->AddValue(StatsReport::kStatsValueNameFingerprint, fingerprint);
582 report->AddValue(StatsReport::kStatsValueNameFingerprintAlgorithm,
583 digest_algorithm);
584 report->AddValue(StatsReport::kStatsValueNameDer, der_base64);
wu@webrtc.org4551b792013-10-09 15:37:36 +0000585 if (!issuer_id.empty())
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000586 report->AddValue(StatsReport::kStatsValueNameIssuerId, issuer_id);
587 return report->id;
wu@webrtc.org4551b792013-10-09 15:37:36 +0000588}
589
590std::string StatsCollector::AddCertificateReports(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000591 const rtc::SSLCertificate* cert) {
wu@webrtc.org4551b792013-10-09 15:37:36 +0000592 // Produces a chain of StatsReports representing this certificate and the rest
593 // of its chain, and adds those reports to |reports_|. The return value is
594 // the id of the leaf report. The provided cert must be non-null, so at least
595 // one report will always be provided and the returned string will never be
596 // empty.
597 ASSERT(cert != NULL);
598
599 std::string issuer_id;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000600 rtc::scoped_ptr<rtc::SSLCertChain> chain;
wu@webrtc.org4551b792013-10-09 15:37:36 +0000601 if (cert->GetChain(chain.accept())) {
602 // This loop runs in reverse, i.e. from root to leaf, so that each
603 // certificate's issuer's report ID is known before the child certificate's
604 // report is generated. The root certificate does not have an issuer ID
605 // value.
606 for (ptrdiff_t i = chain->GetSize() - 1; i >= 0; --i) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000607 const rtc::SSLCertificate& cert_i = chain->Get(i);
wu@webrtc.org4551b792013-10-09 15:37:36 +0000608 issuer_id = AddOneCertificateReport(&cert_i, issuer_id);
609 }
610 }
611 // Add the leaf certificate.
612 return AddOneCertificateReport(cert, issuer_id);
613}
614
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000615void StatsCollector::ExtractSessionInfo() {
616 // Extract information from the base session.
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000617 StatsReport* report = reports_.ReplaceOrAddNew(
618 StatsId(StatsReport::kStatsReportTypeSession, session_->id()));
619 report->type = StatsReport::kStatsReportTypeSession;
620 report->timestamp = stats_gathering_started_;
621 report->values.clear();
622 report->AddBoolean(StatsReport::kStatsValueNameInitiator,
623 session_->initiator());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000624
625 cricket::SessionStats stats;
626 if (session_->GetStats(&stats)) {
627 // Store the proxy map away for use in SSRC reporting.
628 proxy_to_transport_ = stats.proxy_to_transport;
629
630 for (cricket::TransportStatsMap::iterator transport_iter
631 = stats.transport_stats.begin();
632 transport_iter != stats.transport_stats.end(); ++transport_iter) {
wu@webrtc.org4551b792013-10-09 15:37:36 +0000633 // Attempt to get a copy of the certificates from the transport and
634 // expose them in stats reports. All channels in a transport share the
635 // same local and remote certificates.
jiayl@webrtc.org06b04ec2014-07-24 20:41:20 +0000636 //
637 // Note that Transport::GetIdentity and Transport::GetRemoteCertificate
638 // invoke method calls on the worker thread and block this thread, but
639 // messages are still processed on this thread, which may blow way the
640 // existing transports. So we cannot reuse |transport| after these calls.
wu@webrtc.org4551b792013-10-09 15:37:36 +0000641 std::string local_cert_report_id, remote_cert_report_id;
jiayl@webrtc.org06b04ec2014-07-24 20:41:20 +0000642
wu@webrtc.org4551b792013-10-09 15:37:36 +0000643 cricket::Transport* transport =
644 session_->GetTransport(transport_iter->second.content_name);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000645 rtc::scoped_ptr<rtc::SSLIdentity> identity;
jiayl@webrtc.org06b04ec2014-07-24 20:41:20 +0000646 if (transport && transport->GetIdentity(identity.accept())) {
647 local_cert_report_id =
648 AddCertificateReports(&(identity->certificate()));
wu@webrtc.org4551b792013-10-09 15:37:36 +0000649 }
jiayl@webrtc.org06b04ec2014-07-24 20:41:20 +0000650
651 transport = session_->GetTransport(transport_iter->second.content_name);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000652 rtc::scoped_ptr<rtc::SSLCertificate> cert;
jiayl@webrtc.org06b04ec2014-07-24 20:41:20 +0000653 if (transport && transport->GetRemoteCertificate(cert.accept())) {
654 remote_cert_report_id = AddCertificateReports(cert.get());
655 }
656
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000657 for (cricket::TransportChannelStatsList::iterator channel_iter
658 = transport_iter->second.channel_stats.begin();
659 channel_iter != transport_iter->second.channel_stats.end();
660 ++channel_iter) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000661 std::ostringstream ostc;
662 ostc << "Channel-" << transport_iter->second.content_name
663 << "-" << channel_iter->component;
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000664 StatsReport* channel_report = reports_.ReplaceOrAddNew(ostc.str());
665 channel_report->type = StatsReport::kStatsReportTypeComponent;
666 channel_report->timestamp = stats_gathering_started_;
667 channel_report->AddValue(StatsReport::kStatsValueNameComponent,
668 channel_iter->component);
wu@webrtc.org4551b792013-10-09 15:37:36 +0000669 if (!local_cert_report_id.empty())
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000670 channel_report->AddValue(
wu@webrtc.org4551b792013-10-09 15:37:36 +0000671 StatsReport::kStatsValueNameLocalCertificateId,
672 local_cert_report_id);
673 if (!remote_cert_report_id.empty())
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000674 channel_report->AddValue(
wu@webrtc.org4551b792013-10-09 15:37:36 +0000675 StatsReport::kStatsValueNameRemoteCertificateId,
676 remote_cert_report_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000677 for (size_t i = 0;
678 i < channel_iter->connection_infos.size();
679 ++i) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000680 std::ostringstream ost;
681 ost << "Conn-" << transport_iter->first << "-"
682 << channel_iter->component << "-" << i;
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000683 StatsReport* report = reports_.ReplaceOrAddNew(ost.str());
684 report->type = StatsReport::kStatsReportTypeCandidatePair;
685 report->timestamp = stats_gathering_started_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000686 // Link from connection to its containing channel.
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000687 report->AddValue(StatsReport::kStatsValueNameChannelId,
688 channel_report->id);
689
690 const cricket::ConnectionInfo& info =
691 channel_iter->connection_infos[i];
692 report->AddValue(StatsReport::kStatsValueNameBytesSent,
693 info.sent_total_bytes);
guoweis@webrtc.org930e0042014-11-17 19:42:14 +0000694 report->AddValue(StatsReport::kStatsValueNameSendPacketsDiscarded,
695 info.sent_discarded_packets);
696 report->AddValue(StatsReport::kStatsValueNamePacketsSent,
697 info.sent_total_packets);
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000698 report->AddValue(StatsReport::kStatsValueNameBytesReceived,
699 info.recv_total_bytes);
700 report->AddBoolean(StatsReport::kStatsValueNameWritable,
701 info.writable);
702 report->AddBoolean(StatsReport::kStatsValueNameReadable,
703 info.readable);
704 report->AddBoolean(StatsReport::kStatsValueNameActiveConnection,
705 info.best_connection);
706 report->AddValue(StatsReport::kStatsValueNameLocalAddress,
707 info.local_candidate.address().ToString());
708 report->AddValue(StatsReport::kStatsValueNameRemoteAddress,
709 info.remote_candidate.address().ToString());
710 report->AddValue(StatsReport::kStatsValueNameRtt, info.rtt);
711 report->AddValue(StatsReport::kStatsValueNameTransportType,
712 info.local_candidate.protocol());
713 report->AddValue(StatsReport::kStatsValueNameLocalCandidateType,
714 info.local_candidate.type());
715 report->AddValue(StatsReport::kStatsValueNameRemoteCandidateType,
716 info.remote_candidate.type());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000717 }
718 }
719 }
720 }
721}
722
723void StatsCollector::ExtractVoiceInfo() {
724 if (!session_->voice_channel()) {
725 return;
726 }
727 cricket::VoiceMediaInfo voice_info;
728 if (!session_->voice_channel()->GetStats(&voice_info)) {
729 LOG(LS_ERROR) << "Failed to get voice channel stats.";
730 return;
731 }
732 std::string transport_id;
tommi@webrtc.org47218952014-07-15 19:22:37 +0000733 if (!GetTransportIdFromProxy(proxy_to_transport_,
734 session_->voice_channel()->content_name(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000735 &transport_id)) {
736 LOG(LS_ERROR) << "Failed to get transport name for proxy "
737 << session_->voice_channel()->content_name();
738 return;
739 }
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000740 ExtractStatsFromList(voice_info.receivers, transport_id, this, kReceiving);
741 ExtractStatsFromList(voice_info.senders, transport_id, this, kSending);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000742
743 UpdateStatsFromExistingLocalAudioTracks();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000744}
745
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000746void StatsCollector::ExtractVideoInfo(
747 PeerConnectionInterface::StatsOutputLevel level) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000748 if (!session_->video_channel()) {
749 return;
750 }
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000751 cricket::StatsOptions options;
752 options.include_received_propagation_stats =
753 (level >= PeerConnectionInterface::kStatsOutputLevelDebug) ?
754 true : false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000755 cricket::VideoMediaInfo video_info;
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000756 if (!session_->video_channel()->GetStats(options, &video_info)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000757 LOG(LS_ERROR) << "Failed to get video channel stats.";
758 return;
759 }
760 std::string transport_id;
tommi@webrtc.org47218952014-07-15 19:22:37 +0000761 if (!GetTransportIdFromProxy(proxy_to_transport_,
762 session_->video_channel()->content_name(),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000763 &transport_id)) {
764 LOG(LS_ERROR) << "Failed to get transport name for proxy "
765 << session_->video_channel()->content_name();
766 return;
767 }
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000768 ExtractStatsFromList(video_info.receivers, transport_id, this, kReceiving);
769 ExtractStatsFromList(video_info.senders, transport_id, this, kSending);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000770 if (video_info.bw_estimations.size() != 1) {
771 LOG(LS_ERROR) << "BWEs count: " << video_info.bw_estimations.size();
772 } else {
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000773 StatsReport* report =
774 reports_.FindOrAddNew(StatsReport::kStatsReportVideoBweId);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000775 ExtractStats(
wu@webrtc.orgb9a088b2014-02-13 23:18:49 +0000776 video_info.bw_estimations[0], stats_gathering_started_, level, report);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000777 }
778}
779
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000780StatsReport* StatsCollector::GetReport(const std::string& type,
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000781 const std::string& id,
782 TrackDirection direction) {
783 ASSERT(type == StatsReport::kStatsReportTypeSsrc ||
784 type == StatsReport::kStatsReportTypeRemoteSsrc);
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000785 return reports_.Find(StatsId(type, id, direction));
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000786}
787
788StatsReport* StatsCollector::GetOrCreateReport(const std::string& type,
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000789 const std::string& id,
790 TrackDirection direction) {
791 ASSERT(type == StatsReport::kStatsReportTypeSsrc ||
792 type == StatsReport::kStatsReportTypeRemoteSsrc);
793 StatsReport* report = GetReport(type, id, direction);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000794 if (report == NULL) {
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000795 std::string statsid = StatsId(type, id, direction);
tommi@webrtc.org5b06b062014-08-15 08:38:30 +0000796 report = reports_.FindOrAddNew(statsid);
797 ASSERT(report->id == statsid);
wu@webrtc.org97077a32013-10-25 21:18:33 +0000798 report->type = type;
wu@webrtc.org97077a32013-10-25 21:18:33 +0000799 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000800
wu@webrtc.org97077a32013-10-25 21:18:33 +0000801 return report;
802}
803
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000804void StatsCollector::UpdateStatsFromExistingLocalAudioTracks() {
805 // Loop through the existing local audio tracks.
806 for (LocalAudioTrackVector::const_iterator it = local_audio_tracks_.begin();
807 it != local_audio_tracks_.end(); ++it) {
808 AudioTrackInterface* track = it->first;
809 uint32 ssrc = it->second;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000810 std::string ssrc_id = rtc::ToString<uint32>(ssrc);
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000811 StatsReport* report = GetReport(StatsReport::kStatsReportTypeSsrc,
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000812 ssrc_id,
813 kSending);
henrike@webrtc.orgd3d6bce2014-03-10 20:41:22 +0000814 if (report == NULL) {
815 // This can happen if a local audio track is added to a stream on the
816 // fly and the report has not been set up yet. Do nothing in this case.
817 LOG(LS_ERROR) << "Stats report does not exist for ssrc " << ssrc;
818 continue;
819 }
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000820
821 // The same ssrc can be used by both local and remote audio tracks.
822 std::string track_id;
823 if (!ExtractValueFromReport(*report,
824 StatsReport::kStatsValueNameTrackId,
825 &track_id) ||
826 track_id != track->id()) {
827 continue;
828 }
829
830 UpdateReportFromAudioTrack(track, report);
831 }
832}
833
834void StatsCollector::UpdateReportFromAudioTrack(AudioTrackInterface* track,
835 StatsReport* report) {
836 ASSERT(track != NULL);
837 if (report == NULL)
838 return;
839
840 int signal_level = 0;
841 if (track->GetSignalLevel(&signal_level)) {
842 report->ReplaceValue(StatsReport::kStatsValueNameAudioInputLevel,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000843 rtc::ToString<int>(signal_level));
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000844 }
845
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000846 rtc::scoped_refptr<AudioProcessorInterface> audio_processor(
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000847 track->GetAudioProcessor());
848 if (audio_processor.get() == NULL)
849 return;
850
851 AudioProcessorInterface::AudioProcessorStats stats;
852 audio_processor->GetStats(&stats);
853 report->ReplaceValue(StatsReport::kStatsValueNameTypingNoiseState,
854 stats.typing_noise_detected ? "true" : "false");
855 report->ReplaceValue(StatsReport::kStatsValueNameEchoReturnLoss,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000856 rtc::ToString<int>(stats.echo_return_loss));
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000857 report->ReplaceValue(
858 StatsReport::kStatsValueNameEchoReturnLossEnhancement,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000859 rtc::ToString<int>(stats.echo_return_loss_enhancement));
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000860 report->ReplaceValue(StatsReport::kStatsValueNameEchoDelayMedian,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000861 rtc::ToString<int>(stats.echo_delay_median_ms));
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000862 report->ReplaceValue(StatsReport::kStatsValueNameEchoCancellationQualityMin,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000863 rtc::ToString<float>(stats.aec_quality_min));
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000864 report->ReplaceValue(StatsReport::kStatsValueNameEchoDelayStdDev,
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000865 rtc::ToString<int>(stats.echo_delay_std_ms));
henrike@webrtc.org40b3b682014-03-03 18:30:11 +0000866}
867
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000868bool StatsCollector::GetTrackIdBySsrc(uint32 ssrc, std::string* track_id,
869 TrackDirection direction) {
870 if (direction == kSending) {
tommi@webrtc.org03505bc2014-07-14 20:15:26 +0000871 if (!session_->GetLocalTrackIdBySsrc(ssrc, track_id)) {
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000872 LOG(LS_WARNING) << "The SSRC " << ssrc
873 << " is not associated with a sending track";
874 return false;
875 }
876 } else {
877 ASSERT(direction == kReceiving);
tommi@webrtc.org03505bc2014-07-14 20:15:26 +0000878 if (!session_->GetRemoteTrackIdBySsrc(ssrc, track_id)) {
xians@webrtc.org4cb01282014-06-12 14:57:05 +0000879 LOG(LS_WARNING) << "The SSRC " << ssrc
880 << " is not associated with a receiving track";
881 return false;
882 }
883 }
884
885 return true;
886}
887
xians@webrtc.org01bda202014-07-09 07:38:38 +0000888void StatsCollector::ClearUpdateStatsCache() {
889 stats_gathering_started_ = 0;
890}
891
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000892} // namespace webrtc