Chromium Code Reviews| 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 <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" |
| 11 #include "base/message_loop.h" | 11 #include "base/message_loop.h" |
| 12 #include "base/process_util.h" | 12 #include "base/process_util.h" |
| 13 #include "base/stl_util.h" | 13 #include "base/stl_util.h" |
| 14 #include "base/stringprintf.h" | 14 #include "base/stringprintf.h" |
| 15 #include "content/browser/browser_thread_impl.h" | 15 #include "content/browser/browser_thread_impl.h" |
| 16 #include "content/browser/renderer_host/media/audio_input_device_manager.h" | |
| 16 #include "content/browser/renderer_host/media/media_stream_manager.h" | 17 #include "content/browser/renderer_host/media/media_stream_manager.h" |
| 17 #include "content/browser/renderer_host/media/video_capture_host.h" | 18 #include "content/browser/renderer_host/media/video_capture_host.h" |
| 18 #include "content/browser/renderer_host/media/video_capture_manager.h" | 19 #include "content/browser/renderer_host/media/video_capture_manager.h" |
| 19 #include "content/common/media/video_capture_messages.h" | 20 #include "content/common/media/video_capture_messages.h" |
| 20 #include "content/public/test/mock_resource_context.h" | 21 #include "content/public/test/mock_resource_context.h" |
| 21 #include "media/audio/audio_manager.h" | 22 #include "media/audio/audio_manager.h" |
| 22 #include "media/video/capture/video_capture_types.h" | 23 #include "media/video/capture/video_capture_types.h" |
| 23 #include "net/url_request/url_request_context.h" | 24 #include "net/url_request/url_request_context.h" |
| 24 #include "testing/gmock/include/gmock/gmock.h" | 25 #include "testing/gmock/include/gmock/gmock.h" |
| 25 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 64 } | 65 } |
| 65 } | 66 } |
| 66 | 67 |
| 67 private: | 68 private: |
| 68 file_util::ScopedFILE file_; | 69 file_util::ScopedFILE file_; |
| 69 int expected_size_; | 70 int expected_size_; |
| 70 }; | 71 }; |
| 71 | 72 |
| 72 class MockVideoCaptureHost : public VideoCaptureHost { | 73 class MockVideoCaptureHost : public VideoCaptureHost { |
| 73 public: | 74 public: |
| 74 MockVideoCaptureHost(content::ResourceContext* resource_context, | 75 MockVideoCaptureHost(media_stream::MediaStreamManager* manager) |
| 75 media::AudioManager* audio_manager) | 76 : VideoCaptureHost(), |
| 76 : VideoCaptureHost(resource_context, audio_manager), | |
| 77 return_buffers_(false), | 77 return_buffers_(false), |
| 78 dump_video_(false) {} | 78 dump_video_(false), |
| 79 manager_(manager) {} | |
| 79 | 80 |
| 80 // A list of mock methods. | 81 // A list of mock methods. |
| 81 MOCK_METHOD4(OnNewBufferCreated, | 82 MOCK_METHOD4(OnNewBufferCreated, |
| 82 void(int device_id, base::SharedMemoryHandle handle, | 83 void(int device_id, base::SharedMemoryHandle handle, |
| 83 int length, int buffer_id)); | 84 int length, int buffer_id)); |
| 84 MOCK_METHOD3(OnBufferFilled, | 85 MOCK_METHOD3(OnBufferFilled, |
| 85 void(int device_id, int buffer_id, base::Time timestamp)); | 86 void(int device_id, int buffer_id, base::Time timestamp)); |
| 86 MOCK_METHOD2(OnStateChanged, | 87 MOCK_METHOD2(OnStateChanged, |
| 87 void(int device_id, video_capture::State state)); | 88 void(int device_id, video_capture::State state)); |
| 88 MOCK_METHOD1(OnDeviceInfo, void(int device_id)); | 89 MOCK_METHOD1(OnDeviceInfo, void(int device_id)); |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 118 | 119 |
| 119 private: | 120 private: |
| 120 virtual ~MockVideoCaptureHost() { | 121 virtual ~MockVideoCaptureHost() { |
| 121 STLDeleteContainerPairSecondPointers(filled_dib_.begin(), | 122 STLDeleteContainerPairSecondPointers(filled_dib_.begin(), |
| 122 filled_dib_.end()); | 123 filled_dib_.end()); |
| 123 } | 124 } |
| 124 | 125 |
| 125 // This method is used to dispatch IPC messages to the renderer. We intercept | 126 // This method is used to dispatch IPC messages to the renderer. We intercept |
| 126 // these messages here and dispatch to our mock methods to verify the | 127 // these messages here and dispatch to our mock methods to verify the |
| 127 // conversation between this object and the renderer. | 128 // conversation between this object and the renderer. |
| 128 virtual bool Send(IPC::Message* message) { | 129 virtual bool Send(IPC::Message* message) OVERRIDE { |
| 129 CHECK(message); | 130 CHECK(message); |
| 130 | 131 |
| 131 // In this method we dispatch the messages to the according handlers as if | 132 // In this method we dispatch the messages to the according handlers as if |
| 132 // we are the renderer. | 133 // we are the renderer. |
| 133 bool handled = true; | 134 bool handled = true; |
| 134 IPC_BEGIN_MESSAGE_MAP(MockVideoCaptureHost, *message) | 135 IPC_BEGIN_MESSAGE_MAP(MockVideoCaptureHost, *message) |
| 135 IPC_MESSAGE_HANDLER(VideoCaptureMsg_NewBuffer, OnNewBufferCreatedDispatch) | 136 IPC_MESSAGE_HANDLER(VideoCaptureMsg_NewBuffer, OnNewBufferCreatedDispatch) |
| 136 IPC_MESSAGE_HANDLER(VideoCaptureMsg_BufferReady, OnBufferFilledDispatch) | 137 IPC_MESSAGE_HANDLER(VideoCaptureMsg_BufferReady, OnBufferFilledDispatch) |
| 137 IPC_MESSAGE_HANDLER(VideoCaptureMsg_StateChanged, OnStateChangedDispatch) | 138 IPC_MESSAGE_HANDLER(VideoCaptureMsg_StateChanged, OnStateChangedDispatch) |
| 138 IPC_MESSAGE_HANDLER(VideoCaptureMsg_DeviceInfo, OnDeviceInfoDispatch) | 139 IPC_MESSAGE_HANDLER(VideoCaptureMsg_DeviceInfo, OnDeviceInfoDispatch) |
| 139 IPC_MESSAGE_UNHANDLED(handled = false) | 140 IPC_MESSAGE_UNHANDLED(handled = false) |
| 140 IPC_END_MESSAGE_MAP() | 141 IPC_END_MESSAGE_MAP() |
| 141 EXPECT_TRUE(handled); | 142 EXPECT_TRUE(handled); |
| 142 | 143 |
| 143 delete message; | 144 delete message; |
| 144 return true; | 145 return true; |
| 145 } | 146 } |
| 146 | 147 |
| 148 virtual media_stream::VideoCaptureManager* GetVideoCaptureManager() OVERRIDE { | |
| 149 return manager_->video_capture_manager(); | |
| 150 } | |
| 151 | |
| 147 // These handler methods do minimal things and delegate to the mock methods. | 152 // These handler methods do minimal things and delegate to the mock methods. |
| 148 void OnNewBufferCreatedDispatch(int device_id, | 153 void OnNewBufferCreatedDispatch(int device_id, |
| 149 base::SharedMemoryHandle handle, | 154 base::SharedMemoryHandle handle, |
| 150 int length, int buffer_id) { | 155 int length, int buffer_id) { |
| 151 OnNewBufferCreated(device_id, handle, length, buffer_id); | 156 OnNewBufferCreated(device_id, handle, length, buffer_id); |
| 152 base::SharedMemory* dib = new base::SharedMemory(handle, false); | 157 base::SharedMemory* dib = new base::SharedMemory(handle, false); |
| 153 dib->Map(length); | 158 dib->Map(length); |
| 154 filled_dib_[buffer_id] = dib; | 159 filled_dib_[buffer_id] = dib; |
| 155 } | 160 } |
| 156 | 161 |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 178 if (dump_video_) { | 183 if (dump_video_) { |
| 179 dumper_.StartDump(params.width, params.height); | 184 dumper_.StartDump(params.width, params.height); |
| 180 } | 185 } |
| 181 OnDeviceInfo(device_id); | 186 OnDeviceInfo(device_id); |
| 182 } | 187 } |
| 183 | 188 |
| 184 std::map<int, base::SharedMemory*> filled_dib_; | 189 std::map<int, base::SharedMemory*> filled_dib_; |
| 185 bool return_buffers_; | 190 bool return_buffers_; |
| 186 bool dump_video_; | 191 bool dump_video_; |
| 187 DumpVideo dumper_; | 192 DumpVideo dumper_; |
| 193 media_stream::MediaStreamManager* manager_; | |
| 188 }; | 194 }; |
| 189 | 195 |
| 190 ACTION_P(ExitMessageLoop, message_loop) { | 196 ACTION_P(ExitMessageLoop, message_loop) { |
| 191 message_loop->PostTask(FROM_HERE, MessageLoop::QuitClosure()); | 197 message_loop->PostTask(FROM_HERE, MessageLoop::QuitClosure()); |
| 192 } | 198 } |
| 193 | 199 |
| 194 class VideoCaptureHostTest : public testing::Test { | 200 class VideoCaptureHostTest : public testing::Test { |
| 195 public: | 201 public: |
| 196 VideoCaptureHostTest() {} | 202 VideoCaptureHostTest() {} |
| 197 | 203 |
| 198 protected: | 204 protected: |
| 199 virtual void SetUp() { | 205 virtual void SetUp() OVERRIDE { |
| 200 // Create a message loop so VideoCaptureHostTest can use it. | 206 // Create a message loop so VideoCaptureHostTest can use it. |
| 201 message_loop_.reset(new MessageLoop(MessageLoop::TYPE_IO)); | 207 message_loop_.reset(new MessageLoop(MessageLoop::TYPE_IO)); |
| 202 | 208 |
| 203 // ResourceContext must be created on the UI thread. | |
| 204 ui_thread_.reset(new BrowserThreadImpl(BrowserThread::UI, | |
| 205 message_loop_.get())); | |
| 206 | |
| 207 // MediaStreamManager must be created on the IO thread. | 209 // MediaStreamManager must be created on the IO thread. |
| 208 io_thread_.reset(new BrowserThreadImpl(BrowserThread::IO, | 210 io_thread_.reset(new BrowserThreadImpl(BrowserThread::IO, |
| 209 message_loop_.get())); | 211 message_loop_.get())); |
| 210 | 212 |
| 213 // Create our own MediaStreamManager. | |
| 211 audio_manager_.reset(media::AudioManager::Create()); | 214 audio_manager_.reset(media::AudioManager::Create()); |
| 215 scoped_refptr<media_stream::AudioInputDeviceManager> | |
| 216 audio_input_device_manager( | |
| 217 new media_stream::AudioInputDeviceManager(audio_manager_.get())); | |
| 218 scoped_refptr<media_stream::VideoCaptureManager> video_capture_manager( | |
| 219 new media_stream::VideoCaptureManager()); | |
| 220 media_stream_manager_.reset(new media_stream::MediaStreamManager( | |
| 221 audio_input_device_manager, video_capture_manager)); | |
| 212 | 222 |
| 213 #ifndef TEST_REAL_CAPTURE_DEVICE | 223 #ifndef TEST_REAL_CAPTURE_DEVICE |
| 214 media_stream::MediaStreamManager::GetForResourceContext( | 224 media_stream_manager_->UseFakeDevice(); |
|
tommi (sloooow) - chröme
2012/07/04 13:46:48
Same thing here. (maybe adding a TODO in this cl w
no longer working on chromium
2012/07/04 14:31:39
Done, I added a TODO in media_stream_manager.h, wh
| |
| 215 &resource_context_, audio_manager_.get())->UseFakeDevice(); | |
| 216 #endif | 225 #endif |
| 217 | 226 |
| 218 host_ = new MockVideoCaptureHost(&resource_context_, audio_manager_.get()); | 227 host_ = new MockVideoCaptureHost(media_stream_manager_.get()); |
| 219 | 228 |
| 220 // Simulate IPC channel connected. | 229 // Simulate IPC channel connected. |
| 221 host_->OnChannelConnected(base::GetCurrentProcId()); | 230 host_->OnChannelConnected(base::GetCurrentProcId()); |
| 222 } | 231 } |
| 223 | 232 |
| 224 virtual void TearDown() { | 233 virtual void TearDown() OVERRIDE { |
| 225 // Verifies and removes the expectations on host_ and | 234 // Verifies and removes the expectations on host_ and |
| 226 // returns true iff successful. | 235 // returns true iff successful. |
| 227 Mock::VerifyAndClearExpectations(host_); | 236 Mock::VerifyAndClearExpectations(host_); |
| 228 | 237 |
| 229 EXPECT_CALL(*host_, OnStateChanged(kDeviceId, | 238 EXPECT_CALL(*host_, OnStateChanged(kDeviceId, |
| 230 video_capture::kStopped)) | 239 video_capture::kStopped)) |
| 231 .Times(AnyNumber()); | 240 .Times(AnyNumber()); |
| 232 | 241 |
| 233 // Simulate closing the IPC channel. | 242 // Simulate closing the IPC channel. |
| 234 host_->OnChannelClosing(); | 243 host_->OnChannelClosing(); |
| 235 | 244 |
| 236 // Release the reference to the mock object. The object will be destructed | 245 // Release the reference to the mock object. The object will be destructed |
| 237 // on message_loop_. | 246 // on message_loop_. |
| 238 host_ = NULL; | 247 host_ = NULL; |
| 239 | 248 |
| 240 // We need to continue running message_loop_ to complete all destructions. | 249 // We need to continue running message_loop_ to complete all destructions. |
| 241 SyncWithVideoCaptureManagerThread(); | 250 message_loop_->RunAllPending(); |
| 242 } | |
| 243 | |
| 244 // Called on the VideoCaptureManager thread. | |
| 245 static void PostQuitMessageLoop(MessageLoop* message_loop) { | |
| 246 message_loop->PostTask(FROM_HERE, MessageLoop::QuitClosure()); | |
| 247 } | |
| 248 | |
| 249 // Called on the main thread. | |
| 250 static void PostQuitOnVideoCaptureManagerThread( | |
| 251 MessageLoop* message_loop, content::ResourceContext* resource_context, | |
| 252 media:: AudioManager* audio_manager) { | |
| 253 media_stream::MediaStreamManager* manager = | |
| 254 media_stream::MediaStreamManager::GetForResourceContext( | |
| 255 resource_context, audio_manager); | |
| 256 manager->video_capture_manager()->GetMessageLoop()->PostTask( | |
| 257 FROM_HERE, base::Bind(&PostQuitMessageLoop, message_loop)); | |
| 258 } | |
| 259 | |
| 260 // SyncWithVideoCaptureManagerThread() waits until all pending tasks on the | |
| 261 // video_capture_manager thread are executed while also processing pending | |
| 262 // task in message_loop_ on the current thread. It is used to synchronize | |
| 263 // with the video capture manager thread when we are stopping a video | |
| 264 // capture device. | |
| 265 void SyncWithVideoCaptureManagerThread() { | |
| 266 message_loop_->PostTask( | |
| 267 FROM_HERE, | |
| 268 base::Bind(&PostQuitOnVideoCaptureManagerThread, message_loop_.get(), | |
| 269 &resource_context_, audio_manager_.get())); | |
| 270 message_loop_->Run(); | |
| 271 } | 251 } |
| 272 | 252 |
| 273 void StartCapture() { | 253 void StartCapture() { |
| 274 InSequence s; | 254 InSequence s; |
| 275 // 1. First - get info about the new resolution | 255 // 1. First - get info about the new resolution |
| 276 EXPECT_CALL(*host_, OnDeviceInfo(kDeviceId)); | 256 EXPECT_CALL(*host_, OnDeviceInfo(kDeviceId)); |
| 277 | 257 |
| 278 // 2. Change state to started | 258 // 2. Change state to started |
| 279 EXPECT_CALL(*host_, OnStateChanged(kDeviceId, | 259 EXPECT_CALL(*host_, OnStateChanged(kDeviceId, |
| 280 video_capture::kStarted)); | 260 video_capture::kStarted)); |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 318 params.frame_per_second = frame_rate; | 298 params.frame_per_second = frame_rate; |
| 319 params.session_id = kTestFakeDeviceId; | 299 params.session_id = kTestFakeDeviceId; |
| 320 host_->SetDumpVideo(true); | 300 host_->SetDumpVideo(true); |
| 321 host_->OnStartCapture(kDeviceId, params); | 301 host_->OnStartCapture(kDeviceId, params); |
| 322 message_loop_->Run(); | 302 message_loop_->Run(); |
| 323 } | 303 } |
| 324 | 304 |
| 325 void StopCapture() { | 305 void StopCapture() { |
| 326 EXPECT_CALL(*host_, OnStateChanged(kDeviceId, | 306 EXPECT_CALL(*host_, OnStateChanged(kDeviceId, |
| 327 video_capture::kStopped)) | 307 video_capture::kStopped)) |
| 328 .Times(AtLeast(1)); | 308 .WillOnce(ExitMessageLoop(message_loop_.get())); |
| 329 | 309 |
| 330 host_->OnStopCapture(kDeviceId); | 310 host_->OnStopCapture(kDeviceId); |
| 331 host_->SetReturnReceviedDibs(true); | 311 host_->SetReturnReceviedDibs(true); |
| 332 host_->ReturnReceivedDibs(kDeviceId); | 312 host_->ReturnReceivedDibs(kDeviceId); |
| 333 | 313 |
| 334 SyncWithVideoCaptureManagerThread(); | 314 message_loop_->Run(); |
| 315 | |
| 335 host_->SetReturnReceviedDibs(false); | 316 host_->SetReturnReceviedDibs(false); |
| 336 // Expect the VideoCaptureDevice has been stopped | 317 // Expect the VideoCaptureDevice has been stopped |
| 337 EXPECT_EQ(0u, host_->entries_.size()); | 318 EXPECT_EQ(0u, host_->entries_.size()); |
| 338 } | 319 } |
| 339 | 320 |
| 340 void NotifyPacketReady() { | 321 void NotifyPacketReady() { |
| 341 EXPECT_CALL(*host_, OnBufferFilled(kDeviceId, _, _)) | 322 EXPECT_CALL(*host_, OnBufferFilled(kDeviceId, _, _)) |
| 342 .Times(AnyNumber()) | 323 .Times(AnyNumber()) |
| 343 .WillOnce(ExitMessageLoop(message_loop_.get())) | 324 .WillOnce(ExitMessageLoop(message_loop_.get())) |
| 344 .RetiresOnSaturation(); | 325 .RetiresOnSaturation(); |
| 345 message_loop_->Run(); | 326 message_loop_->Run(); |
| 346 } | 327 } |
| 347 | 328 |
| 348 void ReturnReceivedPackets() { | 329 void ReturnReceivedPackets() { |
| 349 host_->ReturnReceivedDibs(kDeviceId); | 330 host_->ReturnReceivedDibs(kDeviceId); |
| 350 } | 331 } |
| 351 | 332 |
| 352 void SimulateError() { | 333 void SimulateError() { |
| 353 // Expect a change state to error state sent through IPC. | 334 // Expect a change state to error state sent through IPC. |
| 354 EXPECT_CALL(*host_, OnStateChanged(kDeviceId, | 335 EXPECT_CALL(*host_, OnStateChanged(kDeviceId, |
| 355 video_capture::kError)) | 336 video_capture::kError)) |
| 356 .Times(1); | 337 .Times(1); |
| 357 VideoCaptureControllerID id(kDeviceId); | 338 VideoCaptureControllerID id(kDeviceId); |
| 358 host_->OnError(id); | 339 host_->OnError(id); |
| 359 SyncWithVideoCaptureManagerThread(); | 340 // Wait for the error callback. |
| 341 message_loop_->RunAllPending(); | |
| 360 } | 342 } |
| 361 | 343 |
| 362 scoped_refptr<MockVideoCaptureHost> host_; | 344 scoped_refptr<MockVideoCaptureHost> host_; |
| 363 | 345 |
| 364 private: | 346 private: |
| 365 scoped_ptr<MessageLoop> message_loop_; | 347 scoped_ptr<MessageLoop> message_loop_; |
| 366 scoped_ptr<BrowserThreadImpl> ui_thread_; | |
| 367 scoped_ptr<BrowserThreadImpl> io_thread_; | 348 scoped_ptr<BrowserThreadImpl> io_thread_; |
| 368 scoped_ptr<media::AudioManager> audio_manager_; | 349 scoped_ptr<media::AudioManager> audio_manager_; |
| 369 content::MockResourceContext resource_context_; | 350 scoped_ptr<media_stream::MediaStreamManager> media_stream_manager_; |
| 370 | 351 |
| 371 DISALLOW_COPY_AND_ASSIGN(VideoCaptureHostTest); | 352 DISALLOW_COPY_AND_ASSIGN(VideoCaptureHostTest); |
| 372 }; | 353 }; |
| 373 | 354 |
| 374 TEST_F(VideoCaptureHostTest, StartCapture) { | 355 TEST_F(VideoCaptureHostTest, StartCapture) { |
| 375 StartCapture(); | 356 StartCapture(); |
| 376 } | 357 } |
| 377 | 358 |
| 378 TEST_F(VideoCaptureHostTest, StartCapturePlayStop) { | 359 TEST_F(VideoCaptureHostTest, StartCapturePlayStop) { |
| 379 StartCapture(); | 360 StartCapture(); |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 400 } | 381 } |
| 401 | 382 |
| 402 #ifdef DUMP_VIDEO | 383 #ifdef DUMP_VIDEO |
| 403 TEST_F(VideoCaptureHostTest, CaptureAndDumpVideoVga) { | 384 TEST_F(VideoCaptureHostTest, CaptureAndDumpVideoVga) { |
| 404 CaptureAndDumpVideo(640, 480, 30); | 385 CaptureAndDumpVideo(640, 480, 30); |
| 405 } | 386 } |
| 406 TEST_F(VideoCaptureHostTest, CaptureAndDump720P) { | 387 TEST_F(VideoCaptureHostTest, CaptureAndDump720P) { |
| 407 CaptureAndDumpVideo(1280, 720, 30); | 388 CaptureAndDumpVideo(1280, 720, 30); |
| 408 } | 389 } |
| 409 #endif | 390 #endif |
| OLD | NEW |