1 /*
2 * Copyright (C) 2006-2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #define LOG_TAG "AudioParameter"
18 //#define LOG_NDEBUG 0
19
20 #include <utils/Log.h>
21
22 #include <media/AudioParameter.h>
23 #include <hardware/audio.h>
24 #include <system/audio.h>
25
26 namespace android {
27
28 // static
29 const char * const AudioParameter::keyRouting = AUDIO_PARAMETER_STREAM_ROUTING;
30 const char * const AudioParameter::keySamplingRate = AUDIO_PARAMETER_STREAM_SAMPLING_RATE;
31 const char * const AudioParameter::keyFormat = AUDIO_PARAMETER_STREAM_FORMAT;
32 const char * const AudioParameter::keyChannels = AUDIO_PARAMETER_STREAM_CHANNELS;
33 const char * const AudioParameter::keyFrameCount = AUDIO_PARAMETER_STREAM_FRAME_COUNT;
34 const char * const AudioParameter::keyInputSource = AUDIO_PARAMETER_STREAM_INPUT_SOURCE;
35 const char * const AudioParameter::keyScreenState = AUDIO_PARAMETER_KEY_SCREEN_STATE;
36 const char * const AudioParameter::keyScreenRotation = AUDIO_PARAMETER_KEY_ROTATION;
37 const char * const AudioParameter::keyClosing = AUDIO_PARAMETER_KEY_CLOSING;
38 const char * const AudioParameter::keyExiting = AUDIO_PARAMETER_KEY_EXITING;
39 const char * const AudioParameter::keyBtSco = AUDIO_PARAMETER_KEY_BT_SCO;
40 const char * const AudioParameter::keyBtScoHeadsetName = AUDIO_PARAMETER_KEY_BT_SCO_HEADSET_NAME;
41 const char * const AudioParameter::keyBtNrec = AUDIO_PARAMETER_KEY_BT_NREC;
42 const char * const AudioParameter::keyBtScoWb = AUDIO_PARAMETER_KEY_BT_SCO_WB;
43 const char * const AudioParameter::keyBtHfpEnable = AUDIO_PARAMETER_KEY_HFP_ENABLE;
44 const char * const AudioParameter::keyBtHfpSamplingRate = AUDIO_PARAMETER_KEY_HFP_SET_SAMPLING_RATE;
45 const char * const AudioParameter::keyBtHfpVolume = AUDIO_PARAMETER_KEY_HFP_VOLUME;
46 const char * const AudioParameter::keyTtyMode = AUDIO_PARAMETER_KEY_TTY_MODE;
47 const char * const AudioParameter::valueTtyModeOff = AUDIO_PARAMETER_VALUE_TTY_OFF;
48 const char * const AudioParameter::valueTtyModeFull = AUDIO_PARAMETER_VALUE_TTY_FULL;
49 const char * const AudioParameter::valueTtyModeHco = AUDIO_PARAMETER_VALUE_TTY_HCO;
50 const char * const AudioParameter::valueTtyModeVco = AUDIO_PARAMETER_VALUE_TTY_VCO;
51 const char * const AudioParameter::keyHacSetting = AUDIO_PARAMETER_KEY_HAC;
52 const char * const AudioParameter::valueHacOff = AUDIO_PARAMETER_VALUE_HAC_OFF;
53 const char * const AudioParameter::valueHacOn = AUDIO_PARAMETER_VALUE_HAC_ON;
54 const char * const AudioParameter::keyHwAvSync = AUDIO_PARAMETER_HW_AV_SYNC;
55 const char * const AudioParameter::keyPresentationId = AUDIO_PARAMETER_STREAM_PRESENTATION_ID;
56 const char * const AudioParameter::keyProgramId = AUDIO_PARAMETER_STREAM_PROGRAM_ID;
57 const char * const AudioParameter::keyAudioLanguagePreferred =
58 AUDIO_PARAMETER_KEY_AUDIO_LANGUAGE_PREFERRED;
59 const char * const AudioParameter::keyMonoOutput = AUDIO_PARAMETER_MONO_OUTPUT;
60 const char * const AudioParameter::keyStreamHwAvSync = AUDIO_PARAMETER_STREAM_HW_AV_SYNC;
61 const char * const AudioParameter::keyDeviceConnect = AUDIO_PARAMETER_DEVICE_CONNECT;
62 const char * const AudioParameter::keyDeviceDisconnect = AUDIO_PARAMETER_DEVICE_DISCONNECT;
63 const char * const AudioParameter::keyStreamConnect = AUDIO_PARAMETER_DEVICE_CONNECT;
64 const char * const AudioParameter::keyStreamDisconnect = AUDIO_PARAMETER_DEVICE_DISCONNECT;
65 const char * const AudioParameter::keyStreamSupportedFormats = AUDIO_PARAMETER_STREAM_SUP_FORMATS;
66 const char * const AudioParameter::keyStreamSupportedChannels = AUDIO_PARAMETER_STREAM_SUP_CHANNELS;
67 const char * const AudioParameter::keyStreamSupportedSamplingRates =
68 AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES;
69 const char * const AudioParameter::valueOn = AUDIO_PARAMETER_VALUE_ON;
70 const char * const AudioParameter::valueOff = AUDIO_PARAMETER_VALUE_OFF;
71 const char * const AudioParameter::valueTrue = AUDIO_PARAMETER_VALUE_TRUE;
72 const char * const AudioParameter::valueFalse = AUDIO_PARAMETER_VALUE_FALSE;
73 const char * const AudioParameter::valueListSeparator = AUDIO_PARAMETER_VALUE_LIST_SEPARATOR;
74 const char * const AudioParameter::keyBtA2dpSuspended = AUDIO_PARAMETER_KEY_BT_A2DP_SUSPENDED;
75 const char * const AudioParameter::keyReconfigA2dp = AUDIO_PARAMETER_RECONFIG_A2DP;
76 const char * const AudioParameter::keyReconfigA2dpSupported = AUDIO_PARAMETER_A2DP_RECONFIG_SUPPORTED;
77 const char * const AudioParameter::keyBtLeSuspended = AUDIO_PARAMETER_KEY_BT_LE_SUSPENDED;
78 const char * const AudioParameter::keyReconfigLe = AUDIO_PARAMETER_RECONFIG_LE;
79 const char * const AudioParameter::keyReconfigLeSupported = AUDIO_PARAMETER_LE_RECONFIG_SUPPORTED;
80 // const char * const AudioParameter::keyDeviceSupportedEncapsulationModes =
81 // AUDIO_PARAMETER_DEVICE_SUP_ENCAPSULATION_MODES;
82 // const char * const AudioParameter::keyDeviceSupportedEncapsulationMetadataTypes =
83 // AUDIO_PARAMETER_DEVICE_SUP_ENCAPSULATION_METADATA_TYPES;
84 const char * const AudioParameter::keyAdditionalOutputDeviceDelay =
85 AUDIO_PARAMETER_DEVICE_ADDITIONAL_OUTPUT_DELAY;
86 const char * const AudioParameter::keyMaxAdditionalOutputDeviceDelay =
87 AUDIO_PARAMETER_DEVICE_MAX_ADDITIONAL_OUTPUT_DELAY;
88 const char * const AudioParameter::keyOffloadCodecAverageBitRate = AUDIO_OFFLOAD_CODEC_AVG_BIT_RATE;
89 const char * const AudioParameter::keyOffloadCodecSampleRate = AUDIO_OFFLOAD_CODEC_SAMPLE_RATE;
90 const char * const AudioParameter::keyOffloadCodecChannels = AUDIO_OFFLOAD_CODEC_NUM_CHANNEL;
91 const char * const AudioParameter::keyOffloadCodecDelaySamples = AUDIO_OFFLOAD_CODEC_DELAY_SAMPLES;
92 const char * const AudioParameter::keyOffloadCodecPaddingSamples =
93 AUDIO_OFFLOAD_CODEC_PADDING_SAMPLES;
94
AudioParameter(const String8 & keyValuePairs)95 AudioParameter::AudioParameter(const String8& keyValuePairs)
96 {
97 char *str = new char[keyValuePairs.length()+1];
98 mKeyValuePairs = keyValuePairs;
99 char *last;
100
101 strcpy(str, keyValuePairs.c_str());
102 char *pair = strtok_r(str, ";", &last);
103 while (pair != NULL) {
104 if (strlen(pair) != 0) {
105 size_t eqIdx = strcspn(pair, "=");
106 String8 key = String8(pair, eqIdx);
107 String8 value;
108 if (eqIdx == strlen(pair)) {
109 value = String8("");
110 } else {
111 value = String8(pair + eqIdx + 1);
112 }
113 if (mParameters.indexOfKey(key) < 0) {
114 mParameters.add(key, value);
115 } else {
116 mParameters.replaceValueFor(key, value);
117 }
118 } else {
119 ALOGV("AudioParameter() cstor empty key value pair");
120 }
121 pair = strtok_r(NULL, ";", &last);
122 }
123
124 delete[] str;
125 }
126
~AudioParameter()127 AudioParameter::~AudioParameter()
128 {
129 mParameters.clear();
130 }
131
toStringImpl(bool useValues) const132 String8 AudioParameter::toStringImpl(bool useValues) const
133 {
134 String8 str = String8("");
135
136 size_t size = mParameters.size();
137 for (size_t i = 0; i < size; i++) {
138 str += mParameters.keyAt(i);
139 if (useValues) {
140 str += "=";
141 str += mParameters.valueAt(i);
142 }
143 if (i < (size - 1)) str += ";";
144 }
145 return str;
146 }
147
add(const String8 & key,const String8 & value)148 status_t AudioParameter::add(const String8& key, const String8& value)
149 {
150 if (mParameters.indexOfKey(key) < 0) {
151 mParameters.add(key, value);
152 return NO_ERROR;
153 } else {
154 mParameters.replaceValueFor(key, value);
155 return ALREADY_EXISTS;
156 }
157 }
158
addKey(const String8 & key)159 status_t AudioParameter::addKey(const String8& key)
160 {
161 return add(key, String8());
162 }
163
addInt(const String8 & key,const int value)164 status_t AudioParameter::addInt(const String8& key, const int value)
165 {
166 char str[12];
167 if (snprintf(str, 12, "%d", value) > 0) {
168 String8 str8 = String8(str);
169 return add(key, str8);
170 } else {
171 return BAD_VALUE;
172 }
173 }
174
addFloat(const String8 & key,const float value)175 status_t AudioParameter::addFloat(const String8& key, const float value)
176 {
177 char str[23];
178 if (snprintf(str, 23, "%.10f", value) > 0) {
179 String8 str8 = String8(str);
180 return add(key, str8);
181 } else {
182 return BAD_VALUE;
183 }
184 }
185
remove(const String8 & key)186 status_t AudioParameter::remove(const String8& key)
187 {
188 if (mParameters.indexOfKey(key) >= 0) {
189 mParameters.removeItem(key);
190 return NO_ERROR;
191 } else {
192 return BAD_VALUE;
193 }
194 }
195
get(const String8 & key,String8 & value) const196 status_t AudioParameter::get(const String8& key, String8& value) const
197 {
198 if (mParameters.indexOfKey(key) >= 0) {
199 value = mParameters.valueFor(key);
200 return NO_ERROR;
201 } else {
202 return BAD_VALUE;
203 }
204 }
205
getInt(const String8 & key,int & value) const206 status_t AudioParameter::getInt(const String8& key, int& value) const
207 {
208 String8 str8;
209 status_t result = get(key, str8);
210 value = 0;
211 if (result == NO_ERROR) {
212 int val;
213 if (sscanf(str8.c_str(), "%d", &val) == 1) {
214 value = val;
215 } else {
216 result = INVALID_OPERATION;
217 }
218 }
219 return result;
220 }
221
getFloat(const String8 & key,float & value) const222 status_t AudioParameter::getFloat(const String8& key, float& value) const
223 {
224 String8 str8;
225 status_t result = get(key, str8);
226 value = 0;
227 if (result == NO_ERROR) {
228 float val;
229 if (sscanf(str8.c_str(), "%f", &val) == 1) {
230 value = val;
231 } else {
232 result = INVALID_OPERATION;
233 }
234 }
235 return result;
236 }
237
getAt(size_t index,String8 & key) const238 status_t AudioParameter::getAt(size_t index, String8& key) const
239 {
240 if (mParameters.size() > index) {
241 key = mParameters.keyAt(index);
242 return NO_ERROR;
243 } else {
244 return BAD_VALUE;
245 }
246 }
247
getAt(size_t index,String8 & key,String8 & value) const248 status_t AudioParameter::getAt(size_t index, String8& key, String8& value) const
249 {
250 if (mParameters.size() > index) {
251 key = mParameters.keyAt(index);
252 value = mParameters.valueAt(index);
253 return NO_ERROR;
254 } else {
255 return BAD_VALUE;
256 }
257 }
258
containsKey(const String8 & key) const259 bool AudioParameter::containsKey(const String8& key) const
260 {
261 return mParameters.indexOfKey(key) >= 0;
262 }
263
264 } // namespace android
265