| 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 "content/browser/renderer_host/media/media_stream_dispatcher_host.h" | 5 #include "content/browser/renderer_host/media/media_stream_dispatcher_host.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <queue> | 9 #include <queue> |
| 10 #include <string> | 10 #include <string> |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 74 | 74 |
| 75 } // namespace | 75 } // namespace |
| 76 | 76 |
| 77 class MockMediaStreamDispatcherHost : public MediaStreamDispatcherHost, | 77 class MockMediaStreamDispatcherHost : public MediaStreamDispatcherHost, |
| 78 public TestContentBrowserClient { | 78 public TestContentBrowserClient { |
| 79 public: | 79 public: |
| 80 MockMediaStreamDispatcherHost( | 80 MockMediaStreamDispatcherHost( |
| 81 const std::string& salt, | 81 const std::string& salt, |
| 82 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, | 82 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, |
| 83 MediaStreamManager* manager) | 83 MediaStreamManager* manager) |
| 84 : MediaStreamDispatcherHost(kProcessId, salt, manager, true), | 84 : MediaStreamDispatcherHost(kProcessId, salt, manager), |
| 85 task_runner_(task_runner), | 85 task_runner_(task_runner), |
| 86 current_ipc_(NULL) {} | 86 current_ipc_(NULL) {} |
| 87 | 87 |
| 88 // A list of mock methods. | 88 // A list of mock methods. |
| 89 MOCK_METHOD4(OnStreamGenerated, | 89 MOCK_METHOD4(OnStreamGenerated, |
| 90 void(int routing_id, int request_id, int audio_array_size, | 90 void(int routing_id, int request_id, int audio_array_size, |
| 91 int video_array_size)); | 91 int video_array_size)); |
| 92 MOCK_METHOD3(OnStreamGenerationFailed, void(int routing_id, | 92 MOCK_METHOD3(OnStreamGenerationFailed, void(int routing_id, |
| 93 int request_id, | 93 int request_id, |
| 94 MediaStreamRequestResult result)); | 94 MediaStreamRequestResult result)); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 115 int page_request_id, | 115 int page_request_id, |
| 116 const std::string& device_id, | 116 const std::string& device_id, |
| 117 MediaStreamType type, | 117 MediaStreamType type, |
| 118 const url::Origin& security_origin, | 118 const url::Origin& security_origin, |
| 119 const base::Closure& quit_closure) { | 119 const base::Closure& quit_closure) { |
| 120 quit_closures_.push(quit_closure); | 120 quit_closures_.push(quit_closure); |
| 121 MediaStreamDispatcherHost::OnOpenDevice( | 121 MediaStreamDispatcherHost::OnOpenDevice( |
| 122 render_frame_id, page_request_id, device_id, type, security_origin); | 122 render_frame_id, page_request_id, device_id, type, security_origin); |
| 123 } | 123 } |
| 124 | 124 |
| 125 void OnEnumerateDevices(int render_frame_id, | |
| 126 int page_request_id, | |
| 127 MediaStreamType type, | |
| 128 const url::Origin& security_origin, | |
| 129 const base::Closure& quit_closure) { | |
| 130 quit_closures_.push(quit_closure); | |
| 131 MediaStreamDispatcherHost::OnEnumerateDevices( | |
| 132 render_frame_id, page_request_id, type, security_origin); | |
| 133 } | |
| 134 | |
| 135 void OnCancelEnumerateDevices(int render_frame_id, int page_request_id) { | |
| 136 MediaStreamDispatcherHost::OnCancelEnumerateDevices(render_frame_id, | |
| 137 page_request_id); | |
| 138 } | |
| 139 | |
| 140 void OnSubscribeToDeviceChangeNotifications( | |
| 141 int render_frame_id, | |
| 142 const url::Origin& security_origin, | |
| 143 const base::Closure& quit_closure) { | |
| 144 quit_closures_.push(quit_closure); | |
| 145 MediaStreamDispatcherHost::OnSubscribeToDeviceChangeNotifications( | |
| 146 render_frame_id, security_origin); | |
| 147 } | |
| 148 | |
| 149 std::string label_; | 125 std::string label_; |
| 150 StreamDeviceInfoArray audio_devices_; | 126 StreamDeviceInfoArray audio_devices_; |
| 151 StreamDeviceInfoArray video_devices_; | 127 StreamDeviceInfoArray video_devices_; |
| 152 StreamDeviceInfo opened_device_; | 128 StreamDeviceInfo opened_device_; |
| 153 StreamDeviceInfoArray enumerated_devices_; | |
| 154 | 129 |
| 155 private: | 130 private: |
| 156 ~MockMediaStreamDispatcherHost() override {} | 131 ~MockMediaStreamDispatcherHost() override {} |
| 157 | 132 |
| 158 // This method is used to dispatch IPC messages to the renderer. We intercept | 133 // This method is used to dispatch IPC messages to the renderer. We intercept |
| 159 // these messages here and dispatch to our mock methods to verify the | 134 // these messages here and dispatch to our mock methods to verify the |
| 160 // conversation between this object and the renderer. | 135 // conversation between this object and the renderer. |
| 161 bool Send(IPC::Message* message) override { | 136 bool Send(IPC::Message* message) override { |
| 162 CHECK(message); | 137 CHECK(message); |
| 163 current_ipc_ = message; | 138 current_ipc_ = message; |
| 164 | 139 |
| 165 // In this method we dispatch the messages to the corresponding handlers as | 140 // In this method we dispatch the messages to the corresponding handlers as |
| 166 // if we are the renderer. | 141 // if we are the renderer. |
| 167 bool handled = true; | 142 bool handled = true; |
| 168 IPC_BEGIN_MESSAGE_MAP(MockMediaStreamDispatcherHost, *message) | 143 IPC_BEGIN_MESSAGE_MAP(MockMediaStreamDispatcherHost, *message) |
| 169 IPC_MESSAGE_HANDLER(MediaStreamMsg_StreamGenerated, | 144 IPC_MESSAGE_HANDLER(MediaStreamMsg_StreamGenerated, |
| 170 OnStreamGeneratedInternal) | 145 OnStreamGeneratedInternal) |
| 171 IPC_MESSAGE_HANDLER(MediaStreamMsg_StreamGenerationFailed, | 146 IPC_MESSAGE_HANDLER(MediaStreamMsg_StreamGenerationFailed, |
| 172 OnStreamGenerationFailedInternal) | 147 OnStreamGenerationFailedInternal) |
| 173 IPC_MESSAGE_HANDLER(MediaStreamMsg_DeviceStopped, OnDeviceStoppedInternal) | 148 IPC_MESSAGE_HANDLER(MediaStreamMsg_DeviceStopped, OnDeviceStoppedInternal) |
| 174 IPC_MESSAGE_HANDLER(MediaStreamMsg_DeviceOpened, OnDeviceOpenedInternal) | 149 IPC_MESSAGE_HANDLER(MediaStreamMsg_DeviceOpened, OnDeviceOpenedInternal) |
| 175 IPC_MESSAGE_HANDLER(MediaStreamMsg_DevicesEnumerated, OnDevicesEnumerated) | |
| 176 IPC_MESSAGE_HANDLER(MediaStreamMsg_DevicesChanged, OnDevicesChanged) | |
| 177 IPC_MESSAGE_UNHANDLED(handled = false) | 150 IPC_MESSAGE_UNHANDLED(handled = false) |
| 178 IPC_END_MESSAGE_MAP() | 151 IPC_END_MESSAGE_MAP() |
| 179 EXPECT_TRUE(handled); | 152 EXPECT_TRUE(handled); |
| 180 | 153 |
| 181 delete message; | 154 delete message; |
| 182 current_ipc_ = NULL; | 155 current_ipc_ = NULL; |
| 183 return true; | 156 return true; |
| 184 } | 157 } |
| 185 | 158 |
| 186 // These handler methods do minimal things and delegate to the mock methods. | 159 // These handler methods do minimal things and delegate to the mock methods. |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 227 void OnDeviceOpenedInternal(int request_id, | 200 void OnDeviceOpenedInternal(int request_id, |
| 228 const std::string& label, | 201 const std::string& label, |
| 229 const StreamDeviceInfo& device) { | 202 const StreamDeviceInfo& device) { |
| 230 base::Closure quit_closure = quit_closures_.front(); | 203 base::Closure quit_closure = quit_closures_.front(); |
| 231 quit_closures_.pop(); | 204 quit_closures_.pop(); |
| 232 task_runner_->PostTask(FROM_HERE, base::ResetAndReturn(&quit_closure)); | 205 task_runner_->PostTask(FROM_HERE, base::ResetAndReturn(&quit_closure)); |
| 233 label_ = label; | 206 label_ = label; |
| 234 opened_device_ = device; | 207 opened_device_ = device; |
| 235 } | 208 } |
| 236 | 209 |
| 237 void OnDevicesEnumerated(int request_id, | |
| 238 const StreamDeviceInfoArray& devices) { | |
| 239 base::Closure quit_closure = quit_closures_.front(); | |
| 240 quit_closures_.pop(); | |
| 241 task_runner_->PostTask(FROM_HERE, base::ResetAndReturn(&quit_closure)); | |
| 242 enumerated_devices_ = devices; | |
| 243 } | |
| 244 | |
| 245 void OnDevicesChanged() { | |
| 246 base::Closure quit_closure = quit_closures_.front(); | |
| 247 quit_closures_.pop(); | |
| 248 task_runner_->PostTask(FROM_HERE, base::ResetAndReturn(&quit_closure)); | |
| 249 } | |
| 250 | |
| 251 const scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | 210 const scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 252 IPC::Message* current_ipc_; | 211 IPC::Message* current_ipc_; |
| 253 std::queue<base::Closure> quit_closures_; | 212 std::queue<base::Closure> quit_closures_; |
| 254 }; | 213 }; |
| 255 | 214 |
| 256 class MockMediaStreamUIProxy : public FakeMediaStreamUIProxy { | 215 class MockMediaStreamUIProxy : public FakeMediaStreamUIProxy { |
| 257 public: | 216 public: |
| 258 MOCK_METHOD2( | 217 MOCK_METHOD2( |
| 259 OnStarted, | 218 OnStarted, |
| 260 void(const base::Closure& stop, | 219 void(const base::Closure& stop, |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 379 const std::string& device_id) { | 338 const std::string& device_id) { |
| 380 base::RunLoop run_loop; | 339 base::RunLoop run_loop; |
| 381 host_->OnOpenDevice(render_frame_id, page_request_id, device_id, | 340 host_->OnOpenDevice(render_frame_id, page_request_id, device_id, |
| 382 MEDIA_DEVICE_VIDEO_CAPTURE, origin_, | 341 MEDIA_DEVICE_VIDEO_CAPTURE, origin_, |
| 383 run_loop.QuitClosure()); | 342 run_loop.QuitClosure()); |
| 384 run_loop.Run(); | 343 run_loop.Run(); |
| 385 EXPECT_FALSE(DoesContainRawIds(host_->video_devices_)); | 344 EXPECT_FALSE(DoesContainRawIds(host_->video_devices_)); |
| 386 EXPECT_TRUE(DoesEveryDeviceMapToRawId(host_->video_devices_, origin_)); | 345 EXPECT_TRUE(DoesEveryDeviceMapToRawId(host_->video_devices_, origin_)); |
| 387 } | 346 } |
| 388 | 347 |
| 389 void EnumerateDevicesAndWaitForResult(int render_frame_id, | |
| 390 int page_request_id, | |
| 391 MediaStreamType type) { | |
| 392 base::RunLoop run_loop; | |
| 393 host_->OnEnumerateDevices(render_frame_id, page_request_id, type, origin_, | |
| 394 run_loop.QuitClosure()); | |
| 395 run_loop.Run(); | |
| 396 ASSERT_FALSE(host_->enumerated_devices_.empty()); | |
| 397 EXPECT_FALSE(DoesContainRawIds(host_->enumerated_devices_)); | |
| 398 EXPECT_TRUE(DoesEveryDeviceMapToRawId(host_->enumerated_devices_, origin_)); | |
| 399 // Enumeration requests must be cancelled manually. | |
| 400 host_->OnCancelEnumerateDevices(render_frame_id, page_request_id); | |
| 401 } | |
| 402 | |
| 403 void SubscribeToDeviceChangeNotificationsAndWaitForNotification( | |
| 404 int render_frame_id) { | |
| 405 base::RunLoop run_loop; | |
| 406 host_->OnSubscribeToDeviceChangeNotifications(render_frame_id, origin_, | |
| 407 run_loop.QuitClosure()); | |
| 408 // Simulate a change in the set of devices. | |
| 409 video_capture_device_factory_->set_number_of_devices(5); | |
| 410 media_stream_manager_->media_devices_manager()->OnDevicesChanged( | |
| 411 base::SystemMonitor::DEVTYPE_VIDEO_CAPTURE); | |
| 412 run_loop.Run(); | |
| 413 } | |
| 414 | |
| 415 bool DoesContainRawIds(const StreamDeviceInfoArray& devices) { | 348 bool DoesContainRawIds(const StreamDeviceInfoArray& devices) { |
| 416 for (size_t i = 0; i < devices.size(); ++i) { | 349 for (size_t i = 0; i < devices.size(); ++i) { |
| 417 media::AudioDeviceNames::const_iterator audio_it = | 350 media::AudioDeviceNames::const_iterator audio_it = |
| 418 physical_audio_devices_.begin(); | 351 physical_audio_devices_.begin(); |
| 419 for (; audio_it != physical_audio_devices_.end(); ++audio_it) { | 352 for (; audio_it != physical_audio_devices_.end(); ++audio_it) { |
| 420 // Skip default and communications audio devices, whose IDs are not | 353 // Skip default and communications audio devices, whose IDs are not |
| 421 // translated. | 354 // translated. |
| 422 if (devices[i].device.id == | 355 if (devices[i].device.id == |
| 423 media::AudioDeviceDescription::kDefaultDeviceId || | 356 media::AudioDeviceDescription::kDefaultDeviceId || |
| 424 devices[i].device.id == | 357 devices[i].device.id == |
| (...skipping 517 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 942 | 875 |
| 943 base::RunLoop run_loop; | 876 base::RunLoop run_loop; |
| 944 EXPECT_CALL(*host_.get(), OnDeviceStopped(kRenderId)) | 877 EXPECT_CALL(*host_.get(), OnDeviceStopped(kRenderId)) |
| 945 .WillOnce(testing::InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit)); | 878 .WillOnce(testing::InvokeWithoutArgs(&run_loop, &base::RunLoop::Quit)); |
| 946 media_stream_manager_->media_devices_manager()->OnDevicesChanged( | 879 media_stream_manager_->media_devices_manager()->OnDevicesChanged( |
| 947 base::SystemMonitor::DEVTYPE_VIDEO_CAPTURE); | 880 base::SystemMonitor::DEVTYPE_VIDEO_CAPTURE); |
| 948 | 881 |
| 949 run_loop.Run(); | 882 run_loop.Run(); |
| 950 } | 883 } |
| 951 | 884 |
| 952 TEST_F(MediaStreamDispatcherHostTest, EnumerateAudioDevices) { | |
| 953 SetupFakeUI(false); | |
| 954 EnumerateDevicesAndWaitForResult(kRenderId, kPageRequestId, | |
| 955 MEDIA_DEVICE_AUDIO_CAPTURE); | |
| 956 EXPECT_TRUE(DoesContainLabels(host_->enumerated_devices_)); | |
| 957 } | |
| 958 | |
| 959 TEST_F(MediaStreamDispatcherHostTest, EnumerateVideoDevices) { | |
| 960 SetupFakeUI(false); | |
| 961 EnumerateDevicesAndWaitForResult(kRenderId, kPageRequestId, | |
| 962 MEDIA_DEVICE_VIDEO_CAPTURE); | |
| 963 EXPECT_TRUE(DoesContainLabels(host_->enumerated_devices_)); | |
| 964 } | |
| 965 | |
| 966 TEST_F(MediaStreamDispatcherHostTest, EnumerateAudioDevicesNoAccess) { | |
| 967 SetupFakeUI(false); | |
| 968 stream_ui_->SetMicAccess(false); | |
| 969 EnumerateDevicesAndWaitForResult(kRenderId, kPageRequestId, | |
| 970 MEDIA_DEVICE_AUDIO_CAPTURE); | |
| 971 EXPECT_TRUE(DoesNotContainLabels(host_->enumerated_devices_)); | |
| 972 } | |
| 973 | |
| 974 TEST_F(MediaStreamDispatcherHostTest, EnumerateVideoDevicesNoAccess) { | |
| 975 SetupFakeUI(false); | |
| 976 stream_ui_->SetCameraAccess(false); | |
| 977 EnumerateDevicesAndWaitForResult(kRenderId, kPageRequestId, | |
| 978 MEDIA_DEVICE_VIDEO_CAPTURE); | |
| 979 EXPECT_TRUE(DoesNotContainLabels(host_->enumerated_devices_)); | |
| 980 } | |
| 981 | |
| 982 TEST_F(MediaStreamDispatcherHostTest, DeviceChangeNotification) { | |
| 983 SetupFakeUI(false); | |
| 984 // warm up the cache | |
| 985 EnumerateDevicesAndWaitForResult(kRenderId, kPageRequestId, | |
| 986 MEDIA_DEVICE_VIDEO_CAPTURE); | |
| 987 SubscribeToDeviceChangeNotificationsAndWaitForNotification(kRenderId); | |
| 988 } | |
| 989 | |
| 990 }; // namespace content | 885 }; // namespace content |
| OLD | NEW |