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