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 | |
17 #include <algorithm> | 15 #include <algorithm> |
18 #include <limits> | 16 #include <limits> |
19 | 17 |
| 18 #include "base/atomicops.h" |
20 #include "base/basictypes.h" | 19 #include "base/basictypes.h" |
21 #include "base/logging.h" | 20 #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" |
24 #include "media/base/audio_bus.h" | 25 #include "media/base/audio_bus.h" |
25 | 26 |
26 #if defined(OS_MACOSX) | 27 #if defined(OS_MACOSX) |
27 #include "media/audio/mac/audio_low_latency_input_mac.h" | 28 #include "media/audio/mac/audio_low_latency_input_mac.h" |
28 #include "media/audio/mac/audio_low_latency_output_mac.h" | 29 #include "media/audio/mac/audio_low_latency_output_mac.h" |
29 #elif defined(OS_WIN) | 30 #elif defined(OS_WIN) |
30 #include "base/command_line.h" | 31 #include "base/command_line.h" |
31 #include "base/win/windows_version.h" | 32 #include "base/win/windows_version.h" |
32 #include "media/audio/audio_manager_base.h" | 33 #include "media/audio/audio_manager_base.h" |
33 #include "media/audio/win/audio_low_latency_input_win.h" | 34 #include "media/audio/win/audio_low_latency_input_win.h" |
34 #include "media/audio/win/audio_low_latency_output_win.h" | 35 #include "media/audio/win/audio_low_latency_output_win.h" |
35 #include "media/base/media_switches.h" | 36 #include "media/base/media_switches.h" |
36 #endif | 37 #endif |
37 | 38 |
| 39 using base::subtle::Atomic32; |
| 40 |
| 41 const uint32 kUnknownDataSize = static_cast<uint32>(-1); |
| 42 |
38 namespace media { | 43 namespace media { |
39 | 44 |
40 // TODO(fbarchard): Convert to intrinsics for better efficiency. | 45 // TODO(fbarchard): Convert to intrinsics for better efficiency. |
41 template<class Fixed> | 46 template<class Fixed> |
42 static int ScaleChannel(int channel, int volume) { | 47 static int ScaleChannel(int channel, int volume) { |
43 return static_cast<int>((static_cast<Fixed>(channel) * volume) >> 16); | 48 return static_cast<int>((static_cast<Fixed>(channel) * volume) >> 16); |
44 } | 49 } |
45 | 50 |
46 template<class Format, class Fixed, int bias> | 51 template<class Format, class Fixed, int bias> |
47 static void AdjustVolume(Format* buf_out, | 52 static void AdjustVolume(Format* buf_out, |
(...skipping 423 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
471 // |kMillisecondsPerHardwarePacket| worth of audio data. | 476 // |kMillisecondsPerHardwarePacket| worth of audio data. |
472 size_t samples = kMinSamplesPerHardwarePacket; | 477 size_t samples = kMinSamplesPerHardwarePacket; |
473 while (samples <= kMaxSamplesPerHardwarePacket && | 478 while (samples <= kMaxSamplesPerHardwarePacket && |
474 samples * base::Time::kMillisecondsPerSecond < | 479 samples * base::Time::kMillisecondsPerSecond < |
475 sample_rate * kMillisecondsPerHardwarePacket) { | 480 sample_rate * kMillisecondsPerHardwarePacket) { |
476 samples *= 2; | 481 samples *= 2; |
477 } | 482 } |
478 return samples; | 483 return samples; |
479 } | 484 } |
480 | 485 |
| 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 |
481 #if defined(OS_WIN) | 536 #if defined(OS_WIN) |
482 | 537 |
483 bool IsWASAPISupported() { | 538 bool IsWASAPISupported() { |
484 // Note: that function correctly returns that Windows Server 2003 does not | 539 // Note: that function correctly returns that Windows Server 2003 does not |
485 // support WASAPI. | 540 // support WASAPI. |
486 return base::win::GetVersion() >= base::win::VERSION_VISTA; | 541 return base::win::GetVersion() >= base::win::VERSION_VISTA; |
487 } | 542 } |
488 | 543 |
489 int NumberOfWaveOutBuffers() { | 544 int NumberOfWaveOutBuffers() { |
490 // Use 4 buffers for Vista, 3 for everyone else: | 545 // Use 4 buffers for Vista, 3 for everyone else: |
491 // - The entire Windows audio stack was rewritten for Windows Vista and wave | 546 // - The entire Windows audio stack was rewritten for Windows Vista and wave |
492 // out performance was degraded compared to XP. | 547 // out performance was degraded compared to XP. |
493 // - The regression was fixed in Windows 7 and most configurations will work | 548 // - The regression was fixed in Windows 7 and most configurations will work |
494 // with 2, but some (e.g., some Sound Blasters) still need 3. | 549 // with 2, but some (e.g., some Sound Blasters) still need 3. |
495 // - Some XP configurations (even multi-processor ones) also need 3. | 550 // - Some XP configurations (even multi-processor ones) also need 3. |
496 return (base::win::GetVersion() == base::win::VERSION_VISTA) ? 4 : 3; | 551 return (base::win::GetVersion() == base::win::VERSION_VISTA) ? 4 : 3; |
497 } | 552 } |
498 | 553 |
499 #endif | 554 #endif |
500 | 555 |
501 } // namespace media | 556 } // namespace media |
OLD | NEW |