PreProcessing.cpp 72 KB

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