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 // 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) | 17 #if defined(OS_WIN) |
18 #include "base/win/windows_version.h" | 18 #include "base/win/windows_version.h" |
19 #endif | 19 #endif |
20 #include "media/audio/audio_util.h" | 20 #include "media/audio/audio_util.h" |
21 #if defined(OS_MACOSX) | 21 #if defined(OS_MACOSX) |
22 #include "media/audio/mac/audio_low_latency_input_mac.h" | 22 #include "media/audio/mac/audio_low_latency_input_mac.h" |
23 #include "media/audio/mac/audio_low_latency_output_mac.h" | 23 #include "media/audio/mac/audio_low_latency_output_mac.h" |
24 #endif | 24 #endif |
25 #if defined(OS_WIN) | 25 #if defined(OS_WIN) |
26 #include "media/audio/win/audio_low_latency_input_win.h" | 26 #include "media/audio/win/audio_low_latency_input_win.h" |
| 27 #include "media/audio/win/audio_low_latency_output_win.h" |
27 #endif | 28 #endif |
28 | 29 |
29 using base::subtle::Atomic32; | 30 using base::subtle::Atomic32; |
30 | 31 |
31 const uint32 kUnknownDataSize = static_cast<uint32>(-1); | 32 const uint32 kUnknownDataSize = static_cast<uint32>(-1); |
32 | 33 |
33 namespace media { | 34 namespace media { |
34 | 35 |
35 // TODO(fbarchard): Convert to intrinsics for better efficiency. | 36 // TODO(fbarchard): Convert to intrinsics for better efficiency. |
36 template<class Fixed> | 37 template<class Fixed> |
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
234 | 235 |
235 destination[j * channels + i] = static_cast<int16>(sample); | 236 destination[j * channels + i] = static_cast<int16>(sample); |
236 } | 237 } |
237 } | 238 } |
238 } | 239 } |
239 | 240 |
240 double GetAudioHardwareSampleRate() { | 241 double GetAudioHardwareSampleRate() { |
241 #if defined(OS_MACOSX) | 242 #if defined(OS_MACOSX) |
242 // Hardware sample-rate on the Mac can be configured, so we must query. | 243 // Hardware sample-rate on the Mac can be configured, so we must query. |
243 return AUAudioOutputStream::HardwareSampleRate(); | 244 return AUAudioOutputStream::HardwareSampleRate(); |
| 245 #elif defined(OS_WIN) |
| 246 if (base::win::GetVersion() <= base::win::VERSION_XP) { |
| 247 // Fall back to Windows Wave implementation on Windows XP or lower |
| 248 // and use 48kHz as default input sample rate. |
| 249 return 48000.0; |
| 250 } |
| 251 |
| 252 // Hardware sample-rate on Windows can be configured, so we must query. |
| 253 // TODO(henrika): improve possibility to specify audio endpoint. |
| 254 // Use the default device (same as for Wave) for now to be compatible. |
| 255 return WASAPIAudioOutputStream::HardwareSampleRate(eConsole); |
244 #else | 256 #else |
245 // Hardware for Windows and Linux is nearly always 48KHz. | 257 // Hardware for Linux is nearly always 48KHz. |
246 // TODO(crogers) : return correct value in rare non-48KHz cases. | 258 // TODO(crogers) : return correct value in rare non-48KHz cases. |
247 return 48000.0; | 259 return 48000.0; |
248 #endif | 260 #endif |
249 } | 261 } |
250 | 262 |
251 double GetAudioInputHardwareSampleRate() { | 263 double GetAudioInputHardwareSampleRate() { |
252 #if defined(OS_MACOSX) | 264 #if defined(OS_MACOSX) |
253 // Hardware sample-rate on the Mac can be configured, so we must query. | 265 // Hardware sample-rate on the Mac can be configured, so we must query. |
254 return AUAudioInputStream::HardwareSampleRate(); | 266 return AUAudioInputStream::HardwareSampleRate(); |
255 #elif defined(OS_WIN) | 267 #elif defined(OS_WIN) |
256 if (base::win::GetVersion() <= base::win::VERSION_XP) { | 268 if (base::win::GetVersion() <= base::win::VERSION_XP) { |
257 // Fall back to Windows Wave implementation on Windows XP or lower | 269 // Fall back to Windows Wave implementation on Windows XP or lower |
258 // and use 48kHz as default input sample rate. | 270 // and use 48kHz as default input sample rate. |
259 return 48000.0; | 271 return 48000.0; |
260 } else { | |
261 // Hardware sample-rate on Windows can be configured, so we must query. | |
262 // TODO(henrika): improve possibility to specify audio endpoint. | |
263 // Use the default device (same as for Wave) for now to be compatible. | |
264 return WASAPIAudioInputStream::HardwareSampleRate(eConsole); | |
265 } | 272 } |
| 273 |
| 274 // Hardware sample-rate on Windows can be configured, so we must query. |
| 275 // TODO(henrika): improve possibility to specify audio endpoint. |
| 276 // Use the default device (same as for Wave) for now to be compatible. |
| 277 return WASAPIAudioInputStream::HardwareSampleRate(eConsole); |
266 #else | 278 #else |
267 // Hardware for Linux is nearly always 48KHz. | 279 // Hardware for Linux is nearly always 48KHz. |
268 // TODO(henrika): return correct value in rare non-48KHz cases. | 280 // TODO(henrika): return correct value in rare non-48KHz cases. |
269 return 48000.0; | 281 return 48000.0; |
270 #endif | 282 #endif |
271 } | 283 } |
272 | 284 |
273 size_t GetAudioHardwareBufferSize() { | 285 size_t GetAudioHardwareBufferSize() { |
274 // The sizes here were determined by experimentation and are roughly | 286 // The sizes here were determined by experimentation and are roughly |
275 // the lowest value (for low latency) that still allowed glitch-free | 287 // the lowest value (for low latency) that still allowed glitch-free |
276 // audio under high loads. | 288 // audio under high loads. |
277 // | 289 // |
278 // For Mac OS X the chromium audio backend uses a low-latency | 290 // For Mac OS X and Windows the chromium audio backend uses a low-latency |
279 // CoreAudio API, so a low buffer size is possible. For other OSes, | 291 // Core Audio API, so a low buffer size is possible. For Linux, further |
280 // further tuning may be needed. | 292 // tuning may be needed. |
281 #if defined(OS_MACOSX) | 293 #if defined(OS_MACOSX) |
282 return 128; | 294 return 128; |
283 #elif defined(OS_LINUX) | 295 #elif defined(OS_WIN) |
284 return 2048; | 296 // This call must be done on a COM thread configured as MTA. |
| 297 // TODO(tommi): http://code.google.com/p/chromium/issues/detail?id=103835. |
| 298 int mixing_sample_rate = |
| 299 static_cast<int>(WASAPIAudioOutputStream::HardwareSampleRate(eConsole)); |
| 300 if (mixing_sample_rate == 48000) |
| 301 return 480; |
| 302 else if (mixing_sample_rate == 44100) |
| 303 return 448; |
| 304 else |
| 305 return 960; |
285 #else | 306 #else |
286 return 2048; | 307 return 2048; |
287 #endif | 308 #endif |
288 } | 309 } |
289 | 310 |
290 // When transferring data in the shared memory, first word is size of data | 311 // When transferring data in the shared memory, first word is size of data |
291 // in bytes. Actual data starts immediately after it. | 312 // in bytes. Actual data starts immediately after it. |
292 | 313 |
293 uint32 TotalSharedMemorySizeInBytes(uint32 packet_size) { | 314 uint32 TotalSharedMemorySizeInBytes(uint32 packet_size) { |
294 // Need to reserve extra 4 bytes for size of data. | 315 // Need to reserve extra 4 bytes for size of data. |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
331 char* ptr = static_cast<char*>(shared_memory->memory()) + shared_memory_size; | 352 char* ptr = static_cast<char*>(shared_memory->memory()) + shared_memory_size; |
332 DCHECK_EQ(0u, reinterpret_cast<size_t>(ptr) & 3); | 353 DCHECK_EQ(0u, reinterpret_cast<size_t>(ptr) & 3); |
333 | 354 |
334 // Actual data size stored at the end of the buffer. | 355 // Actual data size stored at the end of the buffer. |
335 uint32 actual_data_size = | 356 uint32 actual_data_size = |
336 base::subtle::Acquire_Load(reinterpret_cast<volatile Atomic32*>(ptr)); | 357 base::subtle::Acquire_Load(reinterpret_cast<volatile Atomic32*>(ptr)); |
337 return actual_data_size == kUnknownDataSize; | 358 return actual_data_size == kUnknownDataSize; |
338 } | 359 } |
339 | 360 |
340 } // namespace media | 361 } // namespace media |
OLD | NEW |