blob: 367d9f6bc501290d13be2dfe6f433b80722ef609 [file] [log] [blame]
Dima Zavindb5cb142011-04-19 22:20:55 -07001/*
2 * Copyright (C) 2006-2011 The Android Open Source Project
Ravishankar Sarawadi20017912013-02-27 18:47:54 +01003 * Copyright (c) 2012-2013, 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;
Ravishankar Sarawadi20017912013-02-27 18:47:54 +010042const char * const AudioParameter::keyADSPStatus = AUDIO_PARAMETER_KEY_ADSP_STATUS;
vivek mehta2192bc92012-12-04 11:46:08 -080043#endif
Dima Zavindb5cb142011-04-19 22:20:55 -070044
45AudioParameter::AudioParameter(const String8& keyValuePairs)
46{
47 char *str = new char[keyValuePairs.length()+1];
48 mKeyValuePairs = keyValuePairs;
49
50 strcpy(str, keyValuePairs.string());
51 char *pair = strtok(str, ";");
52 while (pair != NULL) {
53 if (strlen(pair) != 0) {
54 size_t eqIdx = strcspn(pair, "=");
55 String8 key = String8(pair, eqIdx);
56 String8 value;
57 if (eqIdx == strlen(pair)) {
58 value = String8("");
59 } else {
60 value = String8(pair + eqIdx + 1);
61 }
62 if (mParameters.indexOfKey(key) < 0) {
63 mParameters.add(key, value);
64 } else {
65 mParameters.replaceValueFor(key, value);
66 }
67 } else {
Steve Block3856b092011-10-20 11:56:00 +010068 ALOGV("AudioParameter() cstor empty key value pair");
Dima Zavindb5cb142011-04-19 22:20:55 -070069 }
70 pair = strtok(NULL, ";");
71 }
72
73 delete[] str;
74}
75
76AudioParameter::~AudioParameter()
77{
78 mParameters.clear();
79}
80
81String8 AudioParameter::toString()
82{
83 String8 str = String8("");
84
85 size_t size = mParameters.size();
86 for (size_t i = 0; i < size; i++) {
87 str += mParameters.keyAt(i);
88 str += "=";
89 str += mParameters.valueAt(i);
90 if (i < (size - 1)) str += ";";
91 }
92 return str;
93}
94
95status_t AudioParameter::add(const String8& key, const String8& value)
96{
97 if (mParameters.indexOfKey(key) < 0) {
98 mParameters.add(key, value);
99 return NO_ERROR;
100 } else {
101 mParameters.replaceValueFor(key, value);
102 return ALREADY_EXISTS;
103 }
104}
105
106status_t AudioParameter::addInt(const String8& key, const int value)
107{
108 char str[12];
109 if (snprintf(str, 12, "%d", value) > 0) {
110 String8 str8 = String8(str);
111 return add(key, str8);
112 } else {
113 return BAD_VALUE;
114 }
115}
116
117status_t AudioParameter::addFloat(const String8& key, const float value)
118{
119 char str[23];
120 if (snprintf(str, 23, "%.10f", value) > 0) {
121 String8 str8 = String8(str);
122 return add(key, str8);
123 } else {
124 return BAD_VALUE;
125 }
126}
127
128status_t AudioParameter::remove(const String8& key)
129{
130 if (mParameters.indexOfKey(key) >= 0) {
131 mParameters.removeItem(key);
132 return NO_ERROR;
133 } else {
134 return BAD_VALUE;
135 }
136}
137
138status_t AudioParameter::get(const String8& key, String8& value)
139{
140 if (mParameters.indexOfKey(key) >= 0) {
141 value = mParameters.valueFor(key);
142 return NO_ERROR;
143 } else {
144 return BAD_VALUE;
145 }
146}
147
148status_t AudioParameter::getInt(const String8& key, int& value)
149{
150 String8 str8;
151 status_t result = get(key, str8);
152 value = 0;
153 if (result == NO_ERROR) {
154 int val;
155 if (sscanf(str8.string(), "%d", &val) == 1) {
156 value = val;
157 } else {
158 result = INVALID_OPERATION;
159 }
160 }
161 return result;
162}
163
164status_t AudioParameter::getFloat(const String8& key, float& value)
165{
166 String8 str8;
167 status_t result = get(key, str8);
168 value = 0;
169 if (result == NO_ERROR) {
170 float val;
171 if (sscanf(str8.string(), "%f", &val) == 1) {
172 value = val;
173 } else {
174 result = INVALID_OPERATION;
175 }
176 }
177 return result;
178}
179
180status_t AudioParameter::getAt(size_t index, String8& key, String8& value)
181{
182 if (mParameters.size() > index) {
183 key = mParameters.keyAt(index);
184 value = mParameters.valueAt(index);
185 return NO_ERROR;
186 } else {
187 return BAD_VALUE;
188 }
189}
190
191}; // namespace android