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