Make certificate viewer a tab-modal dialog.
[chromium-blink-merge.git] / ppapi / shared_impl / ppb_audio_config_shared.cc
blob25f356d67fe58ea28e731e617a013a58bb1cd0c1
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"
9 namespace ppapi {
11 PPB_AudioConfig_Shared::PPB_AudioConfig_Shared(ResourceObjectType type,
12 PP_Instance instance)
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,
23 PP_Instance instance,
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))
29 return 0;
30 return object->GetReference();
33 // static
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;
46 // static
47 uint32_t PPB_AudioConfig_Shared::RecommendSampleFrameCount_1_1(
48 PP_Instance instance,
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);
54 if (enter.failed())
55 return 0;
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:
86 return 0;
88 // Unable to make a recommendation.
89 return 0;
92 // static
93 PP_AudioSampleRate PPB_AudioConfig_Shared::RecommendSampleRate(
94 PP_Instance instance) {
95 thunk::EnterInstanceNoLock enter(instance);
96 if (enter.failed())
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() {
104 return this;
107 PP_AudioSampleRate PPB_AudioConfig_Shared::GetSampleRate() {
108 return sample_rate_;
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)
121 return false;
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)
127 return false;
129 sample_rate_ = sample_rate;
130 sample_frame_count_ = sample_frame_count;
131 return true;
134 } // namespace ppapi