blob: 6d5b194afb1885b694fd58635606464b44b08518 [file] [log] [blame]
Dima Zavindb5cb142011-04-19 22:20:55 -07001/*
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
Glenn Kastenedf47a82012-04-01 13:49:17 -070022#include <hardware/audio.h>
Dima Zavindb5cb142011-04-19 22:20:55 -070023#include <media/AudioParameter.h>
24
25namespace android {
26
Glenn Kastenedf47a82012-04-01 13:49:17 -070027// 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;
Glenn Kasten28ed2f92012-06-07 10:17:54 -070034const char * const AudioParameter::keyScreenState = AUDIO_PARAMETER_KEY_SCREEN_STATE;
Mikhail Naganov00260b52016-10-13 12:54:24 -070035const char * const AudioParameter::keyBtNrec = AUDIO_PARAMETER_KEY_BT_NREC;
36const char * const AudioParameter::keyHwAvSync = AUDIO_PARAMETER_HW_AV_SYNC;
37const char * const AudioParameter::keyMonoOutput = AUDIO_PARAMETER_MONO_OUTPUT;
38const char * const AudioParameter::keyStreamHwAvSync = AUDIO_PARAMETER_STREAM_HW_AV_SYNC;
39const char * const AudioParameter::valueOn = AUDIO_PARAMETER_VALUE_ON;
40const char * const AudioParameter::valueOff = AUDIO_PARAMETER_VALUE_OFF;
Dima Zavindb5cb142011-04-19 22:20:55 -070041
42AudioParameter::AudioParameter(const String8& keyValuePairs)
43{
44 char *str = new char[keyValuePairs.length()+1];
45 mKeyValuePairs = keyValuePairs;
seunghak.hanff7455c2013-09-07 14:59:43 +090046 char *last;
Dima Zavindb5cb142011-04-19 22:20:55 -070047
48 strcpy(str, keyValuePairs.string());
seunghak.hanff7455c2013-09-07 14:59:43 +090049 char *pair = strtok_r(str, ";", &last);
Dima Zavindb5cb142011-04-19 22:20:55 -070050 while (pair != NULL) {
51 if (strlen(pair) != 0) {
52 size_t eqIdx = strcspn(pair, "=");
53 String8 key = String8(pair, eqIdx);
54 String8 value;
55 if (eqIdx == strlen(pair)) {
56 value = String8("");
57 } else {
58 value = String8(pair + eqIdx + 1);
59 }
60 if (mParameters.indexOfKey(key) < 0) {
61 mParameters.add(key, value);
62 } else {
63 mParameters.replaceValueFor(key, value);
64 }
65 } else {
Steve Block3856b092011-10-20 11:56:00 +010066 ALOGV("AudioParameter() cstor empty key value pair");
Dima Zavindb5cb142011-04-19 22:20:55 -070067 }
seunghak.hanff7455c2013-09-07 14:59:43 +090068 pair = strtok_r(NULL, ";", &last);
Dima Zavindb5cb142011-04-19 22:20:55 -070069 }
70
71 delete[] str;
72}
73
74AudioParameter::~AudioParameter()
75{
76 mParameters.clear();
77}
78
79String8 AudioParameter::toString()
80{
81 String8 str = String8("");
82
83 size_t size = mParameters.size();
84 for (size_t i = 0; i < size; i++) {
85 str += mParameters.keyAt(i);
86 str += "=";
87 str += mParameters.valueAt(i);
88 if (i < (size - 1)) str += ";";
89 }
90 return str;
91}
92
93status_t AudioParameter::add(const String8& key, const String8& value)
94{
95 if (mParameters.indexOfKey(key) < 0) {
96 mParameters.add(key, value);
97 return NO_ERROR;
98 } else {
99 mParameters.replaceValueFor(key, value);
100 return ALREADY_EXISTS;
101 }
102}
103
104status_t AudioParameter::addInt(const String8& key, const int value)
105{
106 char str[12];
107 if (snprintf(str, 12, "%d", value) > 0) {
108 String8 str8 = String8(str);
109 return add(key, str8);
110 } else {
111 return BAD_VALUE;
112 }
113}
114
115status_t AudioParameter::addFloat(const String8& key, const float value)
116{
117 char str[23];
118 if (snprintf(str, 23, "%.10f", value) > 0) {
119 String8 str8 = String8(str);
120 return add(key, str8);
121 } else {
122 return BAD_VALUE;
123 }
124}
125
126status_t AudioParameter::remove(const String8& key)
127{
128 if (mParameters.indexOfKey(key) >= 0) {
129 mParameters.removeItem(key);
130 return NO_ERROR;
131 } else {
132 return BAD_VALUE;
133 }
134}
135
136status_t AudioParameter::get(const String8& key, String8& value)
137{
138 if (mParameters.indexOfKey(key) >= 0) {
139 value = mParameters.valueFor(key);
140 return NO_ERROR;
141 } else {
142 return BAD_VALUE;
143 }
144}
145
146status_t AudioParameter::getInt(const String8& key, int& value)
147{
148 String8 str8;
149 status_t result = get(key, str8);
150 value = 0;
151 if (result == NO_ERROR) {
152 int val;
153 if (sscanf(str8.string(), "%d", &val) == 1) {
154 value = val;
155 } else {
156 result = INVALID_OPERATION;
157 }
158 }
159 return result;
160}
161
162status_t AudioParameter::getFloat(const String8& key, float& value)
163{
164 String8 str8;
165 status_t result = get(key, str8);
166 value = 0;
167 if (result == NO_ERROR) {
168 float val;
169 if (sscanf(str8.string(), "%f", &val) == 1) {
170 value = val;
171 } else {
172 result = INVALID_OPERATION;
173 }
174 }
175 return result;
176}
177
178status_t AudioParameter::getAt(size_t index, String8& key, String8& value)
179{
180 if (mParameters.size() > index) {
181 key = mParameters.keyAt(index);
182 value = mParameters.valueAt(index);
183 return NO_ERROR;
184 } else {
185 return BAD_VALUE;
186 }
187}
188
Glenn Kasten40bc9062015-03-20 09:09:33 -0700189} // namespace android