AudioParameter.cpp revision db5cb14318bb24cd6ea14ff7ceea0d5e1f83d903
1/* 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> 23 24namespace android { 25 26const char *AudioParameter::keyRouting = "routing"; 27const char *AudioParameter::keySamplingRate = "sampling_rate"; 28const char *AudioParameter::keyFormat = "format"; 29const char *AudioParameter::keyChannels = "channels"; 30const char *AudioParameter::keyFrameCount = "frame_count"; 31const char *AudioParameter::keyInputSource = "input_source"; 32 33AudioParameter::AudioParameter(const String8& keyValuePairs) 34{ 35 char *str = new char[keyValuePairs.length()+1]; 36 mKeyValuePairs = keyValuePairs; 37 38 strcpy(str, keyValuePairs.string()); 39 char *pair = strtok(str, ";"); 40 while (pair != NULL) { 41 if (strlen(pair) != 0) { 42 size_t eqIdx = strcspn(pair, "="); 43 String8 key = String8(pair, eqIdx); 44 String8 value; 45 if (eqIdx == strlen(pair)) { 46 value = String8(""); 47 } else { 48 value = String8(pair + eqIdx + 1); 49 } 50 if (mParameters.indexOfKey(key) < 0) { 51 mParameters.add(key, value); 52 } else { 53 mParameters.replaceValueFor(key, value); 54 } 55 } else { 56 LOGV("AudioParameter() cstor empty key value pair"); 57 } 58 pair = strtok(NULL, ";"); 59 } 60 61 delete[] str; 62} 63 64AudioParameter::~AudioParameter() 65{ 66 mParameters.clear(); 67} 68 69String8 AudioParameter::toString() 70{ 71 String8 str = String8(""); 72 73 size_t size = mParameters.size(); 74 for (size_t i = 0; i < size; i++) { 75 str += mParameters.keyAt(i); 76 str += "="; 77 str += mParameters.valueAt(i); 78 if (i < (size - 1)) str += ";"; 79 } 80 return str; 81} 82 83status_t AudioParameter::add(const String8& key, const String8& value) 84{ 85 if (mParameters.indexOfKey(key) < 0) { 86 mParameters.add(key, value); 87 return NO_ERROR; 88 } else { 89 mParameters.replaceValueFor(key, value); 90 return ALREADY_EXISTS; 91 } 92} 93 94status_t AudioParameter::addInt(const String8& key, const int value) 95{ 96 char str[12]; 97 if (snprintf(str, 12, "%d", value) > 0) { 98 String8 str8 = String8(str); 99 return add(key, str8); 100 } else { 101 return BAD_VALUE; 102 } 103} 104 105status_t AudioParameter::addFloat(const String8& key, const float value) 106{ 107 char str[23]; 108 if (snprintf(str, 23, "%.10f", value) > 0) { 109 String8 str8 = String8(str); 110 return add(key, str8); 111 } else { 112 return BAD_VALUE; 113 } 114} 115 116status_t AudioParameter::remove(const String8& key) 117{ 118 if (mParameters.indexOfKey(key) >= 0) { 119 mParameters.removeItem(key); 120 return NO_ERROR; 121 } else { 122 return BAD_VALUE; 123 } 124} 125 126status_t AudioParameter::get(const String8& key, String8& value) 127{ 128 if (mParameters.indexOfKey(key) >= 0) { 129 value = mParameters.valueFor(key); 130 return NO_ERROR; 131 } else { 132 return BAD_VALUE; 133 } 134} 135 136status_t AudioParameter::getInt(const String8& key, int& value) 137{ 138 String8 str8; 139 status_t result = get(key, str8); 140 value = 0; 141 if (result == NO_ERROR) { 142 int val; 143 if (sscanf(str8.string(), "%d", &val) == 1) { 144 value = val; 145 } else { 146 result = INVALID_OPERATION; 147 } 148 } 149 return result; 150} 151 152status_t AudioParameter::getFloat(const String8& key, float& value) 153{ 154 String8 str8; 155 status_t result = get(key, str8); 156 value = 0; 157 if (result == NO_ERROR) { 158 float val; 159 if (sscanf(str8.string(), "%f", &val) == 1) { 160 value = val; 161 } else { 162 result = INVALID_OPERATION; 163 } 164 } 165 return result; 166} 167 168status_t AudioParameter::getAt(size_t index, String8& key, String8& value) 169{ 170 if (mParameters.size() > index) { 171 key = mParameters.keyAt(index); 172 value = mParameters.valueAt(index); 173 return NO_ERROR; 174 } else { 175 return BAD_VALUE; 176 } 177} 178 179}; // namespace android 180