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