blob: cf4d8c384159917b9174420ab4e039562557b388 [file] [log] [blame]
Dima Zavindb5cb142011-04-19 22:20:55 -07001/*
Alok Kediya601e9b52013-09-27 11:55:10 +05302 * Copyright (c) 2013, The Linux Foundation. All rights reserved.
3 * Not a Contribution.
4 *
Dima Zavindb5cb142011-04-19 22:20:55 -07005 * Copyright (C) 2006-2011 The Android Open Source Project
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20#define LOG_TAG "AudioParameter"
21//#define LOG_NDEBUG 0
22
23#include <utils/Log.h>
24
Glenn Kastenedf47a82012-04-01 13:49:17 -070025#include <hardware/audio.h>
Dima Zavindb5cb142011-04-19 22:20:55 -070026#include <media/AudioParameter.h>
27
28namespace android {
29
Glenn Kastenedf47a82012-04-01 13:49:17 -070030// static
31const char * const AudioParameter::keyRouting = AUDIO_PARAMETER_STREAM_ROUTING;
32const char * const AudioParameter::keySamplingRate = AUDIO_PARAMETER_STREAM_SAMPLING_RATE;
33const char * const AudioParameter::keyFormat = AUDIO_PARAMETER_STREAM_FORMAT;
34const char * const AudioParameter::keyChannels = AUDIO_PARAMETER_STREAM_CHANNELS;
35const char * const AudioParameter::keyFrameCount = AUDIO_PARAMETER_STREAM_FRAME_COUNT;
36const char * const AudioParameter::keyInputSource = AUDIO_PARAMETER_STREAM_INPUT_SOURCE;
Glenn Kasten28ed2f92012-06-07 10:17:54 -070037const char * const AudioParameter::keyScreenState = AUDIO_PARAMETER_KEY_SCREEN_STATE;
Alok Kediya601e9b52013-09-27 11:55:10 +053038#ifdef QCOM_FM_ENABLED
39const char * const AudioParameter::keyHandleFm = AUDIO_PARAMETER_KEY_HANDLE_FM;
40const char * const AudioParameter::keyVoipCheck = AUDIO_PARAMETER_KEY_VOIP_CHECK;
41const char * const AudioParameter::keyFluenceType = AUDIO_PARAMETER_KEY_FLUENCE_TYPE;
42const char * const AudioParameter::keySSR = AUDIO_PARAMETER_KEY_SSR;
43const char * const AudioParameter::keyHandleA2dpDevice = AUDIO_PARAMETER_KEY_HANDLE_A2DP_DEVICE;
44const char * const AudioParameter::keyADSPStatus = AUDIO_PARAMETER_KEY_ADSP_STATUS;
45const char * const AudioParameter::keyCanOpenProxy = AUDIO_CAN_OPEN_PROXY;
46const char * const AudioParameter::keyFmVolume = AUDIO_PARAMETER_KEY_FM_VOLUME;
47#endif
Dima Zavindb5cb142011-04-19 22:20:55 -070048
49AudioParameter::AudioParameter(const String8& keyValuePairs)
50{
51 char *str = new char[keyValuePairs.length()+1];
52 mKeyValuePairs = keyValuePairs;
53
54 strcpy(str, keyValuePairs.string());
55 char *pair = strtok(str, ";");
56 while (pair != NULL) {
57 if (strlen(pair) != 0) {
58 size_t eqIdx = strcspn(pair, "=");
59 String8 key = String8(pair, eqIdx);
60 String8 value;
61 if (eqIdx == strlen(pair)) {
62 value = String8("");
63 } else {
64 value = String8(pair + eqIdx + 1);
65 }
66 if (mParameters.indexOfKey(key) < 0) {
67 mParameters.add(key, value);
68 } else {
69 mParameters.replaceValueFor(key, value);
70 }
71 } else {
Steve Block3856b092011-10-20 11:56:00 +010072 ALOGV("AudioParameter() cstor empty key value pair");
Dima Zavindb5cb142011-04-19 22:20:55 -070073 }
74 pair = strtok(NULL, ";");
75 }
76
77 delete[] str;
78}
79
80AudioParameter::~AudioParameter()
81{
82 mParameters.clear();
83}
84
85String8 AudioParameter::toString()
86{
87 String8 str = String8("");
88
89 size_t size = mParameters.size();
90 for (size_t i = 0; i < size; i++) {
91 str += mParameters.keyAt(i);
92 str += "=";
93 str += mParameters.valueAt(i);
94 if (i < (size - 1)) str += ";";
95 }
96 return str;
97}
98
99status_t AudioParameter::add(const String8& key, const String8& value)
100{
101 if (mParameters.indexOfKey(key) < 0) {
102 mParameters.add(key, value);
103 return NO_ERROR;
104 } else {
105 mParameters.replaceValueFor(key, value);
106 return ALREADY_EXISTS;
107 }
108}
109
110status_t AudioParameter::addInt(const String8& key, const int value)
111{
112 char str[12];
113 if (snprintf(str, 12, "%d", value) > 0) {
114 String8 str8 = String8(str);
115 return add(key, str8);
116 } else {
117 return BAD_VALUE;
118 }
119}
120
121status_t AudioParameter::addFloat(const String8& key, const float value)
122{
123 char str[23];
124 if (snprintf(str, 23, "%.10f", value) > 0) {
125 String8 str8 = String8(str);
126 return add(key, str8);
127 } else {
128 return BAD_VALUE;
129 }
130}
131
132status_t AudioParameter::remove(const String8& key)
133{
134 if (mParameters.indexOfKey(key) >= 0) {
135 mParameters.removeItem(key);
136 return NO_ERROR;
137 } else {
138 return BAD_VALUE;
139 }
140}
141
142status_t AudioParameter::get(const String8& key, String8& value)
143{
144 if (mParameters.indexOfKey(key) >= 0) {
145 value = mParameters.valueFor(key);
146 return NO_ERROR;
147 } else {
148 return BAD_VALUE;
149 }
150}
151
152status_t AudioParameter::getInt(const String8& key, int& value)
153{
154 String8 str8;
155 status_t result = get(key, str8);
156 value = 0;
157 if (result == NO_ERROR) {
158 int val;
159 if (sscanf(str8.string(), "%d", &val) == 1) {
160 value = val;
161 } else {
162 result = INVALID_OPERATION;
163 }
164 }
165 return result;
166}
167
168status_t AudioParameter::getFloat(const String8& key, float& value)
169{
170 String8 str8;
171 status_t result = get(key, str8);
172 value = 0;
173 if (result == NO_ERROR) {
174 float val;
175 if (sscanf(str8.string(), "%f", &val) == 1) {
176 value = val;
177 } else {
178 result = INVALID_OPERATION;
179 }
180 }
181 return result;
182}
183
184status_t AudioParameter::getAt(size_t index, String8& key, String8& value)
185{
186 if (mParameters.size() > index) {
187 key = mParameters.keyAt(index);
188 value = mParameters.valueAt(index);
189 return NO_ERROR;
190 } else {
191 return BAD_VALUE;
192 }
193}
194
195}; // namespace android