| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 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 | #include <stdlib.h> | 
|  | 18 | #include <string.h> | 
|  | 19 | #define LOG_TAG "PreProcessing" | 
|  | 20 | //#define LOG_NDEBUG 0 | 
|  | 21 | #include <utils/Log.h> | 
|  | 22 | #include <utils/Timers.h> | 
|  | 23 | #include <hardware/audio_effect.h> | 
|  | 24 | #include <audio_effects/effect_aec.h> | 
|  | 25 | #include <audio_effects/effect_agc.h> | 
|  | 26 | #include <audio_effects/effect_ns.h> | 
| Eric Laurent | 5387696 | 2012-01-31 12:35:20 -0800 | [diff] [blame] | 27 | #include <module_common_types.h> | 
|  | 28 | #include <audio_processing.h> | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 29 | #include "speex/speex_resampler.h" | 
|  | 30 |  | 
| Eric Laurent | 3f9c84c | 2012-04-03 15:36:53 -0700 | [diff] [blame] | 31 | // undefine to perform multi channels API functional tests | 
|  | 32 | //#define DUAL_MIC_TEST | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 33 |  | 
|  | 34 | //------------------------------------------------------------------------------ | 
|  | 35 | // local definitions | 
|  | 36 | //------------------------------------------------------------------------------ | 
|  | 37 |  | 
|  | 38 | // maximum number of sessions | 
|  | 39 | #define PREPROC_NUM_SESSIONS 8 | 
|  | 40 |  | 
|  | 41 | // types of pre processing modules | 
|  | 42 | enum preproc_id | 
|  | 43 | { | 
|  | 44 | PREPROC_AGC,        // Automatic Gain Control | 
|  | 45 | PREPROC_AEC,        // Acoustic Echo Canceler | 
|  | 46 | PREPROC_NS,         // Noise Suppressor | 
|  | 47 | PREPROC_NUM_EFFECTS | 
|  | 48 | }; | 
|  | 49 |  | 
|  | 50 | // Session state | 
|  | 51 | enum preproc_session_state { | 
|  | 52 | PREPROC_SESSION_STATE_INIT,        // initialized | 
|  | 53 | PREPROC_SESSION_STATE_CONFIG       // configuration received | 
|  | 54 | }; | 
|  | 55 |  | 
|  | 56 | // Effect/Preprocessor state | 
|  | 57 | enum preproc_effect_state { | 
|  | 58 | PREPROC_EFFECT_STATE_INIT,         // initialized | 
|  | 59 | PREPROC_EFFECT_STATE_CREATED,      // webRTC engine created | 
|  | 60 | PREPROC_EFFECT_STATE_CONFIG,       // configuration received/disabled | 
|  | 61 | PREPROC_EFFECT_STATE_ACTIVE        // active/enabled | 
|  | 62 | }; | 
|  | 63 |  | 
|  | 64 | // handle on webRTC engine | 
|  | 65 | typedef void* preproc_fx_handle_t; | 
|  | 66 |  | 
|  | 67 | typedef struct preproc_session_s preproc_session_t; | 
|  | 68 | typedef struct preproc_effect_s preproc_effect_t; | 
|  | 69 | typedef struct preproc_ops_s preproc_ops_t; | 
|  | 70 |  | 
|  | 71 | // Effect operation table. Functions for all pre processors are declared in sPreProcOps[] table. | 
|  | 72 | // Function pointer can be null if no action required. | 
|  | 73 | struct preproc_ops_s { | 
|  | 74 | int (* create)(preproc_effect_t *fx); | 
|  | 75 | int (* init)(preproc_effect_t *fx); | 
|  | 76 | int (* reset)(preproc_effect_t *fx); | 
|  | 77 | void (* enable)(preproc_effect_t *fx); | 
|  | 78 | void (* disable)(preproc_effect_t *fx); | 
|  | 79 | int (* set_parameter)(preproc_effect_t *fx, void *param, void *value); | 
| Ashok Bhat | b302bd5 | 2014-02-18 11:40:00 +0000 | [diff] [blame] | 80 | int (* get_parameter)(preproc_effect_t *fx, void *param, uint32_t *size, void *value); | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 81 | int (* set_device)(preproc_effect_t *fx, uint32_t device); | 
|  | 82 | }; | 
|  | 83 |  | 
|  | 84 | // Effect context | 
|  | 85 | struct preproc_effect_s { | 
|  | 86 | const struct effect_interface_s *itfe; | 
|  | 87 | uint32_t procId;                // type of pre processor (enum preproc_id) | 
|  | 88 | uint32_t state;                 // current state (enum preproc_effect_state) | 
|  | 89 | preproc_session_t *session;     // session the effect is on | 
|  | 90 | const preproc_ops_t *ops;       // effect ops table | 
|  | 91 | preproc_fx_handle_t engine;     // handle on webRTC engine | 
| Alex Luebs | 766bf73 | 2015-12-14 21:32:13 -0800 | [diff] [blame] | 92 | uint32_t type;                  // subtype of effect | 
| Eric Laurent | 3f9c84c | 2012-04-03 15:36:53 -0700 | [diff] [blame] | 93 | #ifdef DUAL_MIC_TEST | 
|  | 94 | bool aux_channels_on;           // support auxiliary channels | 
|  | 95 | size_t cur_channel_config;      // current auciliary channel configuration | 
|  | 96 | #endif | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 97 | }; | 
|  | 98 |  | 
|  | 99 | // Session context | 
|  | 100 | struct preproc_session_s { | 
|  | 101 | struct preproc_effect_s effects[PREPROC_NUM_EFFECTS]; // effects in this session | 
|  | 102 | uint32_t state;                     // current state (enum preproc_session_state) | 
|  | 103 | int id;                             // audio session ID | 
|  | 104 | int io;                             // handle of input stream this session is on | 
|  | 105 | webrtc::AudioProcessing* apm;       // handle on webRTC audio processing module (APM) | 
|  | 106 | size_t apmFrameCount;               // buffer size for webRTC process (10 ms) | 
|  | 107 | uint32_t apmSamplingRate;           // webRTC APM sampling rate (8/16 or 32 kHz) | 
|  | 108 | size_t frameCount;                  // buffer size before input resampler ( <=> apmFrameCount) | 
|  | 109 | uint32_t samplingRate;              // sampling rate at effect process interface | 
|  | 110 | uint32_t inChannelCount;            // input channel count | 
|  | 111 | uint32_t outChannelCount;           // output channel count | 
|  | 112 | uint32_t createdMsk;                // bit field containing IDs of crested pre processors | 
|  | 113 | uint32_t enabledMsk;                // bit field containing IDs of enabled pre processors | 
|  | 114 | uint32_t processedMsk;              // bit field containing IDs of pre processors already | 
|  | 115 | // processed in current round | 
|  | 116 | webrtc::AudioFrame *procFrame;      // audio frame passed to webRTC AMP ProcessStream() | 
|  | 117 | int16_t *inBuf;                     // input buffer used when resampling | 
|  | 118 | size_t inBufSize;                   // input buffer size in frames | 
|  | 119 | size_t framesIn;                    // number of frames in input buffer | 
|  | 120 | SpeexResamplerState *inResampler;   // handle on input speex resampler | 
|  | 121 | int16_t *outBuf;                    // output buffer used when resampling | 
|  | 122 | size_t outBufSize;                  // output buffer size in frames | 
|  | 123 | size_t framesOut;                   // number of frames in output buffer | 
|  | 124 | SpeexResamplerState *outResampler;  // handle on output speex resampler | 
|  | 125 | uint32_t revChannelCount;           // number of channels on reverse stream | 
|  | 126 | uint32_t revEnabledMsk;             // bit field containing IDs of enabled pre processors | 
|  | 127 | // with reverse channel | 
|  | 128 | uint32_t revProcessedMsk;           // bit field containing IDs of pre processors with reverse | 
|  | 129 | // channel already processed in current round | 
|  | 130 | webrtc::AudioFrame *revFrame;       // audio frame passed to webRTC AMP AnalyzeReverseStream() | 
|  | 131 | int16_t *revBuf;                    // reverse channel input buffer | 
|  | 132 | size_t revBufSize;                  // reverse channel input buffer size | 
|  | 133 | size_t framesRev;                   // number of frames in reverse channel input buffer | 
|  | 134 | SpeexResamplerState *revResampler;  // handle on reverse channel input speex resampler | 
|  | 135 | }; | 
|  | 136 |  | 
| Eric Laurent | 3f9c84c | 2012-04-03 15:36:53 -0700 | [diff] [blame] | 137 | #ifdef DUAL_MIC_TEST | 
|  | 138 | enum { | 
|  | 139 | PREPROC_CMD_DUAL_MIC_ENABLE = EFFECT_CMD_FIRST_PROPRIETARY, // enable dual mic mode | 
|  | 140 | PREPROC_CMD_DUAL_MIC_PCM_DUMP_START,                        // start pcm capture | 
|  | 141 | PREPROC_CMD_DUAL_MIC_PCM_DUMP_STOP                          // stop pcm capture | 
|  | 142 | }; | 
|  | 143 |  | 
|  | 144 | enum { | 
|  | 145 | CHANNEL_CFG_MONO, | 
|  | 146 | CHANNEL_CFG_STEREO, | 
|  | 147 | CHANNEL_CFG_MONO_AUX, | 
|  | 148 | CHANNEL_CFG_STEREO_AUX, | 
|  | 149 | CHANNEL_CFG_CNT, | 
|  | 150 | CHANNEL_CFG_FIRST_AUX = CHANNEL_CFG_MONO_AUX, | 
|  | 151 | }; | 
|  | 152 |  | 
|  | 153 | const channel_config_t sDualMicConfigs[CHANNEL_CFG_CNT] = { | 
|  | 154 | {AUDIO_CHANNEL_IN_MONO , 0}, | 
|  | 155 | {AUDIO_CHANNEL_IN_STEREO , 0}, | 
|  | 156 | {AUDIO_CHANNEL_IN_FRONT , AUDIO_CHANNEL_IN_BACK}, | 
|  | 157 | {AUDIO_CHANNEL_IN_STEREO , AUDIO_CHANNEL_IN_RIGHT} | 
|  | 158 | }; | 
|  | 159 |  | 
|  | 160 | bool sHasAuxChannels[PREPROC_NUM_EFFECTS] = { | 
|  | 161 | false,   // PREPROC_AGC | 
|  | 162 | true,   // PREPROC_AEC | 
|  | 163 | true,   // PREPROC_NS | 
|  | 164 | }; | 
|  | 165 |  | 
|  | 166 | bool gDualMicEnabled; | 
|  | 167 | FILE *gPcmDumpFh; | 
|  | 168 | static pthread_mutex_t gPcmDumpLock = PTHREAD_MUTEX_INITIALIZER; | 
|  | 169 | #endif | 
|  | 170 |  | 
|  | 171 |  | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 172 | //------------------------------------------------------------------------------ | 
|  | 173 | // Effect descriptors | 
|  | 174 | //------------------------------------------------------------------------------ | 
|  | 175 |  | 
|  | 176 | // UUIDs for effect types have been generated from http://www.itu.int/ITU-T/asn1/uuid.html | 
|  | 177 | // as the pre processing effects are not defined by OpenSL ES | 
|  | 178 |  | 
|  | 179 | // Automatic Gain Control | 
|  | 180 | static const effect_descriptor_t sAgcDescriptor = { | 
|  | 181 | { 0x0a8abfe0, 0x654c, 0x11e0, 0xba26, { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } }, // type | 
|  | 182 | { 0xaa8130e0, 0x66fc, 0x11e0, 0xbad0, { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } }, // uuid | 
|  | 183 | EFFECT_CONTROL_API_VERSION, | 
|  | 184 | (EFFECT_FLAG_TYPE_PRE_PROC|EFFECT_FLAG_DEVICE_IND), | 
|  | 185 | 0, //FIXME indicate CPU load | 
|  | 186 | 0, //FIXME indicate memory usage | 
|  | 187 | "Automatic Gain Control", | 
|  | 188 | "The Android Open Source Project" | 
|  | 189 | }; | 
|  | 190 |  | 
|  | 191 | // Acoustic Echo Cancellation | 
|  | 192 | static const effect_descriptor_t sAecDescriptor = { | 
|  | 193 | { 0x7b491460, 0x8d4d, 0x11e0, 0xbd61, { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } }, // type | 
|  | 194 | { 0xbb392ec0, 0x8d4d, 0x11e0, 0xa896, { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } }, // uuid | 
|  | 195 | EFFECT_CONTROL_API_VERSION, | 
|  | 196 | (EFFECT_FLAG_TYPE_PRE_PROC|EFFECT_FLAG_DEVICE_IND), | 
|  | 197 | 0, //FIXME indicate CPU load | 
|  | 198 | 0, //FIXME indicate memory usage | 
|  | 199 | "Acoustic Echo Canceler", | 
|  | 200 | "The Android Open Source Project" | 
|  | 201 | }; | 
|  | 202 |  | 
|  | 203 | // Noise suppression | 
|  | 204 | static const effect_descriptor_t sNsDescriptor = { | 
|  | 205 | { 0x58b4b260, 0x8e06, 0x11e0, 0xaa8e, { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } }, // type | 
|  | 206 | { 0xc06c8400, 0x8e06, 0x11e0, 0x9cb6, { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } }, // uuid | 
|  | 207 | EFFECT_CONTROL_API_VERSION, | 
|  | 208 | (EFFECT_FLAG_TYPE_PRE_PROC|EFFECT_FLAG_DEVICE_IND), | 
|  | 209 | 0, //FIXME indicate CPU load | 
|  | 210 | 0, //FIXME indicate memory usage | 
|  | 211 | "Noise Suppression", | 
|  | 212 | "The Android Open Source Project" | 
|  | 213 | }; | 
|  | 214 |  | 
|  | 215 |  | 
|  | 216 | static const effect_descriptor_t *sDescriptors[PREPROC_NUM_EFFECTS] = { | 
|  | 217 | &sAgcDescriptor, | 
|  | 218 | &sAecDescriptor, | 
|  | 219 | &sNsDescriptor | 
|  | 220 | }; | 
|  | 221 |  | 
|  | 222 | //------------------------------------------------------------------------------ | 
|  | 223 | // Helper functions | 
|  | 224 | //------------------------------------------------------------------------------ | 
|  | 225 |  | 
|  | 226 | const effect_uuid_t * const sUuidToPreProcTable[PREPROC_NUM_EFFECTS] = { | 
|  | 227 | FX_IID_AGC, | 
|  | 228 | FX_IID_AEC, | 
|  | 229 | FX_IID_NS | 
|  | 230 | }; | 
|  | 231 |  | 
|  | 232 |  | 
|  | 233 | const effect_uuid_t * ProcIdToUuid(int procId) | 
|  | 234 | { | 
|  | 235 | if (procId >= PREPROC_NUM_EFFECTS) { | 
|  | 236 | return EFFECT_UUID_NULL; | 
|  | 237 | } | 
|  | 238 | return sUuidToPreProcTable[procId]; | 
|  | 239 | } | 
|  | 240 |  | 
|  | 241 | uint32_t UuidToProcId(const effect_uuid_t * uuid) | 
|  | 242 | { | 
|  | 243 | size_t i; | 
|  | 244 | for (i = 0; i < PREPROC_NUM_EFFECTS; i++) { | 
|  | 245 | if (memcmp(uuid, sUuidToPreProcTable[i], sizeof(*uuid)) == 0) { | 
|  | 246 | break; | 
|  | 247 | } | 
|  | 248 | } | 
|  | 249 | return i; | 
|  | 250 | } | 
|  | 251 |  | 
|  | 252 | bool HasReverseStream(uint32_t procId) | 
|  | 253 | { | 
|  | 254 | if (procId == PREPROC_AEC) { | 
|  | 255 | return true; | 
|  | 256 | } | 
|  | 257 | return false; | 
|  | 258 | } | 
|  | 259 |  | 
|  | 260 |  | 
|  | 261 | //------------------------------------------------------------------------------ | 
|  | 262 | // Automatic Gain Control (AGC) | 
|  | 263 | //------------------------------------------------------------------------------ | 
|  | 264 |  | 
| Eric Laurent | 5387696 | 2012-01-31 12:35:20 -0800 | [diff] [blame] | 265 | static const int kAgcDefaultTargetLevel = 3; | 
|  | 266 | static const int kAgcDefaultCompGain = 9; | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 267 | static const bool kAgcDefaultLimiter = true; | 
|  | 268 |  | 
|  | 269 | int  AgcInit (preproc_effect_t *effect) | 
|  | 270 | { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 271 | ALOGV("AgcInit"); | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 272 | webrtc::GainControl *agc = static_cast<webrtc::GainControl *>(effect->engine); | 
|  | 273 | agc->set_mode(webrtc::GainControl::kFixedDigital); | 
|  | 274 | agc->set_target_level_dbfs(kAgcDefaultTargetLevel); | 
|  | 275 | agc->set_compression_gain_db(kAgcDefaultCompGain); | 
|  | 276 | agc->enable_limiter(kAgcDefaultLimiter); | 
|  | 277 | return 0; | 
|  | 278 | } | 
|  | 279 |  | 
|  | 280 | int  AgcCreate(preproc_effect_t *effect) | 
|  | 281 | { | 
|  | 282 | webrtc::GainControl *agc = effect->session->apm->gain_control(); | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 283 | ALOGV("AgcCreate got agc %p", agc); | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 284 | if (agc == NULL) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 285 | ALOGW("AgcCreate Error"); | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 286 | return -ENOMEM; | 
|  | 287 | } | 
|  | 288 | effect->engine = static_cast<preproc_fx_handle_t>(agc); | 
|  | 289 | AgcInit(effect); | 
|  | 290 | return 0; | 
|  | 291 | } | 
|  | 292 |  | 
|  | 293 | int AgcGetParameter(preproc_effect_t *effect, | 
|  | 294 | void *pParam, | 
| Ashok Bhat | b302bd5 | 2014-02-18 11:40:00 +0000 | [diff] [blame] | 295 | uint32_t *pValueSize, | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 296 | void *pValue) | 
|  | 297 | { | 
|  | 298 | int status = 0; | 
|  | 299 | uint32_t param = *(uint32_t *)pParam; | 
|  | 300 | t_agc_settings *pProperties = (t_agc_settings *)pValue; | 
|  | 301 | webrtc::GainControl *agc = static_cast<webrtc::GainControl *>(effect->engine); | 
|  | 302 |  | 
|  | 303 | switch (param) { | 
|  | 304 | case AGC_PARAM_TARGET_LEVEL: | 
|  | 305 | case AGC_PARAM_COMP_GAIN: | 
|  | 306 | if (*pValueSize < sizeof(int16_t)) { | 
|  | 307 | *pValueSize = 0; | 
|  | 308 | return -EINVAL; | 
|  | 309 | } | 
|  | 310 | break; | 
|  | 311 | case AGC_PARAM_LIMITER_ENA: | 
|  | 312 | if (*pValueSize < sizeof(bool)) { | 
|  | 313 | *pValueSize = 0; | 
|  | 314 | return -EINVAL; | 
|  | 315 | } | 
|  | 316 | break; | 
|  | 317 | case AGC_PARAM_PROPERTIES: | 
|  | 318 | if (*pValueSize < sizeof(t_agc_settings)) { | 
|  | 319 | *pValueSize = 0; | 
|  | 320 | return -EINVAL; | 
|  | 321 | } | 
|  | 322 | break; | 
|  | 323 |  | 
|  | 324 | default: | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 325 | ALOGW("AgcGetParameter() unknown param %08x", param); | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 326 | status = -EINVAL; | 
|  | 327 | break; | 
|  | 328 | } | 
|  | 329 |  | 
|  | 330 | switch (param) { | 
|  | 331 | case AGC_PARAM_TARGET_LEVEL: | 
|  | 332 | *(int16_t *) pValue = (int16_t)(agc->target_level_dbfs() * -100); | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 333 | ALOGV("AgcGetParameter() target level %d milliBels", *(int16_t *) pValue); | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 334 | break; | 
|  | 335 | case AGC_PARAM_COMP_GAIN: | 
|  | 336 | *(int16_t *) pValue = (int16_t)(agc->compression_gain_db() * 100); | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 337 | ALOGV("AgcGetParameter() comp gain %d milliBels", *(int16_t *) pValue); | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 338 | break; | 
|  | 339 | case AGC_PARAM_LIMITER_ENA: | 
|  | 340 | *(bool *) pValue = (bool)agc->is_limiter_enabled(); | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 341 | ALOGV("AgcGetParameter() limiter enabled %s", | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 342 | (*(int16_t *) pValue != 0) ? "true" : "false"); | 
|  | 343 | break; | 
|  | 344 | case AGC_PARAM_PROPERTIES: | 
|  | 345 | pProperties->targetLevel = (int16_t)(agc->target_level_dbfs() * -100); | 
|  | 346 | pProperties->compGain = (int16_t)(agc->compression_gain_db() * 100); | 
|  | 347 | pProperties->limiterEnabled = (bool)agc->is_limiter_enabled(); | 
|  | 348 | break; | 
|  | 349 | default: | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 350 | ALOGW("AgcGetParameter() unknown param %d", param); | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 351 | status = -EINVAL; | 
|  | 352 | break; | 
|  | 353 | } | 
|  | 354 | return status; | 
|  | 355 | } | 
|  | 356 |  | 
|  | 357 | int AgcSetParameter (preproc_effect_t *effect, void *pParam, void *pValue) | 
|  | 358 | { | 
|  | 359 | int status = 0; | 
|  | 360 | uint32_t param = *(uint32_t *)pParam; | 
|  | 361 | t_agc_settings *pProperties = (t_agc_settings *)pValue; | 
|  | 362 | webrtc::GainControl *agc = static_cast<webrtc::GainControl *>(effect->engine); | 
|  | 363 |  | 
|  | 364 | switch (param) { | 
|  | 365 | case AGC_PARAM_TARGET_LEVEL: | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 366 | ALOGV("AgcSetParameter() target level %d milliBels", *(int16_t *)pValue); | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 367 | status = agc->set_target_level_dbfs(-(*(int16_t *)pValue / 100)); | 
|  | 368 | break; | 
|  | 369 | case AGC_PARAM_COMP_GAIN: | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 370 | ALOGV("AgcSetParameter() comp gain %d milliBels", *(int16_t *)pValue); | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 371 | status = agc->set_compression_gain_db(*(int16_t *)pValue / 100); | 
|  | 372 | break; | 
|  | 373 | case AGC_PARAM_LIMITER_ENA: | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 374 | ALOGV("AgcSetParameter() limiter enabled %s", *(bool *)pValue ? "true" : "false"); | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 375 | status = agc->enable_limiter(*(bool *)pValue); | 
|  | 376 | break; | 
|  | 377 | case AGC_PARAM_PROPERTIES: | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 378 | ALOGV("AgcSetParameter() properties level %d, gain %d limiter %d", | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 379 | pProperties->targetLevel, | 
|  | 380 | pProperties->compGain, | 
|  | 381 | pProperties->limiterEnabled); | 
|  | 382 | status = agc->set_target_level_dbfs(-(pProperties->targetLevel / 100)); | 
|  | 383 | if (status != 0) break; | 
|  | 384 | status = agc->set_compression_gain_db(pProperties->compGain / 100); | 
|  | 385 | if (status != 0) break; | 
|  | 386 | status = agc->enable_limiter(pProperties->limiterEnabled); | 
|  | 387 | break; | 
|  | 388 | default: | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 389 | ALOGW("AgcSetParameter() unknown param %08x value %08x", param, *(uint32_t *)pValue); | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 390 | status = -EINVAL; | 
|  | 391 | break; | 
|  | 392 | } | 
|  | 393 |  | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 394 | ALOGV("AgcSetParameter() done status %d", status); | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 395 |  | 
|  | 396 | return status; | 
|  | 397 | } | 
|  | 398 |  | 
|  | 399 | void AgcEnable(preproc_effect_t *effect) | 
|  | 400 | { | 
|  | 401 | webrtc::GainControl *agc = static_cast<webrtc::GainControl *>(effect->engine); | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 402 | ALOGV("AgcEnable agc %p", agc); | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 403 | agc->Enable(true); | 
|  | 404 | } | 
|  | 405 |  | 
|  | 406 | void AgcDisable(preproc_effect_t *effect) | 
|  | 407 | { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 408 | ALOGV("AgcDisable"); | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 409 | webrtc::GainControl *agc = static_cast<webrtc::GainControl *>(effect->engine); | 
|  | 410 | agc->Enable(false); | 
|  | 411 | } | 
|  | 412 |  | 
|  | 413 |  | 
|  | 414 | static const preproc_ops_t sAgcOps = { | 
|  | 415 | AgcCreate, | 
|  | 416 | AgcInit, | 
|  | 417 | NULL, | 
|  | 418 | AgcEnable, | 
|  | 419 | AgcDisable, | 
|  | 420 | AgcSetParameter, | 
|  | 421 | AgcGetParameter, | 
|  | 422 | NULL | 
|  | 423 | }; | 
|  | 424 |  | 
|  | 425 |  | 
|  | 426 | //------------------------------------------------------------------------------ | 
|  | 427 | // Acoustic Echo Canceler (AEC) | 
|  | 428 | //------------------------------------------------------------------------------ | 
|  | 429 |  | 
|  | 430 | static const webrtc::EchoControlMobile::RoutingMode kAecDefaultMode = | 
|  | 431 | webrtc::EchoControlMobile::kEarpiece; | 
|  | 432 | static const bool kAecDefaultComfortNoise = true; | 
|  | 433 |  | 
|  | 434 | int  AecInit (preproc_effect_t *effect) | 
|  | 435 | { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 436 | ALOGV("AecInit"); | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 437 | webrtc::EchoControlMobile *aec = static_cast<webrtc::EchoControlMobile *>(effect->engine); | 
|  | 438 | aec->set_routing_mode(kAecDefaultMode); | 
|  | 439 | aec->enable_comfort_noise(kAecDefaultComfortNoise); | 
|  | 440 | return 0; | 
|  | 441 | } | 
|  | 442 |  | 
|  | 443 | int  AecCreate(preproc_effect_t *effect) | 
|  | 444 | { | 
|  | 445 | webrtc::EchoControlMobile *aec = effect->session->apm->echo_control_mobile(); | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 446 | ALOGV("AecCreate got aec %p", aec); | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 447 | if (aec == NULL) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 448 | ALOGW("AgcCreate Error"); | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 449 | return -ENOMEM; | 
|  | 450 | } | 
|  | 451 | effect->engine = static_cast<preproc_fx_handle_t>(aec); | 
|  | 452 | AecInit (effect); | 
|  | 453 | return 0; | 
|  | 454 | } | 
|  | 455 |  | 
| Ashok Bhat | b302bd5 | 2014-02-18 11:40:00 +0000 | [diff] [blame] | 456 | int AecGetParameter(preproc_effect_t  *effect, | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 457 | void              *pParam, | 
| Ashok Bhat | b302bd5 | 2014-02-18 11:40:00 +0000 | [diff] [blame] | 458 | uint32_t          *pValueSize, | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 459 | void              *pValue) | 
|  | 460 | { | 
|  | 461 | int status = 0; | 
|  | 462 | uint32_t param = *(uint32_t *)pParam; | 
|  | 463 |  | 
|  | 464 | if (*pValueSize < sizeof(uint32_t)) { | 
|  | 465 | return -EINVAL; | 
|  | 466 | } | 
|  | 467 | switch (param) { | 
|  | 468 | case AEC_PARAM_ECHO_DELAY: | 
|  | 469 | case AEC_PARAM_PROPERTIES: | 
|  | 470 | *(uint32_t *)pValue = 1000 * effect->session->apm->stream_delay_ms(); | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 471 | ALOGV("AecGetParameter() echo delay %d us", *(uint32_t *)pValue); | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 472 | break; | 
|  | 473 | default: | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 474 | ALOGW("AecGetParameter() unknown param %08x value %08x", param, *(uint32_t *)pValue); | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 475 | status = -EINVAL; | 
|  | 476 | break; | 
|  | 477 | } | 
|  | 478 | return status; | 
|  | 479 | } | 
|  | 480 |  | 
|  | 481 | int AecSetParameter (preproc_effect_t *effect, void *pParam, void *pValue) | 
|  | 482 | { | 
|  | 483 | int status = 0; | 
|  | 484 | uint32_t param = *(uint32_t *)pParam; | 
|  | 485 | uint32_t value = *(uint32_t *)pValue; | 
|  | 486 |  | 
|  | 487 | switch (param) { | 
|  | 488 | case AEC_PARAM_ECHO_DELAY: | 
|  | 489 | case AEC_PARAM_PROPERTIES: | 
|  | 490 | status = effect->session->apm->set_stream_delay_ms(value/1000); | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 491 | ALOGV("AecSetParameter() echo delay %d us, status %d", value, status); | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 492 | break; | 
|  | 493 | default: | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 494 | ALOGW("AecSetParameter() unknown param %08x value %08x", param, *(uint32_t *)pValue); | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 495 | status = -EINVAL; | 
|  | 496 | break; | 
|  | 497 | } | 
|  | 498 | return status; | 
|  | 499 | } | 
|  | 500 |  | 
|  | 501 | void AecEnable(preproc_effect_t *effect) | 
|  | 502 | { | 
|  | 503 | webrtc::EchoControlMobile *aec = static_cast<webrtc::EchoControlMobile *>(effect->engine); | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 504 | ALOGV("AecEnable aec %p", aec); | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 505 | aec->Enable(true); | 
|  | 506 | } | 
|  | 507 |  | 
|  | 508 | void AecDisable(preproc_effect_t *effect) | 
|  | 509 | { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 510 | ALOGV("AecDisable"); | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 511 | webrtc::EchoControlMobile *aec = static_cast<webrtc::EchoControlMobile *>(effect->engine); | 
|  | 512 | aec->Enable(false); | 
|  | 513 | } | 
|  | 514 |  | 
|  | 515 | int AecSetDevice(preproc_effect_t *effect, uint32_t device) | 
|  | 516 | { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 517 | ALOGV("AecSetDevice %08x", device); | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 518 | webrtc::EchoControlMobile *aec = static_cast<webrtc::EchoControlMobile *>(effect->engine); | 
|  | 519 | webrtc::EchoControlMobile::RoutingMode mode = webrtc::EchoControlMobile::kQuietEarpieceOrHeadset; | 
|  | 520 |  | 
| Eric Laurent | 8895925 | 2012-08-28 14:26:53 -0700 | [diff] [blame] | 521 | if (audio_is_input_device(device)) { | 
|  | 522 | return 0; | 
|  | 523 | } | 
|  | 524 |  | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 525 | switch(device) { | 
|  | 526 | case AUDIO_DEVICE_OUT_EARPIECE: | 
|  | 527 | mode = webrtc::EchoControlMobile::kEarpiece; | 
|  | 528 | break; | 
|  | 529 | case AUDIO_DEVICE_OUT_SPEAKER: | 
|  | 530 | mode = webrtc::EchoControlMobile::kSpeakerphone; | 
|  | 531 | break; | 
|  | 532 | case AUDIO_DEVICE_OUT_WIRED_HEADSET: | 
|  | 533 | case AUDIO_DEVICE_OUT_WIRED_HEADPHONE: | 
|  | 534 | default: | 
|  | 535 | break; | 
|  | 536 | } | 
|  | 537 | aec->set_routing_mode(mode); | 
|  | 538 | return 0; | 
|  | 539 | } | 
|  | 540 |  | 
|  | 541 | static const preproc_ops_t sAecOps = { | 
|  | 542 | AecCreate, | 
|  | 543 | AecInit, | 
|  | 544 | NULL, | 
|  | 545 | AecEnable, | 
|  | 546 | AecDisable, | 
|  | 547 | AecSetParameter, | 
|  | 548 | AecGetParameter, | 
|  | 549 | AecSetDevice | 
|  | 550 | }; | 
|  | 551 |  | 
|  | 552 | //------------------------------------------------------------------------------ | 
|  | 553 | // Noise Suppression (NS) | 
|  | 554 | //------------------------------------------------------------------------------ | 
|  | 555 |  | 
|  | 556 | static const webrtc::NoiseSuppression::Level kNsDefaultLevel = webrtc::NoiseSuppression::kModerate; | 
|  | 557 |  | 
|  | 558 | int  NsInit (preproc_effect_t *effect) | 
|  | 559 | { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 560 | ALOGV("NsInit"); | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 561 | webrtc::NoiseSuppression *ns = static_cast<webrtc::NoiseSuppression *>(effect->engine); | 
|  | 562 | ns->set_level(kNsDefaultLevel); | 
| Alex Luebs | 766bf73 | 2015-12-14 21:32:13 -0800 | [diff] [blame] | 563 | webrtc::Config config; | 
|  | 564 | std::vector<webrtc::Point> geometry; | 
|  | 565 | // TODO(aluebs): Make the geometry settable. | 
|  | 566 | geometry.push_back(webrtc::Point(-0.03f, 0.f, 0.f)); | 
|  | 567 | geometry.push_back(webrtc::Point(-0.01f, 0.f, 0.f)); | 
|  | 568 | geometry.push_back(webrtc::Point(0.01f, 0.f, 0.f)); | 
|  | 569 | geometry.push_back(webrtc::Point(0.03f, 0.f, 0.f)); | 
|  | 570 | // The geometry needs to be set with Beamforming enabled. | 
|  | 571 | config.Set<webrtc::Beamforming>( | 
|  | 572 | new webrtc::Beamforming(true, geometry)); | 
|  | 573 | effect->session->apm->SetExtraOptions(config); | 
|  | 574 | config.Set<webrtc::Beamforming>( | 
|  | 575 | new webrtc::Beamforming(false, geometry)); | 
|  | 576 | effect->session->apm->SetExtraOptions(config); | 
|  | 577 | effect->type = NS_TYPE_SINGLE_CHANNEL; | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 578 | return 0; | 
|  | 579 | } | 
|  | 580 |  | 
|  | 581 | int  NsCreate(preproc_effect_t *effect) | 
|  | 582 | { | 
|  | 583 | webrtc::NoiseSuppression *ns = effect->session->apm->noise_suppression(); | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 584 | ALOGV("NsCreate got ns %p", ns); | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 585 | if (ns == NULL) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 586 | ALOGW("AgcCreate Error"); | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 587 | return -ENOMEM; | 
|  | 588 | } | 
|  | 589 | effect->engine = static_cast<preproc_fx_handle_t>(ns); | 
|  | 590 | NsInit (effect); | 
|  | 591 | return 0; | 
|  | 592 | } | 
|  | 593 |  | 
| Eric Laurent | 0f714a4 | 2015-06-19 15:33:57 -0700 | [diff] [blame] | 594 | int NsGetParameter(preproc_effect_t  *effect __unused, | 
|  | 595 | void              *pParam __unused, | 
|  | 596 | uint32_t          *pValueSize __unused, | 
|  | 597 | void              *pValue __unused) | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 598 | { | 
|  | 599 | int status = 0; | 
|  | 600 | return status; | 
|  | 601 | } | 
|  | 602 |  | 
| Alex Luebs | 766bf73 | 2015-12-14 21:32:13 -0800 | [diff] [blame] | 603 | int NsSetParameter (preproc_effect_t *effect, void *pParam, void *pValue) | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 604 | { | 
|  | 605 | int status = 0; | 
| Alex Luebs | 766bf73 | 2015-12-14 21:32:13 -0800 | [diff] [blame] | 606 | webrtc::NoiseSuppression *ns = static_cast<webrtc::NoiseSuppression *>(effect->engine); | 
|  | 607 | uint32_t param = *(uint32_t *)pParam; | 
|  | 608 | uint32_t value = *(uint32_t *)pValue; | 
|  | 609 | switch(param) { | 
|  | 610 | case NS_PARAM_LEVEL: | 
|  | 611 | ns->set_level((webrtc::NoiseSuppression::Level)value); | 
|  | 612 | ALOGV("NsSetParameter() level %d", value); | 
|  | 613 | break; | 
|  | 614 | case NS_PARAM_TYPE: | 
|  | 615 | { | 
|  | 616 | webrtc::Config config; | 
|  | 617 | std::vector<webrtc::Point> geometry; | 
|  | 618 | bool is_beamforming_enabled = | 
|  | 619 | value == NS_TYPE_MULTI_CHANNEL && ns->is_enabled(); | 
|  | 620 | config.Set<webrtc::Beamforming>( | 
|  | 621 | new webrtc::Beamforming(is_beamforming_enabled, geometry)); | 
|  | 622 | effect->session->apm->SetExtraOptions(config); | 
|  | 623 | effect->type = value; | 
|  | 624 | ALOGV("NsSetParameter() type %d", value); | 
|  | 625 | break; | 
|  | 626 | } | 
|  | 627 | default: | 
|  | 628 | ALOGW("NsSetParameter() unknown param %08x value %08x", param, value); | 
|  | 629 | status = -EINVAL; | 
|  | 630 | } | 
|  | 631 |  | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 632 | return status; | 
|  | 633 | } | 
|  | 634 |  | 
|  | 635 | void NsEnable(preproc_effect_t *effect) | 
|  | 636 | { | 
|  | 637 | webrtc::NoiseSuppression *ns = static_cast<webrtc::NoiseSuppression *>(effect->engine); | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 638 | ALOGV("NsEnable ns %p", ns); | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 639 | ns->Enable(true); | 
| Alex Luebs | 766bf73 | 2015-12-14 21:32:13 -0800 | [diff] [blame] | 640 | if (effect->type == NS_TYPE_MULTI_CHANNEL) { | 
|  | 641 | webrtc::Config config; | 
|  | 642 | std::vector<webrtc::Point> geometry; | 
|  | 643 | config.Set<webrtc::Beamforming>(new webrtc::Beamforming(true, geometry)); | 
|  | 644 | effect->session->apm->SetExtraOptions(config); | 
|  | 645 | } | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 646 | } | 
|  | 647 |  | 
|  | 648 | void NsDisable(preproc_effect_t *effect) | 
|  | 649 | { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 650 | ALOGV("NsDisable"); | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 651 | webrtc::NoiseSuppression *ns = static_cast<webrtc::NoiseSuppression *>(effect->engine); | 
|  | 652 | ns->Enable(false); | 
| Alex Luebs | 766bf73 | 2015-12-14 21:32:13 -0800 | [diff] [blame] | 653 | webrtc::Config config; | 
|  | 654 | std::vector<webrtc::Point> geometry; | 
|  | 655 | config.Set<webrtc::Beamforming>(new webrtc::Beamforming(false, geometry)); | 
|  | 656 | effect->session->apm->SetExtraOptions(config); | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 657 | } | 
|  | 658 |  | 
|  | 659 | static const preproc_ops_t sNsOps = { | 
|  | 660 | NsCreate, | 
|  | 661 | NsInit, | 
|  | 662 | NULL, | 
|  | 663 | NsEnable, | 
|  | 664 | NsDisable, | 
|  | 665 | NsSetParameter, | 
|  | 666 | NsGetParameter, | 
|  | 667 | NULL | 
|  | 668 | }; | 
|  | 669 |  | 
|  | 670 |  | 
|  | 671 | static const preproc_ops_t *sPreProcOps[PREPROC_NUM_EFFECTS] = { | 
|  | 672 | &sAgcOps, | 
|  | 673 | &sAecOps, | 
|  | 674 | &sNsOps | 
|  | 675 | }; | 
|  | 676 |  | 
|  | 677 |  | 
|  | 678 | //------------------------------------------------------------------------------ | 
|  | 679 | // Effect functions | 
|  | 680 | //------------------------------------------------------------------------------ | 
|  | 681 |  | 
|  | 682 | void Session_SetProcEnabled(preproc_session_t *session, uint32_t procId, bool enabled); | 
|  | 683 |  | 
|  | 684 | extern "C" const struct effect_interface_s sEffectInterface; | 
|  | 685 | extern "C" const struct effect_interface_s sEffectInterfaceReverse; | 
|  | 686 |  | 
|  | 687 | #define BAD_STATE_ABORT(from, to) \ | 
|  | 688 | LOG_ALWAYS_FATAL("Bad state transition from %d to %d", from, to); | 
|  | 689 |  | 
|  | 690 | int Effect_SetState(preproc_effect_t *effect, uint32_t state) | 
|  | 691 | { | 
|  | 692 | int status = 0; | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 693 | ALOGV("Effect_SetState proc %d, new %d old %d", effect->procId, state, effect->state); | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 694 | switch(state) { | 
|  | 695 | case PREPROC_EFFECT_STATE_INIT: | 
|  | 696 | switch(effect->state) { | 
|  | 697 | case PREPROC_EFFECT_STATE_ACTIVE: | 
|  | 698 | effect->ops->disable(effect); | 
|  | 699 | Session_SetProcEnabled(effect->session, effect->procId, false); | 
|  | 700 | case PREPROC_EFFECT_STATE_CONFIG: | 
|  | 701 | case PREPROC_EFFECT_STATE_CREATED: | 
|  | 702 | case PREPROC_EFFECT_STATE_INIT: | 
|  | 703 | break; | 
|  | 704 | default: | 
|  | 705 | BAD_STATE_ABORT(effect->state, state); | 
|  | 706 | } | 
|  | 707 | break; | 
|  | 708 | case PREPROC_EFFECT_STATE_CREATED: | 
|  | 709 | switch(effect->state) { | 
|  | 710 | case PREPROC_EFFECT_STATE_INIT: | 
|  | 711 | status = effect->ops->create(effect); | 
|  | 712 | break; | 
|  | 713 | case PREPROC_EFFECT_STATE_CREATED: | 
|  | 714 | case PREPROC_EFFECT_STATE_ACTIVE: | 
|  | 715 | case PREPROC_EFFECT_STATE_CONFIG: | 
| Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 716 | ALOGE("Effect_SetState invalid transition"); | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 717 | status = -ENOSYS; | 
|  | 718 | break; | 
|  | 719 | default: | 
|  | 720 | BAD_STATE_ABORT(effect->state, state); | 
|  | 721 | } | 
|  | 722 | break; | 
|  | 723 | case PREPROC_EFFECT_STATE_CONFIG: | 
|  | 724 | switch(effect->state) { | 
|  | 725 | case PREPROC_EFFECT_STATE_INIT: | 
| Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 726 | ALOGE("Effect_SetState invalid transition"); | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 727 | status = -ENOSYS; | 
|  | 728 | break; | 
|  | 729 | case PREPROC_EFFECT_STATE_ACTIVE: | 
|  | 730 | effect->ops->disable(effect); | 
|  | 731 | Session_SetProcEnabled(effect->session, effect->procId, false); | 
|  | 732 | break; | 
|  | 733 | case PREPROC_EFFECT_STATE_CREATED: | 
|  | 734 | case PREPROC_EFFECT_STATE_CONFIG: | 
|  | 735 | break; | 
|  | 736 | default: | 
|  | 737 | BAD_STATE_ABORT(effect->state, state); | 
|  | 738 | } | 
|  | 739 | break; | 
|  | 740 | case PREPROC_EFFECT_STATE_ACTIVE: | 
|  | 741 | switch(effect->state) { | 
|  | 742 | case PREPROC_EFFECT_STATE_INIT: | 
|  | 743 | case PREPROC_EFFECT_STATE_CREATED: | 
| Steve Block | 29357bc | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 744 | ALOGE("Effect_SetState invalid transition"); | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 745 | status = -ENOSYS; | 
|  | 746 | break; | 
| Eric Laurent | 3f9c84c | 2012-04-03 15:36:53 -0700 | [diff] [blame] | 747 | case PREPROC_EFFECT_STATE_ACTIVE: | 
|  | 748 | // enabling an already enabled effect is just ignored | 
|  | 749 | break; | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 750 | case PREPROC_EFFECT_STATE_CONFIG: | 
|  | 751 | effect->ops->enable(effect); | 
|  | 752 | Session_SetProcEnabled(effect->session, effect->procId, true); | 
|  | 753 | break; | 
|  | 754 | default: | 
|  | 755 | BAD_STATE_ABORT(effect->state, state); | 
|  | 756 | } | 
|  | 757 | break; | 
|  | 758 | default: | 
|  | 759 | BAD_STATE_ABORT(effect->state, state); | 
|  | 760 | } | 
|  | 761 | if (status == 0) { | 
|  | 762 | effect->state = state; | 
|  | 763 | } | 
|  | 764 | return status; | 
|  | 765 | } | 
|  | 766 |  | 
|  | 767 | int Effect_Init(preproc_effect_t *effect, uint32_t procId) | 
|  | 768 | { | 
|  | 769 | if (HasReverseStream(procId)) { | 
|  | 770 | effect->itfe = &sEffectInterfaceReverse; | 
|  | 771 | } else { | 
|  | 772 | effect->itfe = &sEffectInterface; | 
|  | 773 | } | 
|  | 774 | effect->ops = sPreProcOps[procId]; | 
|  | 775 | effect->procId = procId; | 
|  | 776 | effect->state = PREPROC_EFFECT_STATE_INIT; | 
|  | 777 | return 0; | 
|  | 778 | } | 
|  | 779 |  | 
|  | 780 | int Effect_Create(preproc_effect_t *effect, | 
|  | 781 | preproc_session_t *session, | 
|  | 782 | effect_handle_t  *interface) | 
|  | 783 | { | 
|  | 784 | effect->session = session; | 
|  | 785 | *interface = (effect_handle_t)&effect->itfe; | 
|  | 786 | return Effect_SetState(effect, PREPROC_EFFECT_STATE_CREATED); | 
|  | 787 | } | 
|  | 788 |  | 
|  | 789 | int Effect_Release(preproc_effect_t *effect) | 
|  | 790 | { | 
|  | 791 | return Effect_SetState(effect, PREPROC_EFFECT_STATE_INIT); | 
|  | 792 | } | 
|  | 793 |  | 
|  | 794 |  | 
|  | 795 | //------------------------------------------------------------------------------ | 
|  | 796 | // Session functions | 
|  | 797 | //------------------------------------------------------------------------------ | 
|  | 798 |  | 
|  | 799 | #define RESAMPLER_QUALITY SPEEX_RESAMPLER_QUALITY_VOIP | 
|  | 800 |  | 
|  | 801 | static const int kPreprocDefaultSr = 16000; | 
|  | 802 | static const int kPreProcDefaultCnl = 1; | 
|  | 803 |  | 
|  | 804 | int Session_Init(preproc_session_t *session) | 
|  | 805 | { | 
|  | 806 | size_t i; | 
|  | 807 | int status = 0; | 
|  | 808 |  | 
|  | 809 | session->state = PREPROC_SESSION_STATE_INIT; | 
|  | 810 | session->id = 0; | 
|  | 811 | session->io = 0; | 
|  | 812 | session->createdMsk = 0; | 
|  | 813 | session->apm = NULL; | 
|  | 814 | for (i = 0; i < PREPROC_NUM_EFFECTS && status == 0; i++) { | 
|  | 815 | status = Effect_Init(&session->effects[i], i); | 
|  | 816 | } | 
|  | 817 | return status; | 
|  | 818 | } | 
|  | 819 |  | 
|  | 820 |  | 
|  | 821 | extern "C" int Session_CreateEffect(preproc_session_t *session, | 
|  | 822 | int32_t procId, | 
|  | 823 | effect_handle_t  *interface) | 
|  | 824 | { | 
|  | 825 | int status = -ENOMEM; | 
|  | 826 |  | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 827 | ALOGV("Session_CreateEffect procId %d, createdMsk %08x", procId, session->createdMsk); | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 828 |  | 
|  | 829 | if (session->createdMsk == 0) { | 
| Alex Luebs | 9718b7d | 2015-11-24 14:33:14 -0800 | [diff] [blame] | 830 | session->apm = webrtc::AudioProcessing::Create(); | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 831 | if (session->apm == NULL) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 832 | ALOGW("Session_CreateEffect could not get apm engine"); | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 833 | goto error; | 
|  | 834 | } | 
| Alex Luebs | 9718b7d | 2015-11-24 14:33:14 -0800 | [diff] [blame] | 835 | const webrtc::ProcessingConfig processing_config = { | 
|  | 836 | {{kPreprocDefaultSr, kPreProcDefaultCnl}, | 
|  | 837 | {kPreprocDefaultSr, kPreProcDefaultCnl}, | 
|  | 838 | {kPreprocDefaultSr, kPreProcDefaultCnl}, | 
|  | 839 | {kPreprocDefaultSr, kPreProcDefaultCnl}}}; | 
|  | 840 | session->apm->Initialize(processing_config); | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 841 | session->procFrame = new webrtc::AudioFrame(); | 
|  | 842 | if (session->procFrame == NULL) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 843 | ALOGW("Session_CreateEffect could not allocate audio frame"); | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 844 | goto error; | 
|  | 845 | } | 
|  | 846 | session->revFrame = new webrtc::AudioFrame(); | 
|  | 847 | if (session->revFrame == NULL) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 848 | ALOGW("Session_CreateEffect could not allocate reverse audio frame"); | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 849 | goto error; | 
|  | 850 | } | 
|  | 851 | session->apmSamplingRate = kPreprocDefaultSr; | 
|  | 852 | session->apmFrameCount = (kPreprocDefaultSr) / 100; | 
|  | 853 | session->frameCount = session->apmFrameCount; | 
|  | 854 | session->samplingRate = kPreprocDefaultSr; | 
|  | 855 | session->inChannelCount = kPreProcDefaultCnl; | 
|  | 856 | session->outChannelCount = kPreProcDefaultCnl; | 
| Chih-Hung Hsieh | de7fa31 | 2015-10-13 10:58:08 -0700 | [diff] [blame] | 857 | session->procFrame->sample_rate_hz_ = kPreprocDefaultSr; | 
|  | 858 | session->procFrame->num_channels_ = kPreProcDefaultCnl; | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 859 | session->revChannelCount = kPreProcDefaultCnl; | 
| Chih-Hung Hsieh | de7fa31 | 2015-10-13 10:58:08 -0700 | [diff] [blame] | 860 | session->revFrame->sample_rate_hz_ = kPreprocDefaultSr; | 
|  | 861 | session->revFrame->num_channels_ = kPreProcDefaultCnl; | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 862 | session->enabledMsk = 0; | 
|  | 863 | session->processedMsk = 0; | 
|  | 864 | session->revEnabledMsk = 0; | 
|  | 865 | session->revProcessedMsk = 0; | 
|  | 866 | session->inResampler = NULL; | 
|  | 867 | session->inBuf = NULL; | 
|  | 868 | session->inBufSize = 0; | 
|  | 869 | session->outResampler = NULL; | 
|  | 870 | session->outBuf = NULL; | 
|  | 871 | session->outBufSize = 0; | 
|  | 872 | session->revResampler = NULL; | 
|  | 873 | session->revBuf = NULL; | 
|  | 874 | session->revBufSize = 0; | 
|  | 875 | } | 
|  | 876 | status = Effect_Create(&session->effects[procId], session, interface); | 
|  | 877 | if (status < 0) { | 
|  | 878 | goto error; | 
|  | 879 | } | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 880 | ALOGV("Session_CreateEffect OK"); | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 881 | session->createdMsk |= (1<<procId); | 
|  | 882 | return status; | 
|  | 883 |  | 
|  | 884 | error: | 
|  | 885 | if (session->createdMsk == 0) { | 
|  | 886 | delete session->revFrame; | 
|  | 887 | session->revFrame = NULL; | 
|  | 888 | delete session->procFrame; | 
|  | 889 | session->procFrame = NULL; | 
| Alex Luebs | 9718b7d | 2015-11-24 14:33:14 -0800 | [diff] [blame] | 890 | delete session->apm; | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 891 | session->apm = NULL; | 
|  | 892 | } | 
|  | 893 | return status; | 
|  | 894 | } | 
|  | 895 |  | 
|  | 896 | int Session_ReleaseEffect(preproc_session_t *session, | 
|  | 897 | preproc_effect_t *fx) | 
|  | 898 | { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 899 | ALOGW_IF(Effect_Release(fx) != 0, " Effect_Release() failed for proc ID %d", fx->procId); | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 900 | session->createdMsk &= ~(1<<fx->procId); | 
|  | 901 | if (session->createdMsk == 0) { | 
| Alex Luebs | 9718b7d | 2015-11-24 14:33:14 -0800 | [diff] [blame] | 902 | delete session->apm; | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 903 | session->apm = NULL; | 
|  | 904 | delete session->procFrame; | 
|  | 905 | session->procFrame = NULL; | 
|  | 906 | delete session->revFrame; | 
|  | 907 | session->revFrame = NULL; | 
|  | 908 | if (session->inResampler != NULL) { | 
|  | 909 | speex_resampler_destroy(session->inResampler); | 
|  | 910 | session->inResampler = NULL; | 
|  | 911 | } | 
|  | 912 | if (session->outResampler != NULL) { | 
|  | 913 | speex_resampler_destroy(session->outResampler); | 
|  | 914 | session->outResampler = NULL; | 
|  | 915 | } | 
|  | 916 | if (session->revResampler != NULL) { | 
|  | 917 | speex_resampler_destroy(session->revResampler); | 
|  | 918 | session->revResampler = NULL; | 
|  | 919 | } | 
|  | 920 | delete session->inBuf; | 
|  | 921 | session->inBuf = NULL; | 
|  | 922 | delete session->outBuf; | 
|  | 923 | session->outBuf = NULL; | 
|  | 924 | delete session->revBuf; | 
|  | 925 | session->revBuf = NULL; | 
|  | 926 |  | 
|  | 927 | session->io = 0; | 
|  | 928 | } | 
|  | 929 |  | 
|  | 930 | return 0; | 
|  | 931 | } | 
|  | 932 |  | 
|  | 933 |  | 
|  | 934 | int Session_SetConfig(preproc_session_t *session, effect_config_t *config) | 
|  | 935 | { | 
| Alex Luebs | 3f11ef0 | 2016-01-15 15:51:58 -0800 | [diff] [blame] | 936 | uint32_t inCnl = audio_channel_count_from_in_mask(config->inputCfg.channels); | 
|  | 937 | uint32_t outCnl = audio_channel_count_from_in_mask(config->outputCfg.channels); | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 938 |  | 
|  | 939 | if (config->inputCfg.samplingRate != config->outputCfg.samplingRate || | 
|  | 940 | config->inputCfg.format != config->outputCfg.format || | 
|  | 941 | config->inputCfg.format != AUDIO_FORMAT_PCM_16_BIT) { | 
|  | 942 | return -EINVAL; | 
|  | 943 | } | 
|  | 944 |  | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 945 | ALOGV("Session_SetConfig sr %d cnl %08x", | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 946 | config->inputCfg.samplingRate, config->inputCfg.channels); | 
|  | 947 | int status; | 
|  | 948 |  | 
|  | 949 | // AEC implementation is limited to 16kHz | 
|  | 950 | if (config->inputCfg.samplingRate >= 32000 && !(session->createdMsk & (1 << PREPROC_AEC))) { | 
|  | 951 | session->apmSamplingRate = 32000; | 
|  | 952 | } else | 
|  | 953 | if (config->inputCfg.samplingRate >= 16000) { | 
|  | 954 | session->apmSamplingRate = 16000; | 
|  | 955 | } else if (config->inputCfg.samplingRate >= 8000) { | 
|  | 956 | session->apmSamplingRate = 8000; | 
|  | 957 | } | 
| Alex Luebs | 9718b7d | 2015-11-24 14:33:14 -0800 | [diff] [blame] | 958 |  | 
|  | 959 | const webrtc::ProcessingConfig processing_config = { | 
| Alex Luebs | 3f11ef0 | 2016-01-15 15:51:58 -0800 | [diff] [blame] | 960 | {{static_cast<int>(session->apmSamplingRate), inCnl}, | 
|  | 961 | {static_cast<int>(session->apmSamplingRate), outCnl}, | 
|  | 962 | {static_cast<int>(session->apmSamplingRate), inCnl}, | 
|  | 963 | {static_cast<int>(session->apmSamplingRate), inCnl}}}; | 
| Alex Luebs | 9718b7d | 2015-11-24 14:33:14 -0800 | [diff] [blame] | 964 | status = session->apm->Initialize(processing_config); | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 965 | if (status < 0) { | 
|  | 966 | return -EINVAL; | 
|  | 967 | } | 
|  | 968 |  | 
|  | 969 | session->samplingRate = config->inputCfg.samplingRate; | 
|  | 970 | session->apmFrameCount = session->apmSamplingRate / 100; | 
|  | 971 | if (session->samplingRate == session->apmSamplingRate) { | 
|  | 972 | session->frameCount = session->apmFrameCount; | 
|  | 973 | } else { | 
|  | 974 | session->frameCount = (session->apmFrameCount * session->samplingRate) / | 
|  | 975 | session->apmSamplingRate  + 1; | 
|  | 976 | } | 
|  | 977 | session->inChannelCount = inCnl; | 
|  | 978 | session->outChannelCount = outCnl; | 
| Chih-Hung Hsieh | de7fa31 | 2015-10-13 10:58:08 -0700 | [diff] [blame] | 979 | session->procFrame->num_channels_ = inCnl; | 
|  | 980 | session->procFrame->sample_rate_hz_ = session->apmSamplingRate; | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 981 |  | 
|  | 982 | session->revChannelCount = inCnl; | 
| Chih-Hung Hsieh | de7fa31 | 2015-10-13 10:58:08 -0700 | [diff] [blame] | 983 | session->revFrame->num_channels_ = inCnl; | 
|  | 984 | session->revFrame->sample_rate_hz_ = session->apmSamplingRate; | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 985 |  | 
| Eric Laurent | 3f9c84c | 2012-04-03 15:36:53 -0700 | [diff] [blame] | 986 | // force process buffer reallocation | 
|  | 987 | session->inBufSize = 0; | 
|  | 988 | session->outBufSize = 0; | 
|  | 989 | session->framesIn = 0; | 
|  | 990 | session->framesOut = 0; | 
|  | 991 |  | 
|  | 992 |  | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 993 | if (session->inResampler != NULL) { | 
|  | 994 | speex_resampler_destroy(session->inResampler); | 
|  | 995 | session->inResampler = NULL; | 
|  | 996 | } | 
|  | 997 | if (session->outResampler != NULL) { | 
|  | 998 | speex_resampler_destroy(session->outResampler); | 
|  | 999 | session->outResampler = NULL; | 
|  | 1000 | } | 
|  | 1001 | if (session->revResampler != NULL) { | 
|  | 1002 | speex_resampler_destroy(session->revResampler); | 
|  | 1003 | session->revResampler = NULL; | 
|  | 1004 | } | 
|  | 1005 | if (session->samplingRate != session->apmSamplingRate) { | 
|  | 1006 | int error; | 
|  | 1007 | session->inResampler = speex_resampler_init(session->inChannelCount, | 
|  | 1008 | session->samplingRate, | 
|  | 1009 | session->apmSamplingRate, | 
|  | 1010 | RESAMPLER_QUALITY, | 
|  | 1011 | &error); | 
|  | 1012 | if (session->inResampler == NULL) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1013 | ALOGW("Session_SetConfig Cannot create speex resampler: %s", | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1014 | speex_resampler_strerror(error)); | 
|  | 1015 | return -EINVAL; | 
|  | 1016 | } | 
|  | 1017 | session->outResampler = speex_resampler_init(session->outChannelCount, | 
|  | 1018 | session->apmSamplingRate, | 
|  | 1019 | session->samplingRate, | 
|  | 1020 | RESAMPLER_QUALITY, | 
|  | 1021 | &error); | 
|  | 1022 | if (session->outResampler == NULL) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1023 | ALOGW("Session_SetConfig Cannot create speex resampler: %s", | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1024 | speex_resampler_strerror(error)); | 
|  | 1025 | speex_resampler_destroy(session->inResampler); | 
|  | 1026 | session->inResampler = NULL; | 
|  | 1027 | return -EINVAL; | 
|  | 1028 | } | 
|  | 1029 | session->revResampler = speex_resampler_init(session->inChannelCount, | 
|  | 1030 | session->samplingRate, | 
|  | 1031 | session->apmSamplingRate, | 
|  | 1032 | RESAMPLER_QUALITY, | 
|  | 1033 | &error); | 
|  | 1034 | if (session->revResampler == NULL) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1035 | ALOGW("Session_SetConfig Cannot create speex resampler: %s", | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1036 | speex_resampler_strerror(error)); | 
|  | 1037 | speex_resampler_destroy(session->inResampler); | 
|  | 1038 | session->inResampler = NULL; | 
|  | 1039 | speex_resampler_destroy(session->outResampler); | 
|  | 1040 | session->outResampler = NULL; | 
|  | 1041 | return -EINVAL; | 
|  | 1042 | } | 
|  | 1043 | } | 
|  | 1044 |  | 
|  | 1045 | session->state = PREPROC_SESSION_STATE_CONFIG; | 
|  | 1046 | return 0; | 
|  | 1047 | } | 
|  | 1048 |  | 
| Eric Laurent | 3d5188b | 2011-12-16 15:30:36 -0800 | [diff] [blame] | 1049 | void Session_GetConfig(preproc_session_t *session, effect_config_t *config) | 
|  | 1050 | { | 
|  | 1051 | memset(config, 0, sizeof(effect_config_t)); | 
|  | 1052 | config->inputCfg.samplingRate = config->outputCfg.samplingRate = session->samplingRate; | 
|  | 1053 | config->inputCfg.format = config->outputCfg.format = AUDIO_FORMAT_PCM_16_BIT; | 
| Glenn Kasten | ab334fd | 2012-03-14 12:56:06 -0700 | [diff] [blame] | 1054 | config->inputCfg.channels = audio_channel_in_mask_from_count(session->inChannelCount); | 
|  | 1055 | // "out" doesn't mean output device, so this is the correct API to convert channel count to mask | 
|  | 1056 | config->outputCfg.channels = audio_channel_in_mask_from_count(session->outChannelCount); | 
| Eric Laurent | 3d5188b | 2011-12-16 15:30:36 -0800 | [diff] [blame] | 1057 | config->inputCfg.mask = config->outputCfg.mask = | 
|  | 1058 | (EFFECT_CONFIG_SMP_RATE | EFFECT_CONFIG_CHANNELS | EFFECT_CONFIG_FORMAT); | 
|  | 1059 | } | 
|  | 1060 |  | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1061 | int Session_SetReverseConfig(preproc_session_t *session, effect_config_t *config) | 
|  | 1062 | { | 
|  | 1063 | if (config->inputCfg.samplingRate != config->outputCfg.samplingRate || | 
|  | 1064 | config->inputCfg.format != config->outputCfg.format || | 
|  | 1065 | config->inputCfg.format != AUDIO_FORMAT_PCM_16_BIT) { | 
|  | 1066 | return -EINVAL; | 
|  | 1067 | } | 
|  | 1068 |  | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1069 | ALOGV("Session_SetReverseConfig sr %d cnl %08x", | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1070 | config->inputCfg.samplingRate, config->inputCfg.channels); | 
|  | 1071 |  | 
|  | 1072 | if (session->state < PREPROC_SESSION_STATE_CONFIG) { | 
|  | 1073 | return -ENOSYS; | 
|  | 1074 | } | 
|  | 1075 | if (config->inputCfg.samplingRate != session->samplingRate || | 
|  | 1076 | config->inputCfg.format != AUDIO_FORMAT_PCM_16_BIT) { | 
|  | 1077 | return -EINVAL; | 
|  | 1078 | } | 
| Andy Hung | e541269 | 2014-05-16 11:25:07 -0700 | [diff] [blame] | 1079 | uint32_t inCnl = audio_channel_count_from_out_mask(config->inputCfg.channels); | 
| Alex Luebs | 9718b7d | 2015-11-24 14:33:14 -0800 | [diff] [blame] | 1080 | const webrtc::ProcessingConfig processing_config = { | 
| Alex Luebs | 3f11ef0 | 2016-01-15 15:51:58 -0800 | [diff] [blame] | 1081 | {{static_cast<int>(session->apmSamplingRate), session->inChannelCount}, | 
|  | 1082 | {static_cast<int>(session->apmSamplingRate), session->outChannelCount}, | 
|  | 1083 | {static_cast<int>(session->apmSamplingRate), inCnl}, | 
|  | 1084 | {static_cast<int>(session->apmSamplingRate), inCnl}}}; | 
| Alex Luebs | 9718b7d | 2015-11-24 14:33:14 -0800 | [diff] [blame] | 1085 | int status = session->apm->Initialize(processing_config); | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1086 | if (status < 0) { | 
|  | 1087 | return -EINVAL; | 
|  | 1088 | } | 
|  | 1089 | session->revChannelCount = inCnl; | 
| Chih-Hung Hsieh | de7fa31 | 2015-10-13 10:58:08 -0700 | [diff] [blame] | 1090 | session->revFrame->num_channels_ = inCnl; | 
|  | 1091 | session->revFrame->sample_rate_hz_ = session->apmSamplingRate; | 
| Eric Laurent | 3f9c84c | 2012-04-03 15:36:53 -0700 | [diff] [blame] | 1092 | // force process buffer reallocation | 
|  | 1093 | session->revBufSize = 0; | 
|  | 1094 | session->framesRev = 0; | 
|  | 1095 |  | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1096 | return 0; | 
|  | 1097 | } | 
|  | 1098 |  | 
| Eric Laurent | 3d5188b | 2011-12-16 15:30:36 -0800 | [diff] [blame] | 1099 | void Session_GetReverseConfig(preproc_session_t *session, effect_config_t *config) | 
|  | 1100 | { | 
|  | 1101 | memset(config, 0, sizeof(effect_config_t)); | 
|  | 1102 | config->inputCfg.samplingRate = config->outputCfg.samplingRate = session->samplingRate; | 
|  | 1103 | config->inputCfg.format = config->outputCfg.format = AUDIO_FORMAT_PCM_16_BIT; | 
|  | 1104 | config->inputCfg.channels = config->outputCfg.channels = | 
| Glenn Kasten | ab334fd | 2012-03-14 12:56:06 -0700 | [diff] [blame] | 1105 | audio_channel_in_mask_from_count(session->revChannelCount); | 
| Eric Laurent | 3d5188b | 2011-12-16 15:30:36 -0800 | [diff] [blame] | 1106 | config->inputCfg.mask = config->outputCfg.mask = | 
|  | 1107 | (EFFECT_CONFIG_SMP_RATE | EFFECT_CONFIG_CHANNELS | EFFECT_CONFIG_FORMAT); | 
|  | 1108 | } | 
|  | 1109 |  | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1110 | void Session_SetProcEnabled(preproc_session_t *session, uint32_t procId, bool enabled) | 
|  | 1111 | { | 
|  | 1112 | if (enabled) { | 
|  | 1113 | if(session->enabledMsk == 0) { | 
|  | 1114 | session->framesIn = 0; | 
|  | 1115 | if (session->inResampler != NULL) { | 
|  | 1116 | speex_resampler_reset_mem(session->inResampler); | 
|  | 1117 | } | 
|  | 1118 | session->framesOut = 0; | 
|  | 1119 | if (session->outResampler != NULL) { | 
|  | 1120 | speex_resampler_reset_mem(session->outResampler); | 
|  | 1121 | } | 
|  | 1122 | } | 
|  | 1123 | session->enabledMsk |= (1 << procId); | 
|  | 1124 | if (HasReverseStream(procId)) { | 
|  | 1125 | session->framesRev = 0; | 
|  | 1126 | if (session->revResampler != NULL) { | 
|  | 1127 | speex_resampler_reset_mem(session->revResampler); | 
|  | 1128 | } | 
|  | 1129 | session->revEnabledMsk |= (1 << procId); | 
|  | 1130 | } | 
|  | 1131 | } else { | 
|  | 1132 | session->enabledMsk &= ~(1 << procId); | 
|  | 1133 | if (HasReverseStream(procId)) { | 
|  | 1134 | session->revEnabledMsk &= ~(1 << procId); | 
|  | 1135 | } | 
|  | 1136 | } | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1137 | ALOGV("Session_SetProcEnabled proc %d, enabled %d enabledMsk %08x revEnabledMsk %08x", | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1138 | procId, enabled, session->enabledMsk, session->revEnabledMsk); | 
|  | 1139 | session->processedMsk = 0; | 
|  | 1140 | if (HasReverseStream(procId)) { | 
|  | 1141 | session->revProcessedMsk = 0; | 
|  | 1142 | } | 
|  | 1143 | } | 
|  | 1144 |  | 
|  | 1145 | //------------------------------------------------------------------------------ | 
|  | 1146 | // Bundle functions | 
|  | 1147 | //------------------------------------------------------------------------------ | 
|  | 1148 |  | 
|  | 1149 | static int sInitStatus = 1; | 
|  | 1150 | static preproc_session_t sSessions[PREPROC_NUM_SESSIONS]; | 
|  | 1151 |  | 
|  | 1152 | preproc_session_t *PreProc_GetSession(int32_t procId, int32_t  sessionId, int32_t  ioId) | 
|  | 1153 | { | 
|  | 1154 | size_t i; | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1155 | for (i = 0; i < PREPROC_NUM_SESSIONS; i++) { | 
|  | 1156 | if (sSessions[i].io == ioId) { | 
|  | 1157 | if (sSessions[i].createdMsk & (1 << procId)) { | 
|  | 1158 | return NULL; | 
|  | 1159 | } | 
|  | 1160 | return &sSessions[i]; | 
|  | 1161 | } | 
|  | 1162 | } | 
|  | 1163 | for (i = 0; i < PREPROC_NUM_SESSIONS; i++) { | 
|  | 1164 | if (sSessions[i].io == 0) { | 
|  | 1165 | sSessions[i].id = sessionId; | 
|  | 1166 | sSessions[i].io = ioId; | 
|  | 1167 | return &sSessions[i]; | 
|  | 1168 | } | 
|  | 1169 | } | 
|  | 1170 | return NULL; | 
|  | 1171 | } | 
|  | 1172 |  | 
|  | 1173 |  | 
|  | 1174 | int PreProc_Init() { | 
|  | 1175 | size_t i; | 
|  | 1176 | int status = 0; | 
|  | 1177 |  | 
|  | 1178 | if (sInitStatus <= 0) { | 
|  | 1179 | return sInitStatus; | 
|  | 1180 | } | 
|  | 1181 | for (i = 0; i < PREPROC_NUM_SESSIONS && status == 0; i++) { | 
|  | 1182 | status = Session_Init(&sSessions[i]); | 
|  | 1183 | } | 
|  | 1184 | sInitStatus = status; | 
|  | 1185 | return sInitStatus; | 
|  | 1186 | } | 
|  | 1187 |  | 
| Glenn Kasten | 5e92a78 | 2012-01-30 07:40:52 -0800 | [diff] [blame] | 1188 | const effect_descriptor_t *PreProc_GetDescriptor(const effect_uuid_t *uuid) | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1189 | { | 
|  | 1190 | size_t i; | 
|  | 1191 | for (i = 0; i < PREPROC_NUM_EFFECTS; i++) { | 
|  | 1192 | if (memcmp(&sDescriptors[i]->uuid, uuid, sizeof(effect_uuid_t)) == 0) { | 
|  | 1193 | return sDescriptors[i]; | 
|  | 1194 | } | 
|  | 1195 | } | 
|  | 1196 | return NULL; | 
|  | 1197 | } | 
|  | 1198 |  | 
|  | 1199 |  | 
|  | 1200 | extern "C" { | 
|  | 1201 |  | 
|  | 1202 | //------------------------------------------------------------------------------ | 
|  | 1203 | // Effect Control Interface Implementation | 
|  | 1204 | //------------------------------------------------------------------------------ | 
|  | 1205 |  | 
|  | 1206 | int PreProcessingFx_Process(effect_handle_t     self, | 
|  | 1207 | audio_buffer_t    *inBuffer, | 
|  | 1208 | audio_buffer_t    *outBuffer) | 
|  | 1209 | { | 
|  | 1210 | preproc_effect_t * effect = (preproc_effect_t *)self; | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1211 |  | 
|  | 1212 | if (effect == NULL){ | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1213 | ALOGV("PreProcessingFx_Process() ERROR effect == NULL"); | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1214 | return -EINVAL; | 
|  | 1215 | } | 
|  | 1216 | preproc_session_t * session = (preproc_session_t *)effect->session; | 
|  | 1217 |  | 
|  | 1218 | if (inBuffer == NULL  || inBuffer->raw == NULL  || | 
|  | 1219 | outBuffer == NULL || outBuffer->raw == NULL){ | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1220 | ALOGW("PreProcessingFx_Process() ERROR bad pointer"); | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1221 | return -EINVAL; | 
|  | 1222 | } | 
|  | 1223 |  | 
|  | 1224 | session->processedMsk |= (1<<effect->procId); | 
|  | 1225 |  | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1226 | //    ALOGV("PreProcessingFx_Process In %d frames enabledMsk %08x processedMsk %08x", | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1227 | //         inBuffer->frameCount, session->enabledMsk, session->processedMsk); | 
|  | 1228 |  | 
|  | 1229 | if ((session->processedMsk & session->enabledMsk) == session->enabledMsk) { | 
|  | 1230 | effect->session->processedMsk = 0; | 
|  | 1231 | size_t framesRq = outBuffer->frameCount; | 
|  | 1232 | size_t framesWr = 0; | 
|  | 1233 | if (session->framesOut) { | 
|  | 1234 | size_t fr = session->framesOut; | 
|  | 1235 | if (outBuffer->frameCount < fr) { | 
|  | 1236 | fr = outBuffer->frameCount; | 
|  | 1237 | } | 
|  | 1238 | memcpy(outBuffer->s16, | 
|  | 1239 | session->outBuf, | 
|  | 1240 | fr * session->outChannelCount * sizeof(int16_t)); | 
|  | 1241 | memcpy(session->outBuf, | 
|  | 1242 | session->outBuf + fr * session->outChannelCount, | 
|  | 1243 | (session->framesOut - fr) * session->outChannelCount * sizeof(int16_t)); | 
|  | 1244 | session->framesOut -= fr; | 
|  | 1245 | framesWr += fr; | 
|  | 1246 | } | 
|  | 1247 | outBuffer->frameCount = framesWr; | 
|  | 1248 | if (framesWr == framesRq) { | 
|  | 1249 | inBuffer->frameCount = 0; | 
|  | 1250 | return 0; | 
|  | 1251 | } | 
|  | 1252 |  | 
|  | 1253 | if (session->inResampler != NULL) { | 
|  | 1254 | size_t fr = session->frameCount - session->framesIn; | 
|  | 1255 | if (inBuffer->frameCount < fr) { | 
|  | 1256 | fr = inBuffer->frameCount; | 
|  | 1257 | } | 
|  | 1258 | if (session->inBufSize < session->framesIn + fr) { | 
| Eric Laurent | 679650f | 2015-08-21 14:01:50 -0700 | [diff] [blame] | 1259 | int16_t *buf; | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1260 | session->inBufSize = session->framesIn + fr; | 
| Eric Laurent | 679650f | 2015-08-21 14:01:50 -0700 | [diff] [blame] | 1261 | buf = (int16_t *)realloc(session->inBuf, | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1262 | session->inBufSize * session->inChannelCount * sizeof(int16_t)); | 
| Eric Laurent | 679650f | 2015-08-21 14:01:50 -0700 | [diff] [blame] | 1263 | if (buf == NULL) { | 
|  | 1264 | session->framesIn = 0; | 
|  | 1265 | free(session->inBuf); | 
|  | 1266 | session->inBuf = NULL; | 
|  | 1267 | return -ENOMEM; | 
|  | 1268 | } | 
|  | 1269 | session->inBuf = buf; | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1270 | } | 
|  | 1271 | memcpy(session->inBuf + session->framesIn * session->inChannelCount, | 
|  | 1272 | inBuffer->s16, | 
|  | 1273 | fr * session->inChannelCount * sizeof(int16_t)); | 
| Eric Laurent | 3f9c84c | 2012-04-03 15:36:53 -0700 | [diff] [blame] | 1274 | #ifdef DUAL_MIC_TEST | 
|  | 1275 | pthread_mutex_lock(&gPcmDumpLock); | 
|  | 1276 | if (gPcmDumpFh != NULL) { | 
|  | 1277 | fwrite(inBuffer->raw, | 
|  | 1278 | fr * session->inChannelCount * sizeof(int16_t), 1, gPcmDumpFh); | 
|  | 1279 | } | 
|  | 1280 | pthread_mutex_unlock(&gPcmDumpLock); | 
|  | 1281 | #endif | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1282 |  | 
|  | 1283 | session->framesIn += fr; | 
|  | 1284 | inBuffer->frameCount = fr; | 
|  | 1285 | if (session->framesIn < session->frameCount) { | 
|  | 1286 | return 0; | 
|  | 1287 | } | 
| Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 1288 | spx_uint32_t frIn = session->framesIn; | 
|  | 1289 | spx_uint32_t frOut = session->apmFrameCount; | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1290 | if (session->inChannelCount == 1) { | 
|  | 1291 | speex_resampler_process_int(session->inResampler, | 
|  | 1292 | 0, | 
|  | 1293 | session->inBuf, | 
|  | 1294 | &frIn, | 
| Chih-Hung Hsieh | de7fa31 | 2015-10-13 10:58:08 -0700 | [diff] [blame] | 1295 | session->procFrame->data_, | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1296 | &frOut); | 
|  | 1297 | } else { | 
|  | 1298 | speex_resampler_process_interleaved_int(session->inResampler, | 
|  | 1299 | session->inBuf, | 
|  | 1300 | &frIn, | 
| Chih-Hung Hsieh | de7fa31 | 2015-10-13 10:58:08 -0700 | [diff] [blame] | 1301 | session->procFrame->data_, | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1302 | &frOut); | 
|  | 1303 | } | 
|  | 1304 | memcpy(session->inBuf, | 
|  | 1305 | session->inBuf + frIn * session->inChannelCount, | 
|  | 1306 | (session->framesIn - frIn) * session->inChannelCount * sizeof(int16_t)); | 
|  | 1307 | session->framesIn -= frIn; | 
|  | 1308 | } else { | 
|  | 1309 | size_t fr = session->frameCount - session->framesIn; | 
|  | 1310 | if (inBuffer->frameCount < fr) { | 
|  | 1311 | fr = inBuffer->frameCount; | 
|  | 1312 | } | 
| Chih-Hung Hsieh | de7fa31 | 2015-10-13 10:58:08 -0700 | [diff] [blame] | 1313 | memcpy(session->procFrame->data_ + session->framesIn * session->inChannelCount, | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1314 | inBuffer->s16, | 
|  | 1315 | fr * session->inChannelCount * sizeof(int16_t)); | 
| Eric Laurent | 3f9c84c | 2012-04-03 15:36:53 -0700 | [diff] [blame] | 1316 |  | 
|  | 1317 | #ifdef DUAL_MIC_TEST | 
|  | 1318 | pthread_mutex_lock(&gPcmDumpLock); | 
|  | 1319 | if (gPcmDumpFh != NULL) { | 
|  | 1320 | fwrite(inBuffer->raw, | 
|  | 1321 | fr * session->inChannelCount * sizeof(int16_t), 1, gPcmDumpFh); | 
|  | 1322 | } | 
|  | 1323 | pthread_mutex_unlock(&gPcmDumpLock); | 
|  | 1324 | #endif | 
|  | 1325 |  | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1326 | session->framesIn += fr; | 
|  | 1327 | inBuffer->frameCount = fr; | 
|  | 1328 | if (session->framesIn < session->frameCount) { | 
|  | 1329 | return 0; | 
|  | 1330 | } | 
|  | 1331 | session->framesIn = 0; | 
|  | 1332 | } | 
| Alex Luebs | 766bf73 | 2015-12-14 21:32:13 -0800 | [diff] [blame] | 1333 | session->procFrame->samples_per_channel_ = session->apmFrameCount; | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1334 |  | 
|  | 1335 | effect->session->apm->ProcessStream(session->procFrame); | 
|  | 1336 |  | 
|  | 1337 | if (session->outBufSize < session->framesOut + session->frameCount) { | 
| Eric Laurent | 679650f | 2015-08-21 14:01:50 -0700 | [diff] [blame] | 1338 | int16_t *buf; | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1339 | session->outBufSize = session->framesOut + session->frameCount; | 
| Eric Laurent | 679650f | 2015-08-21 14:01:50 -0700 | [diff] [blame] | 1340 | buf = (int16_t *)realloc(session->outBuf, | 
|  | 1341 | session->outBufSize * session->outChannelCount * sizeof(int16_t)); | 
|  | 1342 | if (buf == NULL) { | 
|  | 1343 | session->framesOut = 0; | 
|  | 1344 | free(session->outBuf); | 
|  | 1345 | session->outBuf = NULL; | 
|  | 1346 | return -ENOMEM; | 
|  | 1347 | } | 
|  | 1348 | session->outBuf = buf; | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1349 | } | 
|  | 1350 |  | 
|  | 1351 | if (session->outResampler != NULL) { | 
| Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 1352 | spx_uint32_t frIn = session->apmFrameCount; | 
|  | 1353 | spx_uint32_t frOut = session->frameCount; | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1354 | if (session->inChannelCount == 1) { | 
|  | 1355 | speex_resampler_process_int(session->outResampler, | 
|  | 1356 | 0, | 
| Chih-Hung Hsieh | de7fa31 | 2015-10-13 10:58:08 -0700 | [diff] [blame] | 1357 | session->procFrame->data_, | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1358 | &frIn, | 
|  | 1359 | session->outBuf + session->framesOut * session->outChannelCount, | 
|  | 1360 | &frOut); | 
|  | 1361 | } else { | 
|  | 1362 | speex_resampler_process_interleaved_int(session->outResampler, | 
| Chih-Hung Hsieh | de7fa31 | 2015-10-13 10:58:08 -0700 | [diff] [blame] | 1363 | session->procFrame->data_, | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1364 | &frIn, | 
|  | 1365 | session->outBuf + session->framesOut * session->outChannelCount, | 
|  | 1366 | &frOut); | 
|  | 1367 | } | 
|  | 1368 | session->framesOut += frOut; | 
|  | 1369 | } else { | 
|  | 1370 | memcpy(session->outBuf + session->framesOut * session->outChannelCount, | 
| Chih-Hung Hsieh | de7fa31 | 2015-10-13 10:58:08 -0700 | [diff] [blame] | 1371 | session->procFrame->data_, | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1372 | session->frameCount * session->outChannelCount * sizeof(int16_t)); | 
|  | 1373 | session->framesOut += session->frameCount; | 
|  | 1374 | } | 
|  | 1375 | size_t fr = session->framesOut; | 
|  | 1376 | if (framesRq - framesWr < fr) { | 
|  | 1377 | fr = framesRq - framesWr; | 
|  | 1378 | } | 
|  | 1379 | memcpy(outBuffer->s16 + framesWr * session->outChannelCount, | 
|  | 1380 | session->outBuf, | 
|  | 1381 | fr * session->outChannelCount * sizeof(int16_t)); | 
|  | 1382 | memcpy(session->outBuf, | 
|  | 1383 | session->outBuf + fr * session->outChannelCount, | 
|  | 1384 | (session->framesOut - fr) * session->outChannelCount * sizeof(int16_t)); | 
|  | 1385 | session->framesOut -= fr; | 
|  | 1386 | outBuffer->frameCount += fr; | 
|  | 1387 |  | 
|  | 1388 | return 0; | 
|  | 1389 | } else { | 
|  | 1390 | return -ENODATA; | 
|  | 1391 | } | 
|  | 1392 | } | 
|  | 1393 |  | 
|  | 1394 | int PreProcessingFx_Command(effect_handle_t  self, | 
|  | 1395 | uint32_t            cmdCode, | 
|  | 1396 | uint32_t            cmdSize, | 
|  | 1397 | void                *pCmdData, | 
|  | 1398 | uint32_t            *replySize, | 
|  | 1399 | void                *pReplyData) | 
|  | 1400 | { | 
|  | 1401 | preproc_effect_t * effect = (preproc_effect_t *) self; | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1402 |  | 
|  | 1403 | if (effect == NULL){ | 
|  | 1404 | return -EINVAL; | 
|  | 1405 | } | 
|  | 1406 |  | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1407 | //ALOGV("PreProcessingFx_Command: command %d cmdSize %d",cmdCode, cmdSize); | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1408 |  | 
|  | 1409 | switch (cmdCode){ | 
|  | 1410 | case EFFECT_CMD_INIT: | 
|  | 1411 | if (pReplyData == NULL || *replySize != sizeof(int)){ | 
|  | 1412 | return -EINVAL; | 
|  | 1413 | } | 
|  | 1414 | if (effect->ops->init) { | 
|  | 1415 | effect->ops->init(effect); | 
|  | 1416 | } | 
|  | 1417 | *(int *)pReplyData = 0; | 
|  | 1418 | break; | 
|  | 1419 |  | 
| Eric Laurent | 3f9c84c | 2012-04-03 15:36:53 -0700 | [diff] [blame] | 1420 | case EFFECT_CMD_SET_CONFIG: { | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1421 | if (pCmdData    == NULL|| | 
|  | 1422 | cmdSize     != sizeof(effect_config_t)|| | 
|  | 1423 | pReplyData  == NULL|| | 
|  | 1424 | *replySize  != sizeof(int)){ | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1425 | ALOGV("PreProcessingFx_Command cmdCode Case: " | 
| Eric Laurent | 3d5188b | 2011-12-16 15:30:36 -0800 | [diff] [blame] | 1426 | "EFFECT_CMD_SET_CONFIG: ERROR"); | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1427 | return -EINVAL; | 
|  | 1428 | } | 
| Eric Laurent | 3f9c84c | 2012-04-03 15:36:53 -0700 | [diff] [blame] | 1429 | #ifdef DUAL_MIC_TEST | 
|  | 1430 | // make sure that the config command is accepted by making as if all effects were | 
|  | 1431 | // disabled: this is OK for functional tests | 
|  | 1432 | uint32_t enabledMsk = effect->session->enabledMsk; | 
|  | 1433 | if (gDualMicEnabled) { | 
|  | 1434 | effect->session->enabledMsk = 0; | 
|  | 1435 | } | 
|  | 1436 | #endif | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1437 | *(int *)pReplyData = Session_SetConfig(effect->session, (effect_config_t *)pCmdData); | 
| Eric Laurent | 3f9c84c | 2012-04-03 15:36:53 -0700 | [diff] [blame] | 1438 | #ifdef DUAL_MIC_TEST | 
|  | 1439 | if (gDualMicEnabled) { | 
|  | 1440 | effect->session->enabledMsk = enabledMsk; | 
|  | 1441 | } | 
|  | 1442 | #endif | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1443 | if (*(int *)pReplyData != 0) { | 
|  | 1444 | break; | 
|  | 1445 | } | 
| Eric Laurent | 76533e9 | 2012-02-17 17:52:04 -0800 | [diff] [blame] | 1446 | if (effect->state != PREPROC_EFFECT_STATE_ACTIVE) { | 
|  | 1447 | *(int *)pReplyData = Effect_SetState(effect, PREPROC_EFFECT_STATE_CONFIG); | 
|  | 1448 | } | 
| Eric Laurent | 3f9c84c | 2012-04-03 15:36:53 -0700 | [diff] [blame] | 1449 | } break; | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1450 |  | 
| Eric Laurent | 3d5188b | 2011-12-16 15:30:36 -0800 | [diff] [blame] | 1451 | case EFFECT_CMD_GET_CONFIG: | 
|  | 1452 | if (pReplyData == NULL || | 
|  | 1453 | *replySize != sizeof(effect_config_t)) { | 
|  | 1454 | ALOGV("\tLVM_ERROR : PreProcessingFx_Command cmdCode Case: " | 
|  | 1455 | "EFFECT_CMD_GET_CONFIG: ERROR"); | 
|  | 1456 | return -EINVAL; | 
|  | 1457 | } | 
|  | 1458 |  | 
| Eric Laurent | 94fef38 | 2012-02-06 14:28:54 -0800 | [diff] [blame] | 1459 | Session_GetConfig(effect->session, (effect_config_t *)pReplyData); | 
| Eric Laurent | 3d5188b | 2011-12-16 15:30:36 -0800 | [diff] [blame] | 1460 | break; | 
|  | 1461 |  | 
|  | 1462 | case EFFECT_CMD_SET_CONFIG_REVERSE: | 
|  | 1463 | if (pCmdData == NULL || | 
|  | 1464 | cmdSize != sizeof(effect_config_t) || | 
|  | 1465 | pReplyData == NULL || | 
|  | 1466 | *replySize != sizeof(int)) { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1467 | ALOGV("PreProcessingFx_Command cmdCode Case: " | 
| Eric Laurent | 3d5188b | 2011-12-16 15:30:36 -0800 | [diff] [blame] | 1468 | "EFFECT_CMD_SET_CONFIG_REVERSE: ERROR"); | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1469 | return -EINVAL; | 
|  | 1470 | } | 
|  | 1471 | *(int *)pReplyData = Session_SetReverseConfig(effect->session, | 
|  | 1472 | (effect_config_t *)pCmdData); | 
|  | 1473 | if (*(int *)pReplyData != 0) { | 
|  | 1474 | break; | 
|  | 1475 | } | 
|  | 1476 | break; | 
|  | 1477 |  | 
| Eric Laurent | 3d5188b | 2011-12-16 15:30:36 -0800 | [diff] [blame] | 1478 | case EFFECT_CMD_GET_CONFIG_REVERSE: | 
|  | 1479 | if (pReplyData == NULL || | 
|  | 1480 | *replySize != sizeof(effect_config_t)){ | 
|  | 1481 | ALOGV("PreProcessingFx_Command cmdCode Case: " | 
|  | 1482 | "EFFECT_CMD_GET_CONFIG_REVERSE: ERROR"); | 
|  | 1483 | return -EINVAL; | 
|  | 1484 | } | 
|  | 1485 | Session_GetReverseConfig(effect->session, (effect_config_t *)pCmdData); | 
|  | 1486 | break; | 
|  | 1487 |  | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1488 | case EFFECT_CMD_RESET: | 
|  | 1489 | if (effect->ops->reset) { | 
|  | 1490 | effect->ops->reset(effect); | 
|  | 1491 | } | 
|  | 1492 | break; | 
|  | 1493 |  | 
| Eric Laurent | 0f714a4 | 2015-06-19 15:33:57 -0700 | [diff] [blame] | 1494 | case EFFECT_CMD_GET_PARAM: { | 
|  | 1495 | effect_param_t *p = (effect_param_t *)pCmdData; | 
|  | 1496 |  | 
|  | 1497 | if (pCmdData == NULL || cmdSize < sizeof(effect_param_t) || | 
|  | 1498 | cmdSize < (sizeof(effect_param_t) + p->psize) || | 
|  | 1499 | pReplyData == NULL || replySize == NULL || | 
|  | 1500 | *replySize < (sizeof(effect_param_t) + p->psize)){ | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1501 | ALOGV("PreProcessingFx_Command cmdCode Case: " | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1502 | "EFFECT_CMD_GET_PARAM: ERROR"); | 
|  | 1503 | return -EINVAL; | 
|  | 1504 | } | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1505 |  | 
|  | 1506 | memcpy(pReplyData, pCmdData, sizeof(effect_param_t) + p->psize); | 
|  | 1507 |  | 
|  | 1508 | p = (effect_param_t *)pReplyData; | 
|  | 1509 |  | 
|  | 1510 | int voffset = ((p->psize - 1) / sizeof(int32_t) + 1) * sizeof(int32_t); | 
|  | 1511 |  | 
|  | 1512 | if (effect->ops->get_parameter) { | 
|  | 1513 | p->status = effect->ops->get_parameter(effect, p->data, | 
| Ashok Bhat | b302bd5 | 2014-02-18 11:40:00 +0000 | [diff] [blame] | 1514 | &p->vsize, | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1515 | p->data + voffset); | 
|  | 1516 | *replySize = sizeof(effect_param_t) + voffset + p->vsize; | 
|  | 1517 | } | 
|  | 1518 | } break; | 
|  | 1519 |  | 
|  | 1520 | case EFFECT_CMD_SET_PARAM:{ | 
|  | 1521 | if (pCmdData == NULL|| | 
| Eric Laurent | 0f714a4 | 2015-06-19 15:33:57 -0700 | [diff] [blame] | 1522 | cmdSize < sizeof(effect_param_t) || | 
|  | 1523 | pReplyData == NULL || replySize == NULL || | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1524 | *replySize != sizeof(int32_t)){ | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1525 | ALOGV("PreProcessingFx_Command cmdCode Case: " | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1526 | "EFFECT_CMD_SET_PARAM: ERROR"); | 
|  | 1527 | return -EINVAL; | 
|  | 1528 | } | 
|  | 1529 | effect_param_t *p = (effect_param_t *) pCmdData; | 
|  | 1530 |  | 
|  | 1531 | if (p->psize != sizeof(int32_t)){ | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1532 | ALOGV("PreProcessingFx_Command cmdCode Case: " | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1533 | "EFFECT_CMD_SET_PARAM: ERROR, psize is not sizeof(int32_t)"); | 
|  | 1534 | return -EINVAL; | 
|  | 1535 | } | 
|  | 1536 | if (effect->ops->set_parameter) { | 
|  | 1537 | *(int *)pReplyData = effect->ops->set_parameter(effect, | 
|  | 1538 | (void *)p->data, | 
|  | 1539 | p->data + p->psize); | 
|  | 1540 | } | 
|  | 1541 | } break; | 
|  | 1542 |  | 
|  | 1543 | case EFFECT_CMD_ENABLE: | 
| Eric Laurent | 0f714a4 | 2015-06-19 15:33:57 -0700 | [diff] [blame] | 1544 | if (pReplyData == NULL || replySize == NULL || *replySize != sizeof(int)){ | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1545 | ALOGV("PreProcessingFx_Command cmdCode Case: EFFECT_CMD_ENABLE: ERROR"); | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1546 | return -EINVAL; | 
|  | 1547 | } | 
|  | 1548 | *(int *)pReplyData = Effect_SetState(effect, PREPROC_EFFECT_STATE_ACTIVE); | 
|  | 1549 | break; | 
|  | 1550 |  | 
|  | 1551 | case EFFECT_CMD_DISABLE: | 
| Eric Laurent | 0f714a4 | 2015-06-19 15:33:57 -0700 | [diff] [blame] | 1552 | if (pReplyData == NULL || replySize == NULL || *replySize != sizeof(int)){ | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1553 | ALOGV("PreProcessingFx_Command cmdCode Case: EFFECT_CMD_DISABLE: ERROR"); | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1554 | return -EINVAL; | 
|  | 1555 | } | 
|  | 1556 | *(int *)pReplyData  = Effect_SetState(effect, PREPROC_EFFECT_STATE_CONFIG); | 
|  | 1557 | break; | 
|  | 1558 |  | 
|  | 1559 | case EFFECT_CMD_SET_DEVICE: | 
|  | 1560 | case EFFECT_CMD_SET_INPUT_DEVICE: | 
|  | 1561 | if (pCmdData == NULL || | 
|  | 1562 | cmdSize != sizeof(uint32_t)) { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1563 | ALOGV("PreProcessingFx_Command cmdCode Case: EFFECT_CMD_SET_DEVICE: ERROR"); | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1564 | return -EINVAL; | 
|  | 1565 | } | 
|  | 1566 |  | 
|  | 1567 | if (effect->ops->set_device) { | 
|  | 1568 | effect->ops->set_device(effect, *(uint32_t *)pCmdData); | 
|  | 1569 | } | 
|  | 1570 | break; | 
|  | 1571 |  | 
|  | 1572 | case EFFECT_CMD_SET_VOLUME: | 
|  | 1573 | case EFFECT_CMD_SET_AUDIO_MODE: | 
|  | 1574 | break; | 
|  | 1575 |  | 
| Eric Laurent | 3f9c84c | 2012-04-03 15:36:53 -0700 | [diff] [blame] | 1576 | #ifdef DUAL_MIC_TEST | 
|  | 1577 | ///// test commands start | 
|  | 1578 | case PREPROC_CMD_DUAL_MIC_ENABLE: { | 
|  | 1579 | if (pCmdData == NULL|| cmdSize != sizeof(uint32_t) || | 
|  | 1580 | pReplyData == NULL || replySize == NULL) { | 
|  | 1581 | ALOGE("PreProcessingFx_Command cmdCode Case: " | 
|  | 1582 | "PREPROC_CMD_DUAL_MIC_ENABLE: ERROR"); | 
|  | 1583 | *replySize = 0; | 
|  | 1584 | return -EINVAL; | 
|  | 1585 | } | 
|  | 1586 | gDualMicEnabled = *(bool *)pCmdData; | 
|  | 1587 | if (gDualMicEnabled) { | 
|  | 1588 | effect->aux_channels_on = sHasAuxChannels[effect->procId]; | 
|  | 1589 | } else { | 
|  | 1590 | effect->aux_channels_on = false; | 
|  | 1591 | } | 
|  | 1592 | effect->cur_channel_config = (effect->session->inChannelCount == 1) ? | 
|  | 1593 | CHANNEL_CFG_MONO : CHANNEL_CFG_STEREO; | 
|  | 1594 |  | 
|  | 1595 | ALOGV("PREPROC_CMD_DUAL_MIC_ENABLE: %s", gDualMicEnabled ? "enabled" : "disabled"); | 
|  | 1596 | *replySize = sizeof(int); | 
|  | 1597 | *(int *)pReplyData = 0; | 
|  | 1598 | } break; | 
|  | 1599 | case PREPROC_CMD_DUAL_MIC_PCM_DUMP_START: { | 
|  | 1600 | if (pCmdData == NULL|| pReplyData == NULL || replySize == NULL) { | 
|  | 1601 | ALOGE("PreProcessingFx_Command cmdCode Case: " | 
|  | 1602 | "PREPROC_CMD_DUAL_MIC_PCM_DUMP_START: ERROR"); | 
|  | 1603 | *replySize = 0; | 
|  | 1604 | return -EINVAL; | 
|  | 1605 | } | 
|  | 1606 | pthread_mutex_lock(&gPcmDumpLock); | 
|  | 1607 | if (gPcmDumpFh != NULL) { | 
|  | 1608 | fclose(gPcmDumpFh); | 
|  | 1609 | gPcmDumpFh = NULL; | 
|  | 1610 | } | 
|  | 1611 | char *path = strndup((char *)pCmdData, cmdSize); | 
|  | 1612 | gPcmDumpFh = fopen((char *)path, "wb"); | 
|  | 1613 | pthread_mutex_unlock(&gPcmDumpLock); | 
|  | 1614 | ALOGV("PREPROC_CMD_DUAL_MIC_PCM_DUMP_START: path %s gPcmDumpFh %p", | 
|  | 1615 | path, gPcmDumpFh); | 
|  | 1616 | ALOGE_IF(gPcmDumpFh <= 0, "gPcmDumpFh open error %d %s", errno, strerror(errno)); | 
|  | 1617 | free(path); | 
|  | 1618 | *replySize = sizeof(int); | 
|  | 1619 | *(int *)pReplyData = 0; | 
|  | 1620 | } break; | 
|  | 1621 | case PREPROC_CMD_DUAL_MIC_PCM_DUMP_STOP: { | 
|  | 1622 | if (pReplyData == NULL || replySize == NULL) { | 
|  | 1623 | ALOGE("PreProcessingFx_Command cmdCode Case: " | 
|  | 1624 | "PREPROC_CMD_DUAL_MIC_PCM_DUMP_STOP: ERROR"); | 
|  | 1625 | *replySize = 0; | 
|  | 1626 | return -EINVAL; | 
|  | 1627 | } | 
|  | 1628 | pthread_mutex_lock(&gPcmDumpLock); | 
|  | 1629 | if (gPcmDumpFh != NULL) { | 
|  | 1630 | fclose(gPcmDumpFh); | 
|  | 1631 | gPcmDumpFh = NULL; | 
|  | 1632 | } | 
|  | 1633 | pthread_mutex_unlock(&gPcmDumpLock); | 
|  | 1634 | ALOGV("PREPROC_CMD_DUAL_MIC_PCM_DUMP_STOP"); | 
|  | 1635 | *replySize = sizeof(int); | 
|  | 1636 | *(int *)pReplyData = 0; | 
|  | 1637 | } break; | 
|  | 1638 | ///// test commands end | 
|  | 1639 |  | 
|  | 1640 | case EFFECT_CMD_GET_FEATURE_SUPPORTED_CONFIGS: { | 
|  | 1641 | if(!gDualMicEnabled) { | 
|  | 1642 | return -EINVAL; | 
|  | 1643 | } | 
|  | 1644 | if (pCmdData == NULL|| cmdSize != 2 * sizeof(uint32_t) || | 
|  | 1645 | pReplyData == NULL || replySize == NULL) { | 
|  | 1646 | ALOGE("PreProcessingFx_Command cmdCode Case: " | 
|  | 1647 | "EFFECT_CMD_GET_FEATURE_SUPPORTED_CONFIGS: ERROR"); | 
|  | 1648 | *replySize = 0; | 
|  | 1649 | return -EINVAL; | 
|  | 1650 | } | 
|  | 1651 | if (*(uint32_t *)pCmdData != EFFECT_FEATURE_AUX_CHANNELS || | 
|  | 1652 | !effect->aux_channels_on) { | 
|  | 1653 | ALOGV("PreProcessingFx_Command feature EFFECT_FEATURE_AUX_CHANNELS not supported by" | 
|  | 1654 | " fx %d", effect->procId); | 
|  | 1655 | *(uint32_t *)pReplyData = -ENOSYS; | 
|  | 1656 | *replySize = sizeof(uint32_t); | 
|  | 1657 | break; | 
|  | 1658 | } | 
|  | 1659 | size_t num_configs = *((uint32_t *)pCmdData + 1); | 
|  | 1660 | if (*replySize < (2 * sizeof(uint32_t) + | 
|  | 1661 | num_configs * sizeof(channel_config_t))) { | 
|  | 1662 | *replySize = 0; | 
|  | 1663 | return -EINVAL; | 
|  | 1664 | } | 
|  | 1665 |  | 
|  | 1666 | *((uint32_t *)pReplyData + 1) = CHANNEL_CFG_CNT; | 
|  | 1667 | if (num_configs < CHANNEL_CFG_CNT || | 
|  | 1668 | *replySize < (2 * sizeof(uint32_t) + | 
|  | 1669 | CHANNEL_CFG_CNT * sizeof(channel_config_t))) { | 
|  | 1670 | *(uint32_t *)pReplyData = -ENOMEM; | 
|  | 1671 | } else { | 
|  | 1672 | num_configs = CHANNEL_CFG_CNT; | 
|  | 1673 | *(uint32_t *)pReplyData = 0; | 
|  | 1674 | } | 
|  | 1675 | ALOGV("PreProcessingFx_Command EFFECT_CMD_GET_FEATURE_SUPPORTED_CONFIGS num config %d", | 
|  | 1676 | num_configs); | 
|  | 1677 |  | 
|  | 1678 | *replySize = 2 * sizeof(uint32_t) + num_configs * sizeof(channel_config_t); | 
|  | 1679 | *((uint32_t *)pReplyData + 1) = num_configs; | 
|  | 1680 | memcpy((uint32_t *)pReplyData + 2, &sDualMicConfigs, num_configs * sizeof(channel_config_t)); | 
|  | 1681 | } break; | 
|  | 1682 | case EFFECT_CMD_GET_FEATURE_CONFIG: | 
|  | 1683 | if(!gDualMicEnabled) { | 
|  | 1684 | return -EINVAL; | 
|  | 1685 | } | 
|  | 1686 | if (pCmdData == NULL|| cmdSize != sizeof(uint32_t) || | 
|  | 1687 | pReplyData == NULL || replySize == NULL || | 
|  | 1688 | *replySize < sizeof(uint32_t) + sizeof(channel_config_t)) { | 
|  | 1689 | ALOGE("PreProcessingFx_Command cmdCode Case: " | 
|  | 1690 | "EFFECT_CMD_GET_FEATURE_CONFIG: ERROR"); | 
|  | 1691 | return -EINVAL; | 
|  | 1692 | } | 
|  | 1693 | if (*(uint32_t *)pCmdData != EFFECT_FEATURE_AUX_CHANNELS || !effect->aux_channels_on) { | 
|  | 1694 | *(uint32_t *)pReplyData = -ENOSYS; | 
|  | 1695 | *replySize = sizeof(uint32_t); | 
|  | 1696 | break; | 
|  | 1697 | } | 
|  | 1698 | ALOGV("PreProcessingFx_Command EFFECT_CMD_GET_FEATURE_CONFIG"); | 
|  | 1699 | *(uint32_t *)pReplyData = 0; | 
|  | 1700 | *replySize = sizeof(uint32_t) + sizeof(channel_config_t); | 
|  | 1701 | memcpy((uint32_t *)pReplyData + 1, | 
|  | 1702 | &sDualMicConfigs[effect->cur_channel_config], | 
|  | 1703 | sizeof(channel_config_t)); | 
|  | 1704 | break; | 
|  | 1705 | case EFFECT_CMD_SET_FEATURE_CONFIG: { | 
|  | 1706 | ALOGV("PreProcessingFx_Command EFFECT_CMD_SET_FEATURE_CONFIG: " | 
|  | 1707 | "gDualMicEnabled %d effect->aux_channels_on %d", | 
|  | 1708 | gDualMicEnabled, effect->aux_channels_on); | 
|  | 1709 | if(!gDualMicEnabled) { | 
|  | 1710 | return -EINVAL; | 
|  | 1711 | } | 
|  | 1712 | if (pCmdData == NULL|| cmdSize != (sizeof(uint32_t) + sizeof(channel_config_t)) || | 
|  | 1713 | pReplyData == NULL || replySize == NULL || | 
|  | 1714 | *replySize < sizeof(uint32_t)) { | 
|  | 1715 | ALOGE("PreProcessingFx_Command cmdCode Case: " | 
|  | 1716 | "EFFECT_CMD_SET_FEATURE_CONFIG: ERROR\n" | 
|  | 1717 | "pCmdData %p cmdSize %d pReplyData %p replySize %p *replySize %d", | 
|  | 1718 | pCmdData, cmdSize, pReplyData, replySize, replySize ? *replySize : -1); | 
|  | 1719 | return -EINVAL; | 
|  | 1720 | } | 
|  | 1721 | *replySize = sizeof(uint32_t); | 
|  | 1722 | if (*(uint32_t *)pCmdData != EFFECT_FEATURE_AUX_CHANNELS || !effect->aux_channels_on) { | 
|  | 1723 | *(uint32_t *)pReplyData = -ENOSYS; | 
|  | 1724 | ALOGV("PreProcessingFx_Command cmdCode Case: " | 
|  | 1725 | "EFFECT_CMD_SET_FEATURE_CONFIG: ERROR\n" | 
|  | 1726 | "CmdData %d effect->aux_channels_on %d", | 
|  | 1727 | *(uint32_t *)pCmdData, effect->aux_channels_on); | 
|  | 1728 | break; | 
|  | 1729 | } | 
|  | 1730 | size_t i; | 
|  | 1731 | for (i = 0; i < CHANNEL_CFG_CNT;i++) { | 
|  | 1732 | if (memcmp((uint32_t *)pCmdData + 1, | 
|  | 1733 | &sDualMicConfigs[i], sizeof(channel_config_t)) == 0) { | 
|  | 1734 | break; | 
|  | 1735 | } | 
|  | 1736 | } | 
|  | 1737 | if (i == CHANNEL_CFG_CNT) { | 
|  | 1738 | *(uint32_t *)pReplyData = -EINVAL; | 
|  | 1739 | ALOGW("PreProcessingFx_Command EFFECT_CMD_SET_FEATURE_CONFIG invalid config" | 
|  | 1740 | "[%08x].[%08x]", *((uint32_t *)pCmdData + 1), *((uint32_t *)pCmdData + 2)); | 
|  | 1741 | } else { | 
|  | 1742 | effect->cur_channel_config = i; | 
|  | 1743 | *(uint32_t *)pReplyData = 0; | 
|  | 1744 | ALOGV("PreProcessingFx_Command EFFECT_CMD_SET_FEATURE_CONFIG New config" | 
|  | 1745 | "[%08x].[%08x]", sDualMicConfigs[i].main_channels, sDualMicConfigs[i].aux_channels); | 
|  | 1746 | } | 
|  | 1747 | } break; | 
|  | 1748 | #endif | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1749 | default: | 
|  | 1750 | return -EINVAL; | 
|  | 1751 | } | 
|  | 1752 | return 0; | 
|  | 1753 | } | 
|  | 1754 |  | 
|  | 1755 |  | 
|  | 1756 | int PreProcessingFx_GetDescriptor(effect_handle_t   self, | 
|  | 1757 | effect_descriptor_t *pDescriptor) | 
|  | 1758 | { | 
|  | 1759 | preproc_effect_t * effect = (preproc_effect_t *) self; | 
|  | 1760 |  | 
|  | 1761 | if (effect == NULL || pDescriptor == NULL) { | 
|  | 1762 | return -EINVAL; | 
|  | 1763 | } | 
|  | 1764 |  | 
| Glenn Kasten | a189a68 | 2012-02-20 12:16:30 -0800 | [diff] [blame] | 1765 | *pDescriptor = *sDescriptors[effect->procId]; | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1766 |  | 
|  | 1767 | return 0; | 
|  | 1768 | } | 
|  | 1769 |  | 
|  | 1770 | int PreProcessingFx_ProcessReverse(effect_handle_t     self, | 
|  | 1771 | audio_buffer_t    *inBuffer, | 
| Eric Laurent | 0f714a4 | 2015-06-19 15:33:57 -0700 | [diff] [blame] | 1772 | audio_buffer_t    *outBuffer __unused) | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1773 | { | 
|  | 1774 | preproc_effect_t * effect = (preproc_effect_t *)self; | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1775 |  | 
|  | 1776 | if (effect == NULL){ | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1777 | ALOGW("PreProcessingFx_ProcessReverse() ERROR effect == NULL"); | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1778 | return -EINVAL; | 
|  | 1779 | } | 
|  | 1780 | preproc_session_t * session = (preproc_session_t *)effect->session; | 
|  | 1781 |  | 
|  | 1782 | if (inBuffer == NULL  || inBuffer->raw == NULL){ | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1783 | ALOGW("PreProcessingFx_ProcessReverse() ERROR bad pointer"); | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1784 | return -EINVAL; | 
|  | 1785 | } | 
|  | 1786 |  | 
|  | 1787 | session->revProcessedMsk |= (1<<effect->procId); | 
|  | 1788 |  | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1789 | //    ALOGV("PreProcessingFx_ProcessReverse In %d frames revEnabledMsk %08x revProcessedMsk %08x", | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1790 | //         inBuffer->frameCount, session->revEnabledMsk, session->revProcessedMsk); | 
|  | 1791 |  | 
|  | 1792 |  | 
|  | 1793 | if ((session->revProcessedMsk & session->revEnabledMsk) == session->revEnabledMsk) { | 
|  | 1794 | effect->session->revProcessedMsk = 0; | 
|  | 1795 | if (session->revResampler != NULL) { | 
|  | 1796 | size_t fr = session->frameCount - session->framesRev; | 
|  | 1797 | if (inBuffer->frameCount < fr) { | 
|  | 1798 | fr = inBuffer->frameCount; | 
|  | 1799 | } | 
|  | 1800 | if (session->revBufSize < session->framesRev + fr) { | 
| Eric Laurent | 679650f | 2015-08-21 14:01:50 -0700 | [diff] [blame] | 1801 | int16_t *buf; | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1802 | session->revBufSize = session->framesRev + fr; | 
| Eric Laurent | 679650f | 2015-08-21 14:01:50 -0700 | [diff] [blame] | 1803 | buf = (int16_t *)realloc(session->revBuf, | 
|  | 1804 | session->revBufSize * session->inChannelCount * sizeof(int16_t)); | 
|  | 1805 | if (buf == NULL) { | 
|  | 1806 | session->framesRev = 0; | 
|  | 1807 | free(session->revBuf); | 
|  | 1808 | session->revBuf = NULL; | 
|  | 1809 | return -ENOMEM; | 
|  | 1810 | } | 
|  | 1811 | session->revBuf = buf; | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1812 | } | 
|  | 1813 | memcpy(session->revBuf + session->framesRev * session->inChannelCount, | 
|  | 1814 | inBuffer->s16, | 
|  | 1815 | fr * session->inChannelCount * sizeof(int16_t)); | 
|  | 1816 |  | 
|  | 1817 | session->framesRev += fr; | 
|  | 1818 | inBuffer->frameCount = fr; | 
|  | 1819 | if (session->framesRev < session->frameCount) { | 
|  | 1820 | return 0; | 
|  | 1821 | } | 
| Kévin PETIT | 377b2ec | 2014-02-03 12:35:36 +0000 | [diff] [blame] | 1822 | spx_uint32_t frIn = session->framesRev; | 
|  | 1823 | spx_uint32_t frOut = session->apmFrameCount; | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1824 | if (session->inChannelCount == 1) { | 
|  | 1825 | speex_resampler_process_int(session->revResampler, | 
|  | 1826 | 0, | 
|  | 1827 | session->revBuf, | 
|  | 1828 | &frIn, | 
| Chih-Hung Hsieh | de7fa31 | 2015-10-13 10:58:08 -0700 | [diff] [blame] | 1829 | session->revFrame->data_, | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1830 | &frOut); | 
|  | 1831 | } else { | 
|  | 1832 | speex_resampler_process_interleaved_int(session->revResampler, | 
|  | 1833 | session->revBuf, | 
|  | 1834 | &frIn, | 
| Chih-Hung Hsieh | de7fa31 | 2015-10-13 10:58:08 -0700 | [diff] [blame] | 1835 | session->revFrame->data_, | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1836 | &frOut); | 
|  | 1837 | } | 
|  | 1838 | memcpy(session->revBuf, | 
|  | 1839 | session->revBuf + frIn * session->inChannelCount, | 
|  | 1840 | (session->framesRev - frIn) * session->inChannelCount * sizeof(int16_t)); | 
|  | 1841 | session->framesRev -= frIn; | 
|  | 1842 | } else { | 
|  | 1843 | size_t fr = session->frameCount - session->framesRev; | 
|  | 1844 | if (inBuffer->frameCount < fr) { | 
|  | 1845 | fr = inBuffer->frameCount; | 
|  | 1846 | } | 
| Chih-Hung Hsieh | de7fa31 | 2015-10-13 10:58:08 -0700 | [diff] [blame] | 1847 | memcpy(session->revFrame->data_ + session->framesRev * session->inChannelCount, | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1848 | inBuffer->s16, | 
|  | 1849 | fr * session->inChannelCount * sizeof(int16_t)); | 
|  | 1850 | session->framesRev += fr; | 
|  | 1851 | inBuffer->frameCount = fr; | 
|  | 1852 | if (session->framesRev < session->frameCount) { | 
|  | 1853 | return 0; | 
|  | 1854 | } | 
|  | 1855 | session->framesRev = 0; | 
|  | 1856 | } | 
| Alex Luebs | 766bf73 | 2015-12-14 21:32:13 -0800 | [diff] [blame] | 1857 | session->revFrame->samples_per_channel_ = session->apmFrameCount; | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1858 | effect->session->apm->AnalyzeReverseStream(session->revFrame); | 
|  | 1859 | return 0; | 
|  | 1860 | } else { | 
|  | 1861 | return -ENODATA; | 
|  | 1862 | } | 
|  | 1863 | } | 
|  | 1864 |  | 
|  | 1865 |  | 
|  | 1866 | // effect_handle_t interface implementation for effect | 
|  | 1867 | const struct effect_interface_s sEffectInterface = { | 
|  | 1868 | PreProcessingFx_Process, | 
|  | 1869 | PreProcessingFx_Command, | 
|  | 1870 | PreProcessingFx_GetDescriptor, | 
|  | 1871 | NULL | 
|  | 1872 | }; | 
|  | 1873 |  | 
|  | 1874 | const struct effect_interface_s sEffectInterfaceReverse = { | 
|  | 1875 | PreProcessingFx_Process, | 
|  | 1876 | PreProcessingFx_Command, | 
|  | 1877 | PreProcessingFx_GetDescriptor, | 
|  | 1878 | PreProcessingFx_ProcessReverse | 
|  | 1879 | }; | 
|  | 1880 |  | 
|  | 1881 | //------------------------------------------------------------------------------ | 
|  | 1882 | // Effect Library Interface Implementation | 
|  | 1883 | //------------------------------------------------------------------------------ | 
|  | 1884 |  | 
| Glenn Kasten | 5e92a78 | 2012-01-30 07:40:52 -0800 | [diff] [blame] | 1885 | int PreProcessingLib_Create(const effect_uuid_t *uuid, | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1886 | int32_t             sessionId, | 
|  | 1887 | int32_t             ioId, | 
|  | 1888 | effect_handle_t  *pInterface) | 
|  | 1889 | { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1890 | ALOGV("EffectCreate: uuid: %08x session %d IO: %d", uuid->timeLow, sessionId, ioId); | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1891 |  | 
|  | 1892 | int status; | 
|  | 1893 | const effect_descriptor_t *desc; | 
|  | 1894 | preproc_session_t *session; | 
|  | 1895 | uint32_t procId; | 
|  | 1896 |  | 
|  | 1897 | if (PreProc_Init() != 0) { | 
|  | 1898 | return sInitStatus; | 
|  | 1899 | } | 
|  | 1900 | desc =  PreProc_GetDescriptor(uuid); | 
|  | 1901 | if (desc == NULL) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1902 | ALOGW("EffectCreate: fx not found uuid: %08x", uuid->timeLow); | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1903 | return -EINVAL; | 
|  | 1904 | } | 
|  | 1905 | procId = UuidToProcId(&desc->type); | 
|  | 1906 |  | 
|  | 1907 | session = PreProc_GetSession(procId, sessionId, ioId); | 
|  | 1908 | if (session == NULL) { | 
| Steve Block | 5ff1dd5 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 1909 | ALOGW("EffectCreate: no more session available"); | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1910 | return -EINVAL; | 
|  | 1911 | } | 
|  | 1912 |  | 
|  | 1913 | status = Session_CreateEffect(session, procId, pInterface); | 
|  | 1914 |  | 
|  | 1915 | if (status < 0 && session->createdMsk == 0) { | 
|  | 1916 | session->io = 0; | 
|  | 1917 | } | 
|  | 1918 | return status; | 
|  | 1919 | } | 
|  | 1920 |  | 
|  | 1921 | int PreProcessingLib_Release(effect_handle_t interface) | 
|  | 1922 | { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1923 | ALOGV("EffectRelease start %p", interface); | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1924 | if (PreProc_Init() != 0) { | 
|  | 1925 | return sInitStatus; | 
|  | 1926 | } | 
|  | 1927 |  | 
|  | 1928 | preproc_effect_t *fx = (preproc_effect_t *)interface; | 
|  | 1929 |  | 
|  | 1930 | if (fx->session->io == 0) { | 
|  | 1931 | return -EINVAL; | 
|  | 1932 | } | 
|  | 1933 | return Session_ReleaseEffect(fx->session, fx); | 
|  | 1934 | } | 
|  | 1935 |  | 
| Glenn Kasten | 5e92a78 | 2012-01-30 07:40:52 -0800 | [diff] [blame] | 1936 | int PreProcessingLib_GetDescriptor(const effect_uuid_t *uuid, | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1937 | effect_descriptor_t *pDescriptor) { | 
|  | 1938 |  | 
|  | 1939 | if (pDescriptor == NULL || uuid == NULL){ | 
|  | 1940 | return -EINVAL; | 
|  | 1941 | } | 
|  | 1942 |  | 
|  | 1943 | const effect_descriptor_t *desc = PreProc_GetDescriptor(uuid); | 
|  | 1944 | if (desc == NULL) { | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1945 | ALOGV("PreProcessingLib_GetDescriptor() not found"); | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1946 | return  -EINVAL; | 
|  | 1947 | } | 
|  | 1948 |  | 
| Steve Block | 3856b09 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 1949 | ALOGV("PreProcessingLib_GetDescriptor() got fx %s", desc->name); | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1950 |  | 
| Glenn Kasten | a189a68 | 2012-02-20 12:16:30 -0800 | [diff] [blame] | 1951 | *pDescriptor = *desc; | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1952 | return 0; | 
|  | 1953 | } | 
|  | 1954 |  | 
| Marco Nelissen | 7f16b19 | 2012-10-25 16:05:57 -0700 | [diff] [blame] | 1955 | // This is the only symbol that needs to be exported | 
|  | 1956 | __attribute__ ((visibility ("default"))) | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1957 | audio_effect_library_t AUDIO_EFFECT_LIBRARY_INFO_SYM = { | 
| synergydev | c9d8ea7 | 2013-10-19 22:51:33 -0700 | [diff] [blame] | 1958 | .tag = AUDIO_EFFECT_LIBRARY_TAG, | 
|  | 1959 | .version = EFFECT_LIBRARY_API_VERSION, | 
|  | 1960 | .name = "Audio Preprocessing Library", | 
|  | 1961 | .implementor = "The Android Open Source Project", | 
|  | 1962 | .create_effect = PreProcessingLib_Create, | 
|  | 1963 | .release_effect = PreProcessingLib_Release, | 
|  | 1964 | .get_descriptor = PreProcessingLib_GetDescriptor | 
| Eric Laurent | a9390d4 | 2011-06-17 20:17:17 -0700 | [diff] [blame] | 1965 | }; | 
|  | 1966 |  | 
|  | 1967 | }; // extern "C" |