| 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 // 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 // TODO(vrk): This file has been running pretty wild and free, and it's likely | 11 // TODO(vrk): This file has been running pretty wild and free, and it's likely |
| 12 // that a lot of the functions can be simplified and made more elegant. Revisit | 12 // that a lot of the functions can be simplified and made more elegant. Revisit |
| 13 // after other audio cleanup is done. (crbug.com/120319) | 13 // after other audio cleanup is done. (crbug.com/120319) |
| 14 | 14 |
| 15 #include "media/audio/audio_util.h" |
| 16 |
| 15 #include <algorithm> | 17 #include <algorithm> |
| 16 #include <limits> | 18 #include <limits> |
| 17 | 19 |
| 18 #include "base/atomicops.h" | |
| 19 #include "base/basictypes.h" | 20 #include "base/basictypes.h" |
| 20 #include "base/logging.h" | 21 #include "base/logging.h" |
| 21 #include "base/shared_memory.h" | |
| 22 #include "base/time.h" | 22 #include "base/time.h" |
| 23 #include "media/audio/audio_parameters.h" | 23 #include "media/audio/audio_parameters.h" |
| 24 #include "media/audio/audio_util.h" | |
| 25 #include "media/base/audio_bus.h" | 24 #include "media/base/audio_bus.h" |
| 26 | 25 |
| 27 #if defined(OS_MACOSX) | 26 #if defined(OS_MACOSX) |
| 28 #include "media/audio/mac/audio_low_latency_input_mac.h" | 27 #include "media/audio/mac/audio_low_latency_input_mac.h" |
| 29 #include "media/audio/mac/audio_low_latency_output_mac.h" | 28 #include "media/audio/mac/audio_low_latency_output_mac.h" |
| 30 #elif defined(OS_WIN) | 29 #elif defined(OS_WIN) |
| 31 #include "base/command_line.h" | 30 #include "base/command_line.h" |
| 32 #include "base/win/windows_version.h" | 31 #include "base/win/windows_version.h" |
| 33 #include "media/audio/audio_manager_base.h" | 32 #include "media/audio/audio_manager_base.h" |
| 34 #include "media/audio/win/audio_low_latency_input_win.h" | 33 #include "media/audio/win/audio_low_latency_input_win.h" |
| 35 #include "media/audio/win/audio_low_latency_output_win.h" | 34 #include "media/audio/win/audio_low_latency_output_win.h" |
| 36 #include "media/base/media_switches.h" | 35 #include "media/base/media_switches.h" |
| 37 #endif | 36 #endif |
| 38 | 37 |
| 39 using base::subtle::Atomic32; | |
| 40 | |
| 41 const uint32 kUnknownDataSize = static_cast<uint32>(-1); | |
| 42 | |
| 43 namespace media { | 38 namespace media { |
| 44 | 39 |
| 45 // TODO(fbarchard): Convert to intrinsics for better efficiency. | 40 // TODO(fbarchard): Convert to intrinsics for better efficiency. |
| 46 template<class Fixed> | 41 template<class Fixed> |
| 47 static int ScaleChannel(int channel, int volume) { | 42 static int ScaleChannel(int channel, int volume) { |
| 48 return static_cast<int>((static_cast<Fixed>(channel) * volume) >> 16); | 43 return static_cast<int>((static_cast<Fixed>(channel) * volume) >> 16); |
| 49 } | 44 } |
| 50 | 45 |
| 51 template<class Format, class Fixed, int bias> | 46 template<class Format, class Fixed, int bias> |
| 52 static void AdjustVolume(Format* buf_out, | 47 static void AdjustVolume(Format* buf_out, |
| (...skipping 377 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 430 // |kMillisecondsPerHardwarePacket| worth of audio data. | 425 // |kMillisecondsPerHardwarePacket| worth of audio data. |
| 431 size_t samples = kMinSamplesPerHardwarePacket; | 426 size_t samples = kMinSamplesPerHardwarePacket; |
| 432 while (samples <= kMaxSamplesPerHardwarePacket && | 427 while (samples <= kMaxSamplesPerHardwarePacket && |
| 433 samples * base::Time::kMillisecondsPerSecond < | 428 samples * base::Time::kMillisecondsPerSecond < |
| 434 sample_rate * kMillisecondsPerHardwarePacket) { | 429 sample_rate * kMillisecondsPerHardwarePacket) { |
| 435 samples *= 2; | 430 samples *= 2; |
| 436 } | 431 } |
| 437 return samples; | 432 return samples; |
| 438 } | 433 } |
| 439 | 434 |
| 440 // When transferring data in the shared memory, first word is size of data | |
| 441 // in bytes. Actual data starts immediately after it. | |
| 442 | |
| 443 uint32 TotalSharedMemorySizeInBytes(uint32 packet_size) { | |
| 444 // Need to reserve extra 4 bytes for size of data. | |
| 445 return packet_size + sizeof(Atomic32); | |
| 446 } | |
| 447 | |
| 448 uint32 PacketSizeSizeInBytes(uint32 shared_memory_created_size) { | |
| 449 return shared_memory_created_size - sizeof(Atomic32); | |
| 450 } | |
| 451 | |
| 452 uint32 GetActualDataSizeInBytes(base::SharedMemory* shared_memory, | |
| 453 uint32 shared_memory_size) { | |
| 454 char* ptr = static_cast<char*>(shared_memory->memory()) + shared_memory_size; | |
| 455 DCHECK_EQ(0u, reinterpret_cast<size_t>(ptr) & 3); | |
| 456 | |
| 457 // Actual data size stored at the end of the buffer. | |
| 458 uint32 actual_data_size = | |
| 459 base::subtle::Acquire_Load(reinterpret_cast<volatile Atomic32*>(ptr)); | |
| 460 return std::min(actual_data_size, shared_memory_size); | |
| 461 } | |
| 462 | |
| 463 void SetActualDataSizeInBytes(base::SharedMemory* shared_memory, | |
| 464 uint32 shared_memory_size, | |
| 465 uint32 actual_data_size) { | |
| 466 char* ptr = static_cast<char*>(shared_memory->memory()) + shared_memory_size; | |
| 467 DCHECK_EQ(0u, reinterpret_cast<size_t>(ptr) & 3); | |
| 468 | |
| 469 // Set actual data size at the end of the buffer. | |
| 470 base::subtle::Release_Store(reinterpret_cast<volatile Atomic32*>(ptr), | |
| 471 actual_data_size); | |
| 472 } | |
| 473 | |
| 474 void SetUnknownDataSize(base::SharedMemory* shared_memory, | |
| 475 uint32 shared_memory_size) { | |
| 476 SetActualDataSizeInBytes(shared_memory, shared_memory_size, kUnknownDataSize); | |
| 477 } | |
| 478 | |
| 479 bool IsUnknownDataSize(base::SharedMemory* shared_memory, | |
| 480 uint32 shared_memory_size) { | |
| 481 char* ptr = static_cast<char*>(shared_memory->memory()) + shared_memory_size; | |
| 482 DCHECK_EQ(0u, reinterpret_cast<size_t>(ptr) & 3); | |
| 483 | |
| 484 // Actual data size stored at the end of the buffer. | |
| 485 uint32 actual_data_size = | |
| 486 base::subtle::Acquire_Load(reinterpret_cast<volatile Atomic32*>(ptr)); | |
| 487 return actual_data_size == kUnknownDataSize; | |
| 488 } | |
| 489 | |
| 490 #if defined(OS_WIN) | 435 #if defined(OS_WIN) |
| 491 | 436 |
| 492 bool IsWASAPISupported() { | 437 bool IsWASAPISupported() { |
| 493 // Note: that function correctly returns that Windows Server 2003 does not | 438 // Note: that function correctly returns that Windows Server 2003 does not |
| 494 // support WASAPI. | 439 // support WASAPI. |
| 495 return base::win::GetVersion() >= base::win::VERSION_VISTA; | 440 return base::win::GetVersion() >= base::win::VERSION_VISTA; |
| 496 } | 441 } |
| 497 | 442 |
| 498 int NumberOfWaveOutBuffers() { | 443 int NumberOfWaveOutBuffers() { |
| 499 // Use 4 buffers for Vista, 3 for everyone else: | 444 // Use 4 buffers for Vista, 3 for everyone else: |
| 500 // - The entire Windows audio stack was rewritten for Windows Vista and wave | 445 // - The entire Windows audio stack was rewritten for Windows Vista and wave |
| 501 // out performance was degraded compared to XP. | 446 // out performance was degraded compared to XP. |
| 502 // - The regression was fixed in Windows 7 and most configurations will work | 447 // - The regression was fixed in Windows 7 and most configurations will work |
| 503 // with 2, but some (e.g., some Sound Blasters) still need 3. | 448 // with 2, but some (e.g., some Sound Blasters) still need 3. |
| 504 // - Some XP configurations (even multi-processor ones) also need 3. | 449 // - Some XP configurations (even multi-processor ones) also need 3. |
| 505 return (base::win::GetVersion() == base::win::VERSION_VISTA) ? 4 : 3; | 450 return (base::win::GetVersion() == base::win::VERSION_VISTA) ? 4 : 3; |
| 506 } | 451 } |
| 507 | 452 |
| 508 #endif | 453 #endif |
| 509 | 454 |
| 510 } // namespace media | 455 } // namespace media |
| OLD | NEW |