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 "ppapi/shared_impl/ppb_audio_config_shared.h"
6 #include "ppapi/thunk/enter.h"
7 #include "ppapi/thunk/ppb_instance_api.h"
11 PPB_AudioConfig_Shared::PPB_AudioConfig_Shared(ResourceObjectType type
,
13 : Resource(type
, instance
),
14 sample_rate_(PP_AUDIOSAMPLERATE_NONE
),
15 sample_frame_count_(0) {
18 PPB_AudioConfig_Shared::~PPB_AudioConfig_Shared() {
21 PP_Resource
PPB_AudioConfig_Shared::Create(
22 ResourceObjectType type
,
24 PP_AudioSampleRate sample_rate
,
25 uint32_t sample_frame_count
) {
26 scoped_refptr
<PPB_AudioConfig_Shared
> object(
27 new PPB_AudioConfig_Shared(type
, instance
));
28 if (!object
->Init(sample_rate
, sample_frame_count
))
30 return object
->GetReference();
34 uint32_t PPB_AudioConfig_Shared::RecommendSampleFrameCount_1_0(
35 PP_AudioSampleRate sample_rate
,
36 uint32_t requested_sample_frame_count
) {
37 // Version 1.0: Don't actually query to get a value from the
38 // hardware; instead return the input for in-range values.
39 if (requested_sample_frame_count
< PP_AUDIOMINSAMPLEFRAMECOUNT
)
40 return PP_AUDIOMINSAMPLEFRAMECOUNT
;
41 if (requested_sample_frame_count
> PP_AUDIOMAXSAMPLEFRAMECOUNT
)
42 return PP_AUDIOMAXSAMPLEFRAMECOUNT
;
43 return requested_sample_frame_count
;
47 uint32_t PPB_AudioConfig_Shared::RecommendSampleFrameCount_1_1(
49 PP_AudioSampleRate sample_rate
,
50 uint32_t sample_frame_count
) {
51 // Version 1.1: Query the back-end hardware for sample rate and buffer size,
52 // and recommend a best fit based on request.
53 thunk::EnterInstanceNoLock
enter(instance
);
57 // Get the hardware config.
58 PP_AudioSampleRate hardware_sample_rate
= static_cast<PP_AudioSampleRate
>(
59 enter
.functions()->GetAudioHardwareOutputSampleRate(instance
));
60 uint32_t hardware_sample_frame_count
=
61 enter
.functions()->GetAudioHardwareOutputBufferSize(instance
);
62 if (sample_frame_count
< PP_AUDIOMINSAMPLEFRAMECOUNT
)
63 sample_frame_count
= PP_AUDIOMINSAMPLEFRAMECOUNT
;
65 // If client is using same sample rate as audio hardware, then recommend a
66 // multiple of the audio hardware's sample frame count.
67 if (hardware_sample_rate
== sample_rate
&& hardware_sample_frame_count
> 0) {
68 // Round up input sample_frame_count to nearest multiple.
69 uint32_t multiple
= (sample_frame_count
+ hardware_sample_frame_count
- 1) /
70 hardware_sample_frame_count
;
71 uint32_t recommendation
= hardware_sample_frame_count
* multiple
;
72 if (recommendation
> PP_AUDIOMAXSAMPLEFRAMECOUNT
)
73 recommendation
= PP_AUDIOMAXSAMPLEFRAMECOUNT
;
74 return recommendation
;
77 // Otherwise, recommend a conservative 50ms buffer based on sample rate.
78 const uint32_t kDefault50msAt44100kHz
= 2205;
79 const uint32_t kDefault50msAt48000kHz
= 2400;
80 switch (sample_rate
) {
81 case PP_AUDIOSAMPLERATE_44100
:
82 return kDefault50msAt44100kHz
;
83 case PP_AUDIOSAMPLERATE_48000
:
84 return kDefault50msAt48000kHz
;
85 case PP_AUDIOSAMPLERATE_NONE
:
88 // Unable to make a recommendation.
93 PP_AudioSampleRate
PPB_AudioConfig_Shared::RecommendSampleRate(
94 PP_Instance instance
) {
95 thunk::EnterInstanceNoLock
enter(instance
);
97 return PP_AUDIOSAMPLERATE_NONE
;
98 PP_AudioSampleRate hardware_sample_rate
= static_cast<PP_AudioSampleRate
>(
99 enter
.functions()->GetAudioHardwareOutputSampleRate(instance
));
100 return hardware_sample_rate
;
103 thunk::PPB_AudioConfig_API
* PPB_AudioConfig_Shared::AsPPB_AudioConfig_API() {
107 PP_AudioSampleRate
PPB_AudioConfig_Shared::GetSampleRate() {
111 uint32_t PPB_AudioConfig_Shared::GetSampleFrameCount() {
112 return sample_frame_count_
;
115 bool PPB_AudioConfig_Shared::Init(PP_AudioSampleRate sample_rate
,
116 uint32_t sample_frame_count
) {
117 // TODO(brettw): Currently we don't actually check what the hardware
118 // supports, so just allow sample rates of the "guaranteed working" ones.
119 if (sample_rate
!= PP_AUDIOSAMPLERATE_44100
&&
120 sample_rate
!= PP_AUDIOSAMPLERATE_48000
)
123 // TODO(brettw): Currently we don't actually query to get a value from the
124 // hardware, so just validate the range.
125 if (sample_frame_count
> PP_AUDIOMAXSAMPLEFRAMECOUNT
||
126 sample_frame_count
< PP_AUDIOMINSAMPLEFRAMECOUNT
)
129 sample_rate_
= sample_rate
;
130 sample_frame_count_
= sample_frame_count
;