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 #include "media/audio/audio_util.h" | 17 #include "media/audio/audio_util.h" |
18 #if defined(OS_MACOSX) | 18 #if defined(OS_MACOSX) |
19 #include "media/audio/mac/audio_low_latency_output_mac.h" | 19 #include "media/audio/mac/audio_low_latency_output_mac.h" |
20 #endif | 20 #endif |
21 | 21 |
22 using base::subtle::Atomic32; | 22 using base::subtle::Atomic32; |
23 | 23 |
| 24 const uint32 kUnknownDataSize = static_cast<uint32>(-1); |
| 25 |
24 namespace media { | 26 namespace media { |
25 | 27 |
26 // TODO(fbarchard): Convert to intrinsics for better efficiency. | 28 // TODO(fbarchard): Convert to intrinsics for better efficiency. |
27 template<class Fixed> | 29 template<class Fixed> |
28 static int ScaleChannel(int channel, int volume) { | 30 static int ScaleChannel(int channel, int volume) { |
29 return static_cast<int>((static_cast<Fixed>(channel) * volume) >> 16); | 31 return static_cast<int>((static_cast<Fixed>(channel) * volume) >> 16); |
30 } | 32 } |
31 | 33 |
32 template<class Format, class Fixed, int bias> | 34 template<class Format, class Fixed, int bias> |
33 static void AdjustVolume(Format* buf_out, | 35 static void AdjustVolume(Format* buf_out, |
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
250 | 252 |
251 uint32 PacketSizeSizeInBytes(uint32 shared_memory_created_size) { | 253 uint32 PacketSizeSizeInBytes(uint32 shared_memory_created_size) { |
252 return shared_memory_created_size - sizeof(Atomic32); | 254 return shared_memory_created_size - sizeof(Atomic32); |
253 } | 255 } |
254 | 256 |
255 uint32 GetActualDataSizeInBytes(base::SharedMemory* shared_memory, | 257 uint32 GetActualDataSizeInBytes(base::SharedMemory* shared_memory, |
256 uint32 shared_memory_size) { | 258 uint32 shared_memory_size) { |
257 char* ptr = static_cast<char*>(shared_memory->memory()) + shared_memory_size; | 259 char* ptr = static_cast<char*>(shared_memory->memory()) + shared_memory_size; |
258 DCHECK_EQ(0u, reinterpret_cast<size_t>(ptr) & 3); | 260 DCHECK_EQ(0u, reinterpret_cast<size_t>(ptr) & 3); |
259 | 261 |
260 // Actual data size stored in the beginning of the buffer. | 262 // Actual data size stored at the end of the buffer. |
261 uint32 actual_data_size = | 263 uint32 actual_data_size = |
262 base::subtle::Acquire_Load(reinterpret_cast<volatile Atomic32*>(ptr)); | 264 base::subtle::Acquire_Load(reinterpret_cast<volatile Atomic32*>(ptr)); |
263 return std::min(actual_data_size, shared_memory_size); | 265 return std::min(actual_data_size, shared_memory_size); |
264 } | 266 } |
265 | 267 |
266 void SetActualDataSizeInBytes(base::SharedMemory* shared_memory, | 268 void SetActualDataSizeInBytes(base::SharedMemory* shared_memory, |
267 uint32 shared_memory_size, | 269 uint32 shared_memory_size, |
268 uint32 actual_data_size) { | 270 uint32 actual_data_size) { |
269 char* ptr = static_cast<char*>(shared_memory->memory()) + shared_memory_size; | 271 char* ptr = static_cast<char*>(shared_memory->memory()) + shared_memory_size; |
270 DCHECK_EQ(0u, reinterpret_cast<size_t>(ptr) & 3); | 272 DCHECK_EQ(0u, reinterpret_cast<size_t>(ptr) & 3); |
271 | 273 |
272 // Set actual data size in the beginning of the buffer. | 274 // Set actual data size at the end of the buffer. |
273 base::subtle::Release_Store(reinterpret_cast<volatile Atomic32*>(ptr), | 275 base::subtle::Release_Store(reinterpret_cast<volatile Atomic32*>(ptr), |
274 actual_data_size); | 276 actual_data_size); |
275 } | 277 } |
276 | 278 |
| 279 void SetUnknownDataSize(base::SharedMemory* shared_memory, |
| 280 uint32 shared_memory_size) { |
| 281 SetActualDataSizeInBytes(shared_memory, shared_memory_size, kUnknownDataSize); |
| 282 } |
| 283 |
| 284 bool IsUnknownDataSize(base::SharedMemory* shared_memory, |
| 285 uint32 shared_memory_size) { |
| 286 char* ptr = static_cast<char*>(shared_memory->memory()) + shared_memory_size; |
| 287 DCHECK_EQ(0u, reinterpret_cast<size_t>(ptr) & 3); |
| 288 |
| 289 // Actual data size stored at the end of the buffer. |
| 290 uint32 actual_data_size = |
| 291 base::subtle::Acquire_Load(reinterpret_cast<volatile Atomic32*>(ptr)); |
| 292 return actual_data_size == kUnknownDataSize; |
| 293 } |
| 294 |
277 } // namespace media | 295 } // namespace media |
OLD | NEW |