henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
| 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.org | 4551b79 | 2013-10-09 15:37:36 +0000 | [diff] [blame] | 33 | #include "talk/base/base64.h" |
| 34 | #include "talk/base/scoped_ptr.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 35 | #include "talk/session/media/channel.h" |
| 36 | |
| 37 | namespace webrtc { |
| 38 | |
| 39 | // The items below are in alphabetical order. |
| 40 | const char StatsReport::kStatsValueNameActiveConnection[] = |
| 41 | "googActiveConnection"; |
| 42 | const char StatsReport::kStatsValueNameActualEncBitrate[] = |
| 43 | "googActualEncBitrate"; |
| 44 | const char StatsReport::kStatsValueNameAudioOutputLevel[] = "audioOutputLevel"; |
| 45 | const char StatsReport::kStatsValueNameAudioInputLevel[] = "audioInputLevel"; |
| 46 | const char StatsReport::kStatsValueNameAvailableReceiveBandwidth[] = |
| 47 | "googAvailableReceiveBandwidth"; |
| 48 | const char StatsReport::kStatsValueNameAvailableSendBandwidth[] = |
| 49 | "googAvailableSendBandwidth"; |
| 50 | const char StatsReport::kStatsValueNameBucketDelay[] = "googBucketDelay"; |
| 51 | const char StatsReport::kStatsValueNameBytesReceived[] = "bytesReceived"; |
| 52 | const char StatsReport::kStatsValueNameBytesSent[] = "bytesSent"; |
| 53 | const char StatsReport::kStatsValueNameChannelId[] = "googChannelId"; |
| 54 | const char StatsReport::kStatsValueNameCodecName[] = "googCodecName"; |
| 55 | const char StatsReport::kStatsValueNameComponent[] = "googComponent"; |
| 56 | const char StatsReport::kStatsValueNameContentName[] = "googContentName"; |
wu@webrtc.org | 4551b79 | 2013-10-09 15:37:36 +0000 | [diff] [blame] | 57 | const char StatsReport::kStatsValueNameDer[] = "googDerBase64"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 58 | // Echo metrics from the audio processing module. |
| 59 | const char StatsReport::kStatsValueNameEchoCancellationQualityMin[] = |
| 60 | "googEchoCancellationQualityMin"; |
| 61 | const char StatsReport::kStatsValueNameEchoDelayMedian[] = |
| 62 | "googEchoCancellationEchoDelayMedian"; |
| 63 | const char StatsReport::kStatsValueNameEchoDelayStdDev[] = |
| 64 | "googEchoCancellationEchoDelayStdDev"; |
| 65 | const char StatsReport::kStatsValueNameEchoReturnLoss[] = |
| 66 | "googEchoCancellationReturnLoss"; |
| 67 | const char StatsReport::kStatsValueNameEchoReturnLossEnhancement[] = |
| 68 | "googEchoCancellationReturnLossEnhancement"; |
| 69 | |
wu@webrtc.org | 4551b79 | 2013-10-09 15:37:36 +0000 | [diff] [blame] | 70 | const char StatsReport::kStatsValueNameFingerprint[] = "googFingerprint"; |
mallinath@webrtc.org | 19f27e6 | 2013-10-13 17:18:27 +0000 | [diff] [blame] | 71 | const char StatsReport::kStatsValueNameFingerprintAlgorithm[] = |
| 72 | "googFingerprintAlgorithm"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 73 | const char StatsReport::kStatsValueNameFirsReceived[] = "googFirsReceived"; |
| 74 | const char StatsReport::kStatsValueNameFirsSent[] = "googFirsSent"; |
| 75 | const char StatsReport::kStatsValueNameFrameHeightReceived[] = |
| 76 | "googFrameHeightReceived"; |
| 77 | const char StatsReport::kStatsValueNameFrameHeightSent[] = |
| 78 | "googFrameHeightSent"; |
| 79 | const char StatsReport::kStatsValueNameFrameRateReceived[] = |
| 80 | "googFrameRateReceived"; |
| 81 | const char StatsReport::kStatsValueNameFrameRateDecoded[] = |
| 82 | "googFrameRateDecoded"; |
| 83 | const char StatsReport::kStatsValueNameFrameRateOutput[] = |
| 84 | "googFrameRateOutput"; |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame^] | 85 | const char StatsReport::kStatsValueNameDecodeMs[] = "googDecodeMs"; |
| 86 | const char StatsReport::kStatsValueNameMaxDecodeMs[] = "googMaxDecodeMs"; |
| 87 | const char StatsReport::kStatsValueNameCurrentDelayMs[] = "googCurrentDelayMs"; |
| 88 | const char StatsReport::kStatsValueNameTargetDelayMs[] = "googTargetDelayMs"; |
| 89 | const char StatsReport::kStatsValueNameJitterBufferMs[] = "googJitterBufferMs"; |
| 90 | const char StatsReport::kStatsValueNameMinPlayoutDelayMs[] = |
| 91 | "googMinPlayoutDelayMs"; |
| 92 | const char StatsReport::kStatsValueNameRenderDelayMs[] = "googRenderDelayMs"; |
| 93 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 94 | const char StatsReport::kStatsValueNameFrameRateInput[] = "googFrameRateInput"; |
| 95 | const char StatsReport::kStatsValueNameFrameRateSent[] = "googFrameRateSent"; |
| 96 | const char StatsReport::kStatsValueNameFrameWidthReceived[] = |
| 97 | "googFrameWidthReceived"; |
| 98 | const char StatsReport::kStatsValueNameFrameWidthSent[] = "googFrameWidthSent"; |
| 99 | const char StatsReport::kStatsValueNameInitiator[] = "googInitiator"; |
wu@webrtc.org | 4551b79 | 2013-10-09 15:37:36 +0000 | [diff] [blame] | 100 | const char StatsReport::kStatsValueNameIssuerId[] = "googIssuerId"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 101 | const char StatsReport::kStatsValueNameJitterReceived[] = "googJitterReceived"; |
| 102 | const char StatsReport::kStatsValueNameLocalAddress[] = "googLocalAddress"; |
wu@webrtc.org | 4551b79 | 2013-10-09 15:37:36 +0000 | [diff] [blame] | 103 | const char StatsReport::kStatsValueNameLocalCertificateId[] = |
| 104 | "googLocalCertificateId"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 105 | const char StatsReport::kStatsValueNameNacksReceived[] = "googNacksReceived"; |
| 106 | const char StatsReport::kStatsValueNameNacksSent[] = "googNacksSent"; |
| 107 | const char StatsReport::kStatsValueNamePacketsReceived[] = "packetsReceived"; |
| 108 | const char StatsReport::kStatsValueNamePacketsSent[] = "packetsSent"; |
| 109 | const char StatsReport::kStatsValueNamePacketsLost[] = "packetsLost"; |
| 110 | const char StatsReport::kStatsValueNameReadable[] = "googReadable"; |
| 111 | const char StatsReport::kStatsValueNameRemoteAddress[] = "googRemoteAddress"; |
wu@webrtc.org | 4551b79 | 2013-10-09 15:37:36 +0000 | [diff] [blame] | 112 | const char StatsReport::kStatsValueNameRemoteCertificateId[] = |
| 113 | "googRemoteCertificateId"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 114 | const char StatsReport::kStatsValueNameRetransmitBitrate[] = |
| 115 | "googRetransmitBitrate"; |
| 116 | const char StatsReport::kStatsValueNameRtt[] = "googRtt"; |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 117 | const char StatsReport::kStatsValueNameSsrc[] = "ssrc"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 118 | const char StatsReport::kStatsValueNameTargetEncBitrate[] = |
| 119 | "googTargetEncBitrate"; |
| 120 | const char StatsReport::kStatsValueNameTransmitBitrate[] = |
| 121 | "googTransmitBitrate"; |
| 122 | const char StatsReport::kStatsValueNameTransportId[] = "transportId"; |
| 123 | const char StatsReport::kStatsValueNameTransportType[] = "googTransportType"; |
| 124 | const char StatsReport::kStatsValueNameTrackId[] = "googTrackId"; |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 125 | const char StatsReport::kStatsValueNameTypingNoiseState[] = |
| 126 | "googTypingNoiseState"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 127 | const char StatsReport::kStatsValueNameWritable[] = "googWritable"; |
| 128 | |
| 129 | const char StatsReport::kStatsReportTypeSession[] = "googLibjingleSession"; |
| 130 | const char StatsReport::kStatsReportTypeBwe[] = "VideoBwe"; |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame^] | 131 | const char StatsReport::kStatsReportTypeRemoteSsrc[] = "remoteSsrc"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 132 | const char StatsReport::kStatsReportTypeSsrc[] = "ssrc"; |
| 133 | const char StatsReport::kStatsReportTypeTrack[] = "googTrack"; |
| 134 | const char StatsReport::kStatsReportTypeIceCandidate[] = "iceCandidate"; |
| 135 | const char StatsReport::kStatsReportTypeTransport[] = "googTransport"; |
| 136 | const char StatsReport::kStatsReportTypeComponent[] = "googComponent"; |
| 137 | const char StatsReport::kStatsReportTypeCandidatePair[] = "googCandidatePair"; |
wu@webrtc.org | 4551b79 | 2013-10-09 15:37:36 +0000 | [diff] [blame] | 138 | const char StatsReport::kStatsReportTypeCertificate[] = "googCertificate"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 139 | |
| 140 | const char StatsReport::kStatsReportVideoBweId[] = "bweforvideo"; |
| 141 | |
wu@webrtc.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 142 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 143 | // Implementations of functions in statstypes.h |
| 144 | void 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 | |
| 151 | void StatsReport::AddValue(const std::string& name, int64 value) { |
| 152 | AddValue(name, talk_base::ToString<int64>(value)); |
| 153 | } |
| 154 | |
| 155 | void StatsReport::AddBoolean(const std::string& name, bool value) { |
| 156 | AddValue(name, value ? "true" : "false"); |
| 157 | } |
| 158 | |
| 159 | namespace { |
| 160 | typedef std::map<std::string, StatsReport> StatsMap; |
| 161 | |
| 162 | std::string StatsId(const std::string& type, const std::string& id) { |
| 163 | return type + "_" + id; |
| 164 | } |
| 165 | |
| 166 | bool 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 | |
| 180 | template <class TrackVector> |
| 181 | void 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 | |
| 194 | void 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 | |
| 207 | void 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.org | 967bfff | 2013-09-19 05:49:50 +0000 | [diff] [blame] | 228 | report->AddBoolean(StatsReport::kStatsValueNameTypingNoiseState, |
| 229 | info.typing_noise_detected); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | void 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.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame^] | 254 | |
| 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.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | void 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 | |
| 293 | void 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.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame^] | 321 | void 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 | |
| 327 | void 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.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 333 | uint32 ExtractSsrc(const cricket::VoiceReceiverInfo& info) { |
| 334 | return info.ssrc; |
| 335 | } |
| 336 | |
| 337 | uint32 ExtractSsrc(const cricket::VoiceSenderInfo& info) { |
| 338 | return info.ssrc; |
| 339 | } |
| 340 | |
| 341 | uint32 ExtractSsrc(const cricket::VideoReceiverInfo& info) { |
| 342 | return info.ssrcs[0]; |
| 343 | } |
| 344 | |
| 345 | uint32 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. |
| 351 | template<typename T> |
| 352 | void 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.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame^] | 359 | // 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.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 362 | if (!report) { |
| 363 | continue; |
| 364 | } |
| 365 | ExtractStats(*it, report); |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame^] | 366 | 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.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 373 | } |
| 374 | }; |
| 375 | |
| 376 | } // namespace |
| 377 | |
| 378 | StatsCollector::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. |
| 384 | void 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 | |
| 393 | bool 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 | |
| 438 | void 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.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame^] | 455 | StatsReport* StatsCollector::PrepareLocalReport( |
| 456 | uint32 ssrc, |
| 457 | const std::string& transport_id) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 458 | 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.org | a59696b | 2013-09-13 23:48:58 +0000 | [diff] [blame] | 465 | LOG(LS_WARNING) << "The SSRC " << ssrc |
| 466 | << " is not associated with a track"; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 467 | 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.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame^] | 477 | StatsReport* report = GetOrCreateReport(StatsReport::kStatsReportTypeSsrc, |
| 478 | ssrc_id); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 479 | |
| 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.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame^] | 494 | StatsReport* 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.org | 4551b79 | 2013-10-09 15:37:36 +0000 | [diff] [blame] | 532 | std::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.org | 19f27e6 | 2013-10-13 17:18:27 +0000 | [diff] [blame] | 537 | |
| 538 | std::string digest_algorithm; |
| 539 | if (!cert->GetSignatureDigestAlgorithm(&digest_algorithm)) |
| 540 | return std::string(); |
| 541 | |
wu@webrtc.org | 4551b79 | 2013-10-09 15:37:36 +0000 | [diff] [blame] | 542 | talk_base::scoped_ptr<talk_base::SSLFingerprint> ssl_fingerprint( |
mallinath@webrtc.org | 19f27e6 | 2013-10-13 17:18:27 +0000 | [diff] [blame] | 543 | talk_base::SSLFingerprint::Create(digest_algorithm, cert)); |
wu@webrtc.org | 4551b79 | 2013-10-09 15:37:36 +0000 | [diff] [blame] | 544 | 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.org | 19f27e6 | 2013-10-13 17:18:27 +0000 | [diff] [blame] | 557 | report.AddValue(StatsReport::kStatsValueNameFingerprintAlgorithm, |
| 558 | digest_algorithm); |
wu@webrtc.org | 4551b79 | 2013-10-09 15:37:36 +0000 | [diff] [blame] | 559 | 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 | |
| 566 | std::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.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 591 | void 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.org | 4551b79 | 2013-10-09 15:37:36 +0000 | [diff] [blame] | 611 | // 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.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 627 | 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.org | 4551b79 | 2013-10-09 15:37:36 +0000 | [diff] [blame] | 640 | 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.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 648 | 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.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame^] | 678 | report.AddValue(StatsReport::kStatsValueNameRtt, info.rtt); |
| 679 | report.AddValue(StatsReport::kStatsValueNameTransportType, |
| 680 | info.local_candidate.protocol()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 681 | reports_[report.id] = report; |
| 682 | } |
| 683 | } |
| 684 | } |
| 685 | } |
| 686 | } |
| 687 | |
| 688 | void 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 | |
| 708 | void 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 | |
| 735 | double StatsCollector::GetTimeNow() { |
| 736 | return timing_.WallTimeNow() * talk_base::kNumMillisecsPerSec; |
| 737 | } |
| 738 | |
| 739 | bool 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.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame^] | 757 | StatsReport* 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.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 772 | } // namespace webrtc |