| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 "content/browser/renderer_host/media/media_devices_dispatcher_host.h" | 5 #include "content/browser/renderer_host/media/media_devices_dispatcher_host.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <queue> | 10 #include <queue> |
| 11 #include <utility> | 11 #include <utility> |
| 12 #include <vector> | 12 #include <vector> |
| 13 | 13 |
| 14 #include "base/bind.h" | 14 #include "base/bind.h" |
| 15 #include "base/command_line.h" | 15 #include "base/command_line.h" |
| 16 #include "base/memory/ptr_util.h" | 16 #include "base/memory/ptr_util.h" |
| 17 #include "base/run_loop.h" | 17 #include "base/run_loop.h" |
| 18 #include "base/strings/stringprintf.h" |
| 18 #include "base/threading/thread_task_runner_handle.h" | 19 #include "base/threading/thread_task_runner_handle.h" |
| 19 #include "content/browser/renderer_host/media/media_stream_manager.h" | 20 #include "content/browser/renderer_host/media/media_stream_manager.h" |
| 20 #include "content/browser/renderer_host/media/media_stream_ui_proxy.h" | 21 #include "content/browser/renderer_host/media/media_stream_ui_proxy.h" |
| 21 #include "content/browser/renderer_host/media/video_capture_manager.h" | 22 #include "content/browser/renderer_host/media/video_capture_manager.h" |
| 22 #include "content/public/browser/media_device_id.h" | 23 #include "content/public/browser/media_device_id.h" |
| 23 #include "content/public/test/mock_resource_context.h" | 24 #include "content/public/test/mock_resource_context.h" |
| 24 #include "content/public/test/test_browser_context.h" | 25 #include "content/public/test/test_browser_context.h" |
| 25 #include "content/public/test/test_browser_thread_bundle.h" | 26 #include "content/public/test/test_browser_thread_bundle.h" |
| 26 #include "media/audio/audio_device_description.h" | 27 #include "media/audio/audio_device_description.h" |
| 27 #include "media/audio/mock_audio_manager.h" | 28 #include "media/audio/mock_audio_manager.h" |
| 28 #include "media/base/media_switches.h" | 29 #include "media/base/media_switches.h" |
| 29 #include "media/capture/video/fake_video_capture_device_factory.h" | 30 #include "media/capture/video/fake_video_capture_device_factory.h" |
| 30 #include "mojo/public/cpp/bindings/binding.h" | 31 #include "mojo/public/cpp/bindings/binding.h" |
| 31 #include "testing/gmock/include/gmock/gmock.h" | 32 #include "testing/gmock/include/gmock/gmock.h" |
| 32 #include "testing/gtest/include/gtest/gtest.h" | 33 #include "testing/gtest/include/gtest/gtest.h" |
| 33 #include "url/origin.h" | 34 #include "url/origin.h" |
| 34 | 35 |
| 35 using testing::_; | 36 using testing::_; |
| 36 using testing::SaveArg; | 37 using testing::SaveArg; |
| 37 | 38 |
| 38 namespace content { | 39 namespace content { |
| 39 | 40 |
| 40 namespace { | 41 namespace { |
| 41 | 42 |
| 42 const int kProcessId = 5; | 43 const int kProcessId = 5; |
| 43 const int kRenderId = 6; | 44 const int kRenderId = 6; |
| 45 const size_t kNumFakeVideoDevices = 3; |
| 46 const char kDefaultVideoDeviceID[] = "/dev/video2"; |
| 44 | 47 |
| 45 void PhysicalDevicesEnumerated(base::Closure quit_closure, | 48 void PhysicalDevicesEnumerated(base::Closure quit_closure, |
| 46 MediaDeviceEnumeration* out, | 49 MediaDeviceEnumeration* out, |
| 47 const MediaDeviceEnumeration& enumeration) { | 50 const MediaDeviceEnumeration& enumeration) { |
| 48 *out = enumeration; | 51 *out = enumeration; |
| 49 quit_closure.Run(); | 52 quit_closure.Run(); |
| 50 } | 53 } |
| 51 | 54 |
| 52 class MockMediaDevicesListener : public ::mojom::MediaDevicesListener { | 55 class MockMediaDevicesListener : public ::mojom::MediaDevicesListener { |
| 53 public: | 56 public: |
| (...skipping 11 matching lines...) Expand all Loading... |
| 65 }; | 68 }; |
| 66 | 69 |
| 67 } // namespace | 70 } // namespace |
| 68 | 71 |
| 69 class MediaDevicesDispatcherHostTest : public testing::Test { | 72 class MediaDevicesDispatcherHostTest : public testing::Test { |
| 70 public: | 73 public: |
| 71 MediaDevicesDispatcherHostTest() | 74 MediaDevicesDispatcherHostTest() |
| 72 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), | 75 : thread_bundle_(content::TestBrowserThreadBundle::IO_MAINLOOP), |
| 73 origin_(GURL("https://test.com")) { | 76 origin_(GURL("https://test.com")) { |
| 74 // Make sure we use fake devices to avoid long delays. | 77 // Make sure we use fake devices to avoid long delays. |
| 75 base::CommandLine::ForCurrentProcess()->AppendSwitch( | 78 base::CommandLine::ForCurrentProcess()->AppendSwitchASCII( |
| 76 switches::kUseFakeDeviceForMediaStream); | 79 switches::kUseFakeDeviceForMediaStream, |
| 80 base::StringPrintf("device-count=%zu, video-input-default-id=%s", |
| 81 kNumFakeVideoDevices, kDefaultVideoDeviceID)); |
| 77 audio_manager_.reset( | 82 audio_manager_.reset( |
| 78 new media::MockAudioManager(base::ThreadTaskRunnerHandle::Get())); | 83 new media::MockAudioManager(base::ThreadTaskRunnerHandle::Get())); |
| 79 media_stream_manager_.reset(new MediaStreamManager(audio_manager_.get())); | 84 media_stream_manager_.reset(new MediaStreamManager(audio_manager_.get())); |
| 80 | 85 |
| 81 MockResourceContext* mock_resource_context = | 86 MockResourceContext* mock_resource_context = |
| 82 static_cast<MockResourceContext*>( | 87 static_cast<MockResourceContext*>( |
| 83 browser_context_.GetResourceContext()); | 88 browser_context_.GetResourceContext()); |
| 84 | 89 |
| 85 host_ = base::MakeUnique<MediaDevicesDispatcherHost>( | 90 host_ = base::MakeUnique<MediaDevicesDispatcherHost>( |
| 86 kProcessId, kRenderId, mock_resource_context->GetMediaDeviceIDSalt(), | 91 kProcessId, kRenderId, mock_resource_context->GetMediaDeviceIDSalt(), |
| (...skipping 14 matching lines...) Expand all Loading... |
| 101 | 106 |
| 102 ASSERT_GT(physical_devices_[MEDIA_DEVICE_TYPE_AUDIO_INPUT].size(), 0u); | 107 ASSERT_GT(physical_devices_[MEDIA_DEVICE_TYPE_AUDIO_INPUT].size(), 0u); |
| 103 ASSERT_GT(physical_devices_[MEDIA_DEVICE_TYPE_VIDEO_INPUT].size(), 0u); | 108 ASSERT_GT(physical_devices_[MEDIA_DEVICE_TYPE_VIDEO_INPUT].size(), 0u); |
| 104 ASSERT_GT(physical_devices_[MEDIA_DEVICE_TYPE_AUDIO_OUTPUT].size(), 0u); | 109 ASSERT_GT(physical_devices_[MEDIA_DEVICE_TYPE_AUDIO_OUTPUT].size(), 0u); |
| 105 } | 110 } |
| 106 | 111 |
| 107 MOCK_METHOD1(UniqueOriginCallback, | 112 MOCK_METHOD1(UniqueOriginCallback, |
| 108 void(const std::vector<std::vector<MediaDeviceInfo>>&)); | 113 void(const std::vector<std::vector<MediaDeviceInfo>>&)); |
| 109 MOCK_METHOD1(ValidOriginCallback, | 114 MOCK_METHOD1(ValidOriginCallback, |
| 110 void(const std::vector<std::vector<MediaDeviceInfo>>&)); | 115 void(const std::vector<std::vector<MediaDeviceInfo>>&)); |
| 116 MOCK_METHOD0(MockVideoInputCapabilitiesCallback, void()); |
| 117 |
| 118 void VideoInputCapabilitiesCallback( |
| 119 std::vector<::mojom::VideoInputDeviceCapabilitiesPtr> capabilities) { |
| 120 MockVideoInputCapabilitiesCallback(); |
| 121 std::string expected_first_device_id = GetHMACForMediaDeviceID( |
| 122 browser_context_.GetResourceContext()->GetMediaDeviceIDSalt(), origin_, |
| 123 kDefaultVideoDeviceID); |
| 124 EXPECT_EQ(kNumFakeVideoDevices, capabilities.size()); |
| 125 EXPECT_EQ(expected_first_device_id, capabilities[0]->device_id); |
| 126 for (const auto& capability : capabilities) { |
| 127 EXPECT_GT(capability->formats.size(), 1u); |
| 128 EXPECT_GT(capability->formats[0].frame_size.width(), 1); |
| 129 EXPECT_GT(capability->formats[0].frame_size.height(), 1); |
| 130 EXPECT_GT(capability->formats[0].frame_rate, 1); |
| 131 EXPECT_GT(capability->formats[1].frame_size.width(), 1); |
| 132 EXPECT_GT(capability->formats[1].frame_size.height(), 1); |
| 133 EXPECT_GT(capability->formats[1].frame_rate, 1); |
| 134 } |
| 135 } |
| 111 | 136 |
| 112 protected: | 137 protected: |
| 113 void DevicesEnumerated( | 138 void DevicesEnumerated( |
| 114 const base::Closure& closure, | 139 const base::Closure& closure, |
| 115 const std::vector<std::vector<MediaDeviceInfo>>& devices) { | 140 const std::vector<std::vector<MediaDeviceInfo>>& devices) { |
| 116 enumerated_devices_ = devices; | 141 enumerated_devices_ = devices; |
| 117 closure.Run(); | 142 closure.Run(); |
| 118 } | 143 } |
| 119 | 144 |
| 120 void EnumerateDevicesAndWaitForResult(bool enumerate_audio_input, | 145 void EnumerateDevicesAndWaitForResult(bool enumerate_audio_input, |
| (...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 333 base::RunLoop().RunUntilIdle(); | 358 base::RunLoop().RunUntilIdle(); |
| 334 #if defined(OS_WIN) | 359 #if defined(OS_WIN) |
| 335 // On Windows, the underlying MediaStreamManager uses a separate thread for | 360 // On Windows, the underlying MediaStreamManager uses a separate thread for |
| 336 // video capture which must be flushed to guarantee that the callback bound to | 361 // video capture which must be flushed to guarantee that the callback bound to |
| 337 // EnumerateDevices above is invoked before the end of this test's body. | 362 // EnumerateDevices above is invoked before the end of this test's body. |
| 338 media_stream_manager_->FlushVideoCaptureThreadForTesting(); | 363 media_stream_manager_->FlushVideoCaptureThreadForTesting(); |
| 339 base::RunLoop().RunUntilIdle(); | 364 base::RunLoop().RunUntilIdle(); |
| 340 #endif | 365 #endif |
| 341 } | 366 } |
| 342 | 367 |
| 368 TEST_F(MediaDevicesDispatcherHostTest, GetVideoInputCapabilities) { |
| 369 EXPECT_CALL(*this, MockVideoInputCapabilitiesCallback()); |
| 370 host_->GetVideoInputCapabilities( |
| 371 origin_, |
| 372 base::Bind( |
| 373 &MediaDevicesDispatcherHostTest::VideoInputCapabilitiesCallback, |
| 374 base::Unretained(this))); |
| 375 base::RunLoop().RunUntilIdle(); |
| 376 |
| 377 #if defined(OS_WIN) |
| 378 // On Windows, the underlying MediaStreamManager uses a separate thread for |
| 379 // video capture which must be flushed to guarantee that the callback bound to |
| 380 // GetVIdeoInputCapabilities above is invoked before the end of this test's |
| 381 // body. |
| 382 media_stream_manager_->FlushVideoCaptureThreadForTesting(); |
| 383 base::RunLoop().RunUntilIdle(); |
| 384 #endif |
| 385 } |
| 386 |
| 343 }; // namespace content | 387 }; // namespace content |
| OLD | NEW |