Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(648)

Unified Diff: media/base/audio_latency.cc

Issue 2750543003: Support AudioContextOptions latencyHint as double. (Closed)
Patch Set: Fix buffer size calculations based on feedback. Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « media/base/audio_latency.h ('k') | third_party/WebKit/LayoutTests/webaudio/audiocontextoptions.html » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: media/base/audio_latency.cc
diff --git a/media/base/audio_latency.cc b/media/base/audio_latency.cc
index 5d37edf415213ff72aeb6e9753927abf1dfb8894..fe6c395a269ad8383cd130ddc1b37e0b16b867b1 100644
--- a/media/base/audio_latency.cc
+++ b/media/base/audio_latency.cc
@@ -10,55 +10,21 @@
#include "base/logging.h"
#include "build/build_config.h"
+#include "media/base/audio_parameters.h"
namespace media {
-namespace {
-#if !defined(OS_WIN)
-// Taken from "Bit Twiddling Hacks"
-// http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
-uint32_t RoundUpToPowerOfTwo(uint32_t v) {
- v--;
- v |= v >> 1;
- v |= v >> 2;
- v |= v >> 4;
- v |= v >> 8;
- v |= v >> 16;
- v++;
- return v;
-}
-#endif
-} // namespace
-
// static
int AudioLatency::GetHighLatencyBufferSize(int sample_rate,
DaleCurtis 2017/03/30 18:53:47 I wasn't proposing you make this change in this CL
Andrew MacPherson 2017/03/31 12:31:01 Sorry, my mistake, I wasn't sure if you were sugge
int preferred_buffer_size) {
// Empirically, we consider 20ms of samples to be high latency.
const double twenty_ms_size = 2.0 * sample_rate / 100;
-#if defined(OS_WIN)
preferred_buffer_size = std::max(preferred_buffer_size, 1);
- // Windows doesn't use power of two buffer sizes, so we should always round up
- // to the nearest multiple of the output buffer size.
+ // Round up to the nearest multiple of the output buffer size.
const int high_latency_buffer_size =
std::ceil(twenty_ms_size / preferred_buffer_size) * preferred_buffer_size;
-#else
- // On other platforms use the nearest higher power of two buffer size. For a
- // given sample rate, this works out to:
- //
- // <= 3200 : 64
- // <= 6400 : 128
- // <= 12800 : 256
- // <= 25600 : 512
- // <= 51200 : 1024
- // <= 102400 : 2048
- // <= 204800 : 4096
- //
- // On Linux, the minimum hardware buffer size is 512, so the lower calculated
- // values are unused. OSX may have a value as low as 128.
- const int high_latency_buffer_size = RoundUpToPowerOfTwo(twenty_ms_size);
-#endif // defined(OS_WIN)
#if defined(OS_CHROMEOS)
DaleCurtis 2017/03/30 18:53:47 Is this necessary now? We'll always be at least 1
return high_latency_buffer_size; // No preference.
@@ -126,4 +92,22 @@ int AudioLatency::GetInteractiveBufferSize(int hardware_buffer_size) {
return hardware_buffer_size;
}
+int AudioLatency::GetExactBufferSize(base::TimeDelta duration,
+ const AudioParameters& hardware_params) {
DaleCurtis 2017/03/30 18:53:47 Should probably just take int sample_rate, int fra
Andrew MacPherson 2017/03/31 12:31:01 Done.
+ const int hardware_buffer_size = hardware_params.frames_per_buffer();
+ const double requested_buffer_size =
+ duration.InSecondsF() * hardware_params.sample_rate();
+
+ // Round the requested size to the nearest multiple of the hardware size
+ const int buffer_size =
+ std::round(std::max(requested_buffer_size, 1.0) / hardware_buffer_size) *
+ hardware_buffer_size;
+
+ const double twenty_ms_size = 2.0 * hardware_params.sample_rate() / 100;
+ const int max_buffer_size =
+ std::ceil(twenty_ms_size / hardware_buffer_size) * hardware_buffer_size;
+
+ return std::max(std::min(buffer_size, max_buffer_size), hardware_buffer_size);
+}
+
} // namespace media
« no previous file with comments | « media/base/audio_latency.h ('k') | third_party/WebKit/LayoutTests/webaudio/audiocontextoptions.html » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698