| 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 <queue> | 8 #include <queue> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <utility> | 10 #include <utility> |
| 11 | 11 |
| 12 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/callback_helpers.h" | 13 #include "base/callback_helpers.h" |
| 14 #include "base/command_line.h" | 14 #include "base/command_line.h" |
| 15 #include "base/location.h" | 15 #include "base/location.h" |
| 16 #include "base/run_loop.h" | 16 #include "base/run_loop.h" |
| 17 #include "base/single_thread_task_runner.h" | 17 #include "base/single_thread_task_runner.h" |
| 18 #include "base/system_monitor/system_monitor.h" | |
| 19 #include "base/thread_task_runner_handle.h" | 18 #include "base/thread_task_runner_handle.h" |
| 20 #include "build/build_config.h" | 19 #include "build/build_config.h" |
| 21 #include "content/browser/browser_thread_impl.h" | 20 #include "content/browser/browser_thread_impl.h" |
| 22 #include "content/browser/renderer_host/media/audio_input_device_manager.h" | 21 #include "content/browser/renderer_host/media/audio_input_device_manager.h" |
| 23 #include "content/browser/renderer_host/media/media_stream_manager.h" | 22 #include "content/browser/renderer_host/media/media_stream_manager.h" |
| 24 #include "content/browser/renderer_host/media/media_stream_ui_proxy.h" | 23 #include "content/browser/renderer_host/media/media_stream_ui_proxy.h" |
| 25 #include "content/browser/renderer_host/media/video_capture_manager.h" | 24 #include "content/browser/renderer_host/media/video_capture_manager.h" |
| 26 #include "content/common/media/media_stream_messages.h" | 25 #include "content/common/media/media_stream_messages.h" |
| 27 #include "content/common/media/media_stream_options.h" | 26 #include "content/common/media/media_stream_options.h" |
| 28 #include "content/public/browser/media_device_id.h" | 27 #include "content/public/browser/media_device_id.h" |
| (...skipping 30 matching lines...) Expand all Loading... |
| 59 | 58 |
| 60 namespace content { | 59 namespace content { |
| 61 | 60 |
| 62 class MockMediaStreamDispatcherHost : public MediaStreamDispatcherHost, | 61 class MockMediaStreamDispatcherHost : public MediaStreamDispatcherHost, |
| 63 public TestContentBrowserClient { | 62 public TestContentBrowserClient { |
| 64 public: | 63 public: |
| 65 MockMediaStreamDispatcherHost( | 64 MockMediaStreamDispatcherHost( |
| 66 const ResourceContext::SaltCallback salt_callback, | 65 const ResourceContext::SaltCallback salt_callback, |
| 67 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, | 66 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, |
| 68 MediaStreamManager* manager) | 67 MediaStreamManager* manager) |
| 69 : MediaStreamDispatcherHost(kProcessId, salt_callback, manager, true), | 68 : MediaStreamDispatcherHost(kProcessId, salt_callback, manager), |
| 70 task_runner_(task_runner), | 69 task_runner_(task_runner), |
| 71 current_ipc_(NULL) {} | 70 current_ipc_(NULL) {} |
| 72 | 71 |
| 73 // A list of mock methods. | 72 // A list of mock methods. |
| 74 MOCK_METHOD4(OnStreamGenerated, | 73 MOCK_METHOD4(OnStreamGenerated, |
| 75 void(int routing_id, int request_id, int audio_array_size, | 74 void(int routing_id, int request_id, int audio_array_size, |
| 76 int video_array_size)); | 75 int video_array_size)); |
| 77 MOCK_METHOD3(OnStreamGenerationFailed, void(int routing_id, | 76 MOCK_METHOD3(OnStreamGenerationFailed, void(int routing_id, |
| 78 int request_id, | 77 int request_id, |
| 79 MediaStreamRequestResult result)); | 78 MediaStreamRequestResult result)); |
| (...skipping 30 matching lines...) Expand all Loading... |
| 110 void OnEnumerateDevices(int render_frame_id, | 109 void OnEnumerateDevices(int render_frame_id, |
| 111 int page_request_id, | 110 int page_request_id, |
| 112 MediaStreamType type, | 111 MediaStreamType type, |
| 113 const url::Origin& security_origin, | 112 const url::Origin& security_origin, |
| 114 const base::Closure& quit_closure) { | 113 const base::Closure& quit_closure) { |
| 115 quit_closures_.push(quit_closure); | 114 quit_closures_.push(quit_closure); |
| 116 MediaStreamDispatcherHost::OnEnumerateDevices( | 115 MediaStreamDispatcherHost::OnEnumerateDevices( |
| 117 render_frame_id, page_request_id, type, security_origin); | 116 render_frame_id, page_request_id, type, security_origin); |
| 118 } | 117 } |
| 119 | 118 |
| 120 void OnCancelEnumerateDevices(int render_frame_id, int page_request_id) { | |
| 121 MediaStreamDispatcherHost::OnCancelEnumerateDevices(render_frame_id, | |
| 122 page_request_id); | |
| 123 } | |
| 124 | |
| 125 void OnSubscribeToDeviceChangeNotifications( | |
| 126 int render_frame_id, | |
| 127 const url::Origin& security_origin, | |
| 128 const base::Closure& quit_closure) { | |
| 129 quit_closures_.push(quit_closure); | |
| 130 MediaStreamDispatcherHost::OnSubscribeToDeviceChangeNotifications( | |
| 131 render_frame_id, security_origin); | |
| 132 } | |
| 133 | |
| 134 std::string label_; | 119 std::string label_; |
| 135 StreamDeviceInfoArray audio_devices_; | 120 StreamDeviceInfoArray audio_devices_; |
| 136 StreamDeviceInfoArray video_devices_; | 121 StreamDeviceInfoArray video_devices_; |
| 137 StreamDeviceInfo opened_device_; | 122 StreamDeviceInfo opened_device_; |
| 138 StreamDeviceInfoArray enumerated_devices_; | 123 StreamDeviceInfoArray enumerated_devices_; |
| 139 | 124 |
| 140 private: | 125 private: |
| 141 ~MockMediaStreamDispatcherHost() override {} | 126 ~MockMediaStreamDispatcherHost() override {} |
| 142 | 127 |
| 143 // This method is used to dispatch IPC messages to the renderer. We intercept | 128 // This method is used to dispatch IPC messages to the renderer. We intercept |
| 144 // these messages here and dispatch to our mock methods to verify the | 129 // these messages here and dispatch to our mock methods to verify the |
| 145 // conversation between this object and the renderer. | 130 // conversation between this object and the renderer. |
| 146 bool Send(IPC::Message* message) override { | 131 bool Send(IPC::Message* message) override { |
| 147 CHECK(message); | 132 CHECK(message); |
| 148 current_ipc_ = message; | 133 current_ipc_ = message; |
| 149 | 134 |
| 150 // In this method we dispatch the messages to the corresponding handlers as | 135 // In this method we dispatch the messages to the according handlers as if |
| 151 // if we are the renderer. | 136 // we are the renderer. |
| 152 bool handled = true; | 137 bool handled = true; |
| 153 IPC_BEGIN_MESSAGE_MAP(MockMediaStreamDispatcherHost, *message) | 138 IPC_BEGIN_MESSAGE_MAP(MockMediaStreamDispatcherHost, *message) |
| 154 IPC_MESSAGE_HANDLER(MediaStreamMsg_StreamGenerated, | 139 IPC_MESSAGE_HANDLER(MediaStreamMsg_StreamGenerated, |
| 155 OnStreamGeneratedInternal) | 140 OnStreamGeneratedInternal) |
| 156 IPC_MESSAGE_HANDLER(MediaStreamMsg_StreamGenerationFailed, | 141 IPC_MESSAGE_HANDLER(MediaStreamMsg_StreamGenerationFailed, |
| 157 OnStreamGenerationFailedInternal) | 142 OnStreamGenerationFailedInternal) |
| 158 IPC_MESSAGE_HANDLER(MediaStreamMsg_DeviceStopped, OnDeviceStoppedInternal) | 143 IPC_MESSAGE_HANDLER(MediaStreamMsg_DeviceStopped, OnDeviceStoppedInternal) |
| 159 IPC_MESSAGE_HANDLER(MediaStreamMsg_DeviceOpened, OnDeviceOpenedInternal) | 144 IPC_MESSAGE_HANDLER(MediaStreamMsg_DeviceOpened, OnDeviceOpenedInternal) |
| 160 IPC_MESSAGE_HANDLER(MediaStreamMsg_DevicesEnumerated, OnDevicesEnumerated) | 145 IPC_MESSAGE_HANDLER(MediaStreamMsg_DevicesEnumerated, OnDevicesEnumerated) |
| 161 IPC_MESSAGE_HANDLER(MediaStreamMsg_DevicesChanged, OnDevicesChanged) | |
| 162 IPC_MESSAGE_UNHANDLED(handled = false) | 146 IPC_MESSAGE_UNHANDLED(handled = false) |
| 163 IPC_END_MESSAGE_MAP() | 147 IPC_END_MESSAGE_MAP() |
| 164 EXPECT_TRUE(handled); | 148 EXPECT_TRUE(handled); |
| 165 | 149 |
| 166 delete message; | 150 delete message; |
| 167 current_ipc_ = NULL; | 151 current_ipc_ = NULL; |
| 168 return true; | 152 return true; |
| 169 } | 153 } |
| 170 | 154 |
| 171 // These handler methods do minimal things and delegate to the mock methods. | 155 // These handler methods do minimal things and delegate to the mock methods. |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 220 } | 204 } |
| 221 | 205 |
| 222 void OnDevicesEnumerated(int request_id, | 206 void OnDevicesEnumerated(int request_id, |
| 223 const StreamDeviceInfoArray& devices) { | 207 const StreamDeviceInfoArray& devices) { |
| 224 base::Closure quit_closure = quit_closures_.front(); | 208 base::Closure quit_closure = quit_closures_.front(); |
| 225 quit_closures_.pop(); | 209 quit_closures_.pop(); |
| 226 task_runner_->PostTask(FROM_HERE, base::ResetAndReturn(&quit_closure)); | 210 task_runner_->PostTask(FROM_HERE, base::ResetAndReturn(&quit_closure)); |
| 227 enumerated_devices_ = devices; | 211 enumerated_devices_ = devices; |
| 228 } | 212 } |
| 229 | 213 |
| 230 void OnDevicesChanged() { | |
| 231 base::Closure quit_closure = quit_closures_.front(); | |
| 232 quit_closures_.pop(); | |
| 233 task_runner_->PostTask(FROM_HERE, base::ResetAndReturn(&quit_closure)); | |
| 234 } | |
| 235 | |
| 236 const scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | 214 const scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 237 IPC::Message* current_ipc_; | 215 IPC::Message* current_ipc_; |
| 238 std::queue<base::Closure> quit_closures_; | 216 std::queue<base::Closure> quit_closures_; |
| 239 }; | 217 }; |
| 240 | 218 |
| 241 class MockMediaStreamUIProxy : public FakeMediaStreamUIProxy { | 219 class MockMediaStreamUIProxy : public FakeMediaStreamUIProxy { |
| 242 public: | 220 public: |
| 243 MOCK_METHOD2( | 221 MOCK_METHOD2( |
| 244 OnStarted, | 222 OnStarted, |
| 245 void(const base::Closure& stop, | 223 void(const base::Closure& stop, |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 366 void EnumerateDevicesAndWaitForResult(int render_frame_id, | 344 void EnumerateDevicesAndWaitForResult(int render_frame_id, |
| 367 int page_request_id, | 345 int page_request_id, |
| 368 MediaStreamType type) { | 346 MediaStreamType type) { |
| 369 base::RunLoop run_loop; | 347 base::RunLoop run_loop; |
| 370 host_->OnEnumerateDevices(render_frame_id, page_request_id, type, origin_, | 348 host_->OnEnumerateDevices(render_frame_id, page_request_id, type, origin_, |
| 371 run_loop.QuitClosure()); | 349 run_loop.QuitClosure()); |
| 372 run_loop.Run(); | 350 run_loop.Run(); |
| 373 ASSERT_FALSE(host_->enumerated_devices_.empty()); | 351 ASSERT_FALSE(host_->enumerated_devices_.empty()); |
| 374 EXPECT_FALSE(DoesContainRawIds(host_->enumerated_devices_)); | 352 EXPECT_FALSE(DoesContainRawIds(host_->enumerated_devices_)); |
| 375 EXPECT_TRUE(DoesEveryDeviceMapToRawId(host_->enumerated_devices_, origin_)); | 353 EXPECT_TRUE(DoesEveryDeviceMapToRawId(host_->enumerated_devices_, origin_)); |
| 376 // Enumeration requests must be cancelled manually. | |
| 377 host_->OnCancelEnumerateDevices(render_frame_id, page_request_id); | |
| 378 } | |
| 379 | |
| 380 void SubscribeToDeviceChangeNotificationsAndWaitForNotification( | |
| 381 int render_frame_id) { | |
| 382 base::RunLoop run_loop; | |
| 383 host_->OnSubscribeToDeviceChangeNotifications(render_frame_id, origin_, | |
| 384 run_loop.QuitClosure()); | |
| 385 // Simulate a change in the set of devices. | |
| 386 video_capture_device_factory_->set_number_of_devices(5); | |
| 387 media_stream_manager_->OnDevicesChanged( | |
| 388 base::SystemMonitor::DEVTYPE_VIDEO_CAPTURE); | |
| 389 run_loop.Run(); | |
| 390 } | 354 } |
| 391 | 355 |
| 392 bool DoesContainRawIds(const StreamDeviceInfoArray& devices) { | 356 bool DoesContainRawIds(const StreamDeviceInfoArray& devices) { |
| 393 for (size_t i = 0; i < devices.size(); ++i) { | 357 for (size_t i = 0; i < devices.size(); ++i) { |
| 394 media::AudioDeviceNames::const_iterator audio_it = | 358 media::AudioDeviceNames::const_iterator audio_it = |
| 395 physical_audio_devices_.begin(); | 359 physical_audio_devices_.begin(); |
| 396 for (; audio_it != physical_audio_devices_.end(); ++audio_it) { | 360 for (; audio_it != physical_audio_devices_.end(); ++audio_it) { |
| 397 if (audio_it->unique_id == devices[i].device.id) | 361 if (audio_it->unique_id == devices[i].device.id) |
| 398 return true; | 362 return true; |
| 399 } | 363 } |
| (...skipping 545 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 945 } | 909 } |
| 946 | 910 |
| 947 TEST_F(MediaStreamDispatcherHostTest, EnumerateVideoDevicesNoAccess) { | 911 TEST_F(MediaStreamDispatcherHostTest, EnumerateVideoDevicesNoAccess) { |
| 948 SetupFakeUI(false); | 912 SetupFakeUI(false); |
| 949 stream_ui_->SetCameraAccess(false); | 913 stream_ui_->SetCameraAccess(false); |
| 950 EnumerateDevicesAndWaitForResult(kRenderId, kPageRequestId, | 914 EnumerateDevicesAndWaitForResult(kRenderId, kPageRequestId, |
| 951 MEDIA_DEVICE_VIDEO_CAPTURE); | 915 MEDIA_DEVICE_VIDEO_CAPTURE); |
| 952 EXPECT_TRUE(DoesNotContainLabels(host_->enumerated_devices_)); | 916 EXPECT_TRUE(DoesNotContainLabels(host_->enumerated_devices_)); |
| 953 } | 917 } |
| 954 | 918 |
| 955 TEST_F(MediaStreamDispatcherHostTest, DeviceChangeNotification) { | |
| 956 SetupFakeUI(false); | |
| 957 // warm up the cache | |
| 958 EnumerateDevicesAndWaitForResult(kRenderId, kPageRequestId, | |
| 959 MEDIA_DEVICE_VIDEO_CAPTURE); | |
| 960 SubscribeToDeviceChangeNotificationsAndWaitForNotification(kRenderId); | |
| 961 } | |
| 962 | |
| 963 }; // namespace content | 919 }; // namespace content |
| OLD | NEW |