Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include <list> | |
| 6 #include <string> | |
| 7 | |
| 8 #include "base/file_util.h" | |
| 9 #include "base/memory/scoped_ptr.h" | |
| 10 #include "base/message_loop.h" | |
| 11 #include "base/process_util.h" | |
| 12 #include "base/stringprintf.h" | |
| 13 #include "content/browser/browser_thread.h" | |
| 14 #include "content/browser/media_stream/video_capture_manager.h" | |
| 15 #include "content/browser/renderer_host/video_capture_host.h" | |
| 16 #include "content/common/video_capture_messages.h" | |
| 17 #include "media/video/capture/video_capture_types.h" | |
| 18 | |
| 19 #include "testing/gmock/include/gmock/gmock.h" | |
| 20 #include "testing/gtest/include/gtest/gtest.h" | |
| 21 | |
| 22 using ::testing::_; | |
| 23 using ::testing::AtLeast; | |
| 24 using ::testing::AnyNumber; | |
| 25 using ::testing::DoAll; | |
| 26 using ::testing::InSequence; | |
| 27 using ::testing::Mock; | |
| 28 using ::testing::Return; | |
| 29 | |
| 30 // IPC::Msg.routing_id. | |
| 31 static const int32 kRouteId = 200; | |
| 32 // Id used to identify the capture session between renderer and | |
| 33 // video_capture_host. | |
| 34 static const int kDeviceId = 1; | |
| 35 // Id of a video capture device | |
| 36 static const media::VideoCaptureSessionId kTestFakeDeviceId = | |
| 37 media_stream::VideoCaptureManager::kStartOpenSessionId; | |
| 38 | |
| 39 // Define to enable test where video is dumped to file. | |
| 40 // #define DUMP_VIDEO | |
| 41 | |
| 42 // Define to use a real video capture device. | |
| 43 // #define TEST_REAL_CAPTURE_DEVICE | |
| 44 | |
| 45 // Simple class used for dumping video to a file. This can be used for | |
| 46 // verifying the output. | |
| 47 class DumpVideo { | |
| 48 public: | |
| 49 DumpVideo() : expected_size_(0) {} | |
| 50 void StartDump(int widht, int height) { | |
| 51 // Create an FilePath that works on all platforms. There should | |
| 52 // be a better way. | |
| 53 FilePath file_name = | |
| 54 FilePath::FromWStringHack(StringPrintf(L"dump_w%d_h%d.yuv", widht, | |
| 55 height)); | |
| 56 file_.reset(file_util::OpenFile(file_name, "wb")); | |
| 57 expected_size_ = widht * height * 3 / 2; | |
| 58 } | |
| 59 void NewVideoFrame(const void* buffer) { | |
| 60 if (file_.get() != NULL) { | |
| 61 fwrite(buffer, expected_size_, 1, file_.get()); | |
| 62 } | |
| 63 } | |
| 64 | |
| 65 private: | |
| 66 file_util::ScopedFILE file_; | |
| 67 int expected_size_; | |
| 68 }; | |
| 69 | |
| 70 class MockVideoCaptureHost : public VideoCaptureHost { | |
| 71 public: | |
| 72 MockVideoCaptureHost() : return_buffers_(false), dump_video_(false) {} | |
| 73 virtual ~MockVideoCaptureHost() {} | |
| 74 | |
| 75 // A list of mock methods. | |
| 76 MOCK_METHOD3(OnBufferFilled, | |
| 77 void(int routing_id, int device_id, TransportDIB::Handle)); | |
| 78 MOCK_METHOD3(OnStateChanged, | |
| 79 void(int routing_id, int device_id, | |
| 80 media::VideoCapture::State state)); | |
| 81 MOCK_METHOD2(OnDeviceInfo, void(int routing_id, int device_id)); | |
| 82 | |
| 83 // Use class DumpVideo to write I420 video to file. | |
| 84 void SetDumpVideo(bool enable) { | |
| 85 dump_video_ = enable; | |
| 86 } | |
| 87 void SetReturnReceviedDibs(bool enable) { | |
| 88 return_buffers_ = enable; | |
| 89 } | |
| 90 | |
| 91 // Return Dibs we currently have received. | |
| 92 void ReturnReceivedDibs(int device_id) { | |
| 93 TransportDIB::Handle handle = GetReceivedDib(); | |
| 94 while (handle != 0) { | |
| 95 IPC::Message msg; | |
| 96 msg.set_routing_id(kRouteId); | |
| 97 this->OnReceiveEmptyBuffer(msg, device_id, handle); | |
| 98 handle = GetReceivedDib(); | |
| 99 } | |
| 100 } | |
| 101 TransportDIB::Handle GetReceivedDib() { | |
| 102 if (filled_dib_.empty()) | |
| 103 return 0; | |
| 104 TransportDIB::Handle h = filled_dib_.front(); | |
| 105 filled_dib_.pop_front(); | |
| 106 | |
|
wjia(left Chromium)
2011/05/19 04:24:37
need only one blank line.
Per K
2011/05/19 08:55:31
Done.
| |
| 107 | |
| 108 return h; | |
| 109 } | |
| 110 | |
| 111 private: | |
| 112 // This method is used to dispatch IPC messages to the renderer. We intercept | |
| 113 // these messages here and dispatch to our mock methods to verify the | |
| 114 // conversation between this object and the renderer. | |
| 115 virtual bool Send(IPC::Message* message) { | |
| 116 CHECK(message); | |
| 117 | |
| 118 // In this method we dispatch the messages to the according handlers as if | |
| 119 // we are the renderer. | |
| 120 bool handled = true; | |
| 121 IPC_BEGIN_MESSAGE_MAP(MockVideoCaptureHost, *message) | |
| 122 IPC_MESSAGE_HANDLER(VideoCaptureMsg_BufferReady, OnBufferFilled) | |
| 123 IPC_MESSAGE_HANDLER(VideoCaptureMsg_StateChanged, OnStateChanged) | |
| 124 IPC_MESSAGE_HANDLER(VideoCaptureMsg_DeviceInfo, OnDeviceInfo) | |
| 125 IPC_MESSAGE_UNHANDLED(handled = false) | |
| 126 IPC_END_MESSAGE_MAP() | |
| 127 EXPECT_TRUE(handled); | |
| 128 | |
| 129 delete message; | |
| 130 return true; | |
| 131 } | |
| 132 | |
| 133 // These handler methods do minimal things and delegate to the mock methods. | |
| 134 void OnBufferFilled(const IPC::Message& msg, int device_id, | |
| 135 TransportDIB::Handle dib_handle) { | |
| 136 if (dump_video_) { | |
| 137 TransportDIB* dib = TransportDIB::Map(dib_handle); | |
| 138 ASSERT_TRUE(dib != NULL); | |
| 139 dumper_.NewVideoFrame(dib->memory()); | |
| 140 } | |
| 141 | |
| 142 OnBufferFilled(msg.routing_id(), device_id, dib_handle); | |
| 143 if (return_buffers_) { | |
| 144 VideoCaptureHost::OnReceiveEmptyBuffer(msg, device_id, dib_handle); | |
| 145 } else { | |
| 146 filled_dib_.push_back(dib_handle); | |
| 147 } | |
| 148 } | |
| 149 | |
| 150 void OnStateChanged(const IPC::Message& msg, int device_id, | |
| 151 media::VideoCapture::State state) { | |
| 152 OnStateChanged(msg.routing_id(), device_id, state); | |
| 153 } | |
| 154 | |
| 155 void OnDeviceInfo(const IPC::Message& msg, int device_id, | |
| 156 media::VideoCaptureParams params) { | |
| 157 if (dump_video_) { | |
| 158 dumper_.StartDump(params.width, params.height); | |
| 159 } | |
| 160 OnDeviceInfo(msg.routing_id(), device_id); | |
| 161 } | |
| 162 | |
| 163 std::list<TransportDIB::Handle> filled_dib_; | |
| 164 bool return_buffers_; | |
| 165 bool dump_video_; | |
| 166 DumpVideo dumper_; | |
| 167 }; | |
| 168 | |
| 169 ACTION_P(ExitMessageLoop, message_loop) { | |
| 170 message_loop->PostTask(FROM_HERE, new MessageLoop::QuitTask()); | |
| 171 } | |
| 172 | |
| 173 class VideoCaptureHostTest : public testing::Test { | |
| 174 public: | |
| 175 VideoCaptureHostTest() {} | |
| 176 | |
| 177 protected: | |
| 178 virtual void SetUp() { | |
| 179 // Setup the VideoCaptureManager to use fake video capture device. | |
| 180 #ifndef TEST_REAL_CAPTURE_DEVICE | |
| 181 media_stream::VideoCaptureManager::CreateTestManager(); | |
| 182 #endif | |
| 183 // Create a message loop so VideoCaptureHostTest can use it. | |
| 184 message_loop_.reset(new MessageLoop(MessageLoop::TYPE_IO)); | |
| 185 io_thread_.reset(new BrowserThread(BrowserThread::IO, message_loop_.get())); | |
| 186 host_ = new MockVideoCaptureHost(); | |
| 187 | |
| 188 // Simulate IPC channel connected. | |
| 189 host_->OnChannelConnected(base::GetCurrentProcId()); | |
| 190 } | |
| 191 | |
| 192 virtual void TearDown() { | |
| 193 // Verifies and removes the expectations on mock_obj; | |
| 194 // returns true iff successful. | |
| 195 Mock::VerifyAndClearExpectations(host_); | |
| 196 | |
| 197 ON_CALL(*host_, OnStateChanged(kRouteId, kDeviceId, | |
| 198 media::VideoCapture::kStopped)) | |
| 199 .WillByDefault(Return()); | |
|
wjia(left Chromium)
2011/05/19 04:24:37
there is a gmock warning when unit test is run:
G
Per K
2011/05/19 08:55:31
Changed to Expect_Call Anynumber.
| |
| 200 // Simulate closing the IPC channel. | |
| 201 host_->OnChannelClosing(); | |
| 202 | |
| 203 // Release the reference to the mock object. The object will be destructed | |
| 204 // on message_loop_. | |
| 205 host_ = NULL; | |
| 206 | |
| 207 // We need to continue running message_loop_ to complete all destructions. | |
| 208 SyncWithVideoCaptureManagerThread(); | |
| 209 | |
| 210 io_thread_.reset(); | |
| 211 } | |
| 212 | |
| 213 // Called on the VideoCaptureManager thread. | |
| 214 static void PostQuitMessageLoop(MessageLoop* message_loop) { | |
| 215 message_loop->PostTask(FROM_HERE, new MessageLoop::QuitTask()); | |
| 216 } | |
| 217 | |
| 218 // Called on the main thread. | |
| 219 static void PostQuitOnVideoCaptureManagerThread(MessageLoop* message_loop) { | |
| 220 media_stream::VideoCaptureManager::Get()->GetMessageLoop()->PostTask( | |
| 221 FROM_HERE, NewRunnableFunction(&PostQuitMessageLoop, message_loop)); | |
| 222 } | |
| 223 | |
| 224 // SyncWithVideoCaptureManagerThread() waits until all pending tasks on the | |
| 225 // video_capture_manager thread are executed while also processing pending | |
| 226 // task in message_loop_ on the current thread. It is used to synchronize | |
| 227 // with the video capture manager thread when we are stopping a video | |
| 228 // capture device. | |
| 229 void SyncWithVideoCaptureManagerThread() { | |
| 230 message_loop_->PostTask( | |
| 231 FROM_HERE, NewRunnableFunction(&PostQuitOnVideoCaptureManagerThread, | |
| 232 message_loop_.get())); | |
| 233 message_loop_->Run(); | |
| 234 } | |
| 235 | |
| 236 void StartCapture() { | |
| 237 InSequence s; | |
| 238 // 1. First - get info about the new resolution | |
| 239 EXPECT_CALL(*host_, OnDeviceInfo(kRouteId, kDeviceId)); | |
| 240 | |
| 241 // 2. Change state to started | |
| 242 EXPECT_CALL(*host_, OnStateChanged(kRouteId, kDeviceId, | |
| 243 media::VideoCapture::kStarted)); | |
| 244 | |
| 245 // 3. First filled buffer will arrive. | |
| 246 EXPECT_CALL(*host_, OnBufferFilled(kRouteId, kDeviceId, _)) | |
| 247 .Times(AnyNumber()) | |
| 248 .WillOnce(ExitMessageLoop(message_loop_.get())); | |
| 249 | |
| 250 IPC::Message msg; | |
| 251 msg.set_routing_id(kRouteId); | |
| 252 | |
| 253 media::VideoCaptureParams params; | |
| 254 params.width = 352; | |
| 255 params.height = 288; | |
| 256 params.frame_per_second = 30; | |
| 257 params.session_id = kTestFakeDeviceId; | |
| 258 host_->OnStartCapture(msg, kDeviceId, params); | |
| 259 message_loop_->Run(); | |
| 260 } | |
| 261 | |
| 262 void CaptureAndDumpVideo(int width, int heigt, int frame_rate) { | |
| 263 InSequence s; | |
| 264 // 1. First - get info about the new resolution | |
| 265 EXPECT_CALL(*host_, OnDeviceInfo(kRouteId, kDeviceId)); | |
| 266 | |
| 267 // 2. Change state to started | |
| 268 EXPECT_CALL(*host_, OnStateChanged(kRouteId, kDeviceId, | |
| 269 media::VideoCapture::kStarted)); | |
| 270 | |
| 271 // 3. First filled buffer will arrive. | |
| 272 EXPECT_CALL(*host_, OnBufferFilled(kRouteId, kDeviceId, _)) | |
| 273 .Times(AnyNumber()) | |
| 274 .WillOnce(ExitMessageLoop(message_loop_.get())); | |
| 275 | |
| 276 IPC::Message msg; | |
| 277 msg.set_routing_id(kRouteId); | |
| 278 | |
| 279 media::VideoCaptureParams params; | |
| 280 params.width = width; | |
| 281 params.height = heigt; | |
| 282 params.frame_per_second = frame_rate; | |
| 283 params.session_id = kTestFakeDeviceId; | |
| 284 host_->SetDumpVideo(true); | |
| 285 host_->OnStartCapture(msg, kDeviceId, params); | |
| 286 message_loop_->Run(); | |
| 287 } | |
| 288 | |
| 289 void StopCapture() { | |
| 290 EXPECT_CALL(*host_, OnStateChanged(kRouteId, kDeviceId, | |
| 291 media::VideoCapture::kStopped)) | |
| 292 .Times(AtLeast(1)); | |
| 293 | |
| 294 IPC::Message msg; | |
| 295 msg.set_routing_id(kRouteId); | |
| 296 host_->OnStopCapture(msg, kDeviceId); | |
| 297 host_->SetReturnReceviedDibs(true); | |
| 298 host_->ReturnReceivedDibs(kDeviceId); | |
| 299 | |
| 300 SyncWithVideoCaptureManagerThread(); | |
| 301 host_->SetReturnReceviedDibs(false); | |
| 302 // Expect the VideoCaptureDevice has been stopped | |
| 303 EXPECT_EQ(0u, host_->entries_.size()); | |
| 304 } | |
| 305 | |
| 306 void NotifyPacketReady() { | |
| 307 EXPECT_CALL(*host_, OnBufferFilled(kRouteId, kDeviceId, _)) | |
| 308 .Times(AnyNumber()) | |
| 309 .WillOnce(ExitMessageLoop(message_loop_.get())) | |
| 310 .RetiresOnSaturation(); | |
| 311 message_loop_->Run(); | |
| 312 } | |
| 313 | |
| 314 void ReturnReceivedPackets() { | |
| 315 host_->ReturnReceivedDibs(kDeviceId); | |
| 316 } | |
| 317 | |
| 318 void SimulateError() { | |
| 319 // Expect a change state to error state sent through IPC. | |
| 320 EXPECT_CALL(*host_, OnStateChanged(kRouteId, kDeviceId, | |
| 321 media::VideoCapture::kError)) | |
| 322 .Times(1); | |
| 323 VideoCaptureMemory::VideoCaptureMemoryId id(kRouteId, kDeviceId); | |
| 324 host_->OnError(id); | |
| 325 SyncWithVideoCaptureManagerThread(); | |
| 326 } | |
| 327 | |
| 328 scoped_refptr<MockVideoCaptureHost> host_; | |
| 329 private: | |
| 330 scoped_ptr<MessageLoop> message_loop_; | |
| 331 scoped_ptr<BrowserThread> io_thread_; | |
| 332 | |
| 333 DISALLOW_COPY_AND_ASSIGN(VideoCaptureHostTest); | |
| 334 }; | |
| 335 | |
| 336 TEST_F(VideoCaptureHostTest, StartCapture) { | |
| 337 StartCapture(); | |
| 338 } | |
| 339 | |
| 340 TEST_F(VideoCaptureHostTest, StartCapturePlayStop) { | |
| 341 StartCapture(); | |
| 342 NotifyPacketReady(); | |
| 343 NotifyPacketReady(); | |
| 344 ReturnReceivedPackets(); | |
| 345 StopCapture(); | |
| 346 } | |
| 347 | |
| 348 TEST_F(VideoCaptureHostTest, StartCaptureErrorStop) { | |
| 349 StartCapture(); | |
| 350 SimulateError(); | |
| 351 StopCapture(); | |
| 352 } | |
| 353 | |
| 354 TEST_F(VideoCaptureHostTest, StartCaptureError) { | |
| 355 EXPECT_CALL(*host_, OnStateChanged(kRouteId, kDeviceId, | |
| 356 media::VideoCapture::kStopped)) | |
| 357 .Times(0); | |
| 358 StartCapture(); | |
| 359 NotifyPacketReady(); | |
| 360 SimulateError(); | |
| 361 base::PlatformThread::Sleep(200); | |
| 362 } | |
| 363 | |
| 364 #ifdef DUMP_VIDEO | |
| 365 TEST_F(VideoCaptureHostTest, CaptureAndDumpVideoVga) { | |
| 366 CaptureAndDumpVideo(640, 480, 30); | |
| 367 } | |
| 368 TEST_F(VideoCaptureHostTest, CaptureAndDump720P) { | |
| 369 CaptureAndDumpVideo(1280, 720, 30); | |
| 370 } | |
| 371 #endif | |
| OLD | NEW |