| 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 423 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 476 // |kMillisecondsPerHardwarePacket| worth of audio data. | 471 // |kMillisecondsPerHardwarePacket| worth of audio data. |
| 477 size_t samples = kMinSamplesPerHardwarePacket; | 472 size_t samples = kMinSamplesPerHardwarePacket; |
| 478 while (samples <= kMaxSamplesPerHardwarePacket && | 473 while (samples <= kMaxSamplesPerHardwarePacket && |
| 479 samples * base::Time::kMillisecondsPerSecond < | 474 samples * base::Time::kMillisecondsPerSecond < |
| 480 sample_rate * kMillisecondsPerHardwarePacket) { | 475 sample_rate * kMillisecondsPerHardwarePacket) { |
| 481 samples *= 2; | 476 samples *= 2; |
| 482 } | 477 } |
| 483 return samples; | 478 return samples; |
| 484 } | 479 } |
| 485 | 480 |
| 486 // When transferring data in the shared memory, first word is size of data | |
| 487 // in bytes. Actual data starts immediately after it. | |
| 488 | |
| 489 uint32 TotalSharedMemorySizeInBytes(uint32 packet_size) { | |
| 490 // Need to reserve extra 4 bytes for size of data. | |
| 491 return packet_size + sizeof(Atomic32); | |
| 492 } | |
| 493 | |
| 494 uint32 PacketSizeSizeInBytes(uint32 shared_memory_created_size) { | |
| 495 return shared_memory_created_size - sizeof(Atomic32); | |
| 496 } | |
| 497 | |
| 498 uint32 GetActualDataSizeInBytes(base::SharedMemory* shared_memory, | |
| 499 uint32 shared_memory_size) { | |
| 500 char* ptr = static_cast<char*>(shared_memory->memory()) + shared_memory_size; | |
| 501 DCHECK_EQ(0u, reinterpret_cast<size_t>(ptr) & 3); | |
| 502 | |
| 503 // Actual data size stored at the end of the buffer. | |
| 504 uint32 actual_data_size = | |
| 505 base::subtle::Acquire_Load(reinterpret_cast<volatile Atomic32*>(ptr)); | |
| 506 return std::min(actual_data_size, shared_memory_size); | |
| 507 } | |
| 508 | |
| 509 void SetActualDataSizeInBytes(base::SharedMemory* shared_memory, | |
| 510 uint32 shared_memory_size, | |
| 511 uint32 actual_data_size) { | |
| 512 char* ptr = static_cast<char*>(shared_memory->memory()) + shared_memory_size; | |
| 513 DCHECK_EQ(0u, reinterpret_cast<size_t>(ptr) & 3); | |
| 514 | |
| 515 // Set actual data size at the end of the buffer. | |
| 516 base::subtle::Release_Store(reinterpret_cast<volatile Atomic32*>(ptr), | |
| 517 actual_data_size); | |
| 518 } | |
| 519 | |
| 520 void SetUnknownDataSize(base::SharedMemory* shared_memory, | |
| 521 uint32 shared_memory_size) { | |
| 522 SetActualDataSizeInBytes(shared_memory, shared_memory_size, kUnknownDataSize); | |
| 523 } | |
| 524 | |
| 525 bool IsUnknownDataSize(base::SharedMemory* shared_memory, | |
| 526 uint32 shared_memory_size) { | |
| 527 char* ptr = static_cast<char*>(shared_memory->memory()) + shared_memory_size; | |
| 528 DCHECK_EQ(0u, reinterpret_cast<size_t>(ptr) & 3); | |
| 529 | |
| 530 // Actual data size stored at the end of the buffer. | |
| 531 uint32 actual_data_size = | |
| 532 base::subtle::Acquire_Load(reinterpret_cast<volatile Atomic32*>(ptr)); | |
| 533 return actual_data_size == kUnknownDataSize; | |
| 534 } | |
| 535 | |
| 536 #if defined(OS_WIN) | 481 #if defined(OS_WIN) |
| 537 | 482 |
| 538 bool IsWASAPISupported() { | 483 bool IsWASAPISupported() { |
| 539 // Note: that function correctly returns that Windows Server 2003 does not | 484 // Note: that function correctly returns that Windows Server 2003 does not |
| 540 // support WASAPI. | 485 // support WASAPI. |
| 541 return base::win::GetVersion() >= base::win::VERSION_VISTA; | 486 return base::win::GetVersion() >= base::win::VERSION_VISTA; |
| 542 } | 487 } |
| 543 | 488 |
| 544 int NumberOfWaveOutBuffers() { | 489 int NumberOfWaveOutBuffers() { |
| 545 // Use 4 buffers for Vista, 3 for everyone else: | 490 // Use 4 buffers for Vista, 3 for everyone else: |
| 546 // - The entire Windows audio stack was rewritten for Windows Vista and wave | 491 // - The entire Windows audio stack was rewritten for Windows Vista and wave |
| 547 // out performance was degraded compared to XP. | 492 // out performance was degraded compared to XP. |
| 548 // - The regression was fixed in Windows 7 and most configurations will work | 493 // - The regression was fixed in Windows 7 and most configurations will work |
| 549 // with 2, but some (e.g., some Sound Blasters) still need 3. | 494 // with 2, but some (e.g., some Sound Blasters) still need 3. |
| 550 // - Some XP configurations (even multi-processor ones) also need 3. | 495 // - Some XP configurations (even multi-processor ones) also need 3. |
| 551 return (base::win::GetVersion() == base::win::VERSION_VISTA) ? 4 : 3; | 496 return (base::win::GetVersion() == base::win::VERSION_VISTA) ? 4 : 3; |
| 552 } | 497 } |
| 553 | 498 |
| 554 #endif | 499 #endif |
| 555 | 500 |
| 556 } // namespace media | 501 } // namespace media |
| OLD | NEW |