[email protected] | 00d320a | 2012-02-14 00:27:04 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 9a57839 | 2011-12-07 18:59:27 | [diff] [blame] | 2 | // 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] | c59ed589 | 2012-02-18 01:10:19 | [diff] [blame] | 6 | #include "ppapi/thunk/enter.h" |
[email protected] | 4f200612 | 2012-04-30 05:13:17 | [diff] [blame] | 7 | #include "ppapi/thunk/ppb_instance_api.h" |
[email protected] | 9a57839 | 2011-12-07 18:59:27 | [diff] [blame] | 8 | |
| 9 | namespace ppapi { |
| 10 | |
[email protected] | 00d320a | 2012-02-14 00:27:04 | [diff] [blame] | 11 | PPB_AudioConfig_Shared::PPB_AudioConfig_Shared(ResourceObjectType type, |
| 12 | PP_Instance instance) |
| 13 | : Resource(type, instance), |
[email protected] | 9a57839 | 2011-12-07 18:59:27 | [diff] [blame] | 14 | sample_rate_(PP_AUDIOSAMPLERATE_NONE), |
| 15 | sample_frame_count_(0) { |
| 16 | } |
| 17 | |
| 18 | PPB_AudioConfig_Shared::~PPB_AudioConfig_Shared() { |
| 19 | } |
| 20 | |
[email protected] | 00d320a | 2012-02-14 00:27:04 | [diff] [blame] | 21 | PP_Resource PPB_AudioConfig_Shared::Create( |
| 22 | ResourceObjectType type, |
[email protected] | 9a57839 | 2011-12-07 18:59:27 | [diff] [blame] | 23 | PP_Instance instance, |
| 24 | PP_AudioSampleRate sample_rate, |
| 25 | uint32_t sample_frame_count) { |
| 26 | scoped_refptr<PPB_AudioConfig_Shared> object( |
[email protected] | 00d320a | 2012-02-14 00:27:04 | [diff] [blame] | 27 | new PPB_AudioConfig_Shared(type, instance)); |
[email protected] | 9a57839 | 2011-12-07 18:59:27 | [diff] [blame] | 28 | if (!object->Init(sample_rate, sample_frame_count)) |
| 29 | return 0; |
| 30 | return object->GetReference(); |
| 31 | } |
| 32 | |
[email protected] | c59ed589 | 2012-02-18 01:10:19 | [diff] [blame] | 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; |
| 44 | } |
| 45 | |
| 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. |
[email protected] | aed9653 | 2012-06-23 14:27:42 | [diff] [blame] | 53 | thunk::EnterInstanceNoLock enter(instance); |
[email protected] | c59ed589 | 2012-02-18 01:10:19 | [diff] [blame] | 54 | 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] | e052340 | 2012-06-26 20:29:17 | [diff] [blame] | 77 | // Otherwise, recommend a conservative 50ms buffer based on sample rate. |
| 78 | const uint32_t kDefault50msAt44100kHz = 2205; |
| 79 | const uint32_t kDefault50msAt48000kHz = 2400; |
[email protected] | c59ed589 | 2012-02-18 01:10:19 | [diff] [blame] | 80 | switch (sample_rate) { |
| 81 | case PP_AUDIOSAMPLERATE_44100: |
[email protected] | e052340 | 2012-06-26 20:29:17 | [diff] [blame] | 82 | return kDefault50msAt44100kHz; |
[email protected] | c59ed589 | 2012-02-18 01:10:19 | [diff] [blame] | 83 | case PP_AUDIOSAMPLERATE_48000: |
[email protected] | e052340 | 2012-06-26 20:29:17 | [diff] [blame] | 84 | return kDefault50msAt48000kHz; |
[email protected] | c59ed589 | 2012-02-18 01:10:19 | [diff] [blame] | 85 | case PP_AUDIOSAMPLERATE_NONE: |
| 86 | return 0; |
| 87 | } |
| 88 | // Unable to make a recommendation. |
| 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | // static |
| 93 | PP_AudioSampleRate PPB_AudioConfig_Shared::RecommendSampleRate( |
| 94 | PP_Instance instance) { |
[email protected] | aed9653 | 2012-06-23 14:27:42 | [diff] [blame] | 95 | thunk::EnterInstanceNoLock enter(instance); |
[email protected] | c59ed589 | 2012-02-18 01:10:19 | [diff] [blame] | 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; |
| 101 | } |
| 102 | |
[email protected] | 9a57839 | 2011-12-07 18:59:27 | [diff] [blame] | 103 | thunk::PPB_AudioConfig_API* PPB_AudioConfig_Shared::AsPPB_AudioConfig_API() { |
| 104 | return this; |
| 105 | } |
| 106 | |
| 107 | PP_AudioSampleRate PPB_AudioConfig_Shared::GetSampleRate() { |
| 108 | return sample_rate_; |
| 109 | } |
| 110 | |
| 111 | uint32_t PPB_AudioConfig_Shared::GetSampleFrameCount() { |
| 112 | return sample_frame_count_; |
| 113 | } |
| 114 | |
| 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; |
| 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 |