Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "media/audio/audio_io.h" | 5 #include "media/audio/audio_io.h" |
| 6 | 6 |
| 7 #include <windows.h> | 7 #include <windows.h> |
| 8 #include <objbase.h> // This has to be before initguid.h | 8 #include <objbase.h> // This has to be before initguid.h |
| 9 #include <initguid.h> | 9 #include <initguid.h> |
| 10 #include <mmsystem.h> | 10 #include <mmsystem.h> |
| 11 #include <setupapi.h> | 11 #include <setupapi.h> |
| 12 | 12 |
| 13 #include "base/basictypes.h" | 13 #include "base/basictypes.h" |
| 14 #include "base/command_line.h" | 14 #include "base/command_line.h" |
| 15 #include "base/file_path.h" | 15 #include "base/file_path.h" |
| 16 #include "base/memory/scoped_ptr.h" | 16 #include "base/memory/scoped_ptr.h" |
| 17 #include "base/path_service.h" | 17 #include "base/path_service.h" |
| 18 #include "base/process_util.h" | 18 #include "base/process_util.h" |
| 19 #include "base/string_number_conversions.h" | 19 #include "base/string_number_conversions.h" |
| 20 #include "base/string_util.h" | 20 #include "base/string_util.h" |
| 21 #include "media/audio/audio_util.h" | 21 #include "media/audio/audio_util.h" |
| 22 #include "media/audio/fake_audio_input_stream.h" | |
| 23 #include "media/audio/fake_audio_output_stream.h" | |
| 24 #include "media/audio/win/audio_low_latency_input_win.h" | 22 #include "media/audio/win/audio_low_latency_input_win.h" |
| 25 #include "media/audio/win/audio_low_latency_output_win.h" | 23 #include "media/audio/win/audio_low_latency_output_win.h" |
| 26 #include "media/audio/win/audio_manager_win.h" | 24 #include "media/audio/win/audio_manager_win.h" |
| 27 #include "media/audio/win/device_enumeration_win.h" | 25 #include "media/audio/win/device_enumeration_win.h" |
| 28 #include "media/audio/win/wavein_input_win.h" | 26 #include "media/audio/win/wavein_input_win.h" |
| 29 #include "media/audio/win/waveout_output_win.h" | 27 #include "media/audio/win/waveout_output_win.h" |
| 30 #include "media/base/limits.h" | 28 #include "media/base/limits.h" |
| 31 | 29 |
| 32 // Libraries required for the SetupAPI and Wbem APIs used here. | 30 // Libraries required for the SetupAPI and Wbem APIs used here. |
| 33 #pragma comment(lib, "setupapi.lib") | 31 #pragma comment(lib, "setupapi.lib") |
| 34 | 32 |
| 35 // The following are defined in various DDK headers, and we (re)define them | 33 // The following are defined in various DDK headers, and we (re)define them |
| 36 // here to avoid adding the DDK as a chrome dependency. | 34 // here to avoid adding the DDK as a chrome dependency. |
| 37 #define DRV_QUERYDEVICEINTERFACE 0x80c | 35 #define DRV_QUERYDEVICEINTERFACE 0x80c |
| 38 #define DRVM_MAPPER_PREFERRED_GET 0x2015 | 36 #define DRVM_MAPPER_PREFERRED_GET 0x2015 |
| 39 #define DRV_QUERYDEVICEINTERFACESIZE 0x80d | 37 #define DRV_QUERYDEVICEINTERFACESIZE 0x80d |
| 40 DEFINE_GUID(AM_KSCATEGORY_AUDIO, 0x6994ad04, 0x93ef, 0x11d0, | 38 DEFINE_GUID(AM_KSCATEGORY_AUDIO, 0x6994ad04, 0x93ef, 0x11d0, |
| 41 0xa3, 0xcc, 0x00, 0xa0, 0xc9, 0x22, 0x31, 0x96); | 39 0xa3, 0xcc, 0x00, 0xa0, 0xc9, 0x22, 0x31, 0x96); |
| 42 | 40 |
| 43 // Maximum number of output streams that can be open simultaneously. | 41 // Maximum number of output streams that can be open simultaneously. |
| 44 static const size_t kMaxOutputStreams = 50; | 42 static const int kMaxOutputStreams = 50; |
| 45 | 43 |
| 46 // Up to 8 channels can be passed to the driver. | 44 // Up to 8 channels can be passed to the driver. |
| 47 // This should work, given the right drivers, but graceful error handling is | 45 // This should work, given the right drivers, but graceful error handling is |
| 48 // needed. | 46 // needed. |
| 49 static const int kWinMaxChannels = 8; | 47 static const int kWinMaxChannels = 8; |
| 50 | 48 |
| 51 static const int kWinMaxInputChannels = 2; | 49 static const int kWinMaxInputChannels = 2; |
| 52 // We use 3 buffers for recording audio so that if a recording callback takes | 50 // We use 3 buffers for recording audio so that if a recording callback takes |
| 53 // some time to return we won't lose audio. More buffers while recording are | 51 // some time to return we won't lose audio. More buffers while recording are |
| 54 // ok because they don't introduce any delay in recording, unlike in playback | 52 // ok because they don't introduce any delay in recording, unlike in playback |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 91 } | 89 } |
| 92 SetupDiDestroyDriverInfoList(device_info, device_data, SPDIT_COMPATDRIVER); | 90 SetupDiDestroyDriverInfoList(device_info, device_data, SPDIT_COMPATDRIVER); |
| 93 } | 91 } |
| 94 | 92 |
| 95 SetupDiSetDeviceInstallParams(device_info, device_data, | 93 SetupDiSetDeviceInstallParams(device_info, device_data, |
| 96 &old_device_install_params); | 94 &old_device_install_params); |
| 97 | 95 |
| 98 return device_and_driver_info; | 96 return device_and_driver_info; |
| 99 } | 97 } |
| 100 | 98 |
| 101 AudioManagerWin::AudioManagerWin() | 99 AudioManagerWin::AudioManagerWin() { |
| 102 : num_output_streams_(0) { | |
| 103 if (!media::IsWASAPISupported()) { | 100 if (!media::IsWASAPISupported()) { |
| 104 // Use the Wave API for device enumeration if XP or lower. | 101 // Use the Wave API for device enumeration if XP or lower. |
| 105 enumeration_type_ = kWaveEnumeration; | 102 enumeration_type_ = kWaveEnumeration; |
| 106 } else { | 103 } else { |
| 107 // Use the MMDevice API for device enumeration if Vista or higher. | 104 // Use the MMDevice API for device enumeration if Vista or higher. |
| 108 enumeration_type_ = kMMDeviceEnumeration; | 105 enumeration_type_ = kMMDeviceEnumeration; |
| 109 } | 106 } |
| 110 } | 107 } |
| 111 | 108 |
| 112 AudioManagerWin::~AudioManagerWin() { | 109 AudioManagerWin::~AudioManagerWin() { |
| 113 // All output streams should be released upon termination. | |
| 114 DCHECK_EQ(0, num_output_streams_); | |
| 115 } | 110 } |
| 116 | 111 |
| 117 bool AudioManagerWin::HasAudioOutputDevices() { | 112 bool AudioManagerWin::HasAudioOutputDevices() { |
| 118 return (::waveOutGetNumDevs() != 0); | 113 return (::waveOutGetNumDevs() != 0); |
| 119 } | 114 } |
| 120 | 115 |
| 121 bool AudioManagerWin::HasAudioInputDevices() { | 116 bool AudioManagerWin::HasAudioInputDevices() { |
| 122 return (::waveInGetNumDevs() != 0); | 117 return (::waveInGetNumDevs() != 0); |
| 123 } | 118 } |
| 124 | 119 |
| 125 // Factory for the implementations of AudioOutputStream. Two implementations | |
| 126 // should suffice most windows user's needs. | |
| 127 // - PCMWaveOutAudioOutputStream: Based on the waveOut API. | |
| 128 // - WASAPIAudioOutputStream: Based on Core Audio (WASAPI) API. | |
| 129 AudioOutputStream* AudioManagerWin::MakeAudioOutputStream( | |
| 130 const AudioParameters& params) { | |
| 131 if (!params.IsValid() || (params.channels > kWinMaxChannels)) | |
| 132 return NULL; | |
| 133 | |
| 134 // Limit the number of audio streams opened. | |
| 135 if (num_output_streams_ >= kMaxOutputStreams) { | |
| 136 return NULL; | |
| 137 } | |
| 138 | |
| 139 if (params.format == AudioParameters::AUDIO_MOCK) { | |
| 140 return FakeAudioOutputStream::MakeFakeStream(params); | |
| 141 } else if (params.format == AudioParameters::AUDIO_PCM_LINEAR) { | |
| 142 num_output_streams_++; | |
| 143 return new PCMWaveOutAudioOutputStream(this, params, 3, WAVE_MAPPER); | |
| 144 } else if (params.format == AudioParameters::AUDIO_PCM_LOW_LATENCY) { | |
| 145 num_output_streams_++; | |
| 146 if (!media::IsWASAPISupported()) { | |
| 147 // Fall back to Windows Wave implementation on Windows XP or lower. | |
| 148 DLOG(INFO) << "Using WaveOut since WASAPI requires at least Vista."; | |
| 149 return new PCMWaveOutAudioOutputStream(this, params, 2, WAVE_MAPPER); | |
| 150 } else { | |
| 151 // TODO(henrika): improve possibility to specify audio endpoint. | |
| 152 // Use the default device (same as for Wave) for now to be compatible. | |
| 153 return new WASAPIAudioOutputStream(this, params, eConsole); | |
| 154 } | |
| 155 } | |
| 156 return NULL; | |
| 157 } | |
| 158 | |
| 159 // Factory for the implementations of AudioInputStream. | |
| 160 AudioInputStream* AudioManagerWin::MakeAudioInputStream( | |
| 161 const AudioParameters& params, const std::string& device_id) { | |
| 162 if (!params.IsValid() || (params.channels > kWinMaxInputChannels) || | |
| 163 device_id.empty()) | |
| 164 return NULL; | |
| 165 | |
| 166 if (params.format == AudioParameters::AUDIO_MOCK) { | |
| 167 return FakeAudioInputStream::MakeFakeStream(params); | |
| 168 } else if (params.format == AudioParameters::AUDIO_PCM_LINEAR) { | |
| 169 return new PCMWaveInAudioInputStream(this, params, kNumInputBuffers, | |
| 170 AudioManagerBase::kDefaultDeviceId); | |
| 171 } else if (params.format == AudioParameters::AUDIO_PCM_LOW_LATENCY) { | |
| 172 if (!media::IsWASAPISupported()) { | |
| 173 // Fall back to Windows Wave implementation on Windows XP or lower. | |
| 174 DLOG(INFO) << "Using WaveIn since WASAPI requires at least Vista."; | |
| 175 return new PCMWaveInAudioInputStream(this, params, kNumInputBuffers, | |
| 176 device_id); | |
| 177 } else { | |
| 178 return new WASAPIAudioInputStream(this, params, device_id); | |
| 179 } | |
| 180 } | |
| 181 return NULL; | |
| 182 } | |
| 183 | |
| 184 void AudioManagerWin::ReleaseOutputStream(AudioOutputStream* stream) { | |
| 185 DCHECK(stream); | |
| 186 num_output_streams_--; | |
| 187 delete stream; | |
| 188 } | |
| 189 | |
| 190 void AudioManagerWin::ReleaseInputStream(AudioInputStream* stream) { | |
| 191 delete stream; | |
| 192 } | |
| 193 | |
| 194 void AudioManagerWin::MuteAll() { | 120 void AudioManagerWin::MuteAll() { |
| 195 } | 121 } |
| 196 | 122 |
| 197 void AudioManagerWin::UnMuteAll() { | 123 void AudioManagerWin::UnMuteAll() { |
| 198 } | 124 } |
| 199 | 125 |
| 200 string16 AudioManagerWin::GetAudioInputDeviceModel() { | 126 string16 AudioManagerWin::GetAudioInputDeviceModel() { |
| 201 // Get the default audio capture device and its device interface name. | 127 // Get the default audio capture device and its device interface name. |
| 202 DWORD device_id = 0; | 128 DWORD device_id = 0; |
| 203 waveInMessage(reinterpret_cast<HWAVEIN>(WAVE_MAPPER), | 129 waveInMessage(reinterpret_cast<HWAVEIN>(WAVE_MAPPER), |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 298 | 224 |
| 299 // Always add default device parameters as first element. | 225 // Always add default device parameters as first element. |
| 300 if (!device_names->empty()) { | 226 if (!device_names->empty()) { |
| 301 media::AudioDeviceName name; | 227 media::AudioDeviceName name; |
| 302 name.device_name = AudioManagerBase::kDefaultDeviceName; | 228 name.device_name = AudioManagerBase::kDefaultDeviceName; |
| 303 name.unique_id = AudioManagerBase::kDefaultDeviceId; | 229 name.unique_id = AudioManagerBase::kDefaultDeviceId; |
| 304 device_names->push_front(name); | 230 device_names->push_front(name); |
| 305 } | 231 } |
| 306 } | 232 } |
| 307 | 233 |
| 234 int AudioManagerWin::GetMaxAudioOutputStreamsAllowed() { | |
| 235 return kMaxOutputStreams; | |
| 236 } | |
| 237 | |
| 238 // Factory for the implementations of AudioOutputStream for AUDIO_PCM_LINEAR | |
| 239 // mode. | |
| 240 // - PCMWaveOutAudioOutputStream: Based on the waveOut API. | |
| 241 AudioOutputStream* AudioManagerWin::MakeAudioLinearOutputStream( | |
| 242 const AudioParameters& params) { | |
| 243 DCHECK_EQ(AudioParameters::AUDIO_PCM_LINEAR, params.format); | |
| 244 if (params.channels > kWinMaxChannels) | |
| 245 return NULL; | |
| 246 | |
| 247 return new PCMWaveOutAudioOutputStream(this, params, 3, WAVE_MAPPER); | |
| 248 } | |
| 249 | |
| 250 // Factory for the implementations of AudioOutputStream for | |
| 251 // AUDIO_PCM_LOW_LATENCY mode. Two implementations should suffice most | |
| 252 // windows user's needs. | |
| 253 // - PCMWaveOutAudioOutputStream: Based on the waveOut API. | |
| 254 // - WASAPIAudioOutputStream: Based on Core Audio (WASAPI) API. | |
| 255 AudioOutputStream* AudioManagerWin::MakeAudioLowLatencyOutputStream( | |
| 256 const AudioParameters& params) { | |
| 257 DCHECK_EQ(AudioParameters::AUDIO_PCM_LOW_LATENCY, params.format); | |
| 258 if (params.channels > kWinMaxChannels) | |
| 259 return NULL; | |
| 260 | |
| 261 AudioOutputStream* stream = NULL; | |
| 262 if (!media::IsWASAPISupported()) { | |
| 263 // Fall back to Windows Wave implementation on Windows XP or lower. | |
| 264 DLOG(INFO) << "Using WaveOut since WASAPI requires at least Vista."; | |
|
tommi (sloooow) - chröme
2012/03/05 14:28:28
DVLOG(1)
(same for any other DLOG(INFO))
no longer working on chromium
2012/03/06 15:27:07
Done.
| |
| 265 stream = new PCMWaveOutAudioOutputStream(this, params, 2, WAVE_MAPPER); | |
| 266 } else { | |
| 267 // TODO(henrika): improve possibility to specify audio endpoint. | |
| 268 // Use the default device (same as for Wave) for now to be compatible. | |
| 269 stream = new WASAPIAudioOutputStream(this, params, eConsole); | |
| 270 } | |
| 271 | |
| 272 return stream; | |
| 273 } | |
| 274 | |
| 275 // Factory for the implementations of AudioInputStream for AUDIO_PCM_LINEAR | |
| 276 // mode. | |
| 277 AudioInputStream* AudioManagerWin::MakeAudioLinearInputStream( | |
| 278 const AudioParameters& params, const std::string& device_id) { | |
| 279 DCHECK_EQ(AudioParameters::AUDIO_PCM_LINEAR, params.format); | |
| 280 if (params.channels > kWinMaxInputChannels) | |
| 281 return NULL; | |
| 282 | |
| 283 return new PCMWaveInAudioInputStream(this, params, kNumInputBuffers, | |
| 284 AudioManagerBase::kDefaultDeviceId); | |
| 285 } | |
| 286 | |
| 287 // Factory for the implementations of AudioInputStream for | |
| 288 // AUDIO_PCM_LOW_LATENCY mode. | |
| 289 AudioInputStream* AudioManagerWin::MakeAudioLowLatencyInputStream( | |
| 290 const AudioParameters& params, const std::string& device_id) { | |
| 291 DCHECK_EQ(AudioParameters::AUDIO_PCM_LOW_LATENCY, params.format); | |
| 292 if (params.channels > kWinMaxInputChannels) | |
| 293 return NULL; | |
| 294 | |
| 295 AudioInputStream* stream = NULL; | |
| 296 if (!media::IsWASAPISupported()) { | |
| 297 // Fall back to Windows Wave implementation on Windows XP or lower. | |
| 298 DLOG(INFO) << "Using WaveIn since WASAPI requires at least Vista."; | |
| 299 stream = new PCMWaveInAudioInputStream(this, params, kNumInputBuffers, | |
| 300 device_id); | |
| 301 } else { | |
| 302 stream = new WASAPIAudioInputStream(this, params, device_id); | |
| 303 } | |
| 304 | |
| 305 return stream; | |
| 306 } | |
| 307 | |
| 308 /// static | 308 /// static |
| 309 AudioManager* CreateAudioManager() { | 309 AudioManager* CreateAudioManager() { |
| 310 return new AudioManagerWin(); | 310 return new AudioManagerWin(); |
| 311 } | 311 } |
| OLD | NEW |