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 <hardware/audio.h>
23#include <media/AudioParameter.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;
35
36AudioParameter::AudioParameter(const String8& keyValuePairs)
37{
38    char *str = new char[keyValuePairs.length()+1];
39    mKeyValuePairs = keyValuePairs;
40    char *last;
41
42    strcpy(str, keyValuePairs.string());
43    char *pair = strtok_r(str, ";", &last);
44    while (pair != NULL) {
45        if (strlen(pair) != 0) {
46            size_t eqIdx = strcspn(pair, "=");
47            String8 key = String8(pair, eqIdx);
48            String8 value;
49            if (eqIdx == strlen(pair)) {
50                value = String8("");
51            } else {
52                value = String8(pair + eqIdx + 1);
53            }
54            if (mParameters.indexOfKey(key) < 0) {
55                mParameters.add(key, value);
56            } else {
57                mParameters.replaceValueFor(key, value);
58            }
59        } else {
60            ALOGV("AudioParameter() cstor empty key value pair");
61        }
62        pair = strtok_r(NULL, ";", &last);
63    }
64
65    delete[] str;
66}
67
68AudioParameter::~AudioParameter()
69{
70    mParameters.clear();
71}
72
73String8 AudioParameter::toString()
74{
75    String8 str = String8("");
76
77    size_t size = mParameters.size();
78    for (size_t i = 0; i < size; i++) {
79        str += mParameters.keyAt(i);
80        str += "=";
81        str += mParameters.valueAt(i);
82        if (i < (size - 1)) str += ";";
83    }
84    return str;
85}
86
87status_t AudioParameter::add(const String8& key, const String8& value)
88{
89    if (mParameters.indexOfKey(key) < 0) {
90        mParameters.add(key, value);
91        return NO_ERROR;
92    } else {
93        mParameters.replaceValueFor(key, value);
94        return ALREADY_EXISTS;
95    }
96}
97
98status_t AudioParameter::addInt(const String8& key, const int value)
99{
100    char str[12];
101    if (snprintf(str, 12, "%d", value) > 0) {
102        String8 str8 = String8(str);
103        return add(key, str8);
104    } else {
105        return BAD_VALUE;
106    }
107}
108
109status_t AudioParameter::addFloat(const String8& key, const float value)
110{
111    char str[23];
112    if (snprintf(str, 23, "%.10f", value) > 0) {
113        String8 str8 = String8(str);
114        return add(key, str8);
115    } else {
116        return BAD_VALUE;
117    }
118}
119
120status_t AudioParameter::remove(const String8& key)
121{
122    if (mParameters.indexOfKey(key) >= 0) {
123        mParameters.removeItem(key);
124        return NO_ERROR;
125    } else {
126        return BAD_VALUE;
127    }
128}
129
130status_t AudioParameter::get(const String8& key, String8& value)
131{
132    if (mParameters.indexOfKey(key) >= 0) {
133        value = mParameters.valueFor(key);
134        return NO_ERROR;
135    } else {
136        return BAD_VALUE;
137    }
138}
139
140status_t AudioParameter::getInt(const String8& key, int& value)
141{
142    String8 str8;
143    status_t result = get(key, str8);
144    value = 0;
145    if (result == NO_ERROR) {
146        int val;
147        if (sscanf(str8.string(), "%d", &val) == 1) {
148            value = val;
149        } else {
150            result = INVALID_OPERATION;
151        }
152    }
153    return result;
154}
155
156status_t AudioParameter::getFloat(const String8& key, float& value)
157{
158    String8 str8;
159    status_t result = get(key, str8);
160    value = 0;
161    if (result == NO_ERROR) {
162        float val;
163        if (sscanf(str8.string(), "%f", &val) == 1) {
164            value = val;
165        } else {
166            result = INVALID_OPERATION;
167        }
168    }
169    return result;
170}
171
172status_t AudioParameter::getAt(size_t index, String8& key, String8& value)
173{
174    if (mParameters.size() > index) {
175        key = mParameters.keyAt(index);
176        value = mParameters.valueAt(index);
177        return NO_ERROR;
178    } else {
179        return BAD_VALUE;
180    }
181}
182
183};  // namespace android
184