pbos@webrtc.org | 1d09690 | 2013-12-13 12:48:05 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2013 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 | #include <assert.h> |
| 11 | |
| 12 | #include <algorithm> |
| 13 | #include <sstream> |
| 14 | #include <string> |
| 15 | |
| 16 | #include "testing/gtest/include/gtest/gtest.h" |
| 17 | |
| 18 | #include "webrtc/call.h" |
henrik.lundin@webrtc.org | ed865b5 | 2014-03-06 10:28:07 +0000 | [diff] [blame] | 19 | #include "webrtc/modules/audio_coding/main/interface/audio_coding_module.h" |
pbos@webrtc.org | 1d09690 | 2013-12-13 12:48:05 +0000 | [diff] [blame] | 20 | #include "webrtc/modules/rtp_rtcp/interface/rtp_header_parser.h" |
| 21 | #include "webrtc/modules/rtp_rtcp/source/rtcp_utility.h" |
| 22 | #include "webrtc/system_wrappers/interface/critical_section_wrapper.h" |
wu@webrtc.org | 66773a0 | 2014-05-07 17:09:44 +0000 | [diff] [blame] | 23 | #include "webrtc/system_wrappers/interface/rtp_to_ntp.h" |
pbos@webrtc.org | 1d09690 | 2013-12-13 12:48:05 +0000 | [diff] [blame] | 24 | #include "webrtc/system_wrappers/interface/scoped_ptr.h" |
pbos@webrtc.org | de1429e | 2014-04-28 13:00:21 +0000 | [diff] [blame] | 25 | #include "webrtc/system_wrappers/interface/thread_annotations.h" |
pbos@webrtc.org | 994d0b7 | 2014-06-27 08:47:52 +0000 | [diff] [blame^] | 26 | #include "webrtc/test/call_test.h" |
pbos@webrtc.org | 1d09690 | 2013-12-13 12:48:05 +0000 | [diff] [blame] | 27 | #include "webrtc/test/direct_transport.h" |
pbos@webrtc.org | f577ae9 | 2014-03-19 08:43:57 +0000 | [diff] [blame] | 28 | #include "webrtc/test/encoder_settings.h" |
pbos@webrtc.org | 1d09690 | 2013-12-13 12:48:05 +0000 | [diff] [blame] | 29 | #include "webrtc/test/fake_audio_device.h" |
| 30 | #include "webrtc/test/fake_decoder.h" |
| 31 | #include "webrtc/test/fake_encoder.h" |
| 32 | #include "webrtc/test/frame_generator.h" |
| 33 | #include "webrtc/test/frame_generator_capturer.h" |
| 34 | #include "webrtc/test/rtp_rtcp_observer.h" |
| 35 | #include "webrtc/test/testsupport/fileutils.h" |
| 36 | #include "webrtc/test/testsupport/perf_test.h" |
| 37 | #include "webrtc/video/transport_adapter.h" |
| 38 | #include "webrtc/voice_engine/include/voe_base.h" |
| 39 | #include "webrtc/voice_engine/include/voe_codec.h" |
| 40 | #include "webrtc/voice_engine/include/voe_network.h" |
| 41 | #include "webrtc/voice_engine/include/voe_rtp_rtcp.h" |
| 42 | #include "webrtc/voice_engine/include/voe_video_sync.h" |
| 43 | |
| 44 | namespace webrtc { |
| 45 | |
pbos@webrtc.org | 994d0b7 | 2014-06-27 08:47:52 +0000 | [diff] [blame^] | 46 | class CallPerfTest : public test::CallTest { |
asapersson@webrtc.org | bdc5ed2 | 2014-01-31 10:05:07 +0000 | [diff] [blame] | 47 | protected: |
pbos@webrtc.org | 3349ae0 | 2014-03-13 12:52:27 +0000 | [diff] [blame] | 48 | void TestMinTransmitBitrate(bool pad_to_min_bitrate); |
| 49 | |
wu@webrtc.org | cd70119 | 2014-04-24 22:10:24 +0000 | [diff] [blame] | 50 | void TestCaptureNtpTime(const FakeNetworkPipe::Config& net_config, |
| 51 | int threshold_ms, |
| 52 | int start_time_ms, |
| 53 | int run_time_ms); |
pbos@webrtc.org | 1d09690 | 2013-12-13 12:48:05 +0000 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | class SyncRtcpObserver : public test::RtpRtcpObserver { |
| 57 | public: |
stefan@webrtc.org | faada6e | 2013-12-18 20:28:25 +0000 | [diff] [blame] | 58 | explicit SyncRtcpObserver(const FakeNetworkPipe::Config& config) |
pbos@webrtc.org | 994d0b7 | 2014-06-27 08:47:52 +0000 | [diff] [blame^] | 59 | : test::RtpRtcpObserver(CallPerfTest::kLongTimeoutMs, config), |
pbos@webrtc.org | de1429e | 2014-04-28 13:00:21 +0000 | [diff] [blame] | 60 | crit_(CriticalSectionWrapper::CreateCriticalSection()) {} |
pbos@webrtc.org | 1d09690 | 2013-12-13 12:48:05 +0000 | [diff] [blame] | 61 | |
| 62 | virtual Action OnSendRtcp(const uint8_t* packet, size_t length) OVERRIDE { |
| 63 | RTCPUtility::RTCPParserV2 parser(packet, length, true); |
| 64 | EXPECT_TRUE(parser.IsValid()); |
| 65 | |
| 66 | for (RTCPUtility::RTCPPacketTypes packet_type = parser.Begin(); |
| 67 | packet_type != RTCPUtility::kRtcpNotValidCode; |
| 68 | packet_type = parser.Iterate()) { |
| 69 | if (packet_type == RTCPUtility::kRtcpSrCode) { |
| 70 | const RTCPUtility::RTCPPacket& packet = parser.Packet(); |
wu@webrtc.org | 66773a0 | 2014-05-07 17:09:44 +0000 | [diff] [blame] | 71 | RtcpMeasurement ntp_rtp_pair( |
pbos@webrtc.org | 1d09690 | 2013-12-13 12:48:05 +0000 | [diff] [blame] | 72 | packet.SR.NTPMostSignificant, |
| 73 | packet.SR.NTPLeastSignificant, |
| 74 | packet.SR.RTPTimestamp); |
| 75 | StoreNtpRtpPair(ntp_rtp_pair); |
| 76 | } |
| 77 | } |
| 78 | return SEND_PACKET; |
| 79 | } |
| 80 | |
| 81 | int64_t RtpTimestampToNtp(uint32_t timestamp) const { |
pbos@webrtc.org | de1429e | 2014-04-28 13:00:21 +0000 | [diff] [blame] | 82 | CriticalSectionScoped lock(crit_.get()); |
pbos@webrtc.org | 1d09690 | 2013-12-13 12:48:05 +0000 | [diff] [blame] | 83 | int64_t timestamp_in_ms = -1; |
| 84 | if (ntp_rtp_pairs_.size() == 2) { |
| 85 | // TODO(stefan): We can't EXPECT_TRUE on this call due to a bug in the |
| 86 | // RTCP sender where it sends RTCP SR before any RTP packets, which leads |
| 87 | // to a bogus NTP/RTP mapping. |
wu@webrtc.org | 66773a0 | 2014-05-07 17:09:44 +0000 | [diff] [blame] | 88 | RtpToNtpMs(timestamp, ntp_rtp_pairs_, ×tamp_in_ms); |
pbos@webrtc.org | 1d09690 | 2013-12-13 12:48:05 +0000 | [diff] [blame] | 89 | return timestamp_in_ms; |
| 90 | } |
| 91 | return -1; |
| 92 | } |
| 93 | |
| 94 | private: |
wu@webrtc.org | 66773a0 | 2014-05-07 17:09:44 +0000 | [diff] [blame] | 95 | void StoreNtpRtpPair(RtcpMeasurement ntp_rtp_pair) { |
pbos@webrtc.org | de1429e | 2014-04-28 13:00:21 +0000 | [diff] [blame] | 96 | CriticalSectionScoped lock(crit_.get()); |
wu@webrtc.org | 66773a0 | 2014-05-07 17:09:44 +0000 | [diff] [blame] | 97 | for (RtcpList::iterator it = ntp_rtp_pairs_.begin(); |
pbos@webrtc.org | 1d09690 | 2013-12-13 12:48:05 +0000 | [diff] [blame] | 98 | it != ntp_rtp_pairs_.end(); |
| 99 | ++it) { |
| 100 | if (ntp_rtp_pair.ntp_secs == it->ntp_secs && |
| 101 | ntp_rtp_pair.ntp_frac == it->ntp_frac) { |
| 102 | // This RTCP has already been added to the list. |
| 103 | return; |
| 104 | } |
| 105 | } |
| 106 | // We need two RTCP SR reports to map between RTP and NTP. More than two |
| 107 | // will not improve the mapping. |
| 108 | if (ntp_rtp_pairs_.size() == 2) { |
| 109 | ntp_rtp_pairs_.pop_back(); |
| 110 | } |
| 111 | ntp_rtp_pairs_.push_front(ntp_rtp_pair); |
| 112 | } |
| 113 | |
pbos@webrtc.org | de1429e | 2014-04-28 13:00:21 +0000 | [diff] [blame] | 114 | const scoped_ptr<CriticalSectionWrapper> crit_; |
wu@webrtc.org | 66773a0 | 2014-05-07 17:09:44 +0000 | [diff] [blame] | 115 | RtcpList ntp_rtp_pairs_ GUARDED_BY(crit_); |
pbos@webrtc.org | 1d09690 | 2013-12-13 12:48:05 +0000 | [diff] [blame] | 116 | }; |
| 117 | |
| 118 | class VideoRtcpAndSyncObserver : public SyncRtcpObserver, public VideoRenderer { |
| 119 | static const int kInSyncThresholdMs = 50; |
| 120 | static const int kStartupTimeMs = 2000; |
| 121 | static const int kMinRunTimeMs = 30000; |
| 122 | |
| 123 | public: |
| 124 | VideoRtcpAndSyncObserver(Clock* clock, |
| 125 | int voe_channel, |
| 126 | VoEVideoSync* voe_sync, |
henrik.lundin@webrtc.org | d144bb6 | 2014-04-22 08:36:33 +0000 | [diff] [blame] | 127 | SyncRtcpObserver* audio_observer) |
stefan@webrtc.org | faada6e | 2013-12-18 20:28:25 +0000 | [diff] [blame] | 128 | : SyncRtcpObserver(FakeNetworkPipe::Config()), |
pbos@webrtc.org | 1d09690 | 2013-12-13 12:48:05 +0000 | [diff] [blame] | 129 | clock_(clock), |
| 130 | voe_channel_(voe_channel), |
| 131 | voe_sync_(voe_sync), |
| 132 | audio_observer_(audio_observer), |
| 133 | creation_time_ms_(clock_->TimeInMilliseconds()), |
henrik.lundin@webrtc.org | d144bb6 | 2014-04-22 08:36:33 +0000 | [diff] [blame] | 134 | first_time_in_sync_(-1) {} |
pbos@webrtc.org | 1d09690 | 2013-12-13 12:48:05 +0000 | [diff] [blame] | 135 | |
| 136 | virtual void RenderFrame(const I420VideoFrame& video_frame, |
| 137 | int time_to_render_ms) OVERRIDE { |
| 138 | int64_t now_ms = clock_->TimeInMilliseconds(); |
| 139 | uint32_t playout_timestamp = 0; |
| 140 | if (voe_sync_->GetPlayoutTimestamp(voe_channel_, playout_timestamp) != 0) |
| 141 | return; |
| 142 | int64_t latest_audio_ntp = |
| 143 | audio_observer_->RtpTimestampToNtp(playout_timestamp); |
| 144 | int64_t latest_video_ntp = RtpTimestampToNtp(video_frame.timestamp()); |
| 145 | if (latest_audio_ntp < 0 || latest_video_ntp < 0) |
| 146 | return; |
| 147 | int time_until_render_ms = |
| 148 | std::max(0, static_cast<int>(video_frame.render_time_ms() - now_ms)); |
| 149 | latest_video_ntp += time_until_render_ms; |
| 150 | int64_t stream_offset = latest_audio_ntp - latest_video_ntp; |
| 151 | std::stringstream ss; |
| 152 | ss << stream_offset; |
henrik.lundin@webrtc.org | ed865b5 | 2014-03-06 10:28:07 +0000 | [diff] [blame] | 153 | webrtc::test::PrintResult("stream_offset", |
henrik.lundin@webrtc.org | d144bb6 | 2014-04-22 08:36:33 +0000 | [diff] [blame] | 154 | "", |
henrik.lundin@webrtc.org | ed865b5 | 2014-03-06 10:28:07 +0000 | [diff] [blame] | 155 | "synchronization", |
| 156 | ss.str(), |
| 157 | "ms", |
| 158 | false); |
pbos@webrtc.org | 1d09690 | 2013-12-13 12:48:05 +0000 | [diff] [blame] | 159 | int64_t time_since_creation = now_ms - creation_time_ms_; |
| 160 | // During the first couple of seconds audio and video can falsely be |
| 161 | // estimated as being synchronized. We don't want to trigger on those. |
| 162 | if (time_since_creation < kStartupTimeMs) |
| 163 | return; |
pbos@webrtc.org | b5f3029 | 2014-03-13 08:53:39 +0000 | [diff] [blame] | 164 | if (std::abs(latest_audio_ntp - latest_video_ntp) < kInSyncThresholdMs) { |
pbos@webrtc.org | 1d09690 | 2013-12-13 12:48:05 +0000 | [diff] [blame] | 165 | if (first_time_in_sync_ == -1) { |
| 166 | first_time_in_sync_ = now_ms; |
| 167 | webrtc::test::PrintResult("sync_convergence_time", |
henrik.lundin@webrtc.org | d144bb6 | 2014-04-22 08:36:33 +0000 | [diff] [blame] | 168 | "", |
pbos@webrtc.org | 1d09690 | 2013-12-13 12:48:05 +0000 | [diff] [blame] | 169 | "synchronization", |
| 170 | time_since_creation, |
| 171 | "ms", |
| 172 | false); |
| 173 | } |
| 174 | if (time_since_creation > kMinRunTimeMs) |
| 175 | observation_complete_->Set(); |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | private: |
pbos@webrtc.org | de1429e | 2014-04-28 13:00:21 +0000 | [diff] [blame] | 180 | Clock* const clock_; |
pbos@webrtc.org | 1d09690 | 2013-12-13 12:48:05 +0000 | [diff] [blame] | 181 | int voe_channel_; |
| 182 | VoEVideoSync* voe_sync_; |
| 183 | SyncRtcpObserver* audio_observer_; |
| 184 | int64_t creation_time_ms_; |
| 185 | int64_t first_time_in_sync_; |
| 186 | }; |
| 187 | |
henrik.lundin@webrtc.org | d144bb6 | 2014-04-22 08:36:33 +0000 | [diff] [blame] | 188 | TEST_F(CallPerfTest, PlaysOutAudioAndVideoInSync) { |
pbos@webrtc.org | 994d0b7 | 2014-06-27 08:47:52 +0000 | [diff] [blame^] | 189 | class AudioPacketReceiver : public PacketReceiver { |
| 190 | public: |
| 191 | AudioPacketReceiver(int channel, VoENetwork* voe_network) |
| 192 | : channel_(channel), |
| 193 | voe_network_(voe_network), |
| 194 | parser_(RtpHeaderParser::Create()) {} |
| 195 | virtual DeliveryStatus DeliverPacket(const uint8_t* packet, |
| 196 | size_t length) OVERRIDE { |
| 197 | int ret; |
| 198 | if (parser_->IsRtcp(packet, static_cast<int>(length))) { |
| 199 | ret = voe_network_->ReceivedRTCPPacket( |
| 200 | channel_, packet, static_cast<unsigned int>(length)); |
| 201 | } else { |
| 202 | ret = voe_network_->ReceivedRTPPacket( |
| 203 | channel_, packet, static_cast<unsigned int>(length), PacketTime()); |
| 204 | } |
| 205 | return ret == 0 ? DELIVERY_OK : DELIVERY_PACKET_ERROR; |
| 206 | } |
| 207 | |
| 208 | private: |
| 209 | int channel_; |
| 210 | VoENetwork* voe_network_; |
| 211 | scoped_ptr<RtpHeaderParser> parser_; |
| 212 | }; |
| 213 | |
pbos@webrtc.org | 1d09690 | 2013-12-13 12:48:05 +0000 | [diff] [blame] | 214 | VoiceEngine* voice_engine = VoiceEngine::Create(); |
| 215 | VoEBase* voe_base = VoEBase::GetInterface(voice_engine); |
| 216 | VoECodec* voe_codec = VoECodec::GetInterface(voice_engine); |
| 217 | VoENetwork* voe_network = VoENetwork::GetInterface(voice_engine); |
| 218 | VoEVideoSync* voe_sync = VoEVideoSync::GetInterface(voice_engine); |
| 219 | const std::string audio_filename = |
| 220 | test::ResourcePath("voice_engine/audio_long16", "pcm"); |
| 221 | ASSERT_STRNE("", audio_filename.c_str()); |
| 222 | test::FakeAudioDevice fake_audio_device(Clock::GetRealTimeClock(), |
| 223 | audio_filename); |
| 224 | EXPECT_EQ(0, voe_base->Init(&fake_audio_device, NULL)); |
henrik.lundin@webrtc.org | d144bb6 | 2014-04-22 08:36:33 +0000 | [diff] [blame] | 225 | int channel = voe_base->CreateChannel(); |
pbos@webrtc.org | 1d09690 | 2013-12-13 12:48:05 +0000 | [diff] [blame] | 226 | |
stefan@webrtc.org | faada6e | 2013-12-18 20:28:25 +0000 | [diff] [blame] | 227 | FakeNetworkPipe::Config net_config; |
| 228 | net_config.queue_delay_ms = 500; |
| 229 | SyncRtcpObserver audio_observer(net_config); |
henrik.lundin@webrtc.org | ed865b5 | 2014-03-06 10:28:07 +0000 | [diff] [blame] | 230 | VideoRtcpAndSyncObserver observer(Clock::GetRealTimeClock(), |
| 231 | channel, |
| 232 | voe_sync, |
henrik.lundin@webrtc.org | d144bb6 | 2014-04-22 08:36:33 +0000 | [diff] [blame] | 233 | &audio_observer); |
pbos@webrtc.org | 1d09690 | 2013-12-13 12:48:05 +0000 | [diff] [blame] | 234 | |
| 235 | Call::Config receiver_config(observer.ReceiveTransport()); |
| 236 | receiver_config.voice_engine = voice_engine; |
pbos@webrtc.org | 994d0b7 | 2014-06-27 08:47:52 +0000 | [diff] [blame^] | 237 | CreateCalls(Call::Config(observer.SendTransport()), receiver_config); |
| 238 | |
pbos@webrtc.org | 1d09690 | 2013-12-13 12:48:05 +0000 | [diff] [blame] | 239 | CodecInst isac = {103, "ISAC", 16000, 480, 1, 32000}; |
| 240 | EXPECT_EQ(0, voe_codec->SetSendCodec(channel, isac)); |
| 241 | |
pbos@webrtc.org | 994d0b7 | 2014-06-27 08:47:52 +0000 | [diff] [blame^] | 242 | AudioPacketReceiver voe_packet_receiver(channel, voe_network); |
pbos@webrtc.org | 1d09690 | 2013-12-13 12:48:05 +0000 | [diff] [blame] | 243 | audio_observer.SetReceivers(&voe_packet_receiver, &voe_packet_receiver); |
| 244 | |
| 245 | internal::TransportAdapter transport_adapter(audio_observer.SendTransport()); |
sprang@webrtc.org | d9b9560 | 2014-01-27 13:03:02 +0000 | [diff] [blame] | 246 | transport_adapter.Enable(); |
pbos@webrtc.org | 1d09690 | 2013-12-13 12:48:05 +0000 | [diff] [blame] | 247 | EXPECT_EQ(0, |
| 248 | voe_network->RegisterExternalTransport(channel, transport_adapter)); |
| 249 | |
pbos@webrtc.org | 994d0b7 | 2014-06-27 08:47:52 +0000 | [diff] [blame^] | 250 | observer.SetReceivers(receiver_call_->Receiver(), sender_call_->Receiver()); |
pbos@webrtc.org | 1d09690 | 2013-12-13 12:48:05 +0000 | [diff] [blame] | 251 | |
pbos@webrtc.org | 1d09690 | 2013-12-13 12:48:05 +0000 | [diff] [blame] | 252 | test::FakeDecoder fake_decoder; |
| 253 | |
pbos@webrtc.org | 994d0b7 | 2014-06-27 08:47:52 +0000 | [diff] [blame^] | 254 | CreateSendConfig(1); |
| 255 | CreateMatchingReceiveConfigs(); |
pbos@webrtc.org | 1d09690 | 2013-12-13 12:48:05 +0000 | [diff] [blame] | 256 | |
pbos@webrtc.org | 994d0b7 | 2014-06-27 08:47:52 +0000 | [diff] [blame^] | 257 | receive_config_.renderer = &observer; |
| 258 | receive_config_.audio_channel_id = channel; |
pbos@webrtc.org | 1d09690 | 2013-12-13 12:48:05 +0000 | [diff] [blame] | 259 | |
pbos@webrtc.org | 994d0b7 | 2014-06-27 08:47:52 +0000 | [diff] [blame^] | 260 | CreateStreams(); |
| 261 | |
| 262 | CreateFrameGeneratorCapturer(); |
| 263 | |
| 264 | Start(); |
pbos@webrtc.org | 1d09690 | 2013-12-13 12:48:05 +0000 | [diff] [blame] | 265 | |
| 266 | fake_audio_device.Start(); |
| 267 | EXPECT_EQ(0, voe_base->StartPlayout(channel)); |
| 268 | EXPECT_EQ(0, voe_base->StartReceive(channel)); |
| 269 | EXPECT_EQ(0, voe_base->StartSend(channel)); |
| 270 | |
| 271 | EXPECT_EQ(kEventSignaled, observer.Wait()) |
| 272 | << "Timed out while waiting for audio and video to be synchronized."; |
| 273 | |
| 274 | EXPECT_EQ(0, voe_base->StopSend(channel)); |
| 275 | EXPECT_EQ(0, voe_base->StopReceive(channel)); |
| 276 | EXPECT_EQ(0, voe_base->StopPlayout(channel)); |
| 277 | fake_audio_device.Stop(); |
| 278 | |
pbos@webrtc.org | 994d0b7 | 2014-06-27 08:47:52 +0000 | [diff] [blame^] | 279 | Stop(); |
pbos@webrtc.org | 1d09690 | 2013-12-13 12:48:05 +0000 | [diff] [blame] | 280 | observer.StopSending(); |
| 281 | audio_observer.StopSending(); |
| 282 | |
| 283 | voe_base->DeleteChannel(channel); |
| 284 | voe_base->Release(); |
| 285 | voe_codec->Release(); |
| 286 | voe_network->Release(); |
| 287 | voe_sync->Release(); |
pbos@webrtc.org | 994d0b7 | 2014-06-27 08:47:52 +0000 | [diff] [blame^] | 288 | |
| 289 | DestroyStreams(); |
| 290 | |
pbos@webrtc.org | 1d09690 | 2013-12-13 12:48:05 +0000 | [diff] [blame] | 291 | VoiceEngine::Delete(voice_engine); |
| 292 | } |
asapersson@webrtc.org | bdc5ed2 | 2014-01-31 10:05:07 +0000 | [diff] [blame] | 293 | |
wu@webrtc.org | cd70119 | 2014-04-24 22:10:24 +0000 | [diff] [blame] | 294 | void CallPerfTest::TestCaptureNtpTime(const FakeNetworkPipe::Config& net_config, |
| 295 | int threshold_ms, |
| 296 | int start_time_ms, |
| 297 | int run_time_ms) { |
pbos@webrtc.org | 994d0b7 | 2014-06-27 08:47:52 +0000 | [diff] [blame^] | 298 | class CaptureNtpTimeObserver : public test::EndToEndTest, |
| 299 | public VideoRenderer { |
| 300 | public: |
| 301 | CaptureNtpTimeObserver(const FakeNetworkPipe::Config& config, |
| 302 | int threshold_ms, |
| 303 | int start_time_ms, |
| 304 | int run_time_ms) |
| 305 | : EndToEndTest(kLongTimeoutMs, config), |
| 306 | clock_(Clock::GetRealTimeClock()), |
| 307 | threshold_ms_(threshold_ms), |
| 308 | start_time_ms_(start_time_ms), |
| 309 | run_time_ms_(run_time_ms), |
| 310 | creation_time_ms_(clock_->TimeInMilliseconds()), |
| 311 | capturer_(NULL), |
| 312 | rtp_start_timestamp_set_(false), |
| 313 | rtp_start_timestamp_(0) {} |
wu@webrtc.org | cd70119 | 2014-04-24 22:10:24 +0000 | [diff] [blame] | 314 | |
pbos@webrtc.org | 994d0b7 | 2014-06-27 08:47:52 +0000 | [diff] [blame^] | 315 | private: |
| 316 | virtual void RenderFrame(const I420VideoFrame& video_frame, |
| 317 | int time_to_render_ms) OVERRIDE { |
| 318 | if (video_frame.ntp_time_ms() <= 0) { |
| 319 | // Haven't got enough RTCP SR in order to calculate the capture ntp |
| 320 | // time. |
| 321 | return; |
| 322 | } |
wu@webrtc.org | cd70119 | 2014-04-24 22:10:24 +0000 | [diff] [blame] | 323 | |
pbos@webrtc.org | 994d0b7 | 2014-06-27 08:47:52 +0000 | [diff] [blame^] | 324 | int64_t now_ms = clock_->TimeInMilliseconds(); |
| 325 | int64_t time_since_creation = now_ms - creation_time_ms_; |
| 326 | if (time_since_creation < start_time_ms_) { |
| 327 | // Wait for |start_time_ms_| before start measuring. |
| 328 | return; |
| 329 | } |
wu@webrtc.org | cd70119 | 2014-04-24 22:10:24 +0000 | [diff] [blame] | 330 | |
pbos@webrtc.org | 994d0b7 | 2014-06-27 08:47:52 +0000 | [diff] [blame^] | 331 | if (time_since_creation > run_time_ms_) { |
| 332 | observation_complete_->Set(); |
| 333 | } |
wu@webrtc.org | cd70119 | 2014-04-24 22:10:24 +0000 | [diff] [blame] | 334 | |
pbos@webrtc.org | 994d0b7 | 2014-06-27 08:47:52 +0000 | [diff] [blame^] | 335 | FrameCaptureTimeList::iterator iter = |
| 336 | capture_time_list_.find(video_frame.timestamp()); |
| 337 | EXPECT_TRUE(iter != capture_time_list_.end()); |
wu@webrtc.org | cd70119 | 2014-04-24 22:10:24 +0000 | [diff] [blame] | 338 | |
pbos@webrtc.org | 994d0b7 | 2014-06-27 08:47:52 +0000 | [diff] [blame^] | 339 | // The real capture time has been wrapped to uint32_t before converted |
| 340 | // to rtp timestamp in the sender side. So here we convert the estimated |
| 341 | // capture time to a uint32_t 90k timestamp also for comparing. |
| 342 | uint32_t estimated_capture_timestamp = |
| 343 | 90 * static_cast<uint32_t>(video_frame.ntp_time_ms()); |
| 344 | uint32_t real_capture_timestamp = iter->second; |
| 345 | int time_offset_ms = real_capture_timestamp - estimated_capture_timestamp; |
| 346 | time_offset_ms = time_offset_ms / 90; |
| 347 | std::stringstream ss; |
| 348 | ss << time_offset_ms; |
wu@webrtc.org | cd70119 | 2014-04-24 22:10:24 +0000 | [diff] [blame] | 349 | |
pbos@webrtc.org | 994d0b7 | 2014-06-27 08:47:52 +0000 | [diff] [blame^] | 350 | webrtc::test::PrintResult( |
| 351 | "capture_ntp_time", "", "real - estimated", ss.str(), "ms", true); |
| 352 | EXPECT_TRUE(std::abs(time_offset_ms) < threshold_ms_); |
| 353 | } |
wu@webrtc.org | cd70119 | 2014-04-24 22:10:24 +0000 | [diff] [blame] | 354 | |
pbos@webrtc.org | 994d0b7 | 2014-06-27 08:47:52 +0000 | [diff] [blame^] | 355 | virtual Action OnSendRtp(const uint8_t* packet, size_t length) { |
| 356 | RTPHeader header; |
| 357 | EXPECT_TRUE(parser_->Parse(packet, static_cast<int>(length), &header)); |
| 358 | |
| 359 | if (!rtp_start_timestamp_set_) { |
| 360 | // Calculate the rtp timestamp offset in order to calculate the real |
| 361 | // capture time. |
| 362 | uint32_t first_capture_timestamp = |
| 363 | 90 * static_cast<uint32_t>(capturer_->first_frame_capture_time()); |
| 364 | rtp_start_timestamp_ = header.timestamp - first_capture_timestamp; |
| 365 | rtp_start_timestamp_set_ = true; |
| 366 | } |
| 367 | |
| 368 | uint32_t capture_timestamp = header.timestamp - rtp_start_timestamp_; |
| 369 | capture_time_list_.insert( |
| 370 | capture_time_list_.end(), |
| 371 | std::make_pair(header.timestamp, capture_timestamp)); |
| 372 | return SEND_PACKET; |
| 373 | } |
| 374 | |
| 375 | virtual void OnFrameGeneratorCapturerCreated( |
| 376 | test::FrameGeneratorCapturer* frame_generator_capturer) OVERRIDE { |
| 377 | capturer_ = frame_generator_capturer; |
| 378 | } |
| 379 | |
| 380 | virtual void ModifyConfigs( |
| 381 | VideoSendStream::Config* send_config, |
| 382 | VideoReceiveStream::Config* receive_config, |
| 383 | std::vector<VideoStream>* video_streams) OVERRIDE { |
| 384 | receive_config->renderer = this; |
| 385 | // Enable the receiver side rtt calculation. |
| 386 | receive_config->rtp.rtcp_xr.receiver_reference_time_report = true; |
| 387 | } |
| 388 | |
| 389 | virtual void PerformTest() OVERRIDE { |
| 390 | EXPECT_EQ(kEventSignaled, Wait()) << "Timed out while waiting for " |
| 391 | "estimated capture NTP time to be " |
| 392 | "within bounds."; |
| 393 | } |
| 394 | |
| 395 | Clock* clock_; |
| 396 | int threshold_ms_; |
| 397 | int start_time_ms_; |
| 398 | int run_time_ms_; |
| 399 | int64_t creation_time_ms_; |
| 400 | test::FrameGeneratorCapturer* capturer_; |
| 401 | bool rtp_start_timestamp_set_; |
| 402 | uint32_t rtp_start_timestamp_; |
| 403 | typedef std::map<uint32_t, uint32_t> FrameCaptureTimeList; |
| 404 | FrameCaptureTimeList capture_time_list_; |
| 405 | } test(net_config, threshold_ms, start_time_ms, run_time_ms); |
| 406 | |
| 407 | RunBaseTest(&test); |
wu@webrtc.org | cd70119 | 2014-04-24 22:10:24 +0000 | [diff] [blame] | 408 | } |
| 409 | |
wu@webrtc.org | 9aa7d8d | 2014-05-29 05:03:52 +0000 | [diff] [blame] | 410 | TEST_F(CallPerfTest, CaptureNtpTimeWithNetworkDelay) { |
wu@webrtc.org | cd70119 | 2014-04-24 22:10:24 +0000 | [diff] [blame] | 411 | FakeNetworkPipe::Config net_config; |
| 412 | net_config.queue_delay_ms = 100; |
| 413 | // TODO(wu): lower the threshold as the calculation/estimatation becomes more |
| 414 | // accurate. |
wu@webrtc.org | 9aa7d8d | 2014-05-29 05:03:52 +0000 | [diff] [blame] | 415 | const int kThresholdMs = 100; |
wu@webrtc.org | cd70119 | 2014-04-24 22:10:24 +0000 | [diff] [blame] | 416 | const int kStartTimeMs = 10000; |
| 417 | const int kRunTimeMs = 20000; |
| 418 | TestCaptureNtpTime(net_config, kThresholdMs, kStartTimeMs, kRunTimeMs); |
| 419 | } |
| 420 | |
wu@webrtc.org | 0224c20 | 2014-05-05 17:42:43 +0000 | [diff] [blame] | 421 | TEST_F(CallPerfTest, CaptureNtpTimeWithNetworkJitter) { |
wu@webrtc.org | cd70119 | 2014-04-24 22:10:24 +0000 | [diff] [blame] | 422 | FakeNetworkPipe::Config net_config; |
wu@webrtc.org | 0224c20 | 2014-05-05 17:42:43 +0000 | [diff] [blame] | 423 | net_config.queue_delay_ms = 100; |
wu@webrtc.org | cd70119 | 2014-04-24 22:10:24 +0000 | [diff] [blame] | 424 | net_config.delay_standard_deviation_ms = 10; |
| 425 | // TODO(wu): lower the threshold as the calculation/estimatation becomes more |
| 426 | // accurate. |
wu@webrtc.org | 0224c20 | 2014-05-05 17:42:43 +0000 | [diff] [blame] | 427 | const int kThresholdMs = 100; |
wu@webrtc.org | cd70119 | 2014-04-24 22:10:24 +0000 | [diff] [blame] | 428 | const int kStartTimeMs = 10000; |
| 429 | const int kRunTimeMs = 20000; |
| 430 | TestCaptureNtpTime(net_config, kThresholdMs, kStartTimeMs, kRunTimeMs); |
| 431 | } |
| 432 | |
asapersson@webrtc.org | bdc5ed2 | 2014-01-31 10:05:07 +0000 | [diff] [blame] | 433 | TEST_F(CallPerfTest, RegisterCpuOveruseObserver) { |
| 434 | // Verifies that either a normal or overuse callback is triggered. |
pbos@webrtc.org | 994d0b7 | 2014-06-27 08:47:52 +0000 | [diff] [blame^] | 435 | class OveruseCallbackObserver : public test::SendTest, |
asapersson@webrtc.org | bdc5ed2 | 2014-01-31 10:05:07 +0000 | [diff] [blame] | 436 | public webrtc::OveruseCallback { |
| 437 | public: |
pbos@webrtc.org | 994d0b7 | 2014-06-27 08:47:52 +0000 | [diff] [blame^] | 438 | OveruseCallbackObserver() : SendTest(kLongTimeoutMs) {} |
asapersson@webrtc.org | bdc5ed2 | 2014-01-31 10:05:07 +0000 | [diff] [blame] | 439 | |
| 440 | virtual void OnOveruse() OVERRIDE { |
| 441 | observation_complete_->Set(); |
| 442 | } |
pbos@webrtc.org | 994d0b7 | 2014-06-27 08:47:52 +0000 | [diff] [blame^] | 443 | |
asapersson@webrtc.org | bdc5ed2 | 2014-01-31 10:05:07 +0000 | [diff] [blame] | 444 | virtual void OnNormalUse() OVERRIDE { |
| 445 | observation_complete_->Set(); |
| 446 | } |
asapersson@webrtc.org | bdc5ed2 | 2014-01-31 10:05:07 +0000 | [diff] [blame] | 447 | |
pbos@webrtc.org | 994d0b7 | 2014-06-27 08:47:52 +0000 | [diff] [blame^] | 448 | virtual Call::Config GetSenderCallConfig() OVERRIDE { |
| 449 | Call::Config config(SendTransport()); |
| 450 | config.overuse_callback = this; |
| 451 | return config; |
| 452 | } |
asapersson@webrtc.org | bdc5ed2 | 2014-01-31 10:05:07 +0000 | [diff] [blame] | 453 | |
pbos@webrtc.org | 994d0b7 | 2014-06-27 08:47:52 +0000 | [diff] [blame^] | 454 | virtual void PerformTest() OVERRIDE { |
| 455 | EXPECT_EQ(kEventSignaled, Wait()) |
| 456 | << "Timed out before receiving an overuse callback."; |
| 457 | } |
| 458 | } test; |
| 459 | |
| 460 | RunBaseTest(&test); |
asapersson@webrtc.org | bdc5ed2 | 2014-01-31 10:05:07 +0000 | [diff] [blame] | 461 | } |
pbos@webrtc.org | 3349ae0 | 2014-03-13 12:52:27 +0000 | [diff] [blame] | 462 | |
| 463 | void CallPerfTest::TestMinTransmitBitrate(bool pad_to_min_bitrate) { |
| 464 | static const int kMaxEncodeBitrateKbps = 30; |
pbos@webrtc.org | 709e297 | 2014-03-19 10:59:52 +0000 | [diff] [blame] | 465 | static const int kMinTransmitBitrateBps = 150000; |
pbos@webrtc.org | 3349ae0 | 2014-03-13 12:52:27 +0000 | [diff] [blame] | 466 | static const int kMinAcceptableTransmitBitrate = 130; |
| 467 | static const int kMaxAcceptableTransmitBitrate = 170; |
| 468 | static const int kNumBitrateObservationsInRange = 100; |
pbos@webrtc.org | 994d0b7 | 2014-06-27 08:47:52 +0000 | [diff] [blame^] | 469 | class BitrateObserver : public test::EndToEndTest, public PacketReceiver { |
pbos@webrtc.org | 3349ae0 | 2014-03-13 12:52:27 +0000 | [diff] [blame] | 470 | public: |
| 471 | explicit BitrateObserver(bool using_min_transmit_bitrate) |
pbos@webrtc.org | 994d0b7 | 2014-06-27 08:47:52 +0000 | [diff] [blame^] | 472 | : EndToEndTest(kLongTimeoutMs), |
pbos@webrtc.org | 3349ae0 | 2014-03-13 12:52:27 +0000 | [diff] [blame] | 473 | send_stream_(NULL), |
| 474 | send_transport_receiver_(NULL), |
pbos@webrtc.org | 994d0b7 | 2014-06-27 08:47:52 +0000 | [diff] [blame^] | 475 | pad_to_min_bitrate_(using_min_transmit_bitrate), |
pbos@webrtc.org | 3349ae0 | 2014-03-13 12:52:27 +0000 | [diff] [blame] | 476 | num_bitrate_observations_in_range_(0) {} |
| 477 | |
pbos@webrtc.org | 994d0b7 | 2014-06-27 08:47:52 +0000 | [diff] [blame^] | 478 | private: |
pbos@webrtc.org | 3349ae0 | 2014-03-13 12:52:27 +0000 | [diff] [blame] | 479 | virtual void SetReceivers(PacketReceiver* send_transport_receiver, |
| 480 | PacketReceiver* receive_transport_receiver) |
| 481 | OVERRIDE { |
| 482 | send_transport_receiver_ = send_transport_receiver; |
| 483 | test::RtpRtcpObserver::SetReceivers(this, receive_transport_receiver); |
| 484 | } |
| 485 | |
pbos@webrtc.org | caba2d2 | 2014-05-14 13:57:12 +0000 | [diff] [blame] | 486 | virtual DeliveryStatus DeliverPacket(const uint8_t* packet, |
| 487 | size_t length) OVERRIDE { |
pbos@webrtc.org | 3349ae0 | 2014-03-13 12:52:27 +0000 | [diff] [blame] | 488 | VideoSendStream::Stats stats = send_stream_->GetStats(); |
| 489 | if (stats.substreams.size() > 0) { |
| 490 | assert(stats.substreams.size() == 1); |
| 491 | int bitrate_kbps = stats.substreams.begin()->second.bitrate_bps / 1000; |
| 492 | if (bitrate_kbps > 0) { |
| 493 | test::PrintResult( |
| 494 | "bitrate_stats_", |
pbos@webrtc.org | 994d0b7 | 2014-06-27 08:47:52 +0000 | [diff] [blame^] | 495 | (pad_to_min_bitrate_ ? "min_transmit_bitrate" |
| 496 | : "without_min_transmit_bitrate"), |
pbos@webrtc.org | 3349ae0 | 2014-03-13 12:52:27 +0000 | [diff] [blame] | 497 | "bitrate_kbps", |
| 498 | static_cast<size_t>(bitrate_kbps), |
| 499 | "kbps", |
| 500 | false); |
pbos@webrtc.org | 994d0b7 | 2014-06-27 08:47:52 +0000 | [diff] [blame^] | 501 | if (pad_to_min_bitrate_) { |
pbos@webrtc.org | 3349ae0 | 2014-03-13 12:52:27 +0000 | [diff] [blame] | 502 | if (bitrate_kbps > kMinAcceptableTransmitBitrate && |
| 503 | bitrate_kbps < kMaxAcceptableTransmitBitrate) { |
| 504 | ++num_bitrate_observations_in_range_; |
| 505 | } |
| 506 | } else { |
| 507 | // Expect bitrate stats to roughly match the max encode bitrate. |
| 508 | if (bitrate_kbps > kMaxEncodeBitrateKbps - 5 && |
| 509 | bitrate_kbps < kMaxEncodeBitrateKbps + 5) { |
| 510 | ++num_bitrate_observations_in_range_; |
| 511 | } |
| 512 | } |
| 513 | if (num_bitrate_observations_in_range_ == |
| 514 | kNumBitrateObservationsInRange) |
| 515 | observation_complete_->Set(); |
| 516 | } |
| 517 | } |
| 518 | return send_transport_receiver_->DeliverPacket(packet, length); |
| 519 | } |
| 520 | |
pbos@webrtc.org | 994d0b7 | 2014-06-27 08:47:52 +0000 | [diff] [blame^] | 521 | virtual void OnStreamsCreated(VideoSendStream* send_stream, |
| 522 | VideoReceiveStream* receive_stream) { |
| 523 | send_stream_ = send_stream; |
| 524 | } |
| 525 | |
| 526 | virtual void ModifyConfigs( |
| 527 | VideoSendStream::Config* send_config, |
| 528 | VideoReceiveStream::Config* receive_config, |
| 529 | std::vector<VideoStream>* video_streams) OVERRIDE { |
| 530 | if (pad_to_min_bitrate_) { |
| 531 | send_config->rtp.min_transmit_bitrate_bps = kMinTransmitBitrateBps; |
| 532 | } else { |
| 533 | assert(send_config->rtp.min_transmit_bitrate_bps == 0); |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | virtual void PerformTest() OVERRIDE { |
| 538 | EXPECT_EQ(kEventSignaled, Wait()) |
| 539 | << "Timeout while waiting for send-bitrate stats."; |
| 540 | } |
| 541 | |
pbos@webrtc.org | 3349ae0 | 2014-03-13 12:52:27 +0000 | [diff] [blame] | 542 | VideoSendStream* send_stream_; |
| 543 | PacketReceiver* send_transport_receiver_; |
pbos@webrtc.org | 994d0b7 | 2014-06-27 08:47:52 +0000 | [diff] [blame^] | 544 | const bool pad_to_min_bitrate_; |
pbos@webrtc.org | 3349ae0 | 2014-03-13 12:52:27 +0000 | [diff] [blame] | 545 | int num_bitrate_observations_in_range_; |
pbos@webrtc.org | 994d0b7 | 2014-06-27 08:47:52 +0000 | [diff] [blame^] | 546 | } test(pad_to_min_bitrate); |
pbos@webrtc.org | 3349ae0 | 2014-03-13 12:52:27 +0000 | [diff] [blame] | 547 | |
pbos@webrtc.org | 3349ae0 | 2014-03-13 12:52:27 +0000 | [diff] [blame] | 548 | fake_encoder_.SetMaxBitrate(kMaxEncodeBitrateKbps); |
pbos@webrtc.org | 994d0b7 | 2014-06-27 08:47:52 +0000 | [diff] [blame^] | 549 | RunBaseTest(&test); |
pbos@webrtc.org | 3349ae0 | 2014-03-13 12:52:27 +0000 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | TEST_F(CallPerfTest, PadsToMinTransmitBitrate) { TestMinTransmitBitrate(true); } |
| 553 | |
| 554 | TEST_F(CallPerfTest, NoPadWithoutMinTransmitBitrate) { |
| 555 | TestMinTransmitBitrate(false); |
| 556 | } |
| 557 | |
pbos@webrtc.org | 1d09690 | 2013-12-13 12:48:05 +0000 | [diff] [blame] | 558 | } // namespace webrtc |