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