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 const 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 base::MessageLoop message_loop_; | 84 base::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_ = new VideoScheduler( | 118 scheduler_ = new VideoScheduler( |
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 scheduler_->Start(); | 126 scheduler_->Start(); |
129 } | 127 } |
130 | 128 |
131 void VideoSchedulerTest::StopVideoScheduler() { | 129 void VideoSchedulerTest::StopVideoScheduler() { |
132 scheduler_->Stop(); | 130 scheduler_->Stop(); |
133 scheduler_ = NULL; | 131 scheduler_ = NULL; |
134 } | 132 } |
135 | 133 |
136 void VideoSchedulerTest::OnCapturerStart( | 134 void VideoSchedulerTest::OnCapturerStart( |
137 media::ScreenCapturer::Delegate* delegate) { | 135 media::ScreenCapturer::Callback* callback) { |
138 EXPECT_FALSE(capturer_delegate_); | 136 EXPECT_FALSE(capturer_callback_); |
139 EXPECT_TRUE(delegate); | 137 EXPECT_TRUE(callback); |
140 | 138 |
141 capturer_delegate_ = delegate; | 139 capturer_callback_ = callback; |
142 } | 140 } |
143 | 141 |
144 void VideoSchedulerTest::OnCapturerStop() { | 142 void VideoSchedulerTest::OnCaptureFrame(const webrtc::DesktopRegion& region) { |
145 capturer_delegate_ = NULL; | 143 frame_->mutable_updated_region()->SetRect( |
146 } | 144 webrtc::DesktopRect::MakeXYWH(0, 0, 10, 10)); |
147 | 145 capturer_callback_->OnCaptureCompleted(frame_.release()); |
148 void VideoSchedulerTest::OnCaptureFrame() { | |
149 SkRegion update_region(SkIRect::MakeXYWH(0, 0, 10, 10)); | |
150 data_->mutable_dirty_region().op(update_region, SkRegion::kUnion_Op); | |
151 | |
152 capturer_delegate_->OnCaptureCompleted(data_); | |
153 } | 146 } |
154 | 147 |
155 // This test mocks capturer, encoder and network layer to simulate one capture | 148 // This test mocks capturer, encoder and network layer to simulate one capture |
156 // cycle. When the first encoded packet is submitted to the network | 149 // cycle. When the first encoded packet is submitted to the network |
157 // VideoScheduler is instructed to come to a complete stop. We expect the stop | 150 // VideoScheduler is instructed to come to a complete stop. We expect the stop |
158 // sequence to be executed successfully. | 151 // sequence to be executed successfully. |
159 TEST_F(VideoSchedulerTest, StartAndStop) { | 152 TEST_F(VideoSchedulerTest, StartAndStop) { |
160 scoped_ptr<media::MockScreenCapturer> capturer( | 153 scoped_ptr<media::MockScreenCapturer> capturer( |
161 new media::MockScreenCapturer()); | 154 new media::MockScreenCapturer()); |
162 Expectation capturer_start = | 155 Expectation capturer_start = |
163 EXPECT_CALL(*capturer, Start(_)) | 156 EXPECT_CALL(*capturer, Start(_)) |
164 .WillOnce(Invoke(this, &VideoSchedulerTest::OnCapturerStart)); | 157 .WillOnce(Invoke(this, &VideoSchedulerTest::OnCapturerStart)); |
165 | 158 |
166 data_ = new media::ScreenCaptureData( | 159 frame_.reset(new webrtc::BasicDesktopFrame( |
167 NULL, kWidth * media::ScreenCaptureData::kBytesPerPixel, | 160 webrtc::DesktopSize(kWidth, kHeight))); |
168 SkISize::Make(kWidth, kHeight)); | 161 webrtc::DesktopFrame* frame_ptr = frame_.get(); |
169 | 162 |
170 // First the capturer is called. | 163 // First the capturer is called. |
171 Expectation capturer_capture = EXPECT_CALL(*capturer, CaptureFrame()) | 164 Expectation capturer_capture = EXPECT_CALL(*capturer, Capture(_)) |
172 .After(capturer_start) | 165 .After(capturer_start) |
173 .WillRepeatedly(Invoke(this, &VideoSchedulerTest::OnCaptureFrame)); | 166 .WillRepeatedly(Invoke(this, &VideoSchedulerTest::OnCaptureFrame)); |
174 | 167 |
175 // Expect the encoder be called. | 168 // Expect the encoder be called. |
176 EXPECT_CALL(*encoder_, Encode(data_, false, _)) | 169 EXPECT_CALL(*encoder_, Encode(frame_ptr, _)) |
177 .WillRepeatedly(FinishEncode()); | 170 .WillRepeatedly(FinishEncode()); |
178 | 171 |
179 // By default delete the arguments when ProcessVideoPacket is received. | 172 // By default delete the arguments when ProcessVideoPacket is received. |
180 EXPECT_CALL(video_stub_, ProcessVideoPacketPtr(_, _)) | 173 EXPECT_CALL(video_stub_, ProcessVideoPacketPtr(_, _)) |
181 .WillRepeatedly(FinishSend()); | 174 .WillRepeatedly(FinishSend()); |
182 | 175 |
183 // For the first time when ProcessVideoPacket is received we stop the | 176 // For the first time when ProcessVideoPacket is received we stop the |
184 // VideoScheduler. | 177 // VideoScheduler. |
185 EXPECT_CALL(video_stub_, ProcessVideoPacketPtr(_, _)) | 178 EXPECT_CALL(video_stub_, ProcessVideoPacketPtr(_, _)) |
186 .WillOnce(DoAll( | 179 .WillOnce(DoAll( |
187 FinishSend(), | 180 FinishSend(), |
188 InvokeWithoutArgs(this, &VideoSchedulerTest::StopVideoScheduler))) | 181 InvokeWithoutArgs(this, &VideoSchedulerTest::StopVideoScheduler))) |
189 .RetiresOnSaturation(); | 182 .RetiresOnSaturation(); |
190 | 183 |
191 // Start video frame capture. | 184 // Start video frame capture. |
192 StartVideoScheduler(capturer.PassAs<media::ScreenCapturer>()); | 185 StartVideoScheduler(capturer.PassAs<media::ScreenCapturer>()); |
193 | 186 |
194 task_runner_ = NULL; | 187 task_runner_ = NULL; |
195 run_loop_.Run(); | 188 run_loop_.Run(); |
196 } | 189 } |
197 | 190 |
198 } // namespace remoting | 191 } // namespace remoting |
OLD | NEW |