| 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_manager.h" | 5 #include "content/browser/renderer_host/media/media_devices_manager.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <algorithm> | 10 #include <algorithm> |
| 11 #include <string> | 11 #include <string> |
| 12 | 12 |
| 13 #include "base/command_line.h" | 13 #include "base/command_line.h" |
| 14 #include "base/location.h" | 14 #include "base/location.h" |
| 15 #include "base/strings/stringprintf.h" | 15 #include "base/strings/stringprintf.h" |
| 16 #include "base/task_runner_util.h" | 16 #include "base/task_runner_util.h" |
| 17 #include "base/threading/thread_checker.h" | 17 #include "base/threading/thread_checker.h" |
| 18 #include "base/threading/thread_task_runner_handle.h" | 18 #include "base/threading/thread_task_runner_handle.h" |
| 19 #include "build/build_config.h" | 19 #include "build/build_config.h" |
| 20 #include "content/browser/renderer_host/media/media_stream_manager.h" | 20 #include "content/browser/renderer_host/media/media_stream_manager.h" |
| 21 #include "content/browser/renderer_host/media/video_capture_manager.h" | 21 #include "content/browser/renderer_host/media/video_capture_manager.h" |
| 22 #include "content/public/browser/browser_thread.h" | 22 #include "content/public/browser/browser_thread.h" |
| 23 #include "media/audio/audio_device_description.h" | 23 #include "media/audio/audio_device_description.h" |
| 24 #include "media/audio/audio_manager.h" | 24 #include "media/audio/audio_system.h" |
| 25 #include "media/base/media_switches.h" | 25 #include "media/base/media_switches.h" |
| 26 | 26 |
| 27 #if defined(OS_MACOSX) | 27 #if defined(OS_MACOSX) |
| 28 #include "base/bind_helpers.h" | 28 #include "base/bind_helpers.h" |
| 29 #include "base/profiler/scoped_tracker.h" | 29 #include "base/profiler/scoped_tracker.h" |
| 30 #include "base/single_thread_task_runner.h" | 30 #include "base/single_thread_task_runner.h" |
| 31 #include "content/browser/browser_main_loop.h" | 31 #include "content/browser/browser_main_loop.h" |
| 32 #include "media/device_monitors/device_monitor_mac.h" | 32 #include "media/device_monitors/device_monitor_mac.h" |
| 33 #endif | 33 #endif |
| 34 | 34 |
| 35 namespace content { | 35 namespace content { |
| 36 | 36 |
| 37 namespace { | 37 namespace { |
| 38 | 38 |
| 39 // Private helper method to generate a string for the log message that lists the | 39 // Private helper method to generate a string for the log message that lists the |
| 40 // human readable names of |devices|. | 40 // human readable names of |devices|. |
| 41 std::string GetLogMessageString(MediaDeviceType device_type, | 41 std::string GetLogMessageString(MediaDeviceType device_type, |
| 42 const MediaDeviceInfoArray& device_infos) { | 42 const MediaDeviceInfoArray& device_infos) { |
| 43 std::string output_string = | 43 std::string output_string = |
| 44 base::StringPrintf("Getting devices of type %d:\n", device_type); | 44 base::StringPrintf("Getting devices of type %d:\n", device_type); |
| 45 if (device_infos.empty()) | 45 if (device_infos.empty()) |
| 46 return output_string + "No devices found."; | 46 return output_string + "No devices found."; |
| 47 for (const auto& device_info : device_infos) | 47 for (const auto& device_info : device_infos) |
| 48 output_string += " " + device_info.label + "\n"; | 48 output_string += " " + device_info.label + "\n"; |
| 49 return output_string; | 49 return output_string; |
| 50 } | 50 } |
| 51 | 51 |
| 52 MediaDeviceInfoArray EnumerateAudioDevicesOnDeviceThread( | |
| 53 media::AudioManager* audio_manager, | |
| 54 bool is_input) { | |
| 55 DCHECK(audio_manager->GetTaskRunner()->BelongsToCurrentThread()); | |
| 56 | |
| 57 MediaDeviceInfoArray snapshot; | |
| 58 media::AudioDeviceDescriptions device_descriptions; | |
| 59 if (is_input) | |
| 60 audio_manager->GetAudioInputDeviceDescriptions(&device_descriptions); | |
| 61 else | |
| 62 audio_manager->GetAudioOutputDeviceDescriptions(&device_descriptions); | |
| 63 | |
| 64 for (const media::AudioDeviceDescription& description : device_descriptions) | |
| 65 snapshot.emplace_back(description); | |
| 66 | |
| 67 return snapshot; | |
| 68 } | |
| 69 | |
| 70 MediaDeviceInfoArray GetFakeAudioDevices(bool is_input) { | 52 MediaDeviceInfoArray GetFakeAudioDevices(bool is_input) { |
| 71 MediaDeviceInfoArray result; | 53 MediaDeviceInfoArray result; |
| 72 if (is_input) { | 54 if (is_input) { |
| 73 result.emplace_back(media::AudioDeviceDescription::kDefaultDeviceId, | 55 result.emplace_back(media::AudioDeviceDescription::kDefaultDeviceId, |
| 74 "Fake Default Audio Input", | 56 "Fake Default Audio Input", |
| 75 "fake_group_audio_input_default"); | 57 "fake_group_audio_input_default"); |
| 76 result.emplace_back("fake_audio_input_1", "Fake Audio Input 1", | 58 result.emplace_back("fake_audio_input_1", "Fake Audio Input 1", |
| 77 "fake_group_audio_input_1"); | 59 "fake_group_audio_input_1"); |
| 78 result.emplace_back("fake_audio_input_2", "Fake Audio Input 2", | 60 result.emplace_back("fake_audio_input_2", "Fake Audio Input 2", |
| 79 "fake_group_audio_input_2"); | 61 "fake_group_audio_input_2"); |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 157 } | 139 } |
| 158 | 140 |
| 159 int64_t current_event_sequence_; | 141 int64_t current_event_sequence_; |
| 160 int64_t seq_last_update_; | 142 int64_t seq_last_update_; |
| 161 int64_t seq_last_invalidation_; | 143 int64_t seq_last_invalidation_; |
| 162 bool is_update_ongoing_; | 144 bool is_update_ongoing_; |
| 163 base::ThreadChecker thread_checker_; | 145 base::ThreadChecker thread_checker_; |
| 164 }; | 146 }; |
| 165 | 147 |
| 166 MediaDevicesManager::MediaDevicesManager( | 148 MediaDevicesManager::MediaDevicesManager( |
| 167 media::AudioManager* audio_manager, | 149 media::AudioSystem* audio_system, |
| 168 const scoped_refptr<VideoCaptureManager>& video_capture_manager, | 150 const scoped_refptr<VideoCaptureManager>& video_capture_manager, |
| 169 MediaStreamManager* media_stream_manager) | 151 MediaStreamManager* media_stream_manager) |
| 170 : use_fake_devices_(base::CommandLine::ForCurrentProcess()->HasSwitch( | 152 : use_fake_devices_(base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 171 switches::kUseFakeDeviceForMediaStream)), | 153 switches::kUseFakeDeviceForMediaStream)), |
| 172 audio_manager_(audio_manager), | 154 audio_system_(audio_system), |
| 173 video_capture_manager_(video_capture_manager), | 155 video_capture_manager_(video_capture_manager), |
| 174 media_stream_manager_(media_stream_manager), | 156 media_stream_manager_(media_stream_manager), |
| 175 cache_infos_(NUM_MEDIA_DEVICE_TYPES), | 157 cache_infos_(NUM_MEDIA_DEVICE_TYPES), |
| 176 monitoring_started_(false), | 158 monitoring_started_(false), |
| 177 weak_factory_(this) { | 159 weak_factory_(this) { |
| 178 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 160 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 179 DCHECK(audio_manager_); | 161 DCHECK(audio_system_); |
| 180 DCHECK(video_capture_manager_.get()); | 162 DCHECK(video_capture_manager_.get()); |
| 181 cache_policies_.fill(CachePolicy::NO_CACHE); | 163 cache_policies_.fill(CachePolicy::NO_CACHE); |
| 182 has_seen_result_.fill(false); | 164 has_seen_result_.fill(false); |
| 183 } | 165 } |
| 184 | 166 |
| 185 MediaDevicesManager::~MediaDevicesManager() { | 167 MediaDevicesManager::~MediaDevicesManager() { |
| 186 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 168 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 187 } | 169 } |
| 188 | 170 |
| 189 void MediaDevicesManager::EnumerateDevices( | 171 void MediaDevicesManager::EnumerateDevices( |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 282 BrowserMainLoop* browser_main_loop = content::BrowserMainLoop::GetInstance(); | 264 BrowserMainLoop* browser_main_loop = content::BrowserMainLoop::GetInstance(); |
| 283 if (!browser_main_loop) | 265 if (!browser_main_loop) |
| 284 return; | 266 return; |
| 285 | 267 |
| 286 // TODO(erikchen): Remove ScopedTracker below once crbug.com/458404 is | 268 // TODO(erikchen): Remove ScopedTracker below once crbug.com/458404 is |
| 287 // fixed. | 269 // fixed. |
| 288 tracked_objects::ScopedTracker tracking_profile2( | 270 tracked_objects::ScopedTracker tracking_profile2( |
| 289 FROM_HERE_WITH_EXPLICIT_FUNCTION( | 271 FROM_HERE_WITH_EXPLICIT_FUNCTION( |
| 290 "458404 MediaDevicesManager::GetTaskRunner")); | 272 "458404 MediaDevicesManager::GetTaskRunner")); |
| 291 const scoped_refptr<base::SingleThreadTaskRunner> task_runner = | 273 const scoped_refptr<base::SingleThreadTaskRunner> task_runner = |
| 292 audio_manager_->GetTaskRunner(); | 274 audio_system_->GetTaskRunner(); |
| 293 // TODO(erikchen): Remove ScopedTracker below once crbug.com/458404 is | 275 // TODO(erikchen): Remove ScopedTracker below once crbug.com/458404 is |
| 294 // fixed. | 276 // fixed. |
| 295 tracked_objects::ScopedTracker tracking_profile3( | 277 tracked_objects::ScopedTracker tracking_profile3( |
| 296 FROM_HERE_WITH_EXPLICIT_FUNCTION( | 278 FROM_HERE_WITH_EXPLICIT_FUNCTION( |
| 297 "458404 MediaDevicesManager::DeviceMonitorMac::StartMonitoring")); | 279 "458404 MediaDevicesManager::DeviceMonitorMac::StartMonitoring")); |
| 298 browser_main_loop->device_monitor_mac()->StartMonitoring(task_runner); | 280 browser_main_loop->device_monitor_mac()->StartMonitoring(task_runner); |
| 299 } | 281 } |
| 300 #endif | 282 #endif |
| 301 | 283 |
| 302 void MediaDevicesManager::StopMonitoring() { | 284 void MediaDevicesManager::StopMonitoring() { |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 365 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 347 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 366 MediaDeviceType type = | 348 MediaDeviceType type = |
| 367 is_input ? MEDIA_DEVICE_TYPE_AUDIO_INPUT : MEDIA_DEVICE_TYPE_AUDIO_OUTPUT; | 349 is_input ? MEDIA_DEVICE_TYPE_AUDIO_INPUT : MEDIA_DEVICE_TYPE_AUDIO_OUTPUT; |
| 368 if (use_fake_devices_) { | 350 if (use_fake_devices_) { |
| 369 base::ThreadTaskRunnerHandle::Get()->PostTask( | 351 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 370 FROM_HERE, base::Bind(&MediaDevicesManager::DevicesEnumerated, | 352 FROM_HERE, base::Bind(&MediaDevicesManager::DevicesEnumerated, |
| 371 weak_factory_.GetWeakPtr(), type, | 353 weak_factory_.GetWeakPtr(), type, |
| 372 GetFakeAudioDevices(is_input))); | 354 GetFakeAudioDevices(is_input))); |
| 373 return; | 355 return; |
| 374 } | 356 } |
| 375 base::PostTaskAndReplyWithResult( | 357 |
| 376 audio_manager_->GetTaskRunner(), FROM_HERE, | 358 audio_system_->GetDeviceDescriptions( |
| 377 base::Bind(&EnumerateAudioDevicesOnDeviceThread, audio_manager_, | 359 base::Bind(&MediaDevicesManager::AudioDevicesEnumerated, |
| 378 is_input), | 360 weak_factory_.GetWeakPtr(), type), |
| 379 base::Bind(&MediaDevicesManager::DevicesEnumerated, | 361 is_input); |
| 380 weak_factory_.GetWeakPtr(), type)); | |
| 381 } | 362 } |
| 382 | 363 |
| 383 void MediaDevicesManager::VideoInputDevicesEnumerated( | 364 void MediaDevicesManager::VideoInputDevicesEnumerated( |
| 384 const media::VideoCaptureDeviceDescriptors& descriptors) { | 365 const media::VideoCaptureDeviceDescriptors& descriptors) { |
| 385 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 366 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 386 MediaDeviceInfoArray snapshot; | 367 MediaDeviceInfoArray snapshot; |
| 387 for (const auto& descriptor : descriptors) { | 368 for (const auto& descriptor : descriptors) { |
| 388 snapshot.emplace_back(descriptor); | 369 snapshot.emplace_back(descriptor); |
| 389 } | 370 } |
| 390 DevicesEnumerated(MEDIA_DEVICE_TYPE_VIDEO_INPUT, snapshot); | 371 DevicesEnumerated(MEDIA_DEVICE_TYPE_VIDEO_INPUT, snapshot); |
| 391 } | 372 } |
| 392 | 373 |
| 374 void MediaDevicesManager::AudioDevicesEnumerated( |
| 375 MediaDeviceType type, |
| 376 media::AudioDeviceDescriptions device_descriptions) { |
| 377 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 378 |
| 379 MediaDeviceInfoArray snapshot; |
| 380 for (const media::AudioDeviceDescription& description : device_descriptions) { |
| 381 snapshot.emplace_back(description); |
| 382 } |
| 383 DevicesEnumerated(type, snapshot); |
| 384 } |
| 385 |
| 393 void MediaDevicesManager::DevicesEnumerated( | 386 void MediaDevicesManager::DevicesEnumerated( |
| 394 MediaDeviceType type, | 387 MediaDeviceType type, |
| 395 const MediaDeviceInfoArray& snapshot) { | 388 const MediaDeviceInfoArray& snapshot) { |
| 396 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 389 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 397 DCHECK(IsValidMediaDeviceType(type)); | 390 DCHECK(IsValidMediaDeviceType(type)); |
| 398 UpdateSnapshot(type, snapshot); | 391 UpdateSnapshot(type, snapshot); |
| 399 cache_infos_[type].UpdateCompleted(); | 392 cache_infos_[type].UpdateCompleted(); |
| 400 has_seen_result_[type] = true; | 393 has_seen_result_[type] = true; |
| 401 | 394 |
| 402 std::string log_message = | 395 std::string log_message = |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 519 const MediaDeviceInfoArray& snapshot) { | 512 const MediaDeviceInfoArray& snapshot) { |
| 520 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 513 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 521 DCHECK(IsValidMediaDeviceType(type)); | 514 DCHECK(IsValidMediaDeviceType(type)); |
| 522 | 515 |
| 523 for (auto* subscriber : device_change_subscribers_[type]) { | 516 for (auto* subscriber : device_change_subscribers_[type]) { |
| 524 subscriber->OnDevicesChanged(type, snapshot); | 517 subscriber->OnDevicesChanged(type, snapshot); |
| 525 } | 518 } |
| 526 } | 519 } |
| 527 | 520 |
| 528 } // namespace content | 521 } // namespace content |
| OLD | NEW |