blob: dad5639e795546ccdd2bafe9b93db8601c263f33 [file] [log] [blame]
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07001/*
Revathi Uddaraju1eac8b02017-05-18 17:13:33 +05302 * Copyright (c) 2013-2017, 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>
29#include <cutils/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"
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -070039#include "surround_rec_interface.h"
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -070040
Revathi Uddaraju1eac8b02017-05-18 17:13:33 +053041#ifdef DYNAMIC_LOG_ENABLED
42#include <log_xml_parser.h>
43#define LOG_MASK HAL_MOD_FILE_SSR
44#include <log_utils.h>
45#endif
46
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -070047#ifdef SSR_ENABLED
Mingming Yin49be8032013-12-19 12:51:25 -080048#define COEFF_ARRAY_SIZE 4
49#define FILT_SIZE ((512+1)* 6) /* # ((FFT bins)/2+1)*numOutputs */
Mingming Yin49be8032013-12-19 12:51:25 -080050#define SSR_CHANNEL_OUTPUT_NUM 6
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -070051#define SSR_PERIOD_SIZE 240
52
53#define NUM_IN_BUFS 4
54#define NUM_OUT_BUFS 4
Naresh Tanniruc9093982015-10-16 18:05:29 +053055#define NUM_IN_CHANNELS 3
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -070056
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -070057#define LIB_SURROUND_3MIC_PROC "libsurround_3mic_proc.so"
58#define LIB_DRC "libdrc.so"
59
60#define AUDIO_PARAMETER_SSRMODE_ON "ssrOn"
61
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -070062
Naresh Tanniruc9093982015-10-16 18:05:29 +053063typedef short Word16;
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -070064typedef const get_param_data_t* (*surround_rec_get_get_param_data_t)(void);
65typedef const set_param_data_t* (*surround_rec_get_set_param_data_t)(void);
66typedef int (*surround_rec_init_t)(void **, int, int, int, int, const char *);
67typedef void (*surround_rec_deinit_t)(void *);
68typedef void (*surround_rec_process_t)(void *, const int16_t *, int16_t *);
69
70typedef int (*drc_init_t)(void **, int, int, const char *);
71typedef void (*drc_deinit_t)(void *);
72typedef int (*drc_process_t)(void *, const int16_t *, int16_t *);
73
74struct pcm_buffer {
75 void *data;
76 int length;
77};
78
79struct pcm_buffer_queue {
80 struct pcm_buffer_queue *next;
81 struct pcm_buffer buffer;
82};
83
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -070084struct ssr_module {
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -070085 int ssr_3mic;
86 int num_out_chan;
Naresh Tanniruc9093982015-10-16 18:05:29 +053087 FILE *fp_input;
88 FILE *fp_output;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -070089 void *surround_obj;
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -070090 Word16 *surround_raw_buffer;
91 int surround_raw_buffer_size;
92 bool is_ssr_enabled;
93 struct stream_in *in;
94 void *drc_obj;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -070095
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -070096
97 void *surround_rec_handle;
98 surround_rec_get_get_param_data_t surround_rec_get_get_param_data;
99 surround_rec_get_set_param_data_t surround_rec_get_set_param_data;
100 surround_rec_init_t surround_rec_init;
101 surround_rec_deinit_t surround_rec_deinit;
102 surround_rec_process_t surround_rec_process;
103
104 void *drc_handle;
105 drc_init_t drc_init;
106 drc_deinit_t drc_deinit;
107 drc_process_t drc_process;
108
109 pthread_t ssr_process_thread;
110 bool ssr_process_thread_started;
111 bool ssr_process_thread_stop;
112 struct pcm_buffer_queue in_buf_nodes[NUM_IN_BUFS];
113 struct pcm_buffer_queue out_buf_nodes[NUM_OUT_BUFS];
114 void *in_buf_data;
115 void *out_buf_data;
116 struct pcm_buffer_queue *out_buf_free;
117 struct pcm_buffer_queue *out_buf;
118 struct pcm_buffer_queue *in_buf_free;
119 struct pcm_buffer_queue *in_buf;
120 pthread_mutex_t ssr_process_lock;
121 pthread_cond_t cond_process;
122 pthread_cond_t cond_read;
123 bool is_ssr_mode_on;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700124};
125
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700126static struct ssr_module ssrmod = {
Naresh Tanniruc9093982015-10-16 18:05:29 +0530127 .fp_input = NULL,
128 .fp_output = NULL,
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700129 .surround_obj = NULL,
Mingming Yin49be8032013-12-19 12:51:25 -0800130 .surround_raw_buffer = NULL,
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700131 .surround_raw_buffer_size = 0,
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700132 .is_ssr_enabled = 0,
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700133 .in = NULL,
134 .drc_obj = NULL,
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700135
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700136 .surround_rec_handle = NULL,
137 .surround_rec_get_get_param_data = NULL,
138 .surround_rec_get_set_param_data = NULL,
139 .surround_rec_init = NULL,
140 .surround_rec_deinit = NULL,
141 .surround_rec_process = NULL,
142
143 .drc_handle = NULL,
144 .drc_init = NULL,
145 .drc_deinit = NULL,
146 .drc_process = NULL,
147
148 .ssr_process_thread_stop = 0,
149 .ssr_process_thread_started = 0,
150 .in_buf_data = NULL,
151 .out_buf_data = NULL,
152 .out_buf_free = NULL,
153 .out_buf = NULL,
154 .in_buf_free = NULL,
155 .in_buf = NULL,
156 .cond_process = PTHREAD_COND_INITIALIZER,
157 .cond_read = PTHREAD_COND_INITIALIZER,
158 .ssr_process_lock = PTHREAD_MUTEX_INITIALIZER,
159 .is_ssr_mode_on = false,
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700160};
161
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700162static void *ssr_process_thread(void *context);
163
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700164static int32_t drc_init_lib(int num_chan, int sample_rate __unused)
165{
166 int ret = 0;
167 const char *cfgFileName = "";
168
169 if (ssrmod.drc_obj) {
170 ALOGE("%s: DRC library is already initialized", __func__);
171 return 0;
172 }
173
174 ssrmod.drc_handle = dlopen(LIB_DRC, RTLD_NOW);
175 if (ssrmod.drc_handle == NULL) {
176 ALOGE("%s: DLOPEN failed for %s", __func__, LIB_DRC);
177 ret = -ENOSYS;
178 goto init_fail;
179 }
180
181 ALOGV("%s: DLOPEN successful for %s", __func__, LIB_DRC);
182 ssrmod.drc_init = (drc_init_t)
183 dlsym(ssrmod.drc_handle, "DRC_init");
184 ssrmod.drc_deinit = (drc_deinit_t)
185 dlsym(ssrmod.drc_handle, "DRC_deinit");
186 ssrmod.drc_process = (drc_process_t)
187 dlsym(ssrmod.drc_handle, "DRC_process");
188
189 if (!ssrmod.drc_init ||
190 !ssrmod.drc_deinit ||
191 !ssrmod.drc_process){
192 ALOGW("%s: Could not find one of the symbols from %s",
193 __func__, LIB_DRC);
194 ret = -ENOSYS;
195 goto init_fail;
196 }
197
198 /* TO DO: different config files for different sample rates */
199 if (num_chan == 6) {
Naresh Tanniru10758b62017-06-05 21:05:53 +0530200 cfgFileName = "/vendor/etc/drc/drc_cfg_5.1.txt";
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700201 } else if (num_chan == 2) {
Naresh Tanniru10758b62017-06-05 21:05:53 +0530202 cfgFileName = "/vendor/etc/drc/drc_cfg_AZ.txt";
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700203 }
204
205 ALOGV("%s: Calling drc_init: num ch: %d, period: %d, cfg file: %s", __func__, num_chan, SSR_PERIOD_SIZE, cfgFileName);
206 ret = ssrmod.drc_init(&ssrmod.drc_obj, num_chan, SSR_PERIOD_SIZE, cfgFileName);
207 if (ret) {
208 ALOGE("drc_init failed with ret:%d",ret);
209 ret = -EINVAL;
210 goto init_fail;
211 }
212
213 return 0;
214
215init_fail:
216 if (ssrmod.drc_obj) {
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700217 ssrmod.drc_obj = NULL;
218 }
219 if(ssrmod.drc_handle) {
220 dlclose(ssrmod.drc_handle);
221 ssrmod.drc_handle = NULL;
222 }
223 return ret;
224}
225
226static int32_t ssr_init_surround_sound_3mic_lib(unsigned long buffersize, int num_in_chan, int num_out_chan, int sample_rate)
227{
228 int ret = 0;
229 const char *cfgFileName = NULL;
230
231 if ( ssrmod.surround_obj ) {
232 ALOGE("%s: surround sound library is already initialized", __func__);
233 return 0;
234 }
235
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700236 ssrmod.surround_rec_handle = dlopen(LIB_SURROUND_3MIC_PROC, RTLD_NOW);
237 if (ssrmod.surround_rec_handle == NULL) {
238 ALOGE("%s: DLOPEN failed for %s", __func__, LIB_SURROUND_3MIC_PROC);
Naresh Tanniruc9093982015-10-16 18:05:29 +0530239 goto init_fail;
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700240 } else {
241 ALOGV("%s: DLOPEN successful for %s", __func__, LIB_SURROUND_3MIC_PROC);
242 ssrmod.surround_rec_get_get_param_data = (surround_rec_get_get_param_data_t)
243 dlsym(ssrmod.surround_rec_handle, "surround_rec_get_get_param_data");
244
245 ssrmod.surround_rec_get_set_param_data = (surround_rec_get_set_param_data_t)
246 dlsym(ssrmod.surround_rec_handle, "surround_rec_get_set_param_data");
247 ssrmod.surround_rec_init = (surround_rec_init_t)
248 dlsym(ssrmod.surround_rec_handle, "surround_rec_init");
249 ssrmod.surround_rec_deinit = (surround_rec_deinit_t)
250 dlsym(ssrmod.surround_rec_handle, "surround_rec_deinit");
251 ssrmod.surround_rec_process = (surround_rec_process_t)
252 dlsym(ssrmod.surround_rec_handle, "surround_rec_process");
253
254 if (!ssrmod.surround_rec_get_get_param_data ||
255 !ssrmod.surround_rec_get_set_param_data ||
256 !ssrmod.surround_rec_init ||
257 !ssrmod.surround_rec_deinit ||
258 !ssrmod.surround_rec_process){
259 ALOGW("%s: Could not find the one of the symbols from %s",
260 __func__, LIB_SURROUND_3MIC_PROC);
261 ret = -ENOSYS;
262 goto init_fail;
263 }
264 }
265
Naresh Tanniruc9093982015-10-16 18:05:29 +0530266 /* Allocate memory for input buffer */
267 ssrmod.surround_raw_buffer = (Word16 *) calloc(buffersize,
268 sizeof(Word16));
269 if (!ssrmod.surround_raw_buffer) {
270 ALOGE("%s: Memory allocation failure. Not able to allocate "
271 "memory for surroundInputBuffer", __func__);
272 ret = -ENOMEM;
273 goto init_fail;
274 }
275
276 ssrmod.surround_raw_buffer_size = buffersize;
277
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700278 ssrmod.num_out_chan = num_out_chan;
279
280 if (num_out_chan == 6) {
Naresh Tanniru10758b62017-06-05 21:05:53 +0530281 cfgFileName = "/vendor/etc/surround_sound_3mic/surround_sound_rec_5.1.cfg";
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700282 } else if (num_out_chan == 2) {
Naresh Tanniru10758b62017-06-05 21:05:53 +0530283 cfgFileName = "/vendor/etc/surround_sound_3mic/surround_sound_rec_AZ.cfg";
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700284 } else {
285 ALOGE("%s: No cfg file for num_out_chan: %d", __func__, num_out_chan);
286 }
287
288 ALOGV("%s: Calling surround_rec_init: in ch: %d, out ch: %d, period: %d, sample rate: %d, cfg file: %s",
289 __func__, num_in_chan, num_out_chan, SSR_PERIOD_SIZE, sample_rate, cfgFileName);
290 ret = ssrmod.surround_rec_init(&ssrmod.surround_obj,
291 num_in_chan, num_out_chan, SSR_PERIOD_SIZE, sample_rate, cfgFileName);
292 if (ret) {
293 ALOGE("surround_rec_init failed with ret:%d",ret);
294 ret = -EINVAL;
295 goto init_fail;
296 }
297
298 return 0;
299
300init_fail:
301 if (ssrmod.surround_obj) {
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700302 ssrmod.surround_obj = NULL;
303 }
304 if (ssrmod.surround_raw_buffer) {
305 free(ssrmod.surround_raw_buffer);
306 ssrmod.surround_raw_buffer = NULL;
307 ssrmod.surround_raw_buffer_size = 0;
308 }
309 if(ssrmod.surround_rec_handle) {
310 dlclose(ssrmod.surround_rec_handle);
311 ssrmod.surround_rec_handle = NULL;
312 }
313 return ret;
314}
315
Mingming Yin49be8032013-12-19 12:51:25 -0800316void audio_extn_ssr_update_enabled()
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700317{
318 char ssr_enabled[PROPERTY_VALUE_MAX] = "false";
319
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -0700320 property_get("ro.vendor.audio.sdk.ssr",ssr_enabled,"0");
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700321 if (!strncmp("true", ssr_enabled, 4)) {
322 ALOGD("%s: surround sound recording is supported", __func__);
323 ssrmod.is_ssr_enabled = true;
324 } else {
325 ALOGD("%s: surround sound recording is not supported", __func__);
326 ssrmod.is_ssr_enabled = false;
327 }
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700328}
329
330bool audio_extn_ssr_get_enabled()
331{
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700332 ALOGV("%s: is_ssr_enabled:%d is_ssr_mode_on:%d ", __func__, ssrmod.is_ssr_enabled, ssrmod.is_ssr_mode_on);
333
334 if(ssrmod.is_ssr_enabled && ssrmod.is_ssr_mode_on)
335 return true;
336
337 return false;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700338}
339
Divya Narayanan Poojary45f19192016-09-30 18:52:13 +0530340bool audio_extn_ssr_check_usecase(struct stream_in *in) {
341 int ret = false;
Naresh Tanniruc9093982015-10-16 18:05:29 +0530342 int channel_count = audio_channel_count_from_in_mask(in->channel_mask);
343 audio_devices_t devices = in->device;
344 audio_source_t source = in->source;
345
Divya Narayanan Poojary45f19192016-09-30 18:52:13 +0530346 if ((audio_extn_ssr_get_enabled()) &&
347 ((channel_count == 2) || (channel_count == 6)) &&
348 ((AUDIO_SOURCE_MIC == source) || (AUDIO_SOURCE_CAMCORDER == source)) &&
349 ((AUDIO_DEVICE_IN_BUILTIN_MIC == devices) || (AUDIO_DEVICE_IN_BACK_MIC == devices)) &&
350 (in->format == AUDIO_FORMAT_PCM_16_BIT)) {
351 ALOGD("%s: SSR enabled with channel_count :%d",
Naresh Tanniruc9093982015-10-16 18:05:29 +0530352 __func__, channel_count);
Divya Narayanan Poojary45f19192016-09-30 18:52:13 +0530353 ret = true;
354 }
355 return ret;
356}
Naresh Tanniruc9093982015-10-16 18:05:29 +0530357
Divya Narayanan Poojary45f19192016-09-30 18:52:13 +0530358int audio_extn_ssr_set_usecase(struct stream_in *in,
359 struct audio_config *config,
360 bool *update_params)
361{
362 int ret = -EINVAL;
363 int channel_count = audio_channel_count_from_in_mask(in->channel_mask);
364 audio_channel_representation_t representation =
365 audio_channel_mask_get_representation(in->channel_mask);
366 *update_params = false;
367
368 if (audio_extn_ssr_check_usecase(in)) {
369
370 if (representation == AUDIO_CHANNEL_REPRESENTATION_INDEX) {
371 /* update params in case channel representation index.
372 * on returning error, flinger will retry with supported representation passed
373 */
374 ALOGD("%s: SSR supports only channel representation position, channel_mask(%#x)"
375 ,__func__, config->channel_mask);
Dhananjay Kumar29b894e2017-04-05 02:41:14 +0530376 config->channel_mask = AUDIO_CHANNEL_IN_6;
Naresh Tanniruc9093982015-10-16 18:05:29 +0530377 ret = 0;
Divya Narayanan Poojary45f19192016-09-30 18:52:13 +0530378 *update_params = true;
Naresh Tanniruc9093982015-10-16 18:05:29 +0530379 } else {
Divya Narayanan Poojary45f19192016-09-30 18:52:13 +0530380 if (!audio_extn_ssr_init(in, channel_count)) {
381 ALOGD("%s: Created SSR session succesfully", __func__);
382 ret = 0;
383 } else {
384 ALOGE("%s: Unable to start SSR record session", __func__);
385 }
Naresh Tanniruc9093982015-10-16 18:05:29 +0530386 }
Divya Narayanan Poojary45f19192016-09-30 18:52:13 +0530387 }
388 return ret;
Naresh Tanniruc9093982015-10-16 18:05:29 +0530389}
390
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700391static void pcm_buffer_queue_push(struct pcm_buffer_queue **queue,
392 struct pcm_buffer_queue *node)
393{
394 struct pcm_buffer_queue *iter;
395
396 node->next = NULL;
397 if ((*queue) == NULL) {
398 *queue = node;
399 } else {
400 iter = *queue;
401 while (iter->next) {
402 iter = iter->next;
403 }
404 iter->next = node;
405 }
406}
407
408static struct pcm_buffer_queue *pcm_buffer_queue_pop(struct pcm_buffer_queue **queue)
409{
410 struct pcm_buffer_queue *node = (*queue);
411 if (node != NULL) {
412 *queue = node->next;
413 node->next = NULL;
414 }
415 return node;
416}
417
418static void deinit_ssr_process_thread()
419{
420 pthread_mutex_lock(&ssrmod.ssr_process_lock);
421 ssrmod.ssr_process_thread_stop = 1;
Naresh Tanniruc9093982015-10-16 18:05:29 +0530422
423 if(ssrmod.in_buf_data != NULL)
424 free(ssrmod.in_buf_data);
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700425 ssrmod.in_buf_data = NULL;
426 ssrmod.in_buf = NULL;
427 ssrmod.in_buf_free = NULL;
Naresh Tanniruc9093982015-10-16 18:05:29 +0530428
429 if(ssrmod.out_buf_data != NULL)
430 free(ssrmod.out_buf_data);
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700431 ssrmod.out_buf_data = NULL;
432 ssrmod.out_buf = NULL;
433 ssrmod.out_buf_free = NULL;
434 pthread_cond_broadcast(&ssrmod.cond_process);
435 pthread_cond_broadcast(&ssrmod.cond_read);
436 pthread_mutex_unlock(&ssrmod.ssr_process_lock);
437 if (ssrmod.ssr_process_thread_started) {
438 pthread_join(ssrmod.ssr_process_thread, (void **)NULL);
439 ssrmod.ssr_process_thread_started = 0;
440 }
441}
442
443struct stream_in *audio_extn_ssr_get_stream()
444{
445 return ssrmod.in;
446}
447
448int32_t audio_extn_ssr_init(struct stream_in *in, int num_out_chan)
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700449{
Naresh Tanniruc9093982015-10-16 18:05:29 +0530450 uint32_t ret = -1;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700451 char c_multi_ch_dump[128] = {0};
452 uint32_t buffer_size;
453
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700454 ALOGD("%s: ssr case, sample rate %d", __func__, in->config.rate);
455
456 if (ssrmod.surround_obj != NULL) {
457 ALOGV("%s: reinitializing surround sound library", __func__);
458 audio_extn_ssr_deinit();
459 }
460
Naresh Tanniruc9093982015-10-16 18:05:29 +0530461 if (audio_extn_ssr_get_enabled()) {
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700462 ssrmod.ssr_3mic = 1;
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700463 } else {
Naresh Tanniruc9093982015-10-16 18:05:29 +0530464 ALOGE(" Rejecting SSR -- init is called without enabling SSR");
465 goto fail;
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700466 }
467
Naresh Tanniruc9093982015-10-16 18:05:29 +0530468 /* buffer size equals to period_size * period_count */
469 buffer_size = SSR_PERIOD_SIZE * NUM_IN_CHANNELS * sizeof(int16_t);
Mingming Yin49be8032013-12-19 12:51:25 -0800470 ALOGV("%s: buffer_size: %d", __func__, buffer_size);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700471
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700472 if (ssrmod.ssr_3mic != 0) {
Naresh Tanniruc9093982015-10-16 18:05:29 +0530473 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 -0700474 if (0 != ret) {
475 ALOGE("%s: ssr_init_surround_sound_3mic_lib failed: %d "
476 "buffer_size:%d", __func__, ret, buffer_size);
477 goto fail;
478 }
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700479 }
480
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700481 /* Initialize DRC if available */
482 ret = drc_init_lib(num_out_chan, in->config.rate);
483 if (0 != ret) {
484 ALOGE("%s: drc_init_lib failed, ret %d", __func__, ret);
485 }
486
487 pthread_mutex_lock(&ssrmod.ssr_process_lock);
488 if (!ssrmod.ssr_process_thread_started) {
489 int i;
490 int output_buf_size = SSR_PERIOD_SIZE * sizeof(int16_t) * num_out_chan;
491
492 ssrmod.in_buf_data = (void *)calloc(buffer_size, NUM_IN_BUFS);
493 if (ssrmod.in_buf_data == NULL) {
494 ALOGE("%s: failed to allocate input buffer", __func__);
495 pthread_mutex_unlock(&ssrmod.ssr_process_lock);
496 ret = -ENOMEM;
497 goto fail;
498 }
499 ssrmod.out_buf_data = (void *)calloc(output_buf_size, NUM_OUT_BUFS);
500 if (ssrmod.out_buf_data == NULL) {
501 ALOGE("%s: failed to allocate output buffer", __func__);
502 pthread_mutex_unlock(&ssrmod.ssr_process_lock);
503 ret = -ENOMEM;
504 // ssrmod.in_buf_data will be freed in deinit_ssr_process_thread()
505 goto fail;
506 }
507
508 ssrmod.in_buf = NULL;
509 ssrmod.in_buf_free = NULL;
510 ssrmod.out_buf = NULL;
511 ssrmod.out_buf_free = NULL;
512
513 for (i=0; i < NUM_IN_BUFS; i++) {
514 struct pcm_buffer_queue *buf = &ssrmod.in_buf_nodes[i];
515 buf->buffer.data = &(((char *)ssrmod.in_buf_data)[i*buffer_size]);
516 buf->buffer.length = buffer_size;
517 pcm_buffer_queue_push(&ssrmod.in_buf_free, buf);
518 }
519
520 for (i=0; i < NUM_OUT_BUFS; i++) {
521 struct pcm_buffer_queue *buf = &ssrmod.out_buf_nodes[i];
522 buf->buffer.data = &(((char *)ssrmod.out_buf_data)[i*output_buf_size]);
523 buf->buffer.length = output_buf_size;
524 pcm_buffer_queue_push(&ssrmod.out_buf, buf);
525 }
526
527 ssrmod.ssr_process_thread_stop = 0;
528 ALOGV("%s: creating thread", __func__);
529 ret = pthread_create(&ssrmod.ssr_process_thread,
530 (const pthread_attr_t *) NULL,
531 ssr_process_thread, NULL);
532 if (ret != 0) {
533 ALOGE("%s: failed to create thread for surround sound recording.",
534 __func__);
535 pthread_mutex_unlock(&ssrmod.ssr_process_lock);
536 goto fail;
537 }
538
539 ssrmod.ssr_process_thread_started = 1;
540 ALOGV("%s: done creating thread", __func__);
541 }
Naresh Tanniruc9093982015-10-16 18:05:29 +0530542
543 in->config.channels = NUM_IN_CHANNELS;
544 in->config.period_size = SSR_PERIOD_SIZE;
545 in->config.period_count = in->config.channels * sizeof(int16_t);
546
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700547 pthread_mutex_unlock(&ssrmod.ssr_process_lock);
548
Aniket Kumar Lata8fc67e62017-05-02 12:33:46 -0700549 property_get("vendor.audio.ssr.pcmdump",c_multi_ch_dump,"0");
Mingming Yin49be8032013-12-19 12:51:25 -0800550 if (0 == strncmp("true", c_multi_ch_dump, sizeof("ssr.dump-pcm"))) {
551 /* Remember to change file system permission of data(e.g. chmod 777 data/),
552 otherwise, fopen may fail */
Naresh Tanniruc9093982015-10-16 18:05:29 +0530553 if ( !ssrmod.fp_input) {
554 ALOGD("%s: Opening ssr input dump file \n", __func__);
Naresh Tanniru10758b62017-06-05 21:05:53 +0530555 ssrmod.fp_input = fopen("/data/vendor/misc/audio/ssr_input_3ch.pcm", "wb");
Naresh Tanniruc9093982015-10-16 18:05:29 +0530556 }
557
558 if ( !ssrmod.fp_output) {
559 if(ssrmod.num_out_chan == 6) {
560 ALOGD("%s: Opening ssr input dump file for 6 channel\n", __func__);
Naresh Tanniru10758b62017-06-05 21:05:53 +0530561 ssrmod.fp_output = fopen("/data/vendor/misc/audio/ssr_output_6ch.pcm", "wb");
Naresh Tanniruc9093982015-10-16 18:05:29 +0530562 } else {
563 ALOGD("%s: Opening ssr input dump file for 2 channel\n", __func__);
Naresh Tanniru10758b62017-06-05 21:05:53 +0530564 ssrmod.fp_output = fopen("/data/vendor/misc/audio/ssr_output_2ch.pcm", "wb");
Naresh Tanniruc9093982015-10-16 18:05:29 +0530565 }
566 }
567
568 if ((!ssrmod.fp_input) || (!ssrmod.fp_output)) {
569 ALOGE("%s: input dump or ouput dump open failed: mfp_4ch:%p mfp_6ch:%p",
570 __func__, ssrmod.fp_input, ssrmod.fp_output);
571 }
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700572 }
573
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700574 ssrmod.in = in;
575
576 ALOGV("%s: exit", __func__);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700577 return 0;
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700578
579fail:
580 (void) audio_extn_ssr_deinit();
581 return ret;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700582}
583
584int32_t audio_extn_ssr_deinit()
585{
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700586 ALOGV("%s: entry", __func__);
587 deinit_ssr_process_thread();
588
589 if (ssrmod.drc_obj) {
590 ssrmod.drc_deinit(ssrmod.drc_obj);
591 ssrmod.drc_obj = NULL;
592 }
593
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700594 if (ssrmod.surround_obj) {
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700595
596 if (ssrmod.ssr_3mic) {
597 ssrmod.surround_rec_deinit(ssrmod.surround_obj);
598 ssrmod.surround_obj = NULL;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700599 }
Mingming Yin49be8032013-12-19 12:51:25 -0800600 if (ssrmod.surround_raw_buffer) {
601 free(ssrmod.surround_raw_buffer);
602 ssrmod.surround_raw_buffer = NULL;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700603 }
Naresh Tanniruc9093982015-10-16 18:05:29 +0530604 if (ssrmod.fp_input)
605 fclose(ssrmod.fp_input);
606 if (ssrmod.fp_output)
607 fclose(ssrmod.fp_output);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700608 }
609
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700610 if(ssrmod.drc_handle) {
611 dlclose(ssrmod.drc_handle);
612 ssrmod.drc_handle = NULL;
613 }
614
615 if(ssrmod.surround_rec_handle) {
616 dlclose(ssrmod.surround_rec_handle);
617 ssrmod.surround_rec_handle = NULL;
618 }
619
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700620 ssrmod.in = NULL;
Naresh Tanniruc9093982015-10-16 18:05:29 +0530621 //SSR session can be closed due to device switch
622 //Do not force reset ssr mode
623
624 //ssrmod.is_ssr_mode_on = false;
Mingming Yin49be8032013-12-19 12:51:25 -0800625 ALOGV("%s: exit", __func__);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700626
627 return 0;
628}
629
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700630static void *ssr_process_thread(void *context __unused)
631{
632 int32_t ret;
633
634 ALOGV("%s: enter", __func__);
635
636 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_URGENT_AUDIO);
637 set_sched_policy(0, SP_FOREGROUND);
638
639 pthread_mutex_lock(&ssrmod.ssr_process_lock);
640 while (!ssrmod.ssr_process_thread_stop) {
641 struct pcm_buffer_queue *out_buf;
642 struct pcm_buffer_queue *in_buf;
643
644 while ((!ssrmod.ssr_process_thread_stop) &&
645 ((ssrmod.out_buf_free == NULL) ||
646 (ssrmod.in_buf == NULL))) {
647 ALOGV("%s: waiting for buffers", __func__);
648 pthread_cond_wait(&ssrmod.cond_process, &ssrmod.ssr_process_lock);
649 }
650 if (ssrmod.ssr_process_thread_stop) {
651 break;
652 }
653 ALOGV("%s: got buffers", __func__);
654
655 out_buf = pcm_buffer_queue_pop(&ssrmod.out_buf_free);
656 in_buf = pcm_buffer_queue_pop(&ssrmod.in_buf);
657
658 pthread_mutex_unlock(&ssrmod.ssr_process_lock);
659
660 /* apply ssr libs to convert 4ch to 6ch */
661 if (ssrmod.ssr_3mic) {
662 ssrmod.surround_rec_process(ssrmod.surround_obj,
663 (int16_t *) in_buf->buffer.data,
664 (int16_t *) out_buf->buffer.data);
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700665 }
666
667 /* Run DRC if initialized */
668 if (ssrmod.drc_obj != NULL) {
669 ALOGV("%s: Running DRC", __func__);
670 ret = ssrmod.drc_process(ssrmod.drc_obj, out_buf->buffer.data, out_buf->buffer.data);
671 if (ret != 0) {
672 ALOGE("%s: drc_process returned %d", __func__, ret);
673 }
674 }
675
676 /*dump for raw pcm data*/
Naresh Tanniruc9093982015-10-16 18:05:29 +0530677 if (ssrmod.fp_input)
678 fwrite(in_buf->buffer.data, 1, in_buf->buffer.length, ssrmod.fp_input);
679 if (ssrmod.fp_output)
680 fwrite(out_buf->buffer.data, 1, out_buf->buffer.length, ssrmod.fp_output);
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700681
682 pthread_mutex_lock(&ssrmod.ssr_process_lock);
683
684 pcm_buffer_queue_push(&ssrmod.out_buf, out_buf);
685 pcm_buffer_queue_push(&ssrmod.in_buf_free, in_buf);
686
687 /* Read thread should go on without waiting for condition
688 * variable. If it has to wait (due to this thread not keeping
689 * up with the read requests), let this thread use the remainder
690 * of its buffers before waking up the read thread. */
691 if (ssrmod.in_buf == NULL) {
692 pthread_cond_signal(&ssrmod.cond_read);
693 }
694 }
695 pthread_mutex_unlock(&ssrmod.ssr_process_lock);
696
697 ALOGV("%s: exit", __func__);
698
699 pthread_exit(NULL);
700}
701
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700702int32_t audio_extn_ssr_read(struct audio_stream_in *stream,
703 void *buffer, size_t bytes)
704{
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700705 struct stream_in *in = (struct stream_in *)stream;
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700706 int32_t ret = 0;
707 struct pcm_buffer_queue *in_buf;
708 struct pcm_buffer_queue *out_buf;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700709
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700710 ALOGV("%s: entry", __func__);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700711
Mingming Yin49be8032013-12-19 12:51:25 -0800712 if (!ssrmod.surround_obj) {
713 ALOGE("%s: surround_obj not initialized", __func__);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700714 return -ENOMEM;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700715 }
Mingming Yin49be8032013-12-19 12:51:25 -0800716
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700717 ret = pcm_read(in->pcm, ssrmod.surround_raw_buffer, ssrmod.surround_raw_buffer_size);
Mingming Yin49be8032013-12-19 12:51:25 -0800718 if (ret < 0) {
719 ALOGE("%s: %s ret:%d", __func__, pcm_get_error(in->pcm),ret);
720 return ret;
721 }
722
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700723 pthread_mutex_lock(&ssrmod.ssr_process_lock);
Mingming Yin49be8032013-12-19 12:51:25 -0800724
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700725 if (!ssrmod.ssr_process_thread_started) {
726 pthread_mutex_unlock(&ssrmod.ssr_process_lock);
727 ALOGV("%s: ssr_process_thread not initialized", __func__);
728 return -EINVAL;
729 }
Mingming Yin49be8032013-12-19 12:51:25 -0800730
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700731 if ((ssrmod.in_buf_free == NULL) || (ssrmod.out_buf == NULL)) {
732 ALOGE("%s: waiting for buffers", __func__);
733 pthread_cond_wait(&ssrmod.cond_read, &ssrmod.ssr_process_lock);
734 if ((ssrmod.in_buf_free == NULL) || (ssrmod.out_buf == NULL)) {
735 pthread_mutex_unlock(&ssrmod.ssr_process_lock);
736 ALOGE("%s: failed to acquire buffers", __func__);
737 return -EINVAL;
738 }
739 }
740
741 in_buf = pcm_buffer_queue_pop(&ssrmod.in_buf_free);
742 out_buf = pcm_buffer_queue_pop(&ssrmod.out_buf);
743
744 memcpy(in_buf->buffer.data, ssrmod.surround_raw_buffer, in_buf->buffer.length);
745 pcm_buffer_queue_push(&ssrmod.in_buf, in_buf);
746
747 memcpy(buffer, out_buf->buffer.data, bytes);
748 pcm_buffer_queue_push(&ssrmod.out_buf_free, out_buf);
749
750 pthread_cond_signal(&ssrmod.cond_process);
751
752 pthread_mutex_unlock(&ssrmod.ssr_process_lock);
753
754 ALOGV("%s: exit", __func__);
Mingming Yin49be8032013-12-19 12:51:25 -0800755 return ret;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700756}
Mingming Yin49be8032013-12-19 12:51:25 -0800757
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700758void audio_extn_ssr_set_parameters(struct audio_device *adev __unused,
759 struct str_parms *parms)
760{
761 int err;
762 char value[4096] = {0};
763
764 //Do not update SSR mode during recording
765 if ( !ssrmod.surround_obj) {
766 int ret = 0;
767 ret = str_parms_get_str(parms, AUDIO_PARAMETER_SSRMODE_ON, value,
768 sizeof(value));
769 if (ret >= 0) {
770 if (strcmp(value, "true") == 0) {
771 ALOGD("Received SSR session request..setting SSR mode to true");
772 ssrmod.is_ssr_mode_on = true;
773 } else {
774 ALOGD("resetting SSR mode to false");
775 ssrmod.is_ssr_mode_on = false;
776 }
777 }
778 }
779 if (ssrmod.ssr_3mic && ssrmod.surround_obj) {
780 const set_param_data_t *set_params = ssrmod.surround_rec_get_set_param_data();
781 if (set_params != NULL) {
782 while (set_params->name != NULL && set_params->set_param_fn != NULL) {
783 err = str_parms_get_str(parms, set_params->name, value, sizeof(value));
784 if (err >= 0) {
785 ALOGV("Set %s to %s\n", set_params->name, value);
786 set_params->set_param_fn(ssrmod.surround_obj, value);
787 }
788 set_params++;
789 }
790 }
791 }
792}
793
794void audio_extn_ssr_get_parameters(const struct audio_device *adev __unused,
795 struct str_parms *parms,
796 struct str_parms *reply)
797{
798 int err;
799 char value[4096] = {0};
800
801 if (ssrmod.ssr_3mic && ssrmod.surround_obj) {
802 const get_param_data_t *get_params = ssrmod.surround_rec_get_get_param_data();
803 int get_all = 0;
804 err = str_parms_get_str(parms, "ssr.all", value, sizeof(value));
805 if (err >= 0) {
806 get_all = 1;
807 }
808 if (get_params != NULL) {
809 while (get_params->name != NULL && get_params->get_param_fn != NULL) {
810 err = str_parms_get_str(parms, get_params->name, value, sizeof(value));
811 if (get_all || (err >= 0)) {
812 ALOGV("Getting parameter %s", get_params->name);
813 char *val = get_params->get_param_fn(ssrmod.surround_obj);
814 if (val != NULL) {
815 str_parms_add_str(reply, get_params->name, val);
816 free(val);
817 }
818 }
819 get_params++;
820 }
821 }
822 }
823}
824
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700825#endif /* SSR_ENABLED */