Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 1 | /* ALSAStreamOps.cpp |
| 2 | ** |
| 3 | ** Copyright 2008-2009 Wind River Systems |
| 4 | ** Copyright (c) 2011, Code Aurora Forum. All rights reserved. |
| 5 | ** |
| 6 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | ** you may not use this file except in compliance with the License. |
| 8 | ** You may obtain a copy of the License at |
| 9 | ** |
| 10 | ** http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | ** |
| 12 | ** Unless required by applicable law or agreed to in writing, software |
| 13 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | ** See the License for the specific language governing permissions and |
| 16 | ** limitations under the License. |
| 17 | */ |
| 18 | |
| 19 | #include <errno.h> |
| 20 | #include <stdarg.h> |
| 21 | #include <sys/stat.h> |
| 22 | #include <fcntl.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <unistd.h> |
| 25 | #include <dlfcn.h> |
| 26 | |
Ajay Dudani | 9746c47 | 2012-06-18 16:01:16 -0700 | [diff] [blame] | 27 | #define LOG_TAG "ALSAStreamOps" |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 28 | //#define LOG_NDEBUG 0 |
Ajay Dudani | 9746c47 | 2012-06-18 16:01:16 -0700 | [diff] [blame] | 29 | #define LOG_NDDEBUG 0 |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 30 | #include <utils/Log.h> |
| 31 | #include <utils/String8.h> |
| 32 | |
| 33 | #include <cutils/properties.h> |
| 34 | #include <media/AudioRecord.h> |
| 35 | #include <hardware_legacy/power.h> |
| 36 | |
| 37 | #include "AudioHardwareALSA.h" |
| 38 | |
| 39 | namespace android_audio_legacy |
| 40 | { |
| 41 | |
| 42 | // ---------------------------------------------------------------------------- |
| 43 | |
| 44 | ALSAStreamOps::ALSAStreamOps(AudioHardwareALSA *parent, alsa_handle_t *handle) : |
| 45 | mParent(parent), |
| 46 | mHandle(handle) |
| 47 | { |
| 48 | } |
| 49 | |
| 50 | ALSAStreamOps::~ALSAStreamOps() |
| 51 | { |
| 52 | Mutex::Autolock autoLock(mParent->mLock); |
| 53 | |
| 54 | if((!strcmp(mHandle->useCase, SND_USE_CASE_VERB_IP_VOICECALL)) || |
| 55 | (!strcmp(mHandle->useCase, SND_USE_CASE_MOD_PLAY_VOIP))) { |
| 56 | if((mParent->mVoipStreamCount)) { |
| 57 | mParent->mVoipStreamCount--; |
| 58 | if(mParent->mVoipStreamCount > 0) { |
Iliyan Malchev | 4113f34 | 2012-06-11 14:39:47 -0700 | [diff] [blame] | 59 | ALOGD("ALSAStreamOps::close() Ignore"); |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 60 | return ; |
| 61 | } |
| 62 | } |
| 63 | mParent->mVoipStreamCount = 0; |
| 64 | mParent->mVoipMicMute = 0; |
| 65 | mParent->mVoipBitRate = 0; |
| 66 | } |
| 67 | close(); |
| 68 | |
| 69 | for(ALSAHandleList::iterator it = mParent->mDeviceList.begin(); |
| 70 | it != mParent->mDeviceList.end(); ++it) { |
| 71 | if (mHandle == &(*it)) { |
| 72 | it->useCase[0] = 0; |
| 73 | mParent->mDeviceList.erase(it); |
| 74 | break; |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | // use emulated popcount optimization |
| 80 | // http://www.df.lth.se/~john_e/gems/gem002d.html |
| 81 | static inline uint32_t popCount(uint32_t u) |
| 82 | { |
| 83 | u = ((u&0x55555555) + ((u>>1)&0x55555555)); |
| 84 | u = ((u&0x33333333) + ((u>>2)&0x33333333)); |
| 85 | u = ((u&0x0f0f0f0f) + ((u>>4)&0x0f0f0f0f)); |
| 86 | u = ((u&0x00ff00ff) + ((u>>8)&0x00ff00ff)); |
| 87 | u = ( u&0x0000ffff) + (u>>16); |
| 88 | return u; |
| 89 | } |
| 90 | |
| 91 | status_t ALSAStreamOps::set(int *format, |
| 92 | uint32_t *channels, |
| 93 | uint32_t *rate, |
| 94 | uint32_t device) |
| 95 | { |
| 96 | mDevices = device; |
| 97 | if (channels && *channels != 0) { |
| 98 | if (mHandle->channels != popCount(*channels)) |
| 99 | return BAD_VALUE; |
| 100 | } else if (channels) { |
| 101 | *channels = 0; |
| 102 | if (mHandle->devices & AudioSystem::DEVICE_OUT_ALL) { |
| 103 | switch(mHandle->channels) { |
| 104 | case 4: |
| 105 | *channels |= AudioSystem::CHANNEL_OUT_BACK_LEFT; |
| 106 | *channels |= AudioSystem::CHANNEL_OUT_BACK_RIGHT; |
| 107 | // Fall through... |
| 108 | default: |
| 109 | case 2: |
| 110 | *channels |= AudioSystem::CHANNEL_OUT_FRONT_RIGHT; |
| 111 | // Fall through... |
| 112 | case 1: |
| 113 | *channels |= AudioSystem::CHANNEL_OUT_FRONT_LEFT; |
| 114 | break; |
| 115 | } |
| 116 | } else { |
| 117 | switch(mHandle->channels) { |
Ajay Dudani | 9746c47 | 2012-06-18 16:01:16 -0700 | [diff] [blame] | 118 | #ifdef QCOM_SSR_ENABLED |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 119 | // For 5.1 recording |
| 120 | case 6 : |
| 121 | *channels |= AudioSystem::CHANNEL_IN_5POINT1; |
| 122 | break; |
| 123 | #endif |
| 124 | // Do not fall through... |
| 125 | default: |
| 126 | case 2: |
| 127 | *channels |= AudioSystem::CHANNEL_IN_RIGHT; |
| 128 | // Fall through... |
| 129 | case 1: |
| 130 | *channels |= AudioSystem::CHANNEL_IN_LEFT; |
| 131 | break; |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | if (rate && *rate > 0) { |
| 137 | if (mHandle->sampleRate != *rate) |
| 138 | return BAD_VALUE; |
| 139 | } else if (rate) { |
| 140 | *rate = mHandle->sampleRate; |
| 141 | } |
| 142 | |
| 143 | snd_pcm_format_t iformat = mHandle->format; |
| 144 | |
| 145 | if (format) { |
| 146 | switch(*format) { |
| 147 | case AudioSystem::FORMAT_DEFAULT: |
| 148 | break; |
| 149 | |
| 150 | case AudioSystem::PCM_16_BIT: |
| 151 | iformat = SNDRV_PCM_FORMAT_S16_LE; |
| 152 | break; |
| 153 | case AudioSystem::AMR_NB: |
| 154 | case AudioSystem::AMR_WB: |
Ajay Dudani | 9746c47 | 2012-06-18 16:01:16 -0700 | [diff] [blame] | 155 | #ifdef QCOM_QCHAT_ENABLED |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 156 | case AudioSystem::EVRC: |
| 157 | case AudioSystem::EVRCB: |
| 158 | case AudioSystem::EVRCWB: |
| 159 | #endif |
| 160 | iformat = *format; |
| 161 | break; |
| 162 | |
| 163 | case AudioSystem::PCM_8_BIT: |
| 164 | iformat = SNDRV_PCM_FORMAT_S8; |
| 165 | break; |
| 166 | |
| 167 | default: |
Iliyan Malchev | 4113f34 | 2012-06-11 14:39:47 -0700 | [diff] [blame] | 168 | ALOGE("Unknown PCM format %i. Forcing default", *format); |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 169 | break; |
| 170 | } |
| 171 | |
| 172 | if (mHandle->format != iformat) |
| 173 | return BAD_VALUE; |
| 174 | |
| 175 | switch(iformat) { |
| 176 | case SNDRV_PCM_FORMAT_S16_LE: |
| 177 | *format = AudioSystem::PCM_16_BIT; |
| 178 | break; |
| 179 | case SNDRV_PCM_FORMAT_S8: |
| 180 | *format = AudioSystem::PCM_8_BIT; |
| 181 | break; |
| 182 | default: |
| 183 | break; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | return NO_ERROR; |
| 188 | } |
| 189 | |
| 190 | status_t ALSAStreamOps::setParameters(const String8& keyValuePairs) |
| 191 | { |
| 192 | AudioParameter param = AudioParameter(keyValuePairs); |
| 193 | String8 key = String8(AudioParameter::keyRouting); |
| 194 | int device; |
ty.lee | 74060de | 2012-08-02 00:47:00 +0900 | [diff] [blame] | 195 | |
| 196 | #ifdef SEPERATED_AUDIO_INPUT |
| 197 | String8 key_input = String8(AudioParameter::keyInputSource); |
| 198 | int source; |
| 199 | |
| 200 | if (param.getInt(key_input, source) == NO_ERROR) { |
| 201 | ALOGD("setParameters(), input_source = %d", source); |
| 202 | mParent->mALSADevice->setInput(source); |
| 203 | param.remove(key_input); |
| 204 | } |
| 205 | #endif |
| 206 | |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 207 | if (param.getInt(key, device) == NO_ERROR) { |
| 208 | // Ignore routing if device is 0. |
Iliyan Malchev | 4113f34 | 2012-06-11 14:39:47 -0700 | [diff] [blame] | 209 | ALOGD("setParameters(): keyRouting with device %d", device); |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 210 | mDevices = device; |
| 211 | if(device) { |
| 212 | mParent->doRouting(device); |
| 213 | } |
| 214 | param.remove(key); |
| 215 | } |
Ajay Dudani | 9746c47 | 2012-06-18 16:01:16 -0700 | [diff] [blame] | 216 | #ifdef QCOM_FM_ENABLED |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 217 | else { |
| 218 | key = String8(AudioParameter::keyHandleFm); |
| 219 | if (param.getInt(key, device) == NO_ERROR) { |
Iliyan Malchev | 4113f34 | 2012-06-11 14:39:47 -0700 | [diff] [blame] | 220 | ALOGD("setParameters(): handleFm with device %d", device); |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 221 | mDevices = device; |
| 222 | if(device) { |
| 223 | mParent->handleFm(device); |
| 224 | } |
| 225 | param.remove(key); |
| 226 | } |
| 227 | } |
| 228 | #endif |
| 229 | |
| 230 | return NO_ERROR; |
| 231 | } |
| 232 | |
| 233 | String8 ALSAStreamOps::getParameters(const String8& keys) |
| 234 | { |
| 235 | AudioParameter param = AudioParameter(keys); |
| 236 | String8 value; |
| 237 | String8 key = String8(AudioParameter::keyRouting); |
| 238 | |
| 239 | if (param.get(key, value) == NO_ERROR) { |
| 240 | param.addInt(key, (int)mDevices); |
| 241 | } |
| 242 | else { |
Ajay Dudani | 9746c47 | 2012-06-18 16:01:16 -0700 | [diff] [blame] | 243 | #ifdef QCOM_VOIP_ENABLED |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 244 | key = String8(AudioParameter::keyVoipCheck); |
| 245 | if (param.get(key, value) == NO_ERROR) { |
| 246 | if((!strncmp(mHandle->useCase, SND_USE_CASE_VERB_IP_VOICECALL, strlen(SND_USE_CASE_VERB_IP_VOICECALL))) || |
| 247 | (!strncmp(mHandle->useCase, SND_USE_CASE_MOD_PLAY_VOIP, strlen(SND_USE_CASE_MOD_PLAY_VOIP)))) |
| 248 | param.addInt(key, true); |
| 249 | else |
| 250 | param.addInt(key, false); |
| 251 | } |
Iliyan Malchev | 4113f34 | 2012-06-11 14:39:47 -0700 | [diff] [blame] | 252 | #endif |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 253 | } |
Iliyan Malchev | 4113f34 | 2012-06-11 14:39:47 -0700 | [diff] [blame] | 254 | ALOGV("getParameters() %s", param.toString().string()); |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 255 | return param.toString(); |
| 256 | } |
| 257 | |
| 258 | uint32_t ALSAStreamOps::sampleRate() const |
| 259 | { |
| 260 | return mHandle->sampleRate; |
| 261 | } |
| 262 | |
| 263 | // |
| 264 | // Return the number of bytes (not frames) |
| 265 | // |
| 266 | size_t ALSAStreamOps::bufferSize() const |
| 267 | { |
Iliyan Malchev | 4113f34 | 2012-06-11 14:39:47 -0700 | [diff] [blame] | 268 | ALOGV("bufferSize() returns %d", mHandle->bufferSize); |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 269 | return mHandle->bufferSize; |
| 270 | } |
| 271 | |
| 272 | int ALSAStreamOps::format() const |
| 273 | { |
| 274 | int audioSystemFormat; |
| 275 | |
| 276 | snd_pcm_format_t ALSAFormat = mHandle->format; |
| 277 | |
| 278 | switch(ALSAFormat) { |
| 279 | case SNDRV_PCM_FORMAT_S8: |
| 280 | audioSystemFormat = AudioSystem::PCM_8_BIT; |
| 281 | break; |
| 282 | |
| 283 | case AudioSystem::AMR_NB: |
| 284 | case AudioSystem::AMR_WB: |
Ajay Dudani | 9746c47 | 2012-06-18 16:01:16 -0700 | [diff] [blame] | 285 | #ifdef QCOM_QCHAT_ENABLED |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 286 | case AudioSystem::EVRC: |
| 287 | case AudioSystem::EVRCB: |
| 288 | case AudioSystem::EVRCWB: |
| 289 | #endif |
| 290 | audioSystemFormat = mHandle->format; |
| 291 | break; |
| 292 | case SNDRV_PCM_FORMAT_S16_LE: |
| 293 | audioSystemFormat = AudioSystem::PCM_16_BIT; |
| 294 | break; |
| 295 | |
| 296 | default: |
| 297 | LOG_FATAL("Unknown AudioSystem bit width %d!", audioSystemFormat); |
| 298 | audioSystemFormat = AudioSystem::PCM_16_BIT; |
| 299 | break; |
| 300 | } |
| 301 | |
Ajay Dudani | 86c852b | 2012-07-19 15:28:45 -0700 | [diff] [blame] | 302 | #if LOCAL_LOGD |
Iliyan Malchev | 4113f34 | 2012-06-11 14:39:47 -0700 | [diff] [blame] | 303 | ALOGD("ALSAFormat:0x%x,audioSystemFormat:0x%x",ALSAFormat,audioSystemFormat); |
Ajay Dudani | 86c852b | 2012-07-19 15:28:45 -0700 | [diff] [blame] | 304 | #endif |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 305 | return audioSystemFormat; |
| 306 | } |
| 307 | |
| 308 | uint32_t ALSAStreamOps::channels() const |
| 309 | { |
| 310 | unsigned int count = mHandle->channels; |
| 311 | uint32_t channels = 0; |
| 312 | |
| 313 | if (mDevices & AudioSystem::DEVICE_OUT_ALL) |
| 314 | switch(count) { |
| 315 | case 4: |
| 316 | channels |= AudioSystem::CHANNEL_OUT_BACK_LEFT; |
| 317 | channels |= AudioSystem::CHANNEL_OUT_BACK_RIGHT; |
| 318 | // Fall through... |
| 319 | default: |
| 320 | case 2: |
| 321 | channels |= AudioSystem::CHANNEL_OUT_FRONT_RIGHT; |
| 322 | // Fall through... |
| 323 | case 1: |
| 324 | channels |= AudioSystem::CHANNEL_OUT_FRONT_LEFT; |
| 325 | break; |
| 326 | } |
| 327 | else |
| 328 | switch(count) { |
Ajay Dudani | 9746c47 | 2012-06-18 16:01:16 -0700 | [diff] [blame] | 329 | #ifdef QCOM_SSR_ENABLED |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 330 | // For 5.1 recording |
| 331 | case 6 : |
| 332 | channels |= AudioSystem::CHANNEL_IN_5POINT1; |
| 333 | break; |
| 334 | // Do not fall through... |
| 335 | #endif |
| 336 | default: |
| 337 | case 2: |
| 338 | channels |= AudioSystem::CHANNEL_IN_RIGHT; |
| 339 | // Fall through... |
| 340 | case 1: |
| 341 | channels |= AudioSystem::CHANNEL_IN_LEFT; |
| 342 | break; |
| 343 | } |
| 344 | |
| 345 | return channels; |
| 346 | } |
| 347 | |
| 348 | void ALSAStreamOps::close() |
| 349 | { |
Iliyan Malchev | 4113f34 | 2012-06-11 14:39:47 -0700 | [diff] [blame] | 350 | ALOGD("close"); |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 351 | if((!strncmp(mHandle->useCase, SND_USE_CASE_VERB_IP_VOICECALL, strlen(SND_USE_CASE_VERB_IP_VOICECALL))) || |
| 352 | (!strncmp(mHandle->useCase, SND_USE_CASE_MOD_PLAY_VOIP, strlen(SND_USE_CASE_MOD_PLAY_VOIP)))) { |
| 353 | mParent->mVoipMicMute = false; |
| 354 | mParent->mVoipBitRate = 0; |
| 355 | mParent->mVoipStreamCount = 0; |
| 356 | } |
| 357 | mParent->mALSADevice->close(mHandle); |
| 358 | } |
| 359 | |
| 360 | // |
| 361 | // Set playback or capture PCM device. It's possible to support audio output |
| 362 | // or input from multiple devices by using the ALSA plugins, but this is |
| 363 | // not supported for simplicity. |
| 364 | // |
| 365 | // The AudioHardwareALSA API does not allow one to set the input routing. |
| 366 | // |
| 367 | // If the "routes" value does not map to a valid device, the default playback |
| 368 | // device is used. |
| 369 | // |
| 370 | status_t ALSAStreamOps::open(int mode) |
| 371 | { |
Iliyan Malchev | 4113f34 | 2012-06-11 14:39:47 -0700 | [diff] [blame] | 372 | ALOGD("open"); |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 373 | return mParent->mALSADevice->open(mHandle); |
| 374 | } |
| 375 | |
| 376 | } // namespace androidi_audio_legacy |