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