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