Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(9)

Side by Side Diff: content/browser/renderer_host/media/video_capture_host_unittest.cc

Issue 11339014: Move content\browser\renderer_host\media to content namespace. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 8 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 <map> 5 #include <map>
6 #include <string> 6 #include <string>
7 7
8 #include "base/bind.h" 8 #include "base/bind.h"
9 #include "base/file_util.h" 9 #include "base/file_util.h"
10 #include "base/memory/scoped_ptr.h" 10 #include "base/memory/scoped_ptr.h"
(...skipping 13 matching lines...) Expand all
24 #include "testing/gmock/include/gmock/gmock.h" 24 #include "testing/gmock/include/gmock/gmock.h"
25 #include "testing/gtest/include/gtest/gtest.h" 25 #include "testing/gtest/include/gtest/gtest.h"
26 26
27 using ::testing::_; 27 using ::testing::_;
28 using ::testing::AtLeast; 28 using ::testing::AtLeast;
29 using ::testing::AnyNumber; 29 using ::testing::AnyNumber;
30 using ::testing::DoAll; 30 using ::testing::DoAll;
31 using ::testing::InSequence; 31 using ::testing::InSequence;
32 using ::testing::Mock; 32 using ::testing::Mock;
33 using ::testing::Return; 33 using ::testing::Return;
34 using content::BrowserThread; 34
35 using content::BrowserThreadImpl; 35 namespace content {
36 36
37 // Id used to identify the capture session between renderer and 37 // Id used to identify the capture session between renderer and
38 // video_capture_host. 38 // video_capture_host.
39 static const int kDeviceId = 1; 39 static const int kDeviceId = 1;
40 // Id of a video capture device 40 // Id of a video capture device
41 static const media::VideoCaptureSessionId kTestFakeDeviceId = 41 static const media::VideoCaptureSessionId kTestFakeDeviceId =
42 media_stream::VideoCaptureManager::kStartOpenSessionId; 42 VideoCaptureManager::kStartOpenSessionId;
43 43
44 // Define to enable test where video is dumped to file. 44 // Define to enable test where video is dumped to file.
45 // #define DUMP_VIDEO 45 // #define DUMP_VIDEO
46 46
47 // Define to use a real video capture device. 47 // Define to use a real video capture device.
48 // #define TEST_REAL_CAPTURE_DEVICE 48 // #define TEST_REAL_CAPTURE_DEVICE
49 49
50 // Simple class used for dumping video to a file. This can be used for 50 // Simple class used for dumping video to a file. This can be used for
51 // verifying the output. 51 // verifying the output.
52 class DumpVideo { 52 class DumpVideo {
(...skipping 11 matching lines...) Expand all
64 } 64 }
65 } 65 }
66 66
67 private: 67 private:
68 file_util::ScopedFILE file_; 68 file_util::ScopedFILE file_;
69 int expected_size_; 69 int expected_size_;
70 }; 70 };
71 71
72 class MockVideoCaptureHost : public VideoCaptureHost { 72 class MockVideoCaptureHost : public VideoCaptureHost {
73 public: 73 public:
74 MockVideoCaptureHost(media_stream::MediaStreamManager* manager) 74 MockVideoCaptureHost(MediaStreamManager* manager)
75 : VideoCaptureHost(), 75 : VideoCaptureHost(),
76 return_buffers_(false), 76 return_buffers_(false),
77 dump_video_(false), 77 dump_video_(false),
78 manager_(manager) {} 78 manager_(manager) {}
79 79
80 // A list of mock methods. 80 // A list of mock methods.
81 MOCK_METHOD4(OnNewBufferCreated, 81 MOCK_METHOD4(OnNewBufferCreated,
82 void(int device_id, base::SharedMemoryHandle handle, 82 void(int device_id, base::SharedMemoryHandle handle,
83 int length, int buffer_id)); 83 int length, int buffer_id));
84 MOCK_METHOD3(OnBufferFilled, 84 MOCK_METHOD3(OnBufferFilled,
85 void(int device_id, int buffer_id, base::Time timestamp)); 85 void(int device_id, int buffer_id, base::Time timestamp));
86 MOCK_METHOD2(OnStateChanged, 86 MOCK_METHOD2(OnStateChanged, void(int device_id, VideoCaptureState state));
87 void(int device_id, video_capture::State state));
88 MOCK_METHOD1(OnDeviceInfo, void(int device_id)); 87 MOCK_METHOD1(OnDeviceInfo, void(int device_id));
89 88
90 // Use class DumpVideo to write I420 video to file. 89 // Use class DumpVideo to write I420 video to file.
91 void SetDumpVideo(bool enable) { 90 void SetDumpVideo(bool enable) {
92 dump_video_ = enable; 91 dump_video_ = enable;
93 } 92 }
94 93
95 void SetReturnReceviedDibs(bool enable) { 94 void SetReturnReceviedDibs(bool enable) {
96 return_buffers_ = enable; 95 return_buffers_ = enable;
97 } 96 }
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
137 IPC_MESSAGE_HANDLER(VideoCaptureMsg_StateChanged, OnStateChangedDispatch) 136 IPC_MESSAGE_HANDLER(VideoCaptureMsg_StateChanged, OnStateChangedDispatch)
138 IPC_MESSAGE_HANDLER(VideoCaptureMsg_DeviceInfo, OnDeviceInfoDispatch) 137 IPC_MESSAGE_HANDLER(VideoCaptureMsg_DeviceInfo, OnDeviceInfoDispatch)
139 IPC_MESSAGE_UNHANDLED(handled = false) 138 IPC_MESSAGE_UNHANDLED(handled = false)
140 IPC_END_MESSAGE_MAP() 139 IPC_END_MESSAGE_MAP()
141 EXPECT_TRUE(handled); 140 EXPECT_TRUE(handled);
142 141
143 delete message; 142 delete message;
144 return true; 143 return true;
145 } 144 }
146 145
147 virtual media_stream::VideoCaptureManager* GetVideoCaptureManager() OVERRIDE { 146 virtual VideoCaptureManager* GetVideoCaptureManager() OVERRIDE {
148 return manager_->video_capture_manager(); 147 return manager_->video_capture_manager();
149 } 148 }
150 149
151 // These handler methods do minimal things and delegate to the mock methods. 150 // These handler methods do minimal things and delegate to the mock methods.
152 void OnNewBufferCreatedDispatch(int device_id, 151 void OnNewBufferCreatedDispatch(int device_id,
153 base::SharedMemoryHandle handle, 152 base::SharedMemoryHandle handle,
154 int length, int buffer_id) { 153 int length, int buffer_id) {
155 OnNewBufferCreated(device_id, handle, length, buffer_id); 154 OnNewBufferCreated(device_id, handle, length, buffer_id);
156 base::SharedMemory* dib = new base::SharedMemory(handle, false); 155 base::SharedMemory* dib = new base::SharedMemory(handle, false);
157 dib->Map(length); 156 dib->Map(length);
158 filled_dib_[buffer_id] = dib; 157 filled_dib_[buffer_id] = dib;
159 } 158 }
160 159
161 void OnBufferFilledDispatch(int device_id, int buffer_id, 160 void OnBufferFilledDispatch(int device_id, int buffer_id,
162 base::Time timestamp) { 161 base::Time timestamp) {
163 if (dump_video_) { 162 if (dump_video_) {
164 base::SharedMemory* dib = filled_dib_[buffer_id]; 163 base::SharedMemory* dib = filled_dib_[buffer_id];
165 ASSERT_TRUE(dib != NULL); 164 ASSERT_TRUE(dib != NULL);
166 dumper_.NewVideoFrame(dib->memory()); 165 dumper_.NewVideoFrame(dib->memory());
167 } 166 }
168 167
169 OnBufferFilled(device_id, buffer_id, timestamp); 168 OnBufferFilled(device_id, buffer_id, timestamp);
170 if (return_buffers_) { 169 if (return_buffers_) {
171 VideoCaptureHost::OnReceiveEmptyBuffer(device_id, buffer_id); 170 VideoCaptureHost::OnReceiveEmptyBuffer(device_id, buffer_id);
172 } 171 }
173 } 172 }
174 173
175 void OnStateChangedDispatch(int device_id, 174 void OnStateChangedDispatch(int device_id, VideoCaptureState state) {
176 video_capture::State state) {
177 OnStateChanged(device_id, state); 175 OnStateChanged(device_id, state);
178 } 176 }
179 177
180 void OnDeviceInfoDispatch(int device_id, 178 void OnDeviceInfoDispatch(int device_id,
181 media::VideoCaptureParams params) { 179 media::VideoCaptureParams params) {
182 if (dump_video_) { 180 if (dump_video_) {
183 dumper_.StartDump(params.width, params.height); 181 dumper_.StartDump(params.width, params.height);
184 } 182 }
185 OnDeviceInfo(device_id); 183 OnDeviceInfo(device_id);
186 } 184 }
187 185
188 std::map<int, base::SharedMemory*> filled_dib_; 186 std::map<int, base::SharedMemory*> filled_dib_;
189 bool return_buffers_; 187 bool return_buffers_;
190 bool dump_video_; 188 bool dump_video_;
191 DumpVideo dumper_; 189 DumpVideo dumper_;
192 media_stream::MediaStreamManager* manager_; 190 MediaStreamManager* manager_;
193 }; 191 };
194 192
195 ACTION_P(ExitMessageLoop, message_loop) { 193 ACTION_P(ExitMessageLoop, message_loop) {
196 message_loop->PostTask(FROM_HERE, MessageLoop::QuitClosure()); 194 message_loop->PostTask(FROM_HERE, MessageLoop::QuitClosure());
197 } 195 }
198 196
199 class VideoCaptureHostTest : public testing::Test { 197 class VideoCaptureHostTest : public testing::Test {
200 public: 198 public:
201 VideoCaptureHostTest() {} 199 VideoCaptureHostTest() {}
202 200
203 protected: 201 protected:
204 virtual void SetUp() OVERRIDE { 202 virtual void SetUp() OVERRIDE {
205 // Create a message loop so VideoCaptureHostTest can use it. 203 // Create a message loop so VideoCaptureHostTest can use it.
206 message_loop_.reset(new MessageLoop(MessageLoop::TYPE_IO)); 204 message_loop_.reset(new MessageLoop(MessageLoop::TYPE_IO));
207 205
208 // MediaStreamManager must be created on the IO thread. 206 // MediaStreamManager must be created on the IO thread.
209 io_thread_.reset(new BrowserThreadImpl(BrowserThread::IO, 207 io_thread_.reset(new BrowserThreadImpl(BrowserThread::IO,
210 message_loop_.get())); 208 message_loop_.get()));
211 209
212 // Create our own MediaStreamManager. 210 // Create our own MediaStreamManager.
213 audio_manager_.reset(media::AudioManager::Create()); 211 audio_manager_.reset(media::AudioManager::Create());
214 media_stream_manager_.reset( 212 media_stream_manager_.reset(new MediaStreamManager(audio_manager_.get()));
215 new media_stream::MediaStreamManager(audio_manager_.get()));
216 #ifndef TEST_REAL_CAPTURE_DEVICE 213 #ifndef TEST_REAL_CAPTURE_DEVICE
217 media_stream_manager_->UseFakeDevice(); 214 media_stream_manager_->UseFakeDevice();
218 #endif 215 #endif
219 216
220 host_ = new MockVideoCaptureHost(media_stream_manager_.get()); 217 host_ = new MockVideoCaptureHost(media_stream_manager_.get());
221 218
222 // Simulate IPC channel connected. 219 // Simulate IPC channel connected.
223 host_->OnChannelConnected(base::GetCurrentProcId()); 220 host_->OnChannelConnected(base::GetCurrentProcId());
224 } 221 }
225 222
226 virtual void TearDown() OVERRIDE { 223 virtual void TearDown() OVERRIDE {
227 // Verifies and removes the expectations on host_ and 224 // Verifies and removes the expectations on host_ and
228 // returns true iff successful. 225 // returns true iff successful.
229 Mock::VerifyAndClearExpectations(host_); 226 Mock::VerifyAndClearExpectations(host_);
230 227
231 EXPECT_CALL(*host_, OnStateChanged(kDeviceId, 228 EXPECT_CALL(*host_, OnStateChanged(kDeviceId,
232 video_capture::kStopped)) 229 VIDEO_CAPTURE_STATE_STOPPED))
233 .Times(AnyNumber()); 230 .Times(AnyNumber());
234 231
235 // Simulate closing the IPC channel. 232 // Simulate closing the IPC channel.
236 host_->OnChannelClosing(); 233 host_->OnChannelClosing();
237 234
238 // Release the reference to the mock object. The object will be destructed 235 // Release the reference to the mock object. The object will be destructed
239 // on message_loop_. 236 // on message_loop_.
240 host_ = NULL; 237 host_ = NULL;
241 238
242 // We need to continue running message_loop_ to complete all destructions. 239 // We need to continue running message_loop_ to complete all destructions.
243 message_loop_->RunAllPending(); 240 message_loop_->RunAllPending();
244 241
245 // Delete the IO message loop. This will cause the MediaStreamManager to be 242 // Delete the IO message loop. This will cause the MediaStreamManager to be
246 // notified so it will stop its device thread and device managers. 243 // notified so it will stop its device thread and device managers.
247 message_loop_.reset(); 244 message_loop_.reset();
248 } 245 }
249 246
250 void StartCapture() { 247 void StartCapture() {
251 InSequence s; 248 InSequence s;
252 // 1. First - get info about the new resolution 249 // 1. First - get info about the new resolution
253 EXPECT_CALL(*host_, OnDeviceInfo(kDeviceId)); 250 EXPECT_CALL(*host_, OnDeviceInfo(kDeviceId));
254 251
255 // 2. Change state to started 252 // 2. Change state to started
256 EXPECT_CALL(*host_, OnStateChanged(kDeviceId, 253 EXPECT_CALL(*host_, OnStateChanged(kDeviceId,
257 video_capture::kStarted)); 254 VIDEO_CAPTURE_STATE_STARTED));
258 255
259 // 3. Newly created buffers will arrive. 256 // 3. Newly created buffers will arrive.
260 EXPECT_CALL(*host_, OnNewBufferCreated(kDeviceId, _, _, _)) 257 EXPECT_CALL(*host_, OnNewBufferCreated(kDeviceId, _, _, _))
261 .Times(AnyNumber()) 258 .Times(AnyNumber())
262 .WillRepeatedly(Return()); 259 .WillRepeatedly(Return());
263 260
264 // 4. First filled buffer will arrive. 261 // 4. First filled buffer will arrive.
265 EXPECT_CALL(*host_, OnBufferFilled(kDeviceId, _, _)) 262 EXPECT_CALL(*host_, OnBufferFilled(kDeviceId, _, _))
266 .Times(AnyNumber()) 263 .Times(AnyNumber())
267 .WillOnce(ExitMessageLoop(message_loop_.get())); 264 .WillOnce(ExitMessageLoop(message_loop_.get()));
268 265
269 media::VideoCaptureParams params; 266 media::VideoCaptureParams params;
270 params.width = 352; 267 params.width = 352;
271 params.height = 288; 268 params.height = 288;
272 params.frame_per_second = 30; 269 params.frame_per_second = 30;
273 params.session_id = kTestFakeDeviceId; 270 params.session_id = kTestFakeDeviceId;
274 host_->OnStartCapture(kDeviceId, params); 271 host_->OnStartCapture(kDeviceId, params);
275 message_loop_->Run(); 272 message_loop_->Run();
276 } 273 }
277 274
278 #ifdef DUMP_VIDEO 275 #ifdef DUMP_VIDEO
279 void CaptureAndDumpVideo(int width, int heigt, int frame_rate) { 276 void CaptureAndDumpVideo(int width, int heigt, int frame_rate) {
280 InSequence s; 277 InSequence s;
281 // 1. First - get info about the new resolution 278 // 1. First - get info about the new resolution
282 EXPECT_CALL(*host_, OnDeviceInfo(kDeviceId)); 279 EXPECT_CALL(*host_, OnDeviceInfo(kDeviceId));
283 280
284 // 2. Change state to started 281 // 2. Change state to started
285 EXPECT_CALL(*host_, OnStateChanged(kDeviceId, 282 EXPECT_CALL(*host_, OnStateChanged(kDeviceId, VIDEO_CAPTURE_STATE_STARTED));
286 video_capture::kStarted));
287 283
288 // 3. First filled buffer will arrive. 284 // 3. First filled buffer will arrive.
289 EXPECT_CALL(*host_, OnBufferFilled(kDeviceId, _, _)) 285 EXPECT_CALL(*host_, OnBufferFilled(kDeviceId, _, _))
290 .Times(AnyNumber()) 286 .Times(AnyNumber())
291 .WillOnce(ExitMessageLoop(message_loop_.get())); 287 .WillOnce(ExitMessageLoop(message_loop_.get()));
292 288
293 media::VideoCaptureParams params; 289 media::VideoCaptureParams params;
294 params.width = width; 290 params.width = width;
295 params.height = heigt; 291 params.height = heigt;
296 params.frame_per_second = frame_rate; 292 params.frame_per_second = frame_rate;
297 params.session_id = kTestFakeDeviceId; 293 params.session_id = kTestFakeDeviceId;
298 host_->SetDumpVideo(true); 294 host_->SetDumpVideo(true);
299 host_->OnStartCapture(kDeviceId, params); 295 host_->OnStartCapture(kDeviceId, params);
300 message_loop_->Run(); 296 message_loop_->Run();
301 } 297 }
302 #endif 298 #endif
303 299
304 void StopCapture() { 300 void StopCapture() {
305 EXPECT_CALL(*host_, OnStateChanged(kDeviceId, 301 EXPECT_CALL(*host_, OnStateChanged(kDeviceId,
306 video_capture::kStopped)) 302 VIDEO_CAPTURE_STATE_STOPPED))
307 .WillOnce(ExitMessageLoop(message_loop_.get())); 303 .WillOnce(ExitMessageLoop(message_loop_.get()));
308 304
309 host_->OnStopCapture(kDeviceId); 305 host_->OnStopCapture(kDeviceId);
310 host_->SetReturnReceviedDibs(true); 306 host_->SetReturnReceviedDibs(true);
311 host_->ReturnReceivedDibs(kDeviceId); 307 host_->ReturnReceivedDibs(kDeviceId);
312 308
313 message_loop_->Run(); 309 message_loop_->Run();
314 310
315 host_->SetReturnReceviedDibs(false); 311 host_->SetReturnReceviedDibs(false);
316 // Expect the VideoCaptureDevice has been stopped 312 // Expect the VideoCaptureDevice has been stopped
317 EXPECT_EQ(0u, host_->entries_.size()); 313 EXPECT_EQ(0u, host_->entries_.size());
318 } 314 }
319 315
320 void NotifyPacketReady() { 316 void NotifyPacketReady() {
321 EXPECT_CALL(*host_, OnBufferFilled(kDeviceId, _, _)) 317 EXPECT_CALL(*host_, OnBufferFilled(kDeviceId, _, _))
322 .Times(AnyNumber()) 318 .Times(AnyNumber())
323 .WillOnce(ExitMessageLoop(message_loop_.get())) 319 .WillOnce(ExitMessageLoop(message_loop_.get()))
324 .RetiresOnSaturation(); 320 .RetiresOnSaturation();
325 message_loop_->Run(); 321 message_loop_->Run();
326 } 322 }
327 323
328 void ReturnReceivedPackets() { 324 void ReturnReceivedPackets() {
329 host_->ReturnReceivedDibs(kDeviceId); 325 host_->ReturnReceivedDibs(kDeviceId);
330 } 326 }
331 327
332 void SimulateError() { 328 void SimulateError() {
333 // Expect a change state to error state sent through IPC. 329 // Expect a change state to error state sent through IPC.
334 EXPECT_CALL(*host_, OnStateChanged(kDeviceId, 330 EXPECT_CALL(*host_, OnStateChanged(kDeviceId, VIDEO_CAPTURE_STATE_ERROR))
335 video_capture::kError))
336 .Times(1); 331 .Times(1);
337 VideoCaptureControllerID id(kDeviceId); 332 VideoCaptureControllerID id(kDeviceId);
338 host_->OnError(id); 333 host_->OnError(id);
339 // Wait for the error callback. 334 // Wait for the error callback.
340 message_loop_->RunAllPending(); 335 message_loop_->RunAllPending();
341 } 336 }
342 337
343 scoped_refptr<MockVideoCaptureHost> host_; 338 scoped_refptr<MockVideoCaptureHost> host_;
344 339
345 private: 340 private:
346 scoped_ptr<MessageLoop> message_loop_; 341 scoped_ptr<MessageLoop> message_loop_;
347 scoped_ptr<BrowserThreadImpl> io_thread_; 342 scoped_ptr<BrowserThreadImpl> io_thread_;
348 scoped_ptr<media::AudioManager> audio_manager_; 343 scoped_ptr<media::AudioManager> audio_manager_;
349 scoped_ptr<media_stream::MediaStreamManager> media_stream_manager_; 344 scoped_ptr<MediaStreamManager> media_stream_manager_;
350 345
351 DISALLOW_COPY_AND_ASSIGN(VideoCaptureHostTest); 346 DISALLOW_COPY_AND_ASSIGN(VideoCaptureHostTest);
352 }; 347 };
353 348
354 TEST_F(VideoCaptureHostTest, StartCapture) { 349 TEST_F(VideoCaptureHostTest, StartCapture) {
355 StartCapture(); 350 StartCapture();
356 } 351 }
357 352
358 TEST_F(VideoCaptureHostTest, StartCapturePlayStop) { 353 TEST_F(VideoCaptureHostTest, StartCapturePlayStop) {
359 StartCapture(); 354 StartCapture();
360 NotifyPacketReady(); 355 NotifyPacketReady();
361 NotifyPacketReady(); 356 NotifyPacketReady();
362 ReturnReceivedPackets(); 357 ReturnReceivedPackets();
363 StopCapture(); 358 StopCapture();
364 } 359 }
365 360
366 TEST_F(VideoCaptureHostTest, StartCaptureErrorStop) { 361 TEST_F(VideoCaptureHostTest, StartCaptureErrorStop) {
367 StartCapture(); 362 StartCapture();
368 SimulateError(); 363 SimulateError();
369 StopCapture(); 364 StopCapture();
370 } 365 }
371 366
372 TEST_F(VideoCaptureHostTest, StartCaptureError) { 367 TEST_F(VideoCaptureHostTest, StartCaptureError) {
373 EXPECT_CALL(*host_, OnStateChanged(kDeviceId, 368 EXPECT_CALL(*host_, OnStateChanged(kDeviceId,
374 video_capture::kStopped)) 369 VIDEO_CAPTURE_STATE_STOPPED))
375 .Times(0); 370 .Times(0);
376 StartCapture(); 371 StartCapture();
377 NotifyPacketReady(); 372 NotifyPacketReady();
378 SimulateError(); 373 SimulateError();
379 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(200)); 374 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(200));
380 } 375 }
381 376
382 #ifdef DUMP_VIDEO 377 #ifdef DUMP_VIDEO
383 TEST_F(VideoCaptureHostTest, CaptureAndDumpVideoVga) { 378 TEST_F(VideoCaptureHostTest, CaptureAndDumpVideoVga) {
384 CaptureAndDumpVideo(640, 480, 30); 379 CaptureAndDumpVideo(640, 480, 30);
385 } 380 }
386 TEST_F(VideoCaptureHostTest, CaptureAndDump720P) { 381 TEST_F(VideoCaptureHostTest, CaptureAndDump720P) {
387 CaptureAndDumpVideo(1280, 720, 30); 382 CaptureAndDumpVideo(1280, 720, 30);
388 } 383 }
389 #endif 384 #endif
385
386 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698