OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "media/base/audio_latency.h" |
| 6 |
| 7 #include <stdint.h> |
| 8 |
| 9 #include <algorithm> |
| 10 |
| 11 #include "base/logging.h" |
| 12 #include "build/build_config.h" |
| 13 |
| 14 namespace media { |
| 15 |
| 16 namespace { |
| 17 #if !defined(OS_WIN) |
| 18 // Taken from "Bit Twiddling Hacks" |
| 19 // http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2 |
| 20 uint32_t RoundUpToPowerOfTwo(uint32_t v) { |
| 21 v--; |
| 22 v |= v >> 1; |
| 23 v |= v >> 2; |
| 24 v |= v >> 4; |
| 25 v |= v >> 8; |
| 26 v |= v >> 16; |
| 27 v++; |
| 28 return v; |
| 29 } |
| 30 #endif |
| 31 } // namespace |
| 32 |
| 33 // static |
| 34 int AudioLatency::GetHighLatencyBufferSize(int sample_rate, |
| 35 int preferred_buffer_size) { |
| 36 // Empirically, we consider 20ms of samples to be high latency. |
| 37 const double twenty_ms_size = 2.0 * sample_rate / 100; |
| 38 |
| 39 #if defined(OS_WIN) |
| 40 preferred_buffer_size = std::max(preferred_buffer_size, 1); |
| 41 |
| 42 // Windows doesn't use power of two buffer sizes, so we should always round up |
| 43 // to the nearest multiple of the output buffer size. |
| 44 const int high_latency_buffer_size = |
| 45 std::ceil(twenty_ms_size / preferred_buffer_size) * preferred_buffer_size; |
| 46 #else |
| 47 // On other platforms use the nearest higher power of two buffer size. For a |
| 48 // given sample rate, this works out to: |
| 49 // |
| 50 // <= 3200 : 64 |
| 51 // <= 6400 : 128 |
| 52 // <= 12800 : 256 |
| 53 // <= 25600 : 512 |
| 54 // <= 51200 : 1024 |
| 55 // <= 102400 : 2048 |
| 56 // <= 204800 : 4096 |
| 57 // |
| 58 // On Linux, the minimum hardware buffer size is 512, so the lower calculated |
| 59 // values are unused. OSX may have a value as low as 128. |
| 60 const int high_latency_buffer_size = RoundUpToPowerOfTwo(twenty_ms_size); |
| 61 #endif // defined(OS_WIN) |
| 62 |
| 63 #if defined(OS_CHROMEOS) |
| 64 return high_latency_buffer_size; // No preference. |
| 65 #else |
| 66 return std::max(preferred_buffer_size, high_latency_buffer_size); |
| 67 #endif // defined(OS_CHROMEOS) |
| 68 } |
| 69 |
| 70 // static |
| 71 int AudioLatency::GetRtcBufferSize(int sample_rate, int hardware_buffer_size) { |
| 72 // Use native hardware buffer size as default. On Windows, we strive to open |
| 73 // up using this native hardware buffer size to achieve best |
| 74 // possible performance and to ensure that no FIFO is needed on the browser |
| 75 // side to match the client request. That is why there is no #if case for |
| 76 // Windows below. |
| 77 int frames_per_buffer = hardware_buffer_size; |
| 78 |
| 79 // No |hardware_buffer_size| is specified, fall back to 10 ms buffer size. |
| 80 if (!frames_per_buffer) { |
| 81 frames_per_buffer = sample_rate / 100; |
| 82 DVLOG(1) << "Using 10 ms sink output buffer size: " << frames_per_buffer; |
| 83 return frames_per_buffer; |
| 84 } |
| 85 |
| 86 #if defined(OS_LINUX) || defined(OS_MACOSX) |
| 87 // On Linux and MacOS, the low level IO implementations on the browser side |
| 88 // supports all buffer size the clients want. We use the native peer |
| 89 // connection buffer size (10ms) to achieve best possible performance. |
| 90 frames_per_buffer = sample_rate / 100; |
| 91 #elif defined(OS_ANDROID) |
| 92 // TODO(olka/henrika): This settings are very old, need to be revisited. |
| 93 int frames_per_10ms = sample_rate / 100; |
| 94 if (frames_per_buffer < 2 * frames_per_10ms) { |
| 95 // Examples of low-latency frame sizes and the resulting |buffer_size|: |
| 96 // Nexus 7 : 240 audio frames => 2*480 = 960 |
| 97 // Nexus 10 : 256 => 2*441 = 882 |
| 98 // Galaxy Nexus: 144 => 2*441 = 882 |
| 99 frames_per_buffer = 2 * frames_per_10ms; |
| 100 DVLOG(1) << "Low-latency output detected on Android"; |
| 101 } |
| 102 #endif |
| 103 |
| 104 DVLOG(1) << "Using sink output buffer size: " << frames_per_buffer; |
| 105 return frames_per_buffer; |
| 106 } |
| 107 |
| 108 // static |
| 109 int AudioLatency::GetInteractiveBufferSize(int hardware_buffer_size) { |
| 110 #if defined(OS_ANDROID) |
| 111 // The optimum low-latency hardware buffer size is usually too small on |
| 112 // Android for WebAudio to render without glitching. So, if it is small, use |
| 113 // a larger size. |
| 114 // |
| 115 // Since WebAudio renders in 128-frame blocks, the small buffer sizes (144 for |
| 116 // a Galaxy Nexus), cause significant processing jitter. Sometimes multiple |
| 117 // blocks will processed, but other times will not be since the WebAudio can't |
| 118 // satisfy the request. By using a larger render buffer size, we smooth out |
| 119 // the jitter. |
| 120 const int kSmallBufferSize = 1024; |
| 121 const int kDefaultCallbackBufferSize = 2048; |
| 122 if (hardware_buffer_size <= kSmallBufferSize) |
| 123 return kDefaultCallbackBufferSize; |
| 124 #endif |
| 125 |
| 126 return hardware_buffer_size; |
| 127 } |
| 128 |
| 129 } // namespace media |
OLD | NEW |