blob: 25f356d67fe58ea28e731e617a013a58bb1cd0c1 [file] [log] [blame]
[email protected]00d320a2012-02-14 00:27:041// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]9a578392011-12-07 18:59:272// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "ppapi/shared_impl/ppb_audio_config_shared.h"
[email protected]c59ed5892012-02-18 01:10:196#include "ppapi/thunk/enter.h"
[email protected]4f2006122012-04-30 05:13:177#include "ppapi/thunk/ppb_instance_api.h"
[email protected]9a578392011-12-07 18:59:278
9namespace ppapi {
10
[email protected]00d320a2012-02-14 00:27:0411PPB_AudioConfig_Shared::PPB_AudioConfig_Shared(ResourceObjectType type,
12 PP_Instance instance)
13 : Resource(type, instance),
[email protected]9a578392011-12-07 18:59:2714 sample_rate_(PP_AUDIOSAMPLERATE_NONE),
15 sample_frame_count_(0) {
16}
17
18PPB_AudioConfig_Shared::~PPB_AudioConfig_Shared() {
19}
20
[email protected]00d320a2012-02-14 00:27:0421PP_Resource PPB_AudioConfig_Shared::Create(
22 ResourceObjectType type,
[email protected]9a578392011-12-07 18:59:2723 PP_Instance instance,
24 PP_AudioSampleRate sample_rate,
25 uint32_t sample_frame_count) {
26 scoped_refptr<PPB_AudioConfig_Shared> object(
[email protected]00d320a2012-02-14 00:27:0427 new PPB_AudioConfig_Shared(type, instance));
[email protected]9a578392011-12-07 18:59:2728 if (!object->Init(sample_rate, sample_frame_count))
29 return 0;
30 return object->GetReference();
31}
32
[email protected]c59ed5892012-02-18 01:10:1933// static
34uint32_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;
44}
45
46// static
47uint32_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.
[email protected]aed96532012-06-23 14:27:4253 thunk::EnterInstanceNoLock enter(instance);
[email protected]c59ed5892012-02-18 01:10:1954 if (enter.failed())
55 return 0;
56
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;
64
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;
75 }
76
[email protected]e0523402012-06-26 20:29:1777 // Otherwise, recommend a conservative 50ms buffer based on sample rate.
78 const uint32_t kDefault50msAt44100kHz = 2205;
79 const uint32_t kDefault50msAt48000kHz = 2400;
[email protected]c59ed5892012-02-18 01:10:1980 switch (sample_rate) {
81 case PP_AUDIOSAMPLERATE_44100:
[email protected]e0523402012-06-26 20:29:1782 return kDefault50msAt44100kHz;
[email protected]c59ed5892012-02-18 01:10:1983 case PP_AUDIOSAMPLERATE_48000:
[email protected]e0523402012-06-26 20:29:1784 return kDefault50msAt48000kHz;
[email protected]c59ed5892012-02-18 01:10:1985 case PP_AUDIOSAMPLERATE_NONE:
86 return 0;
87 }
88 // Unable to make a recommendation.
89 return 0;
90}
91
92// static
93PP_AudioSampleRate PPB_AudioConfig_Shared::RecommendSampleRate(
94 PP_Instance instance) {
[email protected]aed96532012-06-23 14:27:4295 thunk::EnterInstanceNoLock enter(instance);
[email protected]c59ed5892012-02-18 01:10:1996 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;
101}
102
[email protected]9a578392011-12-07 18:59:27103thunk::PPB_AudioConfig_API* PPB_AudioConfig_Shared::AsPPB_AudioConfig_API() {
104 return this;
105}
106
107PP_AudioSampleRate PPB_AudioConfig_Shared::GetSampleRate() {
108 return sample_rate_;
109}
110
111uint32_t PPB_AudioConfig_Shared::GetSampleFrameCount() {
112 return sample_frame_count_;
113}
114
115bool 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;
122
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;
128
129 sample_rate_ = sample_rate;
130 sample_frame_count_ = sample_frame_count;
131 return true;
132}
133
134} // namespace ppapi