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 "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/bind.h" | 13 #include "base/bind.h" |
14 #include "base/bind_helpers.h" | 14 #include "base/bind_helpers.h" |
15 #include "base/command_line.h" | 15 #include "base/command_line.h" |
16 #include "base/files/file_path.h" | 16 #include "base/files/file_path.h" |
17 #include "base/memory/scoped_ptr.h" | 17 #include "base/memory/scoped_ptr.h" |
18 #include "base/message_loop.h" | 18 #include "base/message_loop.h" |
19 #include "base/path_service.h" | 19 #include "base/path_service.h" |
20 #include "base/process_util.h" | 20 #include "base/process_util.h" |
21 #include "base/string_number_conversions.h" | 21 #include "base/string_number_conversions.h" |
22 #include "base/string_util.h" | 22 #include "base/string_util.h" |
23 #include "media/audio/audio_util.h" | 23 #include "media/audio/audio_parameters.h" |
24 #include "media/audio/win/audio_device_listener_win.h" | 24 #include "media/audio/win/audio_device_listener_win.h" |
25 #include "media/audio/win/audio_low_latency_input_win.h" | 25 #include "media/audio/win/audio_low_latency_input_win.h" |
26 #include "media/audio/win/audio_low_latency_output_win.h" | 26 #include "media/audio/win/audio_low_latency_output_win.h" |
27 #include "media/audio/win/audio_manager_win.h" | 27 #include "media/audio/win/audio_manager_win.h" |
28 #include "media/audio/win/audio_unified_win.h" | 28 #include "media/audio/win/audio_unified_win.h" |
29 #include "media/audio/win/core_audio_util_win.h" | 29 #include "media/audio/win/core_audio_util_win.h" |
30 #include "media/audio/win/device_enumeration_win.h" | 30 #include "media/audio/win/device_enumeration_win.h" |
31 #include "media/audio/win/wavein_input_win.h" | 31 #include "media/audio/win/wavein_input_win.h" |
32 #include "media/audio/win/waveout_output_win.h" | 32 #include "media/audio/win/waveout_output_win.h" |
33 #include "media/base/bind_to_loop.h" | 33 #include "media/base/bind_to_loop.h" |
34 #include "media/base/channel_layout.h" | |
34 #include "media/base/limits.h" | 35 #include "media/base/limits.h" |
35 #include "media/base/media_switches.h" | 36 #include "media/base/media_switches.h" |
36 | 37 |
37 // Libraries required for the SetupAPI and Wbem APIs used here. | 38 // Libraries required for the SetupAPI and Wbem APIs used here. |
38 #pragma comment(lib, "setupapi.lib") | 39 #pragma comment(lib, "setupapi.lib") |
39 | 40 |
40 // The following are defined in various DDK headers, and we (re)define them here | 41 // The following are defined in various DDK headers, and we (re)define them here |
41 // to avoid adding the DDK as a chrome dependency. | 42 // to avoid adding the DDK as a chrome dependency. |
42 #define DRV_QUERYDEVICEINTERFACE 0x80c | 43 #define DRV_QUERYDEVICEINTERFACE 0x80c |
43 #define DRVM_MAPPER_PREFERRED_GET 0x2015 | 44 #define DRVM_MAPPER_PREFERRED_GET 0x2015 |
(...skipping 10 matching lines...) Expand all Loading... | |
54 // right drivers, but graceful error handling is needed. | 55 // right drivers, but graceful error handling is needed. |
55 static const int kWinMaxChannels = 8; | 56 static const int kWinMaxChannels = 8; |
56 | 57 |
57 // We use 3 buffers for recording audio so that if a recording callback takes | 58 // We use 3 buffers for recording audio so that if a recording callback takes |
58 // some time to return we won't lose audio. More buffers while recording are | 59 // some time to return we won't lose audio. More buffers while recording are |
59 // ok because they don't introduce any delay in recording, unlike in playback | 60 // ok because they don't introduce any delay in recording, unlike in playback |
60 // where you first need to fill in that number of buffers before starting to | 61 // where you first need to fill in that number of buffers before starting to |
61 // play. | 62 // play. |
62 static const int kNumInputBuffers = 3; | 63 static const int kNumInputBuffers = 3; |
63 | 64 |
65 // Buffer size to use for input and output stream when a proper size can't be | |
66 // determined from the system | |
67 static const int kFallbackBufferSize = 2048; | |
68 | |
64 static int GetVersionPartAsInt(DWORDLONG num) { | 69 static int GetVersionPartAsInt(DWORDLONG num) { |
65 return static_cast<int>(num & 0xffff); | 70 return static_cast<int>(num & 0xffff); |
66 } | 71 } |
67 | 72 |
68 // Returns a string containing the given device's description and installed | 73 // Returns a string containing the given device's description and installed |
69 // driver version. | 74 // driver version. |
70 static string16 GetDeviceAndDriverInfo(HDEVINFO device_info, | 75 static string16 GetDeviceAndDriverInfo(HDEVINFO device_info, |
71 SP_DEVINFO_DATA* device_data) { | 76 SP_DEVINFO_DATA* device_data) { |
72 // Save the old install params setting and set a flag for the | 77 // Save the old install params setting and set a flag for the |
73 // SetupDiBuildDriverInfoList below to return only the installed drivers. | 78 // SetupDiBuildDriverInfoList below to return only the installed drivers. |
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
248 | 253 |
249 // Always add default device parameters as first element. | 254 // Always add default device parameters as first element. |
250 if (!device_names->empty()) { | 255 if (!device_names->empty()) { |
251 media::AudioDeviceName name; | 256 media::AudioDeviceName name; |
252 name.device_name = AudioManagerBase::kDefaultDeviceName; | 257 name.device_name = AudioManagerBase::kDefaultDeviceName; |
253 name.unique_id = AudioManagerBase::kDefaultDeviceId; | 258 name.unique_id = AudioManagerBase::kDefaultDeviceId; |
254 device_names->push_front(name); | 259 device_names->push_front(name); |
255 } | 260 } |
256 } | 261 } |
257 | 262 |
263 AudioParameters AudioManagerWin::GetDefaultOutputStreamParameters( | |
264 const AudioParameters& input_params) { | |
265 const CommandLine* cmd_line = CommandLine::ForCurrentProcess(); | |
266 ChannelLayout channel_layout = CHANNEL_LAYOUT_STEREO; | |
267 int sample_rate = 0; | |
268 int buffer_size = 0; | |
269 int bits_per_sample = 16; | |
270 int input_channels = 0; | |
271 if (!CoreAudioUtil::IsSupported()) { | |
272 // Fall back to Windows Wave implementation on Windows XP or lower. | |
273 if (input_params.IsValid()) { | |
274 // Use the client's input parameters if they are valid. | |
275 sample_rate = input_params.sample_rate(); | |
276 bits_per_sample = input_params.bits_per_sample(); | |
277 channel_layout = input_params.channel_layout(); | |
278 input_channels = input_params.input_channels(); | |
279 buffer_size = input_params.frames_per_buffer(); | |
280 } else { | |
281 // Use 48kHz as default input sample rate, kFallbackBufferSize as | |
282 // default buffer size. | |
283 sample_rate = 48000; | |
284 buffer_size = kFallbackBufferSize; | |
285 } | |
286 } else if (cmd_line->HasSwitch(switches::kEnableExclusiveAudio)) { | |
287 // TODO(crogers): tune these values for best possible WebAudio performance. | |
288 // WebRTC works well at 48kHz and a buffer size of 480 samples will be used | |
289 // for this case. Note that exclusive mode is experimental. | |
290 // This sample rate will be combined with a buffer size of 256 samples, | |
291 // which corresponds to an output delay of ~5.33ms. | |
292 sample_rate = 48000; | |
293 buffer_size = 256;16 | |
Chris Rogers
2013/03/04 01:04:11
256;16 ??
no longer working on chromium
2013/03/04 14:55:04
oh, a mistake. Removed now. thanks.
| |
294 } else { | |
295 // Hardware sample-rate on Windows can be configured, so we must query. | |
296 // TODO(henrika): improve possibility to specify an audio endpoint. | |
297 // Use the default device (same as for Wave) for now to be compatible. | |
298 sample_rate = WASAPIAudioOutputStream::HardwareSampleRate(); | |
299 | |
300 AudioParameters params; | |
301 HRESULT hr = CoreAudioUtil::GetPreferredAudioParameters(eRender, eConsole, | |
302 ¶ms); | |
303 buffer_size = FAILED(hr) ? kFallbackBufferSize : params.frames_per_buffer(); | |
304 channel_layout = WASAPIAudioOutputStream::HardwareChannelLayout(); | |
Chris Rogers
2013/03/04 01:04:11
you need to set |input_channels| here to what |inp
no longer working on chromium
2013/03/04 14:55:04
Fixed now.
| |
305 } | |
306 | |
307 return AudioParameters( | |
308 AudioParameters::AUDIO_PCM_LOW_LATENCY, channel_layout, input_channels, | |
309 sample_rate, bits_per_sample, buffer_size); | |
310 } | |
311 | |
312 AudioParameters AudioManagerWin::GetInputStreamParameters( | |
313 const std::string& device_id) { | |
314 int sample_rate = 0; | |
315 ChannelLayout channel_layout = CHANNEL_LAYOUT_NONE; | |
316 if (!CoreAudioUtil::IsSupported()) { | |
317 sample_rate = 48000; | |
318 channel_layout = CHANNEL_LAYOUT_STEREO; | |
319 } else { | |
320 sample_rate = WASAPIAudioInputStream::HardwareSampleRate(device_id); | |
321 channel_layout = | |
322 WASAPIAudioInputStream::HardwareChannelCount(device_id) == 1 ? | |
323 CHANNEL_LAYOUT_MONO : CHANNEL_LAYOUT_STEREO; | |
324 } | |
325 | |
326 // TODO(Henrika): improve the default buffer size value for input stream. | |
327 return AudioParameters( | |
328 AudioParameters::AUDIO_PCM_LOW_LATENCY, channel_layout, | |
329 sample_rate, 16, kFallbackBufferSize); | |
330 } | |
331 | |
258 // Factory for the implementations of AudioOutputStream for AUDIO_PCM_LINEAR | 332 // Factory for the implementations of AudioOutputStream for AUDIO_PCM_LINEAR |
259 // mode. | 333 // mode. |
260 // - PCMWaveOutAudioOutputStream: Based on the waveOut API. | 334 // - PCMWaveOutAudioOutputStream: Based on the waveOut API. |
261 AudioOutputStream* AudioManagerWin::MakeLinearOutputStream( | 335 AudioOutputStream* AudioManagerWin::MakeLinearOutputStream( |
262 const AudioParameters& params) { | 336 const AudioParameters& params) { |
263 DCHECK_EQ(AudioParameters::AUDIO_PCM_LINEAR, params.format()); | 337 DCHECK_EQ(AudioParameters::AUDIO_PCM_LINEAR, params.format()); |
264 if (params.channels() > kWinMaxChannels) | 338 if (params.channels() > kWinMaxChannels) |
265 return NULL; | 339 return NULL; |
266 | 340 |
267 return new PCMWaveOutAudioOutputStream(this, | 341 return new PCMWaveOutAudioOutputStream(this, |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
345 | 419 |
346 return new PCMWaveInAudioInputStream(this, params, kNumInputBuffers, | 420 return new PCMWaveInAudioInputStream(this, params, kNumInputBuffers, |
347 xp_device_id); | 421 xp_device_id); |
348 } | 422 } |
349 | 423 |
350 /// static | 424 /// static |
351 AudioManager* CreateAudioManager() { | 425 AudioManager* CreateAudioManager() { |
352 return new AudioManagerWin(); | 426 return new AudioManagerWin(); |
353 } | 427 } |
354 | 428 |
355 AudioParameters AudioManagerWin::GetPreferredLowLatencyOutputStreamParameters( | |
356 const AudioParameters& input_params) { | |
357 // If WASAPI isn't supported we'll fallback to WaveOut, which will take care | |
358 // of resampling and bits per sample changes. By setting these equal to the | |
359 // input values, AudioOutputResampler will skip resampling and bit per sample | |
360 // differences (since the input parameters will match the output parameters). | |
361 int sample_rate = input_params.sample_rate(); | |
362 int bits_per_sample = input_params.bits_per_sample(); | |
363 ChannelLayout channel_layout = input_params.channel_layout(); | |
364 int input_channels = input_params.input_channels(); | |
365 if (CoreAudioUtil::IsSupported()) { | |
366 sample_rate = GetAudioHardwareSampleRate(); | |
367 bits_per_sample = 16; | |
368 channel_layout = WASAPIAudioOutputStream::HardwareChannelLayout(); | |
369 } | |
370 | |
371 // TODO(dalecurtis): This should include hardware bits per channel eventually. | |
372 return AudioParameters( | |
373 AudioParameters::AUDIO_PCM_LOW_LATENCY, channel_layout, input_channels, | |
374 sample_rate, bits_per_sample, GetAudioHardwareBufferSize()); | |
375 } | |
376 | |
377 } // namespace media | 429 } // namespace media |
OLD | NEW |