henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * libjingle |
jlmiller@webrtc.org | 5f93d0a | 2015-01-20 21:36:13 +0000 | [diff] [blame] | 3 | * Copyright 2012 Google Inc. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 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.org | a09a999 | 2014-08-13 17:26:08 +0000 | [diff] [blame] | 33 | #include "talk/session/media/channel.h" |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 34 | #include "webrtc/base/base64.h" |
| 35 | #include "webrtc/base/scoped_ptr.h" |
| 36 | #include "webrtc/base/timing.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 37 | |
tommi@webrtc.org | 4fb7e25 | 2015-01-21 11:36:18 +0000 | [diff] [blame] | 38 | using rtc::scoped_ptr; |
| 39 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 40 | namespace webrtc { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 41 | namespace { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 42 | |
guoweis@webrtc.org | 950c518 | 2014-12-16 23:01:31 +0000 | [diff] [blame] | 43 | // The following is the enum RTCStatsIceCandidateType from |
| 44 | // http://w3c.github.io/webrtc-stats/#rtcstatsicecandidatetype-enum such that |
| 45 | // our stats report for ice candidate type could conform to that. |
| 46 | const char STATSREPORT_LOCAL_PORT_TYPE[] = "host"; |
| 47 | const char STATSREPORT_STUN_PORT_TYPE[] = "serverreflexive"; |
| 48 | const char STATSREPORT_PRFLX_PORT_TYPE[] = "peerreflexive"; |
| 49 | const char STATSREPORT_RELAY_PORT_TYPE[] = "relayed"; |
| 50 | |
| 51 | // Strings used by the stats collector to report adapter types. This fits the |
| 52 | // general stype of http://w3c.github.io/webrtc-stats than what |
| 53 | // AdapterTypeToString does. |
| 54 | const char* STATSREPORT_ADAPTER_TYPE_ETHERNET = "lan"; |
| 55 | const char* STATSREPORT_ADAPTER_TYPE_WIFI = "wlan"; |
| 56 | const char* STATSREPORT_ADAPTER_TYPE_WWAN = "wwan"; |
| 57 | const char* STATSREPORT_ADAPTER_TYPE_VPN = "vpn"; |
phoglund@webrtc.org | 006521d | 2015-02-12 09:23:59 +0000 | [diff] [blame] | 58 | const char* STATSREPORT_ADAPTER_TYPE_LOOPBACK = "loopback"; |
guoweis@webrtc.org | 950c518 | 2014-12-16 23:01:31 +0000 | [diff] [blame] | 59 | |
tommi@webrtc.org | d390029 | 2015-03-12 16:35:55 +0000 | [diff] [blame^] | 60 | template<typename ValueType> |
| 61 | struct TypeForAdd { |
tommi@webrtc.org | 92f4018 | 2015-03-04 15:25:19 +0000 | [diff] [blame] | 62 | const StatsReport::StatsValueName name; |
tommi@webrtc.org | d390029 | 2015-03-12 16:35:55 +0000 | [diff] [blame^] | 63 | const ValueType& value; |
tommi@webrtc.org | 92f4018 | 2015-03-04 15:25:19 +0000 | [diff] [blame] | 64 | }; |
| 65 | |
tommi@webrtc.org | d390029 | 2015-03-12 16:35:55 +0000 | [diff] [blame^] | 66 | typedef TypeForAdd<bool> BoolForAdd; |
| 67 | typedef TypeForAdd<float> FloatForAdd; |
| 68 | typedef TypeForAdd<int64> Int64ForAdd; |
| 69 | typedef TypeForAdd<int> IntForAdd; |
tommi@webrtc.org | 92f4018 | 2015-03-04 15:25:19 +0000 | [diff] [blame] | 70 | |
tommi@webrtc.org | d390029 | 2015-03-12 16:35:55 +0000 | [diff] [blame^] | 71 | StatsReport::Id GetTransportIdFromProxy(const cricket::ProxyTransportMap& map, |
| 72 | const std::string& proxy) { |
| 73 | DCHECK(!proxy.empty()); |
tommi@webrtc.org | 4721895 | 2014-07-15 19:22:37 +0000 | [diff] [blame] | 74 | cricket::ProxyTransportMap::const_iterator found = map.find(proxy); |
tommi@webrtc.org | d390029 | 2015-03-12 16:35:55 +0000 | [diff] [blame^] | 75 | if (found == map.end()) |
| 76 | return StatsReport::Id(); |
tommi@webrtc.org | 4721895 | 2014-07-15 19:22:37 +0000 | [diff] [blame] | 77 | |
tommi@webrtc.org | d390029 | 2015-03-12 16:35:55 +0000 | [diff] [blame^] | 78 | return StatsReport::NewComponentId( |
| 79 | found->second, cricket::ICE_CANDIDATE_COMPONENT_RTP); |
tommi@webrtc.org | 4721895 | 2014-07-15 19:22:37 +0000 | [diff] [blame] | 80 | } |
| 81 | |
tommi@webrtc.org | 4fb7e25 | 2015-01-21 11:36:18 +0000 | [diff] [blame] | 82 | void AddTrackReport(StatsCollection* reports, const std::string& track_id) { |
xians@webrtc.org | 01bda20 | 2014-07-09 07:38:38 +0000 | [diff] [blame] | 83 | // Adds an empty track report. |
tommi@webrtc.org | d390029 | 2015-03-12 16:35:55 +0000 | [diff] [blame^] | 84 | StatsReport::Id id( |
tommi@webrtc.org | 4fb7e25 | 2015-01-21 11:36:18 +0000 | [diff] [blame] | 85 | StatsReport::NewTypedId(StatsReport::kStatsReportTypeTrack, track_id)); |
tommi@webrtc.org | d390029 | 2015-03-12 16:35:55 +0000 | [diff] [blame^] | 86 | StatsReport* report = reports->ReplaceOrAddNew(id); |
tommi@webrtc.org | 92f4018 | 2015-03-04 15:25:19 +0000 | [diff] [blame] | 87 | report->AddString(StatsReport::kStatsValueNameTrackId, track_id); |
xians@webrtc.org | 01bda20 | 2014-07-09 07:38:38 +0000 | [diff] [blame] | 88 | } |
| 89 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 90 | template <class TrackVector> |
tommi@webrtc.org | 4fb7e25 | 2015-01-21 11:36:18 +0000 | [diff] [blame] | 91 | void CreateTrackReports(const TrackVector& tracks, StatsCollection* reports) { |
tommi@webrtc.org | d390029 | 2015-03-12 16:35:55 +0000 | [diff] [blame^] | 92 | for (const auto& track : tracks) |
xians@webrtc.org | 01bda20 | 2014-07-09 07:38:38 +0000 | [diff] [blame] | 93 | AddTrackReport(reports, track->id()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 94 | } |
| 95 | |
tommi@webrtc.org | 92f4018 | 2015-03-04 15:25:19 +0000 | [diff] [blame] | 96 | void ExtractCommonSendProperties(const cricket::MediaSenderInfo& info, |
| 97 | StatsReport* report) { |
| 98 | report->AddString(StatsReport::kStatsValueNameCodecName, info.codec_name); |
| 99 | report->AddInt64(StatsReport::kStatsValueNameBytesSent, info.bytes_sent); |
| 100 | report->AddInt64(StatsReport::kStatsValueNameRtt, info.rtt_ms); |
| 101 | } |
| 102 | |
| 103 | void SetAudioProcessingStats(StatsReport* report, int signal_level, |
| 104 | bool typing_noise_detected, int echo_return_loss, |
| 105 | int echo_return_loss_enhancement, int echo_delay_median_ms, |
| 106 | float aec_quality_min, int echo_delay_std_ms) { |
| 107 | report->AddBoolean(StatsReport::kStatsValueNameTypingNoiseState, |
| 108 | typing_noise_detected); |
| 109 | report->AddFloat(StatsReport::kStatsValueNameEchoCancellationQualityMin, |
| 110 | aec_quality_min); |
| 111 | // Don't overwrite the previous signal level if it's not available now. |
| 112 | if (signal_level >= 0) |
| 113 | report->AddInt(StatsReport::kStatsValueNameAudioInputLevel, signal_level); |
| 114 | const IntForAdd ints[] = { |
| 115 | { StatsReport::kStatsValueNameEchoReturnLoss, echo_return_loss }, |
| 116 | { StatsReport::kStatsValueNameEchoReturnLossEnhancement, |
| 117 | echo_return_loss_enhancement }, |
| 118 | { StatsReport::kStatsValueNameEchoDelayMedian, echo_delay_median_ms }, |
| 119 | { StatsReport::kStatsValueNameEchoDelayStdDev, echo_delay_std_ms }, |
| 120 | }; |
| 121 | for (const auto& i : ints) |
| 122 | report->AddInt(i.name, i.value); |
| 123 | } |
| 124 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 125 | void ExtractStats(const cricket::VoiceReceiverInfo& info, StatsReport* report) { |
tommi@webrtc.org | 92f4018 | 2015-03-04 15:25:19 +0000 | [diff] [blame] | 126 | const FloatForAdd floats[] = { |
| 127 | { StatsReport::kStatsValueNameExpandRate, info.expand_rate }, |
| 128 | { StatsReport::kStatsValueNameSecondaryDecodedRate, |
| 129 | info.secondary_decoded_rate }, |
| 130 | { StatsReport::kStatsValueNameSpeechExpandRate, info.speech_expand_rate }, |
| 131 | }; |
| 132 | |
| 133 | const IntForAdd ints[] = { |
| 134 | { StatsReport::kStatsValueNameAudioOutputLevel, info.audio_level }, |
| 135 | { StatsReport::kStatsValueNameCurrentDelayMs, info.delay_estimate_ms }, |
| 136 | { StatsReport::kStatsValueNameDecodingCNG, info.decoding_cng }, |
| 137 | { StatsReport::kStatsValueNameDecodingCTN, info.decoding_calls_to_neteq }, |
| 138 | { StatsReport::kStatsValueNameDecodingCTSG, |
| 139 | info.decoding_calls_to_silence_generator }, |
| 140 | { StatsReport::kStatsValueNameDecodingNormal, info.decoding_normal }, |
| 141 | { StatsReport::kStatsValueNameDecodingPLC, info.decoding_plc }, |
| 142 | { StatsReport::kStatsValueNameDecodingPLCCNG, info.decoding_plc_cng }, |
| 143 | { StatsReport::kStatsValueNameJitterBufferMs, info.jitter_buffer_ms }, |
| 144 | { StatsReport::kStatsValueNameJitterReceived, info.jitter_ms }, |
| 145 | { StatsReport::kStatsValueNamePacketsLost, info.packets_lost }, |
| 146 | { StatsReport::kStatsValueNamePacketsReceived, info.packets_rcvd }, |
| 147 | { StatsReport::kStatsValueNamePreferredJitterBufferMs, |
| 148 | info.jitter_buffer_preferred_ms }, |
| 149 | }; |
| 150 | |
| 151 | for (const auto& f : floats) |
| 152 | report->AddFloat(f.name, f.value); |
| 153 | |
| 154 | for (const auto& i : ints) |
| 155 | report->AddInt(i.name, i.value); |
| 156 | |
| 157 | report->AddInt64(StatsReport::kStatsValueNameBytesReceived, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 158 | info.bytes_rcvd); |
tommi@webrtc.org | 92f4018 | 2015-03-04 15:25:19 +0000 | [diff] [blame] | 159 | report->AddInt64(StatsReport::kStatsValueNameCaptureStartNtpTimeMs, |
buildbot@webrtc.org | b525a9d | 2014-06-03 09:42:15 +0000 | [diff] [blame] | 160 | info.capture_start_ntp_time_ms); |
tommi@webrtc.org | 92f4018 | 2015-03-04 15:25:19 +0000 | [diff] [blame] | 161 | |
| 162 | report->AddString(StatsReport::kStatsValueNameCodecName, info.codec_name); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | void ExtractStats(const cricket::VoiceSenderInfo& info, StatsReport* report) { |
tommi@webrtc.org | 92f4018 | 2015-03-04 15:25:19 +0000 | [diff] [blame] | 166 | ExtractCommonSendProperties(info, report); |
| 167 | |
| 168 | SetAudioProcessingStats(report, info.audio_level, info.typing_noise_detected, |
| 169 | info.echo_return_loss, info.echo_return_loss_enhancement, |
| 170 | info.echo_delay_median_ms, info.aec_quality_min, info.echo_delay_std_ms); |
| 171 | |
| 172 | const IntForAdd ints[] = { |
| 173 | { StatsReport::kStatsValueNameJitterReceived, info.jitter_ms }, |
| 174 | { StatsReport::kStatsValueNamePacketsLost, info.packets_lost }, |
| 175 | { StatsReport::kStatsValueNamePacketsSent, info.packets_sent }, |
| 176 | }; |
| 177 | |
| 178 | for (const auto& i : ints) |
| 179 | report->AddInt(i.name, i.value); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | void ExtractStats(const cricket::VideoReceiverInfo& info, StatsReport* report) { |
tommi@webrtc.org | 92f4018 | 2015-03-04 15:25:19 +0000 | [diff] [blame] | 183 | report->AddInt64(StatsReport::kStatsValueNameBytesReceived, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 184 | info.bytes_rcvd); |
tommi@webrtc.org | 92f4018 | 2015-03-04 15:25:19 +0000 | [diff] [blame] | 185 | report->AddInt64(StatsReport::kStatsValueNameCaptureStartNtpTimeMs, |
buildbot@webrtc.org | 0581f0b | 2014-05-06 21:36:31 +0000 | [diff] [blame] | 186 | info.capture_start_ntp_time_ms); |
tommi@webrtc.org | 92f4018 | 2015-03-04 15:25:19 +0000 | [diff] [blame] | 187 | const IntForAdd ints[] = { |
| 188 | { StatsReport::kStatsValueNameCurrentDelayMs, info.current_delay_ms }, |
| 189 | { StatsReport::kStatsValueNameDecodeMs, info.decode_ms }, |
| 190 | { StatsReport::kStatsValueNameFirsSent, info.firs_sent }, |
| 191 | { StatsReport::kStatsValueNameFrameHeightReceived, info.frame_height }, |
| 192 | { StatsReport::kStatsValueNameFrameRateDecoded, info.framerate_decoded }, |
| 193 | { StatsReport::kStatsValueNameFrameRateOutput, info.framerate_output }, |
| 194 | { StatsReport::kStatsValueNameFrameRateReceived, info.framerate_rcvd }, |
| 195 | { StatsReport::kStatsValueNameFrameWidthReceived, info.frame_width }, |
| 196 | { StatsReport::kStatsValueNameJitterBufferMs, info.jitter_buffer_ms }, |
| 197 | { StatsReport::kStatsValueNameMaxDecodeMs, info.max_decode_ms }, |
| 198 | { StatsReport::kStatsValueNameMinPlayoutDelayMs, |
| 199 | info.min_playout_delay_ms }, |
| 200 | { StatsReport::kStatsValueNameNacksSent, info.nacks_sent }, |
| 201 | { StatsReport::kStatsValueNamePacketsLost, info.packets_lost }, |
| 202 | { StatsReport::kStatsValueNamePacketsReceived, info.packets_rcvd }, |
| 203 | { StatsReport::kStatsValueNamePlisSent, info.plis_sent }, |
| 204 | { StatsReport::kStatsValueNameRenderDelayMs, info.render_delay_ms }, |
| 205 | { StatsReport::kStatsValueNameTargetDelayMs, info.target_delay_ms }, |
| 206 | }; |
| 207 | |
| 208 | for (const auto& i : ints) |
| 209 | report->AddInt(i.name, i.value); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | void ExtractStats(const cricket::VideoSenderInfo& info, StatsReport* report) { |
tommi@webrtc.org | 92f4018 | 2015-03-04 15:25:19 +0000 | [diff] [blame] | 213 | ExtractCommonSendProperties(info, report); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 214 | |
mallinath@webrtc.org | 62451dc | 2013-12-13 12:29:34 +0000 | [diff] [blame] | 215 | report->AddBoolean(StatsReport::kStatsValueNameBandwidthLimitedResolution, |
| 216 | (info.adapt_reason & 0x2) > 0); |
tommi@webrtc.org | 92f4018 | 2015-03-04 15:25:19 +0000 | [diff] [blame] | 217 | report->AddBoolean(StatsReport::kStatsValueNameCpuLimitedResolution, |
| 218 | (info.adapt_reason & 0x1) > 0); |
mallinath@webrtc.org | 62451dc | 2013-12-13 12:29:34 +0000 | [diff] [blame] | 219 | report->AddBoolean(StatsReport::kStatsValueNameViewLimitedResolution, |
| 220 | (info.adapt_reason & 0x4) > 0); |
tommi@webrtc.org | 92f4018 | 2015-03-04 15:25:19 +0000 | [diff] [blame] | 221 | |
| 222 | const IntForAdd ints[] = { |
| 223 | { StatsReport::kStatsValueNameAdaptationChanges, info.adapt_changes }, |
| 224 | { StatsReport::kStatsValueNameAvgEncodeMs, info.avg_encode_ms }, |
| 225 | { StatsReport::kStatsValueNameCaptureJitterMs, info.capture_jitter_ms }, |
| 226 | { StatsReport::kStatsValueNameCaptureQueueDelayMsPerS, |
| 227 | info.capture_queue_delay_ms_per_s }, |
| 228 | { StatsReport::kStatsValueNameEncodeUsagePercent, |
| 229 | info.encode_usage_percent }, |
| 230 | { StatsReport::kStatsValueNameFirsReceived, info.firs_rcvd }, |
| 231 | { StatsReport::kStatsValueNameFrameHeightInput, info.input_frame_height }, |
| 232 | { StatsReport::kStatsValueNameFrameHeightSent, info.send_frame_height }, |
| 233 | { StatsReport::kStatsValueNameFrameRateInput, info.framerate_input }, |
| 234 | { StatsReport::kStatsValueNameFrameRateSent, info.framerate_sent }, |
| 235 | { StatsReport::kStatsValueNameFrameWidthInput, info.input_frame_width }, |
| 236 | { StatsReport::kStatsValueNameFrameWidthSent, info.send_frame_width }, |
| 237 | { StatsReport::kStatsValueNameNacksReceived, info.nacks_rcvd }, |
| 238 | { StatsReport::kStatsValueNamePacketsLost, info.packets_lost }, |
| 239 | { StatsReport::kStatsValueNamePacketsSent, info.packets_sent }, |
| 240 | { StatsReport::kStatsValueNamePlisReceived, info.plis_rcvd }, |
| 241 | }; |
| 242 | |
| 243 | for (const auto& i : ints) |
| 244 | report->AddInt(i.name, i.value); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | void ExtractStats(const cricket::BandwidthEstimationInfo& info, |
| 248 | double stats_gathering_started, |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 249 | PeerConnectionInterface::StatsOutputLevel level, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 250 | StatsReport* report) { |
tommi@webrtc.org | 4fb7e25 | 2015-01-21 11:36:18 +0000 | [diff] [blame] | 251 | ASSERT(report->type() == StatsReport::kStatsReportTypeBwe); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 252 | |
tommi@webrtc.org | d390029 | 2015-03-12 16:35:55 +0000 | [diff] [blame^] | 253 | report->set_timestamp(stats_gathering_started); |
| 254 | const IntForAdd ints[] = { |
| 255 | { StatsReport::kStatsValueNameAvailableSendBandwidth, |
| 256 | info.available_send_bandwidth }, |
| 257 | { StatsReport::kStatsValueNameAvailableReceiveBandwidth, |
| 258 | info.available_recv_bandwidth }, |
| 259 | { StatsReport::kStatsValueNameTargetEncBitrate, info.target_enc_bitrate }, |
| 260 | { StatsReport::kStatsValueNameActualEncBitrate, info.actual_enc_bitrate }, |
| 261 | { StatsReport::kStatsValueNameRetransmitBitrate, info.retransmit_bitrate }, |
| 262 | { StatsReport::kStatsValueNameTransmitBitrate, info.transmit_bitrate }, |
| 263 | }; |
| 264 | for (const auto& i : ints) |
| 265 | report->AddInt(i.name, i.value); |
| 266 | report->AddInt64(StatsReport::kStatsValueNameBucketDelay, info.bucket_delay); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 267 | } |
| 268 | |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame] | 269 | void ExtractRemoteStats(const cricket::MediaSenderInfo& info, |
| 270 | StatsReport* report) { |
tommi@webrtc.org | 8e327c4 | 2015-01-19 20:41:26 +0000 | [diff] [blame] | 271 | report->set_timestamp(info.remote_stats[0].timestamp); |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame] | 272 | // TODO(hta): Extract some stats here. |
| 273 | } |
| 274 | |
| 275 | void ExtractRemoteStats(const cricket::MediaReceiverInfo& info, |
| 276 | StatsReport* report) { |
tommi@webrtc.org | 8e327c4 | 2015-01-19 20:41:26 +0000 | [diff] [blame] | 277 | report->set_timestamp(info.remote_stats[0].timestamp); |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame] | 278 | // TODO(hta): Extract some stats here. |
| 279 | } |
| 280 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 281 | // Template to extract stats from a data vector. |
sergeyu@chromium.org | 5bc25c4 | 2013-12-05 00:24:06 +0000 | [diff] [blame] | 282 | // In order to use the template, the functions that are called from it, |
| 283 | // ExtractStats and ExtractRemoteStats, must be defined and overloaded |
| 284 | // for each type. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 285 | template<typename T> |
| 286 | void ExtractStatsFromList(const std::vector<T>& data, |
tommi@webrtc.org | d390029 | 2015-03-12 16:35:55 +0000 | [diff] [blame^] | 287 | const StatsReport::Id& transport_id, |
xians@webrtc.org | 4cb0128 | 2014-06-12 14:57:05 +0000 | [diff] [blame] | 288 | StatsCollector* collector, |
tommi@webrtc.org | 4fb7e25 | 2015-01-21 11:36:18 +0000 | [diff] [blame] | 289 | StatsReport::Direction direction) { |
| 290 | for (const auto& d : data) { |
| 291 | uint32 ssrc = d.ssrc(); |
xians@webrtc.org | 4cb0128 | 2014-06-12 14:57:05 +0000 | [diff] [blame] | 292 | // Each track can have stats for both local and remote objects. |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame] | 293 | // TODO(hta): Handle the case of multiple SSRCs per object. |
tommi@webrtc.org | 4fb7e25 | 2015-01-21 11:36:18 +0000 | [diff] [blame] | 294 | StatsReport* report = collector->PrepareReport(true, ssrc, transport_id, |
| 295 | direction); |
xians@webrtc.org | 4cb0128 | 2014-06-12 14:57:05 +0000 | [diff] [blame] | 296 | if (report) |
tommi@webrtc.org | 4fb7e25 | 2015-01-21 11:36:18 +0000 | [diff] [blame] | 297 | ExtractStats(d, report); |
xians@webrtc.org | 4cb0128 | 2014-06-12 14:57:05 +0000 | [diff] [blame] | 298 | |
tommi@webrtc.org | 4fb7e25 | 2015-01-21 11:36:18 +0000 | [diff] [blame] | 299 | if (!d.remote_stats.empty()) { |
| 300 | report = collector->PrepareReport(false, ssrc, transport_id, direction); |
| 301 | if (report) |
| 302 | ExtractRemoteStats(d, report); |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame] | 303 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 304 | } |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 305 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 306 | |
| 307 | } // namespace |
| 308 | |
guoweis@webrtc.org | 950c518 | 2014-12-16 23:01:31 +0000 | [diff] [blame] | 309 | const char* IceCandidateTypeToStatsType(const std::string& candidate_type) { |
| 310 | if (candidate_type == cricket::LOCAL_PORT_TYPE) { |
| 311 | return STATSREPORT_LOCAL_PORT_TYPE; |
| 312 | } |
| 313 | if (candidate_type == cricket::STUN_PORT_TYPE) { |
| 314 | return STATSREPORT_STUN_PORT_TYPE; |
| 315 | } |
| 316 | if (candidate_type == cricket::PRFLX_PORT_TYPE) { |
| 317 | return STATSREPORT_PRFLX_PORT_TYPE; |
| 318 | } |
| 319 | if (candidate_type == cricket::RELAY_PORT_TYPE) { |
| 320 | return STATSREPORT_RELAY_PORT_TYPE; |
| 321 | } |
| 322 | ASSERT(false); |
| 323 | return "unknown"; |
| 324 | } |
| 325 | |
| 326 | const char* AdapterTypeToStatsType(rtc::AdapterType type) { |
| 327 | switch (type) { |
| 328 | case rtc::ADAPTER_TYPE_UNKNOWN: |
| 329 | return "unknown"; |
| 330 | case rtc::ADAPTER_TYPE_ETHERNET: |
| 331 | return STATSREPORT_ADAPTER_TYPE_ETHERNET; |
| 332 | case rtc::ADAPTER_TYPE_WIFI: |
| 333 | return STATSREPORT_ADAPTER_TYPE_WIFI; |
| 334 | case rtc::ADAPTER_TYPE_CELLULAR: |
| 335 | return STATSREPORT_ADAPTER_TYPE_WWAN; |
| 336 | case rtc::ADAPTER_TYPE_VPN: |
| 337 | return STATSREPORT_ADAPTER_TYPE_VPN; |
phoglund@webrtc.org | 006521d | 2015-02-12 09:23:59 +0000 | [diff] [blame] | 338 | case rtc::ADAPTER_TYPE_LOOPBACK: |
| 339 | return STATSREPORT_ADAPTER_TYPE_LOOPBACK; |
guoweis@webrtc.org | 950c518 | 2014-12-16 23:01:31 +0000 | [diff] [blame] | 340 | default: |
| 341 | ASSERT(false); |
| 342 | return ""; |
| 343 | } |
| 344 | } |
| 345 | |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 346 | StatsCollector::StatsCollector(WebRtcSession* session) |
decurtis@webrtc.org | 487a444 | 2015-01-15 22:55:07 +0000 | [diff] [blame] | 347 | : session_(session), |
| 348 | stats_gathering_started_(0) { |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 349 | ASSERT(session_); |
| 350 | } |
| 351 | |
| 352 | StatsCollector::~StatsCollector() { |
tommi@webrtc.org | 69bc5a3 | 2014-12-15 09:44:48 +0000 | [diff] [blame] | 353 | ASSERT(session_->signaling_thread()->IsCurrent()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 354 | } |
| 355 | |
decurtis@webrtc.org | 322a564 | 2015-02-03 22:09:37 +0000 | [diff] [blame] | 356 | double StatsCollector::GetTimeNow() { |
| 357 | return rtc::Timing::WallTimeNow() * rtc::kNumMillisecsPerSec; |
| 358 | } |
| 359 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 360 | // Adds a MediaStream with tracks that can be used as a |selector| in a call |
| 361 | // to GetStats. |
| 362 | void StatsCollector::AddStream(MediaStreamInterface* stream) { |
tommi@webrtc.org | 69bc5a3 | 2014-12-15 09:44:48 +0000 | [diff] [blame] | 363 | ASSERT(session_->signaling_thread()->IsCurrent()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 364 | ASSERT(stream != NULL); |
| 365 | |
| 366 | CreateTrackReports<AudioTrackVector>(stream->GetAudioTracks(), |
| 367 | &reports_); |
| 368 | CreateTrackReports<VideoTrackVector>(stream->GetVideoTracks(), |
| 369 | &reports_); |
| 370 | } |
| 371 | |
henrike@webrtc.org | 40b3b68 | 2014-03-03 18:30:11 +0000 | [diff] [blame] | 372 | void StatsCollector::AddLocalAudioTrack(AudioTrackInterface* audio_track, |
| 373 | uint32 ssrc) { |
tommi@webrtc.org | 69bc5a3 | 2014-12-15 09:44:48 +0000 | [diff] [blame] | 374 | ASSERT(session_->signaling_thread()->IsCurrent()); |
henrike@webrtc.org | 40b3b68 | 2014-03-03 18:30:11 +0000 | [diff] [blame] | 375 | ASSERT(audio_track != NULL); |
tommi@webrtc.org | d390029 | 2015-03-12 16:35:55 +0000 | [diff] [blame^] | 376 | #if ENABLE_DEBUG |
| 377 | for (const auto& track : local_audio_tracks_) |
| 378 | ASSERT(track.first != audio_track || track.second != ssrc); |
| 379 | #endif |
xians@webrtc.org | 01bda20 | 2014-07-09 07:38:38 +0000 | [diff] [blame] | 380 | |
henrike@webrtc.org | 40b3b68 | 2014-03-03 18:30:11 +0000 | [diff] [blame] | 381 | local_audio_tracks_.push_back(std::make_pair(audio_track, ssrc)); |
xians@webrtc.org | 01bda20 | 2014-07-09 07:38:38 +0000 | [diff] [blame] | 382 | |
| 383 | // Create the kStatsReportTypeTrack report for the new track if there is no |
| 384 | // report yet. |
tommi@webrtc.org | d390029 | 2015-03-12 16:35:55 +0000 | [diff] [blame^] | 385 | StatsReport::Id id(StatsReport::NewTypedId(StatsReport::kStatsReportTypeTrack, |
| 386 | audio_track->id())); |
| 387 | StatsReport* report = reports_.Find(id); |
tommi@webrtc.org | 4fb7e25 | 2015-01-21 11:36:18 +0000 | [diff] [blame] | 388 | if (!report) { |
tommi@webrtc.org | d390029 | 2015-03-12 16:35:55 +0000 | [diff] [blame^] | 389 | report = reports_.InsertNew(id); |
tommi@webrtc.org | 92f4018 | 2015-03-04 15:25:19 +0000 | [diff] [blame] | 390 | report->AddString(StatsReport::kStatsValueNameTrackId, audio_track->id()); |
tommi@webrtc.org | 4fb7e25 | 2015-01-21 11:36:18 +0000 | [diff] [blame] | 391 | } |
henrike@webrtc.org | 40b3b68 | 2014-03-03 18:30:11 +0000 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | void StatsCollector::RemoveLocalAudioTrack(AudioTrackInterface* audio_track, |
| 395 | uint32 ssrc) { |
| 396 | ASSERT(audio_track != NULL); |
tommi@webrtc.org | d390029 | 2015-03-12 16:35:55 +0000 | [diff] [blame^] | 397 | local_audio_tracks_.erase(std::remove_if(local_audio_tracks_.begin(), |
| 398 | local_audio_tracks_.end(), |
| 399 | [audio_track, ssrc](const LocalAudioTrackVector::value_type& track) { |
| 400 | return track.first == audio_track && track.second == ssrc; |
| 401 | })); |
henrike@webrtc.org | 40b3b68 | 2014-03-03 18:30:11 +0000 | [diff] [blame] | 402 | } |
| 403 | |
tommi@webrtc.org | 5b06b06 | 2014-08-15 08:38:30 +0000 | [diff] [blame] | 404 | void StatsCollector::GetStats(MediaStreamTrackInterface* track, |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 405 | StatsReports* reports) { |
tommi@webrtc.org | 69bc5a3 | 2014-12-15 09:44:48 +0000 | [diff] [blame] | 406 | ASSERT(session_->signaling_thread()->IsCurrent()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 407 | ASSERT(reports != NULL); |
tommi@webrtc.org | 5b06b06 | 2014-08-15 08:38:30 +0000 | [diff] [blame] | 408 | ASSERT(reports->empty()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 409 | |
tommi@webrtc.org | d390029 | 2015-03-12 16:35:55 +0000 | [diff] [blame^] | 410 | rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls; |
| 411 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 412 | if (!track) { |
tommi@webrtc.org | 4fb7e25 | 2015-01-21 11:36:18 +0000 | [diff] [blame] | 413 | reports->reserve(reports_.size()); |
| 414 | for (auto* r : reports_) |
| 415 | reports->push_back(r); |
tommi@webrtc.org | 5b06b06 | 2014-08-15 08:38:30 +0000 | [diff] [blame] | 416 | return; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 417 | } |
| 418 | |
tommi@webrtc.org | 4fb7e25 | 2015-01-21 11:36:18 +0000 | [diff] [blame] | 419 | StatsReport* report = reports_.Find(StatsReport::NewTypedId( |
| 420 | StatsReport::kStatsReportTypeSession, session_->id())); |
tommi@webrtc.org | 5b06b06 | 2014-08-15 08:38:30 +0000 | [diff] [blame] | 421 | if (report) |
| 422 | reports->push_back(report); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 423 | |
tommi@webrtc.org | 4fb7e25 | 2015-01-21 11:36:18 +0000 | [diff] [blame] | 424 | report = reports_.Find(StatsReport::NewTypedId( |
| 425 | StatsReport::kStatsReportTypeTrack, track->id())); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 426 | |
tommi@webrtc.org | 5b06b06 | 2014-08-15 08:38:30 +0000 | [diff] [blame] | 427 | if (!report) |
| 428 | return; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 429 | |
tommi@webrtc.org | 5b06b06 | 2014-08-15 08:38:30 +0000 | [diff] [blame] | 430 | reports->push_back(report); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 431 | |
| 432 | std::string track_id; |
tommi@webrtc.org | 4fb7e25 | 2015-01-21 11:36:18 +0000 | [diff] [blame] | 433 | for (const auto* r : reports_) { |
| 434 | if (r->type() != StatsReport::kStatsReportTypeSsrc) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 435 | continue; |
tommi@webrtc.org | 5b06b06 | 2014-08-15 08:38:30 +0000 | [diff] [blame] | 436 | |
tommi@webrtc.org | 4fb7e25 | 2015-01-21 11:36:18 +0000 | [diff] [blame] | 437 | const StatsReport::Value* v = |
| 438 | r->FindValue(StatsReport::kStatsValueNameTrackId); |
tommi@webrtc.org | d390029 | 2015-03-12 16:35:55 +0000 | [diff] [blame^] | 439 | if (v && v->string_val() == track->id()) |
tommi@webrtc.org | 4fb7e25 | 2015-01-21 11:36:18 +0000 | [diff] [blame] | 440 | reports->push_back(r); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 441 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 442 | } |
| 443 | |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 444 | void |
| 445 | StatsCollector::UpdateStats(PeerConnectionInterface::StatsOutputLevel level) { |
tommi@webrtc.org | 69bc5a3 | 2014-12-15 09:44:48 +0000 | [diff] [blame] | 446 | ASSERT(session_->signaling_thread()->IsCurrent()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 447 | double time_now = GetTimeNow(); |
| 448 | // Calls to UpdateStats() that occur less than kMinGatherStatsPeriod number of |
| 449 | // ms apart will be ignored. |
| 450 | const double kMinGatherStatsPeriod = 50; |
xians@webrtc.org | 01bda20 | 2014-07-09 07:38:38 +0000 | [diff] [blame] | 451 | if (stats_gathering_started_ != 0 && |
| 452 | stats_gathering_started_ + kMinGatherStatsPeriod > time_now) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 453 | return; |
| 454 | } |
| 455 | stats_gathering_started_ = time_now; |
| 456 | |
| 457 | if (session_) { |
tommi@webrtc.org | d390029 | 2015-03-12 16:35:55 +0000 | [diff] [blame^] | 458 | // TODO(tommi): All of these hop over to the worker thread to fetch |
| 459 | // information. We could use an AsyncInvoker to run all of these and post |
| 460 | // the information back to the signaling thread where we can create and |
| 461 | // update stats reports. That would also clean up the threading story a bit |
| 462 | // since we'd be creating/updating the stats report objects consistently on |
| 463 | // the same thread (this class has no locks right now). |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 464 | ExtractSessionInfo(); |
| 465 | ExtractVoiceInfo(); |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 466 | ExtractVideoInfo(level); |
decurtis@webrtc.org | 487a444 | 2015-01-15 22:55:07 +0000 | [diff] [blame] | 467 | ExtractDataInfo(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 468 | } |
| 469 | } |
| 470 | |
tommi@webrtc.org | 4fb7e25 | 2015-01-21 11:36:18 +0000 | [diff] [blame] | 471 | StatsReport* StatsCollector::PrepareReport( |
| 472 | bool local, |
wu@webrtc.org | 97077a3 | 2013-10-25 21:18:33 +0000 | [diff] [blame] | 473 | uint32 ssrc, |
tommi@webrtc.org | d390029 | 2015-03-12 16:35:55 +0000 | [diff] [blame^] | 474 | const StatsReport::Id& transport_id, |
tommi@webrtc.org | 4fb7e25 | 2015-01-21 11:36:18 +0000 | [diff] [blame] | 475 | StatsReport::Direction direction) { |
tommi@webrtc.org | 69bc5a3 | 2014-12-15 09:44:48 +0000 | [diff] [blame] | 476 | ASSERT(session_->signaling_thread()->IsCurrent()); |
tommi@webrtc.org | d390029 | 2015-03-12 16:35:55 +0000 | [diff] [blame^] | 477 | StatsReport::Id id(StatsReport::NewIdWithDirection( |
tommi@webrtc.org | 4fb7e25 | 2015-01-21 11:36:18 +0000 | [diff] [blame] | 478 | local ? StatsReport::kStatsReportTypeSsrc : |
| 479 | StatsReport::kStatsReportTypeRemoteSsrc, |
tommi@webrtc.org | d390029 | 2015-03-12 16:35:55 +0000 | [diff] [blame^] | 480 | rtc::ToString<uint32>(ssrc), direction)); |
| 481 | StatsReport* report = reports_.Find(id); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 482 | |
xians@webrtc.org | 01bda20 | 2014-07-09 07:38:38 +0000 | [diff] [blame] | 483 | // Use the ID of the track that is currently mapped to the SSRC, if any. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 484 | std::string track_id; |
xians@webrtc.org | 01bda20 | 2014-07-09 07:38:38 +0000 | [diff] [blame] | 485 | if (!GetTrackIdBySsrc(ssrc, &track_id, direction)) { |
tommi@webrtc.org | 5b06b06 | 2014-08-15 08:38:30 +0000 | [diff] [blame] | 486 | if (!report) { |
xians@webrtc.org | 01bda20 | 2014-07-09 07:38:38 +0000 | [diff] [blame] | 487 | // The ssrc is not used by any track or existing report, return NULL |
| 488 | // in such case to indicate no report is prepared for the ssrc. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 489 | return NULL; |
xians@webrtc.org | 01bda20 | 2014-07-09 07:38:38 +0000 | [diff] [blame] | 490 | } |
| 491 | |
| 492 | // The ssrc is not used by any existing track. Keeps the old track id |
| 493 | // since we want to report the stats for inactive ssrc. |
tommi@webrtc.org | 4fb7e25 | 2015-01-21 11:36:18 +0000 | [diff] [blame] | 494 | const StatsReport::Value* v = |
| 495 | report->FindValue(StatsReport::kStatsValueNameTrackId); |
| 496 | if (v) |
tommi@webrtc.org | d390029 | 2015-03-12 16:35:55 +0000 | [diff] [blame^] | 497 | track_id = v->string_val(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 498 | } |
| 499 | |
tommi@webrtc.org | d390029 | 2015-03-12 16:35:55 +0000 | [diff] [blame^] | 500 | if (!report) |
| 501 | report = reports_.InsertNew(id); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 502 | |
tommi@webrtc.org | 4fb7e25 | 2015-01-21 11:36:18 +0000 | [diff] [blame] | 503 | // FYI - for remote reports, the timestamp will be overwritten later. |
tommi@webrtc.org | 8e327c4 | 2015-01-19 20:41:26 +0000 | [diff] [blame] | 504 | report->set_timestamp(stats_gathering_started_); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 505 | |
tommi@webrtc.org | d390029 | 2015-03-12 16:35:55 +0000 | [diff] [blame^] | 506 | report->AddInt64(StatsReport::kStatsValueNameSsrc, ssrc); |
tommi@webrtc.org | 92f4018 | 2015-03-04 15:25:19 +0000 | [diff] [blame] | 507 | report->AddString(StatsReport::kStatsValueNameTrackId, track_id); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 508 | // Add the mapping of SSRC to transport. |
tommi@webrtc.org | d390029 | 2015-03-12 16:35:55 +0000 | [diff] [blame^] | 509 | report->AddId(StatsReport::kStatsValueNameTransportId, transport_id); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 510 | return report; |
| 511 | } |
| 512 | |
tommi@webrtc.org | d390029 | 2015-03-12 16:35:55 +0000 | [diff] [blame^] | 513 | StatsReport* StatsCollector::AddOneCertificateReport( |
| 514 | const rtc::SSLCertificate* cert, const StatsReport* issuer) { |
tommi@webrtc.org | 4fb7e25 | 2015-01-21 11:36:18 +0000 | [diff] [blame] | 515 | ASSERT(session_->signaling_thread()->IsCurrent()); |
| 516 | |
wu@webrtc.org | 4551b79 | 2013-10-09 15:37:36 +0000 | [diff] [blame] | 517 | // TODO(bemasc): Move this computation to a helper class that caches these |
| 518 | // values to reduce CPU use in GetStats. This will require adding a fast |
| 519 | // SSLCertificate::Equals() method to detect certificate changes. |
mallinath@webrtc.org | 19f27e6 | 2013-10-13 17:18:27 +0000 | [diff] [blame] | 520 | |
| 521 | std::string digest_algorithm; |
| 522 | if (!cert->GetSignatureDigestAlgorithm(&digest_algorithm)) |
tommi@webrtc.org | d390029 | 2015-03-12 16:35:55 +0000 | [diff] [blame^] | 523 | return nullptr; |
mallinath@webrtc.org | 19f27e6 | 2013-10-13 17:18:27 +0000 | [diff] [blame] | 524 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 525 | rtc::scoped_ptr<rtc::SSLFingerprint> ssl_fingerprint( |
| 526 | rtc::SSLFingerprint::Create(digest_algorithm, cert)); |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 527 | |
| 528 | // SSLFingerprint::Create can fail if the algorithm returned by |
| 529 | // SSLCertificate::GetSignatureDigestAlgorithm is not supported by the |
| 530 | // implementation of SSLCertificate::ComputeDigest. This currently happens |
| 531 | // with MD5- and SHA-224-signed certificates when linked to libNSS. |
| 532 | if (!ssl_fingerprint) |
tommi@webrtc.org | d390029 | 2015-03-12 16:35:55 +0000 | [diff] [blame^] | 533 | return nullptr; |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 534 | |
wu@webrtc.org | 4551b79 | 2013-10-09 15:37:36 +0000 | [diff] [blame] | 535 | std::string fingerprint = ssl_fingerprint->GetRfc4572Fingerprint(); |
| 536 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 537 | rtc::Buffer der_buffer; |
wu@webrtc.org | 4551b79 | 2013-10-09 15:37:36 +0000 | [diff] [blame] | 538 | cert->ToDER(&der_buffer); |
| 539 | std::string der_base64; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 540 | rtc::Base64::EncodeFromArray( |
wu@webrtc.org | 4551b79 | 2013-10-09 15:37:36 +0000 | [diff] [blame] | 541 | der_buffer.data(), der_buffer.length(), &der_base64); |
| 542 | |
tommi@webrtc.org | d390029 | 2015-03-12 16:35:55 +0000 | [diff] [blame^] | 543 | StatsReport::Id id(StatsReport::NewTypedId( |
| 544 | StatsReport::kStatsReportTypeCertificate, fingerprint)); |
| 545 | StatsReport* report = reports_.ReplaceOrAddNew(id); |
tommi@webrtc.org | 8e327c4 | 2015-01-19 20:41:26 +0000 | [diff] [blame] | 546 | report->set_timestamp(stats_gathering_started_); |
tommi@webrtc.org | 92f4018 | 2015-03-04 15:25:19 +0000 | [diff] [blame] | 547 | report->AddString(StatsReport::kStatsValueNameFingerprint, fingerprint); |
| 548 | report->AddString(StatsReport::kStatsValueNameFingerprintAlgorithm, |
| 549 | digest_algorithm); |
| 550 | report->AddString(StatsReport::kStatsValueNameDer, der_base64); |
tommi@webrtc.org | d390029 | 2015-03-12 16:35:55 +0000 | [diff] [blame^] | 551 | if (issuer) |
| 552 | report->AddId(StatsReport::kStatsValueNameIssuerId, issuer->id()); |
| 553 | return report; |
wu@webrtc.org | 4551b79 | 2013-10-09 15:37:36 +0000 | [diff] [blame] | 554 | } |
| 555 | |
tommi@webrtc.org | d390029 | 2015-03-12 16:35:55 +0000 | [diff] [blame^] | 556 | StatsReport* StatsCollector::AddCertificateReports( |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 557 | const rtc::SSLCertificate* cert) { |
tommi@webrtc.org | 4fb7e25 | 2015-01-21 11:36:18 +0000 | [diff] [blame] | 558 | ASSERT(session_->signaling_thread()->IsCurrent()); |
wu@webrtc.org | 4551b79 | 2013-10-09 15:37:36 +0000 | [diff] [blame] | 559 | // Produces a chain of StatsReports representing this certificate and the rest |
| 560 | // of its chain, and adds those reports to |reports_|. The return value is |
| 561 | // the id of the leaf report. The provided cert must be non-null, so at least |
| 562 | // one report will always be provided and the returned string will never be |
| 563 | // empty. |
| 564 | ASSERT(cert != NULL); |
| 565 | |
tommi@webrtc.org | d390029 | 2015-03-12 16:35:55 +0000 | [diff] [blame^] | 566 | StatsReport* issuer = nullptr; |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 567 | rtc::scoped_ptr<rtc::SSLCertChain> chain; |
wu@webrtc.org | 4551b79 | 2013-10-09 15:37:36 +0000 | [diff] [blame] | 568 | if (cert->GetChain(chain.accept())) { |
| 569 | // This loop runs in reverse, i.e. from root to leaf, so that each |
| 570 | // certificate's issuer's report ID is known before the child certificate's |
| 571 | // report is generated. The root certificate does not have an issuer ID |
| 572 | // value. |
| 573 | for (ptrdiff_t i = chain->GetSize() - 1; i >= 0; --i) { |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 574 | const rtc::SSLCertificate& cert_i = chain->Get(i); |
tommi@webrtc.org | d390029 | 2015-03-12 16:35:55 +0000 | [diff] [blame^] | 575 | issuer = AddOneCertificateReport(&cert_i, issuer); |
wu@webrtc.org | 4551b79 | 2013-10-09 15:37:36 +0000 | [diff] [blame] | 576 | } |
| 577 | } |
| 578 | // Add the leaf certificate. |
tommi@webrtc.org | d390029 | 2015-03-12 16:35:55 +0000 | [diff] [blame^] | 579 | return AddOneCertificateReport(cert, issuer); |
wu@webrtc.org | 4551b79 | 2013-10-09 15:37:36 +0000 | [diff] [blame] | 580 | } |
| 581 | |
tommi@webrtc.org | d390029 | 2015-03-12 16:35:55 +0000 | [diff] [blame^] | 582 | StatsReport* StatsCollector::AddConnectionInfoReport( |
| 583 | const std::string& content_name, int component, int connection_id, |
| 584 | const StatsReport::Id& channel_report_id, |
| 585 | const cricket::ConnectionInfo& info) { |
| 586 | StatsReport::Id id(StatsReport::NewCandidatePairId(content_name, component, |
| 587 | connection_id)); |
| 588 | StatsReport* report = reports_.ReplaceOrAddNew(id); |
| 589 | report->set_timestamp(stats_gathering_started_); |
| 590 | |
| 591 | const BoolForAdd bools[] = { |
| 592 | { StatsReport::kStatsValueNameActiveConnection, info.best_connection }, |
| 593 | { StatsReport::kStatsValueNameReadable, info.readable }, |
| 594 | { StatsReport::kStatsValueNameWritable, info.writable }, |
| 595 | }; |
| 596 | for (const auto& b : bools) |
| 597 | report->AddBoolean(b.name, b.value); |
| 598 | |
| 599 | report->AddId(StatsReport::kStatsValueNameChannelId, channel_report_id); |
| 600 | report->AddId(StatsReport::kStatsValueNameLocalCandidateId, |
| 601 | AddCandidateReport(info.local_candidate, true)->id()); |
| 602 | report->AddId(StatsReport::kStatsValueNameRemoteCandidateId, |
| 603 | AddCandidateReport(info.remote_candidate, false)->id()); |
| 604 | |
| 605 | const Int64ForAdd int64s[] = { |
| 606 | { StatsReport::kStatsValueNameBytesReceived, info.recv_total_bytes }, |
| 607 | { StatsReport::kStatsValueNameBytesSent, info.sent_total_bytes }, |
| 608 | { StatsReport::kStatsValueNamePacketsSent, info.sent_total_packets }, |
| 609 | { StatsReport::kStatsValueNameRtt, info.rtt }, |
| 610 | { StatsReport::kStatsValueNameSendPacketsDiscarded, |
| 611 | info.sent_discarded_packets }, |
| 612 | }; |
| 613 | for (const auto& i : int64s) |
| 614 | report->AddInt64(i.name, i.value); |
| 615 | |
| 616 | report->AddString(StatsReport::kStatsValueNameLocalAddress, |
| 617 | info.local_candidate.address().ToString()); |
| 618 | report->AddString(StatsReport::kStatsValueNameLocalCandidateType, |
| 619 | info.local_candidate.type()); |
| 620 | report->AddString(StatsReport::kStatsValueNameRemoteAddress, |
| 621 | info.remote_candidate.address().ToString()); |
| 622 | report->AddString(StatsReport::kStatsValueNameRemoteCandidateType, |
| 623 | info.remote_candidate.type()); |
| 624 | report->AddString(StatsReport::kStatsValueNameTransportType, |
| 625 | info.local_candidate.protocol()); |
| 626 | |
| 627 | return report; |
| 628 | } |
| 629 | |
| 630 | StatsReport* StatsCollector::AddCandidateReport( |
guoweis@webrtc.org | 950c518 | 2014-12-16 23:01:31 +0000 | [diff] [blame] | 631 | const cricket::Candidate& candidate, |
tommi@webrtc.org | 4fb7e25 | 2015-01-21 11:36:18 +0000 | [diff] [blame] | 632 | bool local) { |
tommi@webrtc.org | d390029 | 2015-03-12 16:35:55 +0000 | [diff] [blame^] | 633 | StatsReport::Id id(StatsReport::NewCandidateId(local, candidate.id())); |
| 634 | StatsReport* report = reports_.Find(id); |
guoweis@webrtc.org | 950c518 | 2014-12-16 23:01:31 +0000 | [diff] [blame] | 635 | if (!report) { |
tommi@webrtc.org | d390029 | 2015-03-12 16:35:55 +0000 | [diff] [blame^] | 636 | report = reports_.InsertNew(id); |
tommi@webrtc.org | 4fb7e25 | 2015-01-21 11:36:18 +0000 | [diff] [blame] | 637 | report->set_timestamp(stats_gathering_started_); |
| 638 | if (local) { |
tommi@webrtc.org | 92f4018 | 2015-03-04 15:25:19 +0000 | [diff] [blame] | 639 | report->AddString(StatsReport::kStatsValueNameCandidateNetworkType, |
| 640 | AdapterTypeToStatsType(candidate.network_type())); |
guoweis@webrtc.org | 950c518 | 2014-12-16 23:01:31 +0000 | [diff] [blame] | 641 | } |
tommi@webrtc.org | 92f4018 | 2015-03-04 15:25:19 +0000 | [diff] [blame] | 642 | report->AddString(StatsReport::kStatsValueNameCandidateIPAddress, |
| 643 | candidate.address().ipaddr().ToString()); |
| 644 | report->AddString(StatsReport::kStatsValueNameCandidatePortNumber, |
| 645 | candidate.address().PortAsString()); |
| 646 | report->AddInt(StatsReport::kStatsValueNameCandidatePriority, |
| 647 | candidate.priority()); |
| 648 | report->AddString(StatsReport::kStatsValueNameCandidateType, |
| 649 | IceCandidateTypeToStatsType(candidate.type())); |
| 650 | report->AddString(StatsReport::kStatsValueNameCandidateTransportType, |
| 651 | candidate.protocol()); |
guoweis@webrtc.org | 950c518 | 2014-12-16 23:01:31 +0000 | [diff] [blame] | 652 | } |
| 653 | |
tommi@webrtc.org | d390029 | 2015-03-12 16:35:55 +0000 | [diff] [blame^] | 654 | return report; |
guoweis@webrtc.org | 950c518 | 2014-12-16 23:01:31 +0000 | [diff] [blame] | 655 | } |
| 656 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 657 | void StatsCollector::ExtractSessionInfo() { |
tommi@webrtc.org | 69bc5a3 | 2014-12-15 09:44:48 +0000 | [diff] [blame] | 658 | ASSERT(session_->signaling_thread()->IsCurrent()); |
tommi@webrtc.org | d390029 | 2015-03-12 16:35:55 +0000 | [diff] [blame^] | 659 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 660 | // Extract information from the base session. |
tommi@webrtc.org | d390029 | 2015-03-12 16:35:55 +0000 | [diff] [blame^] | 661 | StatsReport::Id id(StatsReport::NewTypedId( |
| 662 | StatsReport::kStatsReportTypeSession, session_->id())); |
| 663 | StatsReport* report = reports_.ReplaceOrAddNew(id); |
tommi@webrtc.org | 8e327c4 | 2015-01-19 20:41:26 +0000 | [diff] [blame] | 664 | report->set_timestamp(stats_gathering_started_); |
tommi@webrtc.org | 5b06b06 | 2014-08-15 08:38:30 +0000 | [diff] [blame] | 665 | report->AddBoolean(StatsReport::kStatsValueNameInitiator, |
| 666 | session_->initiator()); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 667 | |
| 668 | cricket::SessionStats stats; |
| 669 | if (session_->GetStats(&stats)) { |
| 670 | // Store the proxy map away for use in SSRC reporting. |
tommi@webrtc.org | d390029 | 2015-03-12 16:35:55 +0000 | [diff] [blame^] | 671 | // TODO(tommi): This shouldn't be necessary if we post the stats back to the |
| 672 | // signaling thread after fetching them on the worker thread, then just use |
| 673 | // the proxy map directly from the session stats. |
| 674 | // As is, if GetStats() failed, we could be using old (incorrect?) proxy |
| 675 | // data. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 676 | proxy_to_transport_ = stats.proxy_to_transport; |
| 677 | |
tommi@webrtc.org | d390029 | 2015-03-12 16:35:55 +0000 | [diff] [blame^] | 678 | for (const auto& transport_iter : stats.transport_stats) { |
wu@webrtc.org | 4551b79 | 2013-10-09 15:37:36 +0000 | [diff] [blame] | 679 | // Attempt to get a copy of the certificates from the transport and |
| 680 | // expose them in stats reports. All channels in a transport share the |
| 681 | // same local and remote certificates. |
jiayl@webrtc.org | 06b04ec | 2014-07-24 20:41:20 +0000 | [diff] [blame] | 682 | // |
| 683 | // Note that Transport::GetIdentity and Transport::GetRemoteCertificate |
| 684 | // invoke method calls on the worker thread and block this thread, but |
| 685 | // messages are still processed on this thread, which may blow way the |
| 686 | // existing transports. So we cannot reuse |transport| after these calls. |
tommi@webrtc.org | d390029 | 2015-03-12 16:35:55 +0000 | [diff] [blame^] | 687 | StatsReport::Id local_cert_report_id, remote_cert_report_id; |
jiayl@webrtc.org | 06b04ec | 2014-07-24 20:41:20 +0000 | [diff] [blame] | 688 | |
wu@webrtc.org | 4551b79 | 2013-10-09 15:37:36 +0000 | [diff] [blame] | 689 | cricket::Transport* transport = |
tommi@webrtc.org | d390029 | 2015-03-12 16:35:55 +0000 | [diff] [blame^] | 690 | session_->GetTransport(transport_iter.second.content_name); |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 691 | rtc::scoped_ptr<rtc::SSLIdentity> identity; |
jiayl@webrtc.org | 06b04ec | 2014-07-24 20:41:20 +0000 | [diff] [blame] | 692 | if (transport && transport->GetIdentity(identity.accept())) { |
tommi@webrtc.org | d390029 | 2015-03-12 16:35:55 +0000 | [diff] [blame^] | 693 | StatsReport* r = AddCertificateReports(&(identity->certificate())); |
| 694 | if (r) |
| 695 | local_cert_report_id = r->id(); |
wu@webrtc.org | 4551b79 | 2013-10-09 15:37:36 +0000 | [diff] [blame] | 696 | } |
jiayl@webrtc.org | 06b04ec | 2014-07-24 20:41:20 +0000 | [diff] [blame] | 697 | |
tommi@webrtc.org | d390029 | 2015-03-12 16:35:55 +0000 | [diff] [blame^] | 698 | transport = session_->GetTransport(transport_iter.second.content_name); |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 699 | rtc::scoped_ptr<rtc::SSLCertificate> cert; |
jiayl@webrtc.org | 06b04ec | 2014-07-24 20:41:20 +0000 | [diff] [blame] | 700 | if (transport && transport->GetRemoteCertificate(cert.accept())) { |
tommi@webrtc.org | d390029 | 2015-03-12 16:35:55 +0000 | [diff] [blame^] | 701 | StatsReport* r = AddCertificateReports(cert.get()); |
| 702 | if (r) |
| 703 | remote_cert_report_id = r->id(); |
jiayl@webrtc.org | 06b04ec | 2014-07-24 20:41:20 +0000 | [diff] [blame] | 704 | } |
| 705 | |
tommi@webrtc.org | d390029 | 2015-03-12 16:35:55 +0000 | [diff] [blame^] | 706 | for (const auto& channel_iter : transport_iter.second.channel_stats) { |
| 707 | StatsReport::Id id(StatsReport::NewComponentId( |
| 708 | transport_iter.second.content_name, channel_iter.component)); |
| 709 | StatsReport* channel_report = reports_.ReplaceOrAddNew(id); |
tommi@webrtc.org | 8e327c4 | 2015-01-19 20:41:26 +0000 | [diff] [blame] | 710 | channel_report->set_timestamp(stats_gathering_started_); |
tommi@webrtc.org | 92f4018 | 2015-03-04 15:25:19 +0000 | [diff] [blame] | 711 | channel_report->AddInt(StatsReport::kStatsValueNameComponent, |
tommi@webrtc.org | d390029 | 2015-03-12 16:35:55 +0000 | [diff] [blame^] | 712 | channel_iter.component); |
| 713 | if (local_cert_report_id.get()) { |
| 714 | channel_report->AddId(StatsReport::kStatsValueNameLocalCertificateId, |
| 715 | local_cert_report_id); |
decurtis@webrtc.org | 487a444 | 2015-01-15 22:55:07 +0000 | [diff] [blame] | 716 | } |
tommi@webrtc.org | d390029 | 2015-03-12 16:35:55 +0000 | [diff] [blame^] | 717 | if (remote_cert_report_id.get()) { |
| 718 | channel_report->AddId(StatsReport::kStatsValueNameRemoteCertificateId, |
| 719 | remote_cert_report_id); |
decurtis@webrtc.org | 487a444 | 2015-01-15 22:55:07 +0000 | [diff] [blame] | 720 | } |
tommi@webrtc.org | d390029 | 2015-03-12 16:35:55 +0000 | [diff] [blame^] | 721 | const std::string& srtp_cipher = channel_iter.srtp_cipher; |
pthatcher@webrtc.org | 7bea1ff | 2015-03-04 01:38:30 +0000 | [diff] [blame] | 722 | if (!srtp_cipher.empty()) { |
tommi@webrtc.org | d390029 | 2015-03-12 16:35:55 +0000 | [diff] [blame^] | 723 | channel_report->AddString(StatsReport::kStatsValueNameSrtpCipher, |
| 724 | srtp_cipher); |
pthatcher@webrtc.org | 7bea1ff | 2015-03-04 01:38:30 +0000 | [diff] [blame] | 725 | } |
tommi@webrtc.org | d390029 | 2015-03-12 16:35:55 +0000 | [diff] [blame^] | 726 | const std::string& ssl_cipher = channel_iter.ssl_cipher; |
pthatcher@webrtc.org | 7bea1ff | 2015-03-04 01:38:30 +0000 | [diff] [blame] | 727 | if (!ssl_cipher.empty()) { |
tommi@webrtc.org | d390029 | 2015-03-12 16:35:55 +0000 | [diff] [blame^] | 728 | channel_report->AddString(StatsReport::kStatsValueNameDtlsCipher, |
| 729 | ssl_cipher); |
pthatcher@webrtc.org | 7bea1ff | 2015-03-04 01:38:30 +0000 | [diff] [blame] | 730 | } |
tommi@webrtc.org | 5b06b06 | 2014-08-15 08:38:30 +0000 | [diff] [blame] | 731 | |
tommi@webrtc.org | d390029 | 2015-03-12 16:35:55 +0000 | [diff] [blame^] | 732 | int connection_id = 0; |
| 733 | for (const cricket::ConnectionInfo& info : |
| 734 | channel_iter.connection_infos) { |
| 735 | StatsReport* connection_report = AddConnectionInfoReport( |
| 736 | transport_iter.first, channel_iter.component, connection_id++, |
| 737 | channel_report->id(), info); |
pthatcher@webrtc.org | 7bea1ff | 2015-03-04 01:38:30 +0000 | [diff] [blame] | 738 | if (info.best_connection) { |
tommi@webrtc.org | d390029 | 2015-03-12 16:35:55 +0000 | [diff] [blame^] | 739 | channel_report->AddId( |
pthatcher@webrtc.org | 7bea1ff | 2015-03-04 01:38:30 +0000 | [diff] [blame] | 740 | StatsReport::kStatsValueNameSelectedCandidatePairId, |
tommi@webrtc.org | d390029 | 2015-03-12 16:35:55 +0000 | [diff] [blame^] | 741 | connection_report->id()); |
pthatcher@webrtc.org | 7bea1ff | 2015-03-04 01:38:30 +0000 | [diff] [blame] | 742 | } |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 743 | } |
| 744 | } |
| 745 | } |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | void StatsCollector::ExtractVoiceInfo() { |
tommi@webrtc.org | 69bc5a3 | 2014-12-15 09:44:48 +0000 | [diff] [blame] | 750 | ASSERT(session_->signaling_thread()->IsCurrent()); |
| 751 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 752 | if (!session_->voice_channel()) { |
| 753 | return; |
| 754 | } |
| 755 | cricket::VoiceMediaInfo voice_info; |
| 756 | if (!session_->voice_channel()->GetStats(&voice_info)) { |
| 757 | LOG(LS_ERROR) << "Failed to get voice channel stats."; |
| 758 | return; |
| 759 | } |
tommi@webrtc.org | d390029 | 2015-03-12 16:35:55 +0000 | [diff] [blame^] | 760 | |
| 761 | // TODO(tommi): The above code should run on the worker thread and post the |
| 762 | // results back to the signaling thread, where we can add data to the reports. |
| 763 | rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls; |
| 764 | |
| 765 | StatsReport::Id transport_id(GetTransportIdFromProxy(proxy_to_transport_, |
| 766 | session_->voice_channel()->content_name())); |
| 767 | if (!transport_id.get()) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 768 | LOG(LS_ERROR) << "Failed to get transport name for proxy " |
| 769 | << session_->voice_channel()->content_name(); |
| 770 | return; |
| 771 | } |
tommi@webrtc.org | d390029 | 2015-03-12 16:35:55 +0000 | [diff] [blame^] | 772 | |
tommi@webrtc.org | 4fb7e25 | 2015-01-21 11:36:18 +0000 | [diff] [blame] | 773 | ExtractStatsFromList(voice_info.receivers, transport_id, this, |
| 774 | StatsReport::kReceive); |
| 775 | ExtractStatsFromList(voice_info.senders, transport_id, this, |
| 776 | StatsReport::kSend); |
henrike@webrtc.org | 40b3b68 | 2014-03-03 18:30:11 +0000 | [diff] [blame] | 777 | |
| 778 | UpdateStatsFromExistingLocalAudioTracks(); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 779 | } |
| 780 | |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 781 | void StatsCollector::ExtractVideoInfo( |
| 782 | PeerConnectionInterface::StatsOutputLevel level) { |
tommi@webrtc.org | 69bc5a3 | 2014-12-15 09:44:48 +0000 | [diff] [blame] | 783 | ASSERT(session_->signaling_thread()->IsCurrent()); |
| 784 | |
| 785 | if (!session_->video_channel()) |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 786 | return; |
tommi@webrtc.org | 69bc5a3 | 2014-12-15 09:44:48 +0000 | [diff] [blame] | 787 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 788 | cricket::VideoMediaInfo video_info; |
pbos@webrtc.org | 058b1f1 | 2015-03-04 08:54:32 +0000 | [diff] [blame] | 789 | if (!session_->video_channel()->GetStats(&video_info)) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 790 | LOG(LS_ERROR) << "Failed to get video channel stats."; |
| 791 | return; |
| 792 | } |
tommi@webrtc.org | d390029 | 2015-03-12 16:35:55 +0000 | [diff] [blame^] | 793 | |
| 794 | // TODO(tommi): The above code should run on the worker thread and post the |
| 795 | // results back to the signaling thread, where we can add data to the reports. |
| 796 | rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls; |
| 797 | |
| 798 | StatsReport::Id transport_id(GetTransportIdFromProxy(proxy_to_transport_, |
| 799 | session_->video_channel()->content_name())); |
| 800 | if (!transport_id.get()) { |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 801 | LOG(LS_ERROR) << "Failed to get transport name for proxy " |
| 802 | << session_->video_channel()->content_name(); |
| 803 | return; |
| 804 | } |
tommi@webrtc.org | 4fb7e25 | 2015-01-21 11:36:18 +0000 | [diff] [blame] | 805 | ExtractStatsFromList(video_info.receivers, transport_id, this, |
| 806 | StatsReport::kReceive); |
| 807 | ExtractStatsFromList(video_info.senders, transport_id, this, |
| 808 | StatsReport::kSend); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 809 | if (video_info.bw_estimations.size() != 1) { |
| 810 | LOG(LS_ERROR) << "BWEs count: " << video_info.bw_estimations.size(); |
| 811 | } else { |
tommi@webrtc.org | d390029 | 2015-03-12 16:35:55 +0000 | [diff] [blame^] | 812 | StatsReport::Id report_id(StatsReport::NewBandwidthEstimationId()); |
| 813 | StatsReport* report = reports_.FindOrAddNew(report_id); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 814 | ExtractStats( |
wu@webrtc.org | b9a088b | 2014-02-13 23:18:49 +0000 | [diff] [blame] | 815 | video_info.bw_estimations[0], stats_gathering_started_, level, report); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 816 | } |
| 817 | } |
| 818 | |
decurtis@webrtc.org | 487a444 | 2015-01-15 22:55:07 +0000 | [diff] [blame] | 819 | void StatsCollector::ExtractDataInfo() { |
| 820 | ASSERT(session_->signaling_thread()->IsCurrent()); |
| 821 | |
tommi@webrtc.org | d390029 | 2015-03-12 16:35:55 +0000 | [diff] [blame^] | 822 | rtc::Thread::ScopedDisallowBlockingCalls no_blocking_calls; |
| 823 | |
decurtis@webrtc.org | 487a444 | 2015-01-15 22:55:07 +0000 | [diff] [blame] | 824 | for (const auto& dc : |
| 825 | session_->mediastream_signaling()->sctp_data_channels()) { |
tommi@webrtc.org | d390029 | 2015-03-12 16:35:55 +0000 | [diff] [blame^] | 826 | StatsReport::Id id(StatsReport::NewTypedIntId( |
decurtis@webrtc.org | 322a564 | 2015-02-03 22:09:37 +0000 | [diff] [blame] | 827 | StatsReport::kStatsReportTypeDataChannel, dc->id())); |
tommi@webrtc.org | d390029 | 2015-03-12 16:35:55 +0000 | [diff] [blame^] | 828 | StatsReport* report = reports_.ReplaceOrAddNew(id); |
decurtis@webrtc.org | 322a564 | 2015-02-03 22:09:37 +0000 | [diff] [blame] | 829 | report->set_timestamp(stats_gathering_started_); |
tommi@webrtc.org | 92f4018 | 2015-03-04 15:25:19 +0000 | [diff] [blame] | 830 | report->AddString(StatsReport::kStatsValueNameLabel, dc->label()); |
| 831 | report->AddInt(StatsReport::kStatsValueNameDataChannelId, dc->id()); |
| 832 | report->AddString(StatsReport::kStatsValueNameProtocol, dc->protocol()); |
| 833 | report->AddString(StatsReport::kStatsValueNameState, |
| 834 | DataChannelInterface::DataStateString(dc->state())); |
decurtis@webrtc.org | 487a444 | 2015-01-15 22:55:07 +0000 | [diff] [blame] | 835 | } |
| 836 | } |
| 837 | |
tommi@webrtc.org | 4fb7e25 | 2015-01-21 11:36:18 +0000 | [diff] [blame] | 838 | StatsReport* StatsCollector::GetReport(const StatsReport::StatsType& type, |
xians@webrtc.org | 4cb0128 | 2014-06-12 14:57:05 +0000 | [diff] [blame] | 839 | const std::string& id, |
tommi@webrtc.org | 4fb7e25 | 2015-01-21 11:36:18 +0000 | [diff] [blame] | 840 | StatsReport::Direction direction) { |
tommi@webrtc.org | 69bc5a3 | 2014-12-15 09:44:48 +0000 | [diff] [blame] | 841 | ASSERT(session_->signaling_thread()->IsCurrent()); |
xians@webrtc.org | 4cb0128 | 2014-06-12 14:57:05 +0000 | [diff] [blame] | 842 | ASSERT(type == StatsReport::kStatsReportTypeSsrc || |
| 843 | type == StatsReport::kStatsReportTypeRemoteSsrc); |
tommi@webrtc.org | 4fb7e25 | 2015-01-21 11:36:18 +0000 | [diff] [blame] | 844 | return reports_.Find(StatsReport::NewIdWithDirection(type, id, direction)); |
henrike@webrtc.org | 40b3b68 | 2014-03-03 18:30:11 +0000 | [diff] [blame] | 845 | } |
| 846 | |
henrike@webrtc.org | 40b3b68 | 2014-03-03 18:30:11 +0000 | [diff] [blame] | 847 | void StatsCollector::UpdateStatsFromExistingLocalAudioTracks() { |
tommi@webrtc.org | 69bc5a3 | 2014-12-15 09:44:48 +0000 | [diff] [blame] | 848 | ASSERT(session_->signaling_thread()->IsCurrent()); |
henrike@webrtc.org | 40b3b68 | 2014-03-03 18:30:11 +0000 | [diff] [blame] | 849 | // Loop through the existing local audio tracks. |
tommi@webrtc.org | d390029 | 2015-03-12 16:35:55 +0000 | [diff] [blame^] | 850 | for (const auto& it : local_audio_tracks_) { |
| 851 | AudioTrackInterface* track = it.first; |
| 852 | uint32 ssrc = it.second; |
henrike@webrtc.org | 40b3b68 | 2014-03-03 18:30:11 +0000 | [diff] [blame] | 853 | StatsReport* report = GetReport(StatsReport::kStatsReportTypeSsrc, |
tommi@webrtc.org | d390029 | 2015-03-12 16:35:55 +0000 | [diff] [blame^] | 854 | rtc::ToString<uint32>(ssrc), |
tommi@webrtc.org | 4fb7e25 | 2015-01-21 11:36:18 +0000 | [diff] [blame] | 855 | StatsReport::kSend); |
henrike@webrtc.org | d3d6bce | 2014-03-10 20:41:22 +0000 | [diff] [blame] | 856 | if (report == NULL) { |
| 857 | // This can happen if a local audio track is added to a stream on the |
| 858 | // fly and the report has not been set up yet. Do nothing in this case. |
| 859 | LOG(LS_ERROR) << "Stats report does not exist for ssrc " << ssrc; |
| 860 | continue; |
| 861 | } |
henrike@webrtc.org | 40b3b68 | 2014-03-03 18:30:11 +0000 | [diff] [blame] | 862 | |
| 863 | // The same ssrc can be used by both local and remote audio tracks. |
tommi@webrtc.org | 4fb7e25 | 2015-01-21 11:36:18 +0000 | [diff] [blame] | 864 | const StatsReport::Value* v = |
| 865 | report->FindValue(StatsReport::kStatsValueNameTrackId); |
tommi@webrtc.org | d390029 | 2015-03-12 16:35:55 +0000 | [diff] [blame^] | 866 | if (!v || v->string_val() != track->id()) |
henrike@webrtc.org | 40b3b68 | 2014-03-03 18:30:11 +0000 | [diff] [blame] | 867 | continue; |
henrike@webrtc.org | 40b3b68 | 2014-03-03 18:30:11 +0000 | [diff] [blame] | 868 | |
| 869 | UpdateReportFromAudioTrack(track, report); |
| 870 | } |
| 871 | } |
| 872 | |
| 873 | void StatsCollector::UpdateReportFromAudioTrack(AudioTrackInterface* track, |
| 874 | StatsReport* report) { |
tommi@webrtc.org | 69bc5a3 | 2014-12-15 09:44:48 +0000 | [diff] [blame] | 875 | ASSERT(session_->signaling_thread()->IsCurrent()); |
henrike@webrtc.org | 40b3b68 | 2014-03-03 18:30:11 +0000 | [diff] [blame] | 876 | ASSERT(track != NULL); |
henrike@webrtc.org | 40b3b68 | 2014-03-03 18:30:11 +0000 | [diff] [blame] | 877 | |
| 878 | int signal_level = 0; |
tommi@webrtc.org | 92f4018 | 2015-03-04 15:25:19 +0000 | [diff] [blame] | 879 | if (!track->GetSignalLevel(&signal_level)) |
| 880 | signal_level = -1; |
henrike@webrtc.org | 40b3b68 | 2014-03-03 18:30:11 +0000 | [diff] [blame] | 881 | |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 882 | rtc::scoped_refptr<AudioProcessorInterface> audio_processor( |
henrike@webrtc.org | 40b3b68 | 2014-03-03 18:30:11 +0000 | [diff] [blame] | 883 | track->GetAudioProcessor()); |
henrike@webrtc.org | 40b3b68 | 2014-03-03 18:30:11 +0000 | [diff] [blame] | 884 | |
| 885 | AudioProcessorInterface::AudioProcessorStats stats; |
tommi@webrtc.org | 92f4018 | 2015-03-04 15:25:19 +0000 | [diff] [blame] | 886 | if (audio_processor.get()) |
| 887 | audio_processor->GetStats(&stats); |
| 888 | |
| 889 | SetAudioProcessingStats(report, signal_level, stats.typing_noise_detected, |
| 890 | stats.echo_return_loss, stats.echo_return_loss_enhancement, |
| 891 | stats.echo_delay_median_ms, stats.aec_quality_min, |
| 892 | stats.echo_delay_std_ms); |
henrike@webrtc.org | 40b3b68 | 2014-03-03 18:30:11 +0000 | [diff] [blame] | 893 | } |
| 894 | |
xians@webrtc.org | 4cb0128 | 2014-06-12 14:57:05 +0000 | [diff] [blame] | 895 | bool StatsCollector::GetTrackIdBySsrc(uint32 ssrc, std::string* track_id, |
tommi@webrtc.org | 4fb7e25 | 2015-01-21 11:36:18 +0000 | [diff] [blame] | 896 | StatsReport::Direction direction) { |
tommi@webrtc.org | 69bc5a3 | 2014-12-15 09:44:48 +0000 | [diff] [blame] | 897 | ASSERT(session_->signaling_thread()->IsCurrent()); |
tommi@webrtc.org | 4fb7e25 | 2015-01-21 11:36:18 +0000 | [diff] [blame] | 898 | if (direction == StatsReport::kSend) { |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 899 | if (!session_->GetLocalTrackIdBySsrc(ssrc, track_id)) { |
xians@webrtc.org | 4cb0128 | 2014-06-12 14:57:05 +0000 | [diff] [blame] | 900 | LOG(LS_WARNING) << "The SSRC " << ssrc |
| 901 | << " is not associated with a sending track"; |
| 902 | return false; |
| 903 | } |
| 904 | } else { |
tommi@webrtc.org | 4fb7e25 | 2015-01-21 11:36:18 +0000 | [diff] [blame] | 905 | ASSERT(direction == StatsReport::kReceive); |
tommi@webrtc.org | 03505bc | 2014-07-14 20:15:26 +0000 | [diff] [blame] | 906 | if (!session_->GetRemoteTrackIdBySsrc(ssrc, track_id)) { |
xians@webrtc.org | 4cb0128 | 2014-06-12 14:57:05 +0000 | [diff] [blame] | 907 | LOG(LS_WARNING) << "The SSRC " << ssrc |
| 908 | << " is not associated with a receiving track"; |
| 909 | return false; |
| 910 | } |
| 911 | } |
| 912 | |
| 913 | return true; |
| 914 | } |
| 915 | |
tommi@webrtc.org | 69bc5a3 | 2014-12-15 09:44:48 +0000 | [diff] [blame] | 916 | void StatsCollector::ClearUpdateStatsCacheForTest() { |
xians@webrtc.org | 01bda20 | 2014-07-09 07:38:38 +0000 | [diff] [blame] | 917 | stats_gathering_started_ = 0; |
| 918 | } |
| 919 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 920 | } // namespace webrtc |