perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 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 | |
Erik Språng | 4529fbc | 2018-10-12 10:30:31 +0200 | [diff] [blame] | 11 | #include "video/video_stream_encoder.h" |
| 12 | |
sprang | fe627f3 | 2017-03-29 08:24:59 -0700 | [diff] [blame] | 13 | #include <algorithm> |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 14 | #include <limits> |
Danil Chapovalov | d3ba236 | 2019-04-10 17:01:23 +0200 | [diff] [blame] | 15 | #include <memory> |
Per | 512ecb3 | 2016-09-23 15:52:06 +0200 | [diff] [blame] | 16 | #include <utility> |
| 17 | |
Mirta Dvornicic | 28f0eb2 | 2019-05-28 16:30:16 +0200 | [diff] [blame] | 18 | #include "absl/memory/memory.h" |
Danil Chapovalov | d3ba236 | 2019-04-10 17:01:23 +0200 | [diff] [blame] | 19 | #include "api/task_queue/default_task_queue_factory.h" |
Jiawei Ou | c2ebe21 | 2018-11-08 10:02:56 -0800 | [diff] [blame] | 20 | #include "api/video/builtin_video_bitrate_allocator_factory.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 21 | #include "api/video/i420_buffer.h" |
Erik Språng | f93eda1 | 2019-01-16 17:10:57 +0100 | [diff] [blame] | 22 | #include "api/video/video_bitrate_allocation.h" |
Erik Språng | 4529fbc | 2018-10-12 10:30:31 +0200 | [diff] [blame] | 23 | #include "api/video_codecs/vp8_temporal_layers.h" |
Elad Alon | cde8ab2 | 2019-03-20 11:56:20 +0100 | [diff] [blame] | 24 | #include "api/video_codecs/vp8_temporal_layers_factory.h" |
Mirta Dvornicic | 28f0eb2 | 2019-05-28 16:30:16 +0200 | [diff] [blame] | 25 | #include "common_video/h264/h264_common.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 26 | #include "media/base/video_adapter.h" |
Sergey Silkin | 8668496 | 2018-03-28 19:32:37 +0200 | [diff] [blame] | 27 | #include "modules/video_coding/codecs/vp9/include/vp9_globals.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 28 | #include "modules/video_coding/utility/default_video_bitrate_allocator.h" |
Åsa Persson | c29cb2c | 2019-03-25 12:06:59 +0100 | [diff] [blame] | 29 | #include "modules/video_coding/utility/simulcast_rate_allocator.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 30 | #include "rtc_base/fake_clock.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 31 | #include "rtc_base/logging.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 32 | #include "rtc_base/ref_counted_object.h" |
Erik Språng | 7ca375c | 2019-02-06 16:20:17 +0100 | [diff] [blame] | 33 | #include "system_wrappers/include/field_trial.h" |
Mirko Bonadei | 17f4878 | 2018-09-28 08:51:10 +0200 | [diff] [blame] | 34 | #include "system_wrappers/include/metrics.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 35 | #include "system_wrappers/include/sleep.h" |
| 36 | #include "test/encoder_settings.h" |
| 37 | #include "test/fake_encoder.h" |
Kári Tristan Helgason | 639602a | 2018-08-02 10:51:40 +0200 | [diff] [blame] | 38 | #include "test/field_trial.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 39 | #include "test/frame_generator.h" |
| 40 | #include "test/gmock.h" |
| 41 | #include "test/gtest.h" |
Niels Möller | cbcbc22 | 2018-09-28 09:07:24 +0200 | [diff] [blame] | 42 | #include "test/video_encoder_proxy_factory.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 43 | #include "video/send_statistics_proxy.h" |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 44 | |
| 45 | namespace webrtc { |
| 46 | |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 47 | using ScaleReason = AdaptationObserverInterface::AdaptReason; |
sprang | 57c2fff | 2017-01-16 06:24:02 -0800 | [diff] [blame] | 48 | using ::testing::_; |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 49 | |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 50 | namespace { |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 51 | const int kMinPixelsPerFrame = 320 * 180; |
| 52 | const int kMinFramerateFps = 2; |
| 53 | const int kMinBalancedFramerateFps = 7; |
| 54 | const int64_t kFrameTimeoutMs = 100; |
asapersson | 5f7226f | 2016-11-25 04:37:00 -0800 | [diff] [blame] | 55 | const size_t kMaxPayloadLength = 1440; |
Erik Språng | d7329ca | 2019-02-21 21:19:53 +0100 | [diff] [blame] | 56 | const uint32_t kTargetBitrateBps = 1000000; |
| 57 | const uint32_t kSimulcastTargetBitrateBps = 3150000; |
| 58 | const uint32_t kLowTargetBitrateBps = kTargetBitrateBps / 10; |
kthelgason | 2bc6864 | 2017-02-07 07:02:22 -0800 | [diff] [blame] | 59 | const int kMaxInitialFramedrop = 4; |
sprang | fda496a | 2017-06-15 04:21:07 -0700 | [diff] [blame] | 60 | const int kDefaultFramerate = 30; |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 61 | const int64_t kFrameIntervalMs = rtc::kNumMillisecsPerSec / kDefaultFramerate; |
asapersson | 5f7226f | 2016-11-25 04:37:00 -0800 | [diff] [blame] | 62 | |
Mirta Dvornicic | 28f0eb2 | 2019-05-28 16:30:16 +0200 | [diff] [blame] | 63 | uint8_t optimal_sps[] = {0, 0, 0, 1, H264::NaluType::kSps, |
| 64 | 0x00, 0x00, 0x03, 0x03, 0xF4, |
| 65 | 0x05, 0x03, 0xC7, 0xE0, 0x1B, |
| 66 | 0x41, 0x10, 0x8D, 0x00}; |
| 67 | |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 68 | class TestBuffer : public webrtc::I420Buffer { |
| 69 | public: |
| 70 | TestBuffer(rtc::Event* event, int width, int height) |
| 71 | : I420Buffer(width, height), event_(event) {} |
| 72 | |
| 73 | private: |
| 74 | friend class rtc::RefCountedObject<TestBuffer>; |
| 75 | ~TestBuffer() override { |
| 76 | if (event_) |
| 77 | event_->Set(); |
| 78 | } |
| 79 | rtc::Event* const event_; |
| 80 | }; |
| 81 | |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 82 | class CpuOveruseDetectorProxy : public OveruseFrameDetector { |
| 83 | public: |
Niels Möller | d1f7eb6 | 2018-03-28 16:40:58 +0200 | [diff] [blame] | 84 | explicit CpuOveruseDetectorProxy(CpuOveruseMetricsObserver* metrics_observer) |
| 85 | : OveruseFrameDetector(metrics_observer), |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 86 | last_target_framerate_fps_(-1) {} |
| 87 | virtual ~CpuOveruseDetectorProxy() {} |
| 88 | |
| 89 | void OnTargetFramerateUpdated(int framerate_fps) override { |
| 90 | rtc::CritScope cs(&lock_); |
| 91 | last_target_framerate_fps_ = framerate_fps; |
| 92 | OveruseFrameDetector::OnTargetFramerateUpdated(framerate_fps); |
| 93 | } |
| 94 | |
| 95 | int GetLastTargetFramerate() { |
| 96 | rtc::CritScope cs(&lock_); |
| 97 | return last_target_framerate_fps_; |
| 98 | } |
| 99 | |
Niels Möller | 4db138e | 2018-04-19 09:04:13 +0200 | [diff] [blame] | 100 | CpuOveruseOptions GetOptions() { return options_; } |
| 101 | |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 102 | private: |
| 103 | rtc::CriticalSection lock_; |
| 104 | int last_target_framerate_fps_ RTC_GUARDED_BY(lock_); |
| 105 | }; |
| 106 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 107 | class VideoStreamEncoderUnderTest : public VideoStreamEncoder { |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 108 | public: |
Niels Möller | 213618e | 2018-07-24 09:29:58 +0200 | [diff] [blame] | 109 | VideoStreamEncoderUnderTest(SendStatisticsProxy* stats_proxy, |
Danil Chapovalov | d3ba236 | 2019-04-10 17:01:23 +0200 | [diff] [blame] | 110 | const VideoStreamEncoderSettings& settings, |
| 111 | TaskQueueFactory* task_queue_factory) |
Sebastian Jansson | 572c60f | 2019-03-04 18:30:41 +0100 | [diff] [blame] | 112 | : VideoStreamEncoder(Clock::GetRealTimeClock(), |
| 113 | 1 /* number_of_cores */, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 114 | stats_proxy, |
| 115 | settings, |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 116 | std::unique_ptr<OveruseFrameDetector>( |
| 117 | overuse_detector_proxy_ = |
Sebastian Jansson | 74682c1 | 2019-03-01 11:50:20 +0100 | [diff] [blame] | 118 | new CpuOveruseDetectorProxy(stats_proxy)), |
Danil Chapovalov | d3ba236 | 2019-04-10 17:01:23 +0200 | [diff] [blame] | 119 | task_queue_factory) {} |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 120 | |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 121 | void PostTaskAndWait(bool down, AdaptReason reason) { |
Niels Möller | c572ff3 | 2018-11-07 08:43:50 +0100 | [diff] [blame] | 122 | rtc::Event event; |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 123 | encoder_queue()->PostTask([this, &event, reason, down] { |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 124 | down ? AdaptDown(reason) : AdaptUp(reason); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 125 | event.Set(); |
| 126 | }); |
perkj | 070ba85 | 2017-02-16 15:46:27 -0800 | [diff] [blame] | 127 | ASSERT_TRUE(event.Wait(5000)); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 128 | } |
| 129 | |
kthelgason | 2fc5254 | 2017-03-03 00:24:41 -0800 | [diff] [blame] | 130 | // This is used as a synchronisation mechanism, to make sure that the |
| 131 | // encoder queue is not blocked before we start sending it frames. |
| 132 | void WaitUntilTaskQueueIsIdle() { |
Niels Möller | c572ff3 | 2018-11-07 08:43:50 +0100 | [diff] [blame] | 133 | rtc::Event event; |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 134 | encoder_queue()->PostTask([&event] { event.Set(); }); |
kthelgason | 2fc5254 | 2017-03-03 00:24:41 -0800 | [diff] [blame] | 135 | ASSERT_TRUE(event.Wait(5000)); |
| 136 | } |
| 137 | |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 138 | void TriggerCpuOveruse() { PostTaskAndWait(true, AdaptReason::kCpu); } |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 139 | |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 140 | void TriggerCpuNormalUsage() { PostTaskAndWait(false, AdaptReason::kCpu); } |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 141 | |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 142 | void TriggerQualityLow() { PostTaskAndWait(true, AdaptReason::kQuality); } |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 143 | |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 144 | void TriggerQualityHigh() { PostTaskAndWait(false, AdaptReason::kQuality); } |
sprang | fda496a | 2017-06-15 04:21:07 -0700 | [diff] [blame] | 145 | |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 146 | CpuOveruseDetectorProxy* overuse_detector_proxy_; |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 147 | }; |
| 148 | |
asapersson | 5f7226f | 2016-11-25 04:37:00 -0800 | [diff] [blame] | 149 | class VideoStreamFactory |
| 150 | : public VideoEncoderConfig::VideoStreamFactoryInterface { |
| 151 | public: |
sprang | fda496a | 2017-06-15 04:21:07 -0700 | [diff] [blame] | 152 | explicit VideoStreamFactory(size_t num_temporal_layers, int framerate) |
| 153 | : num_temporal_layers_(num_temporal_layers), framerate_(framerate) { |
asapersson | 5f7226f | 2016-11-25 04:37:00 -0800 | [diff] [blame] | 154 | EXPECT_GT(num_temporal_layers, 0u); |
sprang | fda496a | 2017-06-15 04:21:07 -0700 | [diff] [blame] | 155 | EXPECT_GT(framerate, 0); |
asapersson | 5f7226f | 2016-11-25 04:37:00 -0800 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | private: |
| 159 | std::vector<VideoStream> CreateEncoderStreams( |
| 160 | int width, |
| 161 | int height, |
| 162 | const VideoEncoderConfig& encoder_config) override { |
| 163 | std::vector<VideoStream> streams = |
| 164 | test::CreateVideoStreams(width, height, encoder_config); |
| 165 | for (VideoStream& stream : streams) { |
Sergey Silkin | a796a7e | 2018-03-01 15:11:29 +0100 | [diff] [blame] | 166 | stream.num_temporal_layers = num_temporal_layers_; |
sprang | fda496a | 2017-06-15 04:21:07 -0700 | [diff] [blame] | 167 | stream.max_framerate = framerate_; |
asapersson | 5f7226f | 2016-11-25 04:37:00 -0800 | [diff] [blame] | 168 | } |
| 169 | return streams; |
| 170 | } |
sprang | fda496a | 2017-06-15 04:21:07 -0700 | [diff] [blame] | 171 | |
asapersson | 5f7226f | 2016-11-25 04:37:00 -0800 | [diff] [blame] | 172 | const size_t num_temporal_layers_; |
sprang | fda496a | 2017-06-15 04:21:07 -0700 | [diff] [blame] | 173 | const int framerate_; |
asapersson | 5f7226f | 2016-11-25 04:37:00 -0800 | [diff] [blame] | 174 | }; |
| 175 | |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 176 | class AdaptingFrameForwarder : public test::FrameForwarder { |
| 177 | public: |
| 178 | AdaptingFrameForwarder() : adaptation_enabled_(false) {} |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 179 | ~AdaptingFrameForwarder() override {} |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 180 | |
| 181 | void set_adaptation_enabled(bool enabled) { |
| 182 | rtc::CritScope cs(&crit_); |
| 183 | adaptation_enabled_ = enabled; |
| 184 | } |
| 185 | |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 186 | bool adaption_enabled() const { |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 187 | rtc::CritScope cs(&crit_); |
| 188 | return adaptation_enabled_; |
| 189 | } |
| 190 | |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 191 | rtc::VideoSinkWants last_wants() const { |
| 192 | rtc::CritScope cs(&crit_); |
| 193 | return last_wants_; |
| 194 | } |
| 195 | |
Danil Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame] | 196 | absl::optional<int> last_sent_width() const { return last_width_; } |
| 197 | absl::optional<int> last_sent_height() const { return last_height_; } |
Jonathan Yu | bc771b7 | 2017-12-08 17:04:29 -0800 | [diff] [blame] | 198 | |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 199 | void IncomingCapturedFrame(const VideoFrame& video_frame) override { |
| 200 | int cropped_width = 0; |
| 201 | int cropped_height = 0; |
| 202 | int out_width = 0; |
| 203 | int out_height = 0; |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 204 | if (adaption_enabled()) { |
| 205 | if (adapter_.AdaptFrameResolution( |
| 206 | video_frame.width(), video_frame.height(), |
| 207 | video_frame.timestamp_us() * 1000, &cropped_width, |
| 208 | &cropped_height, &out_width, &out_height)) { |
Artem Titov | 1ebfb6a | 2019-01-03 23:49:37 +0100 | [diff] [blame] | 209 | VideoFrame adapted_frame = |
| 210 | VideoFrame::Builder() |
| 211 | .set_video_frame_buffer(new rtc::RefCountedObject<TestBuffer>( |
| 212 | nullptr, out_width, out_height)) |
| 213 | .set_timestamp_rtp(99) |
| 214 | .set_timestamp_ms(99) |
| 215 | .set_rotation(kVideoRotation_0) |
| 216 | .build(); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 217 | adapted_frame.set_ntp_time_ms(video_frame.ntp_time_ms()); |
| 218 | test::FrameForwarder::IncomingCapturedFrame(adapted_frame); |
Jonathan Yu | bc771b7 | 2017-12-08 17:04:29 -0800 | [diff] [blame] | 219 | last_width_.emplace(adapted_frame.width()); |
| 220 | last_height_.emplace(adapted_frame.height()); |
| 221 | } else { |
Danil Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame] | 222 | last_width_ = absl::nullopt; |
| 223 | last_height_ = absl::nullopt; |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 224 | } |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 225 | } else { |
| 226 | test::FrameForwarder::IncomingCapturedFrame(video_frame); |
Jonathan Yu | bc771b7 | 2017-12-08 17:04:29 -0800 | [diff] [blame] | 227 | last_width_.emplace(video_frame.width()); |
| 228 | last_height_.emplace(video_frame.height()); |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 229 | } |
| 230 | } |
| 231 | |
| 232 | void AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink, |
| 233 | const rtc::VideoSinkWants& wants) override { |
| 234 | rtc::CritScope cs(&crit_); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 235 | last_wants_ = sink_wants(); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 236 | adapter_.OnResolutionFramerateRequest(wants.target_pixel_count, |
| 237 | wants.max_pixel_count, |
| 238 | wants.max_framerate_fps); |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 239 | test::FrameForwarder::AddOrUpdateSink(sink, wants); |
| 240 | } |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 241 | cricket::VideoAdapter adapter_; |
danilchap | a37de39 | 2017-09-09 04:17:22 -0700 | [diff] [blame] | 242 | bool adaptation_enabled_ RTC_GUARDED_BY(crit_); |
| 243 | rtc::VideoSinkWants last_wants_ RTC_GUARDED_BY(crit_); |
Danil Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame] | 244 | absl::optional<int> last_width_; |
| 245 | absl::optional<int> last_height_; |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 246 | }; |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 247 | |
Niels Möller | 213618e | 2018-07-24 09:29:58 +0200 | [diff] [blame] | 248 | // TODO(nisse): Mock only VideoStreamEncoderObserver. |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 249 | class MockableSendStatisticsProxy : public SendStatisticsProxy { |
| 250 | public: |
| 251 | MockableSendStatisticsProxy(Clock* clock, |
| 252 | const VideoSendStream::Config& config, |
| 253 | VideoEncoderConfig::ContentType content_type) |
| 254 | : SendStatisticsProxy(clock, config, content_type) {} |
| 255 | |
| 256 | VideoSendStream::Stats GetStats() override { |
| 257 | rtc::CritScope cs(&lock_); |
| 258 | if (mock_stats_) |
| 259 | return *mock_stats_; |
| 260 | return SendStatisticsProxy::GetStats(); |
| 261 | } |
| 262 | |
Niels Möller | 213618e | 2018-07-24 09:29:58 +0200 | [diff] [blame] | 263 | int GetInputFrameRate() const override { |
| 264 | rtc::CritScope cs(&lock_); |
| 265 | if (mock_stats_) |
| 266 | return mock_stats_->input_frame_rate; |
| 267 | return SendStatisticsProxy::GetInputFrameRate(); |
| 268 | } |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 269 | void SetMockStats(const VideoSendStream::Stats& stats) { |
| 270 | rtc::CritScope cs(&lock_); |
| 271 | mock_stats_.emplace(stats); |
| 272 | } |
| 273 | |
| 274 | void ResetMockStats() { |
| 275 | rtc::CritScope cs(&lock_); |
| 276 | mock_stats_.reset(); |
| 277 | } |
| 278 | |
| 279 | private: |
| 280 | rtc::CriticalSection lock_; |
Danil Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame] | 281 | absl::optional<VideoSendStream::Stats> mock_stats_ RTC_GUARDED_BY(lock_); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 282 | }; |
| 283 | |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 284 | class MockBitrateObserver : public VideoBitrateAllocationObserver { |
| 285 | public: |
Erik Språng | 566124a | 2018-04-23 12:32:22 +0200 | [diff] [blame] | 286 | MOCK_METHOD1(OnBitrateAllocationUpdated, void(const VideoBitrateAllocation&)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 287 | }; |
| 288 | |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 289 | } // namespace |
| 290 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 291 | class VideoStreamEncoderTest : public ::testing::Test { |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 292 | public: |
| 293 | static const int kDefaultTimeoutMs = 30 * 1000; |
| 294 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 295 | VideoStreamEncoderTest() |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 296 | : video_send_config_(VideoSendStream::Config(nullptr)), |
perkj | fa10b55 | 2016-10-02 23:45:26 -0700 | [diff] [blame] | 297 | codec_width_(320), |
| 298 | codec_height_(240), |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 299 | max_framerate_(kDefaultFramerate), |
Danil Chapovalov | d3ba236 | 2019-04-10 17:01:23 +0200 | [diff] [blame] | 300 | task_queue_factory_(CreateDefaultTaskQueueFactory()), |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 301 | fake_encoder_(), |
Niels Möller | 4db138e | 2018-04-19 09:04:13 +0200 | [diff] [blame] | 302 | encoder_factory_(&fake_encoder_), |
Jiawei Ou | c2ebe21 | 2018-11-08 10:02:56 -0800 | [diff] [blame] | 303 | bitrate_allocator_factory_(CreateBuiltinVideoBitrateAllocatorFactory()), |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 304 | stats_proxy_(new MockableSendStatisticsProxy( |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 305 | Clock::GetRealTimeClock(), |
| 306 | video_send_config_, |
| 307 | webrtc::VideoEncoderConfig::ContentType::kRealtimeVideo)), |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 308 | sink_(&fake_encoder_) {} |
| 309 | |
| 310 | void SetUp() override { |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 311 | metrics::Reset(); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 312 | video_send_config_ = VideoSendStream::Config(nullptr); |
Niels Möller | 4db138e | 2018-04-19 09:04:13 +0200 | [diff] [blame] | 313 | video_send_config_.encoder_settings.encoder_factory = &encoder_factory_; |
Jiawei Ou | c2ebe21 | 2018-11-08 10:02:56 -0800 | [diff] [blame] | 314 | video_send_config_.encoder_settings.bitrate_allocator_factory = |
| 315 | bitrate_allocator_factory_.get(); |
Niels Möller | 259a497 | 2018-04-05 15:36:51 +0200 | [diff] [blame] | 316 | video_send_config_.rtp.payload_name = "FAKE"; |
| 317 | video_send_config_.rtp.payload_type = 125; |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 318 | |
Per | 512ecb3 | 2016-09-23 15:52:06 +0200 | [diff] [blame] | 319 | VideoEncoderConfig video_encoder_config; |
Niels Möller | 259a497 | 2018-04-05 15:36:51 +0200 | [diff] [blame] | 320 | test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 321 | video_encoder_config.video_stream_factory = |
| 322 | new rtc::RefCountedObject<VideoStreamFactory>(1, max_framerate_); |
Erik Språng | 08127a9 | 2016-11-16 16:41:30 +0100 | [diff] [blame] | 323 | video_encoder_config_ = video_encoder_config.Copy(); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 324 | |
| 325 | // Framerate limit is specified by the VideoStreamFactory. |
| 326 | std::vector<VideoStream> streams = |
| 327 | video_encoder_config.video_stream_factory->CreateEncoderStreams( |
| 328 | codec_width_, codec_height_, video_encoder_config); |
| 329 | max_framerate_ = streams[0].max_framerate; |
Sebastian Jansson | 40889f3 | 2019-04-17 12:11:20 +0200 | [diff] [blame] | 330 | fake_clock_.SetTime(Timestamp::us(1234)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 331 | |
Niels Möller | f133856 | 2018-04-26 09:51:47 +0200 | [diff] [blame] | 332 | ConfigureEncoder(std::move(video_encoder_config)); |
asapersson | 5f7226f | 2016-11-25 04:37:00 -0800 | [diff] [blame] | 333 | } |
| 334 | |
Niels Möller | f133856 | 2018-04-26 09:51:47 +0200 | [diff] [blame] | 335 | void ConfigureEncoder(VideoEncoderConfig video_encoder_config) { |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 336 | if (video_stream_encoder_) |
| 337 | video_stream_encoder_->Stop(); |
| 338 | video_stream_encoder_.reset(new VideoStreamEncoderUnderTest( |
Danil Chapovalov | d3ba236 | 2019-04-10 17:01:23 +0200 | [diff] [blame] | 339 | stats_proxy_.get(), video_send_config_.encoder_settings, |
| 340 | task_queue_factory_.get())); |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 341 | video_stream_encoder_->SetSink(&sink_, false /* rotation_applied */); |
| 342 | video_stream_encoder_->SetSource( |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 343 | &video_source_, webrtc::DegradationPreference::MAINTAIN_FRAMERATE); |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 344 | video_stream_encoder_->SetStartBitrate(kTargetBitrateBps); |
| 345 | video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config), |
Niels Möller | f133856 | 2018-04-26 09:51:47 +0200 | [diff] [blame] | 346 | kMaxPayloadLength); |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 347 | video_stream_encoder_->WaitUntilTaskQueueIsIdle(); |
asapersson | 5f7226f | 2016-11-25 04:37:00 -0800 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | void ResetEncoder(const std::string& payload_name, |
| 351 | size_t num_streams, |
| 352 | size_t num_temporal_layers, |
emircan | bbcc356 | 2017-08-18 00:28:40 -0700 | [diff] [blame] | 353 | unsigned char num_spatial_layers, |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 354 | bool screenshare) { |
Niels Möller | 259a497 | 2018-04-05 15:36:51 +0200 | [diff] [blame] | 355 | video_send_config_.rtp.payload_name = payload_name; |
asapersson | 5f7226f | 2016-11-25 04:37:00 -0800 | [diff] [blame] | 356 | |
| 357 | VideoEncoderConfig video_encoder_config; |
Niels Möller | 259a497 | 2018-04-05 15:36:51 +0200 | [diff] [blame] | 358 | video_encoder_config.codec_type = PayloadStringToCodecType(payload_name); |
asapersson | 5f7226f | 2016-11-25 04:37:00 -0800 | [diff] [blame] | 359 | video_encoder_config.number_of_streams = num_streams; |
Erik Språng | d7329ca | 2019-02-21 21:19:53 +0100 | [diff] [blame] | 360 | video_encoder_config.max_bitrate_bps = |
| 361 | num_streams == 1 ? kTargetBitrateBps : kSimulcastTargetBitrateBps; |
asapersson | 5f7226f | 2016-11-25 04:37:00 -0800 | [diff] [blame] | 362 | video_encoder_config.video_stream_factory = |
sprang | fda496a | 2017-06-15 04:21:07 -0700 | [diff] [blame] | 363 | new rtc::RefCountedObject<VideoStreamFactory>(num_temporal_layers, |
| 364 | kDefaultFramerate); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 365 | video_encoder_config.content_type = |
| 366 | screenshare ? VideoEncoderConfig::ContentType::kScreen |
| 367 | : VideoEncoderConfig::ContentType::kRealtimeVideo; |
emircan | bbcc356 | 2017-08-18 00:28:40 -0700 | [diff] [blame] | 368 | if (payload_name == "VP9") { |
| 369 | VideoCodecVP9 vp9_settings = VideoEncoder::GetDefaultVp9Settings(); |
| 370 | vp9_settings.numberOfSpatialLayers = num_spatial_layers; |
| 371 | video_encoder_config.encoder_specific_settings = |
| 372 | new rtc::RefCountedObject< |
| 373 | VideoEncoderConfig::Vp9EncoderSpecificSettings>(vp9_settings); |
| 374 | } |
Niels Möller | f133856 | 2018-04-26 09:51:47 +0200 | [diff] [blame] | 375 | ConfigureEncoder(std::move(video_encoder_config)); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 376 | } |
| 377 | |
sprang | 57c2fff | 2017-01-16 06:24:02 -0800 | [diff] [blame] | 378 | VideoFrame CreateFrame(int64_t ntp_time_ms, |
| 379 | rtc::Event* destruction_event) const { |
Artem Titov | 1ebfb6a | 2019-01-03 23:49:37 +0100 | [diff] [blame] | 380 | VideoFrame frame = |
| 381 | VideoFrame::Builder() |
| 382 | .set_video_frame_buffer(new rtc::RefCountedObject<TestBuffer>( |
| 383 | destruction_event, codec_width_, codec_height_)) |
| 384 | .set_timestamp_rtp(99) |
| 385 | .set_timestamp_ms(99) |
| 386 | .set_rotation(kVideoRotation_0) |
| 387 | .build(); |
sprang | 57c2fff | 2017-01-16 06:24:02 -0800 | [diff] [blame] | 388 | frame.set_ntp_time_ms(ntp_time_ms); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 389 | return frame; |
| 390 | } |
| 391 | |
Ilya Nikolaevskiy | 71aee3a | 2019-02-18 13:01:26 +0100 | [diff] [blame] | 392 | VideoFrame CreateFrameWithUpdatedPixel(int64_t ntp_time_ms, |
| 393 | rtc::Event* destruction_event, |
| 394 | int offset_x) const { |
| 395 | VideoFrame frame = |
| 396 | VideoFrame::Builder() |
| 397 | .set_video_frame_buffer(new rtc::RefCountedObject<TestBuffer>( |
| 398 | destruction_event, codec_width_, codec_height_)) |
| 399 | .set_timestamp_rtp(99) |
| 400 | .set_timestamp_ms(99) |
| 401 | .set_rotation(kVideoRotation_0) |
| 402 | .set_update_rect({offset_x, 0, 1, 1}) |
| 403 | .build(); |
| 404 | frame.set_ntp_time_ms(ntp_time_ms); |
| 405 | return frame; |
| 406 | } |
| 407 | |
sprang | 57c2fff | 2017-01-16 06:24:02 -0800 | [diff] [blame] | 408 | VideoFrame CreateFrame(int64_t ntp_time_ms, int width, int height) const { |
Artem Titov | 1ebfb6a | 2019-01-03 23:49:37 +0100 | [diff] [blame] | 409 | VideoFrame frame = |
| 410 | VideoFrame::Builder() |
| 411 | .set_video_frame_buffer( |
| 412 | new rtc::RefCountedObject<TestBuffer>(nullptr, width, height)) |
| 413 | .set_timestamp_rtp(99) |
| 414 | .set_timestamp_ms(99) |
| 415 | .set_rotation(kVideoRotation_0) |
| 416 | .build(); |
sprang | 57c2fff | 2017-01-16 06:24:02 -0800 | [diff] [blame] | 417 | frame.set_ntp_time_ms(ntp_time_ms); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 418 | frame.set_timestamp_us(ntp_time_ms * 1000); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 419 | return frame; |
| 420 | } |
| 421 | |
Åsa Persson | c29cb2c | 2019-03-25 12:06:59 +0100 | [diff] [blame] | 422 | void VerifyAllocatedBitrate(const VideoBitrateAllocation& expected_bitrate) { |
| 423 | MockBitrateObserver bitrate_observer; |
| 424 | video_stream_encoder_->SetBitrateAllocationObserver(&bitrate_observer); |
| 425 | |
| 426 | EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(expected_bitrate)) |
| 427 | .Times(1); |
| 428 | video_stream_encoder_->OnBitrateUpdated(DataRate::bps(kTargetBitrateBps), |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 429 | DataRate::bps(kTargetBitrateBps), 0, |
| 430 | 0); |
Åsa Persson | c29cb2c | 2019-03-25 12:06:59 +0100 | [diff] [blame] | 431 | |
| 432 | video_source_.IncomingCapturedFrame( |
| 433 | CreateFrame(1, codec_width_, codec_height_)); |
| 434 | WaitForEncodedFrame(1); |
| 435 | } |
| 436 | |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 437 | void VerifyNoLimitation(const rtc::VideoSinkWants& wants) { |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 438 | EXPECT_EQ(std::numeric_limits<int>::max(), wants.max_framerate_fps); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 439 | EXPECT_EQ(std::numeric_limits<int>::max(), wants.max_pixel_count); |
| 440 | EXPECT_FALSE(wants.target_pixel_count); |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 441 | } |
| 442 | |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 443 | void VerifyFpsEqResolutionEq(const rtc::VideoSinkWants& wants1, |
| 444 | const rtc::VideoSinkWants& wants2) { |
| 445 | EXPECT_EQ(wants1.max_framerate_fps, wants2.max_framerate_fps); |
| 446 | EXPECT_EQ(wants1.max_pixel_count, wants2.max_pixel_count); |
| 447 | } |
| 448 | |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 449 | void VerifyFpsMaxResolutionMax(const rtc::VideoSinkWants& wants) { |
| 450 | EXPECT_EQ(kDefaultFramerate, wants.max_framerate_fps); |
| 451 | EXPECT_EQ(std::numeric_limits<int>::max(), wants.max_pixel_count); |
| 452 | EXPECT_FALSE(wants.target_pixel_count); |
| 453 | } |
| 454 | |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 455 | void VerifyFpsMaxResolutionLt(const rtc::VideoSinkWants& wants1, |
| 456 | const rtc::VideoSinkWants& wants2) { |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 457 | EXPECT_EQ(kDefaultFramerate, wants1.max_framerate_fps); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 458 | EXPECT_LT(wants1.max_pixel_count, wants2.max_pixel_count); |
| 459 | EXPECT_GT(wants1.max_pixel_count, 0); |
| 460 | } |
| 461 | |
| 462 | void VerifyFpsMaxResolutionGt(const rtc::VideoSinkWants& wants1, |
| 463 | const rtc::VideoSinkWants& wants2) { |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 464 | EXPECT_EQ(kDefaultFramerate, wants1.max_framerate_fps); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 465 | EXPECT_GT(wants1.max_pixel_count, wants2.max_pixel_count); |
| 466 | } |
| 467 | |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 468 | void VerifyFpsMaxResolutionEq(const rtc::VideoSinkWants& wants1, |
| 469 | const rtc::VideoSinkWants& wants2) { |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 470 | EXPECT_EQ(kDefaultFramerate, wants1.max_framerate_fps); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 471 | EXPECT_EQ(wants1.max_pixel_count, wants2.max_pixel_count); |
| 472 | } |
| 473 | |
| 474 | void VerifyFpsLtResolutionEq(const rtc::VideoSinkWants& wants1, |
| 475 | const rtc::VideoSinkWants& wants2) { |
| 476 | EXPECT_LT(wants1.max_framerate_fps, wants2.max_framerate_fps); |
| 477 | EXPECT_EQ(wants1.max_pixel_count, wants2.max_pixel_count); |
| 478 | } |
| 479 | |
| 480 | void VerifyFpsGtResolutionEq(const rtc::VideoSinkWants& wants1, |
| 481 | const rtc::VideoSinkWants& wants2) { |
| 482 | EXPECT_GT(wants1.max_framerate_fps, wants2.max_framerate_fps); |
| 483 | EXPECT_EQ(wants1.max_pixel_count, wants2.max_pixel_count); |
| 484 | } |
| 485 | |
| 486 | void VerifyFpsEqResolutionLt(const rtc::VideoSinkWants& wants1, |
| 487 | const rtc::VideoSinkWants& wants2) { |
| 488 | EXPECT_EQ(wants1.max_framerate_fps, wants2.max_framerate_fps); |
| 489 | EXPECT_LT(wants1.max_pixel_count, wants2.max_pixel_count); |
| 490 | EXPECT_GT(wants1.max_pixel_count, 0); |
| 491 | } |
| 492 | |
| 493 | void VerifyFpsEqResolutionGt(const rtc::VideoSinkWants& wants1, |
| 494 | const rtc::VideoSinkWants& wants2) { |
| 495 | EXPECT_EQ(wants1.max_framerate_fps, wants2.max_framerate_fps); |
| 496 | EXPECT_GT(wants1.max_pixel_count, wants2.max_pixel_count); |
| 497 | } |
| 498 | |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 499 | void VerifyFpsMaxResolutionLt(const rtc::VideoSinkWants& wants, |
| 500 | int pixel_count) { |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 501 | EXPECT_EQ(kDefaultFramerate, wants.max_framerate_fps); |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 502 | EXPECT_LT(wants.max_pixel_count, pixel_count); |
| 503 | EXPECT_GT(wants.max_pixel_count, 0); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 504 | } |
| 505 | |
| 506 | void VerifyFpsLtResolutionMax(const rtc::VideoSinkWants& wants, int fps) { |
| 507 | EXPECT_LT(wants.max_framerate_fps, fps); |
| 508 | EXPECT_EQ(std::numeric_limits<int>::max(), wants.max_pixel_count); |
| 509 | EXPECT_FALSE(wants.target_pixel_count); |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 510 | } |
| 511 | |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 512 | void VerifyFpsEqResolutionMax(const rtc::VideoSinkWants& wants, |
| 513 | int expected_fps) { |
| 514 | EXPECT_EQ(expected_fps, wants.max_framerate_fps); |
| 515 | EXPECT_EQ(std::numeric_limits<int>::max(), wants.max_pixel_count); |
| 516 | EXPECT_FALSE(wants.target_pixel_count); |
| 517 | } |
| 518 | |
Jonathan Yu | bc771b7 | 2017-12-08 17:04:29 -0800 | [diff] [blame] | 519 | void VerifyBalancedModeFpsRange(const rtc::VideoSinkWants& wants, |
| 520 | int last_frame_pixels) { |
| 521 | // Balanced mode should always scale FPS to the desired range before |
| 522 | // attempting to scale resolution. |
| 523 | int fps_limit = wants.max_framerate_fps; |
| 524 | if (last_frame_pixels <= 320 * 240) { |
| 525 | EXPECT_TRUE(7 <= fps_limit && fps_limit <= 10); |
| 526 | } else if (last_frame_pixels <= 480 * 270) { |
| 527 | EXPECT_TRUE(10 <= fps_limit && fps_limit <= 15); |
| 528 | } else if (last_frame_pixels <= 640 * 480) { |
| 529 | EXPECT_LE(15, fps_limit); |
| 530 | } else { |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 531 | EXPECT_EQ(kDefaultFramerate, fps_limit); |
Jonathan Yu | bc771b7 | 2017-12-08 17:04:29 -0800 | [diff] [blame] | 532 | } |
| 533 | } |
| 534 | |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 535 | void WaitForEncodedFrame(int64_t expected_ntp_time) { |
| 536 | sink_.WaitForEncodedFrame(expected_ntp_time); |
Sebastian Jansson | 40889f3 | 2019-04-17 12:11:20 +0200 | [diff] [blame] | 537 | fake_clock_.AdvanceTime(TimeDelta::seconds(1) / max_framerate_); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 538 | } |
| 539 | |
| 540 | bool TimedWaitForEncodedFrame(int64_t expected_ntp_time, int64_t timeout_ms) { |
| 541 | bool ok = sink_.TimedWaitForEncodedFrame(expected_ntp_time, timeout_ms); |
Sebastian Jansson | 40889f3 | 2019-04-17 12:11:20 +0200 | [diff] [blame] | 542 | fake_clock_.AdvanceTime(TimeDelta::seconds(1) / max_framerate_); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 543 | return ok; |
| 544 | } |
| 545 | |
| 546 | void WaitForEncodedFrame(uint32_t expected_width, uint32_t expected_height) { |
| 547 | sink_.WaitForEncodedFrame(expected_width, expected_height); |
Sebastian Jansson | 40889f3 | 2019-04-17 12:11:20 +0200 | [diff] [blame] | 548 | fake_clock_.AdvanceTime(TimeDelta::seconds(1) / max_framerate_); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | void ExpectDroppedFrame() { |
| 552 | sink_.ExpectDroppedFrame(); |
Sebastian Jansson | 40889f3 | 2019-04-17 12:11:20 +0200 | [diff] [blame] | 553 | fake_clock_.AdvanceTime(TimeDelta::seconds(1) / max_framerate_); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | bool WaitForFrame(int64_t timeout_ms) { |
| 557 | bool ok = sink_.WaitForFrame(timeout_ms); |
Sebastian Jansson | 40889f3 | 2019-04-17 12:11:20 +0200 | [diff] [blame] | 558 | fake_clock_.AdvanceTime(TimeDelta::seconds(1) / max_framerate_); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 559 | return ok; |
| 560 | } |
| 561 | |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 562 | class TestEncoder : public test::FakeEncoder { |
| 563 | public: |
Niels Möller | c572ff3 | 2018-11-07 08:43:50 +0100 | [diff] [blame] | 564 | TestEncoder() : FakeEncoder(Clock::GetRealTimeClock()) {} |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 565 | |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 566 | VideoCodec codec_config() const { |
brandtr | e78d266 | 2017-01-16 05:57:16 -0800 | [diff] [blame] | 567 | rtc::CritScope lock(&crit_sect_); |
perkj | fa10b55 | 2016-10-02 23:45:26 -0700 | [diff] [blame] | 568 | return config_; |
| 569 | } |
| 570 | |
| 571 | void BlockNextEncode() { |
brandtr | e78d266 | 2017-01-16 05:57:16 -0800 | [diff] [blame] | 572 | rtc::CritScope lock(&local_crit_sect_); |
perkj | fa10b55 | 2016-10-02 23:45:26 -0700 | [diff] [blame] | 573 | block_next_encode_ = true; |
| 574 | } |
| 575 | |
Erik Språng | aed3070 | 2018-11-05 12:57:17 +0100 | [diff] [blame] | 576 | VideoEncoder::EncoderInfo GetEncoderInfo() const override { |
kthelgason | 2fc5254 | 2017-03-03 00:24:41 -0800 | [diff] [blame] | 577 | rtc::CritScope lock(&local_crit_sect_); |
Mirta Dvornicic | ccc1b57 | 2019-01-15 12:42:18 +0100 | [diff] [blame] | 578 | EncoderInfo info; |
Erik Språng | b7cb7b5 | 2019-02-26 15:52:33 +0100 | [diff] [blame] | 579 | if (initialized_ == EncoderState::kInitialized) { |
Mirta Dvornicic | ccc1b57 | 2019-01-15 12:42:18 +0100 | [diff] [blame] | 580 | if (quality_scaling_) { |
| 581 | info.scaling_settings = |
| 582 | VideoEncoder::ScalingSettings(1, 2, kMinPixelsPerFrame); |
| 583 | } |
| 584 | info.is_hardware_accelerated = is_hardware_accelerated_; |
Åsa Persson | c29cb2c | 2019-03-25 12:06:59 +0100 | [diff] [blame] | 585 | for (int i = 0; i < kMaxSpatialLayers; ++i) { |
| 586 | if (temporal_layers_supported_[i]) { |
| 587 | int num_layers = temporal_layers_supported_[i].value() ? 2 : 1; |
| 588 | info.fps_allocation[i].resize(num_layers); |
| 589 | } |
| 590 | } |
Erik Språng | aed3070 | 2018-11-05 12:57:17 +0100 | [diff] [blame] | 591 | } |
| 592 | return info; |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 593 | } |
| 594 | |
Erik Språng | b7cb7b5 | 2019-02-26 15:52:33 +0100 | [diff] [blame] | 595 | int32_t RegisterEncodeCompleteCallback( |
| 596 | EncodedImageCallback* callback) override { |
| 597 | rtc::CritScope lock(&local_crit_sect_); |
| 598 | encoded_image_callback_ = callback; |
| 599 | return FakeEncoder::RegisterEncodeCompleteCallback(callback); |
| 600 | } |
| 601 | |
perkj | fa10b55 | 2016-10-02 23:45:26 -0700 | [diff] [blame] | 602 | void ContinueEncode() { continue_encode_event_.Set(); } |
| 603 | |
| 604 | void CheckLastTimeStampsMatch(int64_t ntp_time_ms, |
| 605 | uint32_t timestamp) const { |
brandtr | e78d266 | 2017-01-16 05:57:16 -0800 | [diff] [blame] | 606 | rtc::CritScope lock(&local_crit_sect_); |
perkj | fa10b55 | 2016-10-02 23:45:26 -0700 | [diff] [blame] | 607 | EXPECT_EQ(timestamp_, timestamp); |
| 608 | EXPECT_EQ(ntp_time_ms_, ntp_time_ms); |
| 609 | } |
| 610 | |
kthelgason | 2fc5254 | 2017-03-03 00:24:41 -0800 | [diff] [blame] | 611 | void SetQualityScaling(bool b) { |
| 612 | rtc::CritScope lock(&local_crit_sect_); |
| 613 | quality_scaling_ = b; |
| 614 | } |
kthelgason | ad9010c | 2017-02-14 00:46:51 -0800 | [diff] [blame] | 615 | |
Mirta Dvornicic | ccc1b57 | 2019-01-15 12:42:18 +0100 | [diff] [blame] | 616 | void SetIsHardwareAccelerated(bool is_hardware_accelerated) { |
| 617 | rtc::CritScope lock(&local_crit_sect_); |
| 618 | is_hardware_accelerated_ = is_hardware_accelerated; |
| 619 | } |
| 620 | |
Åsa Persson | c29cb2c | 2019-03-25 12:06:59 +0100 | [diff] [blame] | 621 | void SetTemporalLayersSupported(size_t spatial_idx, bool supported) { |
| 622 | RTC_DCHECK_LT(spatial_idx, kMaxSpatialLayers); |
| 623 | rtc::CritScope lock(&local_crit_sect_); |
| 624 | temporal_layers_supported_[spatial_idx] = supported; |
| 625 | } |
| 626 | |
sprang | fe627f3 | 2017-03-29 08:24:59 -0700 | [diff] [blame] | 627 | void ForceInitEncodeFailure(bool force_failure) { |
| 628 | rtc::CritScope lock(&local_crit_sect_); |
| 629 | force_init_encode_failed_ = force_failure; |
| 630 | } |
| 631 | |
Niels Möller | 6bb5ab9 | 2019-01-11 11:11:10 +0100 | [diff] [blame] | 632 | void SimulateOvershoot(double rate_factor) { |
| 633 | rtc::CritScope lock(&local_crit_sect_); |
| 634 | rate_factor_ = rate_factor; |
| 635 | } |
| 636 | |
Erik Språng | d7329ca | 2019-02-21 21:19:53 +0100 | [diff] [blame] | 637 | uint32_t GetLastFramerate() const { |
Niels Möller | 6bb5ab9 | 2019-01-11 11:11:10 +0100 | [diff] [blame] | 638 | rtc::CritScope lock(&local_crit_sect_); |
| 639 | return last_framerate_; |
| 640 | } |
| 641 | |
Erik Språng | d7329ca | 2019-02-21 21:19:53 +0100 | [diff] [blame] | 642 | VideoFrame::UpdateRect GetLastUpdateRect() const { |
Ilya Nikolaevskiy | 71aee3a | 2019-02-18 13:01:26 +0100 | [diff] [blame] | 643 | rtc::CritScope lock(&local_crit_sect_); |
| 644 | return last_update_rect_; |
| 645 | } |
| 646 | |
Niels Möller | 87e2d78 | 2019-03-07 10:18:23 +0100 | [diff] [blame] | 647 | const std::vector<VideoFrameType>& LastFrameTypes() const { |
Erik Språng | d7329ca | 2019-02-21 21:19:53 +0100 | [diff] [blame] | 648 | rtc::CritScope lock(&local_crit_sect_); |
| 649 | return last_frame_types_; |
| 650 | } |
| 651 | |
| 652 | void InjectFrame(const VideoFrame& input_image, bool keyframe) { |
Niels Möller | 87e2d78 | 2019-03-07 10:18:23 +0100 | [diff] [blame] | 653 | const std::vector<VideoFrameType> frame_type = { |
Niels Möller | 8f7ce22 | 2019-03-21 15:43:58 +0100 | [diff] [blame] | 654 | keyframe ? VideoFrameType::kVideoFrameKey |
| 655 | : VideoFrameType::kVideoFrameDelta}; |
Erik Språng | d7329ca | 2019-02-21 21:19:53 +0100 | [diff] [blame] | 656 | { |
| 657 | rtc::CritScope lock(&local_crit_sect_); |
| 658 | last_frame_types_ = frame_type; |
| 659 | } |
Niels Möller | b859b32 | 2019-03-07 12:40:01 +0100 | [diff] [blame] | 660 | FakeEncoder::Encode(input_image, &frame_type); |
Erik Språng | d7329ca | 2019-02-21 21:19:53 +0100 | [diff] [blame] | 661 | } |
| 662 | |
Erik Språng | b7cb7b5 | 2019-02-26 15:52:33 +0100 | [diff] [blame] | 663 | void InjectEncodedImage(const EncodedImage& image) { |
| 664 | rtc::CritScope lock(&local_crit_sect_); |
| 665 | encoded_image_callback_->OnEncodedImage(image, nullptr, nullptr); |
| 666 | } |
| 667 | |
Mirta Dvornicic | 28f0eb2 | 2019-05-28 16:30:16 +0200 | [diff] [blame] | 668 | void InjectEncodedImage(const EncodedImage& image, |
| 669 | const CodecSpecificInfo* codec_specific_info, |
| 670 | const RTPFragmentationHeader* fragmentation) { |
| 671 | rtc::CritScope lock(&local_crit_sect_); |
| 672 | encoded_image_callback_->OnEncodedImage(image, codec_specific_info, |
| 673 | fragmentation); |
| 674 | } |
| 675 | |
Erik Språng | d7329ca | 2019-02-21 21:19:53 +0100 | [diff] [blame] | 676 | void ExpectNullFrame() { |
| 677 | rtc::CritScope lock(&local_crit_sect_); |
| 678 | expect_null_frame_ = true; |
| 679 | } |
| 680 | |
| 681 | absl::optional<VideoBitrateAllocation> GetAndResetLastBitrateAllocation() { |
| 682 | auto allocation = last_bitrate_allocation_; |
| 683 | last_bitrate_allocation_.reset(); |
| 684 | return allocation; |
| 685 | } |
| 686 | |
perkj | fa10b55 | 2016-10-02 23:45:26 -0700 | [diff] [blame] | 687 | private: |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 688 | int32_t Encode(const VideoFrame& input_image, |
Niels Möller | 87e2d78 | 2019-03-07 10:18:23 +0100 | [diff] [blame] | 689 | const std::vector<VideoFrameType>* frame_types) override { |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 690 | bool block_encode; |
| 691 | { |
brandtr | e78d266 | 2017-01-16 05:57:16 -0800 | [diff] [blame] | 692 | rtc::CritScope lock(&local_crit_sect_); |
Erik Språng | d7329ca | 2019-02-21 21:19:53 +0100 | [diff] [blame] | 693 | if (expect_null_frame_) { |
| 694 | EXPECT_EQ(input_image.timestamp(), 0u); |
| 695 | EXPECT_EQ(input_image.width(), 1); |
| 696 | last_frame_types_ = *frame_types; |
| 697 | expect_null_frame_ = false; |
| 698 | } else { |
| 699 | EXPECT_GT(input_image.timestamp(), timestamp_); |
| 700 | EXPECT_GT(input_image.ntp_time_ms(), ntp_time_ms_); |
| 701 | EXPECT_EQ(input_image.timestamp(), input_image.ntp_time_ms() * 90); |
| 702 | } |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 703 | |
| 704 | timestamp_ = input_image.timestamp(); |
| 705 | ntp_time_ms_ = input_image.ntp_time_ms(); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 706 | last_input_width_ = input_image.width(); |
| 707 | last_input_height_ = input_image.height(); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 708 | block_encode = block_next_encode_; |
| 709 | block_next_encode_ = false; |
Ilya Nikolaevskiy | 71aee3a | 2019-02-18 13:01:26 +0100 | [diff] [blame] | 710 | last_update_rect_ = input_image.update_rect(); |
Erik Språng | d7329ca | 2019-02-21 21:19:53 +0100 | [diff] [blame] | 711 | last_frame_types_ = *frame_types; |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 712 | } |
Niels Möller | b859b32 | 2019-03-07 12:40:01 +0100 | [diff] [blame] | 713 | int32_t result = FakeEncoder::Encode(input_image, frame_types); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 714 | if (block_encode) |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 715 | EXPECT_TRUE(continue_encode_event_.Wait(kDefaultTimeoutMs)); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 716 | return result; |
| 717 | } |
| 718 | |
sprang | fe627f3 | 2017-03-29 08:24:59 -0700 | [diff] [blame] | 719 | int32_t InitEncode(const VideoCodec* config, |
| 720 | int32_t number_of_cores, |
| 721 | size_t max_payload_size) override { |
| 722 | int res = |
| 723 | FakeEncoder::InitEncode(config, number_of_cores, max_payload_size); |
| 724 | rtc::CritScope lock(&local_crit_sect_); |
Erik Språng | b7cb7b5 | 2019-02-26 15:52:33 +0100 | [diff] [blame] | 725 | EXPECT_EQ(initialized_, EncoderState::kUninitialized); |
Erik Språng | 82fad3d | 2018-03-21 09:57:23 +0100 | [diff] [blame] | 726 | if (config->codecType == kVideoCodecVP8) { |
sprang | fe627f3 | 2017-03-29 08:24:59 -0700 | [diff] [blame] | 727 | // Simulate setting up temporal layers, in order to validate the life |
| 728 | // cycle of these objects. |
Elad Alon | cde8ab2 | 2019-03-20 11:56:20 +0100 | [diff] [blame] | 729 | Vp8TemporalLayersFactory factory; |
| 730 | frame_buffer_controller_ = factory.Create(*config); |
sprang | fe627f3 | 2017-03-29 08:24:59 -0700 | [diff] [blame] | 731 | } |
Erik Språng | b7cb7b5 | 2019-02-26 15:52:33 +0100 | [diff] [blame] | 732 | if (force_init_encode_failed_) { |
| 733 | initialized_ = EncoderState::kInitializationFailed; |
sprang | fe627f3 | 2017-03-29 08:24:59 -0700 | [diff] [blame] | 734 | return -1; |
Erik Språng | b7cb7b5 | 2019-02-26 15:52:33 +0100 | [diff] [blame] | 735 | } |
Mirta Dvornicic | ccc1b57 | 2019-01-15 12:42:18 +0100 | [diff] [blame] | 736 | |
Erik Språng | b7cb7b5 | 2019-02-26 15:52:33 +0100 | [diff] [blame] | 737 | initialized_ = EncoderState::kInitialized; |
sprang | fe627f3 | 2017-03-29 08:24:59 -0700 | [diff] [blame] | 738 | return res; |
| 739 | } |
| 740 | |
Erik Språng | b7cb7b5 | 2019-02-26 15:52:33 +0100 | [diff] [blame] | 741 | int32_t Release() override { |
| 742 | rtc::CritScope lock(&local_crit_sect_); |
| 743 | EXPECT_NE(initialized_, EncoderState::kUninitialized); |
| 744 | initialized_ = EncoderState::kUninitialized; |
| 745 | return FakeEncoder::Release(); |
| 746 | } |
| 747 | |
Erik Språng | 16cb8f5 | 2019-04-12 13:59:09 +0200 | [diff] [blame] | 748 | void SetRates(const RateControlParameters& parameters) { |
Niels Möller | 6bb5ab9 | 2019-01-11 11:11:10 +0100 | [diff] [blame] | 749 | rtc::CritScope lock(&local_crit_sect_); |
| 750 | VideoBitrateAllocation adjusted_rate_allocation; |
| 751 | for (size_t si = 0; si < kMaxSpatialLayers; ++si) { |
| 752 | for (size_t ti = 0; ti < kMaxTemporalStreams; ++ti) { |
Erik Språng | 16cb8f5 | 2019-04-12 13:59:09 +0200 | [diff] [blame] | 753 | if (parameters.bitrate.HasBitrate(si, ti)) { |
Niels Möller | 6bb5ab9 | 2019-01-11 11:11:10 +0100 | [diff] [blame] | 754 | adjusted_rate_allocation.SetBitrate( |
| 755 | si, ti, |
Erik Språng | 16cb8f5 | 2019-04-12 13:59:09 +0200 | [diff] [blame] | 756 | static_cast<uint32_t>(parameters.bitrate.GetBitrate(si, ti) * |
Niels Möller | 6bb5ab9 | 2019-01-11 11:11:10 +0100 | [diff] [blame] | 757 | rate_factor_)); |
| 758 | } |
| 759 | } |
| 760 | } |
Erik Språng | 16cb8f5 | 2019-04-12 13:59:09 +0200 | [diff] [blame] | 761 | last_framerate_ = static_cast<uint32_t>(parameters.framerate_fps + 0.5); |
| 762 | last_bitrate_allocation_ = parameters.bitrate; |
| 763 | RateControlParameters adjusted_paramters = parameters; |
| 764 | adjusted_paramters.bitrate = adjusted_rate_allocation; |
| 765 | FakeEncoder::SetRates(adjusted_paramters); |
Niels Möller | 6bb5ab9 | 2019-01-11 11:11:10 +0100 | [diff] [blame] | 766 | } |
| 767 | |
brandtr | e78d266 | 2017-01-16 05:57:16 -0800 | [diff] [blame] | 768 | rtc::CriticalSection local_crit_sect_; |
Erik Språng | b7cb7b5 | 2019-02-26 15:52:33 +0100 | [diff] [blame] | 769 | enum class EncoderState { |
| 770 | kUninitialized, |
| 771 | kInitializationFailed, |
| 772 | kInitialized |
| 773 | } initialized_ RTC_GUARDED_BY(local_crit_sect_) = |
| 774 | EncoderState::kUninitialized; |
danilchap | a37de39 | 2017-09-09 04:17:22 -0700 | [diff] [blame] | 775 | bool block_next_encode_ RTC_GUARDED_BY(local_crit_sect_) = false; |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 776 | rtc::Event continue_encode_event_; |
danilchap | a37de39 | 2017-09-09 04:17:22 -0700 | [diff] [blame] | 777 | uint32_t timestamp_ RTC_GUARDED_BY(local_crit_sect_) = 0; |
| 778 | int64_t ntp_time_ms_ RTC_GUARDED_BY(local_crit_sect_) = 0; |
| 779 | int last_input_width_ RTC_GUARDED_BY(local_crit_sect_) = 0; |
| 780 | int last_input_height_ RTC_GUARDED_BY(local_crit_sect_) = 0; |
| 781 | bool quality_scaling_ RTC_GUARDED_BY(local_crit_sect_) = true; |
Mirta Dvornicic | ccc1b57 | 2019-01-15 12:42:18 +0100 | [diff] [blame] | 782 | bool is_hardware_accelerated_ RTC_GUARDED_BY(local_crit_sect_) = false; |
Elad Alon | cde8ab2 | 2019-03-20 11:56:20 +0100 | [diff] [blame] | 783 | std::unique_ptr<Vp8FrameBufferController> frame_buffer_controller_ |
danilchap | a37de39 | 2017-09-09 04:17:22 -0700 | [diff] [blame] | 784 | RTC_GUARDED_BY(local_crit_sect_); |
Åsa Persson | c29cb2c | 2019-03-25 12:06:59 +0100 | [diff] [blame] | 785 | absl::optional<bool> |
| 786 | temporal_layers_supported_[kMaxSpatialLayers] RTC_GUARDED_BY( |
| 787 | local_crit_sect_); |
danilchap | a37de39 | 2017-09-09 04:17:22 -0700 | [diff] [blame] | 788 | bool force_init_encode_failed_ RTC_GUARDED_BY(local_crit_sect_) = false; |
Niels Möller | 6bb5ab9 | 2019-01-11 11:11:10 +0100 | [diff] [blame] | 789 | double rate_factor_ RTC_GUARDED_BY(local_crit_sect_) = 1.0; |
| 790 | uint32_t last_framerate_ RTC_GUARDED_BY(local_crit_sect_) = 0; |
Erik Språng | d7329ca | 2019-02-21 21:19:53 +0100 | [diff] [blame] | 791 | absl::optional<VideoBitrateAllocation> last_bitrate_allocation_; |
Ilya Nikolaevskiy | 71aee3a | 2019-02-18 13:01:26 +0100 | [diff] [blame] | 792 | VideoFrame::UpdateRect last_update_rect_ |
| 793 | RTC_GUARDED_BY(local_crit_sect_) = {0, 0, 0, 0}; |
Niels Möller | 87e2d78 | 2019-03-07 10:18:23 +0100 | [diff] [blame] | 794 | std::vector<VideoFrameType> last_frame_types_; |
Erik Språng | d7329ca | 2019-02-21 21:19:53 +0100 | [diff] [blame] | 795 | bool expect_null_frame_ = false; |
Erik Språng | b7cb7b5 | 2019-02-26 15:52:33 +0100 | [diff] [blame] | 796 | EncodedImageCallback* encoded_image_callback_ |
| 797 | RTC_GUARDED_BY(local_crit_sect_) = nullptr; |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 798 | }; |
| 799 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 800 | class TestSink : public VideoStreamEncoder::EncoderSink { |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 801 | public: |
| 802 | explicit TestSink(TestEncoder* test_encoder) |
Niels Möller | c572ff3 | 2018-11-07 08:43:50 +0100 | [diff] [blame] | 803 | : test_encoder_(test_encoder) {} |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 804 | |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 805 | void WaitForEncodedFrame(int64_t expected_ntp_time) { |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 806 | EXPECT_TRUE( |
| 807 | TimedWaitForEncodedFrame(expected_ntp_time, kDefaultTimeoutMs)); |
| 808 | } |
| 809 | |
| 810 | bool TimedWaitForEncodedFrame(int64_t expected_ntp_time, |
| 811 | int64_t timeout_ms) { |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 812 | uint32_t timestamp = 0; |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 813 | if (!encoded_frame_event_.Wait(timeout_ms)) |
| 814 | return false; |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 815 | { |
| 816 | rtc::CritScope lock(&crit_); |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 817 | timestamp = last_timestamp_; |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 818 | } |
| 819 | test_encoder_->CheckLastTimeStampsMatch(expected_ntp_time, timestamp); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 820 | return true; |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 821 | } |
| 822 | |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 823 | void WaitForEncodedFrame(uint32_t expected_width, |
| 824 | uint32_t expected_height) { |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 825 | EXPECT_TRUE(encoded_frame_event_.Wait(kDefaultTimeoutMs)); |
Åsa Persson | c74d8da | 2017-12-04 14:13:56 +0100 | [diff] [blame] | 826 | CheckLastFrameSizeMatches(expected_width, expected_height); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 827 | } |
| 828 | |
Åsa Persson | c74d8da | 2017-12-04 14:13:56 +0100 | [diff] [blame] | 829 | void CheckLastFrameSizeMatches(uint32_t expected_width, |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 830 | uint32_t expected_height) { |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 831 | uint32_t width = 0; |
| 832 | uint32_t height = 0; |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 833 | { |
| 834 | rtc::CritScope lock(&crit_); |
| 835 | width = last_width_; |
| 836 | height = last_height_; |
| 837 | } |
| 838 | EXPECT_EQ(expected_height, height); |
| 839 | EXPECT_EQ(expected_width, width); |
| 840 | } |
| 841 | |
kthelgason | 2fc5254 | 2017-03-03 00:24:41 -0800 | [diff] [blame] | 842 | void ExpectDroppedFrame() { EXPECT_FALSE(encoded_frame_event_.Wait(100)); } |
kthelgason | 2bc6864 | 2017-02-07 07:02:22 -0800 | [diff] [blame] | 843 | |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 844 | bool WaitForFrame(int64_t timeout_ms) { |
| 845 | return encoded_frame_event_.Wait(timeout_ms); |
| 846 | } |
| 847 | |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 848 | void SetExpectNoFrames() { |
| 849 | rtc::CritScope lock(&crit_); |
| 850 | expect_frames_ = false; |
| 851 | } |
| 852 | |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 853 | int number_of_reconfigurations() const { |
Per | 512ecb3 | 2016-09-23 15:52:06 +0200 | [diff] [blame] | 854 | rtc::CritScope lock(&crit_); |
| 855 | return number_of_reconfigurations_; |
| 856 | } |
| 857 | |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 858 | int last_min_transmit_bitrate() const { |
Per | 512ecb3 | 2016-09-23 15:52:06 +0200 | [diff] [blame] | 859 | rtc::CritScope lock(&crit_); |
| 860 | return min_transmit_bitrate_bps_; |
| 861 | } |
| 862 | |
Erik Språng | d7329ca | 2019-02-21 21:19:53 +0100 | [diff] [blame] | 863 | void SetNumExpectedLayers(size_t num_layers) { |
| 864 | rtc::CritScope lock(&crit_); |
| 865 | num_expected_layers_ = num_layers; |
| 866 | } |
| 867 | |
Erik Språng | b7cb7b5 | 2019-02-26 15:52:33 +0100 | [diff] [blame] | 868 | int64_t GetLastCaptureTimeMs() const { |
| 869 | rtc::CritScope lock(&crit_); |
| 870 | return last_capture_time_ms_; |
| 871 | } |
| 872 | |
Mirta Dvornicic | 28f0eb2 | 2019-05-28 16:30:16 +0200 | [diff] [blame] | 873 | std::vector<uint8_t> GetLastEncodedImageData() { |
| 874 | rtc::CritScope lock(&crit_); |
| 875 | return std::move(last_encoded_image_data_); |
| 876 | } |
| 877 | |
| 878 | RTPFragmentationHeader GetLastFragmentation() { |
| 879 | rtc::CritScope lock(&crit_); |
| 880 | return std::move(last_fragmentation_); |
| 881 | } |
| 882 | |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 883 | private: |
sergeyu | 2cb155a | 2016-11-04 11:39:29 -0700 | [diff] [blame] | 884 | Result OnEncodedImage( |
| 885 | const EncodedImage& encoded_image, |
| 886 | const CodecSpecificInfo* codec_specific_info, |
| 887 | const RTPFragmentationHeader* fragmentation) override { |
Per | 512ecb3 | 2016-09-23 15:52:06 +0200 | [diff] [blame] | 888 | rtc::CritScope lock(&crit_); |
| 889 | EXPECT_TRUE(expect_frames_); |
Mirta Dvornicic | 28f0eb2 | 2019-05-28 16:30:16 +0200 | [diff] [blame] | 890 | last_encoded_image_data_ = std::vector<uint8_t>( |
| 891 | encoded_image.data(), encoded_image.data() + encoded_image.size()); |
| 892 | if (fragmentation) { |
| 893 | last_fragmentation_.CopyFrom(*fragmentation); |
| 894 | } |
Erik Språng | d7329ca | 2019-02-21 21:19:53 +0100 | [diff] [blame] | 895 | uint32_t timestamp = encoded_image.Timestamp(); |
| 896 | if (last_timestamp_ != timestamp) { |
| 897 | num_received_layers_ = 1; |
| 898 | } else { |
| 899 | ++num_received_layers_; |
| 900 | } |
| 901 | last_timestamp_ = timestamp; |
Erik Språng | b7cb7b5 | 2019-02-26 15:52:33 +0100 | [diff] [blame] | 902 | last_capture_time_ms_ = encoded_image.capture_time_ms_; |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 903 | last_width_ = encoded_image._encodedWidth; |
| 904 | last_height_ = encoded_image._encodedHeight; |
Erik Språng | d7329ca | 2019-02-21 21:19:53 +0100 | [diff] [blame] | 905 | if (num_received_layers_ == num_expected_layers_) { |
| 906 | encoded_frame_event_.Set(); |
| 907 | } |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 908 | return Result(Result::OK, last_timestamp_); |
Per | 512ecb3 | 2016-09-23 15:52:06 +0200 | [diff] [blame] | 909 | } |
| 910 | |
Rasmus Brandt | c402dbe | 2019-02-04 11:09:46 +0100 | [diff] [blame] | 911 | void OnEncoderConfigurationChanged( |
| 912 | std::vector<VideoStream> streams, |
| 913 | VideoEncoderConfig::ContentType content_type, |
| 914 | int min_transmit_bitrate_bps) override { |
Per | 512ecb3 | 2016-09-23 15:52:06 +0200 | [diff] [blame] | 915 | rtc::CriticalSection crit_; |
| 916 | ++number_of_reconfigurations_; |
| 917 | min_transmit_bitrate_bps_ = min_transmit_bitrate_bps; |
| 918 | } |
| 919 | |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 920 | rtc::CriticalSection crit_; |
| 921 | TestEncoder* test_encoder_; |
| 922 | rtc::Event encoded_frame_event_; |
Mirta Dvornicic | 28f0eb2 | 2019-05-28 16:30:16 +0200 | [diff] [blame] | 923 | std::vector<uint8_t> last_encoded_image_data_; |
| 924 | RTPFragmentationHeader last_fragmentation_; |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 925 | uint32_t last_timestamp_ = 0; |
Erik Språng | b7cb7b5 | 2019-02-26 15:52:33 +0100 | [diff] [blame] | 926 | int64_t last_capture_time_ms_ = 0; |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 927 | uint32_t last_height_ = 0; |
| 928 | uint32_t last_width_ = 0; |
Erik Språng | d7329ca | 2019-02-21 21:19:53 +0100 | [diff] [blame] | 929 | size_t num_expected_layers_ = 1; |
| 930 | size_t num_received_layers_ = 0; |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 931 | bool expect_frames_ = true; |
Per | 512ecb3 | 2016-09-23 15:52:06 +0200 | [diff] [blame] | 932 | int number_of_reconfigurations_ = 0; |
| 933 | int min_transmit_bitrate_bps_ = 0; |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 934 | }; |
| 935 | |
| 936 | VideoSendStream::Config video_send_config_; |
Erik Språng | 08127a9 | 2016-11-16 16:41:30 +0100 | [diff] [blame] | 937 | VideoEncoderConfig video_encoder_config_; |
Per | 512ecb3 | 2016-09-23 15:52:06 +0200 | [diff] [blame] | 938 | int codec_width_; |
| 939 | int codec_height_; |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 940 | int max_framerate_; |
Danil Chapovalov | d3ba236 | 2019-04-10 17:01:23 +0200 | [diff] [blame] | 941 | const std::unique_ptr<TaskQueueFactory> task_queue_factory_; |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 942 | TestEncoder fake_encoder_; |
Niels Möller | cbcbc22 | 2018-09-28 09:07:24 +0200 | [diff] [blame] | 943 | test::VideoEncoderProxyFactory encoder_factory_; |
Jiawei Ou | c2ebe21 | 2018-11-08 10:02:56 -0800 | [diff] [blame] | 944 | std::unique_ptr<VideoBitrateAllocatorFactory> bitrate_allocator_factory_; |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 945 | std::unique_ptr<MockableSendStatisticsProxy> stats_proxy_; |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 946 | TestSink sink_; |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 947 | AdaptingFrameForwarder video_source_; |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 948 | std::unique_ptr<VideoStreamEncoderUnderTest> video_stream_encoder_; |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 949 | rtc::ScopedFakeClock fake_clock_; |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 950 | }; |
| 951 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 952 | TEST_F(VideoStreamEncoderTest, EncodeOneFrame) { |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 953 | video_stream_encoder_->OnBitrateUpdated( |
| 954 | DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps), 0, 0); |
Niels Möller | c572ff3 | 2018-11-07 08:43:50 +0100 | [diff] [blame] | 955 | rtc::Event frame_destroyed_event; |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 956 | video_source_.IncomingCapturedFrame(CreateFrame(1, &frame_destroyed_event)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 957 | WaitForEncodedFrame(1); |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 958 | EXPECT_TRUE(frame_destroyed_event.Wait(kDefaultTimeoutMs)); |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 959 | video_stream_encoder_->Stop(); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 960 | } |
| 961 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 962 | TEST_F(VideoStreamEncoderTest, DropsFramesBeforeFirstOnBitrateUpdated) { |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 963 | // Dropped since no target bitrate has been set. |
Niels Möller | c572ff3 | 2018-11-07 08:43:50 +0100 | [diff] [blame] | 964 | rtc::Event frame_destroyed_event; |
Sebastian Jansson | a317705 | 2018-04-10 13:05:49 +0200 | [diff] [blame] | 965 | // The encoder will cache up to one frame for a short duration. Adding two |
| 966 | // frames means that the first frame will be dropped and the second frame will |
| 967 | // be sent when the encoder is enabled. |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 968 | video_source_.IncomingCapturedFrame(CreateFrame(1, &frame_destroyed_event)); |
Sebastian Jansson | a317705 | 2018-04-10 13:05:49 +0200 | [diff] [blame] | 969 | video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr)); |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 970 | EXPECT_TRUE(frame_destroyed_event.Wait(kDefaultTimeoutMs)); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 971 | |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 972 | video_stream_encoder_->OnBitrateUpdated( |
| 973 | DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps), 0, 0); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 974 | |
Sebastian Jansson | a317705 | 2018-04-10 13:05:49 +0200 | [diff] [blame] | 975 | // The pending frame should be received. |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 976 | WaitForEncodedFrame(2); |
Sebastian Jansson | a317705 | 2018-04-10 13:05:49 +0200 | [diff] [blame] | 977 | video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr)); |
| 978 | |
| 979 | WaitForEncodedFrame(3); |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 980 | video_stream_encoder_->Stop(); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 981 | } |
| 982 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 983 | TEST_F(VideoStreamEncoderTest, DropsFramesWhenRateSetToZero) { |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 984 | video_stream_encoder_->OnBitrateUpdated( |
| 985 | DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps), 0, 0); |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 986 | video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 987 | WaitForEncodedFrame(1); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 988 | |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 989 | video_stream_encoder_->OnBitrateUpdated(DataRate::bps(0), DataRate::bps(0), 0, |
Erik Språng | 610c763 | 2019-03-06 15:37:33 +0100 | [diff] [blame] | 990 | 0); |
Sebastian Jansson | a317705 | 2018-04-10 13:05:49 +0200 | [diff] [blame] | 991 | // The encoder will cache up to one frame for a short duration. Adding two |
| 992 | // frames means that the first frame will be dropped and the second frame will |
| 993 | // be sent when the encoder is resumed. |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 994 | video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr)); |
Sebastian Jansson | a317705 | 2018-04-10 13:05:49 +0200 | [diff] [blame] | 995 | video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr)); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 996 | |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 997 | video_stream_encoder_->OnBitrateUpdated( |
| 998 | DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps), 0, 0); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 999 | WaitForEncodedFrame(3); |
Sebastian Jansson | a317705 | 2018-04-10 13:05:49 +0200 | [diff] [blame] | 1000 | video_source_.IncomingCapturedFrame(CreateFrame(4, nullptr)); |
| 1001 | WaitForEncodedFrame(4); |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1002 | video_stream_encoder_->Stop(); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 1003 | } |
| 1004 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1005 | TEST_F(VideoStreamEncoderTest, DropsFramesWithSameOrOldNtpTimestamp) { |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 1006 | video_stream_encoder_->OnBitrateUpdated( |
| 1007 | DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps), 0, 0); |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 1008 | video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1009 | WaitForEncodedFrame(1); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 1010 | |
| 1011 | // This frame will be dropped since it has the same ntp timestamp. |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 1012 | video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr)); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 1013 | |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 1014 | video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1015 | WaitForEncodedFrame(2); |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1016 | video_stream_encoder_->Stop(); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 1017 | } |
| 1018 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1019 | TEST_F(VideoStreamEncoderTest, DropsFrameAfterStop) { |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 1020 | video_stream_encoder_->OnBitrateUpdated( |
| 1021 | DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps), 0, 0); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 1022 | |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 1023 | video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1024 | WaitForEncodedFrame(1); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 1025 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1026 | video_stream_encoder_->Stop(); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 1027 | sink_.SetExpectNoFrames(); |
Niels Möller | c572ff3 | 2018-11-07 08:43:50 +0100 | [diff] [blame] | 1028 | rtc::Event frame_destroyed_event; |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 1029 | video_source_.IncomingCapturedFrame(CreateFrame(2, &frame_destroyed_event)); |
| 1030 | EXPECT_TRUE(frame_destroyed_event.Wait(kDefaultTimeoutMs)); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 1031 | } |
| 1032 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1033 | TEST_F(VideoStreamEncoderTest, DropsPendingFramesOnSlowEncode) { |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 1034 | video_stream_encoder_->OnBitrateUpdated( |
| 1035 | DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps), 0, 0); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 1036 | |
| 1037 | fake_encoder_.BlockNextEncode(); |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 1038 | video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1039 | WaitForEncodedFrame(1); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 1040 | // Here, the encoder thread will be blocked in the TestEncoder waiting for a |
| 1041 | // call to ContinueEncode. |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 1042 | video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr)); |
| 1043 | video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr)); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 1044 | fake_encoder_.ContinueEncode(); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1045 | WaitForEncodedFrame(3); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 1046 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1047 | video_stream_encoder_->Stop(); |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 1048 | } |
| 1049 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1050 | TEST_F(VideoStreamEncoderTest, |
| 1051 | ConfigureEncoderTriggersOnEncoderConfigurationChanged) { |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 1052 | video_stream_encoder_->OnBitrateUpdated( |
| 1053 | DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps), 0, 0); |
Per | 21d45d2 | 2016-10-30 21:37:57 +0100 | [diff] [blame] | 1054 | EXPECT_EQ(0, sink_.number_of_reconfigurations()); |
Per | 512ecb3 | 2016-09-23 15:52:06 +0200 | [diff] [blame] | 1055 | |
| 1056 | // Capture a frame and wait for it to synchronize with the encoder thread. |
Per | f8c5f2b | 2016-09-23 16:24:55 +0200 | [diff] [blame] | 1057 | video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1058 | WaitForEncodedFrame(1); |
Per | 21d45d2 | 2016-10-30 21:37:57 +0100 | [diff] [blame] | 1059 | // The encoder will have been configured once when the first frame is |
| 1060 | // received. |
| 1061 | EXPECT_EQ(1, sink_.number_of_reconfigurations()); |
Per | 512ecb3 | 2016-09-23 15:52:06 +0200 | [diff] [blame] | 1062 | |
| 1063 | VideoEncoderConfig video_encoder_config; |
Niels Möller | 259a497 | 2018-04-05 15:36:51 +0200 | [diff] [blame] | 1064 | test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config); |
Per | 512ecb3 | 2016-09-23 15:52:06 +0200 | [diff] [blame] | 1065 | video_encoder_config.min_transmit_bitrate_bps = 9999; |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1066 | video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config), |
Niels Möller | f133856 | 2018-04-26 09:51:47 +0200 | [diff] [blame] | 1067 | kMaxPayloadLength); |
Per | 512ecb3 | 2016-09-23 15:52:06 +0200 | [diff] [blame] | 1068 | |
| 1069 | // Capture a frame and wait for it to synchronize with the encoder thread. |
Per | f8c5f2b | 2016-09-23 16:24:55 +0200 | [diff] [blame] | 1070 | video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1071 | WaitForEncodedFrame(2); |
Per | 21d45d2 | 2016-10-30 21:37:57 +0100 | [diff] [blame] | 1072 | EXPECT_EQ(2, sink_.number_of_reconfigurations()); |
perkj | 3b703ed | 2016-09-29 23:25:40 -0700 | [diff] [blame] | 1073 | EXPECT_EQ(9999, sink_.last_min_transmit_bitrate()); |
perkj | 26105b4 | 2016-09-29 22:39:10 -0700 | [diff] [blame] | 1074 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1075 | video_stream_encoder_->Stop(); |
perkj | 26105b4 | 2016-09-29 22:39:10 -0700 | [diff] [blame] | 1076 | } |
| 1077 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1078 | TEST_F(VideoStreamEncoderTest, FrameResolutionChangeReconfigureEncoder) { |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 1079 | video_stream_encoder_->OnBitrateUpdated( |
| 1080 | DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps), 0, 0); |
perkj | fa10b55 | 2016-10-02 23:45:26 -0700 | [diff] [blame] | 1081 | |
| 1082 | // Capture a frame and wait for it to synchronize with the encoder thread. |
| 1083 | video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1084 | WaitForEncodedFrame(1); |
Per | 21d45d2 | 2016-10-30 21:37:57 +0100 | [diff] [blame] | 1085 | // The encoder will have been configured once. |
| 1086 | EXPECT_EQ(1, sink_.number_of_reconfigurations()); |
perkj | fa10b55 | 2016-10-02 23:45:26 -0700 | [diff] [blame] | 1087 | EXPECT_EQ(codec_width_, fake_encoder_.codec_config().width); |
| 1088 | EXPECT_EQ(codec_height_, fake_encoder_.codec_config().height); |
| 1089 | |
| 1090 | codec_width_ *= 2; |
| 1091 | codec_height_ *= 2; |
| 1092 | // Capture a frame with a higher resolution and wait for it to synchronize |
| 1093 | // with the encoder thread. |
| 1094 | video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1095 | WaitForEncodedFrame(2); |
perkj | fa10b55 | 2016-10-02 23:45:26 -0700 | [diff] [blame] | 1096 | EXPECT_EQ(codec_width_, fake_encoder_.codec_config().width); |
| 1097 | EXPECT_EQ(codec_height_, fake_encoder_.codec_config().height); |
Per | 21d45d2 | 2016-10-30 21:37:57 +0100 | [diff] [blame] | 1098 | EXPECT_EQ(2, sink_.number_of_reconfigurations()); |
perkj | fa10b55 | 2016-10-02 23:45:26 -0700 | [diff] [blame] | 1099 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1100 | video_stream_encoder_->Stop(); |
perkj | fa10b55 | 2016-10-02 23:45:26 -0700 | [diff] [blame] | 1101 | } |
| 1102 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1103 | TEST_F(VideoStreamEncoderTest, SwitchSourceDeregisterEncoderAsSink) { |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 1104 | EXPECT_TRUE(video_source_.has_sinks()); |
| 1105 | test::FrameForwarder new_video_source; |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1106 | video_stream_encoder_->SetSource( |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1107 | &new_video_source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 1108 | EXPECT_FALSE(video_source_.has_sinks()); |
| 1109 | EXPECT_TRUE(new_video_source.has_sinks()); |
| 1110 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1111 | video_stream_encoder_->Stop(); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 1112 | } |
| 1113 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1114 | TEST_F(VideoStreamEncoderTest, SinkWantsRotationApplied) { |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 1115 | EXPECT_FALSE(video_source_.sink_wants().rotation_applied); |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1116 | video_stream_encoder_->SetSink(&sink_, true /*rotation_applied*/); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 1117 | EXPECT_TRUE(video_source_.sink_wants().rotation_applied); |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1118 | video_stream_encoder_->Stop(); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 1119 | } |
| 1120 | |
Jonathan Yu | bc771b7 | 2017-12-08 17:04:29 -0800 | [diff] [blame] | 1121 | TEST_F(VideoStreamEncoderTest, TestCpuDowngrades_BalancedMode) { |
| 1122 | const int kFramerateFps = 30; |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 1123 | const int kWidth = 1280; |
| 1124 | const int kHeight = 720; |
Jonathan Yu | bc771b7 | 2017-12-08 17:04:29 -0800 | [diff] [blame] | 1125 | |
| 1126 | // We rely on the automatic resolution adaptation, but we handle framerate |
| 1127 | // adaptation manually by mocking the stats proxy. |
| 1128 | video_source_.set_adaptation_enabled(true); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 1129 | |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1130 | // Enable BALANCED preference, no initial limitation. |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 1131 | video_stream_encoder_->OnBitrateUpdated( |
| 1132 | DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps), 0, 0); |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1133 | video_stream_encoder_->SetSource(&video_source_, |
| 1134 | webrtc::DegradationPreference::BALANCED); |
Jonathan Yu | bc771b7 | 2017-12-08 17:04:29 -0800 | [diff] [blame] | 1135 | VerifyNoLimitation(video_source_.sink_wants()); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 1136 | EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution); |
Jonathan Yu | bc771b7 | 2017-12-08 17:04:29 -0800 | [diff] [blame] | 1137 | EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 1138 | EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
| 1139 | |
Jonathan Yu | bc771b7 | 2017-12-08 17:04:29 -0800 | [diff] [blame] | 1140 | // Adapt down as far as possible. |
| 1141 | rtc::VideoSinkWants last_wants; |
| 1142 | int64_t t = 1; |
| 1143 | int loop_count = 0; |
| 1144 | do { |
| 1145 | ++loop_count; |
| 1146 | last_wants = video_source_.sink_wants(); |
| 1147 | |
| 1148 | // Simulate the framerate we've been asked to adapt to. |
| 1149 | const int fps = std::min(kFramerateFps, last_wants.max_framerate_fps); |
| 1150 | const int frame_interval_ms = rtc::kNumMillisecsPerSec / fps; |
| 1151 | VideoSendStream::Stats mock_stats = stats_proxy_->GetStats(); |
| 1152 | mock_stats.input_frame_rate = fps; |
| 1153 | stats_proxy_->SetMockStats(mock_stats); |
| 1154 | |
| 1155 | video_source_.IncomingCapturedFrame(CreateFrame(t, kWidth, kHeight)); |
| 1156 | sink_.WaitForEncodedFrame(t); |
| 1157 | t += frame_interval_ms; |
| 1158 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1159 | video_stream_encoder_->TriggerCpuOveruse(); |
Jonathan Yu | bc771b7 | 2017-12-08 17:04:29 -0800 | [diff] [blame] | 1160 | VerifyBalancedModeFpsRange( |
| 1161 | video_source_.sink_wants(), |
| 1162 | *video_source_.last_sent_width() * *video_source_.last_sent_height()); |
| 1163 | } while (video_source_.sink_wants().max_pixel_count < |
| 1164 | last_wants.max_pixel_count || |
| 1165 | video_source_.sink_wants().max_framerate_fps < |
| 1166 | last_wants.max_framerate_fps); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 1167 | |
Jonathan Yu | bc771b7 | 2017-12-08 17:04:29 -0800 | [diff] [blame] | 1168 | // Verify that we've adapted all the way down. |
| 1169 | stats_proxy_->ResetMockStats(); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 1170 | EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution); |
Jonathan Yu | bc771b7 | 2017-12-08 17:04:29 -0800 | [diff] [blame] | 1171 | EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_framerate); |
| 1172 | EXPECT_EQ(loop_count - 1, |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 1173 | stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
Jonathan Yu | bc771b7 | 2017-12-08 17:04:29 -0800 | [diff] [blame] | 1174 | EXPECT_EQ(kMinPixelsPerFrame, *video_source_.last_sent_width() * |
| 1175 | *video_source_.last_sent_height()); |
| 1176 | EXPECT_EQ(kMinBalancedFramerateFps, |
| 1177 | video_source_.sink_wants().max_framerate_fps); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 1178 | |
Jonathan Yu | bc771b7 | 2017-12-08 17:04:29 -0800 | [diff] [blame] | 1179 | // Adapt back up the same number of times we adapted down. |
| 1180 | for (int i = 0; i < loop_count - 1; ++i) { |
| 1181 | last_wants = video_source_.sink_wants(); |
| 1182 | |
| 1183 | // Simulate the framerate we've been asked to adapt to. |
| 1184 | const int fps = std::min(kFramerateFps, last_wants.max_framerate_fps); |
| 1185 | const int frame_interval_ms = rtc::kNumMillisecsPerSec / fps; |
| 1186 | VideoSendStream::Stats mock_stats = stats_proxy_->GetStats(); |
| 1187 | mock_stats.input_frame_rate = fps; |
| 1188 | stats_proxy_->SetMockStats(mock_stats); |
| 1189 | |
| 1190 | video_source_.IncomingCapturedFrame(CreateFrame(t, kWidth, kHeight)); |
| 1191 | sink_.WaitForEncodedFrame(t); |
| 1192 | t += frame_interval_ms; |
| 1193 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1194 | video_stream_encoder_->TriggerCpuNormalUsage(); |
Jonathan Yu | bc771b7 | 2017-12-08 17:04:29 -0800 | [diff] [blame] | 1195 | VerifyBalancedModeFpsRange( |
| 1196 | video_source_.sink_wants(), |
| 1197 | *video_source_.last_sent_width() * *video_source_.last_sent_height()); |
| 1198 | EXPECT_TRUE(video_source_.sink_wants().max_pixel_count > |
| 1199 | last_wants.max_pixel_count || |
| 1200 | video_source_.sink_wants().max_framerate_fps > |
| 1201 | last_wants.max_framerate_fps); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 1202 | } |
| 1203 | |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 1204 | VerifyFpsMaxResolutionMax(video_source_.sink_wants()); |
Jonathan Yu | bc771b7 | 2017-12-08 17:04:29 -0800 | [diff] [blame] | 1205 | stats_proxy_->ResetMockStats(); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 1206 | EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution); |
Jonathan Yu | bc771b7 | 2017-12-08 17:04:29 -0800 | [diff] [blame] | 1207 | EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate); |
| 1208 | EXPECT_EQ((loop_count - 1) * 2, |
| 1209 | stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 1210 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1211 | video_stream_encoder_->Stop(); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 1212 | } |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1213 | TEST_F(VideoStreamEncoderTest, SinkWantsStoredByDegradationPreference) { |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 1214 | video_stream_encoder_->OnBitrateUpdated( |
| 1215 | DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps), 0, 0); |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1216 | VerifyNoLimitation(video_source_.sink_wants()); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 1217 | |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1218 | const int kFrameWidth = 1280; |
| 1219 | const int kFrameHeight = 720; |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1220 | |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 1221 | int64_t frame_timestamp = 1; |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 1222 | |
kthelgason | 5e13d41 | 2016-12-01 03:59:51 -0800 | [diff] [blame] | 1223 | video_source_.IncomingCapturedFrame( |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1224 | CreateFrame(frame_timestamp, kFrameWidth, kFrameHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1225 | WaitForEncodedFrame(frame_timestamp); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1226 | frame_timestamp += kFrameIntervalMs; |
| 1227 | |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 1228 | // Trigger CPU overuse. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1229 | video_stream_encoder_->TriggerCpuOveruse(); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 1230 | video_source_.IncomingCapturedFrame( |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1231 | CreateFrame(frame_timestamp, kFrameWidth, kFrameHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1232 | WaitForEncodedFrame(frame_timestamp); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1233 | frame_timestamp += kFrameIntervalMs; |
sprang | 3ea3c77 | 2017-03-30 07:23:48 -0700 | [diff] [blame] | 1234 | |
asapersson | 0944a80 | 2017-04-07 00:57:58 -0700 | [diff] [blame] | 1235 | // Default degradation preference is maintain-framerate, so will lower max |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1236 | // wanted resolution. |
| 1237 | EXPECT_FALSE(video_source_.sink_wants().target_pixel_count); |
| 1238 | EXPECT_LT(video_source_.sink_wants().max_pixel_count, |
| 1239 | kFrameWidth * kFrameHeight); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 1240 | EXPECT_EQ(kDefaultFramerate, video_source_.sink_wants().max_framerate_fps); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1241 | |
| 1242 | // Set new source, switch to maintain-resolution. |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 1243 | test::FrameForwarder new_video_source; |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1244 | video_stream_encoder_->SetSource( |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1245 | &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 1246 | |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1247 | // Initially no degradation registered. |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 1248 | VerifyFpsMaxResolutionMax(new_video_source.sink_wants()); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 1249 | |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1250 | // Force an input frame rate to be available, or the adaptation call won't |
| 1251 | // know what framerate to adapt form. |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1252 | const int kInputFps = 30; |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1253 | VideoSendStream::Stats stats = stats_proxy_->GetStats(); |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1254 | stats.input_frame_rate = kInputFps; |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1255 | stats_proxy_->SetMockStats(stats); |
| 1256 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1257 | video_stream_encoder_->TriggerCpuOveruse(); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 1258 | new_video_source.IncomingCapturedFrame( |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1259 | CreateFrame(frame_timestamp, kFrameWidth, kFrameHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1260 | WaitForEncodedFrame(frame_timestamp); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1261 | frame_timestamp += kFrameIntervalMs; |
| 1262 | |
| 1263 | // Some framerate constraint should be set. |
sprang | 84a3759 | 2017-02-10 07:04:27 -0800 | [diff] [blame] | 1264 | EXPECT_FALSE(new_video_source.sink_wants().target_pixel_count); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1265 | EXPECT_EQ(std::numeric_limits<int>::max(), |
| 1266 | new_video_source.sink_wants().max_pixel_count); |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1267 | EXPECT_LT(new_video_source.sink_wants().max_framerate_fps, kInputFps); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1268 | |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1269 | // Turn off degradation completely. |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1270 | video_stream_encoder_->SetSource(&new_video_source, |
| 1271 | webrtc::DegradationPreference::DISABLED); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 1272 | VerifyFpsMaxResolutionMax(new_video_source.sink_wants()); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1273 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1274 | video_stream_encoder_->TriggerCpuOveruse(); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1275 | new_video_source.IncomingCapturedFrame( |
| 1276 | CreateFrame(frame_timestamp, kFrameWidth, kFrameHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1277 | WaitForEncodedFrame(frame_timestamp); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1278 | frame_timestamp += kFrameIntervalMs; |
| 1279 | |
| 1280 | // Still no degradation. |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 1281 | VerifyFpsMaxResolutionMax(new_video_source.sink_wants()); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 1282 | |
| 1283 | // Calling SetSource with resolution scaling enabled apply the old SinkWants. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1284 | video_stream_encoder_->SetSource( |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1285 | &new_video_source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1286 | EXPECT_LT(new_video_source.sink_wants().max_pixel_count, |
| 1287 | kFrameWidth * kFrameHeight); |
sprang | 84a3759 | 2017-02-10 07:04:27 -0800 | [diff] [blame] | 1288 | EXPECT_FALSE(new_video_source.sink_wants().target_pixel_count); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 1289 | EXPECT_EQ(kDefaultFramerate, new_video_source.sink_wants().max_framerate_fps); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1290 | |
| 1291 | // Calling SetSource with framerate scaling enabled apply the old SinkWants. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1292 | video_stream_encoder_->SetSource( |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1293 | &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1294 | EXPECT_FALSE(new_video_source.sink_wants().target_pixel_count); |
| 1295 | EXPECT_EQ(std::numeric_limits<int>::max(), |
| 1296 | new_video_source.sink_wants().max_pixel_count); |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1297 | EXPECT_LT(new_video_source.sink_wants().max_framerate_fps, kInputFps); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 1298 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1299 | video_stream_encoder_->Stop(); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 1300 | } |
| 1301 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1302 | TEST_F(VideoStreamEncoderTest, StatsTracksQualityAdaptationStats) { |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 1303 | video_stream_encoder_->OnBitrateUpdated( |
| 1304 | DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps), 0, 0); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 1305 | |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1306 | const int kWidth = 1280; |
| 1307 | const int kHeight = 720; |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1308 | video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1309 | WaitForEncodedFrame(1); |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1310 | VideoSendStream::Stats stats = stats_proxy_->GetStats(); |
| 1311 | EXPECT_FALSE(stats.bw_limited_resolution); |
| 1312 | EXPECT_EQ(0, stats.number_of_quality_adapt_changes); |
| 1313 | |
| 1314 | // Trigger adapt down. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1315 | video_stream_encoder_->TriggerQualityLow(); |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1316 | video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1317 | WaitForEncodedFrame(2); |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1318 | |
| 1319 | stats = stats_proxy_->GetStats(); |
| 1320 | EXPECT_TRUE(stats.bw_limited_resolution); |
| 1321 | EXPECT_EQ(1, stats.number_of_quality_adapt_changes); |
| 1322 | |
| 1323 | // Trigger adapt up. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1324 | video_stream_encoder_->TriggerQualityHigh(); |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1325 | video_source_.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1326 | WaitForEncodedFrame(3); |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1327 | |
| 1328 | stats = stats_proxy_->GetStats(); |
| 1329 | EXPECT_FALSE(stats.bw_limited_resolution); |
| 1330 | EXPECT_EQ(2, stats.number_of_quality_adapt_changes); |
| 1331 | EXPECT_EQ(0, stats.number_of_cpu_adapt_changes); |
| 1332 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1333 | video_stream_encoder_->Stop(); |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1334 | } |
| 1335 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1336 | TEST_F(VideoStreamEncoderTest, StatsTracksCpuAdaptationStats) { |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 1337 | video_stream_encoder_->OnBitrateUpdated( |
| 1338 | DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps), 0, 0); |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1339 | |
| 1340 | const int kWidth = 1280; |
| 1341 | const int kHeight = 720; |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1342 | video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1343 | WaitForEncodedFrame(1); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 1344 | VideoSendStream::Stats stats = stats_proxy_->GetStats(); |
| 1345 | EXPECT_FALSE(stats.cpu_limited_resolution); |
| 1346 | EXPECT_EQ(0, stats.number_of_cpu_adapt_changes); |
| 1347 | |
| 1348 | // Trigger CPU overuse. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1349 | video_stream_encoder_->TriggerCpuOveruse(); |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1350 | video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1351 | WaitForEncodedFrame(2); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 1352 | |
| 1353 | stats = stats_proxy_->GetStats(); |
| 1354 | EXPECT_TRUE(stats.cpu_limited_resolution); |
| 1355 | EXPECT_EQ(1, stats.number_of_cpu_adapt_changes); |
| 1356 | |
| 1357 | // Trigger CPU normal use. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1358 | video_stream_encoder_->TriggerCpuNormalUsage(); |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1359 | video_source_.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1360 | WaitForEncodedFrame(3); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 1361 | |
| 1362 | stats = stats_proxy_->GetStats(); |
| 1363 | EXPECT_FALSE(stats.cpu_limited_resolution); |
| 1364 | EXPECT_EQ(2, stats.number_of_cpu_adapt_changes); |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1365 | EXPECT_EQ(0, stats.number_of_quality_adapt_changes); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 1366 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1367 | video_stream_encoder_->Stop(); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 1368 | } |
| 1369 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1370 | TEST_F(VideoStreamEncoderTest, SwitchingSourceKeepsCpuAdaptation) { |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 1371 | video_stream_encoder_->OnBitrateUpdated( |
| 1372 | DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps), 0, 0); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1373 | |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1374 | const int kWidth = 1280; |
| 1375 | const int kHeight = 720; |
| 1376 | video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1377 | WaitForEncodedFrame(1); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1378 | VideoSendStream::Stats stats = stats_proxy_->GetStats(); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1379 | EXPECT_FALSE(stats.bw_limited_resolution); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1380 | EXPECT_FALSE(stats.cpu_limited_resolution); |
| 1381 | EXPECT_EQ(0, stats.number_of_cpu_adapt_changes); |
| 1382 | |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1383 | // Trigger CPU overuse. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1384 | video_stream_encoder_->TriggerCpuOveruse(); |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1385 | video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1386 | WaitForEncodedFrame(2); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1387 | stats = stats_proxy_->GetStats(); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1388 | EXPECT_FALSE(stats.bw_limited_resolution); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1389 | EXPECT_TRUE(stats.cpu_limited_resolution); |
| 1390 | EXPECT_EQ(1, stats.number_of_cpu_adapt_changes); |
| 1391 | |
| 1392 | // Set new source with adaptation still enabled. |
| 1393 | test::FrameForwarder new_video_source; |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1394 | video_stream_encoder_->SetSource( |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1395 | &new_video_source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1396 | |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1397 | new_video_source.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1398 | WaitForEncodedFrame(3); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1399 | stats = stats_proxy_->GetStats(); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1400 | EXPECT_FALSE(stats.bw_limited_resolution); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1401 | EXPECT_TRUE(stats.cpu_limited_resolution); |
| 1402 | EXPECT_EQ(1, stats.number_of_cpu_adapt_changes); |
| 1403 | |
| 1404 | // Set adaptation disabled. |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1405 | video_stream_encoder_->SetSource(&new_video_source, |
| 1406 | webrtc::DegradationPreference::DISABLED); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1407 | |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1408 | new_video_source.IncomingCapturedFrame(CreateFrame(4, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1409 | WaitForEncodedFrame(4); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1410 | stats = stats_proxy_->GetStats(); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1411 | EXPECT_FALSE(stats.bw_limited_resolution); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1412 | EXPECT_FALSE(stats.cpu_limited_resolution); |
| 1413 | EXPECT_EQ(1, stats.number_of_cpu_adapt_changes); |
| 1414 | |
| 1415 | // Set adaptation back to enabled. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1416 | video_stream_encoder_->SetSource( |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1417 | &new_video_source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1418 | |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1419 | new_video_source.IncomingCapturedFrame(CreateFrame(5, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1420 | WaitForEncodedFrame(5); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1421 | stats = stats_proxy_->GetStats(); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1422 | EXPECT_FALSE(stats.bw_limited_resolution); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1423 | EXPECT_TRUE(stats.cpu_limited_resolution); |
| 1424 | EXPECT_EQ(1, stats.number_of_cpu_adapt_changes); |
| 1425 | |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1426 | // Trigger CPU normal use. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1427 | video_stream_encoder_->TriggerCpuNormalUsage(); |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1428 | new_video_source.IncomingCapturedFrame(CreateFrame(6, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1429 | WaitForEncodedFrame(6); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1430 | stats = stats_proxy_->GetStats(); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1431 | EXPECT_FALSE(stats.bw_limited_resolution); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1432 | EXPECT_FALSE(stats.cpu_limited_resolution); |
| 1433 | EXPECT_EQ(2, stats.number_of_cpu_adapt_changes); |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1434 | EXPECT_EQ(0, stats.number_of_quality_adapt_changes); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1435 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1436 | video_stream_encoder_->Stop(); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1437 | } |
| 1438 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1439 | TEST_F(VideoStreamEncoderTest, SwitchingSourceKeepsQualityAdaptation) { |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 1440 | video_stream_encoder_->OnBitrateUpdated( |
| 1441 | DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps), 0, 0); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1442 | |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1443 | const int kWidth = 1280; |
| 1444 | const int kHeight = 720; |
| 1445 | video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1446 | WaitForEncodedFrame(1); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1447 | VideoSendStream::Stats stats = stats_proxy_->GetStats(); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1448 | EXPECT_FALSE(stats.bw_limited_resolution); |
asapersson | 1387476 | 2017-06-07 00:01:02 -0700 | [diff] [blame] | 1449 | EXPECT_FALSE(stats.bw_limited_framerate); |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1450 | EXPECT_EQ(0, stats.number_of_quality_adapt_changes); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1451 | |
| 1452 | // Set new source with adaptation still enabled. |
| 1453 | test::FrameForwarder new_video_source; |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1454 | video_stream_encoder_->SetSource(&new_video_source, |
| 1455 | webrtc::DegradationPreference::BALANCED); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1456 | |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1457 | new_video_source.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1458 | WaitForEncodedFrame(2); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1459 | stats = stats_proxy_->GetStats(); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1460 | EXPECT_FALSE(stats.bw_limited_resolution); |
asapersson | 1387476 | 2017-06-07 00:01:02 -0700 | [diff] [blame] | 1461 | EXPECT_FALSE(stats.bw_limited_framerate); |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1462 | EXPECT_EQ(0, stats.number_of_quality_adapt_changes); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1463 | |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1464 | // Trigger adapt down. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1465 | video_stream_encoder_->TriggerQualityLow(); |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1466 | new_video_source.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1467 | WaitForEncodedFrame(3); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1468 | stats = stats_proxy_->GetStats(); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1469 | EXPECT_TRUE(stats.bw_limited_resolution); |
asapersson | 1387476 | 2017-06-07 00:01:02 -0700 | [diff] [blame] | 1470 | EXPECT_FALSE(stats.bw_limited_framerate); |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1471 | EXPECT_EQ(1, stats.number_of_quality_adapt_changes); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1472 | |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1473 | // Set new source with adaptation still enabled. |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1474 | video_stream_encoder_->SetSource(&new_video_source, |
| 1475 | webrtc::DegradationPreference::BALANCED); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1476 | |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1477 | new_video_source.IncomingCapturedFrame(CreateFrame(4, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1478 | WaitForEncodedFrame(4); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1479 | stats = stats_proxy_->GetStats(); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1480 | EXPECT_TRUE(stats.bw_limited_resolution); |
asapersson | 1387476 | 2017-06-07 00:01:02 -0700 | [diff] [blame] | 1481 | EXPECT_FALSE(stats.bw_limited_framerate); |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1482 | EXPECT_EQ(1, stats.number_of_quality_adapt_changes); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1483 | |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1484 | // Disable resolution scaling. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1485 | video_stream_encoder_->SetSource( |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1486 | &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1487 | |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1488 | new_video_source.IncomingCapturedFrame(CreateFrame(5, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1489 | WaitForEncodedFrame(5); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1490 | stats = stats_proxy_->GetStats(); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1491 | EXPECT_FALSE(stats.bw_limited_resolution); |
asapersson | 1387476 | 2017-06-07 00:01:02 -0700 | [diff] [blame] | 1492 | EXPECT_FALSE(stats.bw_limited_framerate); |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1493 | EXPECT_EQ(1, stats.number_of_quality_adapt_changes); |
| 1494 | EXPECT_EQ(0, stats.number_of_cpu_adapt_changes); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1495 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1496 | video_stream_encoder_->Stop(); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1497 | } |
| 1498 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1499 | TEST_F(VideoStreamEncoderTest, |
| 1500 | QualityAdaptationStatsAreResetWhenScalerIsDisabled) { |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 1501 | video_stream_encoder_->OnBitrateUpdated( |
| 1502 | DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps), 0, 0); |
asapersson | 36e9eb4 | 2017-03-31 05:29:12 -0700 | [diff] [blame] | 1503 | |
| 1504 | const int kWidth = 1280; |
| 1505 | const int kHeight = 720; |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 1506 | int64_t timestamp_ms = kFrameIntervalMs; |
asapersson | 36e9eb4 | 2017-03-31 05:29:12 -0700 | [diff] [blame] | 1507 | video_source_.set_adaptation_enabled(true); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 1508 | video_source_.IncomingCapturedFrame( |
| 1509 | CreateFrame(timestamp_ms, kWidth, kHeight)); |
| 1510 | WaitForEncodedFrame(timestamp_ms); |
asapersson | 36e9eb4 | 2017-03-31 05:29:12 -0700 | [diff] [blame] | 1511 | EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution); |
| 1512 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution); |
| 1513 | EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
| 1514 | |
| 1515 | // Trigger adapt down. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1516 | video_stream_encoder_->TriggerQualityLow(); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 1517 | timestamp_ms += kFrameIntervalMs; |
| 1518 | video_source_.IncomingCapturedFrame( |
| 1519 | CreateFrame(timestamp_ms, kWidth, kHeight)); |
| 1520 | WaitForEncodedFrame(timestamp_ms); |
asapersson | 36e9eb4 | 2017-03-31 05:29:12 -0700 | [diff] [blame] | 1521 | EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution); |
| 1522 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution); |
| 1523 | EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
| 1524 | |
| 1525 | // Trigger overuse. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1526 | video_stream_encoder_->TriggerCpuOveruse(); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 1527 | timestamp_ms += kFrameIntervalMs; |
| 1528 | video_source_.IncomingCapturedFrame( |
| 1529 | CreateFrame(timestamp_ms, kWidth, kHeight)); |
| 1530 | WaitForEncodedFrame(timestamp_ms); |
asapersson | 36e9eb4 | 2017-03-31 05:29:12 -0700 | [diff] [blame] | 1531 | EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution); |
| 1532 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution); |
| 1533 | EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
| 1534 | |
Niels Möller | 4db138e | 2018-04-19 09:04:13 +0200 | [diff] [blame] | 1535 | // Leave source unchanged, but disable quality scaler. |
asapersson | 36e9eb4 | 2017-03-31 05:29:12 -0700 | [diff] [blame] | 1536 | fake_encoder_.SetQualityScaling(false); |
Niels Möller | 4db138e | 2018-04-19 09:04:13 +0200 | [diff] [blame] | 1537 | |
| 1538 | VideoEncoderConfig video_encoder_config; |
| 1539 | test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config); |
| 1540 | // Make format different, to force recreation of encoder. |
| 1541 | video_encoder_config.video_format.parameters["foo"] = "foo"; |
| 1542 | video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config), |
Niels Möller | f133856 | 2018-04-26 09:51:47 +0200 | [diff] [blame] | 1543 | kMaxPayloadLength); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 1544 | timestamp_ms += kFrameIntervalMs; |
| 1545 | video_source_.IncomingCapturedFrame( |
| 1546 | CreateFrame(timestamp_ms, kWidth, kHeight)); |
| 1547 | WaitForEncodedFrame(timestamp_ms); |
asapersson | 36e9eb4 | 2017-03-31 05:29:12 -0700 | [diff] [blame] | 1548 | EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution); |
| 1549 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution); |
| 1550 | EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
| 1551 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1552 | video_stream_encoder_->Stop(); |
asapersson | 36e9eb4 | 2017-03-31 05:29:12 -0700 | [diff] [blame] | 1553 | } |
| 1554 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1555 | TEST_F(VideoStreamEncoderTest, |
| 1556 | StatsTracksCpuAdaptationStatsWhenSwitchingSource) { |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 1557 | video_stream_encoder_->OnBitrateUpdated( |
| 1558 | DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps), 0, 0); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 1559 | |
asapersson | 0944a80 | 2017-04-07 00:57:58 -0700 | [diff] [blame] | 1560 | const int kWidth = 1280; |
| 1561 | const int kHeight = 720; |
sprang | 84a3759 | 2017-02-10 07:04:27 -0800 | [diff] [blame] | 1562 | int sequence = 1; |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 1563 | |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1564 | video_source_.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1565 | WaitForEncodedFrame(sequence++); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 1566 | VideoSendStream::Stats stats = stats_proxy_->GetStats(); |
sprang | 84a3759 | 2017-02-10 07:04:27 -0800 | [diff] [blame] | 1567 | EXPECT_FALSE(stats.cpu_limited_resolution); |
asapersson | 1387476 | 2017-06-07 00:01:02 -0700 | [diff] [blame] | 1568 | EXPECT_FALSE(stats.cpu_limited_framerate); |
sprang | 84a3759 | 2017-02-10 07:04:27 -0800 | [diff] [blame] | 1569 | EXPECT_EQ(0, stats.number_of_cpu_adapt_changes); |
| 1570 | |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1571 | // Trigger CPU overuse, should now adapt down. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1572 | video_stream_encoder_->TriggerCpuOveruse(); |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1573 | video_source_.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1574 | WaitForEncodedFrame(sequence++); |
sprang | 84a3759 | 2017-02-10 07:04:27 -0800 | [diff] [blame] | 1575 | stats = stats_proxy_->GetStats(); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 1576 | EXPECT_TRUE(stats.cpu_limited_resolution); |
asapersson | 1387476 | 2017-06-07 00:01:02 -0700 | [diff] [blame] | 1577 | EXPECT_FALSE(stats.cpu_limited_framerate); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 1578 | EXPECT_EQ(1, stats.number_of_cpu_adapt_changes); |
| 1579 | |
| 1580 | // Set new source with adaptation still enabled. |
| 1581 | test::FrameForwarder new_video_source; |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1582 | video_stream_encoder_->SetSource( |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1583 | &new_video_source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 1584 | |
| 1585 | new_video_source.IncomingCapturedFrame( |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1586 | CreateFrame(sequence, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1587 | WaitForEncodedFrame(sequence++); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 1588 | stats = stats_proxy_->GetStats(); |
| 1589 | EXPECT_TRUE(stats.cpu_limited_resolution); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1590 | EXPECT_FALSE(stats.cpu_limited_framerate); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 1591 | EXPECT_EQ(1, stats.number_of_cpu_adapt_changes); |
| 1592 | |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1593 | // Set cpu adaptation by frame dropping. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1594 | video_stream_encoder_->SetSource( |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1595 | &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 1596 | new_video_source.IncomingCapturedFrame( |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1597 | CreateFrame(sequence, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1598 | WaitForEncodedFrame(sequence++); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 1599 | stats = stats_proxy_->GetStats(); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1600 | // Not adapted at first. |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 1601 | EXPECT_FALSE(stats.cpu_limited_resolution); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1602 | EXPECT_FALSE(stats.cpu_limited_framerate); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 1603 | EXPECT_EQ(1, stats.number_of_cpu_adapt_changes); |
| 1604 | |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1605 | // Force an input frame rate to be available, or the adaptation call won't |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1606 | // know what framerate to adapt from. |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1607 | VideoSendStream::Stats mock_stats = stats_proxy_->GetStats(); |
| 1608 | mock_stats.input_frame_rate = 30; |
| 1609 | stats_proxy_->SetMockStats(mock_stats); |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1610 | video_stream_encoder_->TriggerCpuOveruse(); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1611 | stats_proxy_->ResetMockStats(); |
| 1612 | |
| 1613 | new_video_source.IncomingCapturedFrame( |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1614 | CreateFrame(sequence, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1615 | WaitForEncodedFrame(sequence++); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1616 | |
| 1617 | // Framerate now adapted. |
| 1618 | stats = stats_proxy_->GetStats(); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1619 | EXPECT_FALSE(stats.cpu_limited_resolution); |
| 1620 | EXPECT_TRUE(stats.cpu_limited_framerate); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1621 | EXPECT_EQ(2, stats.number_of_cpu_adapt_changes); |
| 1622 | |
| 1623 | // Disable CPU adaptation. |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1624 | video_stream_encoder_->SetSource(&new_video_source, |
| 1625 | webrtc::DegradationPreference::DISABLED); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1626 | new_video_source.IncomingCapturedFrame( |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1627 | CreateFrame(sequence, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1628 | WaitForEncodedFrame(sequence++); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1629 | |
| 1630 | stats = stats_proxy_->GetStats(); |
| 1631 | EXPECT_FALSE(stats.cpu_limited_resolution); |
asapersson | 1387476 | 2017-06-07 00:01:02 -0700 | [diff] [blame] | 1632 | EXPECT_FALSE(stats.cpu_limited_framerate); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1633 | EXPECT_EQ(2, stats.number_of_cpu_adapt_changes); |
| 1634 | |
| 1635 | // Try to trigger overuse. Should not succeed. |
| 1636 | stats_proxy_->SetMockStats(mock_stats); |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1637 | video_stream_encoder_->TriggerCpuOveruse(); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1638 | stats_proxy_->ResetMockStats(); |
| 1639 | |
| 1640 | stats = stats_proxy_->GetStats(); |
| 1641 | EXPECT_FALSE(stats.cpu_limited_resolution); |
asapersson | 1387476 | 2017-06-07 00:01:02 -0700 | [diff] [blame] | 1642 | EXPECT_FALSE(stats.cpu_limited_framerate); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1643 | EXPECT_EQ(2, stats.number_of_cpu_adapt_changes); |
| 1644 | |
| 1645 | // Switch back the source with resolution adaptation enabled. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1646 | video_stream_encoder_->SetSource( |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1647 | &video_source_, webrtc::DegradationPreference::MAINTAIN_FRAMERATE); |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1648 | video_source_.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1649 | WaitForEncodedFrame(sequence++); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 1650 | stats = stats_proxy_->GetStats(); |
| 1651 | EXPECT_TRUE(stats.cpu_limited_resolution); |
asapersson | 1387476 | 2017-06-07 00:01:02 -0700 | [diff] [blame] | 1652 | EXPECT_FALSE(stats.cpu_limited_framerate); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1653 | EXPECT_EQ(2, stats.number_of_cpu_adapt_changes); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 1654 | |
| 1655 | // Trigger CPU normal usage. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1656 | video_stream_encoder_->TriggerCpuNormalUsage(); |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1657 | video_source_.IncomingCapturedFrame(CreateFrame(sequence, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1658 | WaitForEncodedFrame(sequence++); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 1659 | stats = stats_proxy_->GetStats(); |
| 1660 | EXPECT_FALSE(stats.cpu_limited_resolution); |
asapersson | 1387476 | 2017-06-07 00:01:02 -0700 | [diff] [blame] | 1661 | EXPECT_FALSE(stats.cpu_limited_framerate); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1662 | EXPECT_EQ(3, stats.number_of_cpu_adapt_changes); |
| 1663 | |
| 1664 | // Back to the source with adaptation off, set it back to maintain-resolution. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1665 | video_stream_encoder_->SetSource( |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1666 | &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1667 | new_video_source.IncomingCapturedFrame( |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1668 | CreateFrame(sequence, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1669 | WaitForEncodedFrame(sequence++); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1670 | stats = stats_proxy_->GetStats(); |
asapersson | 1387476 | 2017-06-07 00:01:02 -0700 | [diff] [blame] | 1671 | // Disabled, since we previously switched the source to disabled. |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1672 | EXPECT_FALSE(stats.cpu_limited_resolution); |
asapersson | 1387476 | 2017-06-07 00:01:02 -0700 | [diff] [blame] | 1673 | EXPECT_TRUE(stats.cpu_limited_framerate); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1674 | EXPECT_EQ(3, stats.number_of_cpu_adapt_changes); |
| 1675 | |
| 1676 | // Trigger CPU normal usage. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1677 | video_stream_encoder_->TriggerCpuNormalUsage(); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1678 | new_video_source.IncomingCapturedFrame( |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1679 | CreateFrame(sequence, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1680 | WaitForEncodedFrame(sequence++); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1681 | stats = stats_proxy_->GetStats(); |
| 1682 | EXPECT_FALSE(stats.cpu_limited_resolution); |
asapersson | 1387476 | 2017-06-07 00:01:02 -0700 | [diff] [blame] | 1683 | EXPECT_FALSE(stats.cpu_limited_framerate); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1684 | EXPECT_EQ(4, stats.number_of_cpu_adapt_changes); |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1685 | EXPECT_EQ(0, stats.number_of_quality_adapt_changes); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 1686 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1687 | video_stream_encoder_->Stop(); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 1688 | } |
| 1689 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1690 | TEST_F(VideoStreamEncoderTest, |
| 1691 | ScalingUpAndDownDoesNothingWithMaintainResolution) { |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1692 | const int kWidth = 1280; |
| 1693 | const int kHeight = 720; |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 1694 | video_stream_encoder_->OnBitrateUpdated( |
| 1695 | DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps), 0, 0); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1696 | |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1697 | // Expect no scaling to begin with. |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1698 | VerifyNoLimitation(video_source_.sink_wants()); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1699 | |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1700 | video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1701 | WaitForEncodedFrame(1); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1702 | |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1703 | // Trigger scale down. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1704 | video_stream_encoder_->TriggerQualityLow(); |
kthelgason | 5e13d41 | 2016-12-01 03:59:51 -0800 | [diff] [blame] | 1705 | |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1706 | video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1707 | WaitForEncodedFrame(2); |
kthelgason | 5e13d41 | 2016-12-01 03:59:51 -0800 | [diff] [blame] | 1708 | |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1709 | // Expect a scale down. |
| 1710 | EXPECT_TRUE(video_source_.sink_wants().max_pixel_count); |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1711 | EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1712 | |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1713 | // Set resolution scaling disabled. |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1714 | test::FrameForwarder new_video_source; |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1715 | video_stream_encoder_->SetSource( |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1716 | &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1717 | |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1718 | // Trigger scale down. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1719 | video_stream_encoder_->TriggerQualityLow(); |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1720 | new_video_source.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1721 | WaitForEncodedFrame(3); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1722 | |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1723 | // Expect no scaling. |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1724 | EXPECT_EQ(std::numeric_limits<int>::max(), |
| 1725 | new_video_source.sink_wants().max_pixel_count); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1726 | |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1727 | // Trigger scale up. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1728 | video_stream_encoder_->TriggerQualityHigh(); |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 1729 | new_video_source.IncomingCapturedFrame(CreateFrame(4, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1730 | WaitForEncodedFrame(4); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1731 | |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1732 | // Expect nothing to change, still no scaling. |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 1733 | EXPECT_EQ(std::numeric_limits<int>::max(), |
| 1734 | new_video_source.sink_wants().max_pixel_count); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1735 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1736 | video_stream_encoder_->Stop(); |
kthelgason | 876222f | 2016-11-29 01:44:11 -0800 | [diff] [blame] | 1737 | } |
| 1738 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1739 | TEST_F(VideoStreamEncoderTest, |
| 1740 | SkipsSameAdaptDownRequest_MaintainFramerateMode) { |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1741 | const int kWidth = 1280; |
| 1742 | const int kHeight = 720; |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 1743 | video_stream_encoder_->OnBitrateUpdated( |
| 1744 | DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps), 0, 0); |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1745 | |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1746 | // Enable MAINTAIN_FRAMERATE preference, no initial limitation. |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1747 | test::FrameForwarder source; |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1748 | video_stream_encoder_->SetSource( |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1749 | &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE); |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1750 | |
| 1751 | source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1752 | WaitForEncodedFrame(1); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 1753 | VerifyFpsMaxResolutionMax(source.sink_wants()); |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1754 | EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution); |
| 1755 | EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
| 1756 | |
| 1757 | // Trigger adapt down, expect scaled down resolution. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1758 | video_stream_encoder_->TriggerCpuOveruse(); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1759 | VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight); |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1760 | const int kLastMaxPixelCount = source.sink_wants().max_pixel_count; |
| 1761 | EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution); |
| 1762 | EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
| 1763 | |
| 1764 | // Trigger adapt down for same input resolution, expect no change. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1765 | video_stream_encoder_->TriggerCpuOveruse(); |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1766 | EXPECT_EQ(kLastMaxPixelCount, source.sink_wants().max_pixel_count); |
| 1767 | EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution); |
| 1768 | EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
| 1769 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1770 | video_stream_encoder_->Stop(); |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1771 | } |
| 1772 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1773 | TEST_F(VideoStreamEncoderTest, SkipsSameOrLargerAdaptDownRequest_BalancedMode) { |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 1774 | const int kWidth = 1280; |
| 1775 | const int kHeight = 720; |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 1776 | video_stream_encoder_->OnBitrateUpdated( |
| 1777 | DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps), 0, 0); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 1778 | |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1779 | // Enable BALANCED preference, no initial limitation. |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 1780 | test::FrameForwarder source; |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1781 | video_stream_encoder_->SetSource(&source, |
| 1782 | webrtc::DegradationPreference::BALANCED); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 1783 | source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight)); |
| 1784 | sink_.WaitForEncodedFrame(1); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 1785 | VerifyFpsMaxResolutionMax(source.sink_wants()); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 1786 | |
| 1787 | // Trigger adapt down, expect scaled down resolution. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1788 | video_stream_encoder_->TriggerQualityLow(); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 1789 | VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight); |
| 1790 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution); |
| 1791 | EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 1792 | const int kLastMaxPixelCount = source.sink_wants().max_pixel_count; |
| 1793 | |
| 1794 | // Trigger adapt down for same input resolution, expect no change. |
| 1795 | source.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight)); |
| 1796 | sink_.WaitForEncodedFrame(2); |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1797 | video_stream_encoder_->TriggerQualityLow(); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 1798 | EXPECT_EQ(kLastMaxPixelCount, source.sink_wants().max_pixel_count); |
| 1799 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution); |
| 1800 | EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 1801 | |
| 1802 | // Trigger adapt down for larger input resolution, expect no change. |
| 1803 | source.IncomingCapturedFrame(CreateFrame(3, kWidth + 1, kHeight + 1)); |
| 1804 | sink_.WaitForEncodedFrame(3); |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1805 | video_stream_encoder_->TriggerQualityLow(); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 1806 | EXPECT_EQ(kLastMaxPixelCount, source.sink_wants().max_pixel_count); |
| 1807 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution); |
| 1808 | EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 1809 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1810 | video_stream_encoder_->Stop(); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 1811 | } |
| 1812 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1813 | TEST_F(VideoStreamEncoderTest, |
| 1814 | NoChangeForInitialNormalUsage_MaintainFramerateMode) { |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1815 | const int kWidth = 1280; |
| 1816 | const int kHeight = 720; |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 1817 | video_stream_encoder_->OnBitrateUpdated( |
| 1818 | DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps), 0, 0); |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1819 | |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1820 | // Enable MAINTAIN_FRAMERATE preference, no initial limitation. |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1821 | test::FrameForwarder source; |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1822 | video_stream_encoder_->SetSource( |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1823 | &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE); |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1824 | |
| 1825 | source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1826 | WaitForEncodedFrame(kWidth, kHeight); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 1827 | VerifyFpsMaxResolutionMax(source.sink_wants()); |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1828 | EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution); |
| 1829 | EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
| 1830 | |
| 1831 | // Trigger adapt up, expect no change. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1832 | video_stream_encoder_->TriggerCpuNormalUsage(); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 1833 | VerifyFpsMaxResolutionMax(source.sink_wants()); |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1834 | EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution); |
| 1835 | EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
| 1836 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1837 | video_stream_encoder_->Stop(); |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1838 | } |
| 1839 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1840 | TEST_F(VideoStreamEncoderTest, |
| 1841 | NoChangeForInitialNormalUsage_MaintainResolutionMode) { |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1842 | const int kWidth = 1280; |
| 1843 | const int kHeight = 720; |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 1844 | video_stream_encoder_->OnBitrateUpdated( |
| 1845 | DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps), 0, 0); |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1846 | |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1847 | // Enable MAINTAIN_RESOLUTION preference, no initial limitation. |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1848 | test::FrameForwarder source; |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1849 | video_stream_encoder_->SetSource( |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1850 | &source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION); |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1851 | |
| 1852 | source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1853 | WaitForEncodedFrame(kWidth, kHeight); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 1854 | VerifyFpsMaxResolutionMax(source.sink_wants()); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1855 | EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate); |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1856 | EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
| 1857 | |
| 1858 | // Trigger adapt up, expect no change. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1859 | video_stream_encoder_->TriggerCpuNormalUsage(); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 1860 | VerifyFpsMaxResolutionMax(source.sink_wants()); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1861 | EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate); |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1862 | EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
| 1863 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1864 | video_stream_encoder_->Stop(); |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1865 | } |
| 1866 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1867 | TEST_F(VideoStreamEncoderTest, NoChangeForInitialNormalUsage_BalancedMode) { |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 1868 | const int kWidth = 1280; |
| 1869 | const int kHeight = 720; |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 1870 | video_stream_encoder_->OnBitrateUpdated( |
| 1871 | DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps), 0, 0); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 1872 | |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1873 | // Enable BALANCED preference, no initial limitation. |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 1874 | test::FrameForwarder source; |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1875 | video_stream_encoder_->SetSource(&source, |
| 1876 | webrtc::DegradationPreference::BALANCED); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 1877 | |
| 1878 | source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight)); |
| 1879 | sink_.WaitForEncodedFrame(kWidth, kHeight); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 1880 | VerifyFpsMaxResolutionMax(source.sink_wants()); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 1881 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution); |
| 1882 | EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate); |
| 1883 | EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 1884 | |
| 1885 | // Trigger adapt up, expect no change. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1886 | video_stream_encoder_->TriggerQualityHigh(); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 1887 | VerifyFpsMaxResolutionMax(source.sink_wants()); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 1888 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution); |
| 1889 | EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate); |
| 1890 | EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 1891 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1892 | video_stream_encoder_->Stop(); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 1893 | } |
| 1894 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1895 | TEST_F(VideoStreamEncoderTest, NoChangeForInitialNormalUsage_DisabledMode) { |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1896 | const int kWidth = 1280; |
| 1897 | const int kHeight = 720; |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 1898 | video_stream_encoder_->OnBitrateUpdated( |
| 1899 | DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps), 0, 0); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1900 | |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1901 | // Enable DISABLED preference, no initial limitation. |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1902 | test::FrameForwarder source; |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1903 | video_stream_encoder_->SetSource(&source, |
| 1904 | webrtc::DegradationPreference::DISABLED); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1905 | |
| 1906 | source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight)); |
| 1907 | sink_.WaitForEncodedFrame(kWidth, kHeight); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 1908 | VerifyFpsMaxResolutionMax(source.sink_wants()); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1909 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution); |
| 1910 | EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate); |
| 1911 | EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 1912 | |
| 1913 | // Trigger adapt up, expect no change. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1914 | video_stream_encoder_->TriggerQualityHigh(); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 1915 | VerifyFpsMaxResolutionMax(source.sink_wants()); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1916 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution); |
| 1917 | EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate); |
| 1918 | EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 1919 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1920 | video_stream_encoder_->Stop(); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1921 | } |
| 1922 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1923 | TEST_F(VideoStreamEncoderTest, |
| 1924 | AdaptsResolutionForLowQuality_MaintainFramerateMode) { |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1925 | const int kWidth = 1280; |
| 1926 | const int kHeight = 720; |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 1927 | video_stream_encoder_->OnBitrateUpdated( |
| 1928 | DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps), 0, 0); |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1929 | |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1930 | // Enable MAINTAIN_FRAMERATE preference, no initial limitation. |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1931 | AdaptingFrameForwarder source; |
| 1932 | source.set_adaptation_enabled(true); |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1933 | video_stream_encoder_->SetSource( |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1934 | &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE); |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1935 | |
| 1936 | source.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1937 | WaitForEncodedFrame(1); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 1938 | VerifyFpsMaxResolutionMax(source.sink_wants()); |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1939 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution); |
| 1940 | EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 1941 | |
| 1942 | // Trigger adapt down, expect scaled down resolution. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1943 | video_stream_encoder_->TriggerQualityLow(); |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1944 | source.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 1945 | WaitForEncodedFrame(2); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1946 | VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight); |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1947 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution); |
| 1948 | EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 1949 | |
| 1950 | // Trigger adapt up, expect no restriction. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1951 | video_stream_encoder_->TriggerQualityHigh(); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 1952 | VerifyFpsMaxResolutionMax(source.sink_wants()); |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1953 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution); |
| 1954 | EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 1955 | EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
| 1956 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1957 | video_stream_encoder_->Stop(); |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 1958 | } |
| 1959 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1960 | TEST_F(VideoStreamEncoderTest, |
| 1961 | AdaptsFramerateForLowQuality_MaintainResolutionMode) { |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1962 | const int kWidth = 1280; |
| 1963 | const int kHeight = 720; |
| 1964 | const int kInputFps = 30; |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 1965 | video_stream_encoder_->OnBitrateUpdated( |
| 1966 | DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps), 0, 0); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1967 | |
| 1968 | VideoSendStream::Stats stats = stats_proxy_->GetStats(); |
| 1969 | stats.input_frame_rate = kInputFps; |
| 1970 | stats_proxy_->SetMockStats(stats); |
| 1971 | |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1972 | // Expect no scaling to begin with (preference: MAINTAIN_FRAMERATE). |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1973 | video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight)); |
| 1974 | sink_.WaitForEncodedFrame(1); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 1975 | VerifyFpsMaxResolutionMax(video_source_.sink_wants()); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1976 | |
| 1977 | // Trigger adapt down, expect scaled down resolution. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1978 | video_stream_encoder_->TriggerQualityLow(); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1979 | video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight)); |
| 1980 | sink_.WaitForEncodedFrame(2); |
| 1981 | VerifyFpsMaxResolutionLt(video_source_.sink_wants(), kWidth * kHeight); |
| 1982 | |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1983 | // Enable MAINTAIN_RESOLUTION preference. |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1984 | test::FrameForwarder new_video_source; |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1985 | video_stream_encoder_->SetSource( |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 1986 | &new_video_source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 1987 | VerifyFpsMaxResolutionMax(new_video_source.sink_wants()); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1988 | |
| 1989 | // Trigger adapt down, expect reduced framerate. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1990 | video_stream_encoder_->TriggerQualityLow(); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1991 | new_video_source.IncomingCapturedFrame(CreateFrame(3, kWidth, kHeight)); |
| 1992 | sink_.WaitForEncodedFrame(3); |
| 1993 | VerifyFpsLtResolutionMax(new_video_source.sink_wants(), kInputFps); |
| 1994 | |
| 1995 | // Trigger adapt up, expect no restriction. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1996 | video_stream_encoder_->TriggerQualityHigh(); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 1997 | VerifyFpsMaxResolutionMax(new_video_source.sink_wants()); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 1998 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 1999 | video_stream_encoder_->Stop(); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 2000 | } |
| 2001 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2002 | TEST_F(VideoStreamEncoderTest, DoesNotScaleBelowSetResolutionLimit) { |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 2003 | const int kWidth = 1280; |
| 2004 | const int kHeight = 720; |
| 2005 | const size_t kNumFrames = 10; |
| 2006 | |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 2007 | video_stream_encoder_->OnBitrateUpdated( |
| 2008 | DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps), 0, 0); |
kthelgason | 5e13d41 | 2016-12-01 03:59:51 -0800 | [diff] [blame] | 2009 | |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 2010 | // Enable adapter, expected input resolutions when downscaling: |
asapersson | 142fcc9 | 2017-08-17 08:58:54 -0700 | [diff] [blame] | 2011 | // 1280x720 -> 960x540 -> 640x360 -> 480x270 -> 320x180 (kMinPixelsPerFrame) |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 2012 | video_source_.set_adaptation_enabled(true); |
| 2013 | |
| 2014 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution); |
| 2015 | EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 2016 | |
| 2017 | int downscales = 0; |
| 2018 | for (size_t i = 1; i <= kNumFrames; i++) { |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 2019 | video_source_.IncomingCapturedFrame( |
| 2020 | CreateFrame(i * kFrameIntervalMs, kWidth, kHeight)); |
| 2021 | WaitForEncodedFrame(i * kFrameIntervalMs); |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 2022 | |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 2023 | // Trigger scale down. |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 2024 | rtc::VideoSinkWants last_wants = video_source_.sink_wants(); |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2025 | video_stream_encoder_->TriggerQualityLow(); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2026 | EXPECT_GE(video_source_.sink_wants().max_pixel_count, kMinPixelsPerFrame); |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 2027 | |
| 2028 | if (video_source_.sink_wants().max_pixel_count < last_wants.max_pixel_count) |
| 2029 | ++downscales; |
| 2030 | |
| 2031 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution); |
| 2032 | EXPECT_EQ(downscales, |
| 2033 | stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 2034 | EXPECT_GT(downscales, 0); |
kthelgason | 5e13d41 | 2016-12-01 03:59:51 -0800 | [diff] [blame] | 2035 | } |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2036 | video_stream_encoder_->Stop(); |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 2037 | } |
| 2038 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2039 | TEST_F(VideoStreamEncoderTest, |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 2040 | AdaptsResolutionUpAndDownTwiceOnOveruse_MaintainFramerateMode) { |
| 2041 | const int kWidth = 1280; |
| 2042 | const int kHeight = 720; |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 2043 | video_stream_encoder_->OnBitrateUpdated( |
| 2044 | DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps), 0, 0); |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 2045 | |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 2046 | // Enable MAINTAIN_FRAMERATE preference, no initial limitation. |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 2047 | AdaptingFrameForwarder source; |
| 2048 | source.set_adaptation_enabled(true); |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2049 | video_stream_encoder_->SetSource( |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 2050 | &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE); |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 2051 | |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 2052 | int64_t timestamp_ms = kFrameIntervalMs; |
| 2053 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2054 | WaitForEncodedFrame(kWidth, kHeight); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 2055 | VerifyFpsMaxResolutionMax(source.sink_wants()); |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 2056 | EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution); |
| 2057 | EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
| 2058 | |
| 2059 | // Trigger adapt down, expect scaled down resolution. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2060 | video_stream_encoder_->TriggerCpuOveruse(); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 2061 | timestamp_ms += kFrameIntervalMs; |
| 2062 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
| 2063 | WaitForEncodedFrame(timestamp_ms); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 2064 | VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight); |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 2065 | EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution); |
| 2066 | EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
| 2067 | |
| 2068 | // Trigger adapt up, expect no restriction. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2069 | video_stream_encoder_->TriggerCpuNormalUsage(); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 2070 | timestamp_ms += kFrameIntervalMs; |
| 2071 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2072 | WaitForEncodedFrame(kWidth, kHeight); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 2073 | VerifyFpsMaxResolutionMax(source.sink_wants()); |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 2074 | EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution); |
| 2075 | EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
| 2076 | |
| 2077 | // Trigger adapt down, expect scaled down resolution. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2078 | video_stream_encoder_->TriggerCpuOveruse(); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 2079 | timestamp_ms += kFrameIntervalMs; |
| 2080 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
| 2081 | WaitForEncodedFrame(timestamp_ms); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 2082 | VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight); |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 2083 | EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution); |
| 2084 | EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
| 2085 | |
| 2086 | // Trigger adapt up, expect no restriction. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2087 | video_stream_encoder_->TriggerCpuNormalUsage(); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 2088 | timestamp_ms += kFrameIntervalMs; |
| 2089 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 2090 | sink_.WaitForEncodedFrame(kWidth, kHeight); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 2091 | VerifyFpsMaxResolutionMax(source.sink_wants()); |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 2092 | EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution); |
| 2093 | EXPECT_EQ(4, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
| 2094 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2095 | video_stream_encoder_->Stop(); |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 2096 | } |
| 2097 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2098 | TEST_F(VideoStreamEncoderTest, |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2099 | AdaptsResolutionUpAndDownTwiceForLowQuality_BalancedMode_NoFpsLimit) { |
| 2100 | const int kWidth = 1280; |
| 2101 | const int kHeight = 720; |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 2102 | video_stream_encoder_->OnBitrateUpdated( |
| 2103 | DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps), 0, 0); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2104 | |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 2105 | // Enable BALANCED preference, no initial limitation. |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2106 | AdaptingFrameForwarder source; |
| 2107 | source.set_adaptation_enabled(true); |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 2108 | video_stream_encoder_->SetSource(&source, |
| 2109 | webrtc::DegradationPreference::BALANCED); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2110 | |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 2111 | int64_t timestamp_ms = kFrameIntervalMs; |
| 2112 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2113 | sink_.WaitForEncodedFrame(kWidth, kHeight); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 2114 | VerifyFpsMaxResolutionMax(source.sink_wants()); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2115 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution); |
| 2116 | EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 2117 | |
| 2118 | // Trigger adapt down, expect scaled down resolution. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2119 | video_stream_encoder_->TriggerQualityLow(); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 2120 | timestamp_ms += kFrameIntervalMs; |
| 2121 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
| 2122 | sink_.WaitForEncodedFrame(timestamp_ms); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2123 | VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight); |
| 2124 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution); |
| 2125 | EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 2126 | |
| 2127 | // Trigger adapt up, expect no restriction. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2128 | video_stream_encoder_->TriggerQualityHigh(); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 2129 | timestamp_ms += kFrameIntervalMs; |
| 2130 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2131 | sink_.WaitForEncodedFrame(kWidth, kHeight); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 2132 | VerifyFpsMaxResolutionMax(source.sink_wants()); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2133 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution); |
| 2134 | EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 2135 | |
| 2136 | // Trigger adapt down, expect scaled down resolution. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2137 | video_stream_encoder_->TriggerQualityLow(); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 2138 | timestamp_ms += kFrameIntervalMs; |
| 2139 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
| 2140 | sink_.WaitForEncodedFrame(timestamp_ms); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2141 | VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight); |
| 2142 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution); |
| 2143 | EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 2144 | |
| 2145 | // Trigger adapt up, expect no restriction. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2146 | video_stream_encoder_->TriggerQualityHigh(); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 2147 | timestamp_ms += kFrameIntervalMs; |
| 2148 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2149 | sink_.WaitForEncodedFrame(kWidth, kHeight); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 2150 | VerifyFpsMaxResolutionMax(source.sink_wants()); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2151 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution); |
| 2152 | EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 2153 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2154 | video_stream_encoder_->Stop(); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2155 | } |
| 2156 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2157 | TEST_F(VideoStreamEncoderTest, |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 2158 | AdaptsResolutionOnOveruseAndLowQuality_MaintainFramerateMode) { |
| 2159 | const int kWidth = 1280; |
| 2160 | const int kHeight = 720; |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 2161 | video_stream_encoder_->OnBitrateUpdated( |
| 2162 | DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps), 0, 0); |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 2163 | |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 2164 | // Enable MAINTAIN_FRAMERATE preference, no initial limitation. |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 2165 | AdaptingFrameForwarder source; |
| 2166 | source.set_adaptation_enabled(true); |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2167 | video_stream_encoder_->SetSource( |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 2168 | &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE); |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 2169 | |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 2170 | int64_t timestamp_ms = kFrameIntervalMs; |
| 2171 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2172 | WaitForEncodedFrame(kWidth, kHeight); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 2173 | VerifyFpsMaxResolutionMax(source.sink_wants()); |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 2174 | EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution); |
| 2175 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution); |
| 2176 | EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
| 2177 | EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 2178 | |
| 2179 | // Trigger cpu adapt down, expect scaled down resolution (960x540). |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2180 | video_stream_encoder_->TriggerCpuOveruse(); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 2181 | timestamp_ms += kFrameIntervalMs; |
| 2182 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
| 2183 | WaitForEncodedFrame(timestamp_ms); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 2184 | VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight); |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 2185 | EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution); |
| 2186 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution); |
| 2187 | EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
| 2188 | EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 2189 | |
| 2190 | // Trigger cpu adapt down, expect scaled down resolution (640x360). |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2191 | video_stream_encoder_->TriggerCpuOveruse(); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 2192 | timestamp_ms += kFrameIntervalMs; |
| 2193 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
| 2194 | WaitForEncodedFrame(timestamp_ms); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 2195 | VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants()); |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 2196 | EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution); |
| 2197 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution); |
| 2198 | EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
| 2199 | EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 2200 | |
Jonathan Yu | bc771b7 | 2017-12-08 17:04:29 -0800 | [diff] [blame] | 2201 | // Trigger cpu adapt down, expect scaled down resolution (480x270). |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2202 | video_stream_encoder_->TriggerCpuOveruse(); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 2203 | timestamp_ms += kFrameIntervalMs; |
| 2204 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
| 2205 | WaitForEncodedFrame(timestamp_ms); |
Jonathan Yu | bc771b7 | 2017-12-08 17:04:29 -0800 | [diff] [blame] | 2206 | VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants()); |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 2207 | EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution); |
| 2208 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution); |
Jonathan Yu | bc771b7 | 2017-12-08 17:04:29 -0800 | [diff] [blame] | 2209 | EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 2210 | EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 2211 | |
Jonathan Yu | bc771b7 | 2017-12-08 17:04:29 -0800 | [diff] [blame] | 2212 | // Trigger quality adapt down, expect scaled down resolution (320x180). |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2213 | video_stream_encoder_->TriggerQualityLow(); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 2214 | timestamp_ms += kFrameIntervalMs; |
| 2215 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
| 2216 | WaitForEncodedFrame(timestamp_ms); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 2217 | VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants()); |
Jonathan Yu | bc771b7 | 2017-12-08 17:04:29 -0800 | [diff] [blame] | 2218 | rtc::VideoSinkWants last_wants = source.sink_wants(); |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 2219 | EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution); |
| 2220 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution); |
| 2221 | EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
| 2222 | EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 2223 | |
Jonathan Yu | bc771b7 | 2017-12-08 17:04:29 -0800 | [diff] [blame] | 2224 | // Trigger quality adapt down, expect no change (min resolution reached). |
| 2225 | video_stream_encoder_->TriggerQualityLow(); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 2226 | timestamp_ms += kFrameIntervalMs; |
| 2227 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
| 2228 | WaitForEncodedFrame(timestamp_ms); |
Jonathan Yu | bc771b7 | 2017-12-08 17:04:29 -0800 | [diff] [blame] | 2229 | VerifyFpsMaxResolutionEq(source.sink_wants(), last_wants); |
| 2230 | EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution); |
| 2231 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution); |
| 2232 | EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
| 2233 | EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 2234 | |
| 2235 | // Trigger cpu adapt up, expect upscaled resolution (480x270). |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2236 | video_stream_encoder_->TriggerCpuNormalUsage(); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 2237 | timestamp_ms += kFrameIntervalMs; |
| 2238 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
| 2239 | WaitForEncodedFrame(timestamp_ms); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 2240 | VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants()); |
Jonathan Yu | bc771b7 | 2017-12-08 17:04:29 -0800 | [diff] [blame] | 2241 | EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution); |
| 2242 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution); |
| 2243 | EXPECT_EQ(4, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
| 2244 | EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 2245 | |
| 2246 | // Trigger cpu adapt up, expect upscaled resolution (640x360). |
| 2247 | video_stream_encoder_->TriggerCpuNormalUsage(); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 2248 | timestamp_ms += kFrameIntervalMs; |
| 2249 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
| 2250 | WaitForEncodedFrame(timestamp_ms); |
Jonathan Yu | bc771b7 | 2017-12-08 17:04:29 -0800 | [diff] [blame] | 2251 | VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants()); |
| 2252 | EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution); |
| 2253 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution); |
| 2254 | EXPECT_EQ(5, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
| 2255 | EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 2256 | |
| 2257 | // Trigger cpu adapt up, expect upscaled resolution (960x540). |
| 2258 | video_stream_encoder_->TriggerCpuNormalUsage(); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 2259 | timestamp_ms += kFrameIntervalMs; |
| 2260 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
| 2261 | WaitForEncodedFrame(timestamp_ms); |
Jonathan Yu | bc771b7 | 2017-12-08 17:04:29 -0800 | [diff] [blame] | 2262 | VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants()); |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 2263 | last_wants = source.sink_wants(); |
| 2264 | EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution); |
| 2265 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution); |
Jonathan Yu | bc771b7 | 2017-12-08 17:04:29 -0800 | [diff] [blame] | 2266 | EXPECT_EQ(6, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 2267 | EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 2268 | |
| 2269 | // Trigger cpu adapt up, no cpu downgrades, expect no change (960x540). |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2270 | video_stream_encoder_->TriggerCpuNormalUsage(); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 2271 | timestamp_ms += kFrameIntervalMs; |
| 2272 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
| 2273 | WaitForEncodedFrame(timestamp_ms); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 2274 | VerifyFpsEqResolutionEq(source.sink_wants(), last_wants); |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 2275 | EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution); |
| 2276 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution); |
Jonathan Yu | bc771b7 | 2017-12-08 17:04:29 -0800 | [diff] [blame] | 2277 | EXPECT_EQ(6, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 2278 | EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 2279 | |
| 2280 | // Trigger quality adapt up, expect no restriction (1280x720). |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2281 | video_stream_encoder_->TriggerQualityHigh(); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 2282 | timestamp_ms += kFrameIntervalMs; |
| 2283 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2284 | WaitForEncodedFrame(kWidth, kHeight); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 2285 | VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants()); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 2286 | VerifyFpsMaxResolutionMax(source.sink_wants()); |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 2287 | EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution); |
| 2288 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution); |
Jonathan Yu | bc771b7 | 2017-12-08 17:04:29 -0800 | [diff] [blame] | 2289 | EXPECT_EQ(6, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 2290 | EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
kthelgason | 5e13d41 | 2016-12-01 03:59:51 -0800 | [diff] [blame] | 2291 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2292 | video_stream_encoder_->Stop(); |
kthelgason | 5e13d41 | 2016-12-01 03:59:51 -0800 | [diff] [blame] | 2293 | } |
| 2294 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2295 | TEST_F(VideoStreamEncoderTest, CpuLimitedHistogramIsReported) { |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 2296 | const int kWidth = 640; |
| 2297 | const int kHeight = 360; |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 2298 | |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 2299 | video_stream_encoder_->OnBitrateUpdated( |
| 2300 | DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps), 0, 0); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2301 | |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 2302 | for (int i = 1; i <= SendStatisticsProxy::kMinRequiredMetricsSamples; ++i) { |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 2303 | video_source_.IncomingCapturedFrame(CreateFrame(i, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2304 | WaitForEncodedFrame(i); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 2305 | } |
| 2306 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2307 | video_stream_encoder_->TriggerCpuOveruse(); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 2308 | for (int i = 1; i <= SendStatisticsProxy::kMinRequiredMetricsSamples; ++i) { |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 2309 | video_source_.IncomingCapturedFrame(CreateFrame( |
| 2310 | SendStatisticsProxy::kMinRequiredMetricsSamples + i, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2311 | WaitForEncodedFrame(SendStatisticsProxy::kMinRequiredMetricsSamples + i); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 2312 | } |
| 2313 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2314 | video_stream_encoder_->Stop(); |
| 2315 | video_stream_encoder_.reset(); |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 2316 | stats_proxy_.reset(); |
sprang | f8ee65e | 2017-02-28 08:49:33 -0800 | [diff] [blame] | 2317 | |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 2318 | EXPECT_EQ(1, |
| 2319 | metrics::NumSamples("WebRTC.Video.CpuLimitedResolutionInPercent")); |
| 2320 | EXPECT_EQ( |
| 2321 | 1, metrics::NumEvents("WebRTC.Video.CpuLimitedResolutionInPercent", 50)); |
| 2322 | } |
| 2323 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2324 | TEST_F(VideoStreamEncoderTest, |
| 2325 | CpuLimitedHistogramIsNotReportedForDisabledDegradation) { |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 2326 | video_stream_encoder_->OnBitrateUpdated( |
| 2327 | DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps), 0, 0); |
asapersson | f4e44af | 2017-04-19 02:01:06 -0700 | [diff] [blame] | 2328 | const int kWidth = 640; |
| 2329 | const int kHeight = 360; |
| 2330 | |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 2331 | video_stream_encoder_->SetSource(&video_source_, |
| 2332 | webrtc::DegradationPreference::DISABLED); |
asapersson | f4e44af | 2017-04-19 02:01:06 -0700 | [diff] [blame] | 2333 | |
| 2334 | for (int i = 1; i <= SendStatisticsProxy::kMinRequiredMetricsSamples; ++i) { |
| 2335 | video_source_.IncomingCapturedFrame(CreateFrame(i, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2336 | WaitForEncodedFrame(i); |
asapersson | f4e44af | 2017-04-19 02:01:06 -0700 | [diff] [blame] | 2337 | } |
| 2338 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2339 | video_stream_encoder_->Stop(); |
| 2340 | video_stream_encoder_.reset(); |
asapersson | f4e44af | 2017-04-19 02:01:06 -0700 | [diff] [blame] | 2341 | stats_proxy_.reset(); |
| 2342 | |
| 2343 | EXPECT_EQ(0, |
| 2344 | metrics::NumSamples("WebRTC.Video.CpuLimitedResolutionInPercent")); |
| 2345 | } |
| 2346 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2347 | TEST_F(VideoStreamEncoderTest, CallsBitrateObserver) { |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2348 | MockBitrateObserver bitrate_observer; |
Niels Möller | 0327c2d | 2018-05-21 14:09:31 +0200 | [diff] [blame] | 2349 | video_stream_encoder_->SetBitrateAllocationObserver(&bitrate_observer); |
sprang | 57c2fff | 2017-01-16 06:24:02 -0800 | [diff] [blame] | 2350 | |
| 2351 | const int kDefaultFps = 30; |
Erik Språng | 566124a | 2018-04-23 12:32:22 +0200 | [diff] [blame] | 2352 | const VideoBitrateAllocation expected_bitrate = |
sprang | 57c2fff | 2017-01-16 06:24:02 -0800 | [diff] [blame] | 2353 | DefaultVideoBitrateAllocator(fake_encoder_.codec_config()) |
kthelgason | 2bc6864 | 2017-02-07 07:02:22 -0800 | [diff] [blame] | 2354 | .GetAllocation(kLowTargetBitrateBps, kDefaultFps); |
sprang | 57c2fff | 2017-01-16 06:24:02 -0800 | [diff] [blame] | 2355 | |
sprang | 57c2fff | 2017-01-16 06:24:02 -0800 | [diff] [blame] | 2356 | EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(expected_bitrate)) |
Niels Möller | 6bb5ab9 | 2019-01-11 11:11:10 +0100 | [diff] [blame] | 2357 | .Times(1); |
Erik Språng | 610c763 | 2019-03-06 15:37:33 +0100 | [diff] [blame] | 2358 | video_stream_encoder_->OnBitrateUpdated(DataRate::bps(kLowTargetBitrateBps), |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 2359 | DataRate::bps(kLowTargetBitrateBps), |
| 2360 | 0, 0); |
sprang | 57c2fff | 2017-01-16 06:24:02 -0800 | [diff] [blame] | 2361 | |
sprang | 57c2fff | 2017-01-16 06:24:02 -0800 | [diff] [blame] | 2362 | video_source_.IncomingCapturedFrame( |
Erik Språng | d7329ca | 2019-02-21 21:19:53 +0100 | [diff] [blame] | 2363 | CreateFrame(rtc::TimeMillis(), codec_width_, codec_height_)); |
| 2364 | WaitForEncodedFrame(rtc::TimeMillis()); |
| 2365 | absl::optional<VideoBitrateAllocation> bitrate_allocation = |
| 2366 | fake_encoder_.GetAndResetLastBitrateAllocation(); |
| 2367 | // Check that encoder has been updated too, not just allocation observer. |
| 2368 | EXPECT_EQ(bitrate_allocation->get_sum_bps(), kLowTargetBitrateBps); |
Sebastian Jansson | 40889f3 | 2019-04-17 12:11:20 +0200 | [diff] [blame] | 2369 | // TODO(srte): The use of millisecs here looks like an error, but the tests |
| 2370 | // fails using seconds, this should be investigated. |
| 2371 | fake_clock_.AdvanceTime(TimeDelta::ms(1) / kDefaultFps); |
sprang | 57c2fff | 2017-01-16 06:24:02 -0800 | [diff] [blame] | 2372 | |
| 2373 | // Not called on second frame. |
| 2374 | EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(expected_bitrate)) |
| 2375 | .Times(0); |
| 2376 | video_source_.IncomingCapturedFrame( |
Erik Språng | d7329ca | 2019-02-21 21:19:53 +0100 | [diff] [blame] | 2377 | CreateFrame(rtc::TimeMillis(), codec_width_, codec_height_)); |
| 2378 | WaitForEncodedFrame(rtc::TimeMillis()); |
Sebastian Jansson | 40889f3 | 2019-04-17 12:11:20 +0200 | [diff] [blame] | 2379 | fake_clock_.AdvanceTime(TimeDelta::ms(1) / kDefaultFps); |
sprang | 57c2fff | 2017-01-16 06:24:02 -0800 | [diff] [blame] | 2380 | |
| 2381 | // Called after a process interval. |
| 2382 | const int64_t kProcessIntervalMs = |
| 2383 | vcm::VCMProcessTimer::kDefaultProcessIntervalMs; |
sprang | 57c2fff | 2017-01-16 06:24:02 -0800 | [diff] [blame] | 2384 | EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(expected_bitrate)) |
| 2385 | .Times(1); |
Erik Språng | d7329ca | 2019-02-21 21:19:53 +0100 | [diff] [blame] | 2386 | const int64_t start_time_ms = rtc::TimeMillis(); |
| 2387 | while (rtc::TimeMillis() - start_time_ms < kProcessIntervalMs) { |
| 2388 | video_source_.IncomingCapturedFrame( |
| 2389 | CreateFrame(rtc::TimeMillis(), codec_width_, codec_height_)); |
| 2390 | WaitForEncodedFrame(rtc::TimeMillis()); |
Sebastian Jansson | 40889f3 | 2019-04-17 12:11:20 +0200 | [diff] [blame] | 2391 | fake_clock_.AdvanceTime(TimeDelta::ms(1) / kDefaultFps); |
Erik Språng | d7329ca | 2019-02-21 21:19:53 +0100 | [diff] [blame] | 2392 | } |
| 2393 | |
| 2394 | // Since rates are unchanged, encoder should not be reconfigured. |
| 2395 | EXPECT_FALSE(fake_encoder_.GetAndResetLastBitrateAllocation().has_value()); |
sprang | 57c2fff | 2017-01-16 06:24:02 -0800 | [diff] [blame] | 2396 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2397 | video_stream_encoder_->Stop(); |
sprang | 57c2fff | 2017-01-16 06:24:02 -0800 | [diff] [blame] | 2398 | } |
| 2399 | |
Åsa Persson | c29cb2c | 2019-03-25 12:06:59 +0100 | [diff] [blame] | 2400 | TEST_F(VideoStreamEncoderTest, TemporalLayersNotDisabledIfSupported) { |
| 2401 | // 2 TLs configured, temporal layers supported by encoder. |
| 2402 | const int kNumTemporalLayers = 2; |
| 2403 | ResetEncoder("VP8", 1, kNumTemporalLayers, 1, /*screenshare*/ false); |
| 2404 | fake_encoder_.SetTemporalLayersSupported(0, true); |
| 2405 | |
| 2406 | // Bitrate allocated across temporal layers. |
| 2407 | const int kTl0Bps = kTargetBitrateBps * |
| 2408 | webrtc::SimulcastRateAllocator::GetTemporalRateAllocation( |
| 2409 | kNumTemporalLayers, /*temporal_id*/ 0); |
| 2410 | const int kTl1Bps = kTargetBitrateBps * |
| 2411 | webrtc::SimulcastRateAllocator::GetTemporalRateAllocation( |
| 2412 | kNumTemporalLayers, /*temporal_id*/ 1); |
| 2413 | VideoBitrateAllocation expected_bitrate; |
| 2414 | expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 0, kTl0Bps); |
| 2415 | expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 1, kTl1Bps - kTl0Bps); |
| 2416 | |
| 2417 | VerifyAllocatedBitrate(expected_bitrate); |
| 2418 | video_stream_encoder_->Stop(); |
| 2419 | } |
| 2420 | |
| 2421 | TEST_F(VideoStreamEncoderTest, TemporalLayersDisabledIfNotSupported) { |
| 2422 | // 2 TLs configured, temporal layers not supported by encoder. |
| 2423 | ResetEncoder("VP8", 1, /*num_temporal_layers*/ 2, 1, /*screenshare*/ false); |
| 2424 | fake_encoder_.SetTemporalLayersSupported(0, false); |
| 2425 | |
| 2426 | // Temporal layers not supported by the encoder. |
| 2427 | // Total bitrate should be at ti:0. |
| 2428 | VideoBitrateAllocation expected_bitrate; |
| 2429 | expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 0, kTargetBitrateBps); |
| 2430 | |
| 2431 | VerifyAllocatedBitrate(expected_bitrate); |
| 2432 | video_stream_encoder_->Stop(); |
| 2433 | } |
| 2434 | |
| 2435 | TEST_F(VideoStreamEncoderTest, VerifyBitrateAllocationForTwoStreams) { |
| 2436 | // 2 TLs configured, temporal layers only supported for first stream. |
| 2437 | ResetEncoder("VP8", 2, /*num_temporal_layers*/ 2, 1, /*screenshare*/ false); |
| 2438 | fake_encoder_.SetTemporalLayersSupported(0, true); |
| 2439 | fake_encoder_.SetTemporalLayersSupported(1, false); |
| 2440 | |
| 2441 | const int kS0Bps = 150000; |
| 2442 | const int kS0Tl0Bps = |
| 2443 | kS0Bps * webrtc::SimulcastRateAllocator::GetTemporalRateAllocation( |
| 2444 | /*num_layers*/ 2, /*temporal_id*/ 0); |
| 2445 | const int kS0Tl1Bps = |
| 2446 | kS0Bps * webrtc::SimulcastRateAllocator::GetTemporalRateAllocation( |
| 2447 | /*num_layers*/ 2, /*temporal_id*/ 1); |
| 2448 | const int kS1Bps = kTargetBitrateBps - kS0Tl1Bps; |
| 2449 | // Temporal layers not supported by si:1. |
| 2450 | VideoBitrateAllocation expected_bitrate; |
| 2451 | expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 0, kS0Tl0Bps); |
| 2452 | expected_bitrate.SetBitrate(/*si*/ 0, /*ti*/ 1, kS0Tl1Bps - kS0Tl0Bps); |
| 2453 | expected_bitrate.SetBitrate(/*si*/ 1, /*ti*/ 0, kS1Bps); |
| 2454 | |
| 2455 | VerifyAllocatedBitrate(expected_bitrate); |
| 2456 | video_stream_encoder_->Stop(); |
| 2457 | } |
| 2458 | |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 2459 | TEST_F(VideoStreamEncoderTest, OveruseDetectorUpdatedOnReconfigureAndAdaption) { |
| 2460 | const int kFrameWidth = 1280; |
| 2461 | const int kFrameHeight = 720; |
| 2462 | const int kFramerate = 24; |
| 2463 | |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 2464 | video_stream_encoder_->OnBitrateUpdated( |
| 2465 | DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps), 0, 0); |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 2466 | test::FrameForwarder source; |
| 2467 | video_stream_encoder_->SetSource( |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 2468 | &source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION); |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 2469 | |
| 2470 | // Insert a single frame, triggering initial configuration. |
| 2471 | source.IncomingCapturedFrame(CreateFrame(1, kFrameWidth, kFrameHeight)); |
| 2472 | video_stream_encoder_->WaitUntilTaskQueueIsIdle(); |
| 2473 | |
| 2474 | EXPECT_EQ( |
| 2475 | video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(), |
| 2476 | kDefaultFramerate); |
| 2477 | |
| 2478 | // Trigger reconfigure encoder (without resetting the entire instance). |
| 2479 | VideoEncoderConfig video_encoder_config; |
Niels Möller | 259a497 | 2018-04-05 15:36:51 +0200 | [diff] [blame] | 2480 | video_encoder_config.codec_type = kVideoCodecVP8; |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 2481 | video_encoder_config.max_bitrate_bps = kTargetBitrateBps; |
| 2482 | video_encoder_config.number_of_streams = 1; |
| 2483 | video_encoder_config.video_stream_factory = |
| 2484 | new rtc::RefCountedObject<VideoStreamFactory>(1, kFramerate); |
| 2485 | video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config), |
Niels Möller | f133856 | 2018-04-26 09:51:47 +0200 | [diff] [blame] | 2486 | kMaxPayloadLength); |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 2487 | video_stream_encoder_->WaitUntilTaskQueueIsIdle(); |
| 2488 | |
| 2489 | // Detector should be updated with fps limit from codec config. |
| 2490 | EXPECT_EQ( |
| 2491 | video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(), |
| 2492 | kFramerate); |
| 2493 | |
| 2494 | // Trigger overuse, max framerate should be reduced. |
| 2495 | VideoSendStream::Stats stats = stats_proxy_->GetStats(); |
| 2496 | stats.input_frame_rate = kFramerate; |
| 2497 | stats_proxy_->SetMockStats(stats); |
| 2498 | video_stream_encoder_->TriggerCpuOveruse(); |
| 2499 | video_stream_encoder_->WaitUntilTaskQueueIsIdle(); |
| 2500 | int adapted_framerate = |
| 2501 | video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(); |
| 2502 | EXPECT_LT(adapted_framerate, kFramerate); |
| 2503 | |
| 2504 | // Trigger underuse, max framerate should go back to codec configured fps. |
| 2505 | // Set extra low fps, to make sure it's actually reset, not just incremented. |
| 2506 | stats = stats_proxy_->GetStats(); |
| 2507 | stats.input_frame_rate = adapted_framerate / 2; |
| 2508 | stats_proxy_->SetMockStats(stats); |
| 2509 | video_stream_encoder_->TriggerCpuNormalUsage(); |
| 2510 | video_stream_encoder_->WaitUntilTaskQueueIsIdle(); |
| 2511 | EXPECT_EQ( |
| 2512 | video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(), |
| 2513 | kFramerate); |
| 2514 | |
| 2515 | video_stream_encoder_->Stop(); |
| 2516 | } |
| 2517 | |
| 2518 | TEST_F(VideoStreamEncoderTest, |
| 2519 | OveruseDetectorUpdatedRespectsFramerateAfterUnderuse) { |
| 2520 | const int kFrameWidth = 1280; |
| 2521 | const int kFrameHeight = 720; |
| 2522 | const int kLowFramerate = 15; |
| 2523 | const int kHighFramerate = 25; |
| 2524 | |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 2525 | video_stream_encoder_->OnBitrateUpdated( |
| 2526 | DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps), 0, 0); |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 2527 | test::FrameForwarder source; |
| 2528 | video_stream_encoder_->SetSource( |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 2529 | &source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION); |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 2530 | |
| 2531 | // Trigger initial configuration. |
| 2532 | VideoEncoderConfig video_encoder_config; |
Niels Möller | 259a497 | 2018-04-05 15:36:51 +0200 | [diff] [blame] | 2533 | video_encoder_config.codec_type = kVideoCodecVP8; |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 2534 | video_encoder_config.max_bitrate_bps = kTargetBitrateBps; |
| 2535 | video_encoder_config.number_of_streams = 1; |
| 2536 | video_encoder_config.video_stream_factory = |
| 2537 | new rtc::RefCountedObject<VideoStreamFactory>(1, kLowFramerate); |
| 2538 | source.IncomingCapturedFrame(CreateFrame(1, kFrameWidth, kFrameHeight)); |
| 2539 | video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config), |
Niels Möller | f133856 | 2018-04-26 09:51:47 +0200 | [diff] [blame] | 2540 | kMaxPayloadLength); |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 2541 | video_stream_encoder_->WaitUntilTaskQueueIsIdle(); |
| 2542 | |
| 2543 | EXPECT_EQ( |
| 2544 | video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(), |
| 2545 | kLowFramerate); |
| 2546 | |
| 2547 | // Trigger overuse, max framerate should be reduced. |
| 2548 | VideoSendStream::Stats stats = stats_proxy_->GetStats(); |
| 2549 | stats.input_frame_rate = kLowFramerate; |
| 2550 | stats_proxy_->SetMockStats(stats); |
| 2551 | video_stream_encoder_->TriggerCpuOveruse(); |
| 2552 | video_stream_encoder_->WaitUntilTaskQueueIsIdle(); |
| 2553 | int adapted_framerate = |
| 2554 | video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(); |
| 2555 | EXPECT_LT(adapted_framerate, kLowFramerate); |
| 2556 | |
| 2557 | // Reconfigure the encoder with a new (higher max framerate), max fps should |
| 2558 | // still respect the adaptation. |
| 2559 | video_encoder_config.video_stream_factory = |
| 2560 | new rtc::RefCountedObject<VideoStreamFactory>(1, kHighFramerate); |
| 2561 | source.IncomingCapturedFrame(CreateFrame(1, kFrameWidth, kFrameHeight)); |
| 2562 | video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config), |
Niels Möller | f133856 | 2018-04-26 09:51:47 +0200 | [diff] [blame] | 2563 | kMaxPayloadLength); |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 2564 | video_stream_encoder_->WaitUntilTaskQueueIsIdle(); |
| 2565 | |
| 2566 | EXPECT_EQ( |
| 2567 | video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(), |
| 2568 | adapted_framerate); |
| 2569 | |
| 2570 | // Trigger underuse, max framerate should go back to codec configured fps. |
| 2571 | stats = stats_proxy_->GetStats(); |
| 2572 | stats.input_frame_rate = adapted_framerate; |
| 2573 | stats_proxy_->SetMockStats(stats); |
| 2574 | video_stream_encoder_->TriggerCpuNormalUsage(); |
| 2575 | video_stream_encoder_->WaitUntilTaskQueueIsIdle(); |
| 2576 | EXPECT_EQ( |
| 2577 | video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(), |
| 2578 | kHighFramerate); |
| 2579 | |
| 2580 | video_stream_encoder_->Stop(); |
| 2581 | } |
| 2582 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2583 | TEST_F(VideoStreamEncoderTest, |
| 2584 | OveruseDetectorUpdatedOnDegradationPreferenceChange) { |
sprang | fda496a | 2017-06-15 04:21:07 -0700 | [diff] [blame] | 2585 | const int kFrameWidth = 1280; |
| 2586 | const int kFrameHeight = 720; |
| 2587 | const int kFramerate = 24; |
| 2588 | |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 2589 | video_stream_encoder_->OnBitrateUpdated( |
| 2590 | DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps), 0, 0); |
sprang | fda496a | 2017-06-15 04:21:07 -0700 | [diff] [blame] | 2591 | test::FrameForwarder source; |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2592 | video_stream_encoder_->SetSource( |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 2593 | &source, webrtc::DegradationPreference::MAINTAIN_RESOLUTION); |
sprang | fda496a | 2017-06-15 04:21:07 -0700 | [diff] [blame] | 2594 | |
| 2595 | // Trigger initial configuration. |
| 2596 | VideoEncoderConfig video_encoder_config; |
Niels Möller | 259a497 | 2018-04-05 15:36:51 +0200 | [diff] [blame] | 2597 | video_encoder_config.codec_type = kVideoCodecVP8; |
sprang | fda496a | 2017-06-15 04:21:07 -0700 | [diff] [blame] | 2598 | video_encoder_config.max_bitrate_bps = kTargetBitrateBps; |
| 2599 | video_encoder_config.number_of_streams = 1; |
| 2600 | video_encoder_config.video_stream_factory = |
| 2601 | new rtc::RefCountedObject<VideoStreamFactory>(1, kFramerate); |
| 2602 | source.IncomingCapturedFrame(CreateFrame(1, kFrameWidth, kFrameHeight)); |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2603 | video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config), |
Niels Möller | f133856 | 2018-04-26 09:51:47 +0200 | [diff] [blame] | 2604 | kMaxPayloadLength); |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2605 | video_stream_encoder_->WaitUntilTaskQueueIsIdle(); |
sprang | fda496a | 2017-06-15 04:21:07 -0700 | [diff] [blame] | 2606 | |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 2607 | EXPECT_EQ( |
| 2608 | video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(), |
| 2609 | kFramerate); |
sprang | fda496a | 2017-06-15 04:21:07 -0700 | [diff] [blame] | 2610 | |
| 2611 | // Trigger overuse, max framerate should be reduced. |
| 2612 | VideoSendStream::Stats stats = stats_proxy_->GetStats(); |
| 2613 | stats.input_frame_rate = kFramerate; |
| 2614 | stats_proxy_->SetMockStats(stats); |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2615 | video_stream_encoder_->TriggerCpuOveruse(); |
| 2616 | video_stream_encoder_->WaitUntilTaskQueueIsIdle(); |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 2617 | int adapted_framerate = |
| 2618 | video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(); |
sprang | fda496a | 2017-06-15 04:21:07 -0700 | [diff] [blame] | 2619 | EXPECT_LT(adapted_framerate, kFramerate); |
| 2620 | |
| 2621 | // Change degradation preference to not enable framerate scaling. Target |
| 2622 | // framerate should be changed to codec defined limit. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2623 | video_stream_encoder_->SetSource( |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 2624 | &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE); |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2625 | video_stream_encoder_->WaitUntilTaskQueueIsIdle(); |
Niels Möller | 7dc26b7 | 2017-12-06 10:27:48 +0100 | [diff] [blame] | 2626 | EXPECT_EQ( |
| 2627 | video_stream_encoder_->overuse_detector_proxy_->GetLastTargetFramerate(), |
| 2628 | kFramerate); |
sprang | fda496a | 2017-06-15 04:21:07 -0700 | [diff] [blame] | 2629 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2630 | video_stream_encoder_->Stop(); |
sprang | fda496a | 2017-06-15 04:21:07 -0700 | [diff] [blame] | 2631 | } |
| 2632 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2633 | TEST_F(VideoStreamEncoderTest, DropsFramesAndScalesWhenBitrateIsTooLow) { |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 2634 | const int kTooLowBitrateForFrameSizeBps = 10000; |
Erik Språng | 610c763 | 2019-03-06 15:37:33 +0100 | [diff] [blame] | 2635 | video_stream_encoder_->OnBitrateUpdated( |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 2636 | DataRate::bps(kTooLowBitrateForFrameSizeBps), |
| 2637 | DataRate::bps(kTooLowBitrateForFrameSizeBps), 0, 0); |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 2638 | const int kWidth = 640; |
| 2639 | const int kHeight = 360; |
kthelgason | 2bc6864 | 2017-02-07 07:02:22 -0800 | [diff] [blame] | 2640 | |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 2641 | video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight)); |
kthelgason | 2bc6864 | 2017-02-07 07:02:22 -0800 | [diff] [blame] | 2642 | |
| 2643 | // Expect to drop this frame, the wait should time out. |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2644 | ExpectDroppedFrame(); |
kthelgason | 2bc6864 | 2017-02-07 07:02:22 -0800 | [diff] [blame] | 2645 | |
| 2646 | // Expect the sink_wants to specify a scaled frame. |
asapersson | 0944a80 | 2017-04-07 00:57:58 -0700 | [diff] [blame] | 2647 | EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight); |
kthelgason | 2bc6864 | 2017-02-07 07:02:22 -0800 | [diff] [blame] | 2648 | |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2649 | int last_pixel_count = video_source_.sink_wants().max_pixel_count; |
kthelgason | 2bc6864 | 2017-02-07 07:02:22 -0800 | [diff] [blame] | 2650 | |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 2651 | // Next frame is scaled. |
kthelgason | 2bc6864 | 2017-02-07 07:02:22 -0800 | [diff] [blame] | 2652 | video_source_.IncomingCapturedFrame( |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 2653 | CreateFrame(2, kWidth * 3 / 4, kHeight * 3 / 4)); |
kthelgason | 2bc6864 | 2017-02-07 07:02:22 -0800 | [diff] [blame] | 2654 | |
| 2655 | // Expect to drop this frame, the wait should time out. |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2656 | ExpectDroppedFrame(); |
kthelgason | 2bc6864 | 2017-02-07 07:02:22 -0800 | [diff] [blame] | 2657 | |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2658 | EXPECT_LT(video_source_.sink_wants().max_pixel_count, last_pixel_count); |
kthelgason | 2bc6864 | 2017-02-07 07:02:22 -0800 | [diff] [blame] | 2659 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2660 | video_stream_encoder_->Stop(); |
kthelgason | 2bc6864 | 2017-02-07 07:02:22 -0800 | [diff] [blame] | 2661 | } |
| 2662 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2663 | TEST_F(VideoStreamEncoderTest, |
| 2664 | NumberOfDroppedFramesLimitedWhenBitrateIsTooLow) { |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 2665 | const int kTooLowBitrateForFrameSizeBps = 10000; |
Erik Språng | 610c763 | 2019-03-06 15:37:33 +0100 | [diff] [blame] | 2666 | video_stream_encoder_->OnBitrateUpdated( |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 2667 | DataRate::bps(kTooLowBitrateForFrameSizeBps), |
| 2668 | DataRate::bps(kTooLowBitrateForFrameSizeBps), 0, 0); |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 2669 | const int kWidth = 640; |
| 2670 | const int kHeight = 360; |
kthelgason | 2bc6864 | 2017-02-07 07:02:22 -0800 | [diff] [blame] | 2671 | |
| 2672 | // We expect the n initial frames to get dropped. |
| 2673 | int i; |
| 2674 | for (i = 1; i <= kMaxInitialFramedrop; ++i) { |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 2675 | video_source_.IncomingCapturedFrame(CreateFrame(i, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2676 | ExpectDroppedFrame(); |
kthelgason | 2bc6864 | 2017-02-07 07:02:22 -0800 | [diff] [blame] | 2677 | } |
| 2678 | // The n+1th frame should not be dropped, even though it's size is too large. |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 2679 | video_source_.IncomingCapturedFrame(CreateFrame(i, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2680 | WaitForEncodedFrame(i); |
kthelgason | 2bc6864 | 2017-02-07 07:02:22 -0800 | [diff] [blame] | 2681 | |
| 2682 | // Expect the sink_wants to specify a scaled frame. |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 2683 | EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight); |
kthelgason | 2bc6864 | 2017-02-07 07:02:22 -0800 | [diff] [blame] | 2684 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2685 | video_stream_encoder_->Stop(); |
kthelgason | 2bc6864 | 2017-02-07 07:02:22 -0800 | [diff] [blame] | 2686 | } |
| 2687 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2688 | TEST_F(VideoStreamEncoderTest, |
| 2689 | InitialFrameDropOffWithMaintainResolutionPreference) { |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 2690 | const int kWidth = 640; |
| 2691 | const int kHeight = 360; |
Erik Språng | 610c763 | 2019-03-06 15:37:33 +0100 | [diff] [blame] | 2692 | video_stream_encoder_->OnBitrateUpdated(DataRate::bps(kLowTargetBitrateBps), |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 2693 | DataRate::bps(kLowTargetBitrateBps), |
| 2694 | 0, 0); |
kthelgason | 2bc6864 | 2017-02-07 07:02:22 -0800 | [diff] [blame] | 2695 | |
| 2696 | // Set degradation preference. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2697 | video_stream_encoder_->SetSource( |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 2698 | &video_source_, webrtc::DegradationPreference::MAINTAIN_RESOLUTION); |
kthelgason | 2bc6864 | 2017-02-07 07:02:22 -0800 | [diff] [blame] | 2699 | |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 2700 | video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight)); |
kthelgason | 2bc6864 | 2017-02-07 07:02:22 -0800 | [diff] [blame] | 2701 | // Frame should not be dropped, even if it's too large. |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2702 | WaitForEncodedFrame(1); |
kthelgason | 2bc6864 | 2017-02-07 07:02:22 -0800 | [diff] [blame] | 2703 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2704 | video_stream_encoder_->Stop(); |
kthelgason | 2bc6864 | 2017-02-07 07:02:22 -0800 | [diff] [blame] | 2705 | } |
| 2706 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2707 | TEST_F(VideoStreamEncoderTest, InitialFrameDropOffWhenEncoderDisabledScaling) { |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 2708 | const int kWidth = 640; |
| 2709 | const int kHeight = 360; |
kthelgason | ad9010c | 2017-02-14 00:46:51 -0800 | [diff] [blame] | 2710 | fake_encoder_.SetQualityScaling(false); |
Niels Möller | 4db138e | 2018-04-19 09:04:13 +0200 | [diff] [blame] | 2711 | |
| 2712 | VideoEncoderConfig video_encoder_config; |
| 2713 | test::FillEncoderConfiguration(kVideoCodecVP8, 1, &video_encoder_config); |
| 2714 | // Make format different, to force recreation of encoder. |
| 2715 | video_encoder_config.video_format.parameters["foo"] = "foo"; |
| 2716 | video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config), |
Niels Möller | f133856 | 2018-04-26 09:51:47 +0200 | [diff] [blame] | 2717 | kMaxPayloadLength); |
Erik Språng | 610c763 | 2019-03-06 15:37:33 +0100 | [diff] [blame] | 2718 | video_stream_encoder_->OnBitrateUpdated(DataRate::bps(kLowTargetBitrateBps), |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 2719 | DataRate::bps(kLowTargetBitrateBps), |
| 2720 | 0, 0); |
asapersson | 09f0561 | 2017-05-15 23:40:18 -0700 | [diff] [blame] | 2721 | |
kthelgason | b83797b | 2017-02-14 11:57:25 -0800 | [diff] [blame] | 2722 | // Force quality scaler reconfiguration by resetting the source. |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 2723 | video_stream_encoder_->SetSource(&video_source_, |
| 2724 | webrtc::DegradationPreference::BALANCED); |
kthelgason | ad9010c | 2017-02-14 00:46:51 -0800 | [diff] [blame] | 2725 | |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 2726 | video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight)); |
kthelgason | ad9010c | 2017-02-14 00:46:51 -0800 | [diff] [blame] | 2727 | // Frame should not be dropped, even if it's too large. |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2728 | WaitForEncodedFrame(1); |
kthelgason | ad9010c | 2017-02-14 00:46:51 -0800 | [diff] [blame] | 2729 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2730 | video_stream_encoder_->Stop(); |
kthelgason | ad9010c | 2017-02-14 00:46:51 -0800 | [diff] [blame] | 2731 | fake_encoder_.SetQualityScaling(true); |
| 2732 | } |
| 2733 | |
Kári Tristan Helgason | 639602a | 2018-08-02 10:51:40 +0200 | [diff] [blame] | 2734 | TEST_F(VideoStreamEncoderTest, InitialFrameDropActivatesWhenBWEstimateReady) { |
| 2735 | webrtc::test::ScopedFieldTrials field_trials( |
| 2736 | "WebRTC-InitialFramedrop/Enabled/"); |
| 2737 | // Reset encoder for field trials to take effect. |
| 2738 | ConfigureEncoder(video_encoder_config_.Copy()); |
| 2739 | const int kTooLowBitrateForFrameSizeBps = 10000; |
| 2740 | const int kWidth = 640; |
| 2741 | const int kHeight = 360; |
| 2742 | |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 2743 | video_stream_encoder_->OnBitrateUpdated( |
| 2744 | DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps), 0, 0); |
Kári Tristan Helgason | 639602a | 2018-08-02 10:51:40 +0200 | [diff] [blame] | 2745 | video_source_.IncomingCapturedFrame(CreateFrame(1, kWidth, kHeight)); |
| 2746 | // Frame should not be dropped. |
| 2747 | WaitForEncodedFrame(1); |
| 2748 | |
Erik Språng | 610c763 | 2019-03-06 15:37:33 +0100 | [diff] [blame] | 2749 | video_stream_encoder_->OnBitrateUpdated( |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 2750 | DataRate::bps(kTooLowBitrateForFrameSizeBps), |
| 2751 | DataRate::bps(kTooLowBitrateForFrameSizeBps), 0, 0); |
Kári Tristan Helgason | 639602a | 2018-08-02 10:51:40 +0200 | [diff] [blame] | 2752 | video_source_.IncomingCapturedFrame(CreateFrame(2, kWidth, kHeight)); |
| 2753 | // Expect to drop this frame, the wait should time out. |
| 2754 | ExpectDroppedFrame(); |
| 2755 | |
| 2756 | // Expect the sink_wants to specify a scaled frame. |
| 2757 | EXPECT_LT(video_source_.sink_wants().max_pixel_count, kWidth * kHeight); |
| 2758 | video_stream_encoder_->Stop(); |
| 2759 | } |
| 2760 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2761 | TEST_F(VideoStreamEncoderTest, |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 2762 | ResolutionNotAdaptedForTooSmallFrame_MaintainFramerateMode) { |
| 2763 | const int kTooSmallWidth = 10; |
| 2764 | const int kTooSmallHeight = 10; |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 2765 | video_stream_encoder_->OnBitrateUpdated( |
| 2766 | DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps), 0, 0); |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 2767 | |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 2768 | // Enable MAINTAIN_FRAMERATE preference, no initial limitation. |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 2769 | test::FrameForwarder source; |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2770 | video_stream_encoder_->SetSource( |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 2771 | &source, webrtc::DegradationPreference::MAINTAIN_FRAMERATE); |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 2772 | VerifyNoLimitation(source.sink_wants()); |
| 2773 | EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution); |
| 2774 | |
| 2775 | // Trigger adapt down, too small frame, expect no change. |
| 2776 | source.IncomingCapturedFrame(CreateFrame(1, kTooSmallWidth, kTooSmallHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2777 | WaitForEncodedFrame(1); |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2778 | video_stream_encoder_->TriggerCpuOveruse(); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 2779 | VerifyFpsMaxResolutionMax(source.sink_wants()); |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 2780 | EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution); |
| 2781 | EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
| 2782 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2783 | video_stream_encoder_->Stop(); |
asapersson | d0de295 | 2017-04-21 01:47:31 -0700 | [diff] [blame] | 2784 | } |
| 2785 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2786 | TEST_F(VideoStreamEncoderTest, |
| 2787 | ResolutionNotAdaptedForTooSmallFrame_BalancedMode) { |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2788 | const int kTooSmallWidth = 10; |
| 2789 | const int kTooSmallHeight = 10; |
| 2790 | const int kFpsLimit = 7; |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 2791 | video_stream_encoder_->OnBitrateUpdated( |
| 2792 | DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps), 0, 0); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2793 | |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 2794 | // Enable BALANCED preference, no initial limitation. |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2795 | test::FrameForwarder source; |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 2796 | video_stream_encoder_->SetSource(&source, |
| 2797 | webrtc::DegradationPreference::BALANCED); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2798 | VerifyNoLimitation(source.sink_wants()); |
| 2799 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution); |
| 2800 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate); |
| 2801 | |
| 2802 | // Trigger adapt down, expect limited framerate. |
| 2803 | source.IncomingCapturedFrame(CreateFrame(1, kTooSmallWidth, kTooSmallHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2804 | WaitForEncodedFrame(1); |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2805 | video_stream_encoder_->TriggerQualityLow(); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2806 | VerifyFpsEqResolutionMax(source.sink_wants(), kFpsLimit); |
| 2807 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution); |
| 2808 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate); |
| 2809 | EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 2810 | |
| 2811 | // Trigger adapt down, too small frame, expect no change. |
| 2812 | source.IncomingCapturedFrame(CreateFrame(2, kTooSmallWidth, kTooSmallHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2813 | WaitForEncodedFrame(2); |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2814 | video_stream_encoder_->TriggerQualityLow(); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2815 | VerifyFpsEqResolutionMax(source.sink_wants(), kFpsLimit); |
| 2816 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution); |
| 2817 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate); |
| 2818 | EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 2819 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2820 | video_stream_encoder_->Stop(); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 2821 | } |
| 2822 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2823 | TEST_F(VideoStreamEncoderTest, FailingInitEncodeDoesntCauseCrash) { |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 2824 | fake_encoder_.ForceInitEncodeFailure(true); |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 2825 | video_stream_encoder_->OnBitrateUpdated( |
| 2826 | DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps), 0, 0); |
Niels Möller | f133856 | 2018-04-26 09:51:47 +0200 | [diff] [blame] | 2827 | ResetEncoder("VP8", 2, 1, 1, false); |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 2828 | const int kFrameWidth = 1280; |
| 2829 | const int kFrameHeight = 720; |
| 2830 | video_source_.IncomingCapturedFrame( |
| 2831 | CreateFrame(1, kFrameWidth, kFrameHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2832 | ExpectDroppedFrame(); |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2833 | video_stream_encoder_->Stop(); |
asapersson | 02465b8 | 2017-04-10 01:12:52 -0700 | [diff] [blame] | 2834 | } |
| 2835 | |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 2836 | // TODO(sprang): Extend this with fps throttling and any "balanced" extensions. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2837 | TEST_F(VideoStreamEncoderTest, |
| 2838 | AdaptsResolutionOnOveruse_MaintainFramerateMode) { |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 2839 | video_stream_encoder_->OnBitrateUpdated( |
| 2840 | DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps), 0, 0); |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 2841 | |
| 2842 | const int kFrameWidth = 1280; |
| 2843 | const int kFrameHeight = 720; |
| 2844 | // Enabled default VideoAdapter downscaling. First step is 3/4, not 3/5 as |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2845 | // requested by |
| 2846 | // VideoStreamEncoder::VideoSourceProxy::RequestResolutionLowerThan(). |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 2847 | video_source_.set_adaptation_enabled(true); |
| 2848 | |
| 2849 | video_source_.IncomingCapturedFrame( |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 2850 | CreateFrame(1 * kFrameIntervalMs, kFrameWidth, kFrameHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2851 | WaitForEncodedFrame(kFrameWidth, kFrameHeight); |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 2852 | |
| 2853 | // Trigger CPU overuse, downscale by 3/4. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2854 | video_stream_encoder_->TriggerCpuOveruse(); |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 2855 | video_source_.IncomingCapturedFrame( |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 2856 | CreateFrame(2 * kFrameIntervalMs, kFrameWidth, kFrameHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2857 | WaitForEncodedFrame((kFrameWidth * 3) / 4, (kFrameHeight * 3) / 4); |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 2858 | |
asapersson | fab6707 | 2017-04-04 05:51:49 -0700 | [diff] [blame] | 2859 | // Trigger CPU normal use, return to original resolution. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2860 | video_stream_encoder_->TriggerCpuNormalUsage(); |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 2861 | video_source_.IncomingCapturedFrame( |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 2862 | CreateFrame(3 * kFrameIntervalMs, kFrameWidth, kFrameHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2863 | WaitForEncodedFrame(kFrameWidth, kFrameHeight); |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 2864 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2865 | video_stream_encoder_->Stop(); |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 2866 | } |
sprang | fe627f3 | 2017-03-29 08:24:59 -0700 | [diff] [blame] | 2867 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2868 | TEST_F(VideoStreamEncoderTest, |
| 2869 | AdaptsFramerateOnOveruse_MaintainResolutionMode) { |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2870 | const int kFrameWidth = 1280; |
| 2871 | const int kFrameHeight = 720; |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2872 | |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 2873 | video_stream_encoder_->OnBitrateUpdated( |
| 2874 | DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps), 0, 0); |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2875 | video_stream_encoder_->SetSource( |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 2876 | &video_source_, webrtc::DegradationPreference::MAINTAIN_RESOLUTION); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2877 | video_source_.set_adaptation_enabled(true); |
| 2878 | |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2879 | int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec; |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2880 | |
| 2881 | video_source_.IncomingCapturedFrame( |
| 2882 | CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2883 | WaitForEncodedFrame(timestamp_ms); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2884 | |
| 2885 | // Try to trigger overuse. No fps estimate available => no effect. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2886 | video_stream_encoder_->TriggerCpuOveruse(); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2887 | |
| 2888 | // Insert frames for one second to get a stable estimate. |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2889 | for (int i = 0; i < max_framerate_; ++i) { |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2890 | timestamp_ms += kFrameIntervalMs; |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2891 | video_source_.IncomingCapturedFrame( |
| 2892 | CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2893 | WaitForEncodedFrame(timestamp_ms); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2894 | } |
| 2895 | |
| 2896 | // Trigger CPU overuse, reduce framerate by 2/3. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2897 | video_stream_encoder_->TriggerCpuOveruse(); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2898 | int num_frames_dropped = 0; |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2899 | for (int i = 0; i < max_framerate_; ++i) { |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2900 | timestamp_ms += kFrameIntervalMs; |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2901 | video_source_.IncomingCapturedFrame( |
| 2902 | CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2903 | if (!WaitForFrame(kFrameTimeoutMs)) { |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2904 | ++num_frames_dropped; |
| 2905 | } else { |
Åsa Persson | c74d8da | 2017-12-04 14:13:56 +0100 | [diff] [blame] | 2906 | sink_.CheckLastFrameSizeMatches(kFrameWidth, kFrameHeight); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2907 | } |
| 2908 | } |
| 2909 | |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2910 | // Add some slack to account for frames dropped by the frame dropper. |
| 2911 | const int kErrorMargin = 1; |
| 2912 | EXPECT_NEAR(num_frames_dropped, max_framerate_ - (max_framerate_ * 2 / 3), |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2913 | kErrorMargin); |
| 2914 | |
| 2915 | // Trigger CPU overuse, reduce framerate by 2/3 again. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2916 | video_stream_encoder_->TriggerCpuOveruse(); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2917 | num_frames_dropped = 0; |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 2918 | for (int i = 0; i <= max_framerate_; ++i) { |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2919 | timestamp_ms += kFrameIntervalMs; |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2920 | video_source_.IncomingCapturedFrame( |
| 2921 | CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2922 | if (!WaitForFrame(kFrameTimeoutMs)) { |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2923 | ++num_frames_dropped; |
| 2924 | } else { |
Åsa Persson | c74d8da | 2017-12-04 14:13:56 +0100 | [diff] [blame] | 2925 | sink_.CheckLastFrameSizeMatches(kFrameWidth, kFrameHeight); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2926 | } |
| 2927 | } |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2928 | EXPECT_NEAR(num_frames_dropped, max_framerate_ - (max_framerate_ * 4 / 9), |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2929 | kErrorMargin); |
| 2930 | |
| 2931 | // Go back up one step. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2932 | video_stream_encoder_->TriggerCpuNormalUsage(); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2933 | num_frames_dropped = 0; |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2934 | for (int i = 0; i < max_framerate_; ++i) { |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2935 | timestamp_ms += kFrameIntervalMs; |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2936 | video_source_.IncomingCapturedFrame( |
| 2937 | CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2938 | if (!WaitForFrame(kFrameTimeoutMs)) { |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2939 | ++num_frames_dropped; |
| 2940 | } else { |
Åsa Persson | c74d8da | 2017-12-04 14:13:56 +0100 | [diff] [blame] | 2941 | sink_.CheckLastFrameSizeMatches(kFrameWidth, kFrameHeight); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2942 | } |
| 2943 | } |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2944 | EXPECT_NEAR(num_frames_dropped, max_framerate_ - (max_framerate_ * 2 / 3), |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2945 | kErrorMargin); |
| 2946 | |
| 2947 | // Go back up to original mode. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2948 | video_stream_encoder_->TriggerCpuNormalUsage(); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2949 | num_frames_dropped = 0; |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2950 | for (int i = 0; i < max_framerate_; ++i) { |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2951 | timestamp_ms += kFrameIntervalMs; |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2952 | video_source_.IncomingCapturedFrame( |
| 2953 | CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2954 | if (!WaitForFrame(kFrameTimeoutMs)) { |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2955 | ++num_frames_dropped; |
| 2956 | } else { |
Åsa Persson | c74d8da | 2017-12-04 14:13:56 +0100 | [diff] [blame] | 2957 | sink_.CheckLastFrameSizeMatches(kFrameWidth, kFrameHeight); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2958 | } |
| 2959 | } |
| 2960 | EXPECT_NEAR(num_frames_dropped, 0, kErrorMargin); |
| 2961 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2962 | video_stream_encoder_->Stop(); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2963 | } |
| 2964 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2965 | TEST_F(VideoStreamEncoderTest, DoesntAdaptDownPastMinFramerate) { |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2966 | const int kFramerateFps = 5; |
| 2967 | const int kFrameIntervalMs = rtc::kNumMillisecsPerSec / kFramerateFps; |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2968 | const int kFrameWidth = 1280; |
| 2969 | const int kFrameHeight = 720; |
| 2970 | |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2971 | // Reconfigure encoder with two temporal layers and screensharing, which will |
| 2972 | // disable frame dropping and make testing easier. |
Niels Möller | f133856 | 2018-04-26 09:51:47 +0200 | [diff] [blame] | 2973 | ResetEncoder("VP8", 1, 2, 1, true); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2974 | |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 2975 | video_stream_encoder_->OnBitrateUpdated( |
| 2976 | DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps), 0, 0); |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2977 | video_stream_encoder_->SetSource( |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 2978 | &video_source_, webrtc::DegradationPreference::MAINTAIN_RESOLUTION); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2979 | video_source_.set_adaptation_enabled(true); |
| 2980 | |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 2981 | int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec; |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2982 | |
| 2983 | // Trigger overuse as much as we can. |
Jonathan Yu | bc771b7 | 2017-12-08 17:04:29 -0800 | [diff] [blame] | 2984 | rtc::VideoSinkWants last_wants; |
| 2985 | do { |
| 2986 | last_wants = video_source_.sink_wants(); |
| 2987 | |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2988 | // Insert frames to get a new fps estimate... |
| 2989 | for (int j = 0; j < kFramerateFps; ++j) { |
| 2990 | video_source_.IncomingCapturedFrame( |
| 2991 | CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight)); |
Jonathan Yu | bc771b7 | 2017-12-08 17:04:29 -0800 | [diff] [blame] | 2992 | if (video_source_.last_sent_width()) { |
| 2993 | sink_.WaitForEncodedFrame(timestamp_ms); |
| 2994 | } |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2995 | timestamp_ms += kFrameIntervalMs; |
Sebastian Jansson | 40889f3 | 2019-04-17 12:11:20 +0200 | [diff] [blame] | 2996 | fake_clock_.AdvanceTime(TimeDelta::ms(kFrameIntervalMs)); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 2997 | } |
| 2998 | // ...and then try to adapt again. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 2999 | video_stream_encoder_->TriggerCpuOveruse(); |
Jonathan Yu | bc771b7 | 2017-12-08 17:04:29 -0800 | [diff] [blame] | 3000 | } while (video_source_.sink_wants().max_framerate_fps < |
| 3001 | last_wants.max_framerate_fps); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 3002 | |
Jonathan Yu | bc771b7 | 2017-12-08 17:04:29 -0800 | [diff] [blame] | 3003 | VerifyFpsEqResolutionMax(video_source_.sink_wants(), kMinFramerateFps); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3004 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 3005 | video_stream_encoder_->Stop(); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 3006 | } |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3007 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 3008 | TEST_F(VideoStreamEncoderTest, |
| 3009 | AdaptsResolutionAndFramerateForLowQuality_BalancedMode) { |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3010 | const int kWidth = 1280; |
| 3011 | const int kHeight = 720; |
| 3012 | const int64_t kFrameIntervalMs = 150; |
| 3013 | int64_t timestamp_ms = kFrameIntervalMs; |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 3014 | video_stream_encoder_->OnBitrateUpdated( |
| 3015 | DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps), 0, 0); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3016 | |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 3017 | // Enable BALANCED preference, no initial limitation. |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3018 | AdaptingFrameForwarder source; |
| 3019 | source.set_adaptation_enabled(true); |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 3020 | video_stream_encoder_->SetSource(&source, |
| 3021 | webrtc::DegradationPreference::BALANCED); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3022 | timestamp_ms += kFrameIntervalMs; |
| 3023 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 3024 | WaitForEncodedFrame(kWidth, kHeight); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 3025 | VerifyFpsMaxResolutionMax(source.sink_wants()); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3026 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution); |
| 3027 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate); |
| 3028 | EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 3029 | |
| 3030 | // Trigger adapt down, expect scaled down resolution (960x540@30fps). |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 3031 | video_stream_encoder_->TriggerQualityLow(); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3032 | timestamp_ms += kFrameIntervalMs; |
| 3033 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 3034 | WaitForEncodedFrame(timestamp_ms); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3035 | VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight); |
| 3036 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution); |
| 3037 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate); |
| 3038 | EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 3039 | |
| 3040 | // Trigger adapt down, expect scaled down resolution (640x360@30fps). |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 3041 | video_stream_encoder_->TriggerQualityLow(); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3042 | timestamp_ms += kFrameIntervalMs; |
| 3043 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 3044 | WaitForEncodedFrame(timestamp_ms); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3045 | VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants()); |
| 3046 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution); |
| 3047 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate); |
| 3048 | EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 3049 | |
| 3050 | // Trigger adapt down, expect reduced fps (640x360@15fps). |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 3051 | video_stream_encoder_->TriggerQualityLow(); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3052 | timestamp_ms += kFrameIntervalMs; |
| 3053 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 3054 | WaitForEncodedFrame(timestamp_ms); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3055 | VerifyFpsLtResolutionEq(source.sink_wants(), source.last_wants()); |
| 3056 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution); |
| 3057 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate); |
| 3058 | EXPECT_EQ(3, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 3059 | |
| 3060 | // Trigger adapt down, expect scaled down resolution (480x270@15fps). |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 3061 | video_stream_encoder_->TriggerQualityLow(); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3062 | timestamp_ms += kFrameIntervalMs; |
| 3063 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 3064 | WaitForEncodedFrame(timestamp_ms); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3065 | VerifyFpsEqResolutionLt(source.sink_wants(), source.last_wants()); |
| 3066 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution); |
| 3067 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate); |
| 3068 | EXPECT_EQ(4, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 3069 | |
| 3070 | // Restrict bitrate, trigger adapt down, expect reduced fps (480x270@10fps). |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 3071 | video_stream_encoder_->TriggerQualityLow(); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3072 | timestamp_ms += kFrameIntervalMs; |
| 3073 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 3074 | WaitForEncodedFrame(timestamp_ms); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3075 | VerifyFpsLtResolutionEq(source.sink_wants(), source.last_wants()); |
| 3076 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution); |
| 3077 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate); |
| 3078 | EXPECT_EQ(5, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 3079 | |
| 3080 | // Trigger adapt down, expect scaled down resolution (320x180@10fps). |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 3081 | video_stream_encoder_->TriggerQualityLow(); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3082 | timestamp_ms += kFrameIntervalMs; |
| 3083 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 3084 | WaitForEncodedFrame(timestamp_ms); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3085 | VerifyFpsEqResolutionLt(source.sink_wants(), source.last_wants()); |
| 3086 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution); |
| 3087 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate); |
| 3088 | EXPECT_EQ(6, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 3089 | |
| 3090 | // Trigger adapt down, expect reduced fps (320x180@7fps). |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 3091 | video_stream_encoder_->TriggerQualityLow(); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3092 | timestamp_ms += kFrameIntervalMs; |
| 3093 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 3094 | WaitForEncodedFrame(timestamp_ms); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3095 | VerifyFpsLtResolutionEq(source.sink_wants(), source.last_wants()); |
| 3096 | rtc::VideoSinkWants last_wants = source.sink_wants(); |
| 3097 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution); |
| 3098 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate); |
| 3099 | EXPECT_EQ(7, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 3100 | |
| 3101 | // Trigger adapt down, min resolution reached, expect no change. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 3102 | video_stream_encoder_->TriggerQualityLow(); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3103 | timestamp_ms += kFrameIntervalMs; |
| 3104 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 3105 | WaitForEncodedFrame(timestamp_ms); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3106 | VerifyFpsEqResolutionEq(source.sink_wants(), last_wants); |
| 3107 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution); |
| 3108 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate); |
| 3109 | EXPECT_EQ(7, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 3110 | |
| 3111 | // Trigger adapt down, expect expect increased fps (320x180@10fps). |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 3112 | video_stream_encoder_->TriggerQualityHigh(); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3113 | timestamp_ms += kFrameIntervalMs; |
| 3114 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 3115 | WaitForEncodedFrame(timestamp_ms); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3116 | VerifyFpsGtResolutionEq(source.sink_wants(), source.last_wants()); |
| 3117 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution); |
| 3118 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate); |
| 3119 | EXPECT_EQ(8, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 3120 | |
| 3121 | // Trigger adapt up, expect upscaled resolution (480x270@10fps). |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 3122 | video_stream_encoder_->TriggerQualityHigh(); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3123 | timestamp_ms += kFrameIntervalMs; |
| 3124 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 3125 | WaitForEncodedFrame(timestamp_ms); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3126 | VerifyFpsEqResolutionGt(source.sink_wants(), source.last_wants()); |
| 3127 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution); |
| 3128 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate); |
| 3129 | EXPECT_EQ(9, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 3130 | |
| 3131 | // Increase bitrate, trigger adapt up, expect increased fps (480x270@15fps). |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 3132 | video_stream_encoder_->TriggerQualityHigh(); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3133 | timestamp_ms += kFrameIntervalMs; |
| 3134 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 3135 | WaitForEncodedFrame(timestamp_ms); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3136 | VerifyFpsGtResolutionEq(source.sink_wants(), source.last_wants()); |
| 3137 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution); |
| 3138 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate); |
| 3139 | EXPECT_EQ(10, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 3140 | |
| 3141 | // Trigger adapt up, expect upscaled resolution (640x360@15fps). |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 3142 | video_stream_encoder_->TriggerQualityHigh(); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3143 | timestamp_ms += kFrameIntervalMs; |
| 3144 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 3145 | WaitForEncodedFrame(timestamp_ms); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3146 | VerifyFpsEqResolutionGt(source.sink_wants(), source.last_wants()); |
| 3147 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution); |
| 3148 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate); |
| 3149 | EXPECT_EQ(11, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 3150 | |
| 3151 | // Trigger adapt up, expect increased fps (640x360@30fps). |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 3152 | video_stream_encoder_->TriggerQualityHigh(); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3153 | timestamp_ms += kFrameIntervalMs; |
| 3154 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 3155 | WaitForEncodedFrame(timestamp_ms); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3156 | VerifyFpsMaxResolutionEq(source.sink_wants(), source.last_wants()); |
| 3157 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution); |
| 3158 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate); |
| 3159 | EXPECT_EQ(12, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 3160 | |
| 3161 | // Trigger adapt up, expect upscaled resolution (960x540@30fps). |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 3162 | video_stream_encoder_->TriggerQualityHigh(); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3163 | timestamp_ms += kFrameIntervalMs; |
| 3164 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 3165 | WaitForEncodedFrame(timestamp_ms); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3166 | VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants()); |
| 3167 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution); |
| 3168 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate); |
| 3169 | EXPECT_EQ(13, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 3170 | |
| 3171 | // Trigger adapt up, expect no restriction (1280x720fps@30fps). |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 3172 | video_stream_encoder_->TriggerQualityHigh(); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3173 | timestamp_ms += kFrameIntervalMs; |
| 3174 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 3175 | WaitForEncodedFrame(kWidth, kHeight); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3176 | VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants()); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 3177 | VerifyFpsMaxResolutionMax(source.sink_wants()); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3178 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution); |
| 3179 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate); |
| 3180 | EXPECT_EQ(14, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 3181 | |
| 3182 | // Trigger adapt up, expect no change. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 3183 | video_stream_encoder_->TriggerQualityHigh(); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 3184 | VerifyFpsMaxResolutionMax(source.sink_wants()); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3185 | EXPECT_EQ(14, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 3186 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 3187 | video_stream_encoder_->Stop(); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3188 | } |
| 3189 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 3190 | TEST_F(VideoStreamEncoderTest, AdaptWithTwoReasonsAndDifferentOrder_Framerate) { |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3191 | const int kWidth = 1280; |
| 3192 | const int kHeight = 720; |
| 3193 | const int64_t kFrameIntervalMs = 150; |
| 3194 | int64_t timestamp_ms = kFrameIntervalMs; |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 3195 | video_stream_encoder_->OnBitrateUpdated( |
| 3196 | DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps), 0, 0); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3197 | |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 3198 | // Enable BALANCED preference, no initial limitation. |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3199 | AdaptingFrameForwarder source; |
| 3200 | source.set_adaptation_enabled(true); |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 3201 | video_stream_encoder_->SetSource(&source, |
| 3202 | webrtc::DegradationPreference::BALANCED); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3203 | timestamp_ms += kFrameIntervalMs; |
| 3204 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 3205 | WaitForEncodedFrame(kWidth, kHeight); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 3206 | VerifyFpsMaxResolutionMax(source.sink_wants()); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3207 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution); |
| 3208 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate); |
| 3209 | EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution); |
| 3210 | EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate); |
| 3211 | EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
| 3212 | EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 3213 | |
| 3214 | // Trigger cpu adapt down, expect scaled down resolution (960x540@30fps). |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 3215 | video_stream_encoder_->TriggerCpuOveruse(); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3216 | timestamp_ms += kFrameIntervalMs; |
| 3217 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 3218 | WaitForEncodedFrame(timestamp_ms); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3219 | VerifyFpsMaxResolutionLt(source.sink_wants(), kWidth * kHeight); |
| 3220 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution); |
| 3221 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate); |
| 3222 | EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution); |
| 3223 | EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate); |
| 3224 | EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
| 3225 | EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 3226 | |
| 3227 | // Trigger cpu adapt down, expect scaled down resolution (640x360@30fps). |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 3228 | video_stream_encoder_->TriggerCpuOveruse(); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3229 | timestamp_ms += kFrameIntervalMs; |
| 3230 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 3231 | WaitForEncodedFrame(timestamp_ms); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3232 | VerifyFpsMaxResolutionLt(source.sink_wants(), source.last_wants()); |
| 3233 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution); |
| 3234 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate); |
| 3235 | EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution); |
| 3236 | EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate); |
| 3237 | EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
| 3238 | EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 3239 | |
| 3240 | // Trigger quality adapt down, expect reduced fps (640x360@15fps). |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 3241 | video_stream_encoder_->TriggerQualityLow(); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3242 | timestamp_ms += kFrameIntervalMs; |
| 3243 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 3244 | WaitForEncodedFrame(timestamp_ms); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3245 | VerifyFpsLtResolutionEq(source.sink_wants(), source.last_wants()); |
| 3246 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution); |
| 3247 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate); |
| 3248 | EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution); |
| 3249 | EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate); |
| 3250 | EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
| 3251 | EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 3252 | |
| 3253 | // Trigger cpu adapt up, expect increased fps (640x360@30fps). |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 3254 | video_stream_encoder_->TriggerCpuNormalUsage(); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3255 | timestamp_ms += kFrameIntervalMs; |
| 3256 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 3257 | WaitForEncodedFrame(timestamp_ms); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3258 | VerifyFpsMaxResolutionEq(source.sink_wants(), source.last_wants()); |
| 3259 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution); |
| 3260 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate); |
| 3261 | EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution); |
| 3262 | EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate); |
| 3263 | EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
| 3264 | EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 3265 | |
| 3266 | // Trigger quality adapt up, expect upscaled resolution (960x540@30fps). |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 3267 | video_stream_encoder_->TriggerQualityHigh(); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3268 | timestamp_ms += kFrameIntervalMs; |
| 3269 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 3270 | WaitForEncodedFrame(timestamp_ms); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3271 | VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants()); |
| 3272 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution); |
| 3273 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate); |
| 3274 | EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_resolution); |
| 3275 | EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate); |
| 3276 | EXPECT_EQ(3, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
| 3277 | EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 3278 | |
| 3279 | // Trigger cpu adapt up, expect no restriction (1280x720fps@30fps). |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 3280 | video_stream_encoder_->TriggerCpuNormalUsage(); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3281 | timestamp_ms += kFrameIntervalMs; |
| 3282 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 3283 | WaitForEncodedFrame(kWidth, kHeight); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3284 | VerifyFpsMaxResolutionGt(source.sink_wants(), source.last_wants()); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 3285 | VerifyFpsMaxResolutionMax(source.sink_wants()); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3286 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution); |
| 3287 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate); |
| 3288 | EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution); |
| 3289 | EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate); |
| 3290 | EXPECT_EQ(4, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
| 3291 | EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 3292 | |
| 3293 | // Trigger adapt up, expect no change. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 3294 | video_stream_encoder_->TriggerQualityHigh(); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 3295 | VerifyFpsMaxResolutionMax(source.sink_wants()); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3296 | EXPECT_EQ(4, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
| 3297 | EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 3298 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 3299 | video_stream_encoder_->Stop(); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3300 | } |
| 3301 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 3302 | TEST_F(VideoStreamEncoderTest, |
| 3303 | AdaptWithTwoReasonsAndDifferentOrder_Resolution) { |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3304 | const int kWidth = 640; |
| 3305 | const int kHeight = 360; |
| 3306 | const int kFpsLimit = 15; |
| 3307 | const int64_t kFrameIntervalMs = 150; |
| 3308 | int64_t timestamp_ms = kFrameIntervalMs; |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 3309 | video_stream_encoder_->OnBitrateUpdated( |
| 3310 | DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps), 0, 0); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3311 | |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 3312 | // Enable BALANCED preference, no initial limitation. |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3313 | AdaptingFrameForwarder source; |
| 3314 | source.set_adaptation_enabled(true); |
Taylor Brandstetter | 49fcc10 | 2018-05-16 14:20:41 -0700 | [diff] [blame] | 3315 | video_stream_encoder_->SetSource(&source, |
| 3316 | webrtc::DegradationPreference::BALANCED); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3317 | timestamp_ms += kFrameIntervalMs; |
| 3318 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 3319 | WaitForEncodedFrame(kWidth, kHeight); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 3320 | VerifyFpsMaxResolutionMax(source.sink_wants()); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3321 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution); |
| 3322 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate); |
| 3323 | EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution); |
| 3324 | EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate); |
| 3325 | EXPECT_EQ(0, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
| 3326 | EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 3327 | |
| 3328 | // Trigger cpu adapt down, expect scaled down framerate (640x360@15fps). |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 3329 | video_stream_encoder_->TriggerCpuOveruse(); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3330 | timestamp_ms += kFrameIntervalMs; |
| 3331 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 3332 | WaitForEncodedFrame(timestamp_ms); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3333 | VerifyFpsEqResolutionMax(source.sink_wants(), kFpsLimit); |
| 3334 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution); |
| 3335 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate); |
| 3336 | EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution); |
| 3337 | EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_framerate); |
| 3338 | EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
| 3339 | EXPECT_EQ(0, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 3340 | |
| 3341 | // Trigger quality adapt down, expect scaled down resolution (480x270@15fps). |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 3342 | video_stream_encoder_->TriggerQualityLow(); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3343 | timestamp_ms += kFrameIntervalMs; |
| 3344 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 3345 | WaitForEncodedFrame(timestamp_ms); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3346 | VerifyFpsEqResolutionLt(source.sink_wants(), source.last_wants()); |
| 3347 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_resolution); |
| 3348 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate); |
| 3349 | EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution); |
| 3350 | EXPECT_TRUE(stats_proxy_->GetStats().cpu_limited_framerate); |
| 3351 | EXPECT_EQ(1, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
| 3352 | EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 3353 | |
| 3354 | // Trigger cpu adapt up, expect upscaled resolution (640x360@15fps). |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 3355 | video_stream_encoder_->TriggerCpuNormalUsage(); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3356 | timestamp_ms += kFrameIntervalMs; |
| 3357 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 3358 | WaitForEncodedFrame(timestamp_ms); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3359 | VerifyFpsEqResolutionGt(source.sink_wants(), source.last_wants()); |
| 3360 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution); |
| 3361 | EXPECT_TRUE(stats_proxy_->GetStats().bw_limited_framerate); |
| 3362 | EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution); |
| 3363 | EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate); |
| 3364 | EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
| 3365 | EXPECT_EQ(1, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 3366 | |
| 3367 | // Trigger quality adapt up, expect increased fps (640x360@30fps). |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 3368 | video_stream_encoder_->TriggerQualityHigh(); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3369 | timestamp_ms += kFrameIntervalMs; |
| 3370 | source.IncomingCapturedFrame(CreateFrame(timestamp_ms, kWidth, kHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 3371 | WaitForEncodedFrame(timestamp_ms); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 3372 | VerifyFpsMaxResolutionMax(source.sink_wants()); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3373 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_resolution); |
| 3374 | EXPECT_FALSE(stats_proxy_->GetStats().bw_limited_framerate); |
| 3375 | EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_resolution); |
| 3376 | EXPECT_FALSE(stats_proxy_->GetStats().cpu_limited_framerate); |
| 3377 | EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
| 3378 | EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 3379 | |
| 3380 | // Trigger adapt up, expect no change. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 3381 | video_stream_encoder_->TriggerQualityHigh(); |
Åsa Persson | 8c1bf95 | 2018-09-13 10:42:19 +0200 | [diff] [blame] | 3382 | VerifyFpsMaxResolutionMax(source.sink_wants()); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3383 | EXPECT_EQ(2, stats_proxy_->GetStats().number_of_cpu_adapt_changes); |
| 3384 | EXPECT_EQ(2, stats_proxy_->GetStats().number_of_quality_adapt_changes); |
| 3385 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 3386 | video_stream_encoder_->Stop(); |
asapersson | f7e294d | 2017-06-13 23:25:22 -0700 | [diff] [blame] | 3387 | } |
| 3388 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 3389 | TEST_F(VideoStreamEncoderTest, AcceptsFullHdAdaptedDownSimulcastFrames) { |
ilnik | 6b826ef | 2017-06-16 06:53:48 -0700 | [diff] [blame] | 3390 | // Simulates simulcast behavior and makes highest stream resolutions divisible |
| 3391 | // by 4. |
| 3392 | class CroppingVideoStreamFactory |
| 3393 | : public VideoEncoderConfig::VideoStreamFactoryInterface { |
| 3394 | public: |
| 3395 | explicit CroppingVideoStreamFactory(size_t num_temporal_layers, |
| 3396 | int framerate) |
| 3397 | : num_temporal_layers_(num_temporal_layers), framerate_(framerate) { |
| 3398 | EXPECT_GT(num_temporal_layers, 0u); |
| 3399 | EXPECT_GT(framerate, 0); |
| 3400 | } |
| 3401 | |
| 3402 | private: |
| 3403 | std::vector<VideoStream> CreateEncoderStreams( |
| 3404 | int width, |
| 3405 | int height, |
| 3406 | const VideoEncoderConfig& encoder_config) override { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 3407 | std::vector<VideoStream> streams = test::CreateVideoStreams( |
| 3408 | width - width % 4, height - height % 4, encoder_config); |
ilnik | 6b826ef | 2017-06-16 06:53:48 -0700 | [diff] [blame] | 3409 | for (VideoStream& stream : streams) { |
Sergey Silkin | a796a7e | 2018-03-01 15:11:29 +0100 | [diff] [blame] | 3410 | stream.num_temporal_layers = num_temporal_layers_; |
ilnik | 6b826ef | 2017-06-16 06:53:48 -0700 | [diff] [blame] | 3411 | stream.max_framerate = framerate_; |
| 3412 | } |
| 3413 | return streams; |
| 3414 | } |
| 3415 | |
| 3416 | const size_t num_temporal_layers_; |
| 3417 | const int framerate_; |
| 3418 | }; |
| 3419 | |
| 3420 | const int kFrameWidth = 1920; |
| 3421 | const int kFrameHeight = 1080; |
| 3422 | // 3/4 of 1920. |
| 3423 | const int kAdaptedFrameWidth = 1440; |
| 3424 | // 3/4 of 1080 rounded down to multiple of 4. |
| 3425 | const int kAdaptedFrameHeight = 808; |
| 3426 | const int kFramerate = 24; |
| 3427 | |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 3428 | video_stream_encoder_->OnBitrateUpdated( |
| 3429 | DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps), 0, 0); |
ilnik | 6b826ef | 2017-06-16 06:53:48 -0700 | [diff] [blame] | 3430 | // Trigger reconfigure encoder (without resetting the entire instance). |
| 3431 | VideoEncoderConfig video_encoder_config; |
Niels Möller | 259a497 | 2018-04-05 15:36:51 +0200 | [diff] [blame] | 3432 | video_encoder_config.codec_type = kVideoCodecVP8; |
ilnik | 6b826ef | 2017-06-16 06:53:48 -0700 | [diff] [blame] | 3433 | video_encoder_config.max_bitrate_bps = kTargetBitrateBps; |
| 3434 | video_encoder_config.number_of_streams = 1; |
| 3435 | video_encoder_config.video_stream_factory = |
| 3436 | new rtc::RefCountedObject<CroppingVideoStreamFactory>(1, kFramerate); |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 3437 | video_stream_encoder_->ConfigureEncoder(std::move(video_encoder_config), |
Niels Möller | f133856 | 2018-04-26 09:51:47 +0200 | [diff] [blame] | 3438 | kMaxPayloadLength); |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 3439 | video_stream_encoder_->WaitUntilTaskQueueIsIdle(); |
ilnik | 6b826ef | 2017-06-16 06:53:48 -0700 | [diff] [blame] | 3440 | |
| 3441 | video_source_.set_adaptation_enabled(true); |
| 3442 | |
| 3443 | video_source_.IncomingCapturedFrame( |
| 3444 | CreateFrame(1, kFrameWidth, kFrameHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 3445 | WaitForEncodedFrame(kFrameWidth, kFrameHeight); |
ilnik | 6b826ef | 2017-06-16 06:53:48 -0700 | [diff] [blame] | 3446 | |
| 3447 | // Trigger CPU overuse, downscale by 3/4. |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 3448 | video_stream_encoder_->TriggerCpuOveruse(); |
ilnik | 6b826ef | 2017-06-16 06:53:48 -0700 | [diff] [blame] | 3449 | video_source_.IncomingCapturedFrame( |
| 3450 | CreateFrame(2, kFrameWidth, kFrameHeight)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 3451 | WaitForEncodedFrame(kAdaptedFrameWidth, kAdaptedFrameHeight); |
ilnik | 6b826ef | 2017-06-16 06:53:48 -0700 | [diff] [blame] | 3452 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 3453 | video_stream_encoder_->Stop(); |
ilnik | 6b826ef | 2017-06-16 06:53:48 -0700 | [diff] [blame] | 3454 | } |
| 3455 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 3456 | TEST_F(VideoStreamEncoderTest, PeriodicallyUpdatesChannelParameters) { |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 3457 | const int kFrameWidth = 1280; |
| 3458 | const int kFrameHeight = 720; |
| 3459 | const int kLowFps = 2; |
| 3460 | const int kHighFps = 30; |
| 3461 | |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 3462 | video_stream_encoder_->OnBitrateUpdated( |
| 3463 | DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps), 0, 0); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 3464 | |
| 3465 | int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec; |
| 3466 | max_framerate_ = kLowFps; |
| 3467 | |
| 3468 | // Insert 2 seconds of 2fps video. |
| 3469 | for (int i = 0; i < kLowFps * 2; ++i) { |
| 3470 | video_source_.IncomingCapturedFrame( |
| 3471 | CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight)); |
| 3472 | WaitForEncodedFrame(timestamp_ms); |
| 3473 | timestamp_ms += 1000 / kLowFps; |
| 3474 | } |
| 3475 | |
| 3476 | // Make sure encoder is updated with new target. |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 3477 | video_stream_encoder_->OnBitrateUpdated( |
| 3478 | DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps), 0, 0); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 3479 | video_source_.IncomingCapturedFrame( |
| 3480 | CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight)); |
| 3481 | WaitForEncodedFrame(timestamp_ms); |
| 3482 | timestamp_ms += 1000 / kLowFps; |
| 3483 | |
| 3484 | EXPECT_EQ(kLowFps, fake_encoder_.GetConfiguredInputFramerate()); |
| 3485 | |
| 3486 | // Insert 30fps frames for just a little more than the forced update period. |
| 3487 | const int kVcmTimerIntervalFrames = |
| 3488 | (vcm::VCMProcessTimer::kDefaultProcessIntervalMs * kHighFps) / 1000; |
| 3489 | const int kFrameIntervalMs = 1000 / kHighFps; |
| 3490 | max_framerate_ = kHighFps; |
| 3491 | for (int i = 0; i < kVcmTimerIntervalFrames + 2; ++i) { |
| 3492 | video_source_.IncomingCapturedFrame( |
| 3493 | CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight)); |
| 3494 | // Wait for encoded frame, but skip ahead if it doesn't arrive as it might |
| 3495 | // be dropped if the encoder hans't been updated with the new higher target |
| 3496 | // framerate yet, causing it to overshoot the target bitrate and then |
| 3497 | // suffering the wrath of the media optimizer. |
| 3498 | TimedWaitForEncodedFrame(timestamp_ms, 2 * kFrameIntervalMs); |
| 3499 | timestamp_ms += kFrameIntervalMs; |
| 3500 | } |
| 3501 | |
| 3502 | // Don expect correct measurement just yet, but it should be higher than |
| 3503 | // before. |
| 3504 | EXPECT_GT(fake_encoder_.GetConfiguredInputFramerate(), kLowFps); |
| 3505 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 3506 | video_stream_encoder_->Stop(); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 3507 | } |
| 3508 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 3509 | TEST_F(VideoStreamEncoderTest, DoesNotUpdateBitrateAllocationWhenSuspended) { |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 3510 | const int kFrameWidth = 1280; |
| 3511 | const int kFrameHeight = 720; |
| 3512 | const int kTargetBitrateBps = 1000000; |
| 3513 | |
| 3514 | MockBitrateObserver bitrate_observer; |
Niels Möller | 0327c2d | 2018-05-21 14:09:31 +0200 | [diff] [blame] | 3515 | video_stream_encoder_->SetBitrateAllocationObserver(&bitrate_observer); |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 3516 | video_stream_encoder_->OnBitrateUpdated( |
| 3517 | DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps), 0, 0); |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 3518 | video_stream_encoder_->WaitUntilTaskQueueIsIdle(); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 3519 | |
| 3520 | // Insert a first video frame, causes another bitrate update. |
| 3521 | int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec; |
| 3522 | EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(_)).Times(1); |
| 3523 | video_source_.IncomingCapturedFrame( |
| 3524 | CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight)); |
| 3525 | WaitForEncodedFrame(timestamp_ms); |
| 3526 | |
| 3527 | // Next, simulate video suspension due to pacer queue overrun. |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 3528 | video_stream_encoder_->OnBitrateUpdated(DataRate::bps(0), DataRate::bps(0), 0, |
Erik Språng | 610c763 | 2019-03-06 15:37:33 +0100 | [diff] [blame] | 3529 | 1); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 3530 | |
| 3531 | // Skip ahead until a new periodic parameter update should have occured. |
| 3532 | timestamp_ms += vcm::VCMProcessTimer::kDefaultProcessIntervalMs; |
Sebastian Jansson | 40889f3 | 2019-04-17 12:11:20 +0200 | [diff] [blame] | 3533 | fake_clock_.AdvanceTime( |
| 3534 | TimeDelta::ms(vcm::VCMProcessTimer::kDefaultProcessIntervalMs)); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 3535 | |
| 3536 | // Bitrate observer should not be called. |
| 3537 | EXPECT_CALL(bitrate_observer, OnBitrateAllocationUpdated(_)).Times(0); |
| 3538 | video_source_.IncomingCapturedFrame( |
| 3539 | CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight)); |
| 3540 | ExpectDroppedFrame(); |
| 3541 | |
mflodman | cc3d442 | 2017-08-03 08:27:51 -0700 | [diff] [blame] | 3542 | video_stream_encoder_->Stop(); |
sprang | 4847ae6 | 2017-06-27 07:06:52 -0700 | [diff] [blame] | 3543 | } |
ilnik | 6b826ef | 2017-06-16 06:53:48 -0700 | [diff] [blame] | 3544 | |
Niels Möller | 4db138e | 2018-04-19 09:04:13 +0200 | [diff] [blame] | 3545 | TEST_F(VideoStreamEncoderTest, |
| 3546 | DefaultCpuAdaptationThresholdsForSoftwareEncoder) { |
| 3547 | const int kFrameWidth = 1280; |
| 3548 | const int kFrameHeight = 720; |
| 3549 | const CpuOveruseOptions default_options; |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 3550 | video_stream_encoder_->OnBitrateUpdated( |
| 3551 | DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps), 0, 0); |
Niels Möller | 4db138e | 2018-04-19 09:04:13 +0200 | [diff] [blame] | 3552 | video_source_.IncomingCapturedFrame( |
| 3553 | CreateFrame(1, kFrameWidth, kFrameHeight)); |
| 3554 | WaitForEncodedFrame(1); |
| 3555 | EXPECT_EQ(video_stream_encoder_->overuse_detector_proxy_->GetOptions() |
| 3556 | .low_encode_usage_threshold_percent, |
| 3557 | default_options.low_encode_usage_threshold_percent); |
| 3558 | EXPECT_EQ(video_stream_encoder_->overuse_detector_proxy_->GetOptions() |
| 3559 | .high_encode_usage_threshold_percent, |
| 3560 | default_options.high_encode_usage_threshold_percent); |
| 3561 | video_stream_encoder_->Stop(); |
| 3562 | } |
| 3563 | |
| 3564 | TEST_F(VideoStreamEncoderTest, |
| 3565 | HigherCpuAdaptationThresholdsForHardwareEncoder) { |
| 3566 | const int kFrameWidth = 1280; |
| 3567 | const int kFrameHeight = 720; |
| 3568 | CpuOveruseOptions hardware_options; |
| 3569 | hardware_options.low_encode_usage_threshold_percent = 150; |
| 3570 | hardware_options.high_encode_usage_threshold_percent = 200; |
Mirta Dvornicic | ccc1b57 | 2019-01-15 12:42:18 +0100 | [diff] [blame] | 3571 | fake_encoder_.SetIsHardwareAccelerated(true); |
Niels Möller | 4db138e | 2018-04-19 09:04:13 +0200 | [diff] [blame] | 3572 | |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 3573 | video_stream_encoder_->OnBitrateUpdated( |
| 3574 | DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps), 0, 0); |
Niels Möller | 4db138e | 2018-04-19 09:04:13 +0200 | [diff] [blame] | 3575 | video_source_.IncomingCapturedFrame( |
| 3576 | CreateFrame(1, kFrameWidth, kFrameHeight)); |
| 3577 | WaitForEncodedFrame(1); |
| 3578 | EXPECT_EQ(video_stream_encoder_->overuse_detector_proxy_->GetOptions() |
| 3579 | .low_encode_usage_threshold_percent, |
| 3580 | hardware_options.low_encode_usage_threshold_percent); |
| 3581 | EXPECT_EQ(video_stream_encoder_->overuse_detector_proxy_->GetOptions() |
| 3582 | .high_encode_usage_threshold_percent, |
| 3583 | hardware_options.high_encode_usage_threshold_percent); |
| 3584 | video_stream_encoder_->Stop(); |
| 3585 | } |
| 3586 | |
Niels Möller | 6bb5ab9 | 2019-01-11 11:11:10 +0100 | [diff] [blame] | 3587 | TEST_F(VideoStreamEncoderTest, DropsFramesWhenEncoderOvershoots) { |
| 3588 | const int kFrameWidth = 320; |
| 3589 | const int kFrameHeight = 240; |
| 3590 | const int kFps = 30; |
| 3591 | const int kTargetBitrateBps = 120000; |
| 3592 | const int kNumFramesInRun = kFps * 5; // Runs of five seconds. |
| 3593 | |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 3594 | video_stream_encoder_->OnBitrateUpdated( |
| 3595 | DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps), 0, 0); |
Niels Möller | 6bb5ab9 | 2019-01-11 11:11:10 +0100 | [diff] [blame] | 3596 | |
| 3597 | int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec; |
| 3598 | max_framerate_ = kFps; |
| 3599 | |
| 3600 | // Insert 3 seconds of video, verify number of drops with normal bitrate. |
| 3601 | fake_encoder_.SimulateOvershoot(1.0); |
| 3602 | int num_dropped = 0; |
| 3603 | for (int i = 0; i < kNumFramesInRun; ++i) { |
| 3604 | video_source_.IncomingCapturedFrame( |
| 3605 | CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight)); |
| 3606 | // Wait up to two frame durations for a frame to arrive. |
| 3607 | if (!TimedWaitForEncodedFrame(timestamp_ms, 2 * 1000 / kFps)) { |
| 3608 | ++num_dropped; |
| 3609 | } |
| 3610 | timestamp_ms += 1000 / kFps; |
| 3611 | } |
| 3612 | |
Erik Språng | a8d48ab | 2019-02-08 14:17:40 +0100 | [diff] [blame] | 3613 | // Framerate should be measured to be near the expected target rate. |
| 3614 | EXPECT_NEAR(fake_encoder_.GetLastFramerate(), kFps, 1); |
| 3615 | |
| 3616 | // Frame drops should be within 5% of expected 0%. |
| 3617 | EXPECT_NEAR(num_dropped, 0, 5 * kNumFramesInRun / 100); |
Niels Möller | 6bb5ab9 | 2019-01-11 11:11:10 +0100 | [diff] [blame] | 3618 | |
| 3619 | // Make encoder produce frames at double the expected bitrate during 3 seconds |
| 3620 | // of video, verify number of drops. Rate needs to be slightly changed in |
| 3621 | // order to force the rate to be reconfigured. |
Erik Språng | 7ca375c | 2019-02-06 16:20:17 +0100 | [diff] [blame] | 3622 | double overshoot_factor = 2.0; |
| 3623 | if (RateControlSettings::ParseFromFieldTrials().UseEncoderBitrateAdjuster()) { |
| 3624 | // With bitrate adjuster, when need to overshoot even more to trigger |
| 3625 | // frame dropping. |
| 3626 | overshoot_factor *= 2; |
| 3627 | } |
| 3628 | fake_encoder_.SimulateOvershoot(overshoot_factor); |
Erik Språng | 610c763 | 2019-03-06 15:37:33 +0100 | [diff] [blame] | 3629 | video_stream_encoder_->OnBitrateUpdated( |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 3630 | DataRate::bps(kTargetBitrateBps + 1000), |
| 3631 | DataRate::bps(kTargetBitrateBps + 1000), 0, 0); |
Niels Möller | 6bb5ab9 | 2019-01-11 11:11:10 +0100 | [diff] [blame] | 3632 | num_dropped = 0; |
| 3633 | for (int i = 0; i < kNumFramesInRun; ++i) { |
| 3634 | video_source_.IncomingCapturedFrame( |
| 3635 | CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight)); |
| 3636 | // Wait up to two frame durations for a frame to arrive. |
| 3637 | if (!TimedWaitForEncodedFrame(timestamp_ms, 2 * 1000 / kFps)) { |
| 3638 | ++num_dropped; |
| 3639 | } |
| 3640 | timestamp_ms += 1000 / kFps; |
| 3641 | } |
| 3642 | |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 3643 | video_stream_encoder_->OnBitrateUpdated( |
| 3644 | DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps), 0, 0); |
Erik Språng | a8d48ab | 2019-02-08 14:17:40 +0100 | [diff] [blame] | 3645 | |
| 3646 | // Target framerate should be still be near the expected target, despite |
| 3647 | // the frame drops. |
| 3648 | EXPECT_NEAR(fake_encoder_.GetLastFramerate(), kFps, 1); |
| 3649 | |
| 3650 | // Frame drops should be within 5% of expected 50%. |
| 3651 | EXPECT_NEAR(num_dropped, kNumFramesInRun / 2, 5 * kNumFramesInRun / 100); |
Niels Möller | 6bb5ab9 | 2019-01-11 11:11:10 +0100 | [diff] [blame] | 3652 | |
| 3653 | video_stream_encoder_->Stop(); |
| 3654 | } |
| 3655 | |
| 3656 | TEST_F(VideoStreamEncoderTest, ConfiguresCorrectFrameRate) { |
| 3657 | const int kFrameWidth = 320; |
| 3658 | const int kFrameHeight = 240; |
| 3659 | const int kActualInputFps = 24; |
| 3660 | const int kTargetBitrateBps = 120000; |
| 3661 | |
| 3662 | ASSERT_GT(max_framerate_, kActualInputFps); |
| 3663 | |
| 3664 | int64_t timestamp_ms = fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec; |
| 3665 | max_framerate_ = kActualInputFps; |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 3666 | video_stream_encoder_->OnBitrateUpdated( |
| 3667 | DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps), 0, 0); |
Niels Möller | 6bb5ab9 | 2019-01-11 11:11:10 +0100 | [diff] [blame] | 3668 | |
| 3669 | // Insert 3 seconds of video, with an input fps lower than configured max. |
| 3670 | for (int i = 0; i < kActualInputFps * 3; ++i) { |
| 3671 | video_source_.IncomingCapturedFrame( |
| 3672 | CreateFrame(timestamp_ms, kFrameWidth, kFrameHeight)); |
| 3673 | // Wait up to two frame durations for a frame to arrive. |
| 3674 | WaitForEncodedFrame(timestamp_ms); |
| 3675 | timestamp_ms += 1000 / kActualInputFps; |
| 3676 | } |
| 3677 | |
| 3678 | EXPECT_NEAR(kActualInputFps, fake_encoder_.GetLastFramerate(), 1); |
| 3679 | |
| 3680 | video_stream_encoder_->Stop(); |
| 3681 | } |
| 3682 | |
Ilya Nikolaevskiy | 71aee3a | 2019-02-18 13:01:26 +0100 | [diff] [blame] | 3683 | TEST_F(VideoStreamEncoderTest, AccumulatesUpdateRectOnDroppedFrames) { |
| 3684 | VideoFrame::UpdateRect rect; |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 3685 | video_stream_encoder_->OnBitrateUpdated( |
| 3686 | DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps), 0, 0); |
Ilya Nikolaevskiy | 71aee3a | 2019-02-18 13:01:26 +0100 | [diff] [blame] | 3687 | |
| 3688 | fake_encoder_.BlockNextEncode(); |
| 3689 | video_source_.IncomingCapturedFrame( |
| 3690 | CreateFrameWithUpdatedPixel(1, nullptr, 0)); |
| 3691 | WaitForEncodedFrame(1); |
| 3692 | // On the very first frame full update should be forced. |
| 3693 | rect = fake_encoder_.GetLastUpdateRect(); |
| 3694 | EXPECT_EQ(rect.offset_x, 0); |
| 3695 | EXPECT_EQ(rect.offset_y, 0); |
| 3696 | EXPECT_EQ(rect.height, codec_height_); |
| 3697 | EXPECT_EQ(rect.width, codec_width_); |
| 3698 | // Here, the encoder thread will be blocked in the TestEncoder waiting for a |
| 3699 | // call to ContinueEncode. |
| 3700 | video_source_.IncomingCapturedFrame( |
| 3701 | CreateFrameWithUpdatedPixel(2, nullptr, 1)); |
| 3702 | ExpectDroppedFrame(); |
| 3703 | video_source_.IncomingCapturedFrame( |
| 3704 | CreateFrameWithUpdatedPixel(3, nullptr, 10)); |
| 3705 | ExpectDroppedFrame(); |
| 3706 | fake_encoder_.ContinueEncode(); |
| 3707 | WaitForEncodedFrame(3); |
| 3708 | // Updates to pixels 1 and 10 should be accumulated to one 10x1 rect. |
| 3709 | rect = fake_encoder_.GetLastUpdateRect(); |
| 3710 | EXPECT_EQ(rect.offset_x, 1); |
| 3711 | EXPECT_EQ(rect.offset_y, 0); |
| 3712 | EXPECT_EQ(rect.width, 10); |
| 3713 | EXPECT_EQ(rect.height, 1); |
| 3714 | |
| 3715 | video_source_.IncomingCapturedFrame( |
| 3716 | CreateFrameWithUpdatedPixel(4, nullptr, 0)); |
| 3717 | WaitForEncodedFrame(4); |
| 3718 | // Previous frame was encoded, so no accumulation should happen. |
| 3719 | rect = fake_encoder_.GetLastUpdateRect(); |
| 3720 | EXPECT_EQ(rect.offset_x, 0); |
| 3721 | EXPECT_EQ(rect.offset_y, 0); |
| 3722 | EXPECT_EQ(rect.width, 1); |
| 3723 | EXPECT_EQ(rect.height, 1); |
| 3724 | |
| 3725 | video_stream_encoder_->Stop(); |
| 3726 | } |
| 3727 | |
Erik Språng | d7329ca | 2019-02-21 21:19:53 +0100 | [diff] [blame] | 3728 | TEST_F(VideoStreamEncoderTest, SetsFrameTypes) { |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 3729 | video_stream_encoder_->OnBitrateUpdated( |
| 3730 | DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps), 0, 0); |
Erik Språng | d7329ca | 2019-02-21 21:19:53 +0100 | [diff] [blame] | 3731 | |
| 3732 | // First frame is always keyframe. |
| 3733 | video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr)); |
| 3734 | WaitForEncodedFrame(1); |
Niels Möller | 8f7ce22 | 2019-03-21 15:43:58 +0100 | [diff] [blame] | 3735 | EXPECT_THAT( |
| 3736 | fake_encoder_.LastFrameTypes(), |
Mirko Bonadei | 6a489f2 | 2019-04-09 15:11:12 +0200 | [diff] [blame] | 3737 | ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameKey})); |
Erik Språng | d7329ca | 2019-02-21 21:19:53 +0100 | [diff] [blame] | 3738 | |
| 3739 | // Insert delta frame. |
| 3740 | video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr)); |
| 3741 | WaitForEncodedFrame(2); |
Niels Möller | 8f7ce22 | 2019-03-21 15:43:58 +0100 | [diff] [blame] | 3742 | EXPECT_THAT( |
| 3743 | fake_encoder_.LastFrameTypes(), |
Mirko Bonadei | 6a489f2 | 2019-04-09 15:11:12 +0200 | [diff] [blame] | 3744 | ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameDelta})); |
Erik Språng | d7329ca | 2019-02-21 21:19:53 +0100 | [diff] [blame] | 3745 | |
| 3746 | // Request next frame be a key-frame. |
| 3747 | video_stream_encoder_->SendKeyFrame(); |
| 3748 | video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr)); |
| 3749 | WaitForEncodedFrame(3); |
Niels Möller | 8f7ce22 | 2019-03-21 15:43:58 +0100 | [diff] [blame] | 3750 | EXPECT_THAT( |
| 3751 | fake_encoder_.LastFrameTypes(), |
Mirko Bonadei | 6a489f2 | 2019-04-09 15:11:12 +0200 | [diff] [blame] | 3752 | ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameKey})); |
Erik Språng | d7329ca | 2019-02-21 21:19:53 +0100 | [diff] [blame] | 3753 | |
| 3754 | video_stream_encoder_->Stop(); |
| 3755 | } |
| 3756 | |
| 3757 | TEST_F(VideoStreamEncoderTest, SetsFrameTypesSimulcast) { |
| 3758 | // Setup simulcast with three streams. |
| 3759 | ResetEncoder("VP8", 3, 1, 1, false); |
Erik Språng | 610c763 | 2019-03-06 15:37:33 +0100 | [diff] [blame] | 3760 | video_stream_encoder_->OnBitrateUpdated( |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 3761 | DataRate::bps(kSimulcastTargetBitrateBps), |
| 3762 | DataRate::bps(kSimulcastTargetBitrateBps), 0, 0); |
Erik Språng | d7329ca | 2019-02-21 21:19:53 +0100 | [diff] [blame] | 3763 | // Wait for all three layers before triggering event. |
| 3764 | sink_.SetNumExpectedLayers(3); |
| 3765 | |
| 3766 | // First frame is always keyframe. |
| 3767 | video_source_.IncomingCapturedFrame(CreateFrame(1, nullptr)); |
| 3768 | WaitForEncodedFrame(1); |
| 3769 | EXPECT_THAT(fake_encoder_.LastFrameTypes(), |
Mirko Bonadei | 6a489f2 | 2019-04-09 15:11:12 +0200 | [diff] [blame] | 3770 | ::testing::ElementsAreArray({VideoFrameType::kVideoFrameKey, |
| 3771 | VideoFrameType::kVideoFrameKey, |
| 3772 | VideoFrameType::kVideoFrameKey})); |
Erik Språng | d7329ca | 2019-02-21 21:19:53 +0100 | [diff] [blame] | 3773 | |
| 3774 | // Insert delta frame. |
| 3775 | video_source_.IncomingCapturedFrame(CreateFrame(2, nullptr)); |
| 3776 | WaitForEncodedFrame(2); |
| 3777 | EXPECT_THAT(fake_encoder_.LastFrameTypes(), |
Mirko Bonadei | 6a489f2 | 2019-04-09 15:11:12 +0200 | [diff] [blame] | 3778 | ::testing::ElementsAreArray({VideoFrameType::kVideoFrameDelta, |
| 3779 | VideoFrameType::kVideoFrameDelta, |
| 3780 | VideoFrameType::kVideoFrameDelta})); |
Erik Språng | d7329ca | 2019-02-21 21:19:53 +0100 | [diff] [blame] | 3781 | |
| 3782 | // Request next frame be a key-frame. |
| 3783 | // Only first stream is configured to produce key-frame. |
| 3784 | video_stream_encoder_->SendKeyFrame(); |
| 3785 | video_source_.IncomingCapturedFrame(CreateFrame(3, nullptr)); |
| 3786 | WaitForEncodedFrame(3); |
Sergey Silkin | e62a08a | 2019-05-13 13:45:39 +0200 | [diff] [blame] | 3787 | |
| 3788 | // TODO(webrtc:10615): Map keyframe request to spatial layer. Currently |
| 3789 | // keyframe request on any layer triggers keyframe on all layers. |
Erik Språng | d7329ca | 2019-02-21 21:19:53 +0100 | [diff] [blame] | 3790 | EXPECT_THAT(fake_encoder_.LastFrameTypes(), |
Mirko Bonadei | 6a489f2 | 2019-04-09 15:11:12 +0200 | [diff] [blame] | 3791 | ::testing::ElementsAreArray({VideoFrameType::kVideoFrameKey, |
Sergey Silkin | e62a08a | 2019-05-13 13:45:39 +0200 | [diff] [blame] | 3792 | VideoFrameType::kVideoFrameKey, |
| 3793 | VideoFrameType::kVideoFrameKey})); |
Erik Språng | d7329ca | 2019-02-21 21:19:53 +0100 | [diff] [blame] | 3794 | |
| 3795 | video_stream_encoder_->Stop(); |
| 3796 | } |
| 3797 | |
| 3798 | TEST_F(VideoStreamEncoderTest, RequestKeyframeInternalSource) { |
| 3799 | // Configure internal source factory and setup test again. |
| 3800 | encoder_factory_.SetHasInternalSource(true); |
| 3801 | ResetEncoder("VP8", 1, 1, 1, false); |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 3802 | video_stream_encoder_->OnBitrateUpdated( |
| 3803 | DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps), 0, 0); |
Erik Språng | d7329ca | 2019-02-21 21:19:53 +0100 | [diff] [blame] | 3804 | |
| 3805 | // Call encoder directly, simulating internal source where encoded frame |
| 3806 | // callback in VideoStreamEncoder is called despite no OnFrame(). |
| 3807 | fake_encoder_.InjectFrame(CreateFrame(1, nullptr), true); |
| 3808 | EXPECT_TRUE(WaitForFrame(kDefaultTimeoutMs)); |
Niels Möller | 8f7ce22 | 2019-03-21 15:43:58 +0100 | [diff] [blame] | 3809 | EXPECT_THAT( |
| 3810 | fake_encoder_.LastFrameTypes(), |
Mirko Bonadei | 6a489f2 | 2019-04-09 15:11:12 +0200 | [diff] [blame] | 3811 | ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameKey})); |
Erik Språng | d7329ca | 2019-02-21 21:19:53 +0100 | [diff] [blame] | 3812 | |
Niels Möller | 8f7ce22 | 2019-03-21 15:43:58 +0100 | [diff] [blame] | 3813 | const std::vector<VideoFrameType> kDeltaFrame = { |
| 3814 | VideoFrameType::kVideoFrameDelta}; |
Erik Språng | d7329ca | 2019-02-21 21:19:53 +0100 | [diff] [blame] | 3815 | // Need to set timestamp manually since manually for injected frame. |
| 3816 | VideoFrame frame = CreateFrame(101, nullptr); |
| 3817 | frame.set_timestamp(101); |
| 3818 | fake_encoder_.InjectFrame(frame, false); |
| 3819 | EXPECT_TRUE(WaitForFrame(kDefaultTimeoutMs)); |
Niels Möller | 8f7ce22 | 2019-03-21 15:43:58 +0100 | [diff] [blame] | 3820 | EXPECT_THAT( |
| 3821 | fake_encoder_.LastFrameTypes(), |
Mirko Bonadei | 6a489f2 | 2019-04-09 15:11:12 +0200 | [diff] [blame] | 3822 | ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameDelta})); |
Erik Språng | d7329ca | 2019-02-21 21:19:53 +0100 | [diff] [blame] | 3823 | |
| 3824 | // Request key-frame. The forces a dummy frame down into the encoder. |
| 3825 | fake_encoder_.ExpectNullFrame(); |
| 3826 | video_stream_encoder_->SendKeyFrame(); |
| 3827 | EXPECT_TRUE(WaitForFrame(kDefaultTimeoutMs)); |
Niels Möller | 8f7ce22 | 2019-03-21 15:43:58 +0100 | [diff] [blame] | 3828 | EXPECT_THAT( |
| 3829 | fake_encoder_.LastFrameTypes(), |
Mirko Bonadei | 6a489f2 | 2019-04-09 15:11:12 +0200 | [diff] [blame] | 3830 | ::testing::ElementsAre(VideoFrameType{VideoFrameType::kVideoFrameKey})); |
Erik Språng | d7329ca | 2019-02-21 21:19:53 +0100 | [diff] [blame] | 3831 | |
| 3832 | video_stream_encoder_->Stop(); |
| 3833 | } |
Erik Språng | b7cb7b5 | 2019-02-26 15:52:33 +0100 | [diff] [blame] | 3834 | |
| 3835 | TEST_F(VideoStreamEncoderTest, AdjustsTimestampInternalSource) { |
| 3836 | // Configure internal source factory and setup test again. |
| 3837 | encoder_factory_.SetHasInternalSource(true); |
| 3838 | ResetEncoder("VP8", 1, 1, 1, false); |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 3839 | video_stream_encoder_->OnBitrateUpdated( |
| 3840 | DataRate::bps(kTargetBitrateBps), DataRate::bps(kTargetBitrateBps), 0, 0); |
Erik Språng | b7cb7b5 | 2019-02-26 15:52:33 +0100 | [diff] [blame] | 3841 | |
| 3842 | int64_t timestamp = 1; |
| 3843 | EncodedImage image; |
| 3844 | image.Allocate(kTargetBitrateBps / kDefaultFramerate / 8); |
| 3845 | image.capture_time_ms_ = ++timestamp; |
| 3846 | image.SetTimestamp(static_cast<uint32_t>(timestamp * 90)); |
| 3847 | const int64_t kEncodeFinishDelayMs = 10; |
| 3848 | image.timing_.encode_start_ms = timestamp; |
| 3849 | image.timing_.encode_finish_ms = timestamp + kEncodeFinishDelayMs; |
| 3850 | fake_encoder_.InjectEncodedImage(image); |
| 3851 | // Wait for frame without incrementing clock. |
| 3852 | EXPECT_TRUE(sink_.WaitForFrame(kDefaultTimeoutMs)); |
| 3853 | // Frame is captured kEncodeFinishDelayMs before it's encoded, so restored |
| 3854 | // capture timestamp should be kEncodeFinishDelayMs in the past. |
| 3855 | EXPECT_EQ(sink_.GetLastCaptureTimeMs(), |
| 3856 | fake_clock_.TimeNanos() / rtc::kNumNanosecsPerMillisec - |
| 3857 | kEncodeFinishDelayMs); |
| 3858 | |
| 3859 | video_stream_encoder_->Stop(); |
| 3860 | } |
Mirta Dvornicic | 28f0eb2 | 2019-05-28 16:30:16 +0200 | [diff] [blame] | 3861 | |
| 3862 | TEST_F(VideoStreamEncoderTest, DoesNotRewriteH264BitstreamWithOptimalSps) { |
| 3863 | // Configure internal source factory and setup test again. |
| 3864 | encoder_factory_.SetHasInternalSource(true); |
| 3865 | ResetEncoder("H264", 1, 1, 1, false); |
| 3866 | |
| 3867 | EncodedImage image(optimal_sps, sizeof(optimal_sps), sizeof(optimal_sps)); |
| 3868 | image._frameType = VideoFrameType::kVideoFrameKey; |
| 3869 | |
| 3870 | CodecSpecificInfo codec_specific_info; |
| 3871 | codec_specific_info.codecType = kVideoCodecH264; |
| 3872 | |
| 3873 | RTPFragmentationHeader fragmentation; |
| 3874 | fragmentation.VerifyAndAllocateFragmentationHeader(1); |
| 3875 | fragmentation.fragmentationOffset[0] = 4; |
| 3876 | fragmentation.fragmentationLength[0] = sizeof(optimal_sps) - 4; |
| 3877 | |
| 3878 | fake_encoder_.InjectEncodedImage(image, &codec_specific_info, &fragmentation); |
| 3879 | EXPECT_TRUE(sink_.WaitForFrame(kDefaultTimeoutMs)); |
| 3880 | |
| 3881 | EXPECT_THAT(sink_.GetLastEncodedImageData(), |
| 3882 | testing::ElementsAreArray(optimal_sps)); |
| 3883 | RTPFragmentationHeader last_fragmentation = sink_.GetLastFragmentation(); |
| 3884 | ASSERT_THAT(last_fragmentation.fragmentationVectorSize, 1U); |
| 3885 | EXPECT_EQ(last_fragmentation.fragmentationOffset[0], 4U); |
| 3886 | EXPECT_EQ(last_fragmentation.fragmentationLength[0], sizeof(optimal_sps) - 4); |
| 3887 | |
| 3888 | video_stream_encoder_->Stop(); |
| 3889 | } |
| 3890 | |
| 3891 | TEST_F(VideoStreamEncoderTest, RewritesH264BitstreamWithNonOptimalSps) { |
| 3892 | uint8_t original_sps[] = {0, 0, 0, 1, H264::NaluType::kSps, |
| 3893 | 0x00, 0x00, 0x03, 0x03, 0xF4, |
| 3894 | 0x05, 0x03, 0xC7, 0xC0}; |
| 3895 | |
| 3896 | // Configure internal source factory and setup test again. |
| 3897 | encoder_factory_.SetHasInternalSource(true); |
| 3898 | ResetEncoder("H264", 1, 1, 1, false); |
| 3899 | |
| 3900 | EncodedImage image(original_sps, sizeof(original_sps), sizeof(original_sps)); |
| 3901 | image._frameType = VideoFrameType::kVideoFrameKey; |
| 3902 | |
| 3903 | CodecSpecificInfo codec_specific_info; |
| 3904 | codec_specific_info.codecType = kVideoCodecH264; |
| 3905 | |
| 3906 | RTPFragmentationHeader fragmentation; |
| 3907 | fragmentation.VerifyAndAllocateFragmentationHeader(1); |
| 3908 | fragmentation.fragmentationOffset[0] = 4; |
| 3909 | fragmentation.fragmentationLength[0] = sizeof(original_sps) - 4; |
| 3910 | |
| 3911 | fake_encoder_.InjectEncodedImage(image, &codec_specific_info, &fragmentation); |
| 3912 | EXPECT_TRUE(sink_.WaitForFrame(kDefaultTimeoutMs)); |
| 3913 | |
| 3914 | EXPECT_THAT(sink_.GetLastEncodedImageData(), |
| 3915 | testing::ElementsAreArray(optimal_sps)); |
| 3916 | RTPFragmentationHeader last_fragmentation = sink_.GetLastFragmentation(); |
| 3917 | ASSERT_THAT(last_fragmentation.fragmentationVectorSize, 1U); |
| 3918 | EXPECT_EQ(last_fragmentation.fragmentationOffset[0], 4U); |
| 3919 | EXPECT_EQ(last_fragmentation.fragmentationLength[0], sizeof(optimal_sps) - 4); |
| 3920 | |
| 3921 | video_stream_encoder_->Stop(); |
| 3922 | } |
| 3923 | |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 3924 | } // namespace webrtc |