Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(64)

Side by Side Diff: ppapi/shared_impl/ppb_audio_config_shared.cc

Issue 9129007: Work on improving PpbAudioConfig:RecommendSampleFrameCount (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 8 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ppapi/shared_impl/ppb_audio_config_shared.h" 5 #include "ppapi/shared_impl/ppb_audio_config_shared.h"
6 #include "ppapi/thunk/enter.h"
6 7
7 namespace ppapi { 8 namespace ppapi {
8 9
9 PPB_AudioConfig_Shared::PPB_AudioConfig_Shared(ResourceObjectType type, 10 PPB_AudioConfig_Shared::PPB_AudioConfig_Shared(ResourceObjectType type,
10 PP_Instance instance) 11 PP_Instance instance)
11 : Resource(type, instance), 12 : Resource(type, instance),
12 sample_rate_(PP_AUDIOSAMPLERATE_NONE), 13 sample_rate_(PP_AUDIOSAMPLERATE_NONE),
13 sample_frame_count_(0) { 14 sample_frame_count_(0) {
14 } 15 }
15 16
16 PPB_AudioConfig_Shared::~PPB_AudioConfig_Shared() { 17 PPB_AudioConfig_Shared::~PPB_AudioConfig_Shared() {
17 } 18 }
18 19
19 PP_Resource PPB_AudioConfig_Shared::Create( 20 PP_Resource PPB_AudioConfig_Shared::Create(
20 ResourceObjectType type, 21 ResourceObjectType type,
21 PP_Instance instance, 22 PP_Instance instance,
22 PP_AudioSampleRate sample_rate, 23 PP_AudioSampleRate sample_rate,
23 uint32_t sample_frame_count) { 24 uint32_t sample_frame_count) {
24 scoped_refptr<PPB_AudioConfig_Shared> object( 25 scoped_refptr<PPB_AudioConfig_Shared> object(
25 new PPB_AudioConfig_Shared(type, instance)); 26 new PPB_AudioConfig_Shared(type, instance));
26 if (!object->Init(sample_rate, sample_frame_count)) 27 if (!object->Init(sample_rate, sample_frame_count))
27 return 0; 28 return 0;
28 return object->GetReference(); 29 return object->GetReference();
29 } 30 }
30 31
32 // static
33 uint32_t PPB_AudioConfig_Shared::RecommendSampleFrameCount_1_0(
34 PP_AudioSampleRate sample_rate,
35 uint32_t requested_sample_frame_count) {
36 // Version 1.0: Don't actually query to get a value from the
37 // hardware; instead return the input for in-range values.
38 if (requested_sample_frame_count < PP_AUDIOMINSAMPLEFRAMECOUNT)
39 return PP_AUDIOMINSAMPLEFRAMECOUNT;
40 if (requested_sample_frame_count > PP_AUDIOMAXSAMPLEFRAMECOUNT)
41 return PP_AUDIOMAXSAMPLEFRAMECOUNT;
42 return requested_sample_frame_count;
43 }
44
45 // static
46 uint32_t PPB_AudioConfig_Shared::RecommendSampleFrameCount_1_1(
47 PP_Instance instance,
48 PP_AudioSampleRate sample_rate,
49 uint32_t sample_frame_count) {
50 // Version 1.1: Query the back-end hardware for sample rate and buffer size,
51 // and recommend a best fit based on request.
52 thunk::EnterInstance enter(instance);
53 if (enter.failed())
54 return 0;
55
56 // Get the hardware config.
57 PP_AudioSampleRate hardware_sample_rate = static_cast<PP_AudioSampleRate>(
58 enter.functions()->GetAudioHardwareOutputSampleRate(instance));
59 uint32_t hardware_sample_frame_count =
60 enter.functions()->GetAudioHardwareOutputBufferSize(instance);
61 if (sample_frame_count < PP_AUDIOMINSAMPLEFRAMECOUNT)
62 sample_frame_count = PP_AUDIOMINSAMPLEFRAMECOUNT;
63
64 // If client is using same sample rate as audio hardware, then recommend a
65 // multiple of the audio hardware's sample frame count.
66 if (hardware_sample_rate == sample_rate && hardware_sample_frame_count > 0) {
67 // Round up input sample_frame_count to nearest multiple.
68 uint32_t multiple = (sample_frame_count + hardware_sample_frame_count - 1) /
69 hardware_sample_frame_count;
70 uint32_t recommendation = hardware_sample_frame_count * multiple;
71 if (recommendation > PP_AUDIOMAXSAMPLEFRAMECOUNT)
72 recommendation = PP_AUDIOMAXSAMPLEFRAMECOUNT;
73 return recommendation;
74 }
75
76 // Otherwise, recommend a conservative 30ms buffer based on sample rate.
77 const uint32_t kDefault30msAt44100kHz = 1323;
78 const uint32_t kDefault30msAt48000kHz = 1440;
79 switch (sample_rate) {
80 case PP_AUDIOSAMPLERATE_44100:
81 return kDefault30msAt44100kHz;
82 case PP_AUDIOSAMPLERATE_48000:
83 return kDefault30msAt48000kHz;
84 case PP_AUDIOSAMPLERATE_NONE:
85 return 0;
86 }
87 // Unable to make a recommendation.
88 return 0;
89 }
90
91 // static
92 PP_AudioSampleRate PPB_AudioConfig_Shared::RecommendSampleRate(
93 PP_Instance instance) {
94 thunk::EnterInstance enter(instance);
95 if (enter.failed())
96 return PP_AUDIOSAMPLERATE_NONE;
97 PP_AudioSampleRate hardware_sample_rate = static_cast<PP_AudioSampleRate>(
98 enter.functions()->GetAudioHardwareOutputSampleRate(instance));
99 return hardware_sample_rate;
100 }
101
31 thunk::PPB_AudioConfig_API* PPB_AudioConfig_Shared::AsPPB_AudioConfig_API() { 102 thunk::PPB_AudioConfig_API* PPB_AudioConfig_Shared::AsPPB_AudioConfig_API() {
32 return this; 103 return this;
33 } 104 }
34 105
35 PP_AudioSampleRate PPB_AudioConfig_Shared::GetSampleRate() { 106 PP_AudioSampleRate PPB_AudioConfig_Shared::GetSampleRate() {
36 return sample_rate_; 107 return sample_rate_;
37 } 108 }
38 109
39 uint32_t PPB_AudioConfig_Shared::GetSampleFrameCount() { 110 uint32_t PPB_AudioConfig_Shared::GetSampleFrameCount() {
40 return sample_frame_count_; 111 return sample_frame_count_;
(...skipping 12 matching lines...) Expand all
53 if (sample_frame_count > PP_AUDIOMAXSAMPLEFRAMECOUNT || 124 if (sample_frame_count > PP_AUDIOMAXSAMPLEFRAMECOUNT ||
54 sample_frame_count < PP_AUDIOMINSAMPLEFRAMECOUNT) 125 sample_frame_count < PP_AUDIOMINSAMPLEFRAMECOUNT)
55 return false; 126 return false;
56 127
57 sample_rate_ = sample_rate; 128 sample_rate_ = sample_rate;
58 sample_frame_count_ = sample_frame_count; 129 sample_frame_count_ = sample_frame_count;
59 return true; 130 return true;
60 } 131 }
61 132
62 } // namespace ppapi 133 } // namespace ppapi
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698