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