| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/host/video_scheduler.h" | 5 #include "remoting/host/video_scheduler.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/message_loop.h" | 8 #include "base/message_loop.h" |
| 9 #include "base/run_loop.h" | 9 #include "base/run_loop.h" |
| 10 #include "media/video/capture/screen/screen_capture_data.h" | |
| 11 #include "media/video/capture/screen/screen_capturer_mock_objects.h" | 10 #include "media/video/capture/screen/screen_capturer_mock_objects.h" |
| 12 #include "remoting/base/auto_thread_task_runner.h" | 11 #include "remoting/base/auto_thread_task_runner.h" |
| 13 #include "remoting/codec/video_encoder.h" | 12 #include "remoting/codec/video_encoder.h" |
| 14 #include "remoting/proto/video.pb.h" | 13 #include "remoting/proto/video.pb.h" |
| 15 #include "remoting/protocol/protocol_mock_objects.h" | 14 #include "remoting/protocol/protocol_mock_objects.h" |
| 16 #include "testing/gmock/include/gmock/gmock.h" | 15 #include "testing/gmock/include/gmock/gmock.h" |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
| 17 #include "third_party/webrtc/modules/desktop_capture/desktop_frame.h" |
| 18 | 18 |
| 19 using ::remoting::protocol::MockClientStub; | 19 using ::remoting::protocol::MockClientStub; |
| 20 using ::remoting::protocol::MockVideoStub; | 20 using ::remoting::protocol::MockVideoStub; |
| 21 | 21 |
| 22 using ::testing::_; | 22 using ::testing::_; |
| 23 using ::testing::AtLeast; | 23 using ::testing::AtLeast; |
| 24 using ::testing::AnyNumber; | 24 using ::testing::AnyNumber; |
| 25 using ::testing::DeleteArg; | 25 using ::testing::DeleteArg; |
| 26 using ::testing::DoAll; | 26 using ::testing::DoAll; |
| 27 using ::testing::Expectation; | 27 using ::testing::Expectation; |
| 28 using ::testing::InSequence; | 28 using ::testing::InSequence; |
| 29 using ::testing::InvokeWithoutArgs; | 29 using ::testing::InvokeWithoutArgs; |
| 30 using ::testing::Return; | 30 using ::testing::Return; |
| 31 using ::testing::ReturnRef; | 31 using ::testing::ReturnRef; |
| 32 using ::testing::SaveArg; | 32 using ::testing::SaveArg; |
| 33 | 33 |
| 34 namespace remoting { | 34 namespace remoting { |
| 35 | 35 |
| 36 namespace { | 36 namespace { |
| 37 | 37 |
| 38 ACTION(FinishEncode) { | 38 ACTION(FinishEncode) { |
| 39 scoped_ptr<VideoPacket> packet(new VideoPacket()); | 39 scoped_ptr<VideoPacket> packet(new VideoPacket()); |
| 40 packet->set_flags(VideoPacket::LAST_PACKET | VideoPacket::LAST_PARTITION); | 40 packet->set_flags(VideoPacket::LAST_PACKET | VideoPacket::LAST_PARTITION); |
| 41 arg2.Run(packet.Pass()); | 41 arg1.Run(packet.Pass()); |
| 42 } | 42 } |
| 43 | 43 |
| 44 ACTION(FinishSend) { | 44 ACTION(FinishSend) { |
| 45 arg1.Run(); | 45 arg1.Run(); |
| 46 } | 46 } |
| 47 | 47 |
| 48 } // namespace | 48 } // namespace |
| 49 | 49 |
| 50 static const int kWidth = 640; | 50 static const int kWidth = 640; |
| 51 static const int kHeight = 480; | 51 static const int kHeight = 480; |
| 52 | 52 |
| 53 class MockVideoEncoder : public VideoEncoder { | 53 class MockVideoEncoder : public VideoEncoder { |
| 54 public: | 54 public: |
| 55 MockVideoEncoder(); | 55 MockVideoEncoder(); |
| 56 virtual ~MockVideoEncoder(); | 56 virtual ~MockVideoEncoder(); |
| 57 | 57 |
| 58 MOCK_METHOD3(Encode, void( | 58 MOCK_METHOD2(Encode, void( |
| 59 scoped_refptr<media::ScreenCaptureData> capture_data, | 59 webrtc::DesktopFrame* frame, |
| 60 bool key_frame, | |
| 61 const DataAvailableCallback& data_available_callback)); | 60 const DataAvailableCallback& data_available_callback)); |
| 62 | 61 |
| 63 private: | 62 private: |
| 64 DISALLOW_COPY_AND_ASSIGN(MockVideoEncoder); | 63 DISALLOW_COPY_AND_ASSIGN(MockVideoEncoder); |
| 65 }; | 64 }; |
| 66 | 65 |
| 67 MockVideoEncoder::MockVideoEncoder() {} | 66 MockVideoEncoder::MockVideoEncoder() {} |
| 68 | 67 |
| 69 MockVideoEncoder::~MockVideoEncoder() {} | 68 MockVideoEncoder::~MockVideoEncoder() {} |
| 70 | 69 |
| 71 class VideoSchedulerTest : public testing::Test { | 70 class VideoSchedulerTest : public testing::Test { |
| 72 public: | 71 public: |
| 73 VideoSchedulerTest(); | 72 VideoSchedulerTest(); |
| 74 | 73 |
| 75 virtual void SetUp() OVERRIDE; | 74 virtual void SetUp() OVERRIDE; |
| 76 | 75 |
| 77 void StartVideoScheduler(scoped_ptr<media::ScreenCapturer> capturer); | 76 void StartVideoScheduler(scoped_ptr<media::ScreenCapturer> capturer); |
| 78 void StopVideoScheduler(); | 77 void StopVideoScheduler(); |
| 79 | 78 |
| 80 // media::ScreenCapturer mocks. | 79 // media::ScreenCapturer mocks. |
| 81 void OnCapturerStart(media::ScreenCapturer::Delegate* delegate); | 80 void OnCapturerStart(media::ScreenCapturer::Callback* callback); |
| 82 void OnCapturerStop(); | 81 void OnCaptureFrame(const webrtc::DesktopRegion& region); |
| 83 void OnCaptureFrame(); | |
| 84 | 82 |
| 85 protected: | 83 protected: |
| 86 MessageLoop message_loop_; | 84 MessageLoop message_loop_; |
| 87 base::RunLoop run_loop_; | 85 base::RunLoop run_loop_; |
| 88 scoped_refptr<AutoThreadTaskRunner> task_runner_; | 86 scoped_refptr<AutoThreadTaskRunner> task_runner_; |
| 89 scoped_refptr<VideoScheduler> scheduler_; | 87 scoped_refptr<VideoScheduler> scheduler_; |
| 90 | 88 |
| 91 MockClientStub client_stub_; | 89 MockClientStub client_stub_; |
| 92 MockVideoStub video_stub_; | 90 MockVideoStub video_stub_; |
| 93 | 91 |
| 94 // The following mock objects are owned by VideoScheduler. | 92 // The following mock objects are owned by VideoScheduler. |
| 95 MockVideoEncoder* encoder_; | 93 MockVideoEncoder* encoder_; |
| 96 | 94 |
| 97 scoped_refptr<media::ScreenCaptureData> data_; | 95 scoped_ptr<webrtc::DesktopFrame> frame_; |
| 98 | 96 |
| 99 // Points to the delegate passed to media::ScreenCapturer::Start(). | 97 // Points to the callback passed to media::ScreenCapturer::Start(). |
| 100 media::ScreenCapturer::Delegate* capturer_delegate_; | 98 media::ScreenCapturer::Callback* capturer_callback_; |
| 101 | 99 |
| 102 private: | 100 private: |
| 103 DISALLOW_COPY_AND_ASSIGN(VideoSchedulerTest); | 101 DISALLOW_COPY_AND_ASSIGN(VideoSchedulerTest); |
| 104 }; | 102 }; |
| 105 | 103 |
| 106 VideoSchedulerTest::VideoSchedulerTest() | 104 VideoSchedulerTest::VideoSchedulerTest() |
| 107 : encoder_(NULL), | 105 : encoder_(NULL), |
| 108 capturer_delegate_(NULL) { | 106 capturer_callback_(NULL) { |
| 109 } | 107 } |
| 110 | 108 |
| 111 void VideoSchedulerTest::SetUp() { | 109 void VideoSchedulerTest::SetUp() { |
| 112 task_runner_ = new AutoThreadTaskRunner( | 110 task_runner_ = new AutoThreadTaskRunner( |
| 113 message_loop_.message_loop_proxy(), run_loop_.QuitClosure()); | 111 message_loop_.message_loop_proxy(), run_loop_.QuitClosure()); |
| 114 | 112 |
| 115 encoder_ = new MockVideoEncoder(); | 113 encoder_ = new MockVideoEncoder(); |
| 116 } | 114 } |
| 117 | 115 |
| 118 void VideoSchedulerTest::StartVideoScheduler( | 116 void VideoSchedulerTest::StartVideoScheduler( |
| 119 scoped_ptr<media::ScreenCapturer> capturer) { | 117 scoped_ptr<media::ScreenCapturer> capturer) { |
| 120 scheduler_ = VideoScheduler::Create( | 118 scheduler_ = VideoScheduler::Create( |
| 121 task_runner_, // Capture | 119 task_runner_, // Capture |
| 122 task_runner_, // Encode | 120 task_runner_, // Encode |
| 123 task_runner_, // Network | 121 task_runner_, // Network |
| 124 capturer.Pass(), | 122 capturer.Pass(), |
| 125 scoped_ptr<VideoEncoder>(encoder_), | 123 scoped_ptr<VideoEncoder>(encoder_), |
| 126 &client_stub_, | 124 &client_stub_, |
| 127 &video_stub_); | 125 &video_stub_); |
| 128 } | 126 } |
| 129 | 127 |
| 130 void VideoSchedulerTest::StopVideoScheduler() { | 128 void VideoSchedulerTest::StopVideoScheduler() { |
| 131 scheduler_->Stop(); | 129 scheduler_->Stop(); |
| 132 scheduler_ = NULL; | 130 scheduler_ = NULL; |
| 133 } | 131 } |
| 134 | 132 |
| 135 void VideoSchedulerTest::OnCapturerStart( | 133 void VideoSchedulerTest::OnCapturerStart( |
| 136 media::ScreenCapturer::Delegate* delegate) { | 134 media::ScreenCapturer::Callback* callback) { |
| 137 EXPECT_FALSE(capturer_delegate_); | 135 EXPECT_FALSE(capturer_callback_); |
| 138 EXPECT_TRUE(delegate); | 136 EXPECT_TRUE(callback); |
| 139 | 137 |
| 140 capturer_delegate_ = delegate; | 138 capturer_callback_ = callback; |
| 141 } | 139 } |
| 142 | 140 |
| 143 void VideoSchedulerTest::OnCapturerStop() { | 141 void VideoSchedulerTest::OnCaptureFrame(const webrtc::DesktopRegion& region) { |
| 144 capturer_delegate_ = NULL; | 142 frame_->mutable_updated_region()->SetRect( |
| 145 } | 143 webrtc::DesktopRect::MakeXYWH(0, 0, 10, 10)); |
| 146 | 144 capturer_callback_->OnCaptureCompleted(frame_.release()); |
| 147 void VideoSchedulerTest::OnCaptureFrame() { | |
| 148 SkRegion update_region(SkIRect::MakeXYWH(0, 0, 10, 10)); | |
| 149 data_->mutable_dirty_region().op(update_region, SkRegion::kUnion_Op); | |
| 150 | |
| 151 capturer_delegate_->OnCaptureCompleted(data_); | |
| 152 } | 145 } |
| 153 | 146 |
| 154 // This test mocks capturer, encoder and network layer to simulate one capture | 147 // This test mocks capturer, encoder and network layer to simulate one capture |
| 155 // cycle. When the first encoded packet is submitted to the network | 148 // cycle. When the first encoded packet is submitted to the network |
| 156 // VideoScheduler is instructed to come to a complete stop. We expect the stop | 149 // VideoScheduler is instructed to come to a complete stop. We expect the stop |
| 157 // sequence to be executed successfully. | 150 // sequence to be executed successfully. |
| 158 TEST_F(VideoSchedulerTest, StartAndStop) { | 151 TEST_F(VideoSchedulerTest, StartAndStop) { |
| 159 scoped_ptr<media::MockScreenCapturer> capturer( | 152 scoped_ptr<media::MockScreenCapturer> capturer( |
| 160 new media::MockScreenCapturer()); | 153 new media::MockScreenCapturer()); |
| 161 Expectation capturer_start = | 154 Expectation capturer_start = |
| 162 EXPECT_CALL(*capturer, Start(_)) | 155 EXPECT_CALL(*capturer, Start(_)) |
| 163 .WillOnce(Invoke(this, &VideoSchedulerTest::OnCapturerStart)); | 156 .WillOnce(Invoke(this, &VideoSchedulerTest::OnCapturerStart)); |
| 164 | 157 |
| 165 data_ = new media::ScreenCaptureData( | 158 frame_.reset(new webrtc::BasicDesktopFrame( |
| 166 NULL, kWidth * media::ScreenCaptureData::kBytesPerPixel, | 159 webrtc::DesktopSize(kWidth, kHeight))); |
| 167 SkISize::Make(kWidth, kHeight)); | 160 webrtc::DesktopFrame* frame_ptr = frame_.get(); |
| 168 | 161 |
| 169 // First the capturer is called. | 162 // First the capturer is called. |
| 170 Expectation capturer_capture = EXPECT_CALL(*capturer, CaptureFrame()) | 163 Expectation capturer_capture = EXPECT_CALL(*capturer, Capture(_)) |
| 171 .After(capturer_start) | 164 .After(capturer_start) |
| 172 .WillRepeatedly(Invoke(this, &VideoSchedulerTest::OnCaptureFrame)); | 165 .WillRepeatedly(Invoke(this, &VideoSchedulerTest::OnCaptureFrame)); |
| 173 | 166 |
| 174 // Expect the encoder be called. | 167 // Expect the encoder be called. |
| 175 EXPECT_CALL(*encoder_, Encode(data_, false, _)) | 168 EXPECT_CALL(*encoder_, Encode(frame_ptr, _)) |
| 176 .WillRepeatedly(FinishEncode()); | 169 .WillRepeatedly(FinishEncode()); |
| 177 | 170 |
| 178 // By default delete the arguments when ProcessVideoPacket is received. | 171 // By default delete the arguments when ProcessVideoPacket is received. |
| 179 EXPECT_CALL(video_stub_, ProcessVideoPacketPtr(_, _)) | 172 EXPECT_CALL(video_stub_, ProcessVideoPacketPtr(_, _)) |
| 180 .WillRepeatedly(FinishSend()); | 173 .WillRepeatedly(FinishSend()); |
| 181 | 174 |
| 182 // For the first time when ProcessVideoPacket is received we stop the | 175 // For the first time when ProcessVideoPacket is received we stop the |
| 183 // VideoScheduler. | 176 // VideoScheduler. |
| 184 EXPECT_CALL(video_stub_, ProcessVideoPacketPtr(_, _)) | 177 EXPECT_CALL(video_stub_, ProcessVideoPacketPtr(_, _)) |
| 185 .WillOnce(DoAll( | 178 .WillOnce(DoAll( |
| 186 FinishSend(), | 179 FinishSend(), |
| 187 InvokeWithoutArgs(this, &VideoSchedulerTest::StopVideoScheduler))) | 180 InvokeWithoutArgs(this, &VideoSchedulerTest::StopVideoScheduler))) |
| 188 .RetiresOnSaturation(); | 181 .RetiresOnSaturation(); |
| 189 | 182 |
| 190 // Start video frame capture. | 183 // Start video frame capture. |
| 191 StartVideoScheduler(capturer.PassAs<media::ScreenCapturer>()); | 184 StartVideoScheduler(capturer.PassAs<media::ScreenCapturer>()); |
| 192 | 185 |
| 193 task_runner_ = NULL; | 186 task_runner_ = NULL; |
| 194 run_loop_.Run(); | 187 run_loop_.Run(); |
| 195 } | 188 } |
| 196 | 189 |
| 197 } // namespace remoting | 190 } // namespace remoting |
| OLD | NEW |