1db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin/*
2db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin * Copyright (C) 2006-2011 The Android Open Source Project
3db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin *
4db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin * Licensed under the Apache License, Version 2.0 (the "License");
5db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin * you may not use this file except in compliance with the License.
6db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin * You may obtain a copy of the License at
7db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin *
8db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin *      http://www.apache.org/licenses/LICENSE-2.0
9db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin *
10db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin * Unless required by applicable law or agreed to in writing, software
11db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin * distributed under the License is distributed on an "AS IS" BASIS,
12db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin * See the License for the specific language governing permissions and
14db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin * limitations under the License.
15db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin */
16db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin
17db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin#define LOG_TAG "AudioParameter"
18db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin//#define LOG_NDEBUG 0
19db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin
20db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin#include <utils/Log.h>
21db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin
22edf47a8cba2d1d8985b64bf4a4fac661ba34f35aGlenn Kasten#include <hardware/audio.h>
23db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin#include <media/AudioParameter.h>
24db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin
25db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavinnamespace android {
26db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin
27edf47a8cba2d1d8985b64bf4a4fac661ba34f35aGlenn Kasten// static
28edf47a8cba2d1d8985b64bf4a4fac661ba34f35aGlenn Kastenconst char * const AudioParameter::keyRouting = AUDIO_PARAMETER_STREAM_ROUTING;
29edf47a8cba2d1d8985b64bf4a4fac661ba34f35aGlenn Kastenconst char * const AudioParameter::keySamplingRate = AUDIO_PARAMETER_STREAM_SAMPLING_RATE;
30edf47a8cba2d1d8985b64bf4a4fac661ba34f35aGlenn Kastenconst char * const AudioParameter::keyFormat = AUDIO_PARAMETER_STREAM_FORMAT;
31edf47a8cba2d1d8985b64bf4a4fac661ba34f35aGlenn Kastenconst char * const AudioParameter::keyChannels = AUDIO_PARAMETER_STREAM_CHANNELS;
32edf47a8cba2d1d8985b64bf4a4fac661ba34f35aGlenn Kastenconst char * const AudioParameter::keyFrameCount = AUDIO_PARAMETER_STREAM_FRAME_COUNT;
33edf47a8cba2d1d8985b64bf4a4fac661ba34f35aGlenn Kastenconst char * const AudioParameter::keyInputSource = AUDIO_PARAMETER_STREAM_INPUT_SOURCE;
3428ed2f93324988767b5658eba7c1fa781a275183Glenn Kastenconst char * const AudioParameter::keyScreenState = AUDIO_PARAMETER_KEY_SCREEN_STATE;
35db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin
36db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima ZavinAudioParameter::AudioParameter(const String8& keyValuePairs)
37db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin{
38db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin    char *str = new char[keyValuePairs.length()+1];
39db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin    mKeyValuePairs = keyValuePairs;
40ff7455c0c1df0c316a306fb81ee660840d08b6d0seunghak.han    char *last;
41db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin
42db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin    strcpy(str, keyValuePairs.string());
43ff7455c0c1df0c316a306fb81ee660840d08b6d0seunghak.han    char *pair = strtok_r(str, ";", &last);
44db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin    while (pair != NULL) {
45db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin        if (strlen(pair) != 0) {
46db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin            size_t eqIdx = strcspn(pair, "=");
47db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin            String8 key = String8(pair, eqIdx);
48db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin            String8 value;
49db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin            if (eqIdx == strlen(pair)) {
50db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin                value = String8("");
51db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin            } else {
52db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin                value = String8(pair + eqIdx + 1);
53db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin            }
54db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin            if (mParameters.indexOfKey(key) < 0) {
55db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin                mParameters.add(key, value);
56db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin            } else {
57db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin                mParameters.replaceValueFor(key, value);
58db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin            }
59db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin        } else {
603856b090cd04ba5dd4a59a12430ed724d5995909Steve Block            ALOGV("AudioParameter() cstor empty key value pair");
61db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin        }
62ff7455c0c1df0c316a306fb81ee660840d08b6d0seunghak.han        pair = strtok_r(NULL, ";", &last);
63db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin    }
64db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin
65db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin    delete[] str;
66db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin}
67db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin
68db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima ZavinAudioParameter::~AudioParameter()
69db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin{
70db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin    mParameters.clear();
71db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin}
72db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin
73db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima ZavinString8 AudioParameter::toString()
74db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin{
75db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin    String8 str = String8("");
76db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin
77db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin    size_t size = mParameters.size();
78db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin    for (size_t i = 0; i < size; i++) {
79db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin        str += mParameters.keyAt(i);
80db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin        str += "=";
81db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin        str += mParameters.valueAt(i);
82db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin        if (i < (size - 1)) str += ";";
83db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin    }
84db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin    return str;
85db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin}
86db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin
87db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavinstatus_t AudioParameter::add(const String8& key, const String8& value)
88db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin{
89db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin    if (mParameters.indexOfKey(key) < 0) {
90db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin        mParameters.add(key, value);
91db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin        return NO_ERROR;
92db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin    } else {
93db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin        mParameters.replaceValueFor(key, value);
94db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin        return ALREADY_EXISTS;
95db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin    }
96db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin}
97db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin
98db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavinstatus_t AudioParameter::addInt(const String8& key, const int value)
99db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin{
100db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin    char str[12];
101db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin    if (snprintf(str, 12, "%d", value) > 0) {
102db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin        String8 str8 = String8(str);
103db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin        return add(key, str8);
104db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin    } else {
105db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin        return BAD_VALUE;
106db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin    }
107db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin}
108db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin
109db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavinstatus_t AudioParameter::addFloat(const String8& key, const float value)
110db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin{
111db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin    char str[23];
112db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin    if (snprintf(str, 23, "%.10f", value) > 0) {
113db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin        String8 str8 = String8(str);
114db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin        return add(key, str8);
115db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin    } else {
116db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin        return BAD_VALUE;
117db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin    }
118db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin}
119db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin
120db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavinstatus_t AudioParameter::remove(const String8& key)
121db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin{
122db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin    if (mParameters.indexOfKey(key) >= 0) {
123db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin        mParameters.removeItem(key);
124db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin        return NO_ERROR;
125db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin    } else {
126db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin        return BAD_VALUE;
127db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin    }
128db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin}
129db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin
130db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavinstatus_t AudioParameter::get(const String8& key, String8& value)
131db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin{
132db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin    if (mParameters.indexOfKey(key) >= 0) {
133db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin        value = mParameters.valueFor(key);
134db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin        return NO_ERROR;
135db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin    } else {
136db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin        return BAD_VALUE;
137db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin    }
138db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin}
139db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin
140db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavinstatus_t AudioParameter::getInt(const String8& key, int& value)
141db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin{
142db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin    String8 str8;
143db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin    status_t result = get(key, str8);
144db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin    value = 0;
145db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin    if (result == NO_ERROR) {
146db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin        int val;
147db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin        if (sscanf(str8.string(), "%d", &val) == 1) {
148db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin            value = val;
149db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin        } else {
150db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin            result = INVALID_OPERATION;
151db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin        }
152db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin    }
153db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin    return result;
154db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin}
155db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin
156db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavinstatus_t AudioParameter::getFloat(const String8& key, float& value)
157db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin{
158db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin    String8 str8;
159db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin    status_t result = get(key, str8);
160db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin    value = 0;
161db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin    if (result == NO_ERROR) {
162db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin        float val;
163db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin        if (sscanf(str8.string(), "%f", &val) == 1) {
164db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin            value = val;
165db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin        } else {
166db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin            result = INVALID_OPERATION;
167db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin        }
168db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin    }
169db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin    return result;
170db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin}
171db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin
172db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavinstatus_t AudioParameter::getAt(size_t index, String8& key, String8& value)
173db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin{
174db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin    if (mParameters.size() > index) {
175db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin        key = mParameters.keyAt(index);
176db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin        value = mParameters.valueAt(index);
177db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin        return NO_ERROR;
178db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin    } else {
179db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin        return BAD_VALUE;
180db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin    }
181db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin}
182db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin
183db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903Dima Zavin};  // namespace android
184