hbos | d565b73 | 2016-08-30 21:04:35 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
hbos | 74e1a4f | 2016-09-16 06:33:01 | [diff] [blame] | 11 | #include "webrtc/api/rtcstatscollector.h" |
hbos | d565b73 | 2016-08-30 21:04:35 | [diff] [blame] | 12 | |
| 13 | #include <memory> |
hbos | da389e3 | 2016-10-25 17:55:08 | [diff] [blame] | 14 | #include <ostream> |
hbos | d565b73 | 2016-08-30 21:04:35 | [diff] [blame] | 15 | #include <string> |
| 16 | #include <vector> |
| 17 | |
hbos | 0adb828 | 2016-11-23 10:32:06 | [diff] [blame] | 18 | #include "webrtc/api/jsepsessiondescription.h" |
hbos | 09bc128 | 2016-11-08 14:29:22 | [diff] [blame] | 19 | #include "webrtc/api/mediastream.h" |
| 20 | #include "webrtc/api/mediastreamtrack.h" |
hbos | 0adb828 | 2016-11-23 10:32:06 | [diff] [blame] | 21 | #include "webrtc/api/rtpparameters.h" |
hbos | 74e1a4f | 2016-09-16 06:33:01 | [diff] [blame] | 22 | #include "webrtc/api/stats/rtcstats_objects.h" |
| 23 | #include "webrtc/api/stats/rtcstatsreport.h" |
hbos | d565b73 | 2016-08-30 21:04:35 | [diff] [blame] | 24 | #include "webrtc/api/test/mock_datachannel.h" |
| 25 | #include "webrtc/api/test/mock_peerconnection.h" |
| 26 | #include "webrtc/api/test/mock_webrtcsession.h" |
hbos | db346a7 | 2016-11-29 09:57:01 | [diff] [blame] | 27 | #include "webrtc/api/test/rtcstatsobtainer.h" |
hbos | d565b73 | 2016-08-30 21:04:35 | [diff] [blame] | 28 | #include "webrtc/base/checks.h" |
hbos | 0e6758d | 2016-08-31 14:57:36 | [diff] [blame] | 29 | #include "webrtc/base/fakeclock.h" |
hbos | 6ab97ce0 | 2016-10-03 21:16:56 | [diff] [blame] | 30 | #include "webrtc/base/fakesslidentity.h" |
hbos | d565b73 | 2016-08-30 21:04:35 | [diff] [blame] | 31 | #include "webrtc/base/gunit.h" |
| 32 | #include "webrtc/base/logging.h" |
hbos | ab9f6e4 | 2016-10-07 09:18:47 | [diff] [blame] | 33 | #include "webrtc/base/socketaddress.h" |
hbos | c82f2e1 | 2016-09-05 08:36:50 | [diff] [blame] | 34 | #include "webrtc/base/thread_checker.h" |
hbos | 0e6758d | 2016-08-31 14:57:36 | [diff] [blame] | 35 | #include "webrtc/base/timedelta.h" |
| 36 | #include "webrtc/base/timeutils.h" |
skvlad | 11a9cbf | 2016-10-07 18:53:05 | [diff] [blame] | 37 | #include "webrtc/logging/rtc_event_log/rtc_event_log.h" |
hbos | d565b73 | 2016-08-30 21:04:35 | [diff] [blame] | 38 | #include "webrtc/media/base/fakemediaengine.h" |
hbos | 6ded190 | 2016-11-01 08:50:46 | [diff] [blame] | 39 | #include "webrtc/media/base/test/mock_mediachannel.h" |
hbos | 2fa7c67 | 2016-10-24 11:00:05 | [diff] [blame] | 40 | #include "webrtc/p2p/base/p2pconstants.h" |
hbos | ab9f6e4 | 2016-10-07 09:18:47 | [diff] [blame] | 41 | #include "webrtc/p2p/base/port.h" |
hbos | d565b73 | 2016-08-30 21:04:35 | [diff] [blame] | 42 | |
hbos | 6ab97ce0 | 2016-10-03 21:16:56 | [diff] [blame] | 43 | using testing::_; |
| 44 | using testing::Invoke; |
hbos | d565b73 | 2016-08-30 21:04:35 | [diff] [blame] | 45 | using testing::Return; |
hbos | 6ded190 | 2016-11-01 08:50:46 | [diff] [blame] | 46 | using testing::ReturnNull; |
hbos | d565b73 | 2016-08-30 21:04:35 | [diff] [blame] | 47 | using testing::ReturnRef; |
hbos | 6ded190 | 2016-11-01 08:50:46 | [diff] [blame] | 48 | using testing::SetArgPointee; |
hbos | d565b73 | 2016-08-30 21:04:35 | [diff] [blame] | 49 | |
| 50 | namespace webrtc { |
| 51 | |
hbos | da389e3 | 2016-10-25 17:55:08 | [diff] [blame] | 52 | // These are used by gtest code, such as if |EXPECT_EQ| fails. |
| 53 | void PrintTo(const RTCCertificateStats& stats, ::std::ostream* os) { |
| 54 | *os << stats.ToString(); |
| 55 | } |
| 56 | |
hbos | 0adb828 | 2016-11-23 10:32:06 | [diff] [blame] | 57 | void PrintTo(const RTCCodecStats& stats, ::std::ostream* os) { |
| 58 | *os << stats.ToString(); |
| 59 | } |
| 60 | |
hbos | da389e3 | 2016-10-25 17:55:08 | [diff] [blame] | 61 | void PrintTo(const RTCDataChannelStats& stats, ::std::ostream* os) { |
| 62 | *os << stats.ToString(); |
| 63 | } |
| 64 | |
| 65 | void PrintTo(const RTCIceCandidatePairStats& stats, ::std::ostream* os) { |
| 66 | *os << stats.ToString(); |
| 67 | } |
| 68 | |
| 69 | void PrintTo(const RTCLocalIceCandidateStats& stats, ::std::ostream* os) { |
| 70 | *os << stats.ToString(); |
| 71 | } |
| 72 | |
| 73 | void PrintTo(const RTCRemoteIceCandidateStats& stats, ::std::ostream* os) { |
| 74 | *os << stats.ToString(); |
| 75 | } |
| 76 | |
| 77 | void PrintTo(const RTCPeerConnectionStats& stats, ::std::ostream* os) { |
| 78 | *os << stats.ToString(); |
| 79 | } |
| 80 | |
hbos | 09bc128 | 2016-11-08 14:29:22 | [diff] [blame] | 81 | void PrintTo(const RTCMediaStreamStats& stats, ::std::ostream* os) { |
| 82 | *os << stats.ToString(); |
| 83 | } |
| 84 | |
| 85 | void PrintTo(const RTCMediaStreamTrackStats& stats, ::std::ostream* os) { |
| 86 | *os << stats.ToString(); |
| 87 | } |
| 88 | |
hbos | eeafe94 | 2016-11-01 10:00:17 | [diff] [blame] | 89 | void PrintTo(const RTCInboundRTPStreamStats& stats, ::std::ostream* os) { |
| 90 | *os << stats.ToString(); |
| 91 | } |
| 92 | |
hbos | 6ded190 | 2016-11-01 08:50:46 | [diff] [blame] | 93 | void PrintTo(const RTCOutboundRTPStreamStats& stats, ::std::ostream* os) { |
| 94 | *os << stats.ToString(); |
| 95 | } |
| 96 | |
hbos | da389e3 | 2016-10-25 17:55:08 | [diff] [blame] | 97 | void PrintTo(const RTCTransportStats& stats, ::std::ostream* os) { |
| 98 | *os << stats.ToString(); |
| 99 | } |
| 100 | |
hbos | c82f2e1 | 2016-09-05 08:36:50 | [diff] [blame] | 101 | namespace { |
| 102 | |
| 103 | const int64_t kGetStatsReportTimeoutMs = 1000; |
| 104 | |
hbos | 6ab97ce0 | 2016-10-03 21:16:56 | [diff] [blame] | 105 | struct CertificateInfo { |
| 106 | rtc::scoped_refptr<rtc::RTCCertificate> certificate; |
| 107 | std::vector<std::string> ders; |
| 108 | std::vector<std::string> pems; |
| 109 | std::vector<std::string> fingerprints; |
| 110 | }; |
| 111 | |
| 112 | std::unique_ptr<CertificateInfo> CreateFakeCertificateAndInfoFromDers( |
| 113 | const std::vector<std::string>& ders) { |
| 114 | RTC_CHECK(!ders.empty()); |
| 115 | std::unique_ptr<CertificateInfo> info(new CertificateInfo()); |
| 116 | info->ders = ders; |
| 117 | for (const std::string& der : ders) { |
| 118 | info->pems.push_back(rtc::SSLIdentity::DerToPem( |
| 119 | "CERTIFICATE", |
| 120 | reinterpret_cast<const unsigned char*>(der.c_str()), |
| 121 | der.length())); |
| 122 | } |
| 123 | info->certificate = |
| 124 | rtc::RTCCertificate::Create(std::unique_ptr<rtc::FakeSSLIdentity>( |
| 125 | new rtc::FakeSSLIdentity(rtc::FakeSSLCertificate(info->pems)))); |
| 126 | // Strip header/footer and newline characters of PEM strings. |
| 127 | for (size_t i = 0; i < info->pems.size(); ++i) { |
| 128 | rtc::replace_substrs("-----BEGIN CERTIFICATE-----", 27, |
| 129 | "", 0, &info->pems[i]); |
| 130 | rtc::replace_substrs("-----END CERTIFICATE-----", 25, |
| 131 | "", 0, &info->pems[i]); |
| 132 | rtc::replace_substrs("\n", 1, |
| 133 | "", 0, &info->pems[i]); |
| 134 | } |
| 135 | // Fingerprint of leaf certificate. |
| 136 | std::unique_ptr<rtc::SSLFingerprint> fp( |
| 137 | rtc::SSLFingerprint::Create("sha-1", |
| 138 | &info->certificate->ssl_certificate())); |
| 139 | EXPECT_TRUE(fp); |
| 140 | info->fingerprints.push_back(fp->GetRfc4572Fingerprint()); |
| 141 | // Fingerprints of the rest of the chain. |
| 142 | std::unique_ptr<rtc::SSLCertChain> chain = |
| 143 | info->certificate->ssl_certificate().GetChain(); |
| 144 | if (chain) { |
| 145 | for (size_t i = 0; i < chain->GetSize(); i++) { |
| 146 | fp.reset(rtc::SSLFingerprint::Create("sha-1", &chain->Get(i))); |
| 147 | EXPECT_TRUE(fp); |
| 148 | info->fingerprints.push_back(fp->GetRfc4572Fingerprint()); |
| 149 | } |
| 150 | } |
| 151 | EXPECT_EQ(info->ders.size(), info->fingerprints.size()); |
| 152 | return info; |
| 153 | } |
| 154 | |
hbos | ab9f6e4 | 2016-10-07 09:18:47 | [diff] [blame] | 155 | std::unique_ptr<cricket::Candidate> CreateFakeCandidate( |
| 156 | const std::string& hostname, |
| 157 | int port, |
| 158 | const std::string& protocol, |
| 159 | const std::string& candidate_type, |
| 160 | uint32_t priority) { |
| 161 | std::unique_ptr<cricket::Candidate> candidate(new cricket::Candidate()); |
| 162 | candidate->set_address(rtc::SocketAddress(hostname, port)); |
| 163 | candidate->set_protocol(protocol); |
| 164 | candidate->set_type(candidate_type); |
| 165 | candidate->set_priority(priority); |
| 166 | return candidate; |
| 167 | } |
| 168 | |
hbos | 09bc128 | 2016-11-08 14:29:22 | [diff] [blame] | 169 | class FakeAudioProcessorForStats |
| 170 | : public rtc::RefCountedObject<AudioProcessorInterface> { |
| 171 | public: |
| 172 | FakeAudioProcessorForStats( |
| 173 | AudioProcessorInterface::AudioProcessorStats stats) |
| 174 | : stats_(stats) { |
| 175 | } |
| 176 | |
| 177 | void GetStats(AudioProcessorInterface::AudioProcessorStats* stats) override { |
| 178 | *stats = stats_; |
| 179 | } |
| 180 | |
| 181 | private: |
| 182 | AudioProcessorInterface::AudioProcessorStats stats_; |
| 183 | }; |
| 184 | |
| 185 | class FakeAudioTrackForStats |
| 186 | : public MediaStreamTrack<AudioTrackInterface> { |
| 187 | public: |
| 188 | static rtc::scoped_refptr<FakeAudioTrackForStats> Create( |
| 189 | const std::string& id, |
| 190 | MediaStreamTrackInterface::TrackState state, |
| 191 | int signal_level, |
| 192 | rtc::scoped_refptr<FakeAudioProcessorForStats> processor) { |
| 193 | rtc::scoped_refptr<FakeAudioTrackForStats> audio_track_stats( |
| 194 | new rtc::RefCountedObject<FakeAudioTrackForStats>( |
| 195 | id, signal_level, processor)); |
| 196 | audio_track_stats->set_state(state); |
| 197 | return audio_track_stats; |
| 198 | } |
| 199 | |
| 200 | FakeAudioTrackForStats( |
| 201 | const std::string& id, |
| 202 | int signal_level, |
| 203 | rtc::scoped_refptr<FakeAudioProcessorForStats> processor) |
| 204 | : MediaStreamTrack<AudioTrackInterface>(id), |
| 205 | signal_level_(signal_level), |
| 206 | processor_(processor) { |
| 207 | } |
| 208 | |
| 209 | std::string kind() const override { |
| 210 | return MediaStreamTrackInterface::kAudioKind; |
| 211 | } |
| 212 | webrtc::AudioSourceInterface* GetSource() const override { return nullptr; } |
| 213 | void AddSink(webrtc::AudioTrackSinkInterface* sink) override {} |
| 214 | void RemoveSink(webrtc::AudioTrackSinkInterface* sink) override {} |
| 215 | bool GetSignalLevel(int* level) override { |
| 216 | *level = signal_level_; |
| 217 | return true; |
| 218 | } |
| 219 | rtc::scoped_refptr<AudioProcessorInterface> GetAudioProcessor() override { |
| 220 | return processor_; |
| 221 | } |
| 222 | |
| 223 | private: |
| 224 | int signal_level_; |
| 225 | rtc::scoped_refptr<FakeAudioProcessorForStats> processor_; |
| 226 | }; |
| 227 | |
| 228 | class FakeVideoTrackSourceForStats |
| 229 | : public rtc::RefCountedObject<VideoTrackSourceInterface> { |
| 230 | public: |
| 231 | FakeVideoTrackSourceForStats(VideoTrackSourceInterface::Stats stats) |
| 232 | : stats_(stats) { |
| 233 | } |
| 234 | |
| 235 | MediaSourceInterface::SourceState state() const override { |
| 236 | return MediaSourceInterface::kLive; |
| 237 | } |
| 238 | bool remote() const override { return false; } |
| 239 | void RegisterObserver(ObserverInterface* observer) override {} |
| 240 | void UnregisterObserver(ObserverInterface* observer) override {} |
nisse | acd935b | 2016-11-11 11:55:13 | [diff] [blame] | 241 | void AddOrUpdateSink(rtc::VideoSinkInterface<webrtc::VideoFrame>* sink, |
hbos | 09bc128 | 2016-11-08 14:29:22 | [diff] [blame] | 242 | const rtc::VideoSinkWants& wants) override {} |
nisse | acd935b | 2016-11-11 11:55:13 | [diff] [blame] | 243 | void RemoveSink(rtc::VideoSinkInterface<webrtc::VideoFrame>* sink) override { |
hbos | 09bc128 | 2016-11-08 14:29:22 | [diff] [blame] | 244 | } |
| 245 | bool is_screencast() const override { return false; } |
| 246 | rtc::Optional<bool> needs_denoising() const override { |
| 247 | return rtc::Optional<bool>(); |
| 248 | } |
| 249 | bool GetStats(VideoTrackSourceInterface::Stats* stats) override { |
| 250 | *stats = stats_; |
| 251 | return true; |
| 252 | } |
| 253 | |
| 254 | private: |
| 255 | VideoTrackSourceInterface::Stats stats_; |
| 256 | }; |
| 257 | |
| 258 | class FakeVideoTrackForStats |
| 259 | : public MediaStreamTrack<VideoTrackInterface> { |
| 260 | public: |
| 261 | static rtc::scoped_refptr<FakeVideoTrackForStats> Create( |
| 262 | const std::string& id, |
| 263 | MediaStreamTrackInterface::TrackState state, |
| 264 | rtc::scoped_refptr<VideoTrackSourceInterface> source) { |
| 265 | rtc::scoped_refptr<FakeVideoTrackForStats> video_track( |
| 266 | new rtc::RefCountedObject<FakeVideoTrackForStats>(id, source)); |
| 267 | video_track->set_state(state); |
| 268 | return video_track; |
| 269 | } |
| 270 | |
| 271 | FakeVideoTrackForStats( |
| 272 | const std::string& id, |
| 273 | rtc::scoped_refptr<VideoTrackSourceInterface> source) |
| 274 | : MediaStreamTrack<VideoTrackInterface>(id), |
| 275 | source_(source) { |
| 276 | } |
| 277 | |
| 278 | std::string kind() const override { |
| 279 | return MediaStreamTrackInterface::kVideoKind; |
| 280 | } |
| 281 | VideoTrackSourceInterface* GetSource() const override { |
| 282 | return source_; |
| 283 | } |
| 284 | |
| 285 | private: |
| 286 | rtc::scoped_refptr<VideoTrackSourceInterface> source_; |
| 287 | }; |
| 288 | |
hbos | c82f2e1 | 2016-09-05 08:36:50 | [diff] [blame] | 289 | class RTCStatsCollectorTestHelper : public SetSessionDescriptionObserver { |
hbos | d565b73 | 2016-08-30 21:04:35 | [diff] [blame] | 290 | public: |
hbos | c82f2e1 | 2016-09-05 08:36:50 | [diff] [blame] | 291 | RTCStatsCollectorTestHelper() |
hbos | d565b73 | 2016-08-30 21:04:35 | [diff] [blame] | 292 | : worker_thread_(rtc::Thread::Current()), |
| 293 | network_thread_(rtc::Thread::Current()), |
hbos | 6ded190 | 2016-11-01 08:50:46 | [diff] [blame] | 294 | media_engine_(new cricket::FakeMediaEngine()), |
skvlad | 11a9cbf | 2016-10-07 18:53:05 | [diff] [blame] | 295 | channel_manager_( |
hbos | 6ded190 | 2016-11-01 08:50:46 | [diff] [blame] | 296 | new cricket::ChannelManager(media_engine_, |
skvlad | 11a9cbf | 2016-10-07 18:53:05 | [diff] [blame] | 297 | worker_thread_, |
| 298 | network_thread_)), |
hbos | d565b73 | 2016-08-30 21:04:35 | [diff] [blame] | 299 | media_controller_( |
| 300 | MediaControllerInterface::Create(cricket::MediaConfig(), |
| 301 | worker_thread_, |
skvlad | 11a9cbf | 2016-10-07 18:53:05 | [diff] [blame] | 302 | channel_manager_.get(), |
| 303 | &event_log_)), |
hbos | d565b73 | 2016-08-30 21:04:35 | [diff] [blame] | 304 | session_(media_controller_.get()), |
| 305 | pc_() { |
hbos | 6ab97ce0 | 2016-10-03 21:16:56 | [diff] [blame] | 306 | // Default return values for mocks. |
hbos | 09bc128 | 2016-11-08 14:29:22 | [diff] [blame] | 307 | EXPECT_CALL(pc_, local_streams()).WillRepeatedly(Return(nullptr)); |
| 308 | EXPECT_CALL(pc_, remote_streams()).WillRepeatedly(Return(nullptr)); |
hbos | d565b73 | 2016-08-30 21:04:35 | [diff] [blame] | 309 | EXPECT_CALL(pc_, session()).WillRepeatedly(Return(&session_)); |
| 310 | EXPECT_CALL(pc_, sctp_data_channels()).WillRepeatedly( |
| 311 | ReturnRef(data_channels_)); |
hbos | 6ded190 | 2016-11-01 08:50:46 | [diff] [blame] | 312 | EXPECT_CALL(session_, video_channel()).WillRepeatedly(ReturnNull()); |
| 313 | EXPECT_CALL(session_, voice_channel()).WillRepeatedly(ReturnNull()); |
hbos | 6ab97ce0 | 2016-10-03 21:16:56 | [diff] [blame] | 314 | EXPECT_CALL(session_, GetTransportStats(_)).WillRepeatedly(Return(false)); |
hbos | ab9f6e4 | 2016-10-07 09:18:47 | [diff] [blame] | 315 | EXPECT_CALL(session_, GetLocalCertificate(_, _)).WillRepeatedly( |
| 316 | Return(false)); |
| 317 | EXPECT_CALL(session_, GetRemoteSSLCertificate_ReturnsRawPointer(_)) |
| 318 | .WillRepeatedly(Return(nullptr)); |
hbos | d565b73 | 2016-08-30 21:04:35 | [diff] [blame] | 319 | } |
| 320 | |
hbos | fdafab8 | 2016-09-14 13:02:13 | [diff] [blame] | 321 | rtc::ScopedFakeClock& fake_clock() { return fake_clock_; } |
hbos | 6ded190 | 2016-11-01 08:50:46 | [diff] [blame] | 322 | rtc::Thread* worker_thread() { return worker_thread_; } |
| 323 | rtc::Thread* network_thread() { return network_thread_; } |
| 324 | cricket::FakeMediaEngine* media_engine() { return media_engine_; } |
hbos | d565b73 | 2016-08-30 21:04:35 | [diff] [blame] | 325 | MockWebRtcSession& session() { return session_; } |
| 326 | MockPeerConnection& pc() { return pc_; } |
| 327 | std::vector<rtc::scoped_refptr<DataChannel>>& data_channels() { |
| 328 | return data_channels_; |
| 329 | } |
| 330 | |
| 331 | // SetSessionDescriptionObserver overrides. |
| 332 | void OnSuccess() override {} |
| 333 | void OnFailure(const std::string& error) override { |
| 334 | RTC_NOTREACHED() << error; |
| 335 | } |
| 336 | |
| 337 | private: |
hbos | fdafab8 | 2016-09-14 13:02:13 | [diff] [blame] | 338 | rtc::ScopedFakeClock fake_clock_; |
hbos | 09bc128 | 2016-11-08 14:29:22 | [diff] [blame] | 339 | RtcEventLogNullImpl event_log_; |
hbos | d565b73 | 2016-08-30 21:04:35 | [diff] [blame] | 340 | rtc::Thread* const worker_thread_; |
| 341 | rtc::Thread* const network_thread_; |
hbos | 6ded190 | 2016-11-01 08:50:46 | [diff] [blame] | 342 | cricket::FakeMediaEngine* media_engine_; |
hbos | d565b73 | 2016-08-30 21:04:35 | [diff] [blame] | 343 | std::unique_ptr<cricket::ChannelManager> channel_manager_; |
hbos | 09bc128 | 2016-11-08 14:29:22 | [diff] [blame] | 344 | std::unique_ptr<MediaControllerInterface> media_controller_; |
hbos | d565b73 | 2016-08-30 21:04:35 | [diff] [blame] | 345 | MockWebRtcSession session_; |
| 346 | MockPeerConnection pc_; |
| 347 | |
| 348 | std::vector<rtc::scoped_refptr<DataChannel>> data_channels_; |
| 349 | }; |
| 350 | |
hbos | c82f2e1 | 2016-09-05 08:36:50 | [diff] [blame] | 351 | class RTCTestStats : public RTCStats { |
hbos | d565b73 | 2016-08-30 21:04:35 | [diff] [blame] | 352 | public: |
hbos | fc5e050 | 2016-10-06 09:06:10 | [diff] [blame] | 353 | WEBRTC_RTCSTATS_DECL(); |
| 354 | |
hbos | c82f2e1 | 2016-09-05 08:36:50 | [diff] [blame] | 355 | RTCTestStats(const std::string& id, int64_t timestamp_us) |
| 356 | : RTCStats(id, timestamp_us), |
| 357 | dummy_stat("dummyStat") {} |
| 358 | |
hbos | c82f2e1 | 2016-09-05 08:36:50 | [diff] [blame] | 359 | RTCStatsMember<int32_t> dummy_stat; |
| 360 | }; |
| 361 | |
hbos | fc5e050 | 2016-10-06 09:06:10 | [diff] [blame] | 362 | WEBRTC_RTCSTATS_IMPL(RTCTestStats, RTCStats, "test-stats", |
| 363 | &dummy_stat); |
hbos | c82f2e1 | 2016-09-05 08:36:50 | [diff] [blame] | 364 | |
| 365 | // Overrides the stats collection to verify thread usage and that the resulting |
| 366 | // partial reports are merged. |
| 367 | class FakeRTCStatsCollector : public RTCStatsCollector, |
| 368 | public RTCStatsCollectorCallback { |
| 369 | public: |
| 370 | static rtc::scoped_refptr<FakeRTCStatsCollector> Create( |
| 371 | PeerConnection* pc, |
| 372 | int64_t cache_lifetime_us) { |
| 373 | return rtc::scoped_refptr<FakeRTCStatsCollector>( |
| 374 | new rtc::RefCountedObject<FakeRTCStatsCollector>( |
| 375 | pc, cache_lifetime_us)); |
| 376 | } |
| 377 | |
| 378 | // RTCStatsCollectorCallback implementation. |
| 379 | void OnStatsDelivered( |
| 380 | const rtc::scoped_refptr<const RTCStatsReport>& report) override { |
| 381 | EXPECT_TRUE(signaling_thread_->IsCurrent()); |
| 382 | rtc::CritScope cs(&lock_); |
| 383 | delivered_report_ = report; |
| 384 | } |
| 385 | |
| 386 | void VerifyThreadUsageAndResultsMerging() { |
| 387 | GetStatsReport(rtc::scoped_refptr<RTCStatsCollectorCallback>(this)); |
| 388 | EXPECT_TRUE_WAIT(HasVerifiedResults(), kGetStatsReportTimeoutMs); |
| 389 | } |
| 390 | |
| 391 | bool HasVerifiedResults() { |
| 392 | EXPECT_TRUE(signaling_thread_->IsCurrent()); |
| 393 | rtc::CritScope cs(&lock_); |
| 394 | if (!delivered_report_) |
| 395 | return false; |
| 396 | EXPECT_EQ(produced_on_signaling_thread_, 1); |
| 397 | EXPECT_EQ(produced_on_worker_thread_, 1); |
| 398 | EXPECT_EQ(produced_on_network_thread_, 1); |
| 399 | |
| 400 | EXPECT_TRUE(delivered_report_->Get("SignalingThreadStats")); |
| 401 | EXPECT_TRUE(delivered_report_->Get("WorkerThreadStats")); |
| 402 | EXPECT_TRUE(delivered_report_->Get("NetworkThreadStats")); |
| 403 | |
| 404 | produced_on_signaling_thread_ = 0; |
| 405 | produced_on_worker_thread_ = 0; |
| 406 | produced_on_network_thread_ = 0; |
| 407 | delivered_report_ = nullptr; |
| 408 | return true; |
hbos | d565b73 | 2016-08-30 21:04:35 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | protected: |
hbos | c82f2e1 | 2016-09-05 08:36:50 | [diff] [blame] | 412 | FakeRTCStatsCollector( |
| 413 | PeerConnection* pc, |
| 414 | int64_t cache_lifetime) |
| 415 | : RTCStatsCollector(pc, cache_lifetime), |
| 416 | signaling_thread_(pc->session()->signaling_thread()), |
| 417 | worker_thread_(pc->session()->worker_thread()), |
| 418 | network_thread_(pc->session()->network_thread()) { |
| 419 | } |
| 420 | |
| 421 | void ProducePartialResultsOnSignalingThread(int64_t timestamp_us) override { |
| 422 | EXPECT_TRUE(signaling_thread_->IsCurrent()); |
| 423 | { |
| 424 | rtc::CritScope cs(&lock_); |
| 425 | EXPECT_FALSE(delivered_report_); |
| 426 | ++produced_on_signaling_thread_; |
| 427 | } |
| 428 | |
| 429 | rtc::scoped_refptr<RTCStatsReport> signaling_report = |
hbos | 6ded190 | 2016-11-01 08:50:46 | [diff] [blame] | 430 | RTCStatsReport::Create(0); |
hbos | c82f2e1 | 2016-09-05 08:36:50 | [diff] [blame] | 431 | signaling_report->AddStats(std::unique_ptr<const RTCStats>( |
| 432 | new RTCTestStats("SignalingThreadStats", timestamp_us))); |
| 433 | AddPartialResults(signaling_report); |
| 434 | } |
| 435 | void ProducePartialResultsOnWorkerThread(int64_t timestamp_us) override { |
| 436 | EXPECT_TRUE(worker_thread_->IsCurrent()); |
| 437 | { |
| 438 | rtc::CritScope cs(&lock_); |
| 439 | EXPECT_FALSE(delivered_report_); |
| 440 | ++produced_on_worker_thread_; |
| 441 | } |
| 442 | |
hbos | 6ded190 | 2016-11-01 08:50:46 | [diff] [blame] | 443 | rtc::scoped_refptr<RTCStatsReport> worker_report = |
| 444 | RTCStatsReport::Create(0); |
hbos | c82f2e1 | 2016-09-05 08:36:50 | [diff] [blame] | 445 | worker_report->AddStats(std::unique_ptr<const RTCStats>( |
| 446 | new RTCTestStats("WorkerThreadStats", timestamp_us))); |
| 447 | AddPartialResults(worker_report); |
| 448 | } |
| 449 | void ProducePartialResultsOnNetworkThread(int64_t timestamp_us) override { |
| 450 | EXPECT_TRUE(network_thread_->IsCurrent()); |
| 451 | { |
| 452 | rtc::CritScope cs(&lock_); |
| 453 | EXPECT_FALSE(delivered_report_); |
| 454 | ++produced_on_network_thread_; |
| 455 | } |
| 456 | |
| 457 | rtc::scoped_refptr<RTCStatsReport> network_report = |
hbos | 6ded190 | 2016-11-01 08:50:46 | [diff] [blame] | 458 | RTCStatsReport::Create(0); |
hbos | c82f2e1 | 2016-09-05 08:36:50 | [diff] [blame] | 459 | network_report->AddStats(std::unique_ptr<const RTCStats>( |
| 460 | new RTCTestStats("NetworkThreadStats", timestamp_us))); |
| 461 | AddPartialResults(network_report); |
| 462 | } |
| 463 | |
| 464 | private: |
| 465 | rtc::Thread* const signaling_thread_; |
| 466 | rtc::Thread* const worker_thread_; |
| 467 | rtc::Thread* const network_thread_; |
| 468 | |
| 469 | rtc::CriticalSection lock_; |
| 470 | rtc::scoped_refptr<const RTCStatsReport> delivered_report_; |
| 471 | int produced_on_signaling_thread_ = 0; |
| 472 | int produced_on_worker_thread_ = 0; |
| 473 | int produced_on_network_thread_ = 0; |
hbos | d565b73 | 2016-08-30 21:04:35 | [diff] [blame] | 474 | }; |
| 475 | |
hbos | c82f2e1 | 2016-09-05 08:36:50 | [diff] [blame] | 476 | class RTCStatsCollectorTest : public testing::Test { |
| 477 | public: |
| 478 | RTCStatsCollectorTest() |
| 479 | : test_(new rtc::RefCountedObject<RTCStatsCollectorTestHelper>()), |
| 480 | collector_(RTCStatsCollector::Create( |
| 481 | &test_->pc(), 50 * rtc::kNumMicrosecsPerMillisec)) { |
| 482 | } |
| 483 | |
| 484 | rtc::scoped_refptr<const RTCStatsReport> GetStatsReport() { |
hbos | db346a7 | 2016-11-29 09:57:01 | [diff] [blame] | 485 | rtc::scoped_refptr<RTCStatsObtainer> callback = RTCStatsObtainer::Create(); |
hbos | c82f2e1 | 2016-09-05 08:36:50 | [diff] [blame] | 486 | collector_->GetStatsReport(callback); |
| 487 | EXPECT_TRUE_WAIT(callback->report(), kGetStatsReportTimeoutMs); |
hbos | cc555c5 | 2016-10-18 19:48:31 | [diff] [blame] | 488 | int64_t after = rtc::TimeUTCMicros(); |
| 489 | for (const RTCStats& stats : *callback->report()) { |
| 490 | EXPECT_LE(stats.timestamp_us(), after); |
| 491 | } |
hbos | c82f2e1 | 2016-09-05 08:36:50 | [diff] [blame] | 492 | return callback->report(); |
| 493 | } |
| 494 | |
hbos | c47a0c3 | 2016-10-11 21:54:49 | [diff] [blame] | 495 | const RTCIceCandidateStats* ExpectReportContainsCandidate( |
hbos | ab9f6e4 | 2016-10-07 09:18:47 | [diff] [blame] | 496 | const rtc::scoped_refptr<const RTCStatsReport>& report, |
| 497 | const cricket::Candidate& candidate, |
| 498 | bool is_local) { |
hbos | c47a0c3 | 2016-10-11 21:54:49 | [diff] [blame] | 499 | const RTCStats* stats = report->Get("RTCIceCandidate_" + candidate.id()); |
hbos | ab9f6e4 | 2016-10-07 09:18:47 | [diff] [blame] | 500 | EXPECT_TRUE(stats); |
| 501 | const RTCIceCandidateStats* candidate_stats; |
| 502 | if (is_local) |
| 503 | candidate_stats = &stats->cast_to<RTCLocalIceCandidateStats>(); |
| 504 | else |
| 505 | candidate_stats = &stats->cast_to<RTCRemoteIceCandidateStats>(); |
| 506 | EXPECT_EQ(*candidate_stats->ip, candidate.address().ipaddr().ToString()); |
| 507 | EXPECT_EQ(*candidate_stats->port, |
| 508 | static_cast<int32_t>(candidate.address().port())); |
| 509 | EXPECT_EQ(*candidate_stats->protocol, candidate.protocol()); |
| 510 | EXPECT_EQ(*candidate_stats->candidate_type, |
hbos | cc555c5 | 2016-10-18 19:48:31 | [diff] [blame] | 511 | CandidateTypeToRTCIceCandidateTypeForTesting(candidate.type())); |
hbos | ab9f6e4 | 2016-10-07 09:18:47 | [diff] [blame] | 512 | EXPECT_EQ(*candidate_stats->priority, |
| 513 | static_cast<int32_t>(candidate.priority())); |
| 514 | // TODO(hbos): Define candidate_stats->url. crbug.com/632723 |
| 515 | EXPECT_FALSE(candidate_stats->url.is_defined()); |
hbos | c47a0c3 | 2016-10-11 21:54:49 | [diff] [blame] | 516 | return candidate_stats; |
| 517 | } |
| 518 | |
hbos | 6ab97ce0 | 2016-10-03 21:16:56 | [diff] [blame] | 519 | void ExpectReportContainsCertificateInfo( |
| 520 | const rtc::scoped_refptr<const RTCStatsReport>& report, |
| 521 | const CertificateInfo& cert_info) { |
| 522 | for (size_t i = 0; i < cert_info.fingerprints.size(); ++i) { |
| 523 | const RTCStats* stats = report->Get( |
| 524 | "RTCCertificate_" + cert_info.fingerprints[i]); |
hbos | 2fa7c67 | 2016-10-24 11:00:05 | [diff] [blame] | 525 | ASSERT_TRUE(stats); |
hbos | 6ab97ce0 | 2016-10-03 21:16:56 | [diff] [blame] | 526 | const RTCCertificateStats& cert_stats = |
| 527 | stats->cast_to<const RTCCertificateStats>(); |
| 528 | EXPECT_EQ(*cert_stats.fingerprint, cert_info.fingerprints[i]); |
| 529 | EXPECT_EQ(*cert_stats.fingerprint_algorithm, "sha-1"); |
| 530 | EXPECT_EQ(*cert_stats.base64_certificate, cert_info.pems[i]); |
| 531 | if (i + 1 < cert_info.fingerprints.size()) { |
| 532 | EXPECT_EQ(*cert_stats.issuer_certificate_id, |
| 533 | "RTCCertificate_" + cert_info.fingerprints[i + 1]); |
| 534 | } else { |
| 535 | EXPECT_FALSE(cert_stats.issuer_certificate_id.is_defined()); |
| 536 | } |
| 537 | } |
| 538 | } |
| 539 | |
hbos | cc555c5 | 2016-10-18 19:48:31 | [diff] [blame] | 540 | void ExpectReportContainsDataChannel( |
| 541 | const rtc::scoped_refptr<const RTCStatsReport>& report, |
| 542 | const DataChannel& data_channel) { |
| 543 | const RTCStats* stats = report->Get("RTCDataChannel_" + |
| 544 | rtc::ToString<>(data_channel.id())); |
| 545 | EXPECT_TRUE(stats); |
| 546 | const RTCDataChannelStats& data_channel_stats = |
| 547 | stats->cast_to<const RTCDataChannelStats>(); |
| 548 | EXPECT_EQ(*data_channel_stats.label, data_channel.label()); |
| 549 | EXPECT_EQ(*data_channel_stats.protocol, data_channel.protocol()); |
| 550 | EXPECT_EQ(*data_channel_stats.datachannelid, data_channel.id()); |
| 551 | EXPECT_EQ(*data_channel_stats.state, |
| 552 | DataStateToRTCDataChannelStateForTesting(data_channel.state())); |
| 553 | EXPECT_EQ(*data_channel_stats.messages_sent, data_channel.messages_sent()); |
| 554 | EXPECT_EQ(*data_channel_stats.bytes_sent, data_channel.bytes_sent()); |
| 555 | EXPECT_EQ(*data_channel_stats.messages_received, |
| 556 | data_channel.messages_received()); |
| 557 | EXPECT_EQ(*data_channel_stats.bytes_received, |
| 558 | data_channel.bytes_received()); |
| 559 | } |
| 560 | |
hbos | c82f2e1 | 2016-09-05 08:36:50 | [diff] [blame] | 561 | protected: |
| 562 | rtc::scoped_refptr<RTCStatsCollectorTestHelper> test_; |
| 563 | rtc::scoped_refptr<RTCStatsCollector> collector_; |
| 564 | }; |
| 565 | |
| 566 | TEST_F(RTCStatsCollectorTest, SingleCallback) { |
| 567 | rtc::scoped_refptr<const RTCStatsReport> result; |
hbos | db346a7 | 2016-11-29 09:57:01 | [diff] [blame] | 568 | collector_->GetStatsReport(RTCStatsObtainer::Create(&result)); |
hbos | c82f2e1 | 2016-09-05 08:36:50 | [diff] [blame] | 569 | EXPECT_TRUE_WAIT(result, kGetStatsReportTimeoutMs); |
| 570 | } |
| 571 | |
| 572 | TEST_F(RTCStatsCollectorTest, MultipleCallbacks) { |
| 573 | rtc::scoped_refptr<const RTCStatsReport> a; |
| 574 | rtc::scoped_refptr<const RTCStatsReport> b; |
| 575 | rtc::scoped_refptr<const RTCStatsReport> c; |
hbos | db346a7 | 2016-11-29 09:57:01 | [diff] [blame] | 576 | collector_->GetStatsReport(RTCStatsObtainer::Create(&a)); |
| 577 | collector_->GetStatsReport(RTCStatsObtainer::Create(&b)); |
| 578 | collector_->GetStatsReport(RTCStatsObtainer::Create(&c)); |
hbos | c82f2e1 | 2016-09-05 08:36:50 | [diff] [blame] | 579 | EXPECT_TRUE_WAIT(a, kGetStatsReportTimeoutMs); |
| 580 | EXPECT_TRUE_WAIT(b, kGetStatsReportTimeoutMs); |
| 581 | EXPECT_TRUE_WAIT(c, kGetStatsReportTimeoutMs); |
| 582 | EXPECT_EQ(a.get(), b.get()); |
| 583 | EXPECT_EQ(b.get(), c.get()); |
| 584 | } |
| 585 | |
| 586 | TEST_F(RTCStatsCollectorTest, CachedStatsReports) { |
hbos | d565b73 | 2016-08-30 21:04:35 | [diff] [blame] | 587 | // Caching should ensure |a| and |b| are the same report. |
hbos | c82f2e1 | 2016-09-05 08:36:50 | [diff] [blame] | 588 | rtc::scoped_refptr<const RTCStatsReport> a = GetStatsReport(); |
| 589 | rtc::scoped_refptr<const RTCStatsReport> b = GetStatsReport(); |
hbos | d565b73 | 2016-08-30 21:04:35 | [diff] [blame] | 590 | EXPECT_EQ(a.get(), b.get()); |
| 591 | // Invalidate cache by clearing it. |
hbos | c82f2e1 | 2016-09-05 08:36:50 | [diff] [blame] | 592 | collector_->ClearCachedStatsReport(); |
| 593 | rtc::scoped_refptr<const RTCStatsReport> c = GetStatsReport(); |
hbos | d565b73 | 2016-08-30 21:04:35 | [diff] [blame] | 594 | EXPECT_NE(b.get(), c.get()); |
| 595 | // Invalidate cache by advancing time. |
hbos | fdafab8 | 2016-09-14 13:02:13 | [diff] [blame] | 596 | test_->fake_clock().AdvanceTime(rtc::TimeDelta::FromMilliseconds(51)); |
hbos | c82f2e1 | 2016-09-05 08:36:50 | [diff] [blame] | 597 | rtc::scoped_refptr<const RTCStatsReport> d = GetStatsReport(); |
hbos | d565b73 | 2016-08-30 21:04:35 | [diff] [blame] | 598 | EXPECT_TRUE(d); |
| 599 | EXPECT_NE(c.get(), d.get()); |
| 600 | } |
| 601 | |
hbos | c82f2e1 | 2016-09-05 08:36:50 | [diff] [blame] | 602 | TEST_F(RTCStatsCollectorTest, MultipleCallbacksWithInvalidatedCacheInBetween) { |
hbos | c82f2e1 | 2016-09-05 08:36:50 | [diff] [blame] | 603 | rtc::scoped_refptr<const RTCStatsReport> a; |
| 604 | rtc::scoped_refptr<const RTCStatsReport> b; |
| 605 | rtc::scoped_refptr<const RTCStatsReport> c; |
hbos | db346a7 | 2016-11-29 09:57:01 | [diff] [blame] | 606 | collector_->GetStatsReport(RTCStatsObtainer::Create(&a)); |
| 607 | collector_->GetStatsReport(RTCStatsObtainer::Create(&b)); |
hbos | c82f2e1 | 2016-09-05 08:36:50 | [diff] [blame] | 608 | // Cache is invalidated after 50 ms. |
hbos | fdafab8 | 2016-09-14 13:02:13 | [diff] [blame] | 609 | test_->fake_clock().AdvanceTime(rtc::TimeDelta::FromMilliseconds(51)); |
hbos | db346a7 | 2016-11-29 09:57:01 | [diff] [blame] | 610 | collector_->GetStatsReport(RTCStatsObtainer::Create(&c)); |
hbos | c82f2e1 | 2016-09-05 08:36:50 | [diff] [blame] | 611 | EXPECT_TRUE_WAIT(a, kGetStatsReportTimeoutMs); |
| 612 | EXPECT_TRUE_WAIT(b, kGetStatsReportTimeoutMs); |
| 613 | EXPECT_TRUE_WAIT(c, kGetStatsReportTimeoutMs); |
| 614 | EXPECT_EQ(a.get(), b.get()); |
| 615 | // The act of doing |AdvanceTime| processes all messages. If this was not the |
| 616 | // case we might not require |c| to be fresher than |b|. |
| 617 | EXPECT_NE(c.get(), b.get()); |
| 618 | } |
| 619 | |
hbos | 6ab97ce0 | 2016-10-03 21:16:56 | [diff] [blame] | 620 | TEST_F(RTCStatsCollectorTest, CollectRTCCertificateStatsSingle) { |
| 621 | std::unique_ptr<CertificateInfo> local_certinfo = |
| 622 | CreateFakeCertificateAndInfoFromDers( |
| 623 | std::vector<std::string>({ "(local) single certificate" })); |
| 624 | std::unique_ptr<CertificateInfo> remote_certinfo = |
| 625 | CreateFakeCertificateAndInfoFromDers( |
| 626 | std::vector<std::string>({ "(remote) single certificate" })); |
| 627 | |
| 628 | // Mock the session to return the local and remote certificates. |
| 629 | EXPECT_CALL(test_->session(), GetTransportStats(_)).WillRepeatedly(Invoke( |
| 630 | [this](SessionStats* stats) { |
| 631 | stats->transport_stats["transport"].transport_name = "transport"; |
| 632 | return true; |
| 633 | })); |
| 634 | EXPECT_CALL(test_->session(), GetLocalCertificate(_, _)).WillRepeatedly( |
| 635 | Invoke([this, &local_certinfo](const std::string& transport_name, |
| 636 | rtc::scoped_refptr<rtc::RTCCertificate>* certificate) { |
| 637 | if (transport_name == "transport") { |
| 638 | *certificate = local_certinfo->certificate; |
| 639 | return true; |
| 640 | } |
| 641 | return false; |
| 642 | })); |
| 643 | EXPECT_CALL(test_->session(), |
| 644 | GetRemoteSSLCertificate_ReturnsRawPointer(_)).WillRepeatedly(Invoke( |
| 645 | [this, &remote_certinfo](const std::string& transport_name) { |
| 646 | if (transport_name == "transport") |
| 647 | return remote_certinfo->certificate->ssl_certificate().GetReference(); |
| 648 | return static_cast<rtc::SSLCertificate*>(nullptr); |
| 649 | })); |
| 650 | |
| 651 | rtc::scoped_refptr<const RTCStatsReport> report = GetStatsReport(); |
| 652 | ExpectReportContainsCertificateInfo(report, *local_certinfo.get()); |
| 653 | ExpectReportContainsCertificateInfo(report, *remote_certinfo.get()); |
| 654 | } |
| 655 | |
hbos | 0adb828 | 2016-11-23 10:32:06 | [diff] [blame] | 656 | TEST_F(RTCStatsCollectorTest, CollectRTCCodecStats) { |
| 657 | MockVoiceMediaChannel* voice_media_channel = new MockVoiceMediaChannel(); |
| 658 | cricket::VoiceChannel voice_channel( |
| 659 | test_->worker_thread(), test_->network_thread(), test_->media_engine(), |
| 660 | voice_media_channel, nullptr, "VoiceContentName", false); |
| 661 | |
| 662 | MockVideoMediaChannel* video_media_channel = new MockVideoMediaChannel(); |
| 663 | cricket::VideoChannel video_channel( |
| 664 | test_->worker_thread(), test_->network_thread(), video_media_channel, |
| 665 | nullptr, "VideoContentName", false); |
| 666 | |
| 667 | // Audio |
| 668 | cricket::VoiceMediaInfo voice_media_info; |
| 669 | |
| 670 | RtpCodecParameters inbound_audio_codec; |
| 671 | inbound_audio_codec.payload_type = 1; |
| 672 | inbound_audio_codec.mime_type = "opus"; |
| 673 | inbound_audio_codec.clock_rate = 1337; |
| 674 | voice_media_info.receive_codecs.insert( |
| 675 | std::make_pair(inbound_audio_codec.payload_type, inbound_audio_codec)); |
| 676 | |
| 677 | RtpCodecParameters outbound_audio_codec; |
| 678 | outbound_audio_codec.payload_type = 2; |
| 679 | outbound_audio_codec.mime_type = "isac"; |
| 680 | outbound_audio_codec.clock_rate = 1338; |
| 681 | voice_media_info.send_codecs.insert( |
| 682 | std::make_pair(outbound_audio_codec.payload_type, outbound_audio_codec)); |
| 683 | |
| 684 | EXPECT_CALL(*voice_media_channel, GetStats(_)) |
| 685 | .WillOnce(DoAll(SetArgPointee<0>(voice_media_info), Return(true))); |
| 686 | |
| 687 | // Video |
| 688 | cricket::VideoMediaInfo video_media_info; |
| 689 | |
| 690 | RtpCodecParameters inbound_video_codec; |
| 691 | inbound_video_codec.payload_type = 3; |
| 692 | inbound_video_codec.mime_type = "H264"; |
| 693 | inbound_video_codec.clock_rate = 1339; |
| 694 | video_media_info.receive_codecs.insert( |
| 695 | std::make_pair(inbound_video_codec.payload_type, inbound_video_codec)); |
| 696 | |
| 697 | RtpCodecParameters outbound_video_codec; |
| 698 | outbound_video_codec.payload_type = 4; |
| 699 | outbound_video_codec.mime_type = "VP8"; |
| 700 | outbound_video_codec.clock_rate = 1340; |
| 701 | video_media_info.send_codecs.insert( |
| 702 | std::make_pair(outbound_video_codec.payload_type, outbound_video_codec)); |
| 703 | |
| 704 | EXPECT_CALL(*video_media_channel, GetStats(_)) |
| 705 | .WillOnce(DoAll(SetArgPointee<0>(video_media_info), Return(true))); |
| 706 | |
| 707 | SessionStats session_stats; |
| 708 | session_stats.proxy_to_transport["VoiceContentName"] = "TransportName"; |
| 709 | session_stats.proxy_to_transport["VideoContentName"] = "TransportName"; |
| 710 | session_stats.transport_stats["TransportName"].transport_name = |
| 711 | "TransportName"; |
| 712 | |
| 713 | EXPECT_CALL(test_->session(), GetTransportStats(_)) |
| 714 | .WillRepeatedly(DoAll(SetArgPointee<0>(session_stats), Return(true))); |
| 715 | EXPECT_CALL(test_->session(), voice_channel()) |
| 716 | .WillRepeatedly(Return(&voice_channel)); |
| 717 | EXPECT_CALL(test_->session(), video_channel()) |
| 718 | .WillRepeatedly(Return(&video_channel)); |
| 719 | |
| 720 | rtc::scoped_refptr<const RTCStatsReport> report = GetStatsReport(); |
| 721 | |
| 722 | RTCCodecStats expected_inbound_audio_codec( |
| 723 | "RTCCodec_InboundAudio_1", report->timestamp_us()); |
| 724 | expected_inbound_audio_codec.payload_type = 1; |
| 725 | expected_inbound_audio_codec.codec = "audio/opus"; |
| 726 | expected_inbound_audio_codec.clock_rate = 1337; |
| 727 | |
| 728 | RTCCodecStats expected_outbound_audio_codec( |
| 729 | "RTCCodec_OutboundAudio_2", report->timestamp_us()); |
| 730 | expected_outbound_audio_codec.payload_type = 2; |
| 731 | expected_outbound_audio_codec.codec = "audio/isac"; |
| 732 | expected_outbound_audio_codec.clock_rate = 1338; |
| 733 | |
| 734 | RTCCodecStats expected_inbound_video_codec( |
| 735 | "RTCCodec_InboundVideo_3", report->timestamp_us()); |
| 736 | expected_inbound_video_codec.payload_type = 3; |
| 737 | expected_inbound_video_codec.codec = "video/H264"; |
| 738 | expected_inbound_video_codec.clock_rate = 1339; |
| 739 | |
| 740 | RTCCodecStats expected_outbound_video_codec( |
| 741 | "RTCCodec_OutboundVideo_4", report->timestamp_us()); |
| 742 | expected_outbound_video_codec.payload_type = 4; |
| 743 | expected_outbound_video_codec.codec = "video/VP8"; |
| 744 | expected_outbound_video_codec.clock_rate = 1340; |
| 745 | |
| 746 | ASSERT(report->Get(expected_inbound_audio_codec.id())); |
| 747 | EXPECT_EQ(expected_inbound_audio_codec, |
| 748 | report->Get(expected_inbound_audio_codec.id())->cast_to< |
| 749 | RTCCodecStats>()); |
| 750 | |
| 751 | ASSERT(report->Get(expected_outbound_audio_codec.id())); |
| 752 | EXPECT_EQ(expected_outbound_audio_codec, |
| 753 | report->Get(expected_outbound_audio_codec.id())->cast_to< |
| 754 | RTCCodecStats>()); |
| 755 | |
| 756 | ASSERT(report->Get(expected_inbound_video_codec.id())); |
| 757 | EXPECT_EQ(expected_inbound_video_codec, |
| 758 | report->Get(expected_inbound_video_codec.id())->cast_to< |
| 759 | RTCCodecStats>()); |
| 760 | |
| 761 | ASSERT(report->Get(expected_outbound_video_codec.id())); |
| 762 | EXPECT_EQ(expected_outbound_video_codec, |
| 763 | report->Get(expected_outbound_video_codec.id())->cast_to< |
| 764 | RTCCodecStats>()); |
| 765 | } |
| 766 | |
hbos | 6ab97ce0 | 2016-10-03 21:16:56 | [diff] [blame] | 767 | TEST_F(RTCStatsCollectorTest, CollectRTCCertificateStatsMultiple) { |
| 768 | std::unique_ptr<CertificateInfo> audio_local_certinfo = |
| 769 | CreateFakeCertificateAndInfoFromDers( |
| 770 | std::vector<std::string>({ "(local) audio" })); |
| 771 | audio_local_certinfo = CreateFakeCertificateAndInfoFromDers( |
| 772 | audio_local_certinfo->ders); |
| 773 | std::unique_ptr<CertificateInfo> audio_remote_certinfo = |
| 774 | CreateFakeCertificateAndInfoFromDers( |
| 775 | std::vector<std::string>({ "(remote) audio" })); |
| 776 | audio_remote_certinfo = CreateFakeCertificateAndInfoFromDers( |
| 777 | audio_remote_certinfo->ders); |
| 778 | |
| 779 | std::unique_ptr<CertificateInfo> video_local_certinfo = |
| 780 | CreateFakeCertificateAndInfoFromDers( |
| 781 | std::vector<std::string>({ "(local) video" })); |
| 782 | video_local_certinfo = CreateFakeCertificateAndInfoFromDers( |
| 783 | video_local_certinfo->ders); |
| 784 | std::unique_ptr<CertificateInfo> video_remote_certinfo = |
| 785 | CreateFakeCertificateAndInfoFromDers( |
| 786 | std::vector<std::string>({ "(remote) video" })); |
| 787 | video_remote_certinfo = CreateFakeCertificateAndInfoFromDers( |
| 788 | video_remote_certinfo->ders); |
| 789 | |
| 790 | // Mock the session to return the local and remote certificates. |
| 791 | EXPECT_CALL(test_->session(), GetTransportStats(_)).WillRepeatedly(Invoke( |
| 792 | [this](SessionStats* stats) { |
| 793 | stats->transport_stats["audio"].transport_name = "audio"; |
| 794 | stats->transport_stats["video"].transport_name = "video"; |
| 795 | return true; |
| 796 | })); |
| 797 | EXPECT_CALL(test_->session(), GetLocalCertificate(_, _)).WillRepeatedly( |
| 798 | Invoke([this, &audio_local_certinfo, &video_local_certinfo]( |
| 799 | const std::string& transport_name, |
| 800 | rtc::scoped_refptr<rtc::RTCCertificate>* certificate) { |
| 801 | if (transport_name == "audio") { |
| 802 | *certificate = audio_local_certinfo->certificate; |
| 803 | return true; |
| 804 | } |
| 805 | if (transport_name == "video") { |
| 806 | *certificate = video_local_certinfo->certificate; |
| 807 | return true; |
| 808 | } |
| 809 | return false; |
| 810 | })); |
| 811 | EXPECT_CALL(test_->session(), |
| 812 | GetRemoteSSLCertificate_ReturnsRawPointer(_)).WillRepeatedly(Invoke( |
| 813 | [this, &audio_remote_certinfo, &video_remote_certinfo]( |
| 814 | const std::string& transport_name) { |
| 815 | if (transport_name == "audio") { |
| 816 | return audio_remote_certinfo->certificate->ssl_certificate() |
| 817 | .GetReference(); |
| 818 | } |
| 819 | if (transport_name == "video") { |
| 820 | return video_remote_certinfo->certificate->ssl_certificate() |
| 821 | .GetReference(); |
| 822 | } |
| 823 | return static_cast<rtc::SSLCertificate*>(nullptr); |
| 824 | })); |
| 825 | |
| 826 | rtc::scoped_refptr<const RTCStatsReport> report = GetStatsReport(); |
| 827 | ExpectReportContainsCertificateInfo(report, *audio_local_certinfo.get()); |
| 828 | ExpectReportContainsCertificateInfo(report, *audio_remote_certinfo.get()); |
| 829 | ExpectReportContainsCertificateInfo(report, *video_local_certinfo.get()); |
| 830 | ExpectReportContainsCertificateInfo(report, *video_remote_certinfo.get()); |
| 831 | } |
| 832 | |
| 833 | TEST_F(RTCStatsCollectorTest, CollectRTCCertificateStatsChain) { |
| 834 | std::vector<std::string> local_ders; |
| 835 | local_ders.push_back("(local) this"); |
| 836 | local_ders.push_back("(local) is"); |
| 837 | local_ders.push_back("(local) a"); |
| 838 | local_ders.push_back("(local) chain"); |
| 839 | std::unique_ptr<CertificateInfo> local_certinfo = |
| 840 | CreateFakeCertificateAndInfoFromDers(local_ders); |
| 841 | std::vector<std::string> remote_ders; |
| 842 | remote_ders.push_back("(remote) this"); |
| 843 | remote_ders.push_back("(remote) is"); |
| 844 | remote_ders.push_back("(remote) another"); |
| 845 | remote_ders.push_back("(remote) chain"); |
| 846 | std::unique_ptr<CertificateInfo> remote_certinfo = |
| 847 | CreateFakeCertificateAndInfoFromDers(remote_ders); |
| 848 | |
| 849 | // Mock the session to return the local and remote certificates. |
| 850 | EXPECT_CALL(test_->session(), GetTransportStats(_)).WillRepeatedly(Invoke( |
| 851 | [this](SessionStats* stats) { |
| 852 | stats->transport_stats["transport"].transport_name = "transport"; |
| 853 | return true; |
| 854 | })); |
| 855 | EXPECT_CALL(test_->session(), GetLocalCertificate(_, _)).WillRepeatedly( |
| 856 | Invoke([this, &local_certinfo](const std::string& transport_name, |
| 857 | rtc::scoped_refptr<rtc::RTCCertificate>* certificate) { |
| 858 | if (transport_name == "transport") { |
| 859 | *certificate = local_certinfo->certificate; |
| 860 | return true; |
| 861 | } |
| 862 | return false; |
| 863 | })); |
| 864 | EXPECT_CALL(test_->session(), |
| 865 | GetRemoteSSLCertificate_ReturnsRawPointer(_)).WillRepeatedly(Invoke( |
| 866 | [this, &remote_certinfo](const std::string& transport_name) { |
| 867 | if (transport_name == "transport") |
| 868 | return remote_certinfo->certificate->ssl_certificate().GetReference(); |
| 869 | return static_cast<rtc::SSLCertificate*>(nullptr); |
| 870 | })); |
| 871 | |
| 872 | rtc::scoped_refptr<const RTCStatsReport> report = GetStatsReport(); |
| 873 | ExpectReportContainsCertificateInfo(report, *local_certinfo.get()); |
| 874 | ExpectReportContainsCertificateInfo(report, *remote_certinfo.get()); |
| 875 | } |
| 876 | |
hbos | cc555c5 | 2016-10-18 19:48:31 | [diff] [blame] | 877 | TEST_F(RTCStatsCollectorTest, CollectRTCDataChannelStats) { |
| 878 | test_->data_channels().push_back( |
| 879 | new MockDataChannel(0, DataChannelInterface::kConnecting)); |
| 880 | test_->data_channels().push_back( |
| 881 | new MockDataChannel(1, DataChannelInterface::kOpen)); |
| 882 | test_->data_channels().push_back( |
| 883 | new MockDataChannel(2, DataChannelInterface::kClosing)); |
| 884 | test_->data_channels().push_back( |
| 885 | new MockDataChannel(3, DataChannelInterface::kClosed)); |
| 886 | |
| 887 | rtc::scoped_refptr<const RTCStatsReport> report = GetStatsReport(); |
| 888 | ExpectReportContainsDataChannel(report, *test_->data_channels()[0]); |
| 889 | ExpectReportContainsDataChannel(report, *test_->data_channels()[1]); |
| 890 | ExpectReportContainsDataChannel(report, *test_->data_channels()[2]); |
| 891 | ExpectReportContainsDataChannel(report, *test_->data_channels()[3]); |
| 892 | |
| 893 | test_->data_channels().clear(); |
| 894 | test_->data_channels().push_back( |
| 895 | new MockDataChannel(0, DataChannelInterface::kConnecting, |
| 896 | 1, 2, 3, 4)); |
| 897 | test_->data_channels().push_back( |
| 898 | new MockDataChannel(1, DataChannelInterface::kOpen, |
| 899 | 5, 6, 7, 8)); |
| 900 | test_->data_channels().push_back( |
| 901 | new MockDataChannel(2, DataChannelInterface::kClosing, |
| 902 | 9, 10, 11, 12)); |
| 903 | test_->data_channels().push_back( |
| 904 | new MockDataChannel(3, DataChannelInterface::kClosed, |
| 905 | 13, 14, 15, 16)); |
| 906 | |
| 907 | collector_->ClearCachedStatsReport(); |
| 908 | report = GetStatsReport(); |
| 909 | ExpectReportContainsDataChannel(report, *test_->data_channels()[0]); |
| 910 | ExpectReportContainsDataChannel(report, *test_->data_channels()[1]); |
| 911 | ExpectReportContainsDataChannel(report, *test_->data_channels()[2]); |
| 912 | ExpectReportContainsDataChannel(report, *test_->data_channels()[3]); |
| 913 | } |
| 914 | |
hbos | ab9f6e4 | 2016-10-07 09:18:47 | [diff] [blame] | 915 | TEST_F(RTCStatsCollectorTest, CollectRTCIceCandidateStats) { |
| 916 | // Candidates in the first transport stats. |
| 917 | std::unique_ptr<cricket::Candidate> a_local_host = CreateFakeCandidate( |
| 918 | "1.2.3.4", 5, |
| 919 | "a_local_host's protocol", |
| 920 | cricket::LOCAL_PORT_TYPE, |
| 921 | 0); |
| 922 | std::unique_ptr<cricket::Candidate> a_remote_srflx = CreateFakeCandidate( |
| 923 | "6.7.8.9", 10, |
| 924 | "remote_srflx's protocol", |
| 925 | cricket::STUN_PORT_TYPE, |
| 926 | 1); |
| 927 | std::unique_ptr<cricket::Candidate> a_local_prflx = CreateFakeCandidate( |
| 928 | "11.12.13.14", 15, |
| 929 | "a_local_prflx's protocol", |
| 930 | cricket::PRFLX_PORT_TYPE, |
| 931 | 2); |
| 932 | std::unique_ptr<cricket::Candidate> a_remote_relay = CreateFakeCandidate( |
| 933 | "16.17.18.19", 20, |
| 934 | "a_remote_relay's protocol", |
| 935 | cricket::RELAY_PORT_TYPE, |
| 936 | 3); |
| 937 | // Candidates in the second transport stats. |
| 938 | std::unique_ptr<cricket::Candidate> b_local = CreateFakeCandidate( |
| 939 | "42.42.42.42", 42, |
| 940 | "b_local's protocol", |
| 941 | cricket::LOCAL_PORT_TYPE, |
| 942 | 42); |
| 943 | std::unique_ptr<cricket::Candidate> b_remote = CreateFakeCandidate( |
| 944 | "42.42.42.42", 42, |
| 945 | "b_remote's protocol", |
| 946 | cricket::LOCAL_PORT_TYPE, |
| 947 | 42); |
| 948 | |
| 949 | SessionStats session_stats; |
| 950 | |
| 951 | cricket::TransportChannelStats a_transport_channel_stats; |
| 952 | a_transport_channel_stats.connection_infos.push_back( |
| 953 | cricket::ConnectionInfo()); |
| 954 | a_transport_channel_stats.connection_infos[0].local_candidate = |
| 955 | *a_local_host.get(); |
| 956 | a_transport_channel_stats.connection_infos[0].remote_candidate = |
| 957 | *a_remote_srflx.get(); |
| 958 | a_transport_channel_stats.connection_infos.push_back( |
| 959 | cricket::ConnectionInfo()); |
| 960 | a_transport_channel_stats.connection_infos[1].local_candidate = |
| 961 | *a_local_prflx.get(); |
| 962 | a_transport_channel_stats.connection_infos[1].remote_candidate = |
| 963 | *a_remote_relay.get(); |
| 964 | session_stats.transport_stats["a"].channel_stats.push_back( |
| 965 | a_transport_channel_stats); |
| 966 | |
| 967 | cricket::TransportChannelStats b_transport_channel_stats; |
| 968 | b_transport_channel_stats.connection_infos.push_back( |
| 969 | cricket::ConnectionInfo()); |
| 970 | b_transport_channel_stats.connection_infos[0].local_candidate = |
| 971 | *b_local.get(); |
| 972 | b_transport_channel_stats.connection_infos[0].remote_candidate = |
| 973 | *b_remote.get(); |
| 974 | session_stats.transport_stats["b"].channel_stats.push_back( |
| 975 | b_transport_channel_stats); |
| 976 | |
| 977 | // Mock the session to return the desired candidates. |
| 978 | EXPECT_CALL(test_->session(), GetTransportStats(_)).WillRepeatedly(Invoke( |
| 979 | [this, &session_stats](SessionStats* stats) { |
| 980 | *stats = session_stats; |
| 981 | return true; |
| 982 | })); |
| 983 | |
| 984 | rtc::scoped_refptr<const RTCStatsReport> report = GetStatsReport(); |
| 985 | ExpectReportContainsCandidate(report, *a_local_host.get(), true); |
| 986 | ExpectReportContainsCandidate(report, *a_remote_srflx.get(), false); |
| 987 | ExpectReportContainsCandidate(report, *a_local_prflx.get(), true); |
| 988 | ExpectReportContainsCandidate(report, *a_remote_relay.get(), false); |
| 989 | ExpectReportContainsCandidate(report, *b_local.get(), true); |
| 990 | ExpectReportContainsCandidate(report, *b_remote.get(), false); |
| 991 | } |
| 992 | |
hbos | c47a0c3 | 2016-10-11 21:54:49 | [diff] [blame] | 993 | TEST_F(RTCStatsCollectorTest, CollectRTCIceCandidatePairStats) { |
| 994 | std::unique_ptr<cricket::Candidate> local_candidate = CreateFakeCandidate( |
| 995 | "42.42.42.42", 42, "protocol", cricket::LOCAL_PORT_TYPE, 42); |
| 996 | std::unique_ptr<cricket::Candidate> remote_candidate = CreateFakeCandidate( |
| 997 | "42.42.42.42", 42, "protocol", cricket::LOCAL_PORT_TYPE, 42); |
| 998 | |
| 999 | SessionStats session_stats; |
| 1000 | |
| 1001 | cricket::ConnectionInfo connection_info; |
| 1002 | connection_info.local_candidate = *local_candidate.get(); |
| 1003 | connection_info.remote_candidate = *remote_candidate.get(); |
| 1004 | connection_info.writable = true; |
| 1005 | connection_info.sent_total_bytes = 42; |
| 1006 | connection_info.recv_total_bytes = 1234; |
| 1007 | connection_info.rtt = 1337; |
hbos | d82f512 | 2016-12-09 12:12:39 | [diff] [blame] | 1008 | connection_info.recv_ping_requests = 2020; |
hbos | e448dd5 | 2016-12-12 09:22:53 | [diff] [blame^] | 1009 | connection_info.sent_ping_requests_total = 2020; |
| 1010 | connection_info.sent_ping_requests_before_first_response = 2000; |
hbos | c47a0c3 | 2016-10-11 21:54:49 | [diff] [blame] | 1011 | connection_info.recv_ping_responses = 4321; |
| 1012 | connection_info.sent_ping_responses = 1000; |
| 1013 | |
| 1014 | cricket::TransportChannelStats transport_channel_stats; |
hbos | 0583b28 | 2016-11-30 09:50:14 | [diff] [blame] | 1015 | transport_channel_stats.component = cricket::ICE_CANDIDATE_COMPONENT_RTP; |
hbos | c47a0c3 | 2016-10-11 21:54:49 | [diff] [blame] | 1016 | transport_channel_stats.connection_infos.push_back(connection_info); |
| 1017 | session_stats.transport_stats["transport"].transport_name = "transport"; |
| 1018 | session_stats.transport_stats["transport"].channel_stats.push_back( |
| 1019 | transport_channel_stats); |
| 1020 | |
| 1021 | // Mock the session to return the desired candidates. |
| 1022 | EXPECT_CALL(test_->session(), GetTransportStats(_)).WillRepeatedly(Invoke( |
| 1023 | [this, &session_stats](SessionStats* stats) { |
| 1024 | *stats = session_stats; |
| 1025 | return true; |
| 1026 | })); |
| 1027 | |
| 1028 | rtc::scoped_refptr<const RTCStatsReport> report = GetStatsReport(); |
hbos | 0583b28 | 2016-11-30 09:50:14 | [diff] [blame] | 1029 | |
| 1030 | RTCIceCandidatePairStats expected_pair("RTCIceCandidatePair_" + |
| 1031 | local_candidate->id() + "_" + |
| 1032 | remote_candidate->id(), |
| 1033 | report->timestamp_us()); |
| 1034 | expected_pair.transport_id = |
| 1035 | "RTCTransport_transport_" + |
| 1036 | rtc::ToString<>(cricket::ICE_CANDIDATE_COMPONENT_RTP); |
| 1037 | expected_pair.local_candidate_id = "RTCIceCandidate_" + local_candidate->id(); |
| 1038 | expected_pair.remote_candidate_id = |
| 1039 | "RTCIceCandidate_" + remote_candidate->id(); |
| 1040 | expected_pair.writable = true; |
| 1041 | expected_pair.bytes_sent = 42; |
| 1042 | expected_pair.bytes_received = 1234; |
| 1043 | expected_pair.current_rtt = 1.337; |
hbos | d82f512 | 2016-12-09 12:12:39 | [diff] [blame] | 1044 | expected_pair.requests_received = 2020; |
hbos | e448dd5 | 2016-12-12 09:22:53 | [diff] [blame^] | 1045 | expected_pair.requests_sent = 2000; |
hbos | 0583b28 | 2016-11-30 09:50:14 | [diff] [blame] | 1046 | expected_pair.responses_received = 4321; |
| 1047 | expected_pair.responses_sent = 1000; |
hbos | e448dd5 | 2016-12-12 09:22:53 | [diff] [blame^] | 1048 | expected_pair.consent_requests_sent = (2020 - 2000); |
hbos | 0583b28 | 2016-11-30 09:50:14 | [diff] [blame] | 1049 | |
| 1050 | EXPECT_TRUE(report->Get(expected_pair.id())); |
| 1051 | EXPECT_EQ( |
| 1052 | expected_pair, |
| 1053 | report->Get(expected_pair.id())->cast_to<RTCIceCandidatePairStats>()); |
| 1054 | |
| 1055 | EXPECT_TRUE(report->Get(*expected_pair.local_candidate_id)); |
| 1056 | ExpectReportContainsCandidate(report, connection_info.local_candidate, true); |
| 1057 | EXPECT_TRUE(report->Get(*expected_pair.remote_candidate_id)); |
| 1058 | ExpectReportContainsCandidate(report, connection_info.remote_candidate, |
| 1059 | false); |
hbos | c47a0c3 | 2016-10-11 21:54:49 | [diff] [blame] | 1060 | } |
| 1061 | |
hbos | d565b73 | 2016-08-30 21:04:35 | [diff] [blame] | 1062 | TEST_F(RTCStatsCollectorTest, CollectRTCPeerConnectionStats) { |
hbos | d565b73 | 2016-08-30 21:04:35 | [diff] [blame] | 1063 | { |
hbos | 82ebe02 | 2016-11-14 09:41:09 | [diff] [blame] | 1064 | rtc::scoped_refptr<const RTCStatsReport> report = GetStatsReport(); |
| 1065 | RTCPeerConnectionStats expected("RTCPeerConnection", |
| 1066 | report->timestamp_us()); |
| 1067 | expected.data_channels_opened = 0; |
| 1068 | expected.data_channels_closed = 0; |
| 1069 | EXPECT_TRUE(report->Get("RTCPeerConnection")); |
| 1070 | EXPECT_EQ(expected, |
| 1071 | report->Get("RTCPeerConnection")->cast_to< |
| 1072 | RTCPeerConnectionStats>()); |
hbos | d565b73 | 2016-08-30 21:04:35 | [diff] [blame] | 1073 | } |
| 1074 | |
hbos | 82ebe02 | 2016-11-14 09:41:09 | [diff] [blame] | 1075 | rtc::scoped_refptr<DataChannel> dummy_channel_a = DataChannel::Create( |
| 1076 | nullptr, cricket::DCT_NONE, "DummyChannelA", InternalDataChannelInit()); |
| 1077 | test_->pc().SignalDataChannelCreated(dummy_channel_a.get()); |
| 1078 | rtc::scoped_refptr<DataChannel> dummy_channel_b = DataChannel::Create( |
| 1079 | nullptr, cricket::DCT_NONE, "DummyChannelB", InternalDataChannelInit()); |
| 1080 | test_->pc().SignalDataChannelCreated(dummy_channel_b.get()); |
hbos | d565b73 | 2016-08-30 21:04:35 | [diff] [blame] | 1081 | |
hbos | 82ebe02 | 2016-11-14 09:41:09 | [diff] [blame] | 1082 | dummy_channel_a->SignalOpened(dummy_channel_a.get()); |
| 1083 | // Closing a channel that is not opened should not affect the counts. |
| 1084 | dummy_channel_b->SignalClosed(dummy_channel_b.get()); |
| 1085 | |
hbos | d565b73 | 2016-08-30 21:04:35 | [diff] [blame] | 1086 | { |
hbos | 82ebe02 | 2016-11-14 09:41:09 | [diff] [blame] | 1087 | collector_->ClearCachedStatsReport(); |
| 1088 | rtc::scoped_refptr<const RTCStatsReport> report = GetStatsReport(); |
| 1089 | RTCPeerConnectionStats expected("RTCPeerConnection", |
| 1090 | report->timestamp_us()); |
| 1091 | expected.data_channels_opened = 1; |
| 1092 | expected.data_channels_closed = 0; |
| 1093 | EXPECT_TRUE(report->Get("RTCPeerConnection")); |
| 1094 | EXPECT_EQ(expected, |
| 1095 | report->Get("RTCPeerConnection")->cast_to< |
| 1096 | RTCPeerConnectionStats>()); |
| 1097 | } |
| 1098 | |
| 1099 | dummy_channel_b->SignalOpened(dummy_channel_b.get()); |
| 1100 | dummy_channel_b->SignalClosed(dummy_channel_b.get()); |
| 1101 | |
| 1102 | { |
| 1103 | collector_->ClearCachedStatsReport(); |
| 1104 | rtc::scoped_refptr<const RTCStatsReport> report = GetStatsReport(); |
| 1105 | RTCPeerConnectionStats expected("RTCPeerConnection", |
| 1106 | report->timestamp_us()); |
| 1107 | expected.data_channels_opened = 2; |
| 1108 | expected.data_channels_closed = 1; |
| 1109 | EXPECT_TRUE(report->Get("RTCPeerConnection")); |
| 1110 | EXPECT_EQ(expected, |
| 1111 | report->Get("RTCPeerConnection")->cast_to< |
| 1112 | RTCPeerConnectionStats>()); |
hbos | d565b73 | 2016-08-30 21:04:35 | [diff] [blame] | 1113 | } |
| 1114 | } |
| 1115 | |
hbos | 09bc128 | 2016-11-08 14:29:22 | [diff] [blame] | 1116 | TEST_F(RTCStatsCollectorTest, |
| 1117 | CollectRTCMediaStreamStatsAndRTCMediaStreamTrackStats_Audio) { |
| 1118 | rtc::scoped_refptr<StreamCollection> local_streams = |
| 1119 | StreamCollection::Create(); |
| 1120 | rtc::scoped_refptr<StreamCollection> remote_streams = |
| 1121 | StreamCollection::Create(); |
| 1122 | EXPECT_CALL(test_->pc(), local_streams()) |
| 1123 | .WillRepeatedly(Return(local_streams)); |
| 1124 | EXPECT_CALL(test_->pc(), remote_streams()) |
| 1125 | .WillRepeatedly(Return(remote_streams)); |
| 1126 | |
| 1127 | rtc::scoped_refptr<MediaStream> local_stream = |
| 1128 | MediaStream::Create("LocalStreamLabel"); |
| 1129 | local_streams->AddStream(local_stream); |
| 1130 | rtc::scoped_refptr<MediaStream> remote_stream = |
| 1131 | MediaStream::Create("RemoteStreamLabel"); |
| 1132 | remote_streams->AddStream(remote_stream); |
| 1133 | |
| 1134 | // Local audio track |
| 1135 | AudioProcessorInterface::AudioProcessorStats local_audio_processor_stats; |
| 1136 | local_audio_processor_stats.echo_return_loss = 42; |
| 1137 | local_audio_processor_stats.echo_return_loss_enhancement = 52; |
| 1138 | rtc::scoped_refptr<FakeAudioTrackForStats> local_audio_track = |
| 1139 | FakeAudioTrackForStats::Create( |
| 1140 | "LocalAudioTrackID", |
| 1141 | MediaStreamTrackInterface::TrackState::kEnded, |
| 1142 | 32767, |
| 1143 | new FakeAudioProcessorForStats(local_audio_processor_stats)); |
| 1144 | local_stream->AddTrack(local_audio_track); |
| 1145 | |
| 1146 | // Remote audio track |
| 1147 | AudioProcessorInterface::AudioProcessorStats remote_audio_processor_stats; |
| 1148 | remote_audio_processor_stats.echo_return_loss = 13; |
| 1149 | remote_audio_processor_stats.echo_return_loss_enhancement = 37; |
| 1150 | rtc::scoped_refptr<FakeAudioTrackForStats> remote_audio_track = |
| 1151 | FakeAudioTrackForStats::Create( |
| 1152 | "RemoteAudioTrackID", |
| 1153 | MediaStreamTrackInterface::TrackState::kLive, |
| 1154 | 0, |
| 1155 | new FakeAudioProcessorForStats(remote_audio_processor_stats)); |
| 1156 | remote_stream->AddTrack(remote_audio_track); |
| 1157 | |
| 1158 | rtc::scoped_refptr<const RTCStatsReport> report = GetStatsReport(); |
| 1159 | |
| 1160 | RTCMediaStreamStats expected_local_stream( |
| 1161 | "RTCMediaStream_LocalStreamLabel", report->timestamp_us()); |
| 1162 | expected_local_stream.stream_identifier = local_stream->label(); |
| 1163 | expected_local_stream.track_ids = std::vector<std::string>(); |
| 1164 | expected_local_stream.track_ids->push_back( |
| 1165 | "RTCMediaStreamTrack_LocalAudioTrackID"); |
| 1166 | EXPECT_TRUE(report->Get(expected_local_stream.id())); |
| 1167 | EXPECT_EQ(expected_local_stream, |
| 1168 | report->Get(expected_local_stream.id())->cast_to< |
| 1169 | RTCMediaStreamStats>()); |
| 1170 | |
| 1171 | RTCMediaStreamStats expected_remote_stream( |
| 1172 | "RTCMediaStream_RemoteStreamLabel", report->timestamp_us()); |
| 1173 | expected_remote_stream.stream_identifier = remote_stream->label(); |
| 1174 | expected_remote_stream.track_ids = std::vector<std::string>(); |
| 1175 | expected_remote_stream.track_ids->push_back( |
| 1176 | "RTCMediaStreamTrack_RemoteAudioTrackID"); |
| 1177 | EXPECT_TRUE(report->Get(expected_remote_stream.id())); |
| 1178 | EXPECT_EQ(expected_remote_stream, |
| 1179 | report->Get(expected_remote_stream.id())->cast_to< |
| 1180 | RTCMediaStreamStats>()); |
| 1181 | |
| 1182 | RTCMediaStreamTrackStats expected_local_audio_track( |
| 1183 | "RTCMediaStreamTrack_LocalAudioTrackID", report->timestamp_us()); |
| 1184 | expected_local_audio_track.track_identifier = local_audio_track->id(); |
| 1185 | expected_local_audio_track.remote_source = false; |
| 1186 | expected_local_audio_track.ended = true; |
| 1187 | expected_local_audio_track.detached = false; |
| 1188 | expected_local_audio_track.audio_level = 1.0; |
| 1189 | expected_local_audio_track.echo_return_loss = 42.0; |
| 1190 | expected_local_audio_track.echo_return_loss_enhancement = 52.0; |
| 1191 | EXPECT_TRUE(report->Get(expected_local_audio_track.id())); |
| 1192 | EXPECT_EQ(expected_local_audio_track, |
| 1193 | report->Get(expected_local_audio_track.id())->cast_to< |
| 1194 | RTCMediaStreamTrackStats>()); |
| 1195 | |
| 1196 | RTCMediaStreamTrackStats expected_remote_audio_track( |
| 1197 | "RTCMediaStreamTrack_RemoteAudioTrackID", report->timestamp_us()); |
| 1198 | expected_remote_audio_track.track_identifier = remote_audio_track->id(); |
| 1199 | expected_remote_audio_track.remote_source = true; |
| 1200 | expected_remote_audio_track.ended = false; |
| 1201 | expected_remote_audio_track.detached = false; |
| 1202 | expected_remote_audio_track.audio_level = 0.0; |
| 1203 | expected_remote_audio_track.echo_return_loss = 13.0; |
| 1204 | expected_remote_audio_track.echo_return_loss_enhancement = 37.0; |
| 1205 | EXPECT_TRUE(report->Get(expected_remote_audio_track.id())); |
| 1206 | EXPECT_EQ(expected_remote_audio_track, |
| 1207 | report->Get(expected_remote_audio_track.id())->cast_to< |
| 1208 | RTCMediaStreamTrackStats>()); |
| 1209 | } |
| 1210 | |
| 1211 | TEST_F(RTCStatsCollectorTest, |
| 1212 | CollectRTCMediaStreamStatsAndRTCMediaStreamTrackStats_Video) { |
| 1213 | rtc::scoped_refptr<StreamCollection> local_streams = |
| 1214 | StreamCollection::Create(); |
| 1215 | rtc::scoped_refptr<StreamCollection> remote_streams = |
| 1216 | StreamCollection::Create(); |
| 1217 | EXPECT_CALL(test_->pc(), local_streams()) |
| 1218 | .WillRepeatedly(Return(local_streams)); |
| 1219 | EXPECT_CALL(test_->pc(), remote_streams()) |
| 1220 | .WillRepeatedly(Return(remote_streams)); |
| 1221 | |
| 1222 | rtc::scoped_refptr<MediaStream> local_stream = |
| 1223 | MediaStream::Create("LocalStreamLabel"); |
| 1224 | local_streams->AddStream(local_stream); |
| 1225 | rtc::scoped_refptr<MediaStream> remote_stream = |
| 1226 | MediaStream::Create("RemoteStreamLabel"); |
| 1227 | remote_streams->AddStream(remote_stream); |
| 1228 | |
| 1229 | // Local video track |
| 1230 | VideoTrackSourceInterface::Stats local_video_track_source_stats; |
| 1231 | local_video_track_source_stats.input_width = 1234; |
| 1232 | local_video_track_source_stats.input_height = 4321; |
| 1233 | rtc::scoped_refptr<FakeVideoTrackSourceForStats> local_video_track_source = |
| 1234 | new FakeVideoTrackSourceForStats(local_video_track_source_stats); |
| 1235 | rtc::scoped_refptr<FakeVideoTrackForStats> local_video_track = |
| 1236 | FakeVideoTrackForStats::Create( |
| 1237 | "LocalVideoTrackID", |
| 1238 | MediaStreamTrackInterface::TrackState::kLive, |
| 1239 | local_video_track_source); |
| 1240 | local_stream->AddTrack(local_video_track); |
| 1241 | |
| 1242 | // Remote video track |
| 1243 | VideoTrackSourceInterface::Stats remote_video_track_source_stats; |
| 1244 | remote_video_track_source_stats.input_width = 1234; |
| 1245 | remote_video_track_source_stats.input_height = 4321; |
| 1246 | rtc::scoped_refptr<FakeVideoTrackSourceForStats> remote_video_track_source = |
| 1247 | new FakeVideoTrackSourceForStats(remote_video_track_source_stats); |
| 1248 | rtc::scoped_refptr<FakeVideoTrackForStats> remote_video_track = |
| 1249 | FakeVideoTrackForStats::Create( |
| 1250 | "RemoteVideoTrackID", |
| 1251 | MediaStreamTrackInterface::TrackState::kEnded, |
| 1252 | remote_video_track_source); |
| 1253 | remote_stream->AddTrack(remote_video_track); |
| 1254 | |
| 1255 | rtc::scoped_refptr<const RTCStatsReport> report = GetStatsReport(); |
| 1256 | |
| 1257 | RTCMediaStreamStats expected_local_stream( |
| 1258 | "RTCMediaStream_LocalStreamLabel", report->timestamp_us()); |
| 1259 | expected_local_stream.stream_identifier = local_stream->label(); |
| 1260 | expected_local_stream.track_ids = std::vector<std::string>(); |
| 1261 | expected_local_stream.track_ids->push_back( |
| 1262 | "RTCMediaStreamTrack_LocalVideoTrackID"); |
| 1263 | EXPECT_TRUE(report->Get(expected_local_stream.id())); |
| 1264 | EXPECT_EQ(expected_local_stream, |
| 1265 | report->Get(expected_local_stream.id())->cast_to< |
| 1266 | RTCMediaStreamStats>()); |
| 1267 | |
| 1268 | RTCMediaStreamStats expected_remote_stream( |
| 1269 | "RTCMediaStream_RemoteStreamLabel", report->timestamp_us()); |
| 1270 | expected_remote_stream.stream_identifier = remote_stream->label(); |
| 1271 | expected_remote_stream.track_ids = std::vector<std::string>(); |
| 1272 | expected_remote_stream.track_ids->push_back( |
| 1273 | "RTCMediaStreamTrack_RemoteVideoTrackID"); |
| 1274 | EXPECT_TRUE(report->Get(expected_remote_stream.id())); |
| 1275 | EXPECT_EQ(expected_remote_stream, |
| 1276 | report->Get(expected_remote_stream.id())->cast_to< |
| 1277 | RTCMediaStreamStats>()); |
| 1278 | |
| 1279 | RTCMediaStreamTrackStats expected_local_video_track( |
| 1280 | "RTCMediaStreamTrack_LocalVideoTrackID", report->timestamp_us()); |
| 1281 | expected_local_video_track.track_identifier = local_video_track->id(); |
| 1282 | expected_local_video_track.remote_source = false; |
| 1283 | expected_local_video_track.ended = false; |
| 1284 | expected_local_video_track.detached = false; |
| 1285 | expected_local_video_track.frame_width = 1234; |
| 1286 | expected_local_video_track.frame_height = 4321; |
| 1287 | EXPECT_TRUE(report->Get(expected_local_video_track.id())); |
| 1288 | EXPECT_EQ(expected_local_video_track, |
| 1289 | report->Get(expected_local_video_track.id())->cast_to< |
| 1290 | RTCMediaStreamTrackStats>()); |
| 1291 | |
| 1292 | RTCMediaStreamTrackStats expected_remote_video_track( |
| 1293 | "RTCMediaStreamTrack_RemoteVideoTrackID", report->timestamp_us()); |
| 1294 | expected_remote_video_track.track_identifier = remote_video_track->id(); |
| 1295 | expected_remote_video_track.remote_source = true; |
| 1296 | expected_remote_video_track.ended = true; |
| 1297 | expected_remote_video_track.detached = false; |
| 1298 | expected_remote_video_track.frame_width = 1234; |
| 1299 | expected_remote_video_track.frame_height = 4321; |
| 1300 | EXPECT_TRUE(report->Get(expected_remote_video_track.id())); |
| 1301 | EXPECT_EQ(expected_remote_video_track, |
| 1302 | report->Get(expected_remote_video_track.id())->cast_to< |
| 1303 | RTCMediaStreamTrackStats>()); |
| 1304 | } |
| 1305 | |
hbos | eeafe94 | 2016-11-01 10:00:17 | [diff] [blame] | 1306 | TEST_F(RTCStatsCollectorTest, CollectRTCInboundRTPStreamStats_Audio) { |
| 1307 | MockVoiceMediaChannel* voice_media_channel = new MockVoiceMediaChannel(); |
| 1308 | cricket::VoiceChannel voice_channel( |
| 1309 | test_->worker_thread(), test_->network_thread(), test_->media_engine(), |
| 1310 | voice_media_channel, nullptr, "VoiceContentName", false); |
| 1311 | |
| 1312 | cricket::VoiceMediaInfo voice_media_info; |
hbos | 0adb828 | 2016-11-23 10:32:06 | [diff] [blame] | 1313 | |
hbos | eeafe94 | 2016-11-01 10:00:17 | [diff] [blame] | 1314 | voice_media_info.receivers.push_back(cricket::VoiceReceiverInfo()); |
| 1315 | voice_media_info.receivers[0].local_stats.push_back( |
| 1316 | cricket::SsrcReceiverInfo()); |
| 1317 | voice_media_info.receivers[0].local_stats[0].ssrc = 1; |
hbos | 02cd4d6 | 2016-12-09 12:19:44 | [diff] [blame] | 1318 | voice_media_info.receivers[0].packets_lost = 42; |
hbos | eeafe94 | 2016-11-01 10:00:17 | [diff] [blame] | 1319 | voice_media_info.receivers[0].packets_rcvd = 2; |
| 1320 | voice_media_info.receivers[0].bytes_rcvd = 3; |
hbos | 0adb828 | 2016-11-23 10:32:06 | [diff] [blame] | 1321 | voice_media_info.receivers[0].codec_payload_type = rtc::Optional<int>(42); |
hbos | eeafe94 | 2016-11-01 10:00:17 | [diff] [blame] | 1322 | voice_media_info.receivers[0].jitter_ms = 4500; |
| 1323 | voice_media_info.receivers[0].fraction_lost = 5.5f; |
hbos | 0adb828 | 2016-11-23 10:32:06 | [diff] [blame] | 1324 | |
| 1325 | RtpCodecParameters codec_parameters; |
| 1326 | codec_parameters.payload_type = 42; |
| 1327 | codec_parameters.mime_type = "dummy"; |
| 1328 | codec_parameters.clock_rate = 0; |
| 1329 | voice_media_info.receive_codecs.insert( |
| 1330 | std::make_pair(codec_parameters.payload_type, codec_parameters)); |
| 1331 | |
hbos | eeafe94 | 2016-11-01 10:00:17 | [diff] [blame] | 1332 | EXPECT_CALL(*voice_media_channel, GetStats(_)) |
| 1333 | .WillOnce(DoAll(SetArgPointee<0>(voice_media_info), Return(true))); |
| 1334 | |
| 1335 | SessionStats session_stats; |
| 1336 | session_stats.proxy_to_transport["VoiceContentName"] = "TransportName"; |
| 1337 | session_stats.transport_stats["TransportName"].transport_name = |
| 1338 | "TransportName"; |
| 1339 | |
| 1340 | // Make sure the associated |RTCTransportStats| is created. |
| 1341 | cricket::TransportChannelStats channel_stats; |
| 1342 | channel_stats.component = cricket::ICE_CANDIDATE_COMPONENT_RTP; |
| 1343 | session_stats.transport_stats["TransportName"].channel_stats.push_back( |
| 1344 | channel_stats); |
| 1345 | |
| 1346 | EXPECT_CALL(test_->session(), GetTransportStats(_)) |
| 1347 | .WillRepeatedly(DoAll(SetArgPointee<0>(session_stats), Return(true))); |
| 1348 | EXPECT_CALL(test_->session(), voice_channel()) |
| 1349 | .WillRepeatedly(Return(&voice_channel)); |
| 1350 | |
| 1351 | rtc::scoped_refptr<const RTCStatsReport> report = GetStatsReport(); |
| 1352 | |
| 1353 | RTCInboundRTPStreamStats expected_audio( |
| 1354 | "RTCInboundRTPAudioStream_1", report->timestamp_us()); |
| 1355 | expected_audio.ssrc = "1"; |
| 1356 | expected_audio.is_remote = false; |
| 1357 | expected_audio.media_type = "audio"; |
| 1358 | expected_audio.transport_id = "RTCTransport_TransportName_" + |
| 1359 | rtc::ToString<>(cricket::ICE_CANDIDATE_COMPONENT_RTP); |
hbos | 0adb828 | 2016-11-23 10:32:06 | [diff] [blame] | 1360 | expected_audio.codec_id = "RTCCodec_InboundAudio_42"; |
hbos | eeafe94 | 2016-11-01 10:00:17 | [diff] [blame] | 1361 | expected_audio.packets_received = 2; |
| 1362 | expected_audio.bytes_received = 3; |
hbos | 02cd4d6 | 2016-12-09 12:19:44 | [diff] [blame] | 1363 | expected_audio.packets_lost = 42; |
hbos | eeafe94 | 2016-11-01 10:00:17 | [diff] [blame] | 1364 | expected_audio.jitter = 4.5; |
| 1365 | expected_audio.fraction_lost = 5.5; |
| 1366 | |
| 1367 | ASSERT(report->Get(expected_audio.id())); |
| 1368 | const RTCInboundRTPStreamStats& audio = report->Get( |
| 1369 | expected_audio.id())->cast_to<RTCInboundRTPStreamStats>(); |
| 1370 | EXPECT_EQ(audio, expected_audio); |
| 1371 | |
| 1372 | EXPECT_TRUE(report->Get(*expected_audio.transport_id)); |
hbos | 0adb828 | 2016-11-23 10:32:06 | [diff] [blame] | 1373 | EXPECT_TRUE(report->Get(*expected_audio.codec_id)); |
hbos | eeafe94 | 2016-11-01 10:00:17 | [diff] [blame] | 1374 | } |
| 1375 | |
| 1376 | TEST_F(RTCStatsCollectorTest, CollectRTCInboundRTPStreamStats_Video) { |
| 1377 | MockVideoMediaChannel* video_media_channel = new MockVideoMediaChannel(); |
| 1378 | cricket::VideoChannel video_channel( |
| 1379 | test_->worker_thread(), test_->network_thread(), video_media_channel, |
| 1380 | nullptr, "VideoContentName", false); |
| 1381 | |
| 1382 | cricket::VideoMediaInfo video_media_info; |
hbos | 0adb828 | 2016-11-23 10:32:06 | [diff] [blame] | 1383 | |
hbos | eeafe94 | 2016-11-01 10:00:17 | [diff] [blame] | 1384 | video_media_info.receivers.push_back(cricket::VideoReceiverInfo()); |
| 1385 | video_media_info.receivers[0].local_stats.push_back( |
| 1386 | cricket::SsrcReceiverInfo()); |
| 1387 | video_media_info.receivers[0].local_stats[0].ssrc = 1; |
| 1388 | video_media_info.receivers[0].packets_rcvd = 2; |
hbos | 02cd4d6 | 2016-12-09 12:19:44 | [diff] [blame] | 1389 | video_media_info.receivers[0].packets_lost = 42; |
hbos | eeafe94 | 2016-11-01 10:00:17 | [diff] [blame] | 1390 | video_media_info.receivers[0].bytes_rcvd = 3; |
| 1391 | video_media_info.receivers[0].fraction_lost = 4.5f; |
hbos | 0adb828 | 2016-11-23 10:32:06 | [diff] [blame] | 1392 | video_media_info.receivers[0].codec_payload_type = rtc::Optional<int>(42); |
hbos | 820f578 | 2016-11-22 11:16:50 | [diff] [blame] | 1393 | video_media_info.receivers[0].firs_sent = 5; |
| 1394 | video_media_info.receivers[0].plis_sent = 6; |
| 1395 | video_media_info.receivers[0].nacks_sent = 7; |
hbos | 0adb828 | 2016-11-23 10:32:06 | [diff] [blame] | 1396 | |
| 1397 | RtpCodecParameters codec_parameters; |
| 1398 | codec_parameters.payload_type = 42; |
| 1399 | codec_parameters.mime_type = "dummy"; |
| 1400 | codec_parameters.clock_rate = 0; |
| 1401 | video_media_info.receive_codecs.insert( |
| 1402 | std::make_pair(codec_parameters.payload_type, codec_parameters)); |
| 1403 | |
hbos | eeafe94 | 2016-11-01 10:00:17 | [diff] [blame] | 1404 | EXPECT_CALL(*video_media_channel, GetStats(_)) |
| 1405 | .WillOnce(DoAll(SetArgPointee<0>(video_media_info), Return(true))); |
| 1406 | |
| 1407 | SessionStats session_stats; |
| 1408 | session_stats.proxy_to_transport["VideoContentName"] = "TransportName"; |
| 1409 | session_stats.transport_stats["TransportName"].transport_name = |
| 1410 | "TransportName"; |
| 1411 | |
| 1412 | // Make sure the associated |RTCTransportStats| is created. |
| 1413 | cricket::TransportChannelStats channel_stats; |
| 1414 | channel_stats.component = cricket::ICE_CANDIDATE_COMPONENT_RTP; |
| 1415 | session_stats.transport_stats["TransportName"].channel_stats.push_back( |
| 1416 | channel_stats); |
| 1417 | |
| 1418 | EXPECT_CALL(test_->session(), GetTransportStats(_)) |
| 1419 | .WillRepeatedly(DoAll(SetArgPointee<0>(session_stats), Return(true))); |
| 1420 | EXPECT_CALL(test_->session(), video_channel()) |
| 1421 | .WillRepeatedly(Return(&video_channel)); |
| 1422 | |
| 1423 | rtc::scoped_refptr<const RTCStatsReport> report = GetStatsReport(); |
| 1424 | |
hbos | 820f578 | 2016-11-22 11:16:50 | [diff] [blame] | 1425 | RTCInboundRTPStreamStats expected_video( |
hbos | eeafe94 | 2016-11-01 10:00:17 | [diff] [blame] | 1426 | "RTCInboundRTPVideoStream_1", report->timestamp_us()); |
hbos | 820f578 | 2016-11-22 11:16:50 | [diff] [blame] | 1427 | expected_video.ssrc = "1"; |
| 1428 | expected_video.is_remote = false; |
| 1429 | expected_video.media_type = "video"; |
| 1430 | expected_video.transport_id = "RTCTransport_TransportName_" + |
hbos | eeafe94 | 2016-11-01 10:00:17 | [diff] [blame] | 1431 | rtc::ToString<>(cricket::ICE_CANDIDATE_COMPONENT_RTP); |
hbos | 0adb828 | 2016-11-23 10:32:06 | [diff] [blame] | 1432 | expected_video.codec_id = "RTCCodec_InboundVideo_42"; |
hbos | 820f578 | 2016-11-22 11:16:50 | [diff] [blame] | 1433 | expected_video.fir_count = 5; |
| 1434 | expected_video.pli_count = 6; |
| 1435 | expected_video.nack_count = 7; |
| 1436 | expected_video.packets_received = 2; |
| 1437 | expected_video.bytes_received = 3; |
hbos | 02cd4d6 | 2016-12-09 12:19:44 | [diff] [blame] | 1438 | expected_video.packets_lost = 42; |
hbos | 820f578 | 2016-11-22 11:16:50 | [diff] [blame] | 1439 | expected_video.fraction_lost = 4.5; |
hbos | eeafe94 | 2016-11-01 10:00:17 | [diff] [blame] | 1440 | |
hbos | 820f578 | 2016-11-22 11:16:50 | [diff] [blame] | 1441 | ASSERT(report->Get(expected_video.id())); |
| 1442 | const RTCInboundRTPStreamStats& video = report->Get( |
| 1443 | expected_video.id())->cast_to<RTCInboundRTPStreamStats>(); |
| 1444 | EXPECT_EQ(video, expected_video); |
hbos | eeafe94 | 2016-11-01 10:00:17 | [diff] [blame] | 1445 | |
hbos | 820f578 | 2016-11-22 11:16:50 | [diff] [blame] | 1446 | EXPECT_TRUE(report->Get(*expected_video.transport_id)); |
hbos | 0adb828 | 2016-11-23 10:32:06 | [diff] [blame] | 1447 | EXPECT_TRUE(report->Get(*video.codec_id)); |
hbos | eeafe94 | 2016-11-01 10:00:17 | [diff] [blame] | 1448 | } |
| 1449 | |
hbos | 6ded190 | 2016-11-01 08:50:46 | [diff] [blame] | 1450 | TEST_F(RTCStatsCollectorTest, CollectRTCOutboundRTPStreamStats_Audio) { |
| 1451 | MockVoiceMediaChannel* voice_media_channel = new MockVoiceMediaChannel(); |
| 1452 | cricket::VoiceChannel voice_channel( |
| 1453 | test_->worker_thread(), test_->network_thread(), test_->media_engine(), |
| 1454 | voice_media_channel, nullptr, "VoiceContentName", false); |
| 1455 | |
| 1456 | cricket::VoiceMediaInfo voice_media_info; |
hbos | 0adb828 | 2016-11-23 10:32:06 | [diff] [blame] | 1457 | |
hbos | 6ded190 | 2016-11-01 08:50:46 | [diff] [blame] | 1458 | voice_media_info.senders.push_back(cricket::VoiceSenderInfo()); |
| 1459 | voice_media_info.senders[0].local_stats.push_back(cricket::SsrcSenderInfo()); |
| 1460 | voice_media_info.senders[0].local_stats[0].ssrc = 1; |
| 1461 | voice_media_info.senders[0].packets_sent = 2; |
| 1462 | voice_media_info.senders[0].bytes_sent = 3; |
hbos | eeafe94 | 2016-11-01 10:00:17 | [diff] [blame] | 1463 | voice_media_info.senders[0].rtt_ms = 4500; |
hbos | 0adb828 | 2016-11-23 10:32:06 | [diff] [blame] | 1464 | voice_media_info.senders[0].codec_payload_type = rtc::Optional<int>(42); |
| 1465 | |
| 1466 | RtpCodecParameters codec_parameters; |
| 1467 | codec_parameters.payload_type = 42; |
| 1468 | codec_parameters.mime_type = "dummy"; |
| 1469 | codec_parameters.clock_rate = 0; |
| 1470 | voice_media_info.send_codecs.insert( |
| 1471 | std::make_pair(codec_parameters.payload_type, codec_parameters)); |
| 1472 | |
hbos | 6ded190 | 2016-11-01 08:50:46 | [diff] [blame] | 1473 | EXPECT_CALL(*voice_media_channel, GetStats(_)) |
| 1474 | .WillOnce(DoAll(SetArgPointee<0>(voice_media_info), Return(true))); |
| 1475 | |
| 1476 | SessionStats session_stats; |
| 1477 | session_stats.proxy_to_transport["VoiceContentName"] = "TransportName"; |
| 1478 | session_stats.transport_stats["TransportName"].transport_name = |
| 1479 | "TransportName"; |
| 1480 | |
| 1481 | // Make sure the associated |RTCTransportStats| is created. |
| 1482 | cricket::TransportChannelStats channel_stats; |
| 1483 | channel_stats.component = cricket::ICE_CANDIDATE_COMPONENT_RTP; |
hbos | 6ded190 | 2016-11-01 08:50:46 | [diff] [blame] | 1484 | session_stats.transport_stats["TransportName"].channel_stats.push_back( |
| 1485 | channel_stats); |
| 1486 | |
| 1487 | EXPECT_CALL(test_->session(), GetTransportStats(_)) |
| 1488 | .WillRepeatedly(DoAll(SetArgPointee<0>(session_stats), Return(true))); |
| 1489 | EXPECT_CALL(test_->session(), voice_channel()) |
| 1490 | .WillRepeatedly(Return(&voice_channel)); |
| 1491 | |
| 1492 | rtc::scoped_refptr<const RTCStatsReport> report = GetStatsReport(); |
| 1493 | |
| 1494 | RTCOutboundRTPStreamStats expected_audio( |
| 1495 | "RTCOutboundRTPAudioStream_1", report->timestamp_us()); |
| 1496 | expected_audio.ssrc = "1"; |
| 1497 | expected_audio.is_remote = false; |
| 1498 | expected_audio.media_type = "audio"; |
| 1499 | expected_audio.transport_id = "RTCTransport_TransportName_" + |
| 1500 | rtc::ToString<>(cricket::ICE_CANDIDATE_COMPONENT_RTP); |
hbos | 0adb828 | 2016-11-23 10:32:06 | [diff] [blame] | 1501 | expected_audio.codec_id = "RTCCodec_OutboundAudio_42"; |
hbos | 6ded190 | 2016-11-01 08:50:46 | [diff] [blame] | 1502 | expected_audio.packets_sent = 2; |
| 1503 | expected_audio.bytes_sent = 3; |
| 1504 | expected_audio.round_trip_time = 4.5; |
| 1505 | |
| 1506 | ASSERT(report->Get(expected_audio.id())); |
| 1507 | const RTCOutboundRTPStreamStats& audio = report->Get( |
| 1508 | expected_audio.id())->cast_to<RTCOutboundRTPStreamStats>(); |
| 1509 | EXPECT_EQ(audio, expected_audio); |
| 1510 | |
| 1511 | EXPECT_TRUE(report->Get(*expected_audio.transport_id)); |
hbos | 0adb828 | 2016-11-23 10:32:06 | [diff] [blame] | 1512 | EXPECT_TRUE(report->Get(*expected_audio.codec_id)); |
hbos | 6ded190 | 2016-11-01 08:50:46 | [diff] [blame] | 1513 | } |
| 1514 | |
| 1515 | TEST_F(RTCStatsCollectorTest, CollectRTCOutboundRTPStreamStats_Video) { |
| 1516 | MockVideoMediaChannel* video_media_channel = new MockVideoMediaChannel(); |
| 1517 | cricket::VideoChannel video_channel( |
| 1518 | test_->worker_thread(), test_->network_thread(), video_media_channel, |
| 1519 | nullptr, "VideoContentName", false); |
| 1520 | |
| 1521 | cricket::VideoMediaInfo video_media_info; |
hbos | 0adb828 | 2016-11-23 10:32:06 | [diff] [blame] | 1522 | |
hbos | 6ded190 | 2016-11-01 08:50:46 | [diff] [blame] | 1523 | video_media_info.senders.push_back(cricket::VideoSenderInfo()); |
| 1524 | video_media_info.senders[0].local_stats.push_back(cricket::SsrcSenderInfo()); |
| 1525 | video_media_info.senders[0].local_stats[0].ssrc = 1; |
| 1526 | video_media_info.senders[0].firs_rcvd = 2; |
| 1527 | video_media_info.senders[0].plis_rcvd = 3; |
| 1528 | video_media_info.senders[0].nacks_rcvd = 4; |
| 1529 | video_media_info.senders[0].packets_sent = 5; |
| 1530 | video_media_info.senders[0].bytes_sent = 6; |
hbos | eeafe94 | 2016-11-01 10:00:17 | [diff] [blame] | 1531 | video_media_info.senders[0].rtt_ms = 7500; |
hbos | 0adb828 | 2016-11-23 10:32:06 | [diff] [blame] | 1532 | video_media_info.senders[0].codec_payload_type = rtc::Optional<int>(42); |
| 1533 | |
| 1534 | RtpCodecParameters codec_parameters; |
| 1535 | codec_parameters.payload_type = 42; |
| 1536 | codec_parameters.mime_type = "dummy"; |
| 1537 | codec_parameters.clock_rate = 0; |
| 1538 | video_media_info.send_codecs.insert( |
| 1539 | std::make_pair(codec_parameters.payload_type, codec_parameters)); |
| 1540 | |
hbos | 6ded190 | 2016-11-01 08:50:46 | [diff] [blame] | 1541 | EXPECT_CALL(*video_media_channel, GetStats(_)) |
| 1542 | .WillOnce(DoAll(SetArgPointee<0>(video_media_info), Return(true))); |
| 1543 | |
| 1544 | SessionStats session_stats; |
| 1545 | session_stats.proxy_to_transport["VideoContentName"] = "TransportName"; |
| 1546 | session_stats.transport_stats["TransportName"].transport_name = |
| 1547 | "TransportName"; |
| 1548 | |
| 1549 | // Make sure the associated |RTCTransportStats| is created. |
| 1550 | cricket::TransportChannelStats channel_stats; |
| 1551 | channel_stats.component = cricket::ICE_CANDIDATE_COMPONENT_RTP; |
hbos | 6ded190 | 2016-11-01 08:50:46 | [diff] [blame] | 1552 | session_stats.transport_stats["TransportName"].channel_stats.push_back( |
| 1553 | channel_stats); |
| 1554 | |
| 1555 | EXPECT_CALL(test_->session(), GetTransportStats(_)) |
| 1556 | .WillRepeatedly(DoAll(SetArgPointee<0>(session_stats), Return(true))); |
| 1557 | EXPECT_CALL(test_->session(), video_channel()) |
| 1558 | .WillRepeatedly(Return(&video_channel)); |
| 1559 | |
| 1560 | rtc::scoped_refptr<const RTCStatsReport> report = GetStatsReport(); |
| 1561 | |
| 1562 | RTCOutboundRTPStreamStats expected_video( |
| 1563 | "RTCOutboundRTPVideoStream_1", report->timestamp_us()); |
| 1564 | expected_video.ssrc = "1"; |
| 1565 | expected_video.is_remote = false; |
| 1566 | expected_video.media_type = "video"; |
| 1567 | expected_video.transport_id = "RTCTransport_TransportName_" + |
| 1568 | rtc::ToString<>(cricket::ICE_CANDIDATE_COMPONENT_RTP); |
hbos | 0adb828 | 2016-11-23 10:32:06 | [diff] [blame] | 1569 | expected_video.codec_id = "RTCCodec_OutboundVideo_42"; |
hbos | 6ded190 | 2016-11-01 08:50:46 | [diff] [blame] | 1570 | expected_video.fir_count = 2; |
| 1571 | expected_video.pli_count = 3; |
| 1572 | expected_video.nack_count = 4; |
| 1573 | expected_video.packets_sent = 5; |
| 1574 | expected_video.bytes_sent = 6; |
| 1575 | expected_video.round_trip_time = 7.5; |
| 1576 | |
| 1577 | ASSERT(report->Get(expected_video.id())); |
| 1578 | const RTCOutboundRTPStreamStats& video = report->Get( |
| 1579 | expected_video.id())->cast_to<RTCOutboundRTPStreamStats>(); |
| 1580 | EXPECT_EQ(video, expected_video); |
| 1581 | |
| 1582 | EXPECT_TRUE(report->Get(*expected_video.transport_id)); |
hbos | 0adb828 | 2016-11-23 10:32:06 | [diff] [blame] | 1583 | EXPECT_TRUE(report->Get(*expected_video.codec_id)); |
hbos | 6ded190 | 2016-11-01 08:50:46 | [diff] [blame] | 1584 | } |
| 1585 | |
hbos | 2fa7c67 | 2016-10-24 11:00:05 | [diff] [blame] | 1586 | TEST_F(RTCStatsCollectorTest, CollectRTCTransportStats) { |
| 1587 | std::unique_ptr<cricket::Candidate> rtp_local_candidate = CreateFakeCandidate( |
| 1588 | "42.42.42.42", 42, "protocol", cricket::LOCAL_PORT_TYPE, 42); |
| 1589 | std::unique_ptr<cricket::Candidate> rtp_remote_candidate = |
| 1590 | CreateFakeCandidate("42.42.42.42", 42, "protocol", |
| 1591 | cricket::LOCAL_PORT_TYPE, 42); |
| 1592 | std::unique_ptr<cricket::Candidate> rtcp_local_candidate = |
| 1593 | CreateFakeCandidate("42.42.42.42", 42, "protocol", |
| 1594 | cricket::LOCAL_PORT_TYPE, 42); |
| 1595 | std::unique_ptr<cricket::Candidate> rtcp_remote_candidate = |
| 1596 | CreateFakeCandidate("42.42.42.42", 42, "protocol", |
| 1597 | cricket::LOCAL_PORT_TYPE, 42); |
| 1598 | |
| 1599 | SessionStats session_stats; |
| 1600 | session_stats.transport_stats["transport"].transport_name = "transport"; |
| 1601 | |
| 1602 | cricket::ConnectionInfo rtp_connection_info; |
| 1603 | rtp_connection_info.best_connection = false; |
| 1604 | rtp_connection_info.local_candidate = *rtp_local_candidate.get(); |
| 1605 | rtp_connection_info.remote_candidate = *rtp_remote_candidate.get(); |
| 1606 | rtp_connection_info.sent_total_bytes = 42; |
| 1607 | rtp_connection_info.recv_total_bytes = 1337; |
| 1608 | cricket::TransportChannelStats rtp_transport_channel_stats; |
| 1609 | rtp_transport_channel_stats.component = cricket::ICE_CANDIDATE_COMPONENT_RTP; |
| 1610 | rtp_transport_channel_stats.connection_infos.push_back(rtp_connection_info); |
| 1611 | session_stats.transport_stats["transport"].channel_stats.push_back( |
| 1612 | rtp_transport_channel_stats); |
| 1613 | |
| 1614 | |
| 1615 | // Mock the session to return the desired candidates. |
| 1616 | EXPECT_CALL(test_->session(), GetTransportStats(_)).WillRepeatedly(Invoke( |
| 1617 | [this, &session_stats](SessionStats* stats) { |
| 1618 | *stats = session_stats; |
| 1619 | return true; |
| 1620 | })); |
| 1621 | |
| 1622 | // Get stats without RTCP, an active connection or certificates. |
| 1623 | rtc::scoped_refptr<const RTCStatsReport> report = GetStatsReport(); |
hbos | 0583b28 | 2016-11-30 09:50:14 | [diff] [blame] | 1624 | |
| 1625 | RTCTransportStats expected_rtp_transport( |
| 1626 | "RTCTransport_transport_" + |
| 1627 | rtc::ToString<>(cricket::ICE_CANDIDATE_COMPONENT_RTP), |
| 1628 | report->timestamp_us()); |
| 1629 | expected_rtp_transport.bytes_sent = 42; |
| 1630 | expected_rtp_transport.bytes_received = 1337; |
| 1631 | expected_rtp_transport.active_connection = false; |
| 1632 | |
| 1633 | EXPECT_TRUE(report->Get(expected_rtp_transport.id())); |
| 1634 | EXPECT_EQ( |
| 1635 | expected_rtp_transport, |
| 1636 | report->Get(expected_rtp_transport.id())->cast_to<RTCTransportStats>()); |
hbos | 2fa7c67 | 2016-10-24 11:00:05 | [diff] [blame] | 1637 | |
| 1638 | cricket::ConnectionInfo rtcp_connection_info; |
| 1639 | rtcp_connection_info.best_connection = false; |
| 1640 | rtcp_connection_info.local_candidate = *rtcp_local_candidate.get(); |
| 1641 | rtcp_connection_info.remote_candidate = *rtcp_remote_candidate.get(); |
| 1642 | rtcp_connection_info.sent_total_bytes = 1337; |
| 1643 | rtcp_connection_info.recv_total_bytes = 42; |
| 1644 | cricket::TransportChannelStats rtcp_transport_channel_stats; |
| 1645 | rtcp_transport_channel_stats.component = |
| 1646 | cricket::ICE_CANDIDATE_COMPONENT_RTCP; |
| 1647 | rtcp_transport_channel_stats.connection_infos.push_back(rtcp_connection_info); |
| 1648 | session_stats.transport_stats["transport"].channel_stats.push_back( |
| 1649 | rtcp_transport_channel_stats); |
| 1650 | |
| 1651 | collector_->ClearCachedStatsReport(); |
| 1652 | // Get stats with RTCP and without an active connection or certificates. |
| 1653 | report = GetStatsReport(); |
hbos | 0583b28 | 2016-11-30 09:50:14 | [diff] [blame] | 1654 | |
| 1655 | RTCTransportStats expected_rtcp_transport( |
| 1656 | "RTCTransport_transport_" + |
| 1657 | rtc::ToString<>(cricket::ICE_CANDIDATE_COMPONENT_RTCP), |
| 1658 | report->timestamp_us()); |
| 1659 | expected_rtcp_transport.bytes_sent = 1337; |
| 1660 | expected_rtcp_transport.bytes_received = 42; |
| 1661 | expected_rtcp_transport.active_connection = false; |
| 1662 | |
| 1663 | expected_rtp_transport.rtcp_transport_stats_id = expected_rtcp_transport.id(); |
| 1664 | |
| 1665 | EXPECT_TRUE(report->Get(expected_rtp_transport.id())); |
| 1666 | EXPECT_EQ( |
| 1667 | expected_rtp_transport, |
| 1668 | report->Get(expected_rtp_transport.id())->cast_to<RTCTransportStats>()); |
| 1669 | EXPECT_TRUE(report->Get(expected_rtcp_transport.id())); |
| 1670 | EXPECT_EQ( |
| 1671 | expected_rtcp_transport, |
| 1672 | report->Get(expected_rtcp_transport.id())->cast_to<RTCTransportStats>()); |
hbos | 2fa7c67 | 2016-10-24 11:00:05 | [diff] [blame] | 1673 | |
| 1674 | // Get stats with an active connection. |
hbos | 0583b28 | 2016-11-30 09:50:14 | [diff] [blame] | 1675 | session_stats.transport_stats["transport"] |
| 1676 | .channel_stats[1] |
| 1677 | .connection_infos[0] |
| 1678 | .best_connection = true; |
hbos | 2fa7c67 | 2016-10-24 11:00:05 | [diff] [blame] | 1679 | |
| 1680 | collector_->ClearCachedStatsReport(); |
| 1681 | report = GetStatsReport(); |
hbos | 0583b28 | 2016-11-30 09:50:14 | [diff] [blame] | 1682 | |
| 1683 | expected_rtcp_transport.active_connection = true; |
| 1684 | expected_rtcp_transport.selected_candidate_pair_id = |
| 1685 | "RTCIceCandidatePair_" + rtcp_local_candidate->id() + "_" + |
| 1686 | rtcp_remote_candidate->id(); |
| 1687 | |
| 1688 | EXPECT_TRUE(report->Get(expected_rtp_transport.id())); |
| 1689 | EXPECT_EQ( |
| 1690 | expected_rtp_transport, |
| 1691 | report->Get(expected_rtp_transport.id())->cast_to<RTCTransportStats>()); |
| 1692 | EXPECT_TRUE(report->Get(expected_rtcp_transport.id())); |
| 1693 | EXPECT_EQ( |
| 1694 | expected_rtcp_transport, |
| 1695 | report->Get(expected_rtcp_transport.id())->cast_to<RTCTransportStats>()); |
hbos | 2fa7c67 | 2016-10-24 11:00:05 | [diff] [blame] | 1696 | |
| 1697 | // Get stats with certificates. |
| 1698 | std::unique_ptr<CertificateInfo> local_certinfo = |
| 1699 | CreateFakeCertificateAndInfoFromDers( |
| 1700 | std::vector<std::string>({ "(local) local", "(local) chain" })); |
| 1701 | std::unique_ptr<CertificateInfo> remote_certinfo = |
| 1702 | CreateFakeCertificateAndInfoFromDers( |
| 1703 | std::vector<std::string>({ "(remote) local", "(remote) chain" })); |
| 1704 | EXPECT_CALL(test_->session(), GetLocalCertificate(_, _)).WillRepeatedly( |
| 1705 | Invoke([this, &local_certinfo](const std::string& transport_name, |
| 1706 | rtc::scoped_refptr<rtc::RTCCertificate>* certificate) { |
| 1707 | if (transport_name == "transport") { |
| 1708 | *certificate = local_certinfo->certificate; |
| 1709 | return true; |
| 1710 | } |
| 1711 | return false; |
| 1712 | })); |
| 1713 | EXPECT_CALL(test_->session(), |
| 1714 | GetRemoteSSLCertificate_ReturnsRawPointer(_)).WillRepeatedly(Invoke( |
| 1715 | [this, &remote_certinfo](const std::string& transport_name) { |
| 1716 | if (transport_name == "transport") |
| 1717 | return remote_certinfo->certificate->ssl_certificate().GetReference(); |
| 1718 | return static_cast<rtc::SSLCertificate*>(nullptr); |
| 1719 | })); |
| 1720 | |
| 1721 | collector_->ClearCachedStatsReport(); |
| 1722 | report = GetStatsReport(); |
hbos | 0583b28 | 2016-11-30 09:50:14 | [diff] [blame] | 1723 | |
| 1724 | expected_rtp_transport.local_certificate_id = |
| 1725 | "RTCCertificate_" + local_certinfo->fingerprints[0]; |
| 1726 | expected_rtp_transport.remote_certificate_id = |
| 1727 | "RTCCertificate_" + remote_certinfo->fingerprints[0]; |
| 1728 | |
| 1729 | expected_rtcp_transport.local_certificate_id = |
| 1730 | *expected_rtp_transport.local_certificate_id; |
| 1731 | expected_rtcp_transport.remote_certificate_id = |
| 1732 | *expected_rtp_transport.remote_certificate_id; |
| 1733 | |
| 1734 | EXPECT_TRUE(report->Get(expected_rtp_transport.id())); |
| 1735 | EXPECT_EQ( |
| 1736 | expected_rtp_transport, |
| 1737 | report->Get(expected_rtp_transport.id())->cast_to<RTCTransportStats>()); |
| 1738 | EXPECT_TRUE(report->Get(expected_rtcp_transport.id())); |
| 1739 | EXPECT_EQ( |
| 1740 | expected_rtcp_transport, |
| 1741 | report->Get(expected_rtcp_transport.id())->cast_to<RTCTransportStats>()); |
hbos | 2fa7c67 | 2016-10-24 11:00:05 | [diff] [blame] | 1742 | } |
| 1743 | |
hbos | c82f2e1 | 2016-09-05 08:36:50 | [diff] [blame] | 1744 | class RTCStatsCollectorTestWithFakeCollector : public testing::Test { |
| 1745 | public: |
| 1746 | RTCStatsCollectorTestWithFakeCollector() |
| 1747 | : test_(new rtc::RefCountedObject<RTCStatsCollectorTestHelper>()), |
| 1748 | collector_(FakeRTCStatsCollector::Create( |
| 1749 | &test_->pc(), 50 * rtc::kNumMicrosecsPerMillisec)) { |
| 1750 | } |
| 1751 | |
| 1752 | protected: |
| 1753 | rtc::scoped_refptr<RTCStatsCollectorTestHelper> test_; |
| 1754 | rtc::scoped_refptr<FakeRTCStatsCollector> collector_; |
| 1755 | }; |
| 1756 | |
| 1757 | TEST_F(RTCStatsCollectorTestWithFakeCollector, ThreadUsageAndResultsMerging) { |
| 1758 | collector_->VerifyThreadUsageAndResultsMerging(); |
| 1759 | } |
| 1760 | |
| 1761 | } // namespace |
| 1762 | |
hbos | d565b73 | 2016-08-30 21:04:35 | [diff] [blame] | 1763 | } // namespace webrtc |