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

Side by Side 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, 8 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 unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "media/base/audio_latency.h" 5 #include "media/base/audio_latency.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 8
9 #include <algorithm> 9 #include <algorithm>
10 10
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "build/build_config.h" 12 #include "build/build_config.h"
13 #include "media/base/audio_parameters.h"
13 14
14 namespace media { 15 namespace media {
15 16
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 17 // static
34 int AudioLatency::GetHighLatencyBufferSize(int sample_rate, 18 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
35 int preferred_buffer_size) { 19 int preferred_buffer_size) {
36 // Empirically, we consider 20ms of samples to be high latency. 20 // Empirically, we consider 20ms of samples to be high latency.
37 const double twenty_ms_size = 2.0 * sample_rate / 100; 21 const double twenty_ms_size = 2.0 * sample_rate / 100;
38 22
39 #if defined(OS_WIN)
40 preferred_buffer_size = std::max(preferred_buffer_size, 1); 23 preferred_buffer_size = std::max(preferred_buffer_size, 1);
41 24
42 // Windows doesn't use power of two buffer sizes, so we should always round up 25 // Round up to the nearest multiple of the output buffer size.
43 // to the nearest multiple of the output buffer size.
44 const int high_latency_buffer_size = 26 const int high_latency_buffer_size =
45 std::ceil(twenty_ms_size / preferred_buffer_size) * preferred_buffer_size; 27 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 28
63 #if defined(OS_CHROMEOS) 29 #if defined(OS_CHROMEOS)
DaleCurtis 2017/03/30 18:53:47 Is this necessary now? We'll always be at least 1
64 return high_latency_buffer_size; // No preference. 30 return high_latency_buffer_size; // No preference.
65 #else 31 #else
66 return std::max(preferred_buffer_size, high_latency_buffer_size); 32 return std::max(preferred_buffer_size, high_latency_buffer_size);
67 #endif // defined(OS_CHROMEOS) 33 #endif // defined(OS_CHROMEOS)
68 } 34 }
69 35
70 // static 36 // static
71 int AudioLatency::GetRtcBufferSize(int sample_rate, int hardware_buffer_size) { 37 int AudioLatency::GetRtcBufferSize(int sample_rate, int hardware_buffer_size) {
72 // Use native hardware buffer size as default. On Windows, we strive to open 38 // Use native hardware buffer size as default. On Windows, we strive to open
73 // up using this native hardware buffer size to achieve best 39 // up using this native hardware buffer size to achieve best
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
119 // the jitter. 85 // the jitter.
120 const int kSmallBufferSize = 1024; 86 const int kSmallBufferSize = 1024;
121 const int kDefaultCallbackBufferSize = 2048; 87 const int kDefaultCallbackBufferSize = 2048;
122 if (hardware_buffer_size <= kSmallBufferSize) 88 if (hardware_buffer_size <= kSmallBufferSize)
123 return kDefaultCallbackBufferSize; 89 return kDefaultCallbackBufferSize;
124 #endif 90 #endif
125 91
126 return hardware_buffer_size; 92 return hardware_buffer_size;
127 } 93 }
128 94
95 int AudioLatency::GetExactBufferSize(base::TimeDelta duration,
96 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.
97 const int hardware_buffer_size = hardware_params.frames_per_buffer();
98 const double requested_buffer_size =
99 duration.InSecondsF() * hardware_params.sample_rate();
100
101 // Round the requested size to the nearest multiple of the hardware size
102 const int buffer_size =
103 std::round(std::max(requested_buffer_size, 1.0) / hardware_buffer_size) *
104 hardware_buffer_size;
105
106 const double twenty_ms_size = 2.0 * hardware_params.sample_rate() / 100;
107 const int max_buffer_size =
108 std::ceil(twenty_ms_size / hardware_buffer_size) * hardware_buffer_size;
109
110 return std::max(std::min(buffer_size, max_buffer_size), hardware_buffer_size);
111 }
112
129 } // namespace media 113 } // namespace media
OLDNEW
« 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