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