blob: c3b0bfe49afc4573a9c1effa3eacc75fadbaaa04 [file] [log] [blame]
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07001/*
Arun Mirpurib1416682018-03-07 19:13:53 -08002 * Copyright (c) 2013-2018, The Linux Foundation. All rights reserved.
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07003 * Not a Contribution.
4 *
5 * Copyright (C) 2013 The Android Open Source Project
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19
20#define LOG_TAG "audio_hw_ssr"
21/*#define LOG_NDEBUG 0*/
22#define LOG_NDDEBUG 0
23
24#include <errno.h>
25#include <cutils/properties.h>
26#include <stdlib.h>
27#include <dlfcn.h>
28#include <cutils/str_parms.h>
Arun Mirpurib1bec9c2019-01-29 16:42:45 -080029#include <log/log.h>
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -070030#include <pthread.h>
31#include <cutils/sched_policy.h>
32#include <sys/resource.h>
33#include <system/thread_defs.h>
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -070034
35#include "audio_hw.h"
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -070036#include "audio_extn.h"
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -070037#include "platform.h"
38#include "platform_api.h"
Ricardo Cerqueira3ce722b2016-06-20 00:41:47 +010039#ifndef _OSS
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -070040#include "surround_rec_interface.h"
Ricardo Cerqueira3ce722b2016-06-20 00:41:47 +010041#else
42typedef struct {
43 const char *name;
44 char *(*get_param_fn)(void *h);
45} get_param_data_t;
46
47typedef struct {
48 const char *name;
49 void (*set_param_fn)(void *h, const char *val);
50} set_param_data_t;
51#endif
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -070052
Revathi Uddaraju1eac8b02017-05-18 17:13:33 +053053#ifdef DYNAMIC_LOG_ENABLED
54#include <log_xml_parser.h>
55#define LOG_MASK HAL_MOD_FILE_SSR
56#include <log_utils.h>
57#endif
58
Mingming Yin49be8032013-12-19 12:51:25 -080059#define COEFF_ARRAY_SIZE 4
60#define FILT_SIZE ((512+1)* 6) /* # ((FFT bins)/2+1)*numOutputs */
Mingming Yin49be8032013-12-19 12:51:25 -080061#define SSR_CHANNEL_OUTPUT_NUM 6
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -070062#define SSR_PERIOD_SIZE 240
63
64#define NUM_IN_BUFS 4
65#define NUM_OUT_BUFS 4
Naresh Tanniruc9093982015-10-16 18:05:29 +053066#define NUM_IN_CHANNELS 3
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -070067
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -070068#define LIB_SURROUND_3MIC_PROC "libsurround_3mic_proc.so"
69#define LIB_DRC "libdrc.so"
70
71#define AUDIO_PARAMETER_SSRMODE_ON "ssrOn"
72
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -070073
Naresh Tanniruc9093982015-10-16 18:05:29 +053074typedef short Word16;
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -070075typedef const get_param_data_t* (*surround_rec_get_get_param_data_t)(void);
76typedef const set_param_data_t* (*surround_rec_get_set_param_data_t)(void);
77typedef int (*surround_rec_init_t)(void **, int, int, int, int, const char *);
78typedef void (*surround_rec_deinit_t)(void *);
79typedef void (*surround_rec_process_t)(void *, const int16_t *, int16_t *);
80
81typedef int (*drc_init_t)(void **, int, int, const char *);
82typedef void (*drc_deinit_t)(void *);
83typedef int (*drc_process_t)(void *, const int16_t *, int16_t *);
84
85struct pcm_buffer {
86 void *data;
87 int length;
88};
89
90struct pcm_buffer_queue {
91 struct pcm_buffer_queue *next;
92 struct pcm_buffer buffer;
93};
94
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -070095struct ssr_module {
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -070096 int ssr_3mic;
97 int num_out_chan;
Naresh Tanniruc9093982015-10-16 18:05:29 +053098 FILE *fp_input;
99 FILE *fp_output;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700100 void *surround_obj;
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700101 Word16 *surround_raw_buffer;
102 int surround_raw_buffer_size;
103 bool is_ssr_enabled;
104 struct stream_in *in;
105 void *drc_obj;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700106
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700107
108 void *surround_rec_handle;
109 surround_rec_get_get_param_data_t surround_rec_get_get_param_data;
110 surround_rec_get_set_param_data_t surround_rec_get_set_param_data;
111 surround_rec_init_t surround_rec_init;
112 surround_rec_deinit_t surround_rec_deinit;
113 surround_rec_process_t surround_rec_process;
114
115 void *drc_handle;
116 drc_init_t drc_init;
117 drc_deinit_t drc_deinit;
118 drc_process_t drc_process;
119
120 pthread_t ssr_process_thread;
121 bool ssr_process_thread_started;
122 bool ssr_process_thread_stop;
123 struct pcm_buffer_queue in_buf_nodes[NUM_IN_BUFS];
124 struct pcm_buffer_queue out_buf_nodes[NUM_OUT_BUFS];
125 void *in_buf_data;
126 void *out_buf_data;
127 struct pcm_buffer_queue *out_buf_free;
128 struct pcm_buffer_queue *out_buf;
129 struct pcm_buffer_queue *in_buf_free;
130 struct pcm_buffer_queue *in_buf;
131 pthread_mutex_t ssr_process_lock;
132 pthread_cond_t cond_process;
133 pthread_cond_t cond_read;
134 bool is_ssr_mode_on;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700135};
136
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700137static struct ssr_module ssrmod = {
Naresh Tanniruc9093982015-10-16 18:05:29 +0530138 .fp_input = NULL,
139 .fp_output = NULL,
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700140 .surround_obj = NULL,
Mingming Yin49be8032013-12-19 12:51:25 -0800141 .surround_raw_buffer = NULL,
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700142 .surround_raw_buffer_size = 0,
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700143 .is_ssr_enabled = 0,
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700144 .in = NULL,
145 .drc_obj = NULL,
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700146
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700147 .surround_rec_handle = NULL,
148 .surround_rec_get_get_param_data = NULL,
149 .surround_rec_get_set_param_data = NULL,
150 .surround_rec_init = NULL,
151 .surround_rec_deinit = NULL,
152 .surround_rec_process = NULL,
153
154 .drc_handle = NULL,
155 .drc_init = NULL,
156 .drc_deinit = NULL,
157 .drc_process = NULL,
158
159 .ssr_process_thread_stop = 0,
160 .ssr_process_thread_started = 0,
161 .in_buf_data = NULL,
162 .out_buf_data = NULL,
163 .out_buf_free = NULL,
164 .out_buf = NULL,
165 .in_buf_free = NULL,
166 .in_buf = NULL,
167 .cond_process = PTHREAD_COND_INITIALIZER,
168 .cond_read = PTHREAD_COND_INITIALIZER,
169 .ssr_process_lock = PTHREAD_MUTEX_INITIALIZER,
170 .is_ssr_mode_on = false,
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700171};
172
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700173static void *ssr_process_thread(void *context);
174
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700175static int32_t drc_init_lib(int num_chan, int sample_rate __unused)
176{
177 int ret = 0;
178 const char *cfgFileName = "";
179
180 if (ssrmod.drc_obj) {
181 ALOGE("%s: DRC library is already initialized", __func__);
182 return 0;
183 }
184
185 ssrmod.drc_handle = dlopen(LIB_DRC, RTLD_NOW);
186 if (ssrmod.drc_handle == NULL) {
187 ALOGE("%s: DLOPEN failed for %s", __func__, LIB_DRC);
188 ret = -ENOSYS;
189 goto init_fail;
190 }
191
192 ALOGV("%s: DLOPEN successful for %s", __func__, LIB_DRC);
193 ssrmod.drc_init = (drc_init_t)
194 dlsym(ssrmod.drc_handle, "DRC_init");
195 ssrmod.drc_deinit = (drc_deinit_t)
196 dlsym(ssrmod.drc_handle, "DRC_deinit");
197 ssrmod.drc_process = (drc_process_t)
198 dlsym(ssrmod.drc_handle, "DRC_process");
199
200 if (!ssrmod.drc_init ||
201 !ssrmod.drc_deinit ||
202 !ssrmod.drc_process){
203 ALOGW("%s: Could not find one of the symbols from %s",
204 __func__, LIB_DRC);
205 ret = -ENOSYS;
206 goto init_fail;
207 }
208
209 /* TO DO: different config files for different sample rates */
210 if (num_chan == 6) {
Naresh Tanniru10758b62017-06-05 21:05:53 +0530211 cfgFileName = "/vendor/etc/drc/drc_cfg_5.1.txt";
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700212 } else if (num_chan == 2) {
Naresh Tanniru10758b62017-06-05 21:05:53 +0530213 cfgFileName = "/vendor/etc/drc/drc_cfg_AZ.txt";
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700214 }
215
216 ALOGV("%s: Calling drc_init: num ch: %d, period: %d, cfg file: %s", __func__, num_chan, SSR_PERIOD_SIZE, cfgFileName);
217 ret = ssrmod.drc_init(&ssrmod.drc_obj, num_chan, SSR_PERIOD_SIZE, cfgFileName);
218 if (ret) {
219 ALOGE("drc_init failed with ret:%d",ret);
220 ret = -EINVAL;
221 goto init_fail;
222 }
223
224 return 0;
225
226init_fail:
227 if (ssrmod.drc_obj) {
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700228 ssrmod.drc_obj = NULL;
229 }
230 if(ssrmod.drc_handle) {
231 dlclose(ssrmod.drc_handle);
232 ssrmod.drc_handle = NULL;
233 }
234 return ret;
235}
236
237static int32_t ssr_init_surround_sound_3mic_lib(unsigned long buffersize, int num_in_chan, int num_out_chan, int sample_rate)
238{
239 int ret = 0;
240 const char *cfgFileName = NULL;
241
242 if ( ssrmod.surround_obj ) {
243 ALOGE("%s: surround sound library is already initialized", __func__);
244 return 0;
245 }
246
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700247 ssrmod.surround_rec_handle = dlopen(LIB_SURROUND_3MIC_PROC, RTLD_NOW);
248 if (ssrmod.surround_rec_handle == NULL) {
249 ALOGE("%s: DLOPEN failed for %s", __func__, LIB_SURROUND_3MIC_PROC);
Naresh Tanniruc9093982015-10-16 18:05:29 +0530250 goto init_fail;
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700251 } else {
252 ALOGV("%s: DLOPEN successful for %s", __func__, LIB_SURROUND_3MIC_PROC);
253 ssrmod.surround_rec_get_get_param_data = (surround_rec_get_get_param_data_t)
254 dlsym(ssrmod.surround_rec_handle, "surround_rec_get_get_param_data");
255
256 ssrmod.surround_rec_get_set_param_data = (surround_rec_get_set_param_data_t)
257 dlsym(ssrmod.surround_rec_handle, "surround_rec_get_set_param_data");
258 ssrmod.surround_rec_init = (surround_rec_init_t)
259 dlsym(ssrmod.surround_rec_handle, "surround_rec_init");
260 ssrmod.surround_rec_deinit = (surround_rec_deinit_t)
261 dlsym(ssrmod.surround_rec_handle, "surround_rec_deinit");
262 ssrmod.surround_rec_process = (surround_rec_process_t)
263 dlsym(ssrmod.surround_rec_handle, "surround_rec_process");
264
265 if (!ssrmod.surround_rec_get_get_param_data ||
266 !ssrmod.surround_rec_get_set_param_data ||
267 !ssrmod.surround_rec_init ||
268 !ssrmod.surround_rec_deinit ||
269 !ssrmod.surround_rec_process){
270 ALOGW("%s: Could not find the one of the symbols from %s",
271 __func__, LIB_SURROUND_3MIC_PROC);
272 ret = -ENOSYS;
273 goto init_fail;
274 }
275 }
276
Naresh Tanniruc9093982015-10-16 18:05:29 +0530277 /* Allocate memory for input buffer */
278 ssrmod.surround_raw_buffer = (Word16 *) calloc(buffersize,
279 sizeof(Word16));
280 if (!ssrmod.surround_raw_buffer) {
281 ALOGE("%s: Memory allocation failure. Not able to allocate "
282 "memory for surroundInputBuffer", __func__);
283 ret = -ENOMEM;
284 goto init_fail;
285 }
286
287 ssrmod.surround_raw_buffer_size = buffersize;
288
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700289 ssrmod.num_out_chan = num_out_chan;
290
291 if (num_out_chan == 6) {
Naresh Tanniru10758b62017-06-05 21:05:53 +0530292 cfgFileName = "/vendor/etc/surround_sound_3mic/surround_sound_rec_5.1.cfg";
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700293 } else if (num_out_chan == 2) {
Naresh Tanniru10758b62017-06-05 21:05:53 +0530294 cfgFileName = "/vendor/etc/surround_sound_3mic/surround_sound_rec_AZ.cfg";
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700295 } else {
296 ALOGE("%s: No cfg file for num_out_chan: %d", __func__, num_out_chan);
297 }
298
299 ALOGV("%s: Calling surround_rec_init: in ch: %d, out ch: %d, period: %d, sample rate: %d, cfg file: %s",
300 __func__, num_in_chan, num_out_chan, SSR_PERIOD_SIZE, sample_rate, cfgFileName);
301 ret = ssrmod.surround_rec_init(&ssrmod.surround_obj,
302 num_in_chan, num_out_chan, SSR_PERIOD_SIZE, sample_rate, cfgFileName);
303 if (ret) {
304 ALOGE("surround_rec_init failed with ret:%d",ret);
305 ret = -EINVAL;
306 goto init_fail;
307 }
308
309 return 0;
310
311init_fail:
312 if (ssrmod.surround_obj) {
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700313 ssrmod.surround_obj = NULL;
314 }
315 if (ssrmod.surround_raw_buffer) {
316 free(ssrmod.surround_raw_buffer);
317 ssrmod.surround_raw_buffer = NULL;
318 ssrmod.surround_raw_buffer_size = 0;
319 }
320 if(ssrmod.surround_rec_handle) {
321 dlclose(ssrmod.surround_rec_handle);
322 ssrmod.surround_rec_handle = NULL;
323 }
324 return ret;
325}
326
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800327void ssr_update_enabled()
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700328{
329 char ssr_enabled[PROPERTY_VALUE_MAX] = "false";
330
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -0700331 property_get("ro.vendor.audio.sdk.ssr",ssr_enabled,"0");
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700332 if (!strncmp("true", ssr_enabled, 4)) {
333 ALOGD("%s: surround sound recording is supported", __func__);
334 ssrmod.is_ssr_enabled = true;
335 } else {
336 ALOGD("%s: surround sound recording is not supported", __func__);
337 ssrmod.is_ssr_enabled = false;
338 }
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700339}
340
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800341bool ssr_get_enabled()
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700342{
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700343 ALOGV("%s: is_ssr_enabled:%d is_ssr_mode_on:%d ", __func__, ssrmod.is_ssr_enabled, ssrmod.is_ssr_mode_on);
344
345 if(ssrmod.is_ssr_enabled && ssrmod.is_ssr_mode_on)
346 return true;
347
348 return false;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700349}
350
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800351bool ssr_check_usecase(struct stream_in *in) {
Divya Narayanan Poojary45f19192016-09-30 18:52:13 +0530352 int ret = false;
Naresh Tanniruc9093982015-10-16 18:05:29 +0530353 int channel_count = audio_channel_count_from_in_mask(in->channel_mask);
354 audio_devices_t devices = in->device;
355 audio_source_t source = in->source;
356
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800357 if ((ssr_get_enabled()) &&
Divya Narayanan Poojary45f19192016-09-30 18:52:13 +0530358 ((channel_count == 2) || (channel_count == 6)) &&
359 ((AUDIO_SOURCE_MIC == source) || (AUDIO_SOURCE_CAMCORDER == source)) &&
360 ((AUDIO_DEVICE_IN_BUILTIN_MIC == devices) || (AUDIO_DEVICE_IN_BACK_MIC == devices)) &&
361 (in->format == AUDIO_FORMAT_PCM_16_BIT)) {
362 ALOGD("%s: SSR enabled with channel_count :%d",
Naresh Tanniruc9093982015-10-16 18:05:29 +0530363 __func__, channel_count);
Divya Narayanan Poojary45f19192016-09-30 18:52:13 +0530364 ret = true;
365 }
366 return ret;
367}
Naresh Tanniruc9093982015-10-16 18:05:29 +0530368
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700369static void pcm_buffer_queue_push(struct pcm_buffer_queue **queue,
370 struct pcm_buffer_queue *node)
371{
372 struct pcm_buffer_queue *iter;
373
374 node->next = NULL;
375 if ((*queue) == NULL) {
376 *queue = node;
377 } else {
378 iter = *queue;
379 while (iter->next) {
380 iter = iter->next;
381 }
382 iter->next = node;
383 }
384}
385
386static struct pcm_buffer_queue *pcm_buffer_queue_pop(struct pcm_buffer_queue **queue)
387{
388 struct pcm_buffer_queue *node = (*queue);
389 if (node != NULL) {
390 *queue = node->next;
391 node->next = NULL;
392 }
393 return node;
394}
395
396static void deinit_ssr_process_thread()
397{
398 pthread_mutex_lock(&ssrmod.ssr_process_lock);
399 ssrmod.ssr_process_thread_stop = 1;
Naresh Tanniruc9093982015-10-16 18:05:29 +0530400
401 if(ssrmod.in_buf_data != NULL)
402 free(ssrmod.in_buf_data);
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700403 ssrmod.in_buf_data = NULL;
404 ssrmod.in_buf = NULL;
405 ssrmod.in_buf_free = NULL;
Naresh Tanniruc9093982015-10-16 18:05:29 +0530406
407 if(ssrmod.out_buf_data != NULL)
408 free(ssrmod.out_buf_data);
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700409 ssrmod.out_buf_data = NULL;
410 ssrmod.out_buf = NULL;
411 ssrmod.out_buf_free = NULL;
412 pthread_cond_broadcast(&ssrmod.cond_process);
413 pthread_cond_broadcast(&ssrmod.cond_read);
414 pthread_mutex_unlock(&ssrmod.ssr_process_lock);
415 if (ssrmod.ssr_process_thread_started) {
416 pthread_join(ssrmod.ssr_process_thread, (void **)NULL);
417 ssrmod.ssr_process_thread_started = 0;
418 }
419}
420
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800421struct stream_in *ssr_get_stream()
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700422{
423 return ssrmod.in;
424}
425
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800426int32_t ssr_deinit()
427{
428 ALOGV("%s: entry", __func__);
429 deinit_ssr_process_thread();
430
431 if (ssrmod.drc_obj) {
432 ssrmod.drc_deinit(ssrmod.drc_obj);
433 ssrmod.drc_obj = NULL;
434 }
435
436 if (ssrmod.surround_obj) {
437
438 if (ssrmod.ssr_3mic) {
439 ssrmod.surround_rec_deinit(ssrmod.surround_obj);
440 ssrmod.surround_obj = NULL;
441 }
442 if (ssrmod.surround_raw_buffer) {
443 free(ssrmod.surround_raw_buffer);
444 ssrmod.surround_raw_buffer = NULL;
445 }
446 if (ssrmod.fp_input)
447 fclose(ssrmod.fp_input);
448 if (ssrmod.fp_output)
449 fclose(ssrmod.fp_output);
450 }
451
452 if(ssrmod.drc_handle) {
453 dlclose(ssrmod.drc_handle);
454 ssrmod.drc_handle = NULL;
455 }
456
457 if(ssrmod.surround_rec_handle) {
458 dlclose(ssrmod.surround_rec_handle);
459 ssrmod.surround_rec_handle = NULL;
460 }
461
462 ssrmod.in = NULL;
463 //SSR session can be closed due to device switch
464 //Do not force reset ssr mode
465
466 //ssrmod.is_ssr_mode_on = false;
467 ALOGV("%s: exit", __func__);
468
469 return 0;
470}
471
472int32_t ssr_init(struct stream_in *in, int num_out_chan)
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700473{
Naresh Tanniruc9093982015-10-16 18:05:29 +0530474 uint32_t ret = -1;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700475 char c_multi_ch_dump[128] = {0};
476 uint32_t buffer_size;
477
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700478 ALOGD("%s: ssr case, sample rate %d", __func__, in->config.rate);
479
480 if (ssrmod.surround_obj != NULL) {
481 ALOGV("%s: reinitializing surround sound library", __func__);
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800482 ssr_deinit();
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700483 }
484
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800485 if (ssr_get_enabled()) {
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700486 ssrmod.ssr_3mic = 1;
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700487 } else {
Naresh Tanniruc9093982015-10-16 18:05:29 +0530488 ALOGE(" Rejecting SSR -- init is called without enabling SSR");
489 goto fail;
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700490 }
491
Naresh Tanniruc9093982015-10-16 18:05:29 +0530492 /* buffer size equals to period_size * period_count */
493 buffer_size = SSR_PERIOD_SIZE * NUM_IN_CHANNELS * sizeof(int16_t);
Mingming Yin49be8032013-12-19 12:51:25 -0800494 ALOGV("%s: buffer_size: %d", __func__, buffer_size);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700495
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700496 if (ssrmod.ssr_3mic != 0) {
Naresh Tanniruc9093982015-10-16 18:05:29 +0530497 ret = ssr_init_surround_sound_3mic_lib(buffer_size, NUM_IN_CHANNELS, num_out_chan, in->config.rate);
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700498 if (0 != ret) {
499 ALOGE("%s: ssr_init_surround_sound_3mic_lib failed: %d "
500 "buffer_size:%d", __func__, ret, buffer_size);
501 goto fail;
502 }
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700503 }
504
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700505 /* Initialize DRC if available */
506 ret = drc_init_lib(num_out_chan, in->config.rate);
507 if (0 != ret) {
508 ALOGE("%s: drc_init_lib failed, ret %d", __func__, ret);
509 }
510
511 pthread_mutex_lock(&ssrmod.ssr_process_lock);
512 if (!ssrmod.ssr_process_thread_started) {
513 int i;
514 int output_buf_size = SSR_PERIOD_SIZE * sizeof(int16_t) * num_out_chan;
515
516 ssrmod.in_buf_data = (void *)calloc(buffer_size, NUM_IN_BUFS);
517 if (ssrmod.in_buf_data == NULL) {
518 ALOGE("%s: failed to allocate input buffer", __func__);
519 pthread_mutex_unlock(&ssrmod.ssr_process_lock);
520 ret = -ENOMEM;
521 goto fail;
522 }
523 ssrmod.out_buf_data = (void *)calloc(output_buf_size, NUM_OUT_BUFS);
524 if (ssrmod.out_buf_data == NULL) {
525 ALOGE("%s: failed to allocate output buffer", __func__);
526 pthread_mutex_unlock(&ssrmod.ssr_process_lock);
527 ret = -ENOMEM;
528 // ssrmod.in_buf_data will be freed in deinit_ssr_process_thread()
529 goto fail;
530 }
531
532 ssrmod.in_buf = NULL;
533 ssrmod.in_buf_free = NULL;
534 ssrmod.out_buf = NULL;
535 ssrmod.out_buf_free = NULL;
536
537 for (i=0; i < NUM_IN_BUFS; i++) {
538 struct pcm_buffer_queue *buf = &ssrmod.in_buf_nodes[i];
539 buf->buffer.data = &(((char *)ssrmod.in_buf_data)[i*buffer_size]);
540 buf->buffer.length = buffer_size;
541 pcm_buffer_queue_push(&ssrmod.in_buf_free, buf);
542 }
543
544 for (i=0; i < NUM_OUT_BUFS; i++) {
545 struct pcm_buffer_queue *buf = &ssrmod.out_buf_nodes[i];
546 buf->buffer.data = &(((char *)ssrmod.out_buf_data)[i*output_buf_size]);
547 buf->buffer.length = output_buf_size;
548 pcm_buffer_queue_push(&ssrmod.out_buf, buf);
549 }
550
551 ssrmod.ssr_process_thread_stop = 0;
552 ALOGV("%s: creating thread", __func__);
553 ret = pthread_create(&ssrmod.ssr_process_thread,
554 (const pthread_attr_t *) NULL,
555 ssr_process_thread, NULL);
556 if (ret != 0) {
557 ALOGE("%s: failed to create thread for surround sound recording.",
558 __func__);
559 pthread_mutex_unlock(&ssrmod.ssr_process_lock);
560 goto fail;
561 }
562
563 ssrmod.ssr_process_thread_started = 1;
564 ALOGV("%s: done creating thread", __func__);
565 }
Naresh Tanniruc9093982015-10-16 18:05:29 +0530566
567 in->config.channels = NUM_IN_CHANNELS;
568 in->config.period_size = SSR_PERIOD_SIZE;
569 in->config.period_count = in->config.channels * sizeof(int16_t);
570
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700571 pthread_mutex_unlock(&ssrmod.ssr_process_lock);
572
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -0700573 property_get("vendor.audio.ssr.pcmdump",c_multi_ch_dump,"0");
Mingming Yin49be8032013-12-19 12:51:25 -0800574 if (0 == strncmp("true", c_multi_ch_dump, sizeof("ssr.dump-pcm"))) {
575 /* Remember to change file system permission of data(e.g. chmod 777 data/),
576 otherwise, fopen may fail */
Naresh Tanniruc9093982015-10-16 18:05:29 +0530577 if ( !ssrmod.fp_input) {
578 ALOGD("%s: Opening ssr input dump file \n", __func__);
Arun Mirpurib1416682018-03-07 19:13:53 -0800579 ssrmod.fp_input = fopen("/data/vendor/audio/ssr_input_3ch.pcm", "wb");
Naresh Tanniruc9093982015-10-16 18:05:29 +0530580 }
581
582 if ( !ssrmod.fp_output) {
583 if(ssrmod.num_out_chan == 6) {
584 ALOGD("%s: Opening ssr input dump file for 6 channel\n", __func__);
Arun Mirpurib1416682018-03-07 19:13:53 -0800585 ssrmod.fp_output = fopen("/data/vendor/audio/ssr_output_6ch.pcm", "wb");
Naresh Tanniruc9093982015-10-16 18:05:29 +0530586 } else {
587 ALOGD("%s: Opening ssr input dump file for 2 channel\n", __func__);
Arun Mirpurib1416682018-03-07 19:13:53 -0800588 ssrmod.fp_output = fopen("/data/vendor/audio/ssr_output_2ch.pcm", "wb");
Naresh Tanniruc9093982015-10-16 18:05:29 +0530589 }
590 }
591
592 if ((!ssrmod.fp_input) || (!ssrmod.fp_output)) {
593 ALOGE("%s: input dump or ouput dump open failed: mfp_4ch:%p mfp_6ch:%p",
594 __func__, ssrmod.fp_input, ssrmod.fp_output);
595 }
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700596 }
597
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700598 ssrmod.in = in;
599
600 ALOGV("%s: exit", __func__);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700601 return 0;
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700602
603fail:
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800604 (void) ssr_deinit();
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700605 return ret;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700606}
607
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800608int ssr_set_usecase(struct stream_in *in,
609 struct audio_config *config,
610 bool *update_params)
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700611{
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800612 int ret = -EINVAL;
613 int channel_count = audio_channel_count_from_in_mask(in->channel_mask);
614 audio_channel_representation_t representation =
615 audio_channel_mask_get_representation(in->channel_mask);
616 *update_params = false;
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700617
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800618 if (ssr_check_usecase(in)) {
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700619
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800620 if (representation == AUDIO_CHANNEL_REPRESENTATION_INDEX) {
621 /* update params in case channel representation index.
622 * on returning error, flinger will retry with supported representation passed
623 */
624 ALOGD("%s: SSR supports only channel representation position, channel_mask(%#x)"
625 ,__func__, config->channel_mask);
626 config->channel_mask = AUDIO_CHANNEL_IN_6;
627 ret = 0;
628 *update_params = true;
629 } else {
630 if (!ssr_init(in, channel_count)) {
631 ALOGD("%s: Created SSR session succesfully", __func__);
632 ret = 0;
633 } else {
634 ALOGE("%s: Unable to start SSR record session", __func__);
635 }
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700636 }
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700637 }
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800638 return ret;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700639}
640
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700641static void *ssr_process_thread(void *context __unused)
642{
643 int32_t ret;
644
645 ALOGV("%s: enter", __func__);
646
647 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_URGENT_AUDIO);
648 set_sched_policy(0, SP_FOREGROUND);
649
650 pthread_mutex_lock(&ssrmod.ssr_process_lock);
651 while (!ssrmod.ssr_process_thread_stop) {
652 struct pcm_buffer_queue *out_buf;
653 struct pcm_buffer_queue *in_buf;
654
655 while ((!ssrmod.ssr_process_thread_stop) &&
656 ((ssrmod.out_buf_free == NULL) ||
657 (ssrmod.in_buf == NULL))) {
658 ALOGV("%s: waiting for buffers", __func__);
659 pthread_cond_wait(&ssrmod.cond_process, &ssrmod.ssr_process_lock);
660 }
661 if (ssrmod.ssr_process_thread_stop) {
662 break;
663 }
664 ALOGV("%s: got buffers", __func__);
665
666 out_buf = pcm_buffer_queue_pop(&ssrmod.out_buf_free);
667 in_buf = pcm_buffer_queue_pop(&ssrmod.in_buf);
668
669 pthread_mutex_unlock(&ssrmod.ssr_process_lock);
670
671 /* apply ssr libs to convert 4ch to 6ch */
672 if (ssrmod.ssr_3mic) {
673 ssrmod.surround_rec_process(ssrmod.surround_obj,
674 (int16_t *) in_buf->buffer.data,
675 (int16_t *) out_buf->buffer.data);
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700676 }
677
678 /* Run DRC if initialized */
679 if (ssrmod.drc_obj != NULL) {
680 ALOGV("%s: Running DRC", __func__);
681 ret = ssrmod.drc_process(ssrmod.drc_obj, out_buf->buffer.data, out_buf->buffer.data);
682 if (ret != 0) {
683 ALOGE("%s: drc_process returned %d", __func__, ret);
684 }
685 }
686
687 /*dump for raw pcm data*/
Naresh Tanniruc9093982015-10-16 18:05:29 +0530688 if (ssrmod.fp_input)
689 fwrite(in_buf->buffer.data, 1, in_buf->buffer.length, ssrmod.fp_input);
690 if (ssrmod.fp_output)
691 fwrite(out_buf->buffer.data, 1, out_buf->buffer.length, ssrmod.fp_output);
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700692
693 pthread_mutex_lock(&ssrmod.ssr_process_lock);
694
695 pcm_buffer_queue_push(&ssrmod.out_buf, out_buf);
696 pcm_buffer_queue_push(&ssrmod.in_buf_free, in_buf);
697
698 /* Read thread should go on without waiting for condition
699 * variable. If it has to wait (due to this thread not keeping
700 * up with the read requests), let this thread use the remainder
701 * of its buffers before waking up the read thread. */
702 if (ssrmod.in_buf == NULL) {
703 pthread_cond_signal(&ssrmod.cond_read);
704 }
705 }
706 pthread_mutex_unlock(&ssrmod.ssr_process_lock);
707
708 ALOGV("%s: exit", __func__);
709
710 pthread_exit(NULL);
711}
712
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800713int32_t ssr_read(struct audio_stream_in *stream,
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700714 void *buffer, size_t bytes)
715{
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700716 struct stream_in *in = (struct stream_in *)stream;
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700717 int32_t ret = 0;
718 struct pcm_buffer_queue *in_buf;
719 struct pcm_buffer_queue *out_buf;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700720
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700721 ALOGV("%s: entry", __func__);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700722
Mingming Yin49be8032013-12-19 12:51:25 -0800723 if (!ssrmod.surround_obj) {
724 ALOGE("%s: surround_obj not initialized", __func__);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700725 return -ENOMEM;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700726 }
Mingming Yin49be8032013-12-19 12:51:25 -0800727
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700728 ret = pcm_read(in->pcm, ssrmod.surround_raw_buffer, ssrmod.surround_raw_buffer_size);
Mingming Yin49be8032013-12-19 12:51:25 -0800729 if (ret < 0) {
730 ALOGE("%s: %s ret:%d", __func__, pcm_get_error(in->pcm),ret);
731 return ret;
732 }
733
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700734 pthread_mutex_lock(&ssrmod.ssr_process_lock);
Mingming Yin49be8032013-12-19 12:51:25 -0800735
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700736 if (!ssrmod.ssr_process_thread_started) {
737 pthread_mutex_unlock(&ssrmod.ssr_process_lock);
738 ALOGV("%s: ssr_process_thread not initialized", __func__);
739 return -EINVAL;
740 }
Mingming Yin49be8032013-12-19 12:51:25 -0800741
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700742 if ((ssrmod.in_buf_free == NULL) || (ssrmod.out_buf == NULL)) {
743 ALOGE("%s: waiting for buffers", __func__);
744 pthread_cond_wait(&ssrmod.cond_read, &ssrmod.ssr_process_lock);
745 if ((ssrmod.in_buf_free == NULL) || (ssrmod.out_buf == NULL)) {
746 pthread_mutex_unlock(&ssrmod.ssr_process_lock);
747 ALOGE("%s: failed to acquire buffers", __func__);
748 return -EINVAL;
749 }
750 }
751
752 in_buf = pcm_buffer_queue_pop(&ssrmod.in_buf_free);
753 out_buf = pcm_buffer_queue_pop(&ssrmod.out_buf);
754
755 memcpy(in_buf->buffer.data, ssrmod.surround_raw_buffer, in_buf->buffer.length);
756 pcm_buffer_queue_push(&ssrmod.in_buf, in_buf);
757
758 memcpy(buffer, out_buf->buffer.data, bytes);
759 pcm_buffer_queue_push(&ssrmod.out_buf_free, out_buf);
760
761 pthread_cond_signal(&ssrmod.cond_process);
762
763 pthread_mutex_unlock(&ssrmod.ssr_process_lock);
764
765 ALOGV("%s: exit", __func__);
Mingming Yin49be8032013-12-19 12:51:25 -0800766 return ret;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700767}
Mingming Yin49be8032013-12-19 12:51:25 -0800768
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800769void ssr_set_parameters(struct audio_device *adev __unused,
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700770 struct str_parms *parms)
771{
772 int err;
773 char value[4096] = {0};
774
775 //Do not update SSR mode during recording
776 if ( !ssrmod.surround_obj) {
777 int ret = 0;
778 ret = str_parms_get_str(parms, AUDIO_PARAMETER_SSRMODE_ON, value,
779 sizeof(value));
780 if (ret >= 0) {
781 if (strcmp(value, "true") == 0) {
782 ALOGD("Received SSR session request..setting SSR mode to true");
783 ssrmod.is_ssr_mode_on = true;
784 } else {
785 ALOGD("resetting SSR mode to false");
786 ssrmod.is_ssr_mode_on = false;
787 }
788 }
789 }
790 if (ssrmod.ssr_3mic && ssrmod.surround_obj) {
791 const set_param_data_t *set_params = ssrmod.surround_rec_get_set_param_data();
792 if (set_params != NULL) {
793 while (set_params->name != NULL && set_params->set_param_fn != NULL) {
794 err = str_parms_get_str(parms, set_params->name, value, sizeof(value));
795 if (err >= 0) {
796 ALOGV("Set %s to %s\n", set_params->name, value);
797 set_params->set_param_fn(ssrmod.surround_obj, value);
798 }
799 set_params++;
800 }
801 }
802 }
803}
804
Arun Mirpurib1bec9c2019-01-29 16:42:45 -0800805void ssr_get_parameters(const struct audio_device *adev __unused,
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700806 struct str_parms *parms,
807 struct str_parms *reply)
808{
809 int err;
810 char value[4096] = {0};
811
812 if (ssrmod.ssr_3mic && ssrmod.surround_obj) {
813 const get_param_data_t *get_params = ssrmod.surround_rec_get_get_param_data();
814 int get_all = 0;
815 err = str_parms_get_str(parms, "ssr.all", value, sizeof(value));
816 if (err >= 0) {
817 get_all = 1;
818 }
819 if (get_params != NULL) {
820 while (get_params->name != NULL && get_params->get_param_fn != NULL) {
821 err = str_parms_get_str(parms, get_params->name, value, sizeof(value));
822 if (get_all || (err >= 0)) {
823 ALOGV("Getting parameter %s", get_params->name);
824 char *val = get_params->get_param_fn(ssrmod.surround_obj);
825 if (val != NULL) {
826 str_parms_add_str(reply, get_params->name, val);
827 free(val);
828 }
829 }
830 get_params++;
831 }
832 }
833 }
834}
835