blob: 653438503d2c950c93f2205b490c5a87f672ee8c [file] [log] [blame]
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07001/*
Satya Krishna Pindiprolif1cd92b2016-04-14 19:05:23 +05302 * Copyright (c) 2013-2016, 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
41#ifdef SSR_ENABLED
Mingming Yin49be8032013-12-19 12:51:25 -080042#define COEFF_ARRAY_SIZE 4
43#define FILT_SIZE ((512+1)* 6) /* # ((FFT bins)/2+1)*numOutputs */
Mingming Yin49be8032013-12-19 12:51:25 -080044#define SSR_CHANNEL_OUTPUT_NUM 6
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -070045#define SSR_PERIOD_SIZE 240
46
47#define NUM_IN_BUFS 4
48#define NUM_OUT_BUFS 4
Naresh Tanniruc9093982015-10-16 18:05:29 +053049#define NUM_IN_CHANNELS 3
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -070050
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -070051#define LIB_SURROUND_3MIC_PROC "libsurround_3mic_proc.so"
52#define LIB_DRC "libdrc.so"
53
54#define AUDIO_PARAMETER_SSRMODE_ON "ssrOn"
55
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -070056
Naresh Tanniruc9093982015-10-16 18:05:29 +053057typedef short Word16;
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -070058typedef const get_param_data_t* (*surround_rec_get_get_param_data_t)(void);
59typedef const set_param_data_t* (*surround_rec_get_set_param_data_t)(void);
60typedef int (*surround_rec_init_t)(void **, int, int, int, int, const char *);
61typedef void (*surround_rec_deinit_t)(void *);
62typedef void (*surround_rec_process_t)(void *, const int16_t *, int16_t *);
63
64typedef int (*drc_init_t)(void **, int, int, const char *);
65typedef void (*drc_deinit_t)(void *);
66typedef int (*drc_process_t)(void *, const int16_t *, int16_t *);
67
68struct pcm_buffer {
69 void *data;
70 int length;
71};
72
73struct pcm_buffer_queue {
74 struct pcm_buffer_queue *next;
75 struct pcm_buffer buffer;
76};
77
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -070078struct ssr_module {
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -070079 int ssr_3mic;
80 int num_out_chan;
Naresh Tanniruc9093982015-10-16 18:05:29 +053081 FILE *fp_input;
82 FILE *fp_output;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -070083 void *surround_obj;
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -070084 Word16 *surround_raw_buffer;
85 int surround_raw_buffer_size;
86 bool is_ssr_enabled;
87 struct stream_in *in;
88 void *drc_obj;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -070089
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -070090
91 void *surround_rec_handle;
92 surround_rec_get_get_param_data_t surround_rec_get_get_param_data;
93 surround_rec_get_set_param_data_t surround_rec_get_set_param_data;
94 surround_rec_init_t surround_rec_init;
95 surround_rec_deinit_t surround_rec_deinit;
96 surround_rec_process_t surround_rec_process;
97
98 void *drc_handle;
99 drc_init_t drc_init;
100 drc_deinit_t drc_deinit;
101 drc_process_t drc_process;
102
103 pthread_t ssr_process_thread;
104 bool ssr_process_thread_started;
105 bool ssr_process_thread_stop;
106 struct pcm_buffer_queue in_buf_nodes[NUM_IN_BUFS];
107 struct pcm_buffer_queue out_buf_nodes[NUM_OUT_BUFS];
108 void *in_buf_data;
109 void *out_buf_data;
110 struct pcm_buffer_queue *out_buf_free;
111 struct pcm_buffer_queue *out_buf;
112 struct pcm_buffer_queue *in_buf_free;
113 struct pcm_buffer_queue *in_buf;
114 pthread_mutex_t ssr_process_lock;
115 pthread_cond_t cond_process;
116 pthread_cond_t cond_read;
117 bool is_ssr_mode_on;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700118};
119
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700120static struct ssr_module ssrmod = {
Naresh Tanniruc9093982015-10-16 18:05:29 +0530121 .fp_input = NULL,
122 .fp_output = NULL,
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700123 .surround_obj = NULL,
Mingming Yin49be8032013-12-19 12:51:25 -0800124 .surround_raw_buffer = NULL,
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700125 .surround_raw_buffer_size = 0,
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700126 .is_ssr_enabled = 0,
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700127 .in = NULL,
128 .drc_obj = NULL,
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700129
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700130 .surround_rec_handle = NULL,
131 .surround_rec_get_get_param_data = NULL,
132 .surround_rec_get_set_param_data = NULL,
133 .surround_rec_init = NULL,
134 .surround_rec_deinit = NULL,
135 .surround_rec_process = NULL,
136
137 .drc_handle = NULL,
138 .drc_init = NULL,
139 .drc_deinit = NULL,
140 .drc_process = NULL,
141
142 .ssr_process_thread_stop = 0,
143 .ssr_process_thread_started = 0,
144 .in_buf_data = NULL,
145 .out_buf_data = NULL,
146 .out_buf_free = NULL,
147 .out_buf = NULL,
148 .in_buf_free = NULL,
149 .in_buf = NULL,
150 .cond_process = PTHREAD_COND_INITIALIZER,
151 .cond_read = PTHREAD_COND_INITIALIZER,
152 .ssr_process_lock = PTHREAD_MUTEX_INITIALIZER,
153 .is_ssr_mode_on = false,
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700154};
155
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700156static void *ssr_process_thread(void *context);
157
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700158static int32_t drc_init_lib(int num_chan, int sample_rate __unused)
159{
160 int ret = 0;
161 const char *cfgFileName = "";
162
163 if (ssrmod.drc_obj) {
164 ALOGE("%s: DRC library is already initialized", __func__);
165 return 0;
166 }
167
168 ssrmod.drc_handle = dlopen(LIB_DRC, RTLD_NOW);
169 if (ssrmod.drc_handle == NULL) {
170 ALOGE("%s: DLOPEN failed for %s", __func__, LIB_DRC);
171 ret = -ENOSYS;
172 goto init_fail;
173 }
174
175 ALOGV("%s: DLOPEN successful for %s", __func__, LIB_DRC);
176 ssrmod.drc_init = (drc_init_t)
177 dlsym(ssrmod.drc_handle, "DRC_init");
178 ssrmod.drc_deinit = (drc_deinit_t)
179 dlsym(ssrmod.drc_handle, "DRC_deinit");
180 ssrmod.drc_process = (drc_process_t)
181 dlsym(ssrmod.drc_handle, "DRC_process");
182
183 if (!ssrmod.drc_init ||
184 !ssrmod.drc_deinit ||
185 !ssrmod.drc_process){
186 ALOGW("%s: Could not find one of the symbols from %s",
187 __func__, LIB_DRC);
188 ret = -ENOSYS;
189 goto init_fail;
190 }
191
192 /* TO DO: different config files for different sample rates */
193 if (num_chan == 6) {
194 cfgFileName = "/system/etc/drc/drc_cfg_5.1.txt";
195 } else if (num_chan == 2) {
196 cfgFileName = "/system/etc/drc/drc_cfg_AZ.txt";
197 }
198
199 ALOGV("%s: Calling drc_init: num ch: %d, period: %d, cfg file: %s", __func__, num_chan, SSR_PERIOD_SIZE, cfgFileName);
200 ret = ssrmod.drc_init(&ssrmod.drc_obj, num_chan, SSR_PERIOD_SIZE, cfgFileName);
201 if (ret) {
202 ALOGE("drc_init failed with ret:%d",ret);
203 ret = -EINVAL;
204 goto init_fail;
205 }
206
207 return 0;
208
209init_fail:
210 if (ssrmod.drc_obj) {
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700211 ssrmod.drc_obj = NULL;
212 }
213 if(ssrmod.drc_handle) {
214 dlclose(ssrmod.drc_handle);
215 ssrmod.drc_handle = NULL;
216 }
217 return ret;
218}
219
220static int32_t ssr_init_surround_sound_3mic_lib(unsigned long buffersize, int num_in_chan, int num_out_chan, int sample_rate)
221{
222 int ret = 0;
223 const char *cfgFileName = NULL;
224
225 if ( ssrmod.surround_obj ) {
226 ALOGE("%s: surround sound library is already initialized", __func__);
227 return 0;
228 }
229
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700230 ssrmod.surround_rec_handle = dlopen(LIB_SURROUND_3MIC_PROC, RTLD_NOW);
231 if (ssrmod.surround_rec_handle == NULL) {
232 ALOGE("%s: DLOPEN failed for %s", __func__, LIB_SURROUND_3MIC_PROC);
Naresh Tanniruc9093982015-10-16 18:05:29 +0530233 goto init_fail;
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700234 } else {
235 ALOGV("%s: DLOPEN successful for %s", __func__, LIB_SURROUND_3MIC_PROC);
236 ssrmod.surround_rec_get_get_param_data = (surround_rec_get_get_param_data_t)
237 dlsym(ssrmod.surround_rec_handle, "surround_rec_get_get_param_data");
238
239 ssrmod.surround_rec_get_set_param_data = (surround_rec_get_set_param_data_t)
240 dlsym(ssrmod.surround_rec_handle, "surround_rec_get_set_param_data");
241 ssrmod.surround_rec_init = (surround_rec_init_t)
242 dlsym(ssrmod.surround_rec_handle, "surround_rec_init");
243 ssrmod.surround_rec_deinit = (surround_rec_deinit_t)
244 dlsym(ssrmod.surround_rec_handle, "surround_rec_deinit");
245 ssrmod.surround_rec_process = (surround_rec_process_t)
246 dlsym(ssrmod.surround_rec_handle, "surround_rec_process");
247
248 if (!ssrmod.surround_rec_get_get_param_data ||
249 !ssrmod.surround_rec_get_set_param_data ||
250 !ssrmod.surround_rec_init ||
251 !ssrmod.surround_rec_deinit ||
252 !ssrmod.surround_rec_process){
253 ALOGW("%s: Could not find the one of the symbols from %s",
254 __func__, LIB_SURROUND_3MIC_PROC);
255 ret = -ENOSYS;
256 goto init_fail;
257 }
258 }
259
Naresh Tanniruc9093982015-10-16 18:05:29 +0530260 /* Allocate memory for input buffer */
261 ssrmod.surround_raw_buffer = (Word16 *) calloc(buffersize,
262 sizeof(Word16));
263 if (!ssrmod.surround_raw_buffer) {
264 ALOGE("%s: Memory allocation failure. Not able to allocate "
265 "memory for surroundInputBuffer", __func__);
266 ret = -ENOMEM;
267 goto init_fail;
268 }
269
270 ssrmod.surround_raw_buffer_size = buffersize;
271
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700272 ssrmod.num_out_chan = num_out_chan;
273
274 if (num_out_chan == 6) {
275 cfgFileName = "/system/etc/surround_sound_3mic/surround_sound_rec_5.1.cfg";
276 } else if (num_out_chan == 2) {
277 cfgFileName = "/system/etc/surround_sound_3mic/surround_sound_rec_AZ.cfg";
278 } else {
279 ALOGE("%s: No cfg file for num_out_chan: %d", __func__, num_out_chan);
280 }
281
282 ALOGV("%s: Calling surround_rec_init: in ch: %d, out ch: %d, period: %d, sample rate: %d, cfg file: %s",
283 __func__, num_in_chan, num_out_chan, SSR_PERIOD_SIZE, sample_rate, cfgFileName);
284 ret = ssrmod.surround_rec_init(&ssrmod.surround_obj,
285 num_in_chan, num_out_chan, SSR_PERIOD_SIZE, sample_rate, cfgFileName);
286 if (ret) {
287 ALOGE("surround_rec_init failed with ret:%d",ret);
288 ret = -EINVAL;
289 goto init_fail;
290 }
291
292 return 0;
293
294init_fail:
295 if (ssrmod.surround_obj) {
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700296 ssrmod.surround_obj = NULL;
297 }
298 if (ssrmod.surround_raw_buffer) {
299 free(ssrmod.surround_raw_buffer);
300 ssrmod.surround_raw_buffer = NULL;
301 ssrmod.surround_raw_buffer_size = 0;
302 }
303 if(ssrmod.surround_rec_handle) {
304 dlclose(ssrmod.surround_rec_handle);
305 ssrmod.surround_rec_handle = NULL;
306 }
307 return ret;
308}
309
Mingming Yin49be8032013-12-19 12:51:25 -0800310void audio_extn_ssr_update_enabled()
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700311{
312 char ssr_enabled[PROPERTY_VALUE_MAX] = "false";
313
314 property_get("ro.qc.sdk.audio.ssr",ssr_enabled,"0");
315 if (!strncmp("true", ssr_enabled, 4)) {
316 ALOGD("%s: surround sound recording is supported", __func__);
317 ssrmod.is_ssr_enabled = true;
318 } else {
319 ALOGD("%s: surround sound recording is not supported", __func__);
320 ssrmod.is_ssr_enabled = false;
321 }
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700322}
323
324bool audio_extn_ssr_get_enabled()
325{
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700326 ALOGV("%s: is_ssr_enabled:%d is_ssr_mode_on:%d ", __func__, ssrmod.is_ssr_enabled, ssrmod.is_ssr_mode_on);
327
328 if(ssrmod.is_ssr_enabled && ssrmod.is_ssr_mode_on)
329 return true;
330
331 return false;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700332}
333
Naresh Tanniruc9093982015-10-16 18:05:29 +0530334int audio_extn_ssr_check_and_set_usecase(struct stream_in *in)
335{
336 int ret = -1;
337 int channel_count = audio_channel_count_from_in_mask(in->channel_mask);
338 audio_devices_t devices = in->device;
339 audio_source_t source = in->source;
340
341 /* validate input params
342 * only stereo and 5:1 channel config is supported
343 * only AUDIO_DEVICE_IN_BUILTIN_MIC, AUDIO_DEVICE_IN_BACK_MIC supports 3 mics */
344 if (audio_extn_ssr_get_enabled() &&
345 ((channel_count == 2) || (channel_count == 6)) &&
346 ((AUDIO_SOURCE_MIC == source) || (AUDIO_SOURCE_CAMCORDER == source)) &&
347 ((AUDIO_DEVICE_IN_BUILTIN_MIC == devices) || (AUDIO_DEVICE_IN_BACK_MIC == devices))) {
348
349 ALOGD("%s: Found SSR use case starting SSR lib with channel_count :%d",
350 __func__, channel_count);
351
352 if (!audio_extn_ssr_init(in, channel_count)) {
353 ALOGD("%s: Created SSR session succesfully", __func__);
354 ret = 0;
355 } else {
356 ALOGE("%s: Unable to start SSR record session", __func__);
357 }
358 }
359 return ret;
360}
361
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700362static void pcm_buffer_queue_push(struct pcm_buffer_queue **queue,
363 struct pcm_buffer_queue *node)
364{
365 struct pcm_buffer_queue *iter;
366
367 node->next = NULL;
368 if ((*queue) == NULL) {
369 *queue = node;
370 } else {
371 iter = *queue;
372 while (iter->next) {
373 iter = iter->next;
374 }
375 iter->next = node;
376 }
377}
378
379static struct pcm_buffer_queue *pcm_buffer_queue_pop(struct pcm_buffer_queue **queue)
380{
381 struct pcm_buffer_queue *node = (*queue);
382 if (node != NULL) {
383 *queue = node->next;
384 node->next = NULL;
385 }
386 return node;
387}
388
389static void deinit_ssr_process_thread()
390{
391 pthread_mutex_lock(&ssrmod.ssr_process_lock);
392 ssrmod.ssr_process_thread_stop = 1;
Naresh Tanniruc9093982015-10-16 18:05:29 +0530393
394 if(ssrmod.in_buf_data != NULL)
395 free(ssrmod.in_buf_data);
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700396 ssrmod.in_buf_data = NULL;
397 ssrmod.in_buf = NULL;
398 ssrmod.in_buf_free = NULL;
Naresh Tanniruc9093982015-10-16 18:05:29 +0530399
400 if(ssrmod.out_buf_data != NULL)
401 free(ssrmod.out_buf_data);
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700402 ssrmod.out_buf_data = NULL;
403 ssrmod.out_buf = NULL;
404 ssrmod.out_buf_free = NULL;
405 pthread_cond_broadcast(&ssrmod.cond_process);
406 pthread_cond_broadcast(&ssrmod.cond_read);
407 pthread_mutex_unlock(&ssrmod.ssr_process_lock);
408 if (ssrmod.ssr_process_thread_started) {
409 pthread_join(ssrmod.ssr_process_thread, (void **)NULL);
410 ssrmod.ssr_process_thread_started = 0;
411 }
412}
413
414struct stream_in *audio_extn_ssr_get_stream()
415{
416 return ssrmod.in;
417}
418
419int32_t audio_extn_ssr_init(struct stream_in *in, int num_out_chan)
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700420{
Naresh Tanniruc9093982015-10-16 18:05:29 +0530421 uint32_t ret = -1;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700422 char c_multi_ch_dump[128] = {0};
423 uint32_t buffer_size;
424
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700425 ALOGD("%s: ssr case, sample rate %d", __func__, in->config.rate);
426
427 if (ssrmod.surround_obj != NULL) {
428 ALOGV("%s: reinitializing surround sound library", __func__);
429 audio_extn_ssr_deinit();
430 }
431
Naresh Tanniruc9093982015-10-16 18:05:29 +0530432 if (audio_extn_ssr_get_enabled()) {
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700433 ssrmod.ssr_3mic = 1;
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700434 } else {
Naresh Tanniruc9093982015-10-16 18:05:29 +0530435 ALOGE(" Rejecting SSR -- init is called without enabling SSR");
436 goto fail;
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700437 }
438
Naresh Tanniruc9093982015-10-16 18:05:29 +0530439 /* buffer size equals to period_size * period_count */
440 buffer_size = SSR_PERIOD_SIZE * NUM_IN_CHANNELS * sizeof(int16_t);
Mingming Yin49be8032013-12-19 12:51:25 -0800441 ALOGV("%s: buffer_size: %d", __func__, buffer_size);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700442
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700443 if (ssrmod.ssr_3mic != 0) {
Naresh Tanniruc9093982015-10-16 18:05:29 +0530444 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 -0700445 if (0 != ret) {
446 ALOGE("%s: ssr_init_surround_sound_3mic_lib failed: %d "
447 "buffer_size:%d", __func__, ret, buffer_size);
448 goto fail;
449 }
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700450 }
451
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700452 /* Initialize DRC if available */
453 ret = drc_init_lib(num_out_chan, in->config.rate);
454 if (0 != ret) {
455 ALOGE("%s: drc_init_lib failed, ret %d", __func__, ret);
456 }
457
458 pthread_mutex_lock(&ssrmod.ssr_process_lock);
459 if (!ssrmod.ssr_process_thread_started) {
460 int i;
461 int output_buf_size = SSR_PERIOD_SIZE * sizeof(int16_t) * num_out_chan;
462
463 ssrmod.in_buf_data = (void *)calloc(buffer_size, NUM_IN_BUFS);
464 if (ssrmod.in_buf_data == NULL) {
465 ALOGE("%s: failed to allocate input buffer", __func__);
466 pthread_mutex_unlock(&ssrmod.ssr_process_lock);
467 ret = -ENOMEM;
468 goto fail;
469 }
470 ssrmod.out_buf_data = (void *)calloc(output_buf_size, NUM_OUT_BUFS);
471 if (ssrmod.out_buf_data == NULL) {
472 ALOGE("%s: failed to allocate output buffer", __func__);
473 pthread_mutex_unlock(&ssrmod.ssr_process_lock);
474 ret = -ENOMEM;
475 // ssrmod.in_buf_data will be freed in deinit_ssr_process_thread()
476 goto fail;
477 }
478
479 ssrmod.in_buf = NULL;
480 ssrmod.in_buf_free = NULL;
481 ssrmod.out_buf = NULL;
482 ssrmod.out_buf_free = NULL;
483
484 for (i=0; i < NUM_IN_BUFS; i++) {
485 struct pcm_buffer_queue *buf = &ssrmod.in_buf_nodes[i];
486 buf->buffer.data = &(((char *)ssrmod.in_buf_data)[i*buffer_size]);
487 buf->buffer.length = buffer_size;
488 pcm_buffer_queue_push(&ssrmod.in_buf_free, buf);
489 }
490
491 for (i=0; i < NUM_OUT_BUFS; i++) {
492 struct pcm_buffer_queue *buf = &ssrmod.out_buf_nodes[i];
493 buf->buffer.data = &(((char *)ssrmod.out_buf_data)[i*output_buf_size]);
494 buf->buffer.length = output_buf_size;
495 pcm_buffer_queue_push(&ssrmod.out_buf, buf);
496 }
497
498 ssrmod.ssr_process_thread_stop = 0;
499 ALOGV("%s: creating thread", __func__);
500 ret = pthread_create(&ssrmod.ssr_process_thread,
501 (const pthread_attr_t *) NULL,
502 ssr_process_thread, NULL);
503 if (ret != 0) {
504 ALOGE("%s: failed to create thread for surround sound recording.",
505 __func__);
506 pthread_mutex_unlock(&ssrmod.ssr_process_lock);
507 goto fail;
508 }
509
510 ssrmod.ssr_process_thread_started = 1;
511 ALOGV("%s: done creating thread", __func__);
512 }
Naresh Tanniruc9093982015-10-16 18:05:29 +0530513
514 in->config.channels = NUM_IN_CHANNELS;
515 in->config.period_size = SSR_PERIOD_SIZE;
516 in->config.period_count = in->config.channels * sizeof(int16_t);
517
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700518 pthread_mutex_unlock(&ssrmod.ssr_process_lock);
519
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700520 property_get("ssr.pcmdump",c_multi_ch_dump,"0");
Mingming Yin49be8032013-12-19 12:51:25 -0800521 if (0 == strncmp("true", c_multi_ch_dump, sizeof("ssr.dump-pcm"))) {
522 /* Remember to change file system permission of data(e.g. chmod 777 data/),
523 otherwise, fopen may fail */
Naresh Tanniruc9093982015-10-16 18:05:29 +0530524 if ( !ssrmod.fp_input) {
525 ALOGD("%s: Opening ssr input dump file \n", __func__);
526 ssrmod.fp_input = fopen("/data/misc/audio/ssr_input_3ch.pcm", "wb");
527 }
528
529 if ( !ssrmod.fp_output) {
530 if(ssrmod.num_out_chan == 6) {
531 ALOGD("%s: Opening ssr input dump file for 6 channel\n", __func__);
532 ssrmod.fp_output = fopen("/data/misc/audio/ssr_output_6ch.pcm", "wb");
533 } else {
534 ALOGD("%s: Opening ssr input dump file for 2 channel\n", __func__);
535 ssrmod.fp_output = fopen("/data/misc/audio/ssr_output_2ch.pcm", "wb");
536 }
537 }
538
539 if ((!ssrmod.fp_input) || (!ssrmod.fp_output)) {
540 ALOGE("%s: input dump or ouput dump open failed: mfp_4ch:%p mfp_6ch:%p",
541 __func__, ssrmod.fp_input, ssrmod.fp_output);
542 }
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700543 }
544
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700545 ssrmod.in = in;
546
547 ALOGV("%s: exit", __func__);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700548 return 0;
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700549
550fail:
551 (void) audio_extn_ssr_deinit();
552 return ret;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700553}
554
555int32_t audio_extn_ssr_deinit()
556{
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700557 ALOGV("%s: entry", __func__);
558 deinit_ssr_process_thread();
559
560 if (ssrmod.drc_obj) {
561 ssrmod.drc_deinit(ssrmod.drc_obj);
562 ssrmod.drc_obj = NULL;
563 }
564
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700565 if (ssrmod.surround_obj) {
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700566
567 if (ssrmod.ssr_3mic) {
568 ssrmod.surround_rec_deinit(ssrmod.surround_obj);
569 ssrmod.surround_obj = NULL;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700570 }
Mingming Yin49be8032013-12-19 12:51:25 -0800571 if (ssrmod.surround_raw_buffer) {
572 free(ssrmod.surround_raw_buffer);
573 ssrmod.surround_raw_buffer = NULL;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700574 }
Naresh Tanniruc9093982015-10-16 18:05:29 +0530575 if (ssrmod.fp_input)
576 fclose(ssrmod.fp_input);
577 if (ssrmod.fp_output)
578 fclose(ssrmod.fp_output);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700579 }
580
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700581 if(ssrmod.drc_handle) {
582 dlclose(ssrmod.drc_handle);
583 ssrmod.drc_handle = NULL;
584 }
585
586 if(ssrmod.surround_rec_handle) {
587 dlclose(ssrmod.surround_rec_handle);
588 ssrmod.surround_rec_handle = NULL;
589 }
590
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700591 ssrmod.in = NULL;
Naresh Tanniruc9093982015-10-16 18:05:29 +0530592 //SSR session can be closed due to device switch
593 //Do not force reset ssr mode
594
595 //ssrmod.is_ssr_mode_on = false;
Mingming Yin49be8032013-12-19 12:51:25 -0800596 ALOGV("%s: exit", __func__);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700597
598 return 0;
599}
600
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700601static void *ssr_process_thread(void *context __unused)
602{
603 int32_t ret;
604
605 ALOGV("%s: enter", __func__);
606
607 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_URGENT_AUDIO);
608 set_sched_policy(0, SP_FOREGROUND);
609
610 pthread_mutex_lock(&ssrmod.ssr_process_lock);
611 while (!ssrmod.ssr_process_thread_stop) {
612 struct pcm_buffer_queue *out_buf;
613 struct pcm_buffer_queue *in_buf;
614
615 while ((!ssrmod.ssr_process_thread_stop) &&
616 ((ssrmod.out_buf_free == NULL) ||
617 (ssrmod.in_buf == NULL))) {
618 ALOGV("%s: waiting for buffers", __func__);
619 pthread_cond_wait(&ssrmod.cond_process, &ssrmod.ssr_process_lock);
620 }
621 if (ssrmod.ssr_process_thread_stop) {
622 break;
623 }
624 ALOGV("%s: got buffers", __func__);
625
626 out_buf = pcm_buffer_queue_pop(&ssrmod.out_buf_free);
627 in_buf = pcm_buffer_queue_pop(&ssrmod.in_buf);
628
629 pthread_mutex_unlock(&ssrmod.ssr_process_lock);
630
631 /* apply ssr libs to convert 4ch to 6ch */
632 if (ssrmod.ssr_3mic) {
633 ssrmod.surround_rec_process(ssrmod.surround_obj,
634 (int16_t *) in_buf->buffer.data,
635 (int16_t *) out_buf->buffer.data);
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700636 }
637
638 /* Run DRC if initialized */
639 if (ssrmod.drc_obj != NULL) {
640 ALOGV("%s: Running DRC", __func__);
641 ret = ssrmod.drc_process(ssrmod.drc_obj, out_buf->buffer.data, out_buf->buffer.data);
642 if (ret != 0) {
643 ALOGE("%s: drc_process returned %d", __func__, ret);
644 }
645 }
646
647 /*dump for raw pcm data*/
Naresh Tanniruc9093982015-10-16 18:05:29 +0530648 if (ssrmod.fp_input)
649 fwrite(in_buf->buffer.data, 1, in_buf->buffer.length, ssrmod.fp_input);
650 if (ssrmod.fp_output)
651 fwrite(out_buf->buffer.data, 1, out_buf->buffer.length, ssrmod.fp_output);
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700652
653 pthread_mutex_lock(&ssrmod.ssr_process_lock);
654
655 pcm_buffer_queue_push(&ssrmod.out_buf, out_buf);
656 pcm_buffer_queue_push(&ssrmod.in_buf_free, in_buf);
657
658 /* Read thread should go on without waiting for condition
659 * variable. If it has to wait (due to this thread not keeping
660 * up with the read requests), let this thread use the remainder
661 * of its buffers before waking up the read thread. */
662 if (ssrmod.in_buf == NULL) {
663 pthread_cond_signal(&ssrmod.cond_read);
664 }
665 }
666 pthread_mutex_unlock(&ssrmod.ssr_process_lock);
667
668 ALOGV("%s: exit", __func__);
669
670 pthread_exit(NULL);
671}
672
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700673int32_t audio_extn_ssr_read(struct audio_stream_in *stream,
674 void *buffer, size_t bytes)
675{
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700676 struct stream_in *in = (struct stream_in *)stream;
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700677 int32_t ret = 0;
678 struct pcm_buffer_queue *in_buf;
679 struct pcm_buffer_queue *out_buf;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700680
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700681 ALOGV("%s: entry", __func__);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700682
Mingming Yin49be8032013-12-19 12:51:25 -0800683 if (!ssrmod.surround_obj) {
684 ALOGE("%s: surround_obj not initialized", __func__);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700685 return -ENOMEM;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700686 }
Mingming Yin49be8032013-12-19 12:51:25 -0800687
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700688 ret = pcm_read(in->pcm, ssrmod.surround_raw_buffer, ssrmod.surround_raw_buffer_size);
Mingming Yin49be8032013-12-19 12:51:25 -0800689 if (ret < 0) {
690 ALOGE("%s: %s ret:%d", __func__, pcm_get_error(in->pcm),ret);
691 return ret;
692 }
693
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700694 pthread_mutex_lock(&ssrmod.ssr_process_lock);
Mingming Yin49be8032013-12-19 12:51:25 -0800695
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700696 if (!ssrmod.ssr_process_thread_started) {
697 pthread_mutex_unlock(&ssrmod.ssr_process_lock);
698 ALOGV("%s: ssr_process_thread not initialized", __func__);
699 return -EINVAL;
700 }
Mingming Yin49be8032013-12-19 12:51:25 -0800701
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700702 if ((ssrmod.in_buf_free == NULL) || (ssrmod.out_buf == NULL)) {
703 ALOGE("%s: waiting for buffers", __func__);
704 pthread_cond_wait(&ssrmod.cond_read, &ssrmod.ssr_process_lock);
705 if ((ssrmod.in_buf_free == NULL) || (ssrmod.out_buf == NULL)) {
706 pthread_mutex_unlock(&ssrmod.ssr_process_lock);
707 ALOGE("%s: failed to acquire buffers", __func__);
708 return -EINVAL;
709 }
710 }
711
712 in_buf = pcm_buffer_queue_pop(&ssrmod.in_buf_free);
713 out_buf = pcm_buffer_queue_pop(&ssrmod.out_buf);
714
715 memcpy(in_buf->buffer.data, ssrmod.surround_raw_buffer, in_buf->buffer.length);
716 pcm_buffer_queue_push(&ssrmod.in_buf, in_buf);
717
718 memcpy(buffer, out_buf->buffer.data, bytes);
719 pcm_buffer_queue_push(&ssrmod.out_buf_free, out_buf);
720
721 pthread_cond_signal(&ssrmod.cond_process);
722
723 pthread_mutex_unlock(&ssrmod.ssr_process_lock);
724
725 ALOGV("%s: exit", __func__);
Mingming Yin49be8032013-12-19 12:51:25 -0800726 return ret;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700727}
Mingming Yin49be8032013-12-19 12:51:25 -0800728
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700729void audio_extn_ssr_set_parameters(struct audio_device *adev __unused,
730 struct str_parms *parms)
731{
732 int err;
733 char value[4096] = {0};
734
735 //Do not update SSR mode during recording
736 if ( !ssrmod.surround_obj) {
737 int ret = 0;
738 ret = str_parms_get_str(parms, AUDIO_PARAMETER_SSRMODE_ON, value,
739 sizeof(value));
740 if (ret >= 0) {
741 if (strcmp(value, "true") == 0) {
742 ALOGD("Received SSR session request..setting SSR mode to true");
743 ssrmod.is_ssr_mode_on = true;
744 } else {
745 ALOGD("resetting SSR mode to false");
746 ssrmod.is_ssr_mode_on = false;
747 }
748 }
749 }
750 if (ssrmod.ssr_3mic && ssrmod.surround_obj) {
751 const set_param_data_t *set_params = ssrmod.surround_rec_get_set_param_data();
752 if (set_params != NULL) {
753 while (set_params->name != NULL && set_params->set_param_fn != NULL) {
754 err = str_parms_get_str(parms, set_params->name, value, sizeof(value));
755 if (err >= 0) {
756 ALOGV("Set %s to %s\n", set_params->name, value);
757 set_params->set_param_fn(ssrmod.surround_obj, value);
758 }
759 set_params++;
760 }
761 }
762 }
763}
764
765void audio_extn_ssr_get_parameters(const struct audio_device *adev __unused,
766 struct str_parms *parms,
767 struct str_parms *reply)
768{
769 int err;
770 char value[4096] = {0};
771
772 if (ssrmod.ssr_3mic && ssrmod.surround_obj) {
773 const get_param_data_t *get_params = ssrmod.surround_rec_get_get_param_data();
774 int get_all = 0;
775 err = str_parms_get_str(parms, "ssr.all", value, sizeof(value));
776 if (err >= 0) {
777 get_all = 1;
778 }
779 if (get_params != NULL) {
780 while (get_params->name != NULL && get_params->get_param_fn != NULL) {
781 err = str_parms_get_str(parms, get_params->name, value, sizeof(value));
782 if (get_all || (err >= 0)) {
783 ALOGV("Getting parameter %s", get_params->name);
784 char *val = get_params->get_param_fn(ssrmod.surround_obj);
785 if (val != NULL) {
786 str_parms_add_str(reply, get_params->name, val);
787 free(val);
788 }
789 }
790 get_params++;
791 }
792 }
793 }
794}
795
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700796#endif /* SSR_ENABLED */