blob: cb0e9272093f480aa499b9ca63ea305731a7322a [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
22#include <media/AudioParameter.h>
Mikhail Naganov388360c2016-10-17 17:09:41 -070023#include <system/audio.h>
Dima Zavindb5cb142011-04-19 22:20:55 -070024
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;
Dean Wheatleya70eef72018-01-04 14:23:50 +110037const char * const AudioParameter::keyPresentationId = AUDIO_PARAMETER_STREAM_PRESENTATION_ID;
38const char * const AudioParameter::keyProgramId = AUDIO_PARAMETER_STREAM_PROGRAM_ID;
Mikhail Naganov00260b52016-10-13 12:54:24 -070039const char * const AudioParameter::keyMonoOutput = AUDIO_PARAMETER_MONO_OUTPUT;
40const char * const AudioParameter::keyStreamHwAvSync = AUDIO_PARAMETER_STREAM_HW_AV_SYNC;
Mikhail Naganov388360c2016-10-17 17:09:41 -070041const char * const AudioParameter::keyStreamConnect = AUDIO_PARAMETER_DEVICE_CONNECT;
42const char * const AudioParameter::keyStreamDisconnect = AUDIO_PARAMETER_DEVICE_DISCONNECT;
43const char * const AudioParameter::keyStreamSupportedFormats = AUDIO_PARAMETER_STREAM_SUP_FORMATS;
44const char * const AudioParameter::keyStreamSupportedChannels = AUDIO_PARAMETER_STREAM_SUP_CHANNELS;
45const char * const AudioParameter::keyStreamSupportedSamplingRates =
46 AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES;
Mikhail Naganov00260b52016-10-13 12:54:24 -070047const char * const AudioParameter::valueOn = AUDIO_PARAMETER_VALUE_ON;
48const char * const AudioParameter::valueOff = AUDIO_PARAMETER_VALUE_OFF;
Mikhail Naganov388360c2016-10-17 17:09:41 -070049const char * const AudioParameter::valueListSeparator = AUDIO_PARAMETER_VALUE_LIST_SEPARATOR;
Dima Zavindb5cb142011-04-19 22:20:55 -070050
51AudioParameter::AudioParameter(const String8& keyValuePairs)
52{
53 char *str = new char[keyValuePairs.length()+1];
54 mKeyValuePairs = keyValuePairs;
seunghak.hanff7455c2013-09-07 14:59:43 +090055 char *last;
Dima Zavindb5cb142011-04-19 22:20:55 -070056
57 strcpy(str, keyValuePairs.string());
seunghak.hanff7455c2013-09-07 14:59:43 +090058 char *pair = strtok_r(str, ";", &last);
Dima Zavindb5cb142011-04-19 22:20:55 -070059 while (pair != NULL) {
60 if (strlen(pair) != 0) {
61 size_t eqIdx = strcspn(pair, "=");
62 String8 key = String8(pair, eqIdx);
63 String8 value;
64 if (eqIdx == strlen(pair)) {
65 value = String8("");
66 } else {
67 value = String8(pair + eqIdx + 1);
68 }
69 if (mParameters.indexOfKey(key) < 0) {
70 mParameters.add(key, value);
71 } else {
72 mParameters.replaceValueFor(key, value);
73 }
74 } else {
Steve Block3856b092011-10-20 11:56:00 +010075 ALOGV("AudioParameter() cstor empty key value pair");
Dima Zavindb5cb142011-04-19 22:20:55 -070076 }
seunghak.hanff7455c2013-09-07 14:59:43 +090077 pair = strtok_r(NULL, ";", &last);
Dima Zavindb5cb142011-04-19 22:20:55 -070078 }
79
80 delete[] str;
81}
82
83AudioParameter::~AudioParameter()
84{
85 mParameters.clear();
86}
87
Mikhail Naganovaad28fc2016-11-14 14:56:09 -080088String8 AudioParameter::toStringImpl(bool useValues) const
Dima Zavindb5cb142011-04-19 22:20:55 -070089{
90 String8 str = String8("");
91
92 size_t size = mParameters.size();
93 for (size_t i = 0; i < size; i++) {
94 str += mParameters.keyAt(i);
Mikhail Naganovaad28fc2016-11-14 14:56:09 -080095 if (useValues) {
96 str += "=";
97 str += mParameters.valueAt(i);
98 }
Dima Zavindb5cb142011-04-19 22:20:55 -070099 if (i < (size - 1)) str += ";";
100 }
101 return str;
102}
103
104status_t AudioParameter::add(const String8& key, const String8& value)
105{
106 if (mParameters.indexOfKey(key) < 0) {
107 mParameters.add(key, value);
108 return NO_ERROR;
109 } else {
110 mParameters.replaceValueFor(key, value);
111 return ALREADY_EXISTS;
112 }
113}
114
Mikhail Naganovaad28fc2016-11-14 14:56:09 -0800115status_t AudioParameter::addKey(const String8& key)
116{
117 return add(key, String8());
118}
119
Dima Zavindb5cb142011-04-19 22:20:55 -0700120status_t AudioParameter::addInt(const String8& key, const int value)
121{
122 char str[12];
123 if (snprintf(str, 12, "%d", value) > 0) {
124 String8 str8 = String8(str);
125 return add(key, str8);
126 } else {
127 return BAD_VALUE;
128 }
129}
130
131status_t AudioParameter::addFloat(const String8& key, const float value)
132{
133 char str[23];
134 if (snprintf(str, 23, "%.10f", value) > 0) {
135 String8 str8 = String8(str);
136 return add(key, str8);
137 } else {
138 return BAD_VALUE;
139 }
140}
141
142status_t AudioParameter::remove(const String8& key)
143{
144 if (mParameters.indexOfKey(key) >= 0) {
145 mParameters.removeItem(key);
146 return NO_ERROR;
147 } else {
148 return BAD_VALUE;
149 }
150}
151
Mikhail Naganov913d06c2016-11-01 12:49:22 -0700152status_t AudioParameter::get(const String8& key, String8& value) const
Dima Zavindb5cb142011-04-19 22:20:55 -0700153{
154 if (mParameters.indexOfKey(key) >= 0) {
155 value = mParameters.valueFor(key);
156 return NO_ERROR;
157 } else {
158 return BAD_VALUE;
159 }
160}
161
Mikhail Naganov913d06c2016-11-01 12:49:22 -0700162status_t AudioParameter::getInt(const String8& key, int& value) const
Dima Zavindb5cb142011-04-19 22:20:55 -0700163{
164 String8 str8;
165 status_t result = get(key, str8);
166 value = 0;
167 if (result == NO_ERROR) {
168 int val;
169 if (sscanf(str8.string(), "%d", &val) == 1) {
170 value = val;
171 } else {
172 result = INVALID_OPERATION;
173 }
174 }
175 return result;
176}
177
Mikhail Naganov913d06c2016-11-01 12:49:22 -0700178status_t AudioParameter::getFloat(const String8& key, float& value) const
Dima Zavindb5cb142011-04-19 22:20:55 -0700179{
180 String8 str8;
181 status_t result = get(key, str8);
182 value = 0;
183 if (result == NO_ERROR) {
184 float val;
185 if (sscanf(str8.string(), "%f", &val) == 1) {
186 value = val;
187 } else {
188 result = INVALID_OPERATION;
189 }
190 }
191 return result;
192}
193
Mikhail Naganovf558e022016-11-14 17:45:17 -0800194status_t AudioParameter::getAt(size_t index, String8& key) const
195{
196 if (mParameters.size() > index) {
197 key = mParameters.keyAt(index);
198 return NO_ERROR;
199 } else {
200 return BAD_VALUE;
201 }
202}
203
Mikhail Naganov913d06c2016-11-01 12:49:22 -0700204status_t AudioParameter::getAt(size_t index, String8& key, String8& value) const
Dima Zavindb5cb142011-04-19 22:20:55 -0700205{
206 if (mParameters.size() > index) {
207 key = mParameters.keyAt(index);
208 value = mParameters.valueAt(index);
209 return NO_ERROR;
210 } else {
211 return BAD_VALUE;
212 }
213}
214
Glenn Kasten40bc9062015-03-20 09:09:33 -0700215} // namespace android