1 // Copyright (c) 2012 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.
5 #include "build/build_config.h"
6 #include "ppapi/shared_impl/ppb_audio_config_shared.h"
7 #include "ppapi/thunk/enter.h"
8 #include "ppapi/thunk/ppb_instance_api.h"
12 // Rounds up requested_size to the nearest multiple of minimum_size.
13 static uint32_t CalculateMultipleOfSampleFrameCount(uint32_t minimum_size,
14 uint32_t requested_size) {
15 const uint32_t multiple = (requested_size + minimum_size - 1) / minimum_size;
16 return std::min(minimum_size * multiple,
17 static_cast<uint32_t>(PP_AUDIOMAXSAMPLEFRAMECOUNT));
20 PPB_AudioConfig_Shared::PPB_AudioConfig_Shared(ResourceObjectType type,
22 : Resource(type, instance),
23 sample_rate_(PP_AUDIOSAMPLERATE_NONE),
24 sample_frame_count_(0) {
27 PPB_AudioConfig_Shared::~PPB_AudioConfig_Shared() {
30 PP_Resource PPB_AudioConfig_Shared::Create(
31 ResourceObjectType type,
33 PP_AudioSampleRate sample_rate,
34 uint32_t sample_frame_count) {
35 scoped_refptr<PPB_AudioConfig_Shared> object(
36 new PPB_AudioConfig_Shared(type, instance));
37 if (!object->Init(sample_rate, sample_frame_count))
39 return object->GetReference();
43 uint32_t PPB_AudioConfig_Shared::RecommendSampleFrameCount_1_0(
44 PP_AudioSampleRate sample_rate,
45 uint32_t requested_sample_frame_count) {
46 // Version 1.0: Don't actually query to get a value from the
47 // hardware; instead return the input for in-range values.
48 if (requested_sample_frame_count < PP_AUDIOMINSAMPLEFRAMECOUNT)
49 return PP_AUDIOMINSAMPLEFRAMECOUNT;
50 if (requested_sample_frame_count > PP_AUDIOMAXSAMPLEFRAMECOUNT)
51 return PP_AUDIOMAXSAMPLEFRAMECOUNT;
52 return requested_sample_frame_count;
56 uint32_t PPB_AudioConfig_Shared::RecommendSampleFrameCount_1_1(
58 PP_AudioSampleRate sample_rate,
59 uint32_t sample_frame_count) {
60 // Version 1.1: Query the back-end hardware for sample rate and buffer size,
61 // and recommend a best fit based on request.
62 thunk::EnterInstanceNoLock enter(instance);
66 // Get the hardware config.
67 PP_AudioSampleRate hardware_sample_rate = static_cast<PP_AudioSampleRate>(
68 enter.functions()->GetAudioHardwareOutputSampleRate(instance));
69 uint32_t hardware_sample_frame_count =
70 enter.functions()->GetAudioHardwareOutputBufferSize(instance);
71 if (sample_frame_count < PP_AUDIOMINSAMPLEFRAMECOUNT)
72 sample_frame_count = PP_AUDIOMINSAMPLEFRAMECOUNT;
74 // If hardware information isn't available we're connected to a fake audio
75 // output stream on the browser side, so we can use whatever sample count the
77 if (!hardware_sample_frame_count || !hardware_sample_rate)
78 return sample_frame_count;
80 // Note: All the values below were determined through experimentation to
81 // minimize jitter and back-to-back callbacks from the browser. Please take
82 // care when modifying these values as they impact a large number of users.
83 // TODO(dalecurtis): Land jitter test and add documentation for updating this.
85 // Should track the value reported by XP and ALSA backends.
86 const uint32_t kHighLatencySampleFrameCount = 2048;
87 #if defined(OS_CHROMEOS) && defined(ARCH_CPU_ARM_FAMILY)
88 // TODO(ihf): Remove this once ARM Chromebooks support low latency audio. For
89 // now we classify them as high latency. See crbug.com/289770. Note that
90 // Adobe Flash is affected but not HTML5, WebRTC and WebAudio (they are using
91 // real time threads).
92 const bool kHighLatencyDevice = true;
94 const bool kHighLatencyDevice = false;
97 // If client is using same sample rate as audio hardware, then recommend a
98 // multiple of the audio hardware's sample frame count.
99 if (!kHighLatencyDevice && hardware_sample_rate == sample_rate) {
100 return CalculateMultipleOfSampleFrameCount(
101 hardware_sample_frame_count, sample_frame_count);
104 // If the hardware requires a high latency buffer or we're at a low sample
105 // rate w/ a buffer that's larger than 10ms, choose the nearest multiple of
106 // the high latency sample frame count. An example of too low and too large
107 // is 16kHz and a sample frame count greater than 160 frames.
108 if (kHighLatencyDevice ||
109 hardware_sample_frame_count >= kHighLatencySampleFrameCount ||
110 (hardware_sample_rate < 44100 &&
111 hardware_sample_frame_count > hardware_sample_rate / 100u)) {
112 return CalculateMultipleOfSampleFrameCount(
114 std::max(kHighLatencySampleFrameCount, hardware_sample_frame_count));
117 // All low latency clients should be able to handle a 512 frame buffer with
118 // resampling from 44.1kHz and 48kHz to higher sample rates.
119 // TODO(dalecurtis): We may need to investigate making the callback thread
120 // high priority to handle buffers at the absolute minimum w/o glitching.
121 const uint32_t kLowLatencySampleFrameCount = 512;
123 // Special case for 48kHz -> 44.1kHz and buffer sizes greater than 10ms. In
124 // testing most buffer sizes > 10ms led to glitching, so we choose a size we
125 // know won't cause jitter.
126 int min_sample_frame_count = kLowLatencySampleFrameCount;
127 if (hardware_sample_rate == 44100 && sample_rate == 48000 &&
128 hardware_sample_frame_count > hardware_sample_rate / 100u) {
129 min_sample_frame_count = std::max(
130 2 * kLowLatencySampleFrameCount, hardware_sample_frame_count);
133 return CalculateMultipleOfSampleFrameCount(
134 min_sample_frame_count, sample_frame_count);
138 PP_AudioSampleRate PPB_AudioConfig_Shared::RecommendSampleRate(
139 PP_Instance instance) {
140 thunk::EnterInstanceNoLock enter(instance);
142 return PP_AUDIOSAMPLERATE_NONE;
143 PP_AudioSampleRate hardware_sample_rate = static_cast<PP_AudioSampleRate>(
144 enter.functions()->GetAudioHardwareOutputSampleRate(instance));
145 return hardware_sample_rate;
148 thunk::PPB_AudioConfig_API* PPB_AudioConfig_Shared::AsPPB_AudioConfig_API() {
152 PP_AudioSampleRate PPB_AudioConfig_Shared::GetSampleRate() {
156 uint32_t PPB_AudioConfig_Shared::GetSampleFrameCount() {
157 return sample_frame_count_;
160 bool PPB_AudioConfig_Shared::Init(PP_AudioSampleRate sample_rate,
161 uint32_t sample_frame_count) {
162 // TODO(brettw): Currently we don't actually check what the hardware
163 // supports, so just allow sample rates of the "guaranteed working" ones.
164 // TODO(dalecurtis): If sample rates are added RecommendSampleFrameCount_1_1()
165 // must be updated to account for the new rates.
166 if (sample_rate != PP_AUDIOSAMPLERATE_44100 &&
167 sample_rate != PP_AUDIOSAMPLERATE_48000)
170 // TODO(brettw): Currently we don't actually query to get a value from the
171 // hardware, so just validate the range.
172 if (sample_frame_count > PP_AUDIOMAXSAMPLEFRAMECOUNT ||
173 sample_frame_count < PP_AUDIOMINSAMPLEFRAMECOUNT)
176 sample_rate_ = sample_rate;
177 sample_frame_count_ = sample_frame_count;