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::keyMonoOutput = AUDIO_PARAMETER_MONO_OUTPUT;
40 const char * const AudioParameter::keyStreamHwAvSync = AUDIO_PARAMETER_STREAM_HW_AV_SYNC;
41 const char * const AudioParameter::keyStreamConnect = AUDIO_PARAMETER_DEVICE_CONNECT;
42 const char * const AudioParameter::keyStreamDisconnect = AUDIO_PARAMETER_DEVICE_DISCONNECT;
43 const char * const AudioParameter::keyStreamSupportedFormats = AUDIO_PARAMETER_STREAM_SUP_FORMATS;
44 const char * const AudioParameter::keyStreamSupportedChannels = AUDIO_PARAMETER_STREAM_SUP_CHANNELS;
45 const char * const AudioParameter::keyStreamSupportedSamplingRates =
46 AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES;
47 const char * const AudioParameter::valueOn = AUDIO_PARAMETER_VALUE_ON;
48 const char * const AudioParameter::valueOff = AUDIO_PARAMETER_VALUE_OFF;
49 const char * const AudioParameter::valueListSeparator = AUDIO_PARAMETER_VALUE_LIST_SEPARATOR;
50 const char * const AudioParameter::keyReconfigA2dp = AUDIO_PARAMETER_RECONFIG_A2DP;
51 const char * const AudioParameter::keyReconfigA2dpSupported = AUDIO_PARAMETER_A2DP_RECONFIG_SUPPORTED;
52
AudioParameter(const String8 & keyValuePairs)53 AudioParameter::AudioParameter(const String8& keyValuePairs)
54 {
55 char *str = new char[keyValuePairs.length()+1];
56 mKeyValuePairs = keyValuePairs;
57 char *last;
58
59 strcpy(str, keyValuePairs.string());
60 char *pair = strtok_r(str, ";", &last);
61 while (pair != NULL) {
62 if (strlen(pair) != 0) {
63 size_t eqIdx = strcspn(pair, "=");
64 String8 key = String8(pair, eqIdx);
65 String8 value;
66 if (eqIdx == strlen(pair)) {
67 value = String8("");
68 } else {
69 value = String8(pair + eqIdx + 1);
70 }
71 if (mParameters.indexOfKey(key) < 0) {
72 mParameters.add(key, value);
73 } else {
74 mParameters.replaceValueFor(key, value);
75 }
76 } else {
77 ALOGV("AudioParameter() cstor empty key value pair");
78 }
79 pair = strtok_r(NULL, ";", &last);
80 }
81
82 delete[] str;
83 }
84
~AudioParameter()85 AudioParameter::~AudioParameter()
86 {
87 mParameters.clear();
88 }
89
toStringImpl(bool useValues) const90 String8 AudioParameter::toStringImpl(bool useValues) const
91 {
92 String8 str = String8("");
93
94 size_t size = mParameters.size();
95 for (size_t i = 0; i < size; i++) {
96 str += mParameters.keyAt(i);
97 if (useValues) {
98 str += "=";
99 str += mParameters.valueAt(i);
100 }
101 if (i < (size - 1)) str += ";";
102 }
103 return str;
104 }
105
add(const String8 & key,const String8 & value)106 status_t AudioParameter::add(const String8& key, const String8& value)
107 {
108 if (mParameters.indexOfKey(key) < 0) {
109 mParameters.add(key, value);
110 return NO_ERROR;
111 } else {
112 mParameters.replaceValueFor(key, value);
113 return ALREADY_EXISTS;
114 }
115 }
116
addKey(const String8 & key)117 status_t AudioParameter::addKey(const String8& key)
118 {
119 return add(key, String8());
120 }
121
addInt(const String8 & key,const int value)122 status_t AudioParameter::addInt(const String8& key, const int value)
123 {
124 char str[12];
125 if (snprintf(str, 12, "%d", value) > 0) {
126 String8 str8 = String8(str);
127 return add(key, str8);
128 } else {
129 return BAD_VALUE;
130 }
131 }
132
addFloat(const String8 & key,const float value)133 status_t AudioParameter::addFloat(const String8& key, const float value)
134 {
135 char str[23];
136 if (snprintf(str, 23, "%.10f", value) > 0) {
137 String8 str8 = String8(str);
138 return add(key, str8);
139 } else {
140 return BAD_VALUE;
141 }
142 }
143
remove(const String8 & key)144 status_t AudioParameter::remove(const String8& key)
145 {
146 if (mParameters.indexOfKey(key) >= 0) {
147 mParameters.removeItem(key);
148 return NO_ERROR;
149 } else {
150 return BAD_VALUE;
151 }
152 }
153
get(const String8 & key,String8 & value) const154 status_t AudioParameter::get(const String8& key, String8& value) const
155 {
156 if (mParameters.indexOfKey(key) >= 0) {
157 value = mParameters.valueFor(key);
158 return NO_ERROR;
159 } else {
160 return BAD_VALUE;
161 }
162 }
163
getInt(const String8 & key,int & value) const164 status_t AudioParameter::getInt(const String8& key, int& value) const
165 {
166 String8 str8;
167 status_t result = get(key, str8);
168 value = 0;
169 if (result == NO_ERROR) {
170 int val;
171 if (sscanf(str8.string(), "%d", &val) == 1) {
172 value = val;
173 } else {
174 result = INVALID_OPERATION;
175 }
176 }
177 return result;
178 }
179
getFloat(const String8 & key,float & value) const180 status_t AudioParameter::getFloat(const String8& key, float& value) const
181 {
182 String8 str8;
183 status_t result = get(key, str8);
184 value = 0;
185 if (result == NO_ERROR) {
186 float val;
187 if (sscanf(str8.string(), "%f", &val) == 1) {
188 value = val;
189 } else {
190 result = INVALID_OPERATION;
191 }
192 }
193 return result;
194 }
195
getAt(size_t index,String8 & key) const196 status_t AudioParameter::getAt(size_t index, String8& key) const
197 {
198 if (mParameters.size() > index) {
199 key = mParameters.keyAt(index);
200 return NO_ERROR;
201 } else {
202 return BAD_VALUE;
203 }
204 }
205
getAt(size_t index,String8 & key,String8 & value) const206 status_t AudioParameter::getAt(size_t index, String8& key, String8& value) const
207 {
208 if (mParameters.size() > index) {
209 key = mParameters.keyAt(index);
210 value = mParameters.valueAt(index);
211 return NO_ERROR;
212 } else {
213 return BAD_VALUE;
214 }
215 }
216
217 } // namespace android
218