| 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 <string> | 5 #include <string> |
| 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 "content/browser/browser_thread_impl.h" | 9 #include "content/browser/browser_thread_impl.h" |
| 10 #include "content/browser/renderer_host/media/media_stream_dispatcher_host.h" | 10 #include "content/browser/renderer_host/media/media_stream_dispatcher_host.h" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 #include "media/video/capture/fake_video_capture_device.h" | 21 #include "media/video/capture/fake_video_capture_device.h" |
| 22 #include "net/url_request/url_request_context.h" | 22 #include "net/url_request/url_request_context.h" |
| 23 #include "testing/gmock/include/gmock/gmock.h" | 23 #include "testing/gmock/include/gmock/gmock.h" |
| 24 #include "testing/gtest/include/gtest/gtest.h" | 24 #include "testing/gtest/include/gtest/gtest.h" |
| 25 | 25 |
| 26 using ::testing::_; | 26 using ::testing::_; |
| 27 using ::testing::DeleteArg; | 27 using ::testing::DeleteArg; |
| 28 using ::testing::DoAll; | 28 using ::testing::DoAll; |
| 29 using ::testing::Return; | 29 using ::testing::Return; |
| 30 | 30 |
| 31 using content::BrowserThread; | |
| 32 using content::BrowserThreadImpl; | |
| 33 | |
| 34 const int kProcessId = 5; | 31 const int kProcessId = 5; |
| 35 const int kRenderId = 6; | 32 const int kRenderId = 6; |
| 36 const int kPageRequestId = 7; | 33 const int kPageRequestId = 7; |
| 37 | 34 |
| 38 namespace media_stream { | 35 namespace content { |
| 39 | 36 |
| 40 class MockMediaStreamDispatcherHost : public MediaStreamDispatcherHost, | 37 class MockMediaStreamDispatcherHost : public MediaStreamDispatcherHost, |
| 41 public content::TestContentBrowserClient { | 38 public TestContentBrowserClient { |
| 42 public: | 39 public: |
| 43 MockMediaStreamDispatcherHost(MessageLoop* message_loop, | 40 MockMediaStreamDispatcherHost(MessageLoop* message_loop, |
| 44 MediaStreamManager* manager) | 41 MediaStreamManager* manager) |
| 45 : MediaStreamDispatcherHost(kProcessId), | 42 : MediaStreamDispatcherHost(kProcessId), |
| 46 message_loop_(message_loop), | 43 message_loop_(message_loop), |
| 47 manager_(manager) {} | 44 manager_(manager) {} |
| 48 | 45 |
| 49 // A list of mock methods. | 46 // A list of mock methods. |
| 50 MOCK_METHOD4(OnStreamGenerated, | 47 MOCK_METHOD4(OnStreamGenerated, |
| 51 void(int routing_id, int request_id, int audio_array_size, | 48 void(int routing_id, int request_id, int audio_array_size, |
| 52 int video_array_size)); | 49 int video_array_size)); |
| 53 MOCK_METHOD2(OnStreamGenerationFailed, void(int routing_id, int request_id)); | 50 MOCK_METHOD2(OnStreamGenerationFailed, void(int routing_id, int request_id)); |
| 54 MOCK_METHOD0(GetMediaObserver, content::MediaObserver*()); | 51 MOCK_METHOD0(GetMediaObserver, MediaObserver*()); |
| 55 | 52 |
| 56 // Accessor to private functions. | 53 // Accessor to private functions. |
| 57 void OnGenerateStream(int page_request_id, const StreamOptions& components) { | 54 void OnGenerateStream(int page_request_id, const StreamOptions& components) { |
| 58 MediaStreamDispatcherHost::OnGenerateStream(kRenderId, | 55 MediaStreamDispatcherHost::OnGenerateStream(kRenderId, |
| 59 page_request_id, | 56 page_request_id, |
| 60 components, | 57 components, |
| 61 GURL()); | 58 GURL()); |
| 62 } | 59 } |
| 63 void OnStopGeneratedStream(const std::string& label) { | 60 void OnStopGeneratedStream(const std::string& label) { |
| 64 MediaStreamDispatcherHost::OnStopGeneratedStream(kRenderId, label); | 61 MediaStreamDispatcherHost::OnStopGeneratedStream(kRenderId, label); |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 143 // MediaStreamManager must be created and called on IO thread. | 140 // MediaStreamManager must be created and called on IO thread. |
| 144 message_loop_.reset(new MessageLoop(MessageLoop::TYPE_IO)); | 141 message_loop_.reset(new MessageLoop(MessageLoop::TYPE_IO)); |
| 145 io_thread_.reset(new BrowserThreadImpl(BrowserThread::IO, | 142 io_thread_.reset(new BrowserThreadImpl(BrowserThread::IO, |
| 146 message_loop_.get())); | 143 message_loop_.get())); |
| 147 | 144 |
| 148 // Create our own media observer. | 145 // Create our own media observer. |
| 149 media_observer_.reset(new MockMediaObserver()); | 146 media_observer_.reset(new MockMediaObserver()); |
| 150 | 147 |
| 151 // Create our own MediaStreamManager. | 148 // Create our own MediaStreamManager. |
| 152 audio_manager_.reset(media::AudioManager::Create()); | 149 audio_manager_.reset(media::AudioManager::Create()); |
| 153 media_stream_manager_.reset( | 150 media_stream_manager_.reset(new MediaStreamManager(audio_manager_.get())); |
| 154 new media_stream::MediaStreamManager(audio_manager_.get())); | |
| 155 // Make sure we use fake devices to avoid long delays. | 151 // Make sure we use fake devices to avoid long delays. |
| 156 media_stream_manager_->UseFakeDevice(); | 152 media_stream_manager_->UseFakeDevice(); |
| 157 | 153 |
| 158 host_ = new MockMediaStreamDispatcherHost(message_loop_.get(), | 154 host_ = new MockMediaStreamDispatcherHost(message_loop_.get(), |
| 159 media_stream_manager_.get()); | 155 media_stream_manager_.get()); |
| 160 | 156 |
| 161 // Use the fake content client and browser. | 157 // Use the fake content client and browser. |
| 162 old_client_ = content::GetContentClient(); | 158 old_client_ = GetContentClient(); |
| 163 old_browser_client_ = content::GetContentClient()->browser(); | 159 old_browser_client_ = GetContentClient()->browser(); |
| 164 content_client_.reset(new TestContentClient); | 160 content_client_.reset(new TestContentClient); |
| 165 content::SetContentClient(content_client_.get()); | 161 SetContentClient(content_client_.get()); |
| 166 content_client_->set_browser_for_testing(host_); | 162 content_client_->set_browser_for_testing(host_); |
| 167 } | 163 } |
| 168 | 164 |
| 169 virtual void TearDown() OVERRIDE { | 165 virtual void TearDown() OVERRIDE { |
| 170 message_loop_->RunAllPending(); | 166 message_loop_->RunAllPending(); |
| 171 | 167 |
| 172 // Recover the old browser client and content client. | 168 // Recover the old browser client and content client. |
| 173 content::GetContentClient()->set_browser_for_testing(old_browser_client_); | 169 GetContentClient()->set_browser_for_testing(old_browser_client_); |
| 174 content::SetContentClient(old_client_); | 170 SetContentClient(old_client_); |
| 175 content_client_.reset(); | 171 content_client_.reset(); |
| 176 | 172 |
| 177 // Delete the IO message loop to delete the device thread, | 173 // Delete the IO message loop to delete the device thread, |
| 178 // AudioInputDeviceManager and VideoCaptureManager. | 174 // AudioInputDeviceManager and VideoCaptureManager. |
| 179 message_loop_.reset(); | 175 message_loop_.reset(); |
| 180 } | 176 } |
| 181 | 177 |
| 182 scoped_refptr<MockMediaStreamDispatcherHost> host_; | 178 scoped_refptr<MockMediaStreamDispatcherHost> host_; |
| 183 scoped_ptr<MessageLoop> message_loop_; | 179 scoped_ptr<MessageLoop> message_loop_; |
| 184 scoped_ptr<BrowserThreadImpl> io_thread_; | 180 scoped_ptr<BrowserThreadImpl> io_thread_; |
| 185 scoped_ptr<media::AudioManager> audio_manager_; | 181 scoped_ptr<media::AudioManager> audio_manager_; |
| 186 scoped_ptr<MediaStreamManager> media_stream_manager_; | 182 scoped_ptr<MediaStreamManager> media_stream_manager_; |
| 187 content::ContentClient* old_client_; | 183 ContentClient* old_client_; |
| 188 content::ContentBrowserClient* old_browser_client_; | 184 ContentBrowserClient* old_browser_client_; |
| 189 scoped_ptr<content::ContentClient> content_client_; | 185 scoped_ptr<ContentClient> content_client_; |
| 190 scoped_ptr<MockMediaObserver> media_observer_; | 186 scoped_ptr<MockMediaObserver> media_observer_; |
| 191 }; | 187 }; |
| 192 | 188 |
| 193 TEST_F(MediaStreamDispatcherHostTest, GenerateStream) { | 189 TEST_F(MediaStreamDispatcherHostTest, GenerateStream) { |
| 194 StreamOptions options(content::MEDIA_NO_SERVICE, | 190 StreamOptions options(MEDIA_NO_SERVICE, MEDIA_DEVICE_VIDEO_CAPTURE); |
| 195 content::MEDIA_DEVICE_VIDEO_CAPTURE); | |
| 196 | 191 |
| 197 EXPECT_CALL(*host_, GetMediaObserver()) | 192 EXPECT_CALL(*host_, GetMediaObserver()) |
| 198 .WillRepeatedly(Return(media_observer_.get())); | 193 .WillRepeatedly(Return(media_observer_.get())); |
| 199 EXPECT_CALL(*host_, OnStreamGenerated(kRenderId, kPageRequestId, 0, 1)); | 194 EXPECT_CALL(*host_, OnStreamGenerated(kRenderId, kPageRequestId, 0, 1)); |
| 200 host_->OnGenerateStream(kPageRequestId, options); | 195 host_->OnGenerateStream(kPageRequestId, options); |
| 201 | 196 |
| 202 EXPECT_CALL(*media_observer_.get(), OnCaptureDevicesOpened(_, _, _)); | 197 EXPECT_CALL(*media_observer_.get(), OnCaptureDevicesOpened(_, _, _)); |
| 203 EXPECT_CALL(*media_observer_.get(), OnCaptureDevicesClosed(_, _, _)); | 198 EXPECT_CALL(*media_observer_.get(), OnCaptureDevicesClosed(_, _, _)); |
| 204 | 199 |
| 205 WaitForResult(); | 200 WaitForResult(); |
| 206 | 201 |
| 207 std::string label = host_->label_; | 202 std::string label = host_->label_; |
| 208 | 203 |
| 209 EXPECT_EQ(host_->audio_devices_.size(), 0u); | 204 EXPECT_EQ(host_->audio_devices_.size(), 0u); |
| 210 EXPECT_EQ(host_->video_devices_.size(), 1u); | 205 EXPECT_EQ(host_->video_devices_.size(), 1u); |
| 211 EXPECT_EQ(host_->NumberOfStreams(), 1u); | 206 EXPECT_EQ(host_->NumberOfStreams(), 1u); |
| 212 | 207 |
| 213 host_->OnStopGeneratedStream(label); | 208 host_->OnStopGeneratedStream(label); |
| 214 EXPECT_EQ(host_->NumberOfStreams(), 0u); | 209 EXPECT_EQ(host_->NumberOfStreams(), 0u); |
| 215 } | 210 } |
| 216 | 211 |
| 217 TEST_F(MediaStreamDispatcherHostTest, GenerateThreeStreams) { | 212 TEST_F(MediaStreamDispatcherHostTest, GenerateThreeStreams) { |
| 218 // This test opens three video capture devices. Two fake devices exists and it | 213 // This test opens three video capture devices. Two fake devices exists and it |
| 219 // is expected the last call to |Open()| will open the first device again, but | 214 // is expected the last call to |Open()| will open the first device again, but |
| 220 // with a different label. | 215 // with a different label. |
| 221 StreamOptions options(content::MEDIA_NO_SERVICE, | 216 StreamOptions options(MEDIA_NO_SERVICE, MEDIA_DEVICE_VIDEO_CAPTURE); |
| 222 content::MEDIA_DEVICE_VIDEO_CAPTURE); | |
| 223 | 217 |
| 224 // Generate first stream. | 218 // Generate first stream. |
| 225 EXPECT_CALL(*host_, GetMediaObserver()) | 219 EXPECT_CALL(*host_, GetMediaObserver()) |
| 226 .WillRepeatedly(Return(media_observer_.get())); | 220 .WillRepeatedly(Return(media_observer_.get())); |
| 227 EXPECT_CALL(*host_, OnStreamGenerated(kRenderId, kPageRequestId, 0, 1)); | 221 EXPECT_CALL(*host_, OnStreamGenerated(kRenderId, kPageRequestId, 0, 1)); |
| 228 host_->OnGenerateStream(kPageRequestId, options); | 222 host_->OnGenerateStream(kPageRequestId, options); |
| 229 | 223 |
| 230 EXPECT_CALL(*media_observer_.get(), OnCaptureDevicesOpened(_, _, _)); | 224 EXPECT_CALL(*media_observer_.get(), OnCaptureDevicesOpened(_, _, _)); |
| 231 | 225 |
| 232 WaitForResult(); | 226 WaitForResult(); |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 281 // Check that we now have three opened streams. | 275 // Check that we now have three opened streams. |
| 282 EXPECT_EQ(host_->NumberOfStreams(), 3u); | 276 EXPECT_EQ(host_->NumberOfStreams(), 3u); |
| 283 | 277 |
| 284 host_->OnStopGeneratedStream(label1); | 278 host_->OnStopGeneratedStream(label1); |
| 285 host_->OnStopGeneratedStream(label2); | 279 host_->OnStopGeneratedStream(label2); |
| 286 host_->OnStopGeneratedStream(label3); | 280 host_->OnStopGeneratedStream(label3); |
| 287 EXPECT_EQ(host_->NumberOfStreams(), 0u); | 281 EXPECT_EQ(host_->NumberOfStreams(), 0u); |
| 288 } | 282 } |
| 289 | 283 |
| 290 TEST_F(MediaStreamDispatcherHostTest, FailOpenVideoDevice) { | 284 TEST_F(MediaStreamDispatcherHostTest, FailOpenVideoDevice) { |
| 291 StreamOptions options(content::MEDIA_NO_SERVICE, | 285 StreamOptions options(MEDIA_NO_SERVICE, MEDIA_DEVICE_VIDEO_CAPTURE); |
| 292 content::MEDIA_DEVICE_VIDEO_CAPTURE); | |
| 293 | 286 |
| 294 EXPECT_CALL(*host_, GetMediaObserver()) | 287 EXPECT_CALL(*host_, GetMediaObserver()) |
| 295 .WillRepeatedly(Return(media_observer_.get())); | 288 .WillRepeatedly(Return(media_observer_.get())); |
| 296 media::FakeVideoCaptureDevice::SetFailNextCreate(); | 289 media::FakeVideoCaptureDevice::SetFailNextCreate(); |
| 297 media_stream_manager_->UseFakeDevice(); | 290 media_stream_manager_->UseFakeDevice(); |
| 298 host_->OnGenerateStream(kPageRequestId, options); | 291 host_->OnGenerateStream(kPageRequestId, options); |
| 299 EXPECT_CALL(*host_, OnStreamGenerationFailed(kRenderId, kPageRequestId)); | 292 EXPECT_CALL(*host_, OnStreamGenerationFailed(kRenderId, kPageRequestId)); |
| 300 WaitForResult(); | 293 WaitForResult(); |
| 301 } | 294 } |
| 302 | 295 |
| 303 TEST_F(MediaStreamDispatcherHostTest, CancelPendingStreamsOnChannelClosing) { | 296 TEST_F(MediaStreamDispatcherHostTest, CancelPendingStreamsOnChannelClosing) { |
| 304 StreamOptions options(content::MEDIA_NO_SERVICE, | 297 StreamOptions options(MEDIA_NO_SERVICE, MEDIA_DEVICE_VIDEO_CAPTURE); |
| 305 content::MEDIA_DEVICE_VIDEO_CAPTURE); | |
| 306 | 298 |
| 307 EXPECT_CALL(*host_, GetMediaObserver()) | 299 EXPECT_CALL(*host_, GetMediaObserver()) |
| 308 .WillRepeatedly(Return(media_observer_.get())); | 300 .WillRepeatedly(Return(media_observer_.get())); |
| 309 | 301 |
| 310 // Create multiple GenerateStream requests. | 302 // Create multiple GenerateStream requests. |
| 311 size_t streams = 5; | 303 size_t streams = 5; |
| 312 for (size_t i = 1; i <= streams; ++i) { | 304 for (size_t i = 1; i <= streams; ++i) { |
| 313 host_->OnGenerateStream(kPageRequestId + i, options); | 305 host_->OnGenerateStream(kPageRequestId + i, options); |
| 314 EXPECT_EQ(host_->NumberOfStreams(), i); | 306 EXPECT_EQ(host_->NumberOfStreams(), i); |
| 315 } | 307 } |
| 316 | 308 |
| 317 // Calling OnChannelClosing() to cancel all the pending requests. | 309 // Calling OnChannelClosing() to cancel all the pending requests. |
| 318 host_->OnChannelClosing(); | 310 host_->OnChannelClosing(); |
| 319 | 311 |
| 320 // Streams should have been cleaned up. | 312 // Streams should have been cleaned up. |
| 321 EXPECT_EQ(host_->NumberOfStreams(), 0u); | 313 EXPECT_EQ(host_->NumberOfStreams(), 0u); |
| 322 } | 314 } |
| 323 | 315 |
| 324 TEST_F(MediaStreamDispatcherHostTest, StopGeneratedStreamsOnChannelClosing) { | 316 TEST_F(MediaStreamDispatcherHostTest, StopGeneratedStreamsOnChannelClosing) { |
| 325 StreamOptions options(content::MEDIA_NO_SERVICE, | 317 StreamOptions options(MEDIA_NO_SERVICE, MEDIA_DEVICE_VIDEO_CAPTURE); |
| 326 content::MEDIA_DEVICE_VIDEO_CAPTURE); | |
| 327 | 318 |
| 328 EXPECT_CALL(*host_, GetMediaObserver()) | 319 EXPECT_CALL(*host_, GetMediaObserver()) |
| 329 .WillRepeatedly(Return(media_observer_.get())); | 320 .WillRepeatedly(Return(media_observer_.get())); |
| 330 | 321 |
| 331 // Create first group of streams. | 322 // Create first group of streams. |
| 332 size_t generated_streams = 3; | 323 size_t generated_streams = 3; |
| 333 for (size_t i = 0; i < generated_streams; ++i) { | 324 for (size_t i = 0; i < generated_streams; ++i) { |
| 334 EXPECT_CALL(*host_, OnStreamGenerated(kRenderId, kPageRequestId + i, 0, 1)); | 325 EXPECT_CALL(*host_, OnStreamGenerated(kRenderId, kPageRequestId + i, 0, 1)); |
| 335 EXPECT_CALL(*media_observer_.get(), OnCaptureDevicesOpened(_, _, _)); | 326 EXPECT_CALL(*media_observer_.get(), OnCaptureDevicesOpened(_, _, _)); |
| 336 host_->OnGenerateStream(kPageRequestId + i, options); | 327 host_->OnGenerateStream(kPageRequestId + i, options); |
| 337 | 328 |
| 338 // Wait until the stream is generated. | 329 // Wait until the stream is generated. |
| 339 WaitForResult(); | 330 WaitForResult(); |
| 340 } | 331 } |
| 341 EXPECT_EQ(host_->NumberOfStreams(), generated_streams); | 332 EXPECT_EQ(host_->NumberOfStreams(), generated_streams); |
| 342 | 333 |
| 343 // Calling OnChannelClosing() to cancel all the pending/generated streams. | 334 // Calling OnChannelClosing() to cancel all the pending/generated streams. |
| 344 EXPECT_CALL(*media_observer_.get(), OnCaptureDevicesClosed(_, _, _)) | 335 EXPECT_CALL(*media_observer_.get(), OnCaptureDevicesClosed(_, _, _)) |
| 345 .Times(3); | 336 .Times(3); |
| 346 host_->OnChannelClosing(); | 337 host_->OnChannelClosing(); |
| 347 | 338 |
| 348 // Streams should have been cleaned up. | 339 // Streams should have been cleaned up. |
| 349 EXPECT_EQ(host_->NumberOfStreams(), 0u); | 340 EXPECT_EQ(host_->NumberOfStreams(), 0u); |
| 350 } | 341 } |
| 351 | 342 |
| 352 }; // namespace media_stream | 343 }; // namespace content |
| OLD | NEW |