| 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/audio_input_device_manager.h" | 5 #include "content/browser/renderer_host/media/audio_input_device_manager.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "content/public/browser/browser_thread.h" | 9 #include "content/public/browser/browser_thread.h" |
| 10 #include "content/public/common/media_stream_request.h" | 10 #include "content/public/common/media_stream_request.h" |
| 11 #include "media/audio/audio_device_name.h" |
| 11 #include "media/audio/audio_input_ipc.h" | 12 #include "media/audio/audio_input_ipc.h" |
| 12 #include "media/audio/audio_manager_base.h" | 13 #include "media/audio/audio_manager_base.h" |
| 13 #include "media/audio/audio_parameters.h" | 14 #include "media/audio/audio_parameters.h" |
| 14 #include "media/base/channel_layout.h" | 15 #include "media/base/channel_layout.h" |
| 15 #include "media/base/scoped_histogram_timer.h" | 16 #include "media/base/scoped_histogram_timer.h" |
| 16 | 17 |
| 17 namespace content { | 18 namespace content { |
| 18 | 19 |
| 19 const int AudioInputDeviceManager::kFakeOpenSessionId = 1; | 20 const int AudioInputDeviceManager::kFakeOpenSessionId = 1; |
| 20 | 21 |
| 21 namespace { | 22 namespace { |
| 22 // Starting id for the first capture session. | 23 // Starting id for the first capture session. |
| 23 const int kFirstSessionId = AudioInputDeviceManager::kFakeOpenSessionId + 1; | 24 const int kFirstSessionId = AudioInputDeviceManager::kFakeOpenSessionId + 1; |
| 24 } | 25 } |
| 25 | 26 |
| 26 AudioInputDeviceManager::AudioInputDeviceManager( | 27 AudioInputDeviceManager::AudioInputDeviceManager( |
| 27 media::AudioManager* audio_manager) | 28 media::AudioManager* audio_manager) |
| 28 : listener_(NULL), | 29 : listener_(NULL), |
| 29 next_capture_session_id_(kFirstSessionId), | 30 next_capture_session_id_(kFirstSessionId), |
| 30 use_fake_device_(false), | 31 use_fake_device_(false), |
| 31 audio_manager_(audio_manager) { | 32 audio_manager_(audio_manager) { |
| 33 // TODO(xians): Remove this fake_device after the unittests do not need it. |
| 34 StreamDeviceInfo fake_device(MEDIA_DEVICE_AUDIO_CAPTURE, |
| 35 media::AudioManagerBase::kDefaultDeviceName, |
| 36 media::AudioManagerBase::kDefaultDeviceId, |
| 37 44100, media::CHANNEL_LAYOUT_STEREO, |
| 38 0); |
| 39 fake_device.session_id = kFakeOpenSessionId; |
| 40 devices_.push_back(fake_device); |
| 32 } | 41 } |
| 33 | 42 |
| 34 AudioInputDeviceManager::~AudioInputDeviceManager() { | 43 AudioInputDeviceManager::~AudioInputDeviceManager() { |
| 35 } | 44 } |
| 36 | 45 |
| 37 const StreamDeviceInfo* AudioInputDeviceManager::GetOpenedDeviceInfoById( | 46 const StreamDeviceInfo* AudioInputDeviceManager::GetOpenedDeviceInfoById( |
| 38 int session_id) { | 47 int session_id) { |
| 39 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 48 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 40 StreamDeviceList::iterator device = GetDevice(session_id); | 49 StreamDeviceList::iterator device = GetDevice(session_id); |
| 41 if (device == devices_.end()) | 50 if (device == devices_.end()) |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 107 bool AudioInputDeviceManager::ShouldUseFakeDevice() const { | 116 bool AudioInputDeviceManager::ShouldUseFakeDevice() const { |
| 108 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 117 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 109 return use_fake_device_; | 118 return use_fake_device_; |
| 110 } | 119 } |
| 111 | 120 |
| 112 void AudioInputDeviceManager::EnumerateOnDeviceThread( | 121 void AudioInputDeviceManager::EnumerateOnDeviceThread( |
| 113 MediaStreamType stream_type) { | 122 MediaStreamType stream_type) { |
| 114 SCOPED_UMA_HISTOGRAM_TIMER( | 123 SCOPED_UMA_HISTOGRAM_TIMER( |
| 115 "Media.AudioInputDeviceManager.EnumerateOnDeviceThreadTime"); | 124 "Media.AudioInputDeviceManager.EnumerateOnDeviceThreadTime"); |
| 116 DCHECK(IsOnDeviceThread()); | 125 DCHECK(IsOnDeviceThread()); |
| 117 DCHECK_EQ(MEDIA_DEVICE_AUDIO_CAPTURE, stream_type); | |
| 118 | 126 |
| 119 media::AudioDeviceNames device_names; | 127 media::AudioDeviceNames device_names; |
| 120 if (use_fake_device_) { | 128 |
| 121 // Use the fake devices. | 129 switch (stream_type) { |
| 122 GetFakeDeviceNames(&device_names); | 130 case MEDIA_DEVICE_AUDIO_CAPTURE: |
| 123 } else { | 131 // AudioManager is guaranteed to outlive MediaStreamManager in |
| 124 // Enumerate the devices on the OS. | 132 // BrowserMainloop. |
| 125 // AudioManager is guaranteed to outlive MediaStreamManager in | 133 audio_manager_->GetAudioInputDeviceNames(&device_names); |
| 126 // BrowserMainloop. | 134 break; |
| 127 media::AudioDeviceNames device_names; | 135 |
| 128 audio_manager_->GetAudioInputDeviceNames(&device_names); | 136 default: |
| 137 NOTREACHED(); |
| 138 break; |
| 129 } | 139 } |
| 130 | 140 |
| 131 scoped_ptr<StreamDeviceInfoArray> devices(new StreamDeviceInfoArray()); | 141 scoped_ptr<StreamDeviceInfoArray> devices(new StreamDeviceInfoArray()); |
| 132 for (media::AudioDeviceNames::iterator it = device_names.begin(); | 142 for (media::AudioDeviceNames::iterator it = device_names.begin(); |
| 133 it != device_names.end(); ++it) { | 143 it != device_names.end(); ++it) { |
| 134 // Add device information to device vector. | 144 // Add device information to device vector. |
| 135 devices->push_back(StreamDeviceInfo( | 145 devices->push_back(StreamDeviceInfo( |
| 136 stream_type, it->device_name, it->unique_id)); | 146 stream_type, it->device_name, it->unique_id)); |
| 137 } | 147 } |
| 138 | 148 |
| 149 // If the |use_fake_device_| flag is on, inject the fake device if there is |
| 150 // no available device on the OS. |
| 151 if (use_fake_device_ && devices->empty()) { |
| 152 devices->push_back(StreamDeviceInfo( |
| 153 stream_type, media::AudioManagerBase::kDefaultDeviceName, |
| 154 media::AudioManagerBase::kDefaultDeviceId)); |
| 155 } |
| 156 |
| 139 // Return the device list through the listener by posting a task on | 157 // Return the device list through the listener by posting a task on |
| 140 // IO thread since MediaStreamManager handles the callback asynchronously. | 158 // IO thread since MediaStreamManager handles the callback asynchronously. |
| 141 BrowserThread::PostTask( | 159 BrowserThread::PostTask( |
| 142 BrowserThread::IO, | 160 BrowserThread::IO, |
| 143 FROM_HERE, | 161 FROM_HERE, |
| 144 base::Bind(&AudioInputDeviceManager::DevicesEnumeratedOnIOThread, | 162 base::Bind(&AudioInputDeviceManager::DevicesEnumeratedOnIOThread, |
| 145 this, stream_type, base::Passed(&devices))); | 163 this, stream_type, base::Passed(&devices))); |
| 146 } | 164 } |
| 147 | 165 |
| 148 void AudioInputDeviceManager::OpenOnDeviceThread( | 166 void AudioInputDeviceManager::OpenOnDeviceThread( |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 230 AudioInputDeviceManager::GetDevice(int session_id) { | 248 AudioInputDeviceManager::GetDevice(int session_id) { |
| 231 for (StreamDeviceList::iterator i(devices_.begin()); i != devices_.end(); | 249 for (StreamDeviceList::iterator i(devices_.begin()); i != devices_.end(); |
| 232 ++i) { | 250 ++i) { |
| 233 if (i->session_id == session_id) | 251 if (i->session_id == session_id) |
| 234 return i; | 252 return i; |
| 235 } | 253 } |
| 236 | 254 |
| 237 return devices_.end(); | 255 return devices_.end(); |
| 238 } | 256 } |
| 239 | 257 |
| 240 void AudioInputDeviceManager::GetFakeDeviceNames( | |
| 241 media::AudioDeviceNames* device_names) { | |
| 242 static const char kFakeDeviceName1[] = "Fake Audio 1"; | |
| 243 static const char kFakeDeviceId1[] = "fake_audio_1"; | |
| 244 static const char kFakeDeviceName2[] = "Fake Audio 2"; | |
| 245 static const char kFakeDeviceId2[] = "fake_audio_2"; | |
| 246 DCHECK(device_names->empty()); | |
| 247 DCHECK(use_fake_device_); | |
| 248 device_names->push_back(media::AudioDeviceName(kFakeDeviceName1, | |
| 249 kFakeDeviceId1)); | |
| 250 device_names->push_back(media::AudioDeviceName(kFakeDeviceName2, | |
| 251 kFakeDeviceId2)); | |
| 252 } | |
| 253 | |
| 254 } // namespace content | 258 } // namespace content |
| OLD | NEW |