Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 1 | /* AudioStreamInALSA.cpp |
| 2 | ** |
| 3 | ** Copyright 2008-2009 Wind River Systems |
| 4 | ** Copyright (c) 2011-2012, 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 "AudioStreamInALSA" |
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 | extern "C" { |
Ajay Dudani | 9746c47 | 2012-06-18 16:01:16 -0700 | [diff] [blame] | 40 | #ifdef QCOM_CSDCLIENT_ENABLED |
SathishKumar Mani | 5ff7a02 | 2012-09-14 11:36:35 -0700 | [diff] [blame] | 41 | static int (*csd_start_record)(int); |
| 42 | static int (*csd_stop_record)(void); |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 43 | #endif |
SathishKumar Mani | 5ff7a02 | 2012-09-14 11:36:35 -0700 | [diff] [blame] | 44 | |
Ajay Dudani | 9746c47 | 2012-06-18 16:01:16 -0700 | [diff] [blame] | 45 | #ifdef QCOM_SSR_ENABLED |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 46 | #include "surround_filters_interface.h" |
| 47 | #endif |
| 48 | } |
| 49 | |
| 50 | namespace android_audio_legacy |
| 51 | { |
Ajay Dudani | 9746c47 | 2012-06-18 16:01:16 -0700 | [diff] [blame] | 52 | #ifdef QCOM_SSR_ENABLED |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 53 | #define SURROUND_FILE_1R "/system/etc/surround_sound/filter1r.pcm" |
| 54 | #define SURROUND_FILE_2R "/system/etc/surround_sound/filter2r.pcm" |
| 55 | #define SURROUND_FILE_3R "/system/etc/surround_sound/filter3r.pcm" |
| 56 | #define SURROUND_FILE_4R "/system/etc/surround_sound/filter4r.pcm" |
| 57 | |
| 58 | #define SURROUND_FILE_1I "/system/etc/surround_sound/filter1i.pcm" |
| 59 | #define SURROUND_FILE_2I "/system/etc/surround_sound/filter2i.pcm" |
| 60 | #define SURROUND_FILE_3I "/system/etc/surround_sound/filter3i.pcm" |
| 61 | #define SURROUND_FILE_4I "/system/etc/surround_sound/filter4i.pcm" |
| 62 | |
| 63 | // Use AAC/DTS channel mapping as default channel mapping: C,FL,FR,Ls,Rs,LFE |
| 64 | const int chanMap[] = { 1, 2, 4, 3, 0, 5 }; |
| 65 | #endif |
| 66 | |
| 67 | AudioStreamInALSA::AudioStreamInALSA(AudioHardwareALSA *parent, |
| 68 | alsa_handle_t *handle, |
| 69 | AudioSystem::audio_in_acoustics audio_acoustics) : |
| 70 | ALSAStreamOps(parent, handle), |
| 71 | mFramesLost(0), |
SathishKumar Mani | 5ff7a02 | 2012-09-14 11:36:35 -0700 | [diff] [blame] | 72 | mAcoustics(audio_acoustics), |
| 73 | mParent(parent) |
Ajay Dudani | 9746c47 | 2012-06-18 16:01:16 -0700 | [diff] [blame] | 74 | #ifdef QCOM_SSR_ENABLED |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 75 | , mFp_4ch(NULL), |
| 76 | mFp_6ch(NULL), |
| 77 | mRealCoeffs(NULL), |
| 78 | mImagCoeffs(NULL), |
| 79 | mSurroundObj(NULL), |
| 80 | mSurroundOutputBuffer(NULL), |
| 81 | mSurroundInputBuffer(NULL), |
| 82 | mSurroundOutputBufferIdx(0), |
| 83 | mSurroundInputBufferIdx(0) |
| 84 | #endif |
| 85 | { |
Ajay Dudani | 9746c47 | 2012-06-18 16:01:16 -0700 | [diff] [blame] | 86 | #ifdef QCOM_SSR_ENABLED |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 87 | char c_multi_ch_dump[128] = {0}; |
| 88 | status_t err = NO_ERROR; |
| 89 | |
| 90 | // Call surround sound library init if device is Surround Sound |
| 91 | if ( handle->channels == 6) { |
| 92 | if (!strncmp(handle->useCase, SND_USE_CASE_VERB_HIFI_REC, strlen(SND_USE_CASE_VERB_HIFI_REC)) |
| 93 | || !strncmp(handle->useCase, SND_USE_CASE_MOD_CAPTURE_MUSIC, strlen(SND_USE_CASE_MOD_CAPTURE_MUSIC))) { |
| 94 | |
| 95 | err = initSurroundSoundLibrary(handle->bufferSize); |
| 96 | if ( NO_ERROR != err) { |
Iliyan Malchev | 4113f34 | 2012-06-11 14:39:47 -0700 | [diff] [blame] | 97 | ALOGE("initSurroundSoundLibrary failed: %d handle->bufferSize:%d", err,handle->bufferSize); |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | property_get("ssr.pcmdump",c_multi_ch_dump,"0"); |
| 101 | if (0 == strncmp("true",c_multi_ch_dump, sizeof("ssr.dump-pcm"))) { |
| 102 | //Remember to change file system permission of data(e.g. chmod 777 data/), |
| 103 | //otherwise, fopen may fail. |
| 104 | if ( !mFp_4ch) |
| 105 | mFp_4ch = fopen("/data/4ch_ssr.pcm", "wb"); |
| 106 | if ( !mFp_6ch) |
| 107 | mFp_6ch = fopen("/data/6ch_ssr.pcm", "wb"); |
| 108 | if ((!mFp_4ch) || (!mFp_6ch)) |
Iliyan Malchev | 4113f34 | 2012-06-11 14:39:47 -0700 | [diff] [blame] | 109 | ALOGE("mfp_4ch or mfp_6ch open failed: mfp_4ch:%p mfp_6ch:%p",mFp_4ch,mFp_6ch); |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 110 | } |
| 111 | } |
| 112 | } |
| 113 | #endif |
| 114 | } |
| 115 | |
| 116 | AudioStreamInALSA::~AudioStreamInALSA() |
| 117 | { |
| 118 | close(); |
| 119 | } |
| 120 | |
| 121 | status_t AudioStreamInALSA::setGain(float gain) |
| 122 | { |
| 123 | return 0; //mixer() ? mixer()->setMasterGain(gain) : (status_t)NO_INIT; |
| 124 | } |
| 125 | |
| 126 | ssize_t AudioStreamInALSA::read(void *buffer, ssize_t bytes) |
| 127 | { |
| 128 | int period_size; |
| 129 | |
Iliyan Malchev | 4113f34 | 2012-06-11 14:39:47 -0700 | [diff] [blame] | 130 | ALOGV("read:: buffer %p, bytes %d", buffer, bytes); |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 131 | |
| 132 | int n; |
| 133 | status_t err; |
SathishKumar Mani | 5ff7a02 | 2012-09-14 11:36:35 -0700 | [diff] [blame] | 134 | ssize_t read = 0; |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 135 | char *use_case; |
| 136 | int newMode = mParent->mode(); |
| 137 | |
| 138 | if((mHandle->handle == NULL) && (mHandle->rxHandle == NULL) && |
| 139 | (strcmp(mHandle->useCase, SND_USE_CASE_VERB_IP_VOICECALL)) && |
| 140 | (strcmp(mHandle->useCase, SND_USE_CASE_MOD_PLAY_VOIP))) { |
| 141 | mParent->mLock.lock(); |
| 142 | snd_use_case_get(mHandle->ucMgr, "_verb", (const char **)&use_case); |
| 143 | if ((use_case != NULL) && (strcmp(use_case, SND_USE_CASE_VERB_INACTIVE))) { |
| 144 | if ((mHandle->devices == AudioSystem::DEVICE_IN_VOICE_CALL) && |
| 145 | (newMode == AudioSystem::MODE_IN_CALL)) { |
Iliyan Malchev | 4113f34 | 2012-06-11 14:39:47 -0700 | [diff] [blame] | 146 | ALOGD("read:: mParent->mIncallMode=%d", mParent->mIncallMode); |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 147 | if ((mParent->mIncallMode & AudioSystem::CHANNEL_IN_VOICE_UPLINK) && |
| 148 | (mParent->mIncallMode & AudioSystem::CHANNEL_IN_VOICE_DNLINK)) { |
Ajay Dudani | 9746c47 | 2012-06-18 16:01:16 -0700 | [diff] [blame] | 149 | #ifdef QCOM_CSDCLIENT_ENABLED |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 150 | if (mParent->mFusion3Platform) { |
| 151 | mParent->mALSADevice->setVocRecMode(INCALL_REC_STEREO); |
| 152 | strlcpy(mHandle->useCase, SND_USE_CASE_MOD_CAPTURE_VOICE, |
| 153 | sizeof(mHandle->useCase)); |
SathishKumar Mani | 5ff7a02 | 2012-09-14 11:36:35 -0700 | [diff] [blame] | 154 | start_csd_record(INCALL_REC_STEREO); |
| 155 | } else |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 156 | #endif |
| 157 | { |
| 158 | strlcpy(mHandle->useCase, SND_USE_CASE_MOD_CAPTURE_VOICE_UL_DL, |
| 159 | sizeof(mHandle->useCase)); |
| 160 | } |
| 161 | } else if (mParent->mIncallMode & AudioSystem::CHANNEL_IN_VOICE_DNLINK) { |
Ajay Dudani | 9746c47 | 2012-06-18 16:01:16 -0700 | [diff] [blame] | 162 | #ifdef QCOM_CSDCLIENT_ENABLED |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 163 | if (mParent->mFusion3Platform) { |
| 164 | mParent->mALSADevice->setVocRecMode(INCALL_REC_MONO); |
| 165 | strlcpy(mHandle->useCase, SND_USE_CASE_MOD_CAPTURE_VOICE, |
| 166 | sizeof(mHandle->useCase)); |
SathishKumar Mani | 5ff7a02 | 2012-09-14 11:36:35 -0700 | [diff] [blame] | 167 | start_csd_record(INCALL_REC_MONO); |
| 168 | } else |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 169 | #endif |
| 170 | { |
| 171 | strlcpy(mHandle->useCase, SND_USE_CASE_MOD_CAPTURE_VOICE_DL, |
| 172 | sizeof(mHandle->useCase)); |
| 173 | } |
| 174 | } |
Ajay Dudani | 9291943 | 2012-06-28 14:23:11 -0700 | [diff] [blame] | 175 | #ifdef QCOM_FM_ENABLED |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 176 | } else if(mHandle->devices == AudioSystem::DEVICE_IN_FM_RX) { |
| 177 | strlcpy(mHandle->useCase, SND_USE_CASE_MOD_CAPTURE_FM, sizeof(mHandle->useCase)); |
| 178 | } else if (mHandle->devices == AudioSystem::DEVICE_IN_FM_RX_A2DP) { |
| 179 | strlcpy(mHandle->useCase, SND_USE_CASE_MOD_CAPTURE_A2DP_FM, sizeof(mHandle->useCase)); |
| 180 | #endif |
| 181 | } else if(!strcmp(mHandle->useCase, SND_USE_CASE_MOD_PLAY_VOIP)) { |
| 182 | strlcpy(mHandle->useCase, SND_USE_CASE_MOD_PLAY_VOIP, sizeof(mHandle->useCase)); |
SathishKumar Mani | 8861338 | 2012-08-13 18:40:18 -0700 | [diff] [blame] | 183 | } else { |
| 184 | char value[128]; |
| 185 | property_get("persist.audio.lowlatency.rec",value,"0"); |
| 186 | if (!strcmp("true", value)) { |
| 187 | strlcpy(mHandle->useCase, SND_USE_CASE_MOD_CAPTURE_LOWLATENCY_MUSIC, sizeof(mHandle->useCase)); |
| 188 | } else { |
| 189 | strlcpy(mHandle->useCase, SND_USE_CASE_MOD_CAPTURE_MUSIC, sizeof(mHandle->useCase)); |
| 190 | } |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 191 | } |
| 192 | } else { |
| 193 | if ((mHandle->devices == AudioSystem::DEVICE_IN_VOICE_CALL) && |
| 194 | (newMode == AudioSystem::MODE_IN_CALL)) { |
Iliyan Malchev | 4113f34 | 2012-06-11 14:39:47 -0700 | [diff] [blame] | 195 | ALOGD("read:: ---- mParent->mIncallMode=%d", mParent->mIncallMode); |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 196 | if ((mParent->mIncallMode & AudioSystem::CHANNEL_IN_VOICE_UPLINK) && |
| 197 | (mParent->mIncallMode & AudioSystem::CHANNEL_IN_VOICE_DNLINK)) { |
Ajay Dudani | 9746c47 | 2012-06-18 16:01:16 -0700 | [diff] [blame] | 198 | #ifdef QCOM_CSDCLIENT_ENABLED |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 199 | if (mParent->mFusion3Platform) { |
| 200 | mParent->mALSADevice->setVocRecMode(INCALL_REC_STEREO); |
| 201 | strlcpy(mHandle->useCase, SND_USE_CASE_VERB_INCALL_REC, |
| 202 | sizeof(mHandle->useCase)); |
SathishKumar Mani | 5ff7a02 | 2012-09-14 11:36:35 -0700 | [diff] [blame] | 203 | start_csd_record(INCALL_REC_STEREO); |
| 204 | } else |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 205 | #endif |
| 206 | { |
| 207 | strlcpy(mHandle->useCase, SND_USE_CASE_VERB_UL_DL_REC, |
| 208 | sizeof(mHandle->useCase)); |
| 209 | } |
| 210 | } else if (mParent->mIncallMode & AudioSystem::CHANNEL_IN_VOICE_DNLINK) { |
Ajay Dudani | 9746c47 | 2012-06-18 16:01:16 -0700 | [diff] [blame] | 211 | #ifdef QCOM_CSDCLIENT_ENABLED |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 212 | if (mParent->mFusion3Platform) { |
| 213 | mParent->mALSADevice->setVocRecMode(INCALL_REC_MONO); |
| 214 | strlcpy(mHandle->useCase, SND_USE_CASE_VERB_INCALL_REC, |
| 215 | sizeof(mHandle->useCase)); |
SathishKumar Mani | 5ff7a02 | 2012-09-14 11:36:35 -0700 | [diff] [blame] | 216 | start_csd_record(INCALL_REC_MONO); |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 217 | } else |
SathishKumar Mani | 5ff7a02 | 2012-09-14 11:36:35 -0700 | [diff] [blame] | 218 | #endif |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 219 | { |
| 220 | strlcpy(mHandle->useCase, SND_USE_CASE_VERB_DL_REC, |
| 221 | sizeof(mHandle->useCase)); |
| 222 | } |
| 223 | } |
Ajay Dudani | 9746c47 | 2012-06-18 16:01:16 -0700 | [diff] [blame] | 224 | #ifdef QCOM_FM_ENABLED |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 225 | } else if(mHandle->devices == AudioSystem::DEVICE_IN_FM_RX) { |
| 226 | strlcpy(mHandle->useCase, SND_USE_CASE_VERB_FM_REC, sizeof(mHandle->useCase)); |
| 227 | } else if (mHandle->devices == AudioSystem::DEVICE_IN_FM_RX_A2DP) { |
| 228 | strlcpy(mHandle->useCase, SND_USE_CASE_VERB_FM_A2DP_REC, sizeof(mHandle->useCase)); |
| 229 | #endif |
| 230 | } else if(!strcmp(mHandle->useCase, SND_USE_CASE_VERB_IP_VOICECALL)){ |
| 231 | strlcpy(mHandle->useCase, SND_USE_CASE_VERB_IP_VOICECALL, sizeof(mHandle->useCase)); |
| 232 | } else { |
SathishKumar Mani | 8861338 | 2012-08-13 18:40:18 -0700 | [diff] [blame] | 233 | char value[128]; |
| 234 | property_get("persist.audio.lowlatency.rec",value,"0"); |
| 235 | if (!strcmp("true", value)) { |
| 236 | strlcpy(mHandle->useCase, SND_USE_CASE_VERB_HIFI_LOWLATENCY_REC, sizeof(mHandle->useCase)); |
| 237 | } else { |
| 238 | strlcpy(mHandle->useCase, SND_USE_CASE_VERB_HIFI_REC, sizeof(mHandle->useCase)); |
| 239 | } |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 240 | } |
| 241 | } |
| 242 | free(use_case); |
| 243 | if((!strcmp(mHandle->useCase, SND_USE_CASE_VERB_IP_VOICECALL)) || |
| 244 | (!strcmp(mHandle->useCase, SND_USE_CASE_MOD_PLAY_VOIP))) { |
Ajay Dudani | 9746c47 | 2012-06-18 16:01:16 -0700 | [diff] [blame] | 245 | #ifdef QCOM_USBAUDIO_ENABLED |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 246 | if((mDevices & AudioSystem::DEVICE_IN_ANLG_DOCK_HEADSET) || |
| 247 | (mDevices & AudioSystem::DEVICE_OUT_ANLG_DOCK_HEADSET)) { |
| 248 | mHandle->module->route(mHandle, (mDevices | AudioSystem::DEVICE_IN_PROXY) , AudioSystem::MODE_IN_COMMUNICATION); |
| 249 | }else |
| 250 | #endif |
| 251 | { |
| 252 | mHandle->module->route(mHandle, mDevices , AudioSystem::MODE_IN_COMMUNICATION); |
| 253 | } |
| 254 | } else { |
Ajay Dudani | 9746c47 | 2012-06-18 16:01:16 -0700 | [diff] [blame] | 255 | #ifdef QCOM_USBAUDIO_ENABLED |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 256 | if((mHandle->devices == AudioSystem::DEVICE_IN_ANLG_DOCK_HEADSET)|| |
| 257 | (mHandle->devices == AudioSystem::DEVICE_OUT_ANLG_DOCK_HEADSET)){ |
| 258 | mHandle->module->route(mHandle, AudioSystem::DEVICE_IN_PROXY , mParent->mode()); |
| 259 | } else |
| 260 | #endif |
| 261 | { |
| 262 | |
| 263 | mHandle->module->route(mHandle, mDevices , mParent->mode()); |
| 264 | } |
| 265 | } |
| 266 | if (!strcmp(mHandle->useCase, SND_USE_CASE_VERB_HIFI_REC) || |
SathishKumar Mani | 8861338 | 2012-08-13 18:40:18 -0700 | [diff] [blame] | 267 | !strcmp(mHandle->useCase, SND_USE_CASE_VERB_HIFI_LOWLATENCY_REC) || |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 268 | !strcmp(mHandle->useCase, SND_USE_CASE_VERB_FM_REC) || |
| 269 | !strcmp(mHandle->useCase, SND_USE_CASE_VERB_IP_VOICECALL) || |
| 270 | !strcmp(mHandle->useCase, SND_USE_CASE_VERB_FM_A2DP_REC) || |
| 271 | !strcmp(mHandle->useCase, SND_USE_CASE_VERB_UL_DL_REC) || |
| 272 | !strcmp(mHandle->useCase, SND_USE_CASE_VERB_DL_REC) || |
| 273 | !strcmp(mHandle->useCase, SND_USE_CASE_VERB_INCALL_REC)) { |
| 274 | snd_use_case_set(mHandle->ucMgr, "_verb", mHandle->useCase); |
| 275 | } else { |
| 276 | snd_use_case_set(mHandle->ucMgr, "_enamod", mHandle->useCase); |
| 277 | } |
| 278 | if((!strcmp(mHandle->useCase, SND_USE_CASE_VERB_IP_VOICECALL)) || |
| 279 | (!strcmp(mHandle->useCase, SND_USE_CASE_MOD_PLAY_VOIP))) { |
| 280 | err = mHandle->module->startVoipCall(mHandle); |
| 281 | } |
| 282 | else |
| 283 | mHandle->module->open(mHandle); |
| 284 | if(mHandle->handle == NULL) { |
Iliyan Malchev | 4113f34 | 2012-06-11 14:39:47 -0700 | [diff] [blame] | 285 | ALOGE("read:: PCM device open failed"); |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 286 | mParent->mLock.unlock(); |
| 287 | |
| 288 | return 0; |
| 289 | } |
Ajay Dudani | 9746c47 | 2012-06-18 16:01:16 -0700 | [diff] [blame] | 290 | #ifdef QCOM_USBAUDIO_ENABLED |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 291 | if((mHandle->devices == AudioSystem::DEVICE_IN_ANLG_DOCK_HEADSET)|| |
| 292 | (mHandle->devices == AudioSystem::DEVICE_OUT_ANLG_DOCK_HEADSET)){ |
| 293 | if((!strcmp(mHandle->useCase, SND_USE_CASE_VERB_IP_VOICECALL)) || |
| 294 | (!strcmp(mHandle->useCase, SND_USE_CASE_MOD_PLAY_VOIP))) { |
| 295 | mParent->musbRecordingState |= USBRECBIT_VOIPCALL; |
| 296 | } else { |
| 297 | mParent->startUsbRecordingIfNotStarted(); |
| 298 | mParent->musbRecordingState |= USBRECBIT_REC; |
| 299 | } |
| 300 | } |
| 301 | #endif |
| 302 | mParent->mLock.unlock(); |
| 303 | } |
Ajay Dudani | 9746c47 | 2012-06-18 16:01:16 -0700 | [diff] [blame] | 304 | #ifdef QCOM_USBAUDIO_ENABLED |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 305 | if(((mDevices & AudioSystem::DEVICE_IN_ANLG_DOCK_HEADSET) || |
| 306 | (mDevices & AudioSystem::DEVICE_OUT_ANLG_DOCK_HEADSET)) && |
| 307 | (!mParent->musbRecordingState)) { |
| 308 | mParent->mLock.lock(); |
Iliyan Malchev | 4113f34 | 2012-06-11 14:39:47 -0700 | [diff] [blame] | 309 | ALOGD("Starting UsbRecording thread"); |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 310 | mParent->startUsbRecordingIfNotStarted(); |
| 311 | if(!strcmp(mHandle->useCase, SND_USE_CASE_VERB_IP_VOICECALL) || |
| 312 | !strcmp(mHandle->useCase, SND_USE_CASE_MOD_PLAY_VOIP)) { |
Iliyan Malchev | 4113f34 | 2012-06-11 14:39:47 -0700 | [diff] [blame] | 313 | ALOGD("Enabling voip recording bit"); |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 314 | mParent->musbRecordingState |= USBRECBIT_VOIPCALL; |
| 315 | }else{ |
Iliyan Malchev | 4113f34 | 2012-06-11 14:39:47 -0700 | [diff] [blame] | 316 | ALOGD("Enabling HiFi Recording bit"); |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 317 | mParent->musbRecordingState |= USBRECBIT_REC; |
| 318 | } |
| 319 | mParent->mLock.unlock(); |
| 320 | } |
| 321 | #endif |
| 322 | period_size = mHandle->periodSize; |
| 323 | int read_pending = bytes; |
| 324 | |
Ajay Dudani | 9746c47 | 2012-06-18 16:01:16 -0700 | [diff] [blame] | 325 | #ifdef QCOM_SSR_ENABLED |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 326 | if (mSurroundObj) { |
| 327 | int processed = 0; |
| 328 | int processed_pending; |
| 329 | int samples = bytes >> 1; |
| 330 | void *buffer_start = buffer; |
| 331 | int period_bytes = mHandle->handle->period_size; |
| 332 | int period_samples = period_bytes >> 1; |
| 333 | |
| 334 | do { |
| 335 | if (mSurroundOutputBufferIdx > 0) { |
Iliyan Malchev | 4113f34 | 2012-06-11 14:39:47 -0700 | [diff] [blame] | 336 | ALOGV("AudioStreamInALSA::read() - copy processed output " |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 337 | "to buffer, mSurroundOutputBufferIdx = %d", |
| 338 | mSurroundOutputBufferIdx); |
| 339 | // Copy processed output to buffer |
| 340 | processed_pending = mSurroundOutputBufferIdx; |
| 341 | if (processed_pending > (samples - processed)) { |
| 342 | processed_pending = (samples - processed); |
| 343 | } |
| 344 | memcpy(buffer, mSurroundOutputBuffer, processed_pending * sizeof(Word16)); |
| 345 | buffer += processed_pending * sizeof(Word16); |
| 346 | processed += processed_pending; |
| 347 | if (mSurroundOutputBufferIdx > processed_pending) { |
| 348 | // Shift leftover samples to beginning of the buffer |
| 349 | memcpy(&mSurroundOutputBuffer[0], |
| 350 | &mSurroundOutputBuffer[processed_pending], |
| 351 | (mSurroundOutputBufferIdx - processed_pending) * sizeof(Word16)); |
| 352 | } |
| 353 | mSurroundOutputBufferIdx -= processed_pending; |
| 354 | } |
| 355 | |
| 356 | if (processed >= samples) { |
Iliyan Malchev | 4113f34 | 2012-06-11 14:39:47 -0700 | [diff] [blame] | 357 | ALOGV("AudioStreamInALSA::read() - done processing buffer, " |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 358 | "processed = %d", processed); |
| 359 | // Done processing this buffer |
| 360 | break; |
| 361 | } |
| 362 | |
| 363 | // Fill input buffer until there is enough to process |
| 364 | read_pending = SSR_INPUT_FRAME_SIZE - mSurroundInputBufferIdx; |
| 365 | read = mSurroundInputBufferIdx; |
| 366 | while (mHandle->handle && read_pending > 0) { |
| 367 | n = pcm_read(mHandle->handle, &mSurroundInputBuffer[read], |
| 368 | period_bytes); |
Iliyan Malchev | 4113f34 | 2012-06-11 14:39:47 -0700 | [diff] [blame] | 369 | ALOGV("pcm_read() returned n = %d buffer:%p size:%d", n, &mSurroundInputBuffer[read], period_bytes); |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 370 | if (n && n != -EAGAIN) { |
| 371 | //Recovery part of pcm_read. TODO:split recovery. |
| 372 | return static_cast<ssize_t>(n); |
| 373 | } |
| 374 | else if (n < 0) { |
| 375 | // Recovery is part of pcm_write. TODO split is later. |
| 376 | return static_cast<ssize_t>(n); |
| 377 | } |
| 378 | else { |
| 379 | read_pending -= period_samples; |
| 380 | read += period_samples; |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | |
| 385 | if (mFp_4ch) { |
| 386 | fwrite( mSurroundInputBuffer, 1, |
| 387 | SSR_INPUT_FRAME_SIZE * sizeof(Word16), mFp_4ch); |
| 388 | } |
| 389 | |
| 390 | //apply ssr libs to conver 4ch to 6ch |
| 391 | surround_filters_intl_process(mSurroundObj, |
| 392 | &mSurroundOutputBuffer[mSurroundOutputBufferIdx], |
| 393 | (Word16 *)mSurroundInputBuffer); |
| 394 | |
| 395 | // Shift leftover samples to beginning of input buffer |
| 396 | if (read_pending < 0) { |
| 397 | memcpy(&mSurroundInputBuffer[0], |
| 398 | &mSurroundInputBuffer[SSR_INPUT_FRAME_SIZE], |
| 399 | (-read_pending) * sizeof(Word16)); |
| 400 | } |
| 401 | mSurroundInputBufferIdx = -read_pending; |
| 402 | |
| 403 | if (mFp_6ch) { |
| 404 | fwrite( &mSurroundOutputBuffer[mSurroundOutputBufferIdx], |
| 405 | 1, SSR_OUTPUT_FRAME_SIZE * sizeof(Word16), mFp_6ch); |
| 406 | } |
| 407 | |
| 408 | mSurroundOutputBufferIdx += SSR_OUTPUT_FRAME_SIZE; |
Iliyan Malchev | 4113f34 | 2012-06-11 14:39:47 -0700 | [diff] [blame] | 409 | ALOGV("do_while loop: processed=%d, samples=%d\n", processed, samples); |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 410 | } while (mHandle->handle && processed < samples); |
| 411 | read = processed * sizeof(Word16); |
| 412 | buffer = buffer_start; |
| 413 | } else |
Ajay Dudani | 9746c47 | 2012-06-18 16:01:16 -0700 | [diff] [blame] | 414 | #endif |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 415 | { |
| 416 | |
| 417 | do { |
| 418 | if (read_pending < period_size) { |
| 419 | read_pending = period_size; |
| 420 | } |
| 421 | |
| 422 | n = pcm_read(mHandle->handle, buffer, |
| 423 | period_size); |
Iliyan Malchev | 4113f34 | 2012-06-11 14:39:47 -0700 | [diff] [blame] | 424 | ALOGV("pcm_read() returned n = %d", n); |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 425 | if (n && (n == -EIO || n == -EAGAIN || n == -EPIPE || n == -EBADFD)) { |
| 426 | mParent->mLock.lock(); |
Iliyan Malchev | 4113f34 | 2012-06-11 14:39:47 -0700 | [diff] [blame] | 427 | ALOGW("pcm_read() returned error n %d, Recovering from error\n", n); |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 428 | pcm_close(mHandle->handle); |
| 429 | mHandle->handle = NULL; |
| 430 | if((!strncmp(mHandle->useCase, SND_USE_CASE_VERB_IP_VOICECALL, strlen(SND_USE_CASE_VERB_IP_VOICECALL))) || |
| 431 | (!strncmp(mHandle->useCase, SND_USE_CASE_MOD_PLAY_VOIP, strlen(SND_USE_CASE_MOD_PLAY_VOIP)))) { |
| 432 | pcm_close(mHandle->rxHandle); |
| 433 | mHandle->rxHandle = NULL; |
| 434 | mHandle->module->startVoipCall(mHandle); |
| 435 | } |
| 436 | else |
| 437 | mHandle->module->open(mHandle); |
| 438 | mParent->mLock.unlock(); |
| 439 | continue; |
| 440 | } |
| 441 | else if (n < 0) { |
Iliyan Malchev | 4113f34 | 2012-06-11 14:39:47 -0700 | [diff] [blame] | 442 | ALOGD("pcm_read() returned n < 0"); |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 443 | return static_cast<ssize_t>(n); |
| 444 | } |
| 445 | else { |
| 446 | read += static_cast<ssize_t>((period_size)); |
| 447 | read_pending -= period_size; |
SathishKumar Mani | 6ccde10 | 2012-09-26 20:23:34 -0700 | [diff] [blame] | 448 | //Set mute by cleanning buffers read |
| 449 | if (mParent->mMicMute) { |
| 450 | memset(buffer, 0, period_size); |
| 451 | } |
SathishKumar Mani | 5ff7a02 | 2012-09-14 11:36:35 -0700 | [diff] [blame] | 452 | buffer = ((uint8_t *)buffer) + period_size; |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | } while (mHandle->handle && read < bytes); |
| 456 | } |
| 457 | |
| 458 | return read; |
| 459 | } |
| 460 | |
| 461 | status_t AudioStreamInALSA::dump(int fd, const Vector<String16>& args) |
| 462 | { |
| 463 | return NO_ERROR; |
| 464 | } |
| 465 | |
| 466 | status_t AudioStreamInALSA::open(int mode) |
| 467 | { |
| 468 | Mutex::Autolock autoLock(mParent->mLock); |
| 469 | |
| 470 | status_t status = ALSAStreamOps::open(mode); |
| 471 | |
| 472 | return status; |
| 473 | } |
| 474 | |
| 475 | status_t AudioStreamInALSA::close() |
| 476 | { |
| 477 | Mutex::Autolock autoLock(mParent->mLock); |
| 478 | |
Iliyan Malchev | 4113f34 | 2012-06-11 14:39:47 -0700 | [diff] [blame] | 479 | ALOGD("close"); |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 480 | if((!strcmp(mHandle->useCase, SND_USE_CASE_VERB_IP_VOICECALL)) || |
| 481 | (!strcmp(mHandle->useCase, SND_USE_CASE_MOD_PLAY_VOIP))) { |
| 482 | if((mParent->mVoipStreamCount)) { |
Ajay Dudani | 9746c47 | 2012-06-18 16:01:16 -0700 | [diff] [blame] | 483 | #ifdef QCOM_USBAUDIO_ENABLED |
Iliyan Malchev | 4113f34 | 2012-06-11 14:39:47 -0700 | [diff] [blame] | 484 | ALOGD("musbRecordingState: %d, mVoipStreamCount:%d",mParent->musbRecordingState, |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 485 | mParent->mVoipStreamCount ); |
| 486 | if(mParent->mVoipStreamCount == 1) { |
SathishKumar Mani | 0a01991 | 2012-09-11 12:33:11 -0700 | [diff] [blame] | 487 | ALOGD("Deregistering VOIP Call bit, musbPlaybackState:%d," |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 488 | "musbRecordingState:%d", mParent->musbPlaybackState, mParent->musbRecordingState); |
| 489 | mParent->musbPlaybackState &= ~USBPLAYBACKBIT_VOIPCALL; |
| 490 | mParent->musbRecordingState &= ~USBRECBIT_VOIPCALL; |
| 491 | mParent->closeUsbRecordingIfNothingActive(); |
| 492 | mParent->closeUsbPlaybackIfNothingActive(); |
| 493 | } |
Ajay Dudani | 9746c47 | 2012-06-18 16:01:16 -0700 | [diff] [blame] | 494 | #endif |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 495 | return NO_ERROR; |
| 496 | } |
| 497 | mParent->mVoipStreamCount = 0; |
Ajay Dudani | 9746c47 | 2012-06-18 16:01:16 -0700 | [diff] [blame] | 498 | #ifdef QCOM_USBAUDIO_ENABLED |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 499 | } else { |
Iliyan Malchev | 4113f34 | 2012-06-11 14:39:47 -0700 | [diff] [blame] | 500 | ALOGD("Deregistering REC bit, musbRecordingState:%d", mParent->musbRecordingState); |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 501 | mParent->musbRecordingState &= ~USBRECBIT_REC; |
Ajay Dudani | 9746c47 | 2012-06-18 16:01:16 -0700 | [diff] [blame] | 502 | #endif |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 503 | } |
Ajay Dudani | 9746c47 | 2012-06-18 16:01:16 -0700 | [diff] [blame] | 504 | #ifdef QCOM_CSDCLIENT_ENABLED |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 505 | if (mParent->mFusion3Platform) { |
| 506 | if((!strcmp(mHandle->useCase, SND_USE_CASE_VERB_INCALL_REC)) || |
| 507 | (!strcmp(mHandle->useCase, SND_USE_CASE_MOD_CAPTURE_VOICE))) { |
SathishKumar Mani | 5ff7a02 | 2012-09-14 11:36:35 -0700 | [diff] [blame] | 508 | stop_csd_record(); |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 509 | } |
| 510 | } |
| 511 | #endif |
Iliyan Malchev | 4113f34 | 2012-06-11 14:39:47 -0700 | [diff] [blame] | 512 | ALOGD("close"); |
Ajay Dudani | 9746c47 | 2012-06-18 16:01:16 -0700 | [diff] [blame] | 513 | #ifdef QCOM_USBAUDIO_ENABLED |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 514 | mParent->closeUsbRecordingIfNothingActive(); |
Ajay Dudani | 9746c47 | 2012-06-18 16:01:16 -0700 | [diff] [blame] | 515 | #endif |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 516 | |
| 517 | ALSAStreamOps::close(); |
| 518 | |
Ajay Dudani | 9746c47 | 2012-06-18 16:01:16 -0700 | [diff] [blame] | 519 | #ifdef QCOM_SSR_ENABLED |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 520 | if (mSurroundObj) { |
| 521 | surround_filters_release(mSurroundObj); |
| 522 | if (mSurroundObj) |
| 523 | free(mSurroundObj); |
| 524 | mSurroundObj = NULL; |
| 525 | if (mRealCoeffs){ |
| 526 | for (int i =0; i<COEFF_ARRAY_SIZE; i++ ) { |
| 527 | if (mRealCoeffs[i]) { |
| 528 | free(mRealCoeffs[i]); |
| 529 | mRealCoeffs[i] = NULL; |
| 530 | } |
| 531 | } |
| 532 | free(mRealCoeffs); |
| 533 | mRealCoeffs = NULL; |
| 534 | } |
| 535 | if (mImagCoeffs){ |
| 536 | for (int i =0; i<COEFF_ARRAY_SIZE; i++ ) { |
| 537 | if (mImagCoeffs[i]) { |
| 538 | free(mImagCoeffs[i]); |
| 539 | mImagCoeffs[i] = NULL; |
| 540 | } |
| 541 | } |
| 542 | free(mImagCoeffs); |
| 543 | mImagCoeffs = NULL; |
| 544 | } |
| 545 | if (mSurroundOutputBuffer){ |
| 546 | free(mSurroundOutputBuffer); |
| 547 | mSurroundOutputBuffer = NULL; |
| 548 | } |
| 549 | if (mSurroundInputBuffer) { |
| 550 | free(mSurroundInputBuffer); |
| 551 | mSurroundInputBuffer = NULL; |
| 552 | } |
| 553 | |
| 554 | if ( mFp_4ch ) fclose(mFp_4ch); |
| 555 | if ( mFp_6ch ) fclose(mFp_6ch); |
| 556 | |
| 557 | } |
| 558 | #endif |
| 559 | |
| 560 | return NO_ERROR; |
| 561 | } |
| 562 | |
| 563 | status_t AudioStreamInALSA::standby() |
| 564 | { |
| 565 | Mutex::Autolock autoLock(mParent->mLock); |
| 566 | |
Iliyan Malchev | 4113f34 | 2012-06-11 14:39:47 -0700 | [diff] [blame] | 567 | ALOGD("standby"); |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 568 | |
| 569 | if((!strcmp(mHandle->useCase, SND_USE_CASE_VERB_IP_VOICECALL)) || |
| 570 | (!strcmp(mHandle->useCase, SND_USE_CASE_MOD_PLAY_VOIP))) { |
| 571 | return NO_ERROR; |
| 572 | } |
| 573 | |
Ajay Dudani | 9746c47 | 2012-06-18 16:01:16 -0700 | [diff] [blame] | 574 | #ifdef QCOM_CSDCLIENT_ENABLED |
Iliyan Malchev | 4113f34 | 2012-06-11 14:39:47 -0700 | [diff] [blame] | 575 | ALOGD("standby"); |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 576 | if (mParent->mFusion3Platform) { |
| 577 | if((!strcmp(mHandle->useCase, SND_USE_CASE_VERB_INCALL_REC)) || |
| 578 | (!strcmp(mHandle->useCase, SND_USE_CASE_MOD_CAPTURE_VOICE))) { |
Iliyan Malchev | 4113f34 | 2012-06-11 14:39:47 -0700 | [diff] [blame] | 579 | ALOGD(" into standby, stop record"); |
SathishKumar Mani | 5ff7a02 | 2012-09-14 11:36:35 -0700 | [diff] [blame] | 580 | stop_csd_record(); |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 581 | } |
| 582 | } |
| 583 | #endif |
| 584 | mHandle->module->standby(mHandle); |
| 585 | |
Ajay Dudani | 9746c47 | 2012-06-18 16:01:16 -0700 | [diff] [blame] | 586 | #ifdef QCOM_USBAUDIO_ENABLED |
Iliyan Malchev | 4113f34 | 2012-06-11 14:39:47 -0700 | [diff] [blame] | 587 | ALOGD("Checking for musbRecordingState %d", mParent->musbRecordingState); |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 588 | mParent->musbRecordingState &= ~USBRECBIT_REC; |
| 589 | mParent->closeUsbRecordingIfNothingActive(); |
Ajay Dudani | 9746c47 | 2012-06-18 16:01:16 -0700 | [diff] [blame] | 590 | #endif |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 591 | |
| 592 | return NO_ERROR; |
| 593 | } |
| 594 | |
| 595 | void AudioStreamInALSA::resetFramesLost() |
| 596 | { |
| 597 | mFramesLost = 0; |
| 598 | } |
| 599 | |
| 600 | unsigned int AudioStreamInALSA::getInputFramesLost() const |
| 601 | { |
| 602 | unsigned int count = mFramesLost; |
| 603 | // Stupid interface wants us to have a side effect of clearing the count |
| 604 | // but is defined as a const to prevent such a thing. |
| 605 | ((AudioStreamInALSA *)this)->resetFramesLost(); |
| 606 | return count; |
| 607 | } |
| 608 | |
| 609 | status_t AudioStreamInALSA::setAcousticParams(void *params) |
| 610 | { |
| 611 | Mutex::Autolock autoLock(mParent->mLock); |
| 612 | |
| 613 | return (status_t)NO_ERROR; |
| 614 | } |
| 615 | |
Ajay Dudani | 9746c47 | 2012-06-18 16:01:16 -0700 | [diff] [blame] | 616 | #ifdef QCOM_SSR_ENABLED |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 617 | status_t AudioStreamInALSA::initSurroundSoundLibrary(unsigned long buffersize) |
| 618 | { |
| 619 | int subwoofer = 0; // subwoofer channel assignment: default as first microphone input channel |
| 620 | int low_freq = 4; // frequency upper bound for subwoofer: frequency=(low_freq-1)/FFT_SIZE*samplingRate, default as 4 |
| 621 | int high_freq = 100; // frequency upper bound for spatial processing: frequency=(high_freq-1)/FFT_SIZE*samplingRate, default as 100 |
| 622 | int ret = 0; |
| 623 | |
| 624 | mSurroundInputBufferIdx = 0; |
| 625 | mSurroundOutputBufferIdx = 0; |
| 626 | |
| 627 | if ( mSurroundObj ) { |
Iliyan Malchev | 4113f34 | 2012-06-11 14:39:47 -0700 | [diff] [blame] | 628 | ALOGE("ola filter library is already initialized"); |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 629 | return ALREADY_EXISTS; |
| 630 | } |
| 631 | |
| 632 | // Allocate memory for input buffer |
| 633 | mSurroundInputBuffer = (Word16 *) calloc(2 * SSR_INPUT_FRAME_SIZE, |
| 634 | sizeof(Word16)); |
| 635 | if ( !mSurroundInputBuffer ) { |
Iliyan Malchev | 4113f34 | 2012-06-11 14:39:47 -0700 | [diff] [blame] | 636 | ALOGE("Memory allocation failure. Not able to allocate memory for surroundInputBuffer"); |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 637 | goto init_fail; |
| 638 | } |
| 639 | |
| 640 | // Allocate memory for output buffer |
| 641 | mSurroundOutputBuffer = (Word16 *) calloc(2 * SSR_OUTPUT_FRAME_SIZE, |
| 642 | sizeof(Word16)); |
| 643 | if ( !mSurroundOutputBuffer ) { |
Iliyan Malchev | 4113f34 | 2012-06-11 14:39:47 -0700 | [diff] [blame] | 644 | ALOGE("Memory allocation failure. Not able to allocate memory for surroundOutputBuffer"); |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 645 | goto init_fail; |
| 646 | } |
| 647 | |
| 648 | // Allocate memory for real and imag coeffs array |
| 649 | mRealCoeffs = (Word16 **) calloc(COEFF_ARRAY_SIZE, sizeof(Word16 *)); |
| 650 | if ( !mRealCoeffs ) { |
Iliyan Malchev | 4113f34 | 2012-06-11 14:39:47 -0700 | [diff] [blame] | 651 | ALOGE("Memory allocation failure during real Coefficient array"); |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 652 | goto init_fail; |
| 653 | } |
| 654 | |
| 655 | mImagCoeffs = (Word16 **) calloc(COEFF_ARRAY_SIZE, sizeof(Word16 *)); |
| 656 | if ( !mImagCoeffs ) { |
Iliyan Malchev | 4113f34 | 2012-06-11 14:39:47 -0700 | [diff] [blame] | 657 | ALOGE("Memory allocation failure during imaginary Coefficient array"); |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 658 | goto init_fail; |
| 659 | } |
| 660 | |
| 661 | if( readCoeffsFromFile() != NO_ERROR) { |
Iliyan Malchev | 4113f34 | 2012-06-11 14:39:47 -0700 | [diff] [blame] | 662 | ALOGE("Error while loading coeffs from file"); |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 663 | goto init_fail; |
| 664 | } |
| 665 | |
| 666 | //calculate the size of data to allocate for mSurroundObj |
| 667 | ret = surround_filters_init(NULL, |
| 668 | 6, // Num output channel |
| 669 | 4, // Num input channel |
| 670 | mRealCoeffs, // Coeffs hardcoded in header |
| 671 | mImagCoeffs, // Coeffs hardcoded in header |
| 672 | subwoofer, |
| 673 | low_freq, |
| 674 | high_freq, |
| 675 | NULL); |
| 676 | |
| 677 | if ( ret > 0 ) { |
Iliyan Malchev | 4113f34 | 2012-06-11 14:39:47 -0700 | [diff] [blame] | 678 | ALOGV("Allocating surroundObj size is %d", ret); |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 679 | mSurroundObj = (void *)malloc(ret); |
| 680 | memset(mSurroundObj,0,ret); |
| 681 | if (NULL != mSurroundObj) { |
| 682 | //initialize after allocating the memory for mSurroundObj |
| 683 | ret = surround_filters_init(mSurroundObj, |
| 684 | 6, |
| 685 | 4, |
| 686 | mRealCoeffs, |
| 687 | mImagCoeffs, |
| 688 | subwoofer, |
| 689 | low_freq, |
| 690 | high_freq, |
| 691 | NULL); |
| 692 | if (0 != ret) { |
Iliyan Malchev | 4113f34 | 2012-06-11 14:39:47 -0700 | [diff] [blame] | 693 | ALOGE("surround_filters_init failed with ret:%d",ret); |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 694 | surround_filters_release(mSurroundObj); |
| 695 | goto init_fail; |
| 696 | } |
| 697 | } else { |
Iliyan Malchev | 4113f34 | 2012-06-11 14:39:47 -0700 | [diff] [blame] | 698 | ALOGE("Allocationg mSurroundObj failed"); |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 699 | goto init_fail; |
| 700 | } |
| 701 | } else { |
Iliyan Malchev | 4113f34 | 2012-06-11 14:39:47 -0700 | [diff] [blame] | 702 | ALOGE("surround_filters_init(mSurroundObj=Null) failed with ret: %d",ret); |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 703 | goto init_fail; |
| 704 | } |
| 705 | |
| 706 | (void) surround_filters_set_channel_map(mSurroundObj, chanMap); |
| 707 | |
| 708 | return NO_ERROR; |
| 709 | |
| 710 | init_fail: |
| 711 | if (mSurroundObj) { |
| 712 | free(mSurroundObj); |
| 713 | mSurroundObj = NULL; |
| 714 | } |
| 715 | if (mSurroundOutputBuffer) { |
| 716 | free(mSurroundOutputBuffer); |
| 717 | mSurroundOutputBuffer = NULL; |
| 718 | } |
| 719 | if (mSurroundInputBuffer) { |
| 720 | free(mSurroundInputBuffer); |
| 721 | mSurroundInputBuffer = NULL; |
| 722 | } |
| 723 | if (mRealCoeffs){ |
| 724 | for (int i =0; i<COEFF_ARRAY_SIZE; i++ ) { |
| 725 | if (mRealCoeffs[i]) { |
| 726 | free(mRealCoeffs[i]); |
| 727 | mRealCoeffs[i] = NULL; |
| 728 | } |
| 729 | } |
| 730 | free(mRealCoeffs); |
| 731 | mRealCoeffs = NULL; |
| 732 | } |
| 733 | if (mImagCoeffs){ |
| 734 | for (int i =0; i<COEFF_ARRAY_SIZE; i++ ) { |
| 735 | if (mImagCoeffs[i]) { |
| 736 | free(mImagCoeffs[i]); |
| 737 | mImagCoeffs[i] = NULL; |
| 738 | } |
| 739 | } |
| 740 | free(mImagCoeffs); |
| 741 | mImagCoeffs = NULL; |
| 742 | } |
| 743 | |
| 744 | return NO_MEMORY; |
| 745 | |
| 746 | } |
| 747 | |
| 748 | |
| 749 | // Helper function to read coeffs from File and updates real and imaginary |
| 750 | // coeff array member variable |
| 751 | status_t AudioStreamInALSA::readCoeffsFromFile() |
| 752 | { |
| 753 | FILE *flt1r; |
| 754 | FILE *flt2r; |
| 755 | FILE *flt3r; |
| 756 | FILE *flt4r; |
| 757 | FILE *flt1i; |
| 758 | FILE *flt2i; |
| 759 | FILE *flt3i; |
| 760 | FILE *flt4i; |
| 761 | |
| 762 | if ( (flt1r = fopen(SURROUND_FILE_1R, "rb")) == NULL ) { |
Iliyan Malchev | 4113f34 | 2012-06-11 14:39:47 -0700 | [diff] [blame] | 763 | ALOGE("Cannot open filter co-efficient file %s", SURROUND_FILE_1R); |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 764 | return NAME_NOT_FOUND; |
| 765 | } |
| 766 | |
| 767 | if ( (flt2r = fopen(SURROUND_FILE_2R, "rb")) == NULL ) { |
Iliyan Malchev | 4113f34 | 2012-06-11 14:39:47 -0700 | [diff] [blame] | 768 | ALOGE("Cannot open filter co-efficient file %s", SURROUND_FILE_2R); |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 769 | return NAME_NOT_FOUND; |
| 770 | } |
| 771 | |
| 772 | if ( (flt3r = fopen(SURROUND_FILE_3R, "rb")) == NULL ) { |
Iliyan Malchev | 4113f34 | 2012-06-11 14:39:47 -0700 | [diff] [blame] | 773 | ALOGE("Cannot open filter co-efficient file %s", SURROUND_FILE_3R); |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 774 | return NAME_NOT_FOUND; |
| 775 | } |
| 776 | |
| 777 | if ( (flt4r = fopen(SURROUND_FILE_4R, "rb")) == NULL ) { |
Iliyan Malchev | 4113f34 | 2012-06-11 14:39:47 -0700 | [diff] [blame] | 778 | ALOGE("Cannot open filter co-efficient file %s", SURROUND_FILE_4R); |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 779 | return NAME_NOT_FOUND; |
| 780 | } |
| 781 | |
| 782 | if ( (flt1i = fopen(SURROUND_FILE_1I, "rb")) == NULL ) { |
Iliyan Malchev | 4113f34 | 2012-06-11 14:39:47 -0700 | [diff] [blame] | 783 | ALOGE("Cannot open filter co-efficient file %s", SURROUND_FILE_1I); |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 784 | return NAME_NOT_FOUND; |
| 785 | } |
| 786 | |
| 787 | if ( (flt2i = fopen(SURROUND_FILE_2I, "rb")) == NULL ) { |
Iliyan Malchev | 4113f34 | 2012-06-11 14:39:47 -0700 | [diff] [blame] | 788 | ALOGE("Cannot open filter co-efficient file %s", SURROUND_FILE_2I); |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 789 | return NAME_NOT_FOUND; |
| 790 | } |
| 791 | |
| 792 | if ( (flt3i = fopen(SURROUND_FILE_3I, "rb")) == NULL ) { |
Iliyan Malchev | 4113f34 | 2012-06-11 14:39:47 -0700 | [diff] [blame] | 793 | ALOGE("Cannot open filter co-efficient file %s", SURROUND_FILE_3I); |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 794 | return NAME_NOT_FOUND; |
| 795 | } |
| 796 | |
| 797 | if ( (flt4i = fopen(SURROUND_FILE_4I, "rb")) == NULL ) { |
Iliyan Malchev | 4113f34 | 2012-06-11 14:39:47 -0700 | [diff] [blame] | 798 | ALOGE("Cannot open filter co-efficient file %s", SURROUND_FILE_4I); |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 799 | return NAME_NOT_FOUND; |
| 800 | } |
Iliyan Malchev | 4113f34 | 2012-06-11 14:39:47 -0700 | [diff] [blame] | 801 | ALOGV("readCoeffsFromFile all filter files opened"); |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 802 | |
| 803 | for (int i=0; i<COEFF_ARRAY_SIZE; i++) { |
| 804 | mRealCoeffs[i] = (Word16 *)calloc(FILT_SIZE, sizeof(Word16)); |
| 805 | } |
| 806 | for (int i=0; i<COEFF_ARRAY_SIZE; i++) { |
| 807 | mImagCoeffs[i] = (Word16 *)calloc(FILT_SIZE, sizeof(Word16)); |
| 808 | } |
| 809 | |
| 810 | // Read real co-efficients |
| 811 | if (NULL != mRealCoeffs[0]) { |
| 812 | fread(mRealCoeffs[0], sizeof(int16), FILT_SIZE, flt1r); |
| 813 | } |
| 814 | if (NULL != mRealCoeffs[0]) { |
| 815 | fread(mRealCoeffs[1], sizeof(int16), FILT_SIZE, flt2r); |
| 816 | } |
| 817 | if (NULL != mRealCoeffs[0]) { |
| 818 | fread(mRealCoeffs[2], sizeof(int16), FILT_SIZE, flt3r); |
| 819 | } |
| 820 | if (NULL != mRealCoeffs[0]) { |
| 821 | fread(mRealCoeffs[3], sizeof(int16), FILT_SIZE, flt4r); |
| 822 | } |
| 823 | |
| 824 | // read imaginary co-efficients |
| 825 | if (NULL != mImagCoeffs[0]) { |
| 826 | fread(mImagCoeffs[0], sizeof(int16), FILT_SIZE, flt1i); |
| 827 | } |
| 828 | if (NULL != mImagCoeffs[0]) { |
| 829 | fread(mImagCoeffs[1], sizeof(int16), FILT_SIZE, flt2i); |
| 830 | } |
| 831 | if (NULL != mImagCoeffs[0]) { |
| 832 | fread(mImagCoeffs[2], sizeof(int16), FILT_SIZE, flt3i); |
| 833 | } |
| 834 | if (NULL != mImagCoeffs[0]) { |
| 835 | fread(mImagCoeffs[3], sizeof(int16), FILT_SIZE, flt4i); |
| 836 | } |
| 837 | |
| 838 | fclose(flt1r); |
| 839 | fclose(flt2r); |
| 840 | fclose(flt3r); |
| 841 | fclose(flt4r); |
| 842 | fclose(flt1i); |
| 843 | fclose(flt2i); |
| 844 | fclose(flt3i); |
| 845 | fclose(flt4i); |
| 846 | |
| 847 | return NO_ERROR; |
| 848 | } |
| 849 | #endif |
| 850 | |
SathishKumar Mani | 5ff7a02 | 2012-09-14 11:36:35 -0700 | [diff] [blame] | 851 | #ifdef QCOM_CSDCLIENT_ENABLED |
| 852 | int AudioStreamInALSA::start_csd_record(int param) |
| 853 | { |
| 854 | int err = NO_ERROR; |
| 855 | |
| 856 | if (mParent->mCsdHandle != NULL) { |
| 857 | csd_start_record = (int (*)(int))::dlsym(mParent->mCsdHandle,"csd_client_start_record"); |
| 858 | if (csd_start_record == NULL) { |
| 859 | ALOGE("dlsym:Error:%s Loading csd_client_start_record", dlerror()); |
| 860 | } else { |
| 861 | err = csd_start_record(param); |
| 862 | } |
| 863 | } |
| 864 | return err; |
| 865 | } |
| 866 | |
| 867 | int AudioStreamInALSA::stop_csd_record() |
| 868 | { |
| 869 | int err = NO_ERROR; |
| 870 | if (mParent->mCsdHandle != NULL) { |
| 871 | csd_stop_record = (int (*)())::dlsym(mParent->mCsdHandle,"csd_client_stop_record"); |
| 872 | if (csd_start_record == NULL) { |
| 873 | ALOGE("dlsym:Error:%s Loading csd_client_start_record", dlerror()); |
| 874 | } else { |
| 875 | csd_stop_record(); |
| 876 | } |
| 877 | } |
| 878 | return err; |
| 879 | } |
| 880 | #endif |
| 881 | |
Iliyan Malchev | 4765c43 | 2012-06-11 14:36:16 -0700 | [diff] [blame] | 882 | } // namespace android_audio_legacy |