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" |
(...skipping 11 matching lines...) Expand all Loading... |
22 // Starting id for the first capture session. | 22 // Starting id for the first capture session. |
23 const int kFirstSessionId = AudioInputDeviceManager::kFakeOpenSessionId + 1; | 23 const int kFirstSessionId = AudioInputDeviceManager::kFakeOpenSessionId + 1; |
24 } | 24 } |
25 | 25 |
26 AudioInputDeviceManager::AudioInputDeviceManager( | 26 AudioInputDeviceManager::AudioInputDeviceManager( |
27 media::AudioManager* audio_manager) | 27 media::AudioManager* audio_manager) |
28 : listener_(NULL), | 28 : listener_(NULL), |
29 next_capture_session_id_(kFirstSessionId), | 29 next_capture_session_id_(kFirstSessionId), |
30 use_fake_device_(false), | 30 use_fake_device_(false), |
31 audio_manager_(audio_manager) { | 31 audio_manager_(audio_manager) { |
| 32 // TODO(xians): Remove this fake_device after the unittests do not need it. |
| 33 StreamDeviceInfo fake_device(MEDIA_DEVICE_AUDIO_CAPTURE, |
| 34 media::AudioManagerBase::kDefaultDeviceName, |
| 35 media::AudioManagerBase::kDefaultDeviceId, |
| 36 44100, media::CHANNEL_LAYOUT_STEREO, false); |
| 37 fake_device.session_id = kFakeOpenSessionId; |
| 38 devices_.push_back(fake_device); |
32 } | 39 } |
33 | 40 |
34 AudioInputDeviceManager::~AudioInputDeviceManager() { | 41 AudioInputDeviceManager::~AudioInputDeviceManager() { |
35 } | 42 } |
36 | 43 |
37 const StreamDeviceInfo* AudioInputDeviceManager::GetOpenedDeviceInfoById( | 44 const StreamDeviceInfo* AudioInputDeviceManager::GetOpenedDeviceInfoById( |
38 int session_id) { | 45 int session_id) { |
39 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 46 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
40 StreamDeviceList::iterator device = GetDevice(session_id); | 47 StreamDeviceList::iterator device = GetDevice(session_id); |
41 if (device == devices_.end()) | 48 if (device == devices_.end()) |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
81 return session_id; | 88 return session_id; |
82 } | 89 } |
83 | 90 |
84 void AudioInputDeviceManager::Close(int session_id) { | 91 void AudioInputDeviceManager::Close(int session_id) { |
85 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 92 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
86 DCHECK(listener_); | 93 DCHECK(listener_); |
87 StreamDeviceList::iterator device = GetDevice(session_id); | 94 StreamDeviceList::iterator device = GetDevice(session_id); |
88 if (device == devices_.end()) | 95 if (device == devices_.end()) |
89 return; | 96 return; |
90 const MediaStreamType stream_type = device->device.type; | 97 const MediaStreamType stream_type = device->device.type; |
91 devices_.erase(device); | 98 if (session_id != kFakeOpenSessionId) |
| 99 devices_.erase(device); |
92 | 100 |
93 // Post a callback through the listener on IO thread since | 101 // Post a callback through the listener on IO thread since |
94 // MediaStreamManager is expecting the callback asynchronously. | 102 // MediaStreamManager is expecting the callback asynchronously. |
95 BrowserThread::PostTask(BrowserThread::IO, | 103 BrowserThread::PostTask(BrowserThread::IO, |
96 FROM_HERE, | 104 FROM_HERE, |
97 base::Bind(&AudioInputDeviceManager::ClosedOnIOThread, | 105 base::Bind(&AudioInputDeviceManager::ClosedOnIOThread, |
98 this, stream_type, session_id)); | 106 this, stream_type, session_id)); |
99 } | 107 } |
100 | 108 |
101 void AudioInputDeviceManager::UseFakeDevice() { | 109 void AudioInputDeviceManager::UseFakeDevice() { |
(...skipping 25 matching lines...) Expand all Loading... |
127 } | 135 } |
128 | 136 |
129 scoped_ptr<StreamDeviceInfoArray> devices(new StreamDeviceInfoArray()); | 137 scoped_ptr<StreamDeviceInfoArray> devices(new StreamDeviceInfoArray()); |
130 for (media::AudioDeviceNames::iterator it = device_names.begin(); | 138 for (media::AudioDeviceNames::iterator it = device_names.begin(); |
131 it != device_names.end(); ++it) { | 139 it != device_names.end(); ++it) { |
132 // Add device information to device vector. | 140 // Add device information to device vector. |
133 devices->push_back(StreamDeviceInfo( | 141 devices->push_back(StreamDeviceInfo( |
134 stream_type, it->device_name, it->unique_id, false)); | 142 stream_type, it->device_name, it->unique_id, false)); |
135 } | 143 } |
136 | 144 |
| 145 // If the |use_fake_device_| flag is on, inject the fake device if there is |
| 146 // no available device on the OS. |
| 147 if (use_fake_device_ && devices->empty()) { |
| 148 devices->push_back(StreamDeviceInfo( |
| 149 stream_type, media::AudioManagerBase::kDefaultDeviceName, |
| 150 media::AudioManagerBase::kDefaultDeviceId, false)); |
| 151 } |
| 152 |
137 // Return the device list through the listener by posting a task on | 153 // Return the device list through the listener by posting a task on |
138 // IO thread since MediaStreamManager handles the callback asynchronously. | 154 // IO thread since MediaStreamManager handles the callback asynchronously. |
139 BrowserThread::PostTask( | 155 BrowserThread::PostTask( |
140 BrowserThread::IO, | 156 BrowserThread::IO, |
141 FROM_HERE, | 157 FROM_HERE, |
142 base::Bind(&AudioInputDeviceManager::DevicesEnumeratedOnIOThread, | 158 base::Bind(&AudioInputDeviceManager::DevicesEnumeratedOnIOThread, |
143 this, stream_type, base::Passed(&devices))); | 159 this, stream_type, base::Passed(&devices))); |
144 } | 160 } |
145 | 161 |
146 void AudioInputDeviceManager::OpenOnDeviceThread( | 162 void AudioInputDeviceManager::OpenOnDeviceThread( |
147 int session_id, const StreamDeviceInfo& info) { | 163 int session_id, const StreamDeviceInfo& info) { |
148 DCHECK(IsOnDeviceThread()); | 164 DCHECK(IsOnDeviceThread()); |
149 | 165 |
150 // Get the preferred sample rate and channel configuration for the | |
151 // audio device. | |
152 media::AudioParameters params = | |
153 audio_manager_->GetInputStreamParameters(info.device.id); | |
154 | |
155 StreamDeviceInfo out(info.device.type, info.device.name, info.device.id, | 166 StreamDeviceInfo out(info.device.type, info.device.name, info.device.id, |
156 params.sample_rate(), params.channel_layout(), false); | 167 0, 0, false); |
157 out.session_id = session_id; | 168 out.session_id = session_id; |
| 169 if (use_fake_device_) { |
| 170 // Don't need to query the hardware information if using fake device. |
| 171 out.device.sample_rate = 44100; |
| 172 out.device.channel_layout = media::CHANNEL_LAYOUT_STEREO; |
| 173 } else { |
| 174 // Get the preferred sample rate and channel configuration for the |
| 175 // audio device. |
| 176 media::AudioParameters params = |
| 177 audio_manager_->GetInputStreamParameters(info.device.id); |
| 178 out.device.sample_rate = params.sample_rate(); |
| 179 out.device.channel_layout = params.channel_layout(); |
| 180 } |
158 | 181 |
159 // Return the |session_id| through the listener by posting a task on | 182 // Return the |session_id| through the listener by posting a task on |
160 // IO thread since MediaStreamManager handles the callback asynchronously. | 183 // IO thread since MediaStreamManager handles the callback asynchronously. |
161 BrowserThread::PostTask(BrowserThread::IO, | 184 BrowserThread::PostTask(BrowserThread::IO, |
162 FROM_HERE, | 185 FROM_HERE, |
163 base::Bind(&AudioInputDeviceManager::OpenedOnIOThread, | 186 base::Bind(&AudioInputDeviceManager::OpenedOnIOThread, |
164 this, session_id, out)); | 187 this, session_id, out)); |
165 } | 188 } |
166 | 189 |
167 void AudioInputDeviceManager::DevicesEnumeratedOnIOThread( | 190 void AudioInputDeviceManager::DevicesEnumeratedOnIOThread( |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
200 for (StreamDeviceList::iterator i(devices_.begin()); i != devices_.end(); | 223 for (StreamDeviceList::iterator i(devices_.begin()); i != devices_.end(); |
201 ++i) { | 224 ++i) { |
202 if (i->session_id == session_id) | 225 if (i->session_id == session_id) |
203 return i; | 226 return i; |
204 } | 227 } |
205 | 228 |
206 return devices_.end(); | 229 return devices_.end(); |
207 } | 230 } |
208 | 231 |
209 } // namespace content | 232 } // namespace content |
OLD | NEW |