software_video_renderer_unittest.cc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // Copyright 2015 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "remoting/client/software_video_renderer.h"
  5. #include <stdint.h>
  6. #include <memory>
  7. #include <utility>
  8. #include <vector>
  9. #include "base/bind.h"
  10. #include "base/run_loop.h"
  11. #include "base/test/task_environment.h"
  12. #include "base/threading/thread.h"
  13. #include "remoting/client/client_context.h"
  14. #include "remoting/codec/video_encoder_verbatim.h"
  15. #include "remoting/proto/video.pb.h"
  16. #include "remoting/protocol/frame_consumer.h"
  17. #include "remoting/protocol/session_config.h"
  18. #include "testing/gtest/include/gtest/gtest.h"
  19. #include "third_party/webrtc/modules/desktop_capture/desktop_frame.h"
  20. using webrtc::DesktopFrame;
  21. namespace remoting {
  22. namespace {
  23. const int kFrameWidth = 200;
  24. const int kFrameHeight = 200;
  25. class TestFrameConsumer : public protocol::FrameConsumer {
  26. public:
  27. TestFrameConsumer() = default;
  28. ~TestFrameConsumer() override = default;
  29. std::unique_ptr<DesktopFrame> WaitForNextFrame(
  30. base::OnceClosure* out_done_callback) {
  31. EXPECT_TRUE(thread_checker_.CalledOnValidThread());
  32. frame_run_loop_ = std::make_unique<base::RunLoop>();
  33. frame_run_loop_->Run();
  34. frame_run_loop_.reset();
  35. *out_done_callback = std::move(last_frame_done_callback_);
  36. return std::move(last_frame_);
  37. }
  38. // FrameConsumer interface.
  39. std::unique_ptr<DesktopFrame> AllocateFrame(
  40. const webrtc::DesktopSize& size) override {
  41. EXPECT_TRUE(thread_checker_.CalledOnValidThread());
  42. return std::make_unique<webrtc::BasicDesktopFrame>(size);
  43. }
  44. void DrawFrame(std::unique_ptr<DesktopFrame> frame,
  45. base::OnceClosure done) override {
  46. EXPECT_TRUE(thread_checker_.CalledOnValidThread());
  47. last_frame_ = std::move(frame);
  48. last_frame_done_callback_ = std::move(done);
  49. frame_run_loop_->Quit();
  50. }
  51. PixelFormat GetPixelFormat() override {
  52. EXPECT_TRUE(thread_checker_.CalledOnValidThread());
  53. return FORMAT_BGRA;
  54. }
  55. private:
  56. base::ThreadChecker thread_checker_;
  57. std::unique_ptr<base::RunLoop> frame_run_loop_;
  58. std::unique_ptr<DesktopFrame> last_frame_;
  59. base::OnceClosure last_frame_done_callback_;
  60. };
  61. std::unique_ptr<DesktopFrame> CreateTestFrame(int index) {
  62. std::unique_ptr<DesktopFrame> frame(new webrtc::BasicDesktopFrame(
  63. webrtc::DesktopSize(kFrameWidth, kFrameHeight)));
  64. for (int y = 0; y < kFrameHeight; y++) {
  65. for (int x = 0; x < kFrameWidth; x++) {
  66. uint8_t* out = frame->data() + x * DesktopFrame::kBytesPerPixel +
  67. y * frame->stride();
  68. out[0] = index + x + y * kFrameWidth;
  69. out[1] = index + x + y * kFrameWidth + 1;
  70. out[2] = index + x + y * kFrameWidth + 2;
  71. out[3] = 0;
  72. }
  73. }
  74. if (index == 0) {
  75. frame->mutable_updated_region()->SetRect(
  76. webrtc::DesktopRect::MakeWH(kFrameWidth, kFrameHeight));
  77. } else {
  78. frame->mutable_updated_region()->SetRect(
  79. webrtc::DesktopRect::MakeWH(index, index));
  80. }
  81. return frame;
  82. }
  83. // Returns true when frames a and b are equivalent.
  84. bool CompareFrames(const DesktopFrame& a, const DesktopFrame& b) {
  85. if (!a.size().equals(b.size()) ||
  86. !a.updated_region().Equals(b.updated_region())) {
  87. return false;
  88. }
  89. for (webrtc::DesktopRegion::Iterator i(a.updated_region()); !i.IsAtEnd();
  90. i.Advance()) {
  91. for (int row = i.rect().top(); row < i.rect().bottom(); ++row) {
  92. if (memcmp(a.data() + a.stride() * row +
  93. i.rect().left() * DesktopFrame::kBytesPerPixel,
  94. b.data() + b.stride() * row +
  95. i.rect().left() * DesktopFrame::kBytesPerPixel,
  96. i.rect().width() * DesktopFrame::kBytesPerPixel) != 0) {
  97. return false;
  98. }
  99. }
  100. }
  101. return true;
  102. }
  103. // Helper to set value at |out| to 1.
  104. void SetTrue(int* out) {
  105. *out = 1;
  106. }
  107. } // namespace
  108. class SoftwareVideoRendererTest : public ::testing::Test {
  109. public:
  110. SoftwareVideoRendererTest() : context_(nullptr) {
  111. context_.Start();
  112. renderer_ = std::make_unique<SoftwareVideoRenderer>(&frame_consumer_);
  113. renderer_->Initialize(context_, nullptr);
  114. renderer_->OnSessionConfig(
  115. *protocol::SessionConfig::ForTestWithVerbatimVideo());
  116. }
  117. protected:
  118. base::test::SingleThreadTaskEnvironment task_environment_;
  119. ClientContext context_;
  120. TestFrameConsumer frame_consumer_;
  121. std::unique_ptr<SoftwareVideoRenderer> renderer_;
  122. VideoEncoderVerbatim encoder_;
  123. };
  124. TEST_F(SoftwareVideoRendererTest, DecodeFrame) {
  125. const int kFrameCount = 5;
  126. std::vector<std::unique_ptr<DesktopFrame>> test_frames;
  127. // std::vector<bool> doesn't allow to get pointer to individual values, so
  128. // int needs to be used instead.
  129. std::vector<int> callback_called(kFrameCount);
  130. for (int frame_index = 0; frame_index < kFrameCount; frame_index++) {
  131. test_frames.push_back(CreateTestFrame(frame_index));
  132. callback_called[frame_index] = 0;
  133. renderer_->ProcessVideoPacket(
  134. encoder_.Encode(*test_frames[frame_index]),
  135. base::BindOnce(&SetTrue, &(callback_called[frame_index])));
  136. }
  137. for (int frame_index = 0; frame_index < kFrameCount; frame_index++) {
  138. base::OnceClosure done_callback;
  139. std::unique_ptr<DesktopFrame> decoded_frame =
  140. frame_consumer_.WaitForNextFrame(&done_callback);
  141. EXPECT_FALSE(callback_called[frame_index]);
  142. std::move(done_callback).Run();
  143. EXPECT_TRUE(callback_called[frame_index]);
  144. EXPECT_TRUE(CompareFrames(*test_frames[frame_index], *decoded_frame));
  145. }
  146. }
  147. } // namespace remoting