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