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 <system/audio.h>
24
25 namespace android {
26
27 // static
28 const char * const AudioParameter::keyRouting = AUDIO_PARAMETER_STREAM_ROUTING;
29 const char * const AudioParameter::keySamplingRate = AUDIO_PARAMETER_STREAM_SAMPLING_RATE;
30 const char * const AudioParameter::keyFormat = AUDIO_PARAMETER_STREAM_FORMAT;
31 const char * const AudioParameter::keyChannels = AUDIO_PARAMETER_STREAM_CHANNELS;
32 const char * const AudioParameter::keyFrameCount = AUDIO_PARAMETER_STREAM_FRAME_COUNT;
33 const char * const AudioParameter::keyInputSource = AUDIO_PARAMETER_STREAM_INPUT_SOURCE;
34 const char * const AudioParameter::keyScreenState = AUDIO_PARAMETER_KEY_SCREEN_STATE;
35 const char * const AudioParameter::keyBtNrec = AUDIO_PARAMETER_KEY_BT_NREC;
36 const char * const AudioParameter::keyHwAvSync = AUDIO_PARAMETER_HW_AV_SYNC;
37 const char * const AudioParameter::keyPresentationId = AUDIO_PARAMETER_STREAM_PRESENTATION_ID;
38 const char * const AudioParameter::keyProgramId = AUDIO_PARAMETER_STREAM_PROGRAM_ID;
39 const char * const AudioParameter::keyAudioLanguagePreferred =
40 AUDIO_PARAMETER_KEY_AUDIO_LANGUAGE_PREFERRED;
41 const char * const AudioParameter::keyMonoOutput = AUDIO_PARAMETER_MONO_OUTPUT;
42 const char * const AudioParameter::keyStreamHwAvSync = AUDIO_PARAMETER_STREAM_HW_AV_SYNC;
43 const char * const AudioParameter::keyDeviceConnect = AUDIO_PARAMETER_DEVICE_CONNECT;
44 const char * const AudioParameter::keyDeviceDisconnect = AUDIO_PARAMETER_DEVICE_DISCONNECT;
45 const char * const AudioParameter::keyStreamConnect = AUDIO_PARAMETER_DEVICE_CONNECT;
46 const char * const AudioParameter::keyStreamDisconnect = AUDIO_PARAMETER_DEVICE_DISCONNECT;
47 const char * const AudioParameter::keyStreamSupportedFormats = AUDIO_PARAMETER_STREAM_SUP_FORMATS;
48 const char * const AudioParameter::keyStreamSupportedChannels = AUDIO_PARAMETER_STREAM_SUP_CHANNELS;
49 const char * const AudioParameter::keyStreamSupportedSamplingRates =
50 AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES;
51 const char * const AudioParameter::valueOn = AUDIO_PARAMETER_VALUE_ON;
52 const char * const AudioParameter::valueOff = AUDIO_PARAMETER_VALUE_OFF;
53 const char * const AudioParameter::valueListSeparator = AUDIO_PARAMETER_VALUE_LIST_SEPARATOR;
54 const char * const AudioParameter::keyReconfigA2dp = AUDIO_PARAMETER_RECONFIG_A2DP;
55 const char * const AudioParameter::keyReconfigA2dpSupported = AUDIO_PARAMETER_A2DP_RECONFIG_SUPPORTED;
56 // const char * const AudioParameter::keyDeviceSupportedEncapsulationModes =
57 // AUDIO_PARAMETER_DEVICE_SUP_ENCAPSULATION_MODES;
58 // const char * const AudioParameter::keyDeviceSupportedEncapsulationMetadataTypes =
59 // AUDIO_PARAMETER_DEVICE_SUP_ENCAPSULATION_METADATA_TYPES;
60 const char * const AudioParameter::keyAdditionalOutputDeviceDelay =
61 AUDIO_PARAMETER_DEVICE_ADDITIONAL_OUTPUT_DELAY;
62 const char * const AudioParameter::keyMaxAdditionalOutputDeviceDelay =
63 AUDIO_PARAMETER_DEVICE_MAX_ADDITIONAL_OUTPUT_DELAY;
64
AudioParameter(const String8 & keyValuePairs)65 AudioParameter::AudioParameter(const String8& keyValuePairs)
66 {
67 char *str = new char[keyValuePairs.length()+1];
68 mKeyValuePairs = keyValuePairs;
69 char *last;
70
71 strcpy(str, keyValuePairs.string());
72 char *pair = strtok_r(str, ";", &last);
73 while (pair != NULL) {
74 if (strlen(pair) != 0) {
75 size_t eqIdx = strcspn(pair, "=");
76 String8 key = String8(pair, eqIdx);
77 String8 value;
78 if (eqIdx == strlen(pair)) {
79 value = String8("");
80 } else {
81 value = String8(pair + eqIdx + 1);
82 }
83 if (mParameters.indexOfKey(key) < 0) {
84 mParameters.add(key, value);
85 } else {
86 mParameters.replaceValueFor(key, value);
87 }
88 } else {
89 ALOGV("AudioParameter() cstor empty key value pair");
90 }
91 pair = strtok_r(NULL, ";", &last);
92 }
93
94 delete[] str;
95 }
96
~AudioParameter()97 AudioParameter::~AudioParameter()
98 {
99 mParameters.clear();
100 }
101
toStringImpl(bool useValues) const102 String8 AudioParameter::toStringImpl(bool useValues) const
103 {
104 String8 str = String8("");
105
106 size_t size = mParameters.size();
107 for (size_t i = 0; i < size; i++) {
108 str += mParameters.keyAt(i);
109 if (useValues) {
110 str += "=";
111 str += mParameters.valueAt(i);
112 }
113 if (i < (size - 1)) str += ";";
114 }
115 return str;
116 }
117
add(const String8 & key,const String8 & value)118 status_t AudioParameter::add(const String8& key, const String8& value)
119 {
120 if (mParameters.indexOfKey(key) < 0) {
121 mParameters.add(key, value);
122 return NO_ERROR;
123 } else {
124 mParameters.replaceValueFor(key, value);
125 return ALREADY_EXISTS;
126 }
127 }
128
addKey(const String8 & key)129 status_t AudioParameter::addKey(const String8& key)
130 {
131 return add(key, String8());
132 }
133
addInt(const String8 & key,const int value)134 status_t AudioParameter::addInt(const String8& key, const int value)
135 {
136 char str[12];
137 if (snprintf(str, 12, "%d", value) > 0) {
138 String8 str8 = String8(str);
139 return add(key, str8);
140 } else {
141 return BAD_VALUE;
142 }
143 }
144
addFloat(const String8 & key,const float value)145 status_t AudioParameter::addFloat(const String8& key, const float value)
146 {
147 char str[23];
148 if (snprintf(str, 23, "%.10f", value) > 0) {
149 String8 str8 = String8(str);
150 return add(key, str8);
151 } else {
152 return BAD_VALUE;
153 }
154 }
155
remove(const String8 & key)156 status_t AudioParameter::remove(const String8& key)
157 {
158 if (mParameters.indexOfKey(key) >= 0) {
159 mParameters.removeItem(key);
160 return NO_ERROR;
161 } else {
162 return BAD_VALUE;
163 }
164 }
165
get(const String8 & key,String8 & value) const166 status_t AudioParameter::get(const String8& key, String8& value) const
167 {
168 if (mParameters.indexOfKey(key) >= 0) {
169 value = mParameters.valueFor(key);
170 return NO_ERROR;
171 } else {
172 return BAD_VALUE;
173 }
174 }
175
getInt(const String8 & key,int & value) const176 status_t AudioParameter::getInt(const String8& key, int& value) const
177 {
178 String8 str8;
179 status_t result = get(key, str8);
180 value = 0;
181 if (result == NO_ERROR) {
182 int val;
183 if (sscanf(str8.string(), "%d", &val) == 1) {
184 value = val;
185 } else {
186 result = INVALID_OPERATION;
187 }
188 }
189 return result;
190 }
191
getFloat(const String8 & key,float & value) const192 status_t AudioParameter::getFloat(const String8& key, float& value) const
193 {
194 String8 str8;
195 status_t result = get(key, str8);
196 value = 0;
197 if (result == NO_ERROR) {
198 float val;
199 if (sscanf(str8.string(), "%f", &val) == 1) {
200 value = val;
201 } else {
202 result = INVALID_OPERATION;
203 }
204 }
205 return result;
206 }
207
getAt(size_t index,String8 & key) const208 status_t AudioParameter::getAt(size_t index, String8& key) const
209 {
210 if (mParameters.size() > index) {
211 key = mParameters.keyAt(index);
212 return NO_ERROR;
213 } else {
214 return BAD_VALUE;
215 }
216 }
217
getAt(size_t index,String8 & key,String8 & value) const218 status_t AudioParameter::getAt(size_t index, String8& key, String8& value) const
219 {
220 if (mParameters.size() > index) {
221 key = mParameters.keyAt(index);
222 value = mParameters.valueAt(index);
223 return NO_ERROR;
224 } else {
225 return BAD_VALUE;
226 }
227 }
228
229 } // namespace android
230