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