Avi Drissman | d6cdf9b | 2022-09-15 19:52:53 | [diff] [blame] | 1 | // Copyright 2015 The Chromium Authors |
sergeyu | 13bca69 | 2015-08-20 02:02:18 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "remoting/client/software_video_renderer.h" |
| 6 | |
avi | 5a080f01 | 2015-12-22 23:15:43 | [diff] [blame] | 7 | #include <stdint.h> |
| 8 | |
avi | 954c9c1 | 2017-01-11 16:52:37 | [diff] [blame] | 9 | #include <memory> |
sergeyu | 42ad7c0 | 2015-12-24 00:20:51 | [diff] [blame] | 10 | #include <utility> |
sergeyu | 13bca69 | 2015-08-20 02:02:18 | [diff] [blame] | 11 | #include <vector> |
| 12 | |
Avi Drissman | 135261e | 2023-01-11 22:43:15 | [diff] [blame] | 13 | #include "base/functional/bind.h" |
sergeyu | 13bca69 | 2015-08-20 02:02:18 | [diff] [blame] | 14 | #include "base/run_loop.h" |
Gabriel Charette | c710874 | 2019-08-23 03:31:40 | [diff] [blame] | 15 | #include "base/test/task_environment.h" |
sergeyu | 13bca69 | 2015-08-20 02:02:18 | [diff] [blame] | 16 | #include "base/threading/thread.h" |
yuweih | 6d9bb4a | 2016-07-22 19:45:08 | [diff] [blame] | 17 | #include "remoting/client/client_context.h" |
sergeyu | 13bca69 | 2015-08-20 02:02:18 | [diff] [blame] | 18 | #include "remoting/codec/video_encoder_verbatim.h" |
| 19 | #include "remoting/proto/video.pb.h" |
sergeyu | 480531b | 2016-01-05 19:12:42 | [diff] [blame] | 20 | #include "remoting/protocol/frame_consumer.h" |
sergeyu | 13bca69 | 2015-08-20 02:02:18 | [diff] [blame] | 21 | #include "remoting/protocol/session_config.h" |
| 22 | #include "testing/gtest/include/gtest/gtest.h" |
| 23 | #include "third_party/webrtc/modules/desktop_capture/desktop_frame.h" |
| 24 | |
| 25 | using webrtc::DesktopFrame; |
| 26 | |
| 27 | namespace remoting { |
| 28 | |
| 29 | namespace { |
| 30 | |
| 31 | const int kFrameWidth = 200; |
| 32 | const int kFrameHeight = 200; |
| 33 | |
sergeyu | 480531b | 2016-01-05 19:12:42 | [diff] [blame] | 34 | class TestFrameConsumer : public protocol::FrameConsumer { |
sergeyu | 13bca69 | 2015-08-20 02:02:18 | [diff] [blame] | 35 | public: |
Chris Watkins | 6fe52aa | 2017-11-28 03:24:05 | [diff] [blame] | 36 | TestFrameConsumer() = default; |
| 37 | ~TestFrameConsumer() override = default; |
sergeyu | 13bca69 | 2015-08-20 02:02:18 | [diff] [blame] | 38 | |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 39 | std::unique_ptr<DesktopFrame> WaitForNextFrame( |
Joey Scarr | 00b7c1f | 2020-02-13 03:34:03 | [diff] [blame] | 40 | base::OnceClosure* out_done_callback) { |
sergeyu | 13bca69 | 2015-08-20 02:02:18 | [diff] [blame] | 41 | EXPECT_TRUE(thread_checker_.CalledOnValidThread()); |
Peter Boström | 42afa23f | 2021-04-02 22:10:46 | [diff] [blame] | 42 | frame_run_loop_ = std::make_unique<base::RunLoop>(); |
sergeyu | 13bca69 | 2015-08-20 02:02:18 | [diff] [blame] | 43 | frame_run_loop_->Run(); |
| 44 | frame_run_loop_.reset(); |
Joey Scarr | 00b7c1f | 2020-02-13 03:34:03 | [diff] [blame] | 45 | *out_done_callback = std::move(last_frame_done_callback_); |
sergeyu | 42ad7c0 | 2015-12-24 00:20:51 | [diff] [blame] | 46 | return std::move(last_frame_); |
sergeyu | 13bca69 | 2015-08-20 02:02:18 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | // FrameConsumer interface. |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 50 | std::unique_ptr<DesktopFrame> AllocateFrame( |
sergeyu | 13bca69 | 2015-08-20 02:02:18 | [diff] [blame] | 51 | const webrtc::DesktopSize& size) override { |
| 52 | EXPECT_TRUE(thread_checker_.CalledOnValidThread()); |
Jinho Bang | 138fde3 | 2018-01-18 23:13:42 | [diff] [blame] | 53 | return std::make_unique<webrtc::BasicDesktopFrame>(size); |
sergeyu | 13bca69 | 2015-08-20 02:02:18 | [diff] [blame] | 54 | } |
| 55 | |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 56 | void DrawFrame(std::unique_ptr<DesktopFrame> frame, |
Joey Scarr | 00b7c1f | 2020-02-13 03:34:03 | [diff] [blame] | 57 | base::OnceClosure done) override { |
sergeyu | 13bca69 | 2015-08-20 02:02:18 | [diff] [blame] | 58 | EXPECT_TRUE(thread_checker_.CalledOnValidThread()); |
sergeyu | 42ad7c0 | 2015-12-24 00:20:51 | [diff] [blame] | 59 | last_frame_ = std::move(frame); |
Joey Scarr | 00b7c1f | 2020-02-13 03:34:03 | [diff] [blame] | 60 | last_frame_done_callback_ = std::move(done); |
sergeyu | 13bca69 | 2015-08-20 02:02:18 | [diff] [blame] | 61 | frame_run_loop_->Quit(); |
| 62 | } |
| 63 | |
| 64 | PixelFormat GetPixelFormat() override { |
| 65 | EXPECT_TRUE(thread_checker_.CalledOnValidThread()); |
| 66 | return FORMAT_BGRA; |
| 67 | } |
| 68 | |
| 69 | private: |
| 70 | base::ThreadChecker thread_checker_; |
| 71 | |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 72 | std::unique_ptr<base::RunLoop> frame_run_loop_; |
sergeyu | 13bca69 | 2015-08-20 02:02:18 | [diff] [blame] | 73 | |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 74 | std::unique_ptr<DesktopFrame> last_frame_; |
Joey Scarr | 00b7c1f | 2020-02-13 03:34:03 | [diff] [blame] | 75 | base::OnceClosure last_frame_done_callback_; |
sergeyu | 13bca69 | 2015-08-20 02:02:18 | [diff] [blame] | 76 | }; |
| 77 | |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 78 | std::unique_ptr<DesktopFrame> CreateTestFrame(int index) { |
| 79 | std::unique_ptr<DesktopFrame> frame(new webrtc::BasicDesktopFrame( |
sergeyu | 13bca69 | 2015-08-20 02:02:18 | [diff] [blame] | 80 | webrtc::DesktopSize(kFrameWidth, kFrameHeight))); |
| 81 | |
| 82 | for (int y = 0; y < kFrameHeight; y++) { |
| 83 | for (int x = 0; x < kFrameWidth; x++) { |
| 84 | uint8_t* out = frame->data() + x * DesktopFrame::kBytesPerPixel + |
| 85 | y * frame->stride(); |
| 86 | out[0] = index + x + y * kFrameWidth; |
| 87 | out[1] = index + x + y * kFrameWidth + 1; |
| 88 | out[2] = index + x + y * kFrameWidth + 2; |
| 89 | out[3] = 0; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | if (index == 0) { |
| 94 | frame->mutable_updated_region()->SetRect( |
| 95 | webrtc::DesktopRect::MakeWH(kFrameWidth, kFrameHeight)); |
| 96 | } else { |
| 97 | frame->mutable_updated_region()->SetRect( |
| 98 | webrtc::DesktopRect::MakeWH(index, index)); |
| 99 | } |
| 100 | |
sergeyu | 42ad7c0 | 2015-12-24 00:20:51 | [diff] [blame] | 101 | return frame; |
sergeyu | 13bca69 | 2015-08-20 02:02:18 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | // Returns true when frames a and b are equivalent. |
| 105 | bool CompareFrames(const DesktopFrame& a, const DesktopFrame& b) { |
| 106 | if (!a.size().equals(b.size()) || |
| 107 | !a.updated_region().Equals(b.updated_region())) { |
| 108 | return false; |
| 109 | } |
| 110 | |
| 111 | for (webrtc::DesktopRegion::Iterator i(a.updated_region()); !i.IsAtEnd(); |
| 112 | i.Advance()) { |
| 113 | for (int row = i.rect().top(); row < i.rect().bottom(); ++row) { |
| 114 | if (memcmp(a.data() + a.stride() * row + |
| 115 | i.rect().left() * DesktopFrame::kBytesPerPixel, |
| 116 | b.data() + b.stride() * row + |
| 117 | i.rect().left() * DesktopFrame::kBytesPerPixel, |
| 118 | i.rect().width() * DesktopFrame::kBytesPerPixel) != 0) { |
| 119 | return false; |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | return true; |
| 125 | } |
| 126 | |
| 127 | // Helper to set value at |out| to 1. |
| 128 | void SetTrue(int* out) { |
| 129 | *out = 1; |
| 130 | } |
| 131 | |
| 132 | } // namespace |
| 133 | |
| 134 | class SoftwareVideoRendererTest : public ::testing::Test { |
| 135 | public: |
yuweih | 6d9bb4a | 2016-07-22 19:45:08 | [diff] [blame] | 136 | SoftwareVideoRendererTest() : context_(nullptr) { |
| 137 | context_.Start(); |
Peter Boström | 42afa23f | 2021-04-02 22:10:46 | [diff] [blame] | 138 | renderer_ = std::make_unique<SoftwareVideoRenderer>(&frame_consumer_); |
yuweih | 6d9bb4a | 2016-07-22 19:45:08 | [diff] [blame] | 139 | renderer_->Initialize(context_, nullptr); |
sergeyu | 13bca69 | 2015-08-20 02:02:18 | [diff] [blame] | 140 | renderer_->OnSessionConfig( |
| 141 | *protocol::SessionConfig::ForTestWithVerbatimVideo()); |
| 142 | } |
| 143 | |
| 144 | protected: |
Gabriel Charette | df0a744 | 2019-09-05 17:32:35 | [diff] [blame] | 145 | base::test::SingleThreadTaskEnvironment task_environment_; |
yuweih | 6d9bb4a | 2016-07-22 19:45:08 | [diff] [blame] | 146 | ClientContext context_; |
sergeyu | 13bca69 | 2015-08-20 02:02:18 | [diff] [blame] | 147 | |
| 148 | TestFrameConsumer frame_consumer_; |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 149 | std::unique_ptr<SoftwareVideoRenderer> renderer_; |
sergeyu | 13bca69 | 2015-08-20 02:02:18 | [diff] [blame] | 150 | |
| 151 | VideoEncoderVerbatim encoder_; |
| 152 | }; |
| 153 | |
| 154 | TEST_F(SoftwareVideoRendererTest, DecodeFrame) { |
| 155 | const int kFrameCount = 5; |
| 156 | |
avi | 954c9c1 | 2017-01-11 16:52:37 | [diff] [blame] | 157 | std::vector<std::unique_ptr<DesktopFrame>> test_frames; |
sergeyu | 13bca69 | 2015-08-20 02:02:18 | [diff] [blame] | 158 | |
| 159 | // std::vector<bool> doesn't allow to get pointer to individual values, so |
| 160 | // int needs to be used instead. |
| 161 | std::vector<int> callback_called(kFrameCount); |
| 162 | |
| 163 | for (int frame_index = 0; frame_index < kFrameCount; frame_index++) { |
| 164 | test_frames.push_back(CreateTestFrame(frame_index)); |
| 165 | callback_called[frame_index] = 0; |
| 166 | |
| 167 | renderer_->ProcessVideoPacket( |
sergeyu | 2becb601 | 2016-09-13 18:41:32 | [diff] [blame] | 168 | encoder_.Encode(*test_frames[frame_index]), |
Evan Stade | b36ca0a5 | 2020-03-12 15:56:52 | [diff] [blame] | 169 | base::BindOnce(&SetTrue, &(callback_called[frame_index]))); |
sergeyu | 13bca69 | 2015-08-20 02:02:18 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | for (int frame_index = 0; frame_index < kFrameCount; frame_index++) { |
Joey Scarr | 00b7c1f | 2020-02-13 03:34:03 | [diff] [blame] | 173 | base::OnceClosure done_callback; |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 174 | std::unique_ptr<DesktopFrame> decoded_frame = |
sergeyu | 13bca69 | 2015-08-20 02:02:18 | [diff] [blame] | 175 | frame_consumer_.WaitForNextFrame(&done_callback); |
| 176 | |
| 177 | EXPECT_FALSE(callback_called[frame_index]); |
Joey Scarr | 00b7c1f | 2020-02-13 03:34:03 | [diff] [blame] | 178 | std::move(done_callback).Run(); |
sergeyu | 13bca69 | 2015-08-20 02:02:18 | [diff] [blame] | 179 | EXPECT_TRUE(callback_called[frame_index]); |
| 180 | |
| 181 | EXPECT_TRUE(CompareFrames(*test_frames[frame_index], *decoded_frame)); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | } // namespace remoting |