| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "remoting/protocol/video_frame_pump.h" | 5 #include "remoting/protocol/video_frame_pump.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/macros.h" | 10 #include "base/macros.h" |
| 11 #include "base/memory/ptr_util.h" |
| 11 #include "base/message_loop/message_loop.h" | 12 #include "base/message_loop/message_loop.h" |
| 12 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
| 13 #include "base/single_thread_task_runner.h" | 14 #include "base/single_thread_task_runner.h" |
| 14 #include "remoting/base/auto_thread.h" | 15 #include "remoting/base/auto_thread.h" |
| 15 #include "remoting/base/auto_thread_task_runner.h" | 16 #include "remoting/base/auto_thread_task_runner.h" |
| 16 #include "remoting/codec/video_encoder.h" | 17 #include "remoting/codec/video_encoder.h" |
| 17 #include "remoting/codec/video_encoder_verbatim.h" | 18 #include "remoting/codec/video_encoder_verbatim.h" |
| 18 #include "remoting/proto/control.pb.h" | 19 #include "remoting/proto/control.pb.h" |
| 19 #include "remoting/proto/video.pb.h" | 20 #include "remoting/proto/video.pb.h" |
| 20 #include "remoting/protocol/fake_desktop_capturer.h" | 21 #include "remoting/protocol/fake_desktop_capturer.h" |
| (...skipping 12 matching lines...) Expand all Loading... |
| 33 | 34 |
| 34 namespace remoting { | 35 namespace remoting { |
| 35 namespace protocol { | 36 namespace protocol { |
| 36 | 37 |
| 37 namespace { | 38 namespace { |
| 38 | 39 |
| 39 ACTION(FinishSend) { | 40 ACTION(FinishSend) { |
| 40 arg1.Run(); | 41 arg1.Run(); |
| 41 } | 42 } |
| 42 | 43 |
| 43 scoped_ptr<webrtc::DesktopFrame> CreateNullFrame( | 44 std::unique_ptr<webrtc::DesktopFrame> CreateNullFrame( |
| 44 webrtc::SharedMemoryFactory* shared_memory_factory) { | 45 webrtc::SharedMemoryFactory* shared_memory_factory) { |
| 45 return nullptr; | 46 return nullptr; |
| 46 } | 47 } |
| 47 | 48 |
| 48 scoped_ptr<webrtc::DesktopFrame> CreateUnchangedFrame( | 49 std::unique_ptr<webrtc::DesktopFrame> CreateUnchangedFrame( |
| 49 webrtc::SharedMemoryFactory* shared_memory_factory) { | 50 webrtc::SharedMemoryFactory* shared_memory_factory) { |
| 50 const webrtc::DesktopSize kSize(800, 640); | 51 const webrtc::DesktopSize kSize(800, 640); |
| 51 // updated_region() is already empty by default in new BasicDesktopFrames. | 52 // updated_region() is already empty by default in new BasicDesktopFrames. |
| 52 return make_scoped_ptr(new webrtc::BasicDesktopFrame(kSize)); | 53 return base::WrapUnique(new webrtc::BasicDesktopFrame(kSize)); |
| 53 } | 54 } |
| 54 | 55 |
| 55 class MockVideoEncoder : public VideoEncoder { | 56 class MockVideoEncoder : public VideoEncoder { |
| 56 public: | 57 public: |
| 57 MockVideoEncoder() {} | 58 MockVideoEncoder() {} |
| 58 ~MockVideoEncoder() {} | 59 ~MockVideoEncoder() {} |
| 59 | 60 |
| 60 MOCK_METHOD1(SetLosslessEncode, void(bool)); | 61 MOCK_METHOD1(SetLosslessEncode, void(bool)); |
| 61 MOCK_METHOD1(SetLosslessColor, void(bool)); | 62 MOCK_METHOD1(SetLosslessColor, void(bool)); |
| 62 MOCK_METHOD1(EncodePtr, VideoPacket*(const webrtc::DesktopFrame&)); | 63 MOCK_METHOD1(EncodePtr, VideoPacket*(const webrtc::DesktopFrame&)); |
| 63 | 64 |
| 64 scoped_ptr<VideoPacket> Encode(const webrtc::DesktopFrame& frame) { | 65 std::unique_ptr<VideoPacket> Encode(const webrtc::DesktopFrame& frame) { |
| 65 return make_scoped_ptr(EncodePtr(frame)); | 66 return base::WrapUnique(EncodePtr(frame)); |
| 66 } | 67 } |
| 67 }; | 68 }; |
| 68 | 69 |
| 69 } // namespace | 70 } // namespace |
| 70 | 71 |
| 71 static const int kWidth = 640; | 72 static const int kWidth = 640; |
| 72 static const int kHeight = 480; | 73 static const int kHeight = 480; |
| 73 | 74 |
| 74 class ThreadCheckVideoEncoder : public VideoEncoderVerbatim { | 75 class ThreadCheckVideoEncoder : public VideoEncoderVerbatim { |
| 75 public: | 76 public: |
| 76 ThreadCheckVideoEncoder( | 77 ThreadCheckVideoEncoder( |
| 77 scoped_refptr<base::SingleThreadTaskRunner> task_runner) | 78 scoped_refptr<base::SingleThreadTaskRunner> task_runner) |
| 78 : task_runner_(task_runner) { | 79 : task_runner_(task_runner) { |
| 79 } | 80 } |
| 80 ~ThreadCheckVideoEncoder() override { | 81 ~ThreadCheckVideoEncoder() override { |
| 81 EXPECT_TRUE(task_runner_->BelongsToCurrentThread()); | 82 EXPECT_TRUE(task_runner_->BelongsToCurrentThread()); |
| 82 } | 83 } |
| 83 | 84 |
| 84 scoped_ptr<VideoPacket> Encode(const webrtc::DesktopFrame& frame) override { | 85 std::unique_ptr<VideoPacket> Encode( |
| 85 return make_scoped_ptr(new VideoPacket()); | 86 const webrtc::DesktopFrame& frame) override { |
| 87 return base::WrapUnique(new VideoPacket()); |
| 86 } | 88 } |
| 87 | 89 |
| 88 private: | 90 private: |
| 89 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | 91 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 90 | 92 |
| 91 DISALLOW_COPY_AND_ASSIGN(ThreadCheckVideoEncoder); | 93 DISALLOW_COPY_AND_ASSIGN(ThreadCheckVideoEncoder); |
| 92 }; | 94 }; |
| 93 | 95 |
| 94 class ThreadCheckDesktopCapturer : public webrtc::DesktopCapturer { | 96 class ThreadCheckDesktopCapturer : public webrtc::DesktopCapturer { |
| 95 public: | 97 public: |
| 96 ThreadCheckDesktopCapturer( | 98 ThreadCheckDesktopCapturer( |
| 97 scoped_refptr<base::SingleThreadTaskRunner> task_runner) | 99 scoped_refptr<base::SingleThreadTaskRunner> task_runner) |
| 98 : task_runner_(task_runner), callback_(nullptr) {} | 100 : task_runner_(task_runner), callback_(nullptr) {} |
| 99 ~ThreadCheckDesktopCapturer() override { | 101 ~ThreadCheckDesktopCapturer() override { |
| 100 EXPECT_TRUE(task_runner_->BelongsToCurrentThread()); | 102 EXPECT_TRUE(task_runner_->BelongsToCurrentThread()); |
| 101 } | 103 } |
| 102 | 104 |
| 103 void Start(Callback* callback) override { | 105 void Start(Callback* callback) override { |
| 104 EXPECT_TRUE(task_runner_->BelongsToCurrentThread()); | 106 EXPECT_TRUE(task_runner_->BelongsToCurrentThread()); |
| 105 EXPECT_FALSE(callback_); | 107 EXPECT_FALSE(callback_); |
| 106 EXPECT_TRUE(callback); | 108 EXPECT_TRUE(callback); |
| 107 | 109 |
| 108 callback_ = callback; | 110 callback_ = callback; |
| 109 } | 111 } |
| 110 | 112 |
| 111 void Capture(const webrtc::DesktopRegion& rect) override { | 113 void Capture(const webrtc::DesktopRegion& rect) override { |
| 112 EXPECT_TRUE(task_runner_->BelongsToCurrentThread()); | 114 EXPECT_TRUE(task_runner_->BelongsToCurrentThread()); |
| 113 | 115 |
| 114 scoped_ptr<webrtc::DesktopFrame> frame( | 116 std::unique_ptr<webrtc::DesktopFrame> frame( |
| 115 new webrtc::BasicDesktopFrame(webrtc::DesktopSize(kWidth, kHeight))); | 117 new webrtc::BasicDesktopFrame(webrtc::DesktopSize(kWidth, kHeight))); |
| 116 frame->mutable_updated_region()->SetRect( | 118 frame->mutable_updated_region()->SetRect( |
| 117 webrtc::DesktopRect::MakeXYWH(0, 0, 10, 10)); | 119 webrtc::DesktopRect::MakeXYWH(0, 0, 10, 10)); |
| 118 callback_->OnCaptureCompleted(frame.release()); | 120 callback_->OnCaptureCompleted(frame.release()); |
| 119 } | 121 } |
| 120 | 122 |
| 121 private: | 123 private: |
| 122 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | 124 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 123 webrtc::DesktopCapturer::Callback* callback_; | 125 webrtc::DesktopCapturer::Callback* callback_; |
| 124 | 126 |
| 125 DISALLOW_COPY_AND_ASSIGN(ThreadCheckDesktopCapturer); | 127 DISALLOW_COPY_AND_ASSIGN(ThreadCheckDesktopCapturer); |
| 126 }; | 128 }; |
| 127 | 129 |
| 128 class VideoFramePumpTest : public testing::Test { | 130 class VideoFramePumpTest : public testing::Test { |
| 129 public: | 131 public: |
| 130 void SetUp() override; | 132 void SetUp() override; |
| 131 void TearDown() override; | 133 void TearDown() override; |
| 132 | 134 |
| 133 void StartVideoFramePump( | 135 void StartVideoFramePump(std::unique_ptr<webrtc::DesktopCapturer> capturer, |
| 134 scoped_ptr<webrtc::DesktopCapturer> capturer, | 136 std::unique_ptr<VideoEncoder> encoder); |
| 135 scoped_ptr<VideoEncoder> encoder); | |
| 136 | 137 |
| 137 protected: | 138 protected: |
| 138 base::MessageLoop message_loop_; | 139 base::MessageLoop message_loop_; |
| 139 base::RunLoop run_loop_; | 140 base::RunLoop run_loop_; |
| 140 scoped_refptr<AutoThreadTaskRunner> encode_task_runner_; | 141 scoped_refptr<AutoThreadTaskRunner> encode_task_runner_; |
| 141 scoped_refptr<AutoThreadTaskRunner> main_task_runner_; | 142 scoped_refptr<AutoThreadTaskRunner> main_task_runner_; |
| 142 scoped_ptr<VideoFramePump> pump_; | 143 std::unique_ptr<VideoFramePump> pump_; |
| 143 | 144 |
| 144 MockVideoStub video_stub_; | 145 MockVideoStub video_stub_; |
| 145 }; | 146 }; |
| 146 | 147 |
| 147 void VideoFramePumpTest::SetUp() { | 148 void VideoFramePumpTest::SetUp() { |
| 148 main_task_runner_ = new AutoThreadTaskRunner( | 149 main_task_runner_ = new AutoThreadTaskRunner( |
| 149 message_loop_.task_runner(), run_loop_.QuitClosure()); | 150 message_loop_.task_runner(), run_loop_.QuitClosure()); |
| 150 encode_task_runner_ = AutoThread::Create("encode", main_task_runner_); | 151 encode_task_runner_ = AutoThread::Create("encode", main_task_runner_); |
| 151 } | 152 } |
| 152 | 153 |
| 153 void VideoFramePumpTest::TearDown() { | 154 void VideoFramePumpTest::TearDown() { |
| 154 pump_.reset(); | 155 pump_.reset(); |
| 155 | 156 |
| 156 // Release the task runners, so that the test can quit. | 157 // Release the task runners, so that the test can quit. |
| 157 encode_task_runner_ = nullptr; | 158 encode_task_runner_ = nullptr; |
| 158 main_task_runner_ = nullptr; | 159 main_task_runner_ = nullptr; |
| 159 | 160 |
| 160 // Run the MessageLoop until everything has torn down. | 161 // Run the MessageLoop until everything has torn down. |
| 161 run_loop_.Run(); | 162 run_loop_.Run(); |
| 162 } | 163 } |
| 163 | 164 |
| 164 // This test mocks capturer, encoder and network layer to simulate one capture | 165 // This test mocks capturer, encoder and network layer to simulate one capture |
| 165 // cycle. | 166 // cycle. |
| 166 TEST_F(VideoFramePumpTest, StartAndStop) { | 167 TEST_F(VideoFramePumpTest, StartAndStop) { |
| 167 scoped_ptr<ThreadCheckDesktopCapturer> capturer( | 168 std::unique_ptr<ThreadCheckDesktopCapturer> capturer( |
| 168 new ThreadCheckDesktopCapturer(main_task_runner_)); | 169 new ThreadCheckDesktopCapturer(main_task_runner_)); |
| 169 scoped_ptr<ThreadCheckVideoEncoder> encoder( | 170 std::unique_ptr<ThreadCheckVideoEncoder> encoder( |
| 170 new ThreadCheckVideoEncoder(encode_task_runner_)); | 171 new ThreadCheckVideoEncoder(encode_task_runner_)); |
| 171 | 172 |
| 172 base::RunLoop run_loop; | 173 base::RunLoop run_loop; |
| 173 | 174 |
| 174 // When the first ProcessVideoPacket is received we stop the VideoFramePump. | 175 // When the first ProcessVideoPacket is received we stop the VideoFramePump. |
| 175 EXPECT_CALL(video_stub_, ProcessVideoPacketPtr(_, _)) | 176 EXPECT_CALL(video_stub_, ProcessVideoPacketPtr(_, _)) |
| 176 .WillOnce(DoAll(FinishSend(), | 177 .WillOnce(DoAll(FinishSend(), |
| 177 InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit))) | 178 InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit))) |
| 178 .RetiresOnSaturation(); | 179 .RetiresOnSaturation(); |
| 179 | 180 |
| 180 // Start video frame capture. | 181 // Start video frame capture. |
| 181 pump_.reset(new VideoFramePump(encode_task_runner_, std::move(capturer), | 182 pump_.reset(new VideoFramePump(encode_task_runner_, std::move(capturer), |
| 182 std::move(encoder), &video_stub_)); | 183 std::move(encoder), &video_stub_)); |
| 183 | 184 |
| 184 // Run MessageLoop until the first frame is received. | 185 // Run MessageLoop until the first frame is received. |
| 185 run_loop.Run(); | 186 run_loop.Run(); |
| 186 } | 187 } |
| 187 | 188 |
| 188 // Tests that the pump handles null frames returned by the capturer. | 189 // Tests that the pump handles null frames returned by the capturer. |
| 189 TEST_F(VideoFramePumpTest, NullFrame) { | 190 TEST_F(VideoFramePumpTest, NullFrame) { |
| 190 scoped_ptr<FakeDesktopCapturer> capturer(new FakeDesktopCapturer); | 191 std::unique_ptr<FakeDesktopCapturer> capturer(new FakeDesktopCapturer); |
| 191 scoped_ptr<MockVideoEncoder> encoder(new MockVideoEncoder); | 192 std::unique_ptr<MockVideoEncoder> encoder(new MockVideoEncoder); |
| 192 | 193 |
| 193 base::RunLoop run_loop; | 194 base::RunLoop run_loop; |
| 194 | 195 |
| 195 // Set up the capturer to return null frames. | 196 // Set up the capturer to return null frames. |
| 196 capturer->set_frame_generator(base::Bind(&CreateNullFrame)); | 197 capturer->set_frame_generator(base::Bind(&CreateNullFrame)); |
| 197 | 198 |
| 198 // Expect that the VideoEncoder::Encode() method is never called. | 199 // Expect that the VideoEncoder::Encode() method is never called. |
| 199 EXPECT_CALL(*encoder, EncodePtr(_)).Times(0); | 200 EXPECT_CALL(*encoder, EncodePtr(_)).Times(0); |
| 200 | 201 |
| 201 // When the first ProcessVideoPacket is received we stop the VideoFramePump. | 202 // When the first ProcessVideoPacket is received we stop the VideoFramePump. |
| 202 EXPECT_CALL(video_stub_, ProcessVideoPacketPtr(_, _)) | 203 EXPECT_CALL(video_stub_, ProcessVideoPacketPtr(_, _)) |
| 203 .WillOnce(DoAll(FinishSend(), | 204 .WillOnce(DoAll(FinishSend(), |
| 204 InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit))) | 205 InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit))) |
| 205 .RetiresOnSaturation(); | 206 .RetiresOnSaturation(); |
| 206 | 207 |
| 207 // Start video frame capture. | 208 // Start video frame capture. |
| 208 pump_.reset(new VideoFramePump(encode_task_runner_, std::move(capturer), | 209 pump_.reset(new VideoFramePump(encode_task_runner_, std::move(capturer), |
| 209 std::move(encoder), &video_stub_)); | 210 std::move(encoder), &video_stub_)); |
| 210 | 211 |
| 211 // Run MessageLoop until the first frame is received.. | 212 // Run MessageLoop until the first frame is received.. |
| 212 run_loop.Run(); | 213 run_loop.Run(); |
| 213 } | 214 } |
| 214 | 215 |
| 215 // Tests how the pump handles unchanged frames returned by the capturer. | 216 // Tests how the pump handles unchanged frames returned by the capturer. |
| 216 TEST_F(VideoFramePumpTest, UnchangedFrame) { | 217 TEST_F(VideoFramePumpTest, UnchangedFrame) { |
| 217 scoped_ptr<FakeDesktopCapturer> capturer(new FakeDesktopCapturer); | 218 std::unique_ptr<FakeDesktopCapturer> capturer(new FakeDesktopCapturer); |
| 218 scoped_ptr<MockVideoEncoder> encoder(new MockVideoEncoder); | 219 std::unique_ptr<MockVideoEncoder> encoder(new MockVideoEncoder); |
| 219 | 220 |
| 220 base::RunLoop run_loop; | 221 base::RunLoop run_loop; |
| 221 | 222 |
| 222 // Set up the capturer to return unchanged frames. | 223 // Set up the capturer to return unchanged frames. |
| 223 capturer->set_frame_generator(base::Bind(&CreateUnchangedFrame)); | 224 capturer->set_frame_generator(base::Bind(&CreateUnchangedFrame)); |
| 224 | 225 |
| 225 // Expect that the VideoEncoder::Encode() method is called. | 226 // Expect that the VideoEncoder::Encode() method is called. |
| 226 EXPECT_CALL(*encoder, EncodePtr(_)).WillRepeatedly(Return(nullptr)); | 227 EXPECT_CALL(*encoder, EncodePtr(_)).WillRepeatedly(Return(nullptr)); |
| 227 | 228 |
| 228 // When the first ProcessVideoPacket is received we stop the VideoFramePump. | 229 // When the first ProcessVideoPacket is received we stop the VideoFramePump. |
| 229 // TODO(wez): Verify that the generated packet has no content here. | 230 // TODO(wez): Verify that the generated packet has no content here. |
| 230 EXPECT_CALL(video_stub_, ProcessVideoPacketPtr(_, _)) | 231 EXPECT_CALL(video_stub_, ProcessVideoPacketPtr(_, _)) |
| 231 .WillOnce(DoAll(FinishSend(), | 232 .WillOnce(DoAll(FinishSend(), |
| 232 InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit))) | 233 InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit))) |
| 233 .RetiresOnSaturation(); | 234 .RetiresOnSaturation(); |
| 234 | 235 |
| 235 // Start video frame capture. | 236 // Start video frame capture. |
| 236 pump_.reset(new VideoFramePump(encode_task_runner_, std::move(capturer), | 237 pump_.reset(new VideoFramePump(encode_task_runner_, std::move(capturer), |
| 237 std::move(encoder), &video_stub_)); | 238 std::move(encoder), &video_stub_)); |
| 238 | 239 |
| 239 // Run MessageLoop until the first frame is received. | 240 // Run MessageLoop until the first frame is received. |
| 240 run_loop.Run(); | 241 run_loop.Run(); |
| 241 } | 242 } |
| 242 | 243 |
| 243 } // namespace protocol | 244 } // namespace protocol |
| 244 } // namespace remoting | 245 } // namespace remoting |
| OLD | NEW |