Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(314)

Side by Side Diff: media/audio/audio_util.cc

Issue 8283032: Low-latency AudioInputStream implementation based on WASAPI for Windows. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: Added ViewHostMsg_GetHardwareInputSampleRate IPC message Created 9 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 // Software adjust volume of samples, allows each audio stream its own 5 // Software adjust volume of samples, allows each audio stream its own
6 // volume without impacting master volume for chrome and other applications. 6 // volume without impacting master volume for chrome and other applications.
7 7
8 // Implemented as templates to allow 8, 16 and 32 bit implementations. 8 // Implemented as templates to allow 8, 16 and 32 bit implementations.
9 // 8 bit is unsigned and biased by 128. 9 // 8 bit is unsigned and biased by 128.
10 10
11 #include <algorithm> 11 #include <algorithm>
12 12
13 #include "base/atomicops.h" 13 #include "base/atomicops.h"
14 #include "base/basictypes.h" 14 #include "base/basictypes.h"
15 #include "base/logging.h" 15 #include "base/logging.h"
16 #include "base/shared_memory.h" 16 #include "base/shared_memory.h"
17 #if defined(OS_WIN)
18 #include "base/win/windows_version.h"
19 #endif
17 #include "media/audio/audio_util.h" 20 #include "media/audio/audio_util.h"
18 #if defined(OS_MACOSX) 21 #if defined(OS_MACOSX)
19 #include "media/audio/mac/audio_low_latency_output_mac.h" 22 #include "media/audio/mac/audio_low_latency_output_mac.h"
20 #endif 23 #endif
24 #if defined(OS_WIN)
25 #include "media/audio/win/audio_low_latency_input_win.h"
26 #endif
21 27
22 using base::subtle::Atomic32; 28 using base::subtle::Atomic32;
23 29
24 const uint32 kUnknownDataSize = static_cast<uint32>(-1); 30 const uint32 kUnknownDataSize = static_cast<uint32>(-1);
25 31
26 namespace media { 32 namespace media {
27 33
28 // TODO(fbarchard): Convert to intrinsics for better efficiency. 34 // TODO(fbarchard): Convert to intrinsics for better efficiency.
29 template<class Fixed> 35 template<class Fixed>
30 static int ScaleChannel(int channel, int volume) { 36 static int ScaleChannel(int channel, int volume) {
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after
223 if (sample < -32768.0) 229 if (sample < -32768.0)
224 sample = -32768.0; 230 sample = -32768.0;
225 else if (sample > 32767.0) 231 else if (sample > 32767.0)
226 sample = 32767.0; 232 sample = 32767.0;
227 233
228 destination[j * channels + i] = static_cast<int16>(sample); 234 destination[j * channels + i] = static_cast<int16>(sample);
229 } 235 }
230 } 236 }
231 } 237 }
232 238
233 double GetAudioHardwareSampleRate() 239 double GetAudioHardwareSampleRate() {
234 {
235 #if defined(OS_MACOSX) 240 #if defined(OS_MACOSX)
236 // Hardware sample-rate on the Mac can be configured, so we must query. 241 // Hardware sample-rate on the Mac can be configured, so we must query.
237 return AUAudioOutputStream::HardwareSampleRate(); 242 return AUAudioOutputStream::HardwareSampleRate();
238 #else 243 #else
239 // Hardware for Windows and Linux is nearly always 48KHz. 244 // Hardware for Windows and Linux is nearly always 48KHz.
240 // TODO(crogers) : return correct value in rare non-48KHz cases. 245 // TODO(crogers) : return correct value in rare non-48KHz cases.
241 return 48000.0; 246 return 48000.0;
242 #endif 247 #endif
243 } 248 }
244 249
250 double GetAudioInputHardwareSampleRate() {
251 #if defined(OS_MACOSX)
252 // Hardware sample-rate on the Mac can be configured, so we must query.
253 return AUAudioInputStream::HardwareSampleRate();
254 #elif defined(OS_WIN)
255 if (base::win::GetVersion() <= base::win::VERSION_XP) {
256 // Fall back to Windows Wave implementation on Windows XP or lower
257 // and use 48kHz as default input sample rate.
258 return 48000.0;
259 } else {
260 // Hardware sample-rate on Windows can be configured, so we must query.
261 // TODO(henrika): improve possibility to specify audio endpoint.
262 // Use the default device (same as for Wave) for now to be compatible.
263 return WASAPIAudioInputStream::HardwareSampleRate(eConsole);
264 }
265 #else
266 // Hardware for Linux is nearly always 48KHz.
267 // TODO(henrika) : return correct value in rare non-48KHz cases.
268 return 48000.0;
269 #endif
270 }
271
245 size_t GetAudioHardwareBufferSize() { 272 size_t GetAudioHardwareBufferSize() {
246 // The sizes here were determined by experimentation and are roughly 273 // The sizes here were determined by experimentation and are roughly
247 // the lowest value (for low latency) that still allowed glitch-free 274 // the lowest value (for low latency) that still allowed glitch-free
248 // audio under high loads. 275 // audio under high loads.
249 // 276 //
250 // For Mac OS X the chromium audio backend uses a low-latency 277 // For Mac OS X the chromium audio backend uses a low-latency
251 // CoreAudio API, so a low buffer size is possible. For other OSes, 278 // CoreAudio API, so a low buffer size is possible. For other OSes,
252 // further tuning may be needed. 279 // further tuning may be needed.
253 #if defined(OS_MACOSX) 280 #if defined(OS_MACOSX)
254 return 128; 281 return 128;
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
303 char* ptr = static_cast<char*>(shared_memory->memory()) + shared_memory_size; 330 char* ptr = static_cast<char*>(shared_memory->memory()) + shared_memory_size;
304 DCHECK_EQ(0u, reinterpret_cast<size_t>(ptr) & 3); 331 DCHECK_EQ(0u, reinterpret_cast<size_t>(ptr) & 3);
305 332
306 // Actual data size stored at the end of the buffer. 333 // Actual data size stored at the end of the buffer.
307 uint32 actual_data_size = 334 uint32 actual_data_size =
308 base::subtle::Acquire_Load(reinterpret_cast<volatile Atomic32*>(ptr)); 335 base::subtle::Acquire_Load(reinterpret_cast<volatile Atomic32*>(ptr));
309 return actual_data_size == kUnknownDataSize; 336 return actual_data_size == kUnknownDataSize;
310 } 337 }
311 338
312 } // namespace media 339 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698