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 
AudioParameter(const String8 & keyValuePairs)57 AudioParameter::AudioParameter(const String8& keyValuePairs)
58 {
59     char *str = new char[keyValuePairs.length()+1];
60     mKeyValuePairs = keyValuePairs;
61     char *last;
62 
63     strcpy(str, keyValuePairs.string());
64     char *pair = strtok_r(str, ";", &last);
65     while (pair != NULL) {
66         if (strlen(pair) != 0) {
67             size_t eqIdx = strcspn(pair, "=");
68             String8 key = String8(pair, eqIdx);
69             String8 value;
70             if (eqIdx == strlen(pair)) {
71                 value = String8("");
72             } else {
73                 value = String8(pair + eqIdx + 1);
74             }
75             if (mParameters.indexOfKey(key) < 0) {
76                 mParameters.add(key, value);
77             } else {
78                 mParameters.replaceValueFor(key, value);
79             }
80         } else {
81             ALOGV("AudioParameter() cstor empty key value pair");
82         }
83         pair = strtok_r(NULL, ";", &last);
84     }
85 
86     delete[] str;
87 }
88 
~AudioParameter()89 AudioParameter::~AudioParameter()
90 {
91     mParameters.clear();
92 }
93 
toStringImpl(bool useValues) const94 String8 AudioParameter::toStringImpl(bool useValues) const
95 {
96     String8 str = String8("");
97 
98     size_t size = mParameters.size();
99     for (size_t i = 0; i < size; i++) {
100         str += mParameters.keyAt(i);
101         if (useValues) {
102             str += "=";
103             str += mParameters.valueAt(i);
104         }
105         if (i < (size - 1)) str += ";";
106     }
107     return str;
108 }
109 
add(const String8 & key,const String8 & value)110 status_t AudioParameter::add(const String8& key, const String8& value)
111 {
112     if (mParameters.indexOfKey(key) < 0) {
113         mParameters.add(key, value);
114         return NO_ERROR;
115     } else {
116         mParameters.replaceValueFor(key, value);
117         return ALREADY_EXISTS;
118     }
119 }
120 
addKey(const String8 & key)121 status_t AudioParameter::addKey(const String8& key)
122 {
123     return add(key, String8());
124 }
125 
addInt(const String8 & key,const int value)126 status_t AudioParameter::addInt(const String8& key, const int value)
127 {
128     char str[12];
129     if (snprintf(str, 12, "%d", value) > 0) {
130         String8 str8 = String8(str);
131         return add(key, str8);
132     } else {
133         return BAD_VALUE;
134     }
135 }
136 
addFloat(const String8 & key,const float value)137 status_t AudioParameter::addFloat(const String8& key, const float value)
138 {
139     char str[23];
140     if (snprintf(str, 23, "%.10f", value) > 0) {
141         String8 str8 = String8(str);
142         return add(key, str8);
143     } else {
144         return BAD_VALUE;
145     }
146 }
147 
remove(const String8 & key)148 status_t AudioParameter::remove(const String8& key)
149 {
150     if (mParameters.indexOfKey(key) >= 0) {
151         mParameters.removeItem(key);
152         return NO_ERROR;
153     } else {
154         return BAD_VALUE;
155     }
156 }
157 
get(const String8 & key,String8 & value) const158 status_t AudioParameter::get(const String8& key, String8& value) const
159 {
160     if (mParameters.indexOfKey(key) >= 0) {
161         value = mParameters.valueFor(key);
162         return NO_ERROR;
163     } else {
164         return BAD_VALUE;
165     }
166 }
167 
getInt(const String8 & key,int & value) const168 status_t AudioParameter::getInt(const String8& key, int& value) const
169 {
170     String8 str8;
171     status_t result = get(key, str8);
172     value = 0;
173     if (result == NO_ERROR) {
174         int val;
175         if (sscanf(str8.string(), "%d", &val) == 1) {
176             value = val;
177         } else {
178             result = INVALID_OPERATION;
179         }
180     }
181     return result;
182 }
183 
getFloat(const String8 & key,float & value) const184 status_t AudioParameter::getFloat(const String8& key, float& value) const
185 {
186     String8 str8;
187     status_t result = get(key, str8);
188     value = 0;
189     if (result == NO_ERROR) {
190         float val;
191         if (sscanf(str8.string(), "%f", &val) == 1) {
192             value = val;
193         } else {
194             result = INVALID_OPERATION;
195         }
196     }
197     return result;
198 }
199 
getAt(size_t index,String8 & key) const200 status_t AudioParameter::getAt(size_t index, String8& key) const
201 {
202     if (mParameters.size() > index) {
203         key = mParameters.keyAt(index);
204         return NO_ERROR;
205     } else {
206         return BAD_VALUE;
207     }
208 }
209 
getAt(size_t index,String8 & key,String8 & value) const210 status_t AudioParameter::getAt(size_t index, String8& key, String8& value) const
211 {
212     if (mParameters.size() > index) {
213         key = mParameters.keyAt(index);
214         value = mParameters.valueAt(index);
215         return NO_ERROR;
216     } else {
217         return BAD_VALUE;
218     }
219 }
220 
221 } // namespace android
222