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