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
25namespace android {
26
27// static
28const char * const AudioParameter::keyRouting = AUDIO_PARAMETER_STREAM_ROUTING;
29const char * const AudioParameter::keySamplingRate = AUDIO_PARAMETER_STREAM_SAMPLING_RATE;
30const char * const AudioParameter::keyFormat = AUDIO_PARAMETER_STREAM_FORMAT;
31const char * const AudioParameter::keyChannels = AUDIO_PARAMETER_STREAM_CHANNELS;
32const char * const AudioParameter::keyFrameCount = AUDIO_PARAMETER_STREAM_FRAME_COUNT;
33const char * const AudioParameter::keyInputSource = AUDIO_PARAMETER_STREAM_INPUT_SOURCE;
34const char * const AudioParameter::keyScreenState = AUDIO_PARAMETER_KEY_SCREEN_STATE;
35const char * const AudioParameter::keyBtNrec = AUDIO_PARAMETER_KEY_BT_NREC;
36const char * const AudioParameter::keyHwAvSync = AUDIO_PARAMETER_HW_AV_SYNC;
37const char * const AudioParameter::keyMonoOutput = AUDIO_PARAMETER_MONO_OUTPUT;
38const char * const AudioParameter::keyStreamHwAvSync = AUDIO_PARAMETER_STREAM_HW_AV_SYNC;
39const char * const AudioParameter::keyStreamConnect = AUDIO_PARAMETER_DEVICE_CONNECT;
40const char * const AudioParameter::keyStreamDisconnect = AUDIO_PARAMETER_DEVICE_DISCONNECT;
41const char * const AudioParameter::keyStreamSupportedFormats = AUDIO_PARAMETER_STREAM_SUP_FORMATS;
42const char * const AudioParameter::keyStreamSupportedChannels = AUDIO_PARAMETER_STREAM_SUP_CHANNELS;
43const char * const AudioParameter::keyStreamSupportedSamplingRates =
44        AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES;
45const char * const AudioParameter::valueOn = AUDIO_PARAMETER_VALUE_ON;
46const char * const AudioParameter::valueOff = AUDIO_PARAMETER_VALUE_OFF;
47const char * const AudioParameter::valueListSeparator = AUDIO_PARAMETER_VALUE_LIST_SEPARATOR;
48
49AudioParameter::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
81AudioParameter::~AudioParameter()
82{
83    mParameters.clear();
84}
85
86String8 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
102status_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
113status_t AudioParameter::addKey(const String8& key)
114{
115    return add(key, String8());
116}
117
118status_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
129status_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
140status_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
150status_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
160status_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
176status_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
192status_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
202status_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