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