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