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