| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 #include "media/base/audio_latency.h" | 5 #include "media/base/audio_latency.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 | 10 |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/time/time.h" | 12 #include "base/time/time.h" |
| 13 #include "build/build_config.h" | 13 #include "build/build_config.h" |
| 14 #include "media/base/limits.h" |
| 14 | 15 |
| 15 namespace media { | 16 namespace media { |
| 16 | 17 |
| 17 namespace { | 18 namespace { |
| 18 #if !defined(OS_WIN) | 19 #if !defined(OS_WIN) |
| 19 // Taken from "Bit Twiddling Hacks" | 20 // Taken from "Bit Twiddling Hacks" |
| 20 // http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2 | 21 // http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2 |
| 21 uint32_t RoundUpToPowerOfTwo(uint32_t v) { | 22 uint32_t RoundUpToPowerOfTwo(uint32_t v) { |
| 22 v--; | 23 v--; |
| 23 v |= v >> 1; | 24 v |= v >> 1; |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 #endif | 126 #endif |
| 126 | 127 |
| 127 return hardware_buffer_size; | 128 return hardware_buffer_size; |
| 128 } | 129 } |
| 129 | 130 |
| 130 int AudioLatency::GetExactBufferSize(base::TimeDelta duration, | 131 int AudioLatency::GetExactBufferSize(base::TimeDelta duration, |
| 131 int sample_rate, | 132 int sample_rate, |
| 132 int hardware_buffer_size) { | 133 int hardware_buffer_size) { |
| 133 const double requested_buffer_size = duration.InSecondsF() * sample_rate; | 134 const double requested_buffer_size = duration.InSecondsF() * sample_rate; |
| 134 | 135 |
| 136 // On OSX the preferred buffer size is larger than the minimum, however we |
| 137 // allow values down to the minimum if requested explicitly. |
| 138 #if defined(OS_MACOSX) |
| 139 hardware_buffer_size = limits::kMinimumOutputBufferSize; |
| 140 #endif |
| 141 |
| 135 DCHECK_NE(0, hardware_buffer_size); | 142 DCHECK_NE(0, hardware_buffer_size); |
| 136 | 143 |
| 137 // Round the requested size to the nearest multiple of the hardware size | 144 // Round the requested size to the nearest multiple of the hardware size |
| 138 const int buffer_size = | 145 const int buffer_size = |
| 139 std::round(std::max(requested_buffer_size, 1.0) / hardware_buffer_size) * | 146 std::round(std::max(requested_buffer_size, 1.0) / hardware_buffer_size) * |
| 140 hardware_buffer_size; | 147 hardware_buffer_size; |
| 141 | 148 |
| 142 return std::max(buffer_size, hardware_buffer_size); | 149 return std::min(static_cast<int>(media::limits::kMaximumOutputBufferSize), |
| 150 std::max(buffer_size, hardware_buffer_size)); |
| 143 } | 151 } |
| 144 | 152 |
| 145 } // namespace media | 153 } // namespace media |
| OLD | NEW |