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