diff options
Diffstat (limited to 'chromium/media/base/audio_latency.cc')
-rw-r--r-- | chromium/media/base/audio_latency.cc | 39 |
1 files changed, 37 insertions, 2 deletions
diff --git a/chromium/media/base/audio_latency.cc b/chromium/media/base/audio_latency.cc index b55a2da37e1..f15e8f95ba5 100644 --- a/chromium/media/base/audio_latency.cc +++ b/chromium/media/base/audio_latency.cc @@ -25,6 +25,7 @@ namespace media { namespace { + #if !defined(OS_WIN) // Taken from "Bit Twiddling Hacks" // http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2 @@ -39,11 +40,30 @@ uint32_t RoundUpToPowerOfTwo(uint32_t v) { return v; } #endif + +#if defined(OS_ANDROID) +// WebAudio renderer's quantum size (frames per callback) that is used for +// calculating the "interactive" buffer size. +// TODO(crbug.com/988121): This number needs to be passed down from Blink when +// user-selectable render quantum size is implemented. +const int kWebAudioRenderQuantumSize = 128; + +// From media/renderers/paint_canvas_video_renderer.cc. To calculate the optimum +// buffer size for Pixel 3/4/5 devices, which has a HW buffer size of 96 frames. +int GCD(int a, int b) { + return a == 0 ? b : GCD(b % a, a); +} + +int LCM(int a, int b) { + return a / GCD(a, b) * b; +} +#endif + } // namespace // static bool AudioLatency::IsResamplingPassthroughSupported(LatencyType type) { -#if BUILDFLAG(IS_ASH) +#if BUILDFLAG(IS_CHROMEOS_ASH) return true; #elif defined(OS_ANDROID) // Only N MR1+ has support for OpenSLES performance modes which allow for @@ -139,13 +159,28 @@ int AudioLatency::GetRtcBufferSize(int sample_rate, int hardware_buffer_size) { // static int AudioLatency::GetInteractiveBufferSize(int hardware_buffer_size) { + CHECK_GT(hardware_buffer_size, 0); + #if defined(OS_ANDROID) // Always log this because it's relatively hard to get this // information out. LOG(INFO) << "audioHardwareBufferSize = " << hardware_buffer_size; -#endif + if (hardware_buffer_size >= kWebAudioRenderQuantumSize) + return hardware_buffer_size; + + // HW buffer size is smaller than the Web Audio's render quantum size, so + // compute LCM to avoid glitches and regulate the workload per callback. + // (e.g. 96 vs 128 -> 384) Also cap the buffer size to 4 render quanta + // (512 frames ~= 10ms at 48K) if LCM goes beyond interactive latency range. + int sensible_buffer_size = std::min( + LCM(hardware_buffer_size, kWebAudioRenderQuantumSize), + kWebAudioRenderQuantumSize * 4); + + return sensible_buffer_size; +#else return hardware_buffer_size; +#endif // defined(OS_ANDROID) } int AudioLatency::GetExactBufferSize(base::TimeDelta duration, |