blob: f55f3cedb38242ca0c5816e6f4501b68c2a6b72d [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)) &&
Manish Dewanganba9fcfa2016-03-24 16:20:06 +0530347 ((AUDIO_DEVICE_IN_BUILTIN_MIC == devices) || (AUDIO_DEVICE_IN_BACK_MIC == devices)) &&
348 (in->format == AUDIO_FORMAT_PCM_16_BIT)) {
Naresh Tanniruc9093982015-10-16 18:05:29 +0530349
350 ALOGD("%s: Found SSR use case starting SSR lib with channel_count :%d",
351 __func__, channel_count);
352
353 if (!audio_extn_ssr_init(in, channel_count)) {
354 ALOGD("%s: Created SSR session succesfully", __func__);
355 ret = 0;
356 } else {
357 ALOGE("%s: Unable to start SSR record session", __func__);
358 }
359 }
360 return ret;
361}
362
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700363static void pcm_buffer_queue_push(struct pcm_buffer_queue **queue,
364 struct pcm_buffer_queue *node)
365{
366 struct pcm_buffer_queue *iter;
367
368 node->next = NULL;
369 if ((*queue) == NULL) {
370 *queue = node;
371 } else {
372 iter = *queue;
373 while (iter->next) {
374 iter = iter->next;
375 }
376 iter->next = node;
377 }
378}
379
380static struct pcm_buffer_queue *pcm_buffer_queue_pop(struct pcm_buffer_queue **queue)
381{
382 struct pcm_buffer_queue *node = (*queue);
383 if (node != NULL) {
384 *queue = node->next;
385 node->next = NULL;
386 }
387 return node;
388}
389
390static void deinit_ssr_process_thread()
391{
392 pthread_mutex_lock(&ssrmod.ssr_process_lock);
393 ssrmod.ssr_process_thread_stop = 1;
Naresh Tanniruc9093982015-10-16 18:05:29 +0530394
395 if(ssrmod.in_buf_data != NULL)
396 free(ssrmod.in_buf_data);
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700397 ssrmod.in_buf_data = NULL;
398 ssrmod.in_buf = NULL;
399 ssrmod.in_buf_free = NULL;
Naresh Tanniruc9093982015-10-16 18:05:29 +0530400
401 if(ssrmod.out_buf_data != NULL)
402 free(ssrmod.out_buf_data);
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700403 ssrmod.out_buf_data = NULL;
404 ssrmod.out_buf = NULL;
405 ssrmod.out_buf_free = NULL;
406 pthread_cond_broadcast(&ssrmod.cond_process);
407 pthread_cond_broadcast(&ssrmod.cond_read);
408 pthread_mutex_unlock(&ssrmod.ssr_process_lock);
409 if (ssrmod.ssr_process_thread_started) {
410 pthread_join(ssrmod.ssr_process_thread, (void **)NULL);
411 ssrmod.ssr_process_thread_started = 0;
412 }
413}
414
415struct stream_in *audio_extn_ssr_get_stream()
416{
417 return ssrmod.in;
418}
419
420int32_t audio_extn_ssr_init(struct stream_in *in, int num_out_chan)
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700421{
Naresh Tanniruc9093982015-10-16 18:05:29 +0530422 uint32_t ret = -1;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700423 char c_multi_ch_dump[128] = {0};
424 uint32_t buffer_size;
425
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700426 ALOGD("%s: ssr case, sample rate %d", __func__, in->config.rate);
427
428 if (ssrmod.surround_obj != NULL) {
429 ALOGV("%s: reinitializing surround sound library", __func__);
430 audio_extn_ssr_deinit();
431 }
432
Naresh Tanniruc9093982015-10-16 18:05:29 +0530433 if (audio_extn_ssr_get_enabled()) {
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700434 ssrmod.ssr_3mic = 1;
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700435 } else {
Naresh Tanniruc9093982015-10-16 18:05:29 +0530436 ALOGE(" Rejecting SSR -- init is called without enabling SSR");
437 goto fail;
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700438 }
439
Naresh Tanniruc9093982015-10-16 18:05:29 +0530440 /* buffer size equals to period_size * period_count */
441 buffer_size = SSR_PERIOD_SIZE * NUM_IN_CHANNELS * sizeof(int16_t);
Mingming Yin49be8032013-12-19 12:51:25 -0800442 ALOGV("%s: buffer_size: %d", __func__, buffer_size);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700443
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700444 if (ssrmod.ssr_3mic != 0) {
Naresh Tanniruc9093982015-10-16 18:05:29 +0530445 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 -0700446 if (0 != ret) {
447 ALOGE("%s: ssr_init_surround_sound_3mic_lib failed: %d "
448 "buffer_size:%d", __func__, ret, buffer_size);
449 goto fail;
450 }
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700451 }
452
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700453 /* Initialize DRC if available */
454 ret = drc_init_lib(num_out_chan, in->config.rate);
455 if (0 != ret) {
456 ALOGE("%s: drc_init_lib failed, ret %d", __func__, ret);
457 }
458
459 pthread_mutex_lock(&ssrmod.ssr_process_lock);
460 if (!ssrmod.ssr_process_thread_started) {
461 int i;
462 int output_buf_size = SSR_PERIOD_SIZE * sizeof(int16_t) * num_out_chan;
463
464 ssrmod.in_buf_data = (void *)calloc(buffer_size, NUM_IN_BUFS);
465 if (ssrmod.in_buf_data == NULL) {
466 ALOGE("%s: failed to allocate input buffer", __func__);
467 pthread_mutex_unlock(&ssrmod.ssr_process_lock);
468 ret = -ENOMEM;
469 goto fail;
470 }
471 ssrmod.out_buf_data = (void *)calloc(output_buf_size, NUM_OUT_BUFS);
472 if (ssrmod.out_buf_data == NULL) {
473 ALOGE("%s: failed to allocate output buffer", __func__);
474 pthread_mutex_unlock(&ssrmod.ssr_process_lock);
475 ret = -ENOMEM;
476 // ssrmod.in_buf_data will be freed in deinit_ssr_process_thread()
477 goto fail;
478 }
479
480 ssrmod.in_buf = NULL;
481 ssrmod.in_buf_free = NULL;
482 ssrmod.out_buf = NULL;
483 ssrmod.out_buf_free = NULL;
484
485 for (i=0; i < NUM_IN_BUFS; i++) {
486 struct pcm_buffer_queue *buf = &ssrmod.in_buf_nodes[i];
487 buf->buffer.data = &(((char *)ssrmod.in_buf_data)[i*buffer_size]);
488 buf->buffer.length = buffer_size;
489 pcm_buffer_queue_push(&ssrmod.in_buf_free, buf);
490 }
491
492 for (i=0; i < NUM_OUT_BUFS; i++) {
493 struct pcm_buffer_queue *buf = &ssrmod.out_buf_nodes[i];
494 buf->buffer.data = &(((char *)ssrmod.out_buf_data)[i*output_buf_size]);
495 buf->buffer.length = output_buf_size;
496 pcm_buffer_queue_push(&ssrmod.out_buf, buf);
497 }
498
499 ssrmod.ssr_process_thread_stop = 0;
500 ALOGV("%s: creating thread", __func__);
501 ret = pthread_create(&ssrmod.ssr_process_thread,
502 (const pthread_attr_t *) NULL,
503 ssr_process_thread, NULL);
504 if (ret != 0) {
505 ALOGE("%s: failed to create thread for surround sound recording.",
506 __func__);
507 pthread_mutex_unlock(&ssrmod.ssr_process_lock);
508 goto fail;
509 }
510
511 ssrmod.ssr_process_thread_started = 1;
512 ALOGV("%s: done creating thread", __func__);
513 }
Naresh Tanniruc9093982015-10-16 18:05:29 +0530514
515 in->config.channels = NUM_IN_CHANNELS;
516 in->config.period_size = SSR_PERIOD_SIZE;
517 in->config.period_count = in->config.channels * sizeof(int16_t);
518
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700519 pthread_mutex_unlock(&ssrmod.ssr_process_lock);
520
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700521 property_get("ssr.pcmdump",c_multi_ch_dump,"0");
Mingming Yin49be8032013-12-19 12:51:25 -0800522 if (0 == strncmp("true", c_multi_ch_dump, sizeof("ssr.dump-pcm"))) {
523 /* Remember to change file system permission of data(e.g. chmod 777 data/),
524 otherwise, fopen may fail */
Naresh Tanniruc9093982015-10-16 18:05:29 +0530525 if ( !ssrmod.fp_input) {
526 ALOGD("%s: Opening ssr input dump file \n", __func__);
527 ssrmod.fp_input = fopen("/data/misc/audio/ssr_input_3ch.pcm", "wb");
528 }
529
530 if ( !ssrmod.fp_output) {
531 if(ssrmod.num_out_chan == 6) {
532 ALOGD("%s: Opening ssr input dump file for 6 channel\n", __func__);
533 ssrmod.fp_output = fopen("/data/misc/audio/ssr_output_6ch.pcm", "wb");
534 } else {
535 ALOGD("%s: Opening ssr input dump file for 2 channel\n", __func__);
536 ssrmod.fp_output = fopen("/data/misc/audio/ssr_output_2ch.pcm", "wb");
537 }
538 }
539
540 if ((!ssrmod.fp_input) || (!ssrmod.fp_output)) {
541 ALOGE("%s: input dump or ouput dump open failed: mfp_4ch:%p mfp_6ch:%p",
542 __func__, ssrmod.fp_input, ssrmod.fp_output);
543 }
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700544 }
545
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700546 ssrmod.in = in;
547
548 ALOGV("%s: exit", __func__);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700549 return 0;
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700550
551fail:
552 (void) audio_extn_ssr_deinit();
553 return ret;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700554}
555
556int32_t audio_extn_ssr_deinit()
557{
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700558 ALOGV("%s: entry", __func__);
559 deinit_ssr_process_thread();
560
561 if (ssrmod.drc_obj) {
562 ssrmod.drc_deinit(ssrmod.drc_obj);
563 ssrmod.drc_obj = NULL;
564 }
565
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700566 if (ssrmod.surround_obj) {
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700567
568 if (ssrmod.ssr_3mic) {
569 ssrmod.surround_rec_deinit(ssrmod.surround_obj);
570 ssrmod.surround_obj = NULL;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700571 }
Mingming Yin49be8032013-12-19 12:51:25 -0800572 if (ssrmod.surround_raw_buffer) {
573 free(ssrmod.surround_raw_buffer);
574 ssrmod.surround_raw_buffer = NULL;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700575 }
Naresh Tanniruc9093982015-10-16 18:05:29 +0530576 if (ssrmod.fp_input)
577 fclose(ssrmod.fp_input);
578 if (ssrmod.fp_output)
579 fclose(ssrmod.fp_output);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700580 }
581
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700582 if(ssrmod.drc_handle) {
583 dlclose(ssrmod.drc_handle);
584 ssrmod.drc_handle = NULL;
585 }
586
587 if(ssrmod.surround_rec_handle) {
588 dlclose(ssrmod.surround_rec_handle);
589 ssrmod.surround_rec_handle = NULL;
590 }
591
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700592 ssrmod.in = NULL;
Naresh Tanniruc9093982015-10-16 18:05:29 +0530593 //SSR session can be closed due to device switch
594 //Do not force reset ssr mode
595
596 //ssrmod.is_ssr_mode_on = false;
Mingming Yin49be8032013-12-19 12:51:25 -0800597 ALOGV("%s: exit", __func__);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700598
599 return 0;
600}
601
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700602static void *ssr_process_thread(void *context __unused)
603{
604 int32_t ret;
605
606 ALOGV("%s: enter", __func__);
607
608 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_URGENT_AUDIO);
609 set_sched_policy(0, SP_FOREGROUND);
610
611 pthread_mutex_lock(&ssrmod.ssr_process_lock);
612 while (!ssrmod.ssr_process_thread_stop) {
613 struct pcm_buffer_queue *out_buf;
614 struct pcm_buffer_queue *in_buf;
615
616 while ((!ssrmod.ssr_process_thread_stop) &&
617 ((ssrmod.out_buf_free == NULL) ||
618 (ssrmod.in_buf == NULL))) {
619 ALOGV("%s: waiting for buffers", __func__);
620 pthread_cond_wait(&ssrmod.cond_process, &ssrmod.ssr_process_lock);
621 }
622 if (ssrmod.ssr_process_thread_stop) {
623 break;
624 }
625 ALOGV("%s: got buffers", __func__);
626
627 out_buf = pcm_buffer_queue_pop(&ssrmod.out_buf_free);
628 in_buf = pcm_buffer_queue_pop(&ssrmod.in_buf);
629
630 pthread_mutex_unlock(&ssrmod.ssr_process_lock);
631
632 /* apply ssr libs to convert 4ch to 6ch */
633 if (ssrmod.ssr_3mic) {
634 ssrmod.surround_rec_process(ssrmod.surround_obj,
635 (int16_t *) in_buf->buffer.data,
636 (int16_t *) out_buf->buffer.data);
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700637 }
638
639 /* Run DRC if initialized */
640 if (ssrmod.drc_obj != NULL) {
641 ALOGV("%s: Running DRC", __func__);
642 ret = ssrmod.drc_process(ssrmod.drc_obj, out_buf->buffer.data, out_buf->buffer.data);
643 if (ret != 0) {
644 ALOGE("%s: drc_process returned %d", __func__, ret);
645 }
646 }
647
648 /*dump for raw pcm data*/
Naresh Tanniruc9093982015-10-16 18:05:29 +0530649 if (ssrmod.fp_input)
650 fwrite(in_buf->buffer.data, 1, in_buf->buffer.length, ssrmod.fp_input);
651 if (ssrmod.fp_output)
652 fwrite(out_buf->buffer.data, 1, out_buf->buffer.length, ssrmod.fp_output);
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700653
654 pthread_mutex_lock(&ssrmod.ssr_process_lock);
655
656 pcm_buffer_queue_push(&ssrmod.out_buf, out_buf);
657 pcm_buffer_queue_push(&ssrmod.in_buf_free, in_buf);
658
659 /* Read thread should go on without waiting for condition
660 * variable. If it has to wait (due to this thread not keeping
661 * up with the read requests), let this thread use the remainder
662 * of its buffers before waking up the read thread. */
663 if (ssrmod.in_buf == NULL) {
664 pthread_cond_signal(&ssrmod.cond_read);
665 }
666 }
667 pthread_mutex_unlock(&ssrmod.ssr_process_lock);
668
669 ALOGV("%s: exit", __func__);
670
671 pthread_exit(NULL);
672}
673
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700674int32_t audio_extn_ssr_read(struct audio_stream_in *stream,
675 void *buffer, size_t bytes)
676{
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700677 struct stream_in *in = (struct stream_in *)stream;
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700678 int32_t ret = 0;
679 struct pcm_buffer_queue *in_buf;
680 struct pcm_buffer_queue *out_buf;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700681
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700682 ALOGV("%s: entry", __func__);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700683
Mingming Yin49be8032013-12-19 12:51:25 -0800684 if (!ssrmod.surround_obj) {
685 ALOGE("%s: surround_obj not initialized", __func__);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700686 return -ENOMEM;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700687 }
Mingming Yin49be8032013-12-19 12:51:25 -0800688
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700689 ret = pcm_read(in->pcm, ssrmod.surround_raw_buffer, ssrmod.surround_raw_buffer_size);
Mingming Yin49be8032013-12-19 12:51:25 -0800690 if (ret < 0) {
691 ALOGE("%s: %s ret:%d", __func__, pcm_get_error(in->pcm),ret);
692 return ret;
693 }
694
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700695 pthread_mutex_lock(&ssrmod.ssr_process_lock);
Mingming Yin49be8032013-12-19 12:51:25 -0800696
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700697 if (!ssrmod.ssr_process_thread_started) {
698 pthread_mutex_unlock(&ssrmod.ssr_process_lock);
699 ALOGV("%s: ssr_process_thread not initialized", __func__);
700 return -EINVAL;
701 }
Mingming Yin49be8032013-12-19 12:51:25 -0800702
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700703 if ((ssrmod.in_buf_free == NULL) || (ssrmod.out_buf == NULL)) {
704 ALOGE("%s: waiting for buffers", __func__);
705 pthread_cond_wait(&ssrmod.cond_read, &ssrmod.ssr_process_lock);
706 if ((ssrmod.in_buf_free == NULL) || (ssrmod.out_buf == NULL)) {
707 pthread_mutex_unlock(&ssrmod.ssr_process_lock);
708 ALOGE("%s: failed to acquire buffers", __func__);
709 return -EINVAL;
710 }
711 }
712
713 in_buf = pcm_buffer_queue_pop(&ssrmod.in_buf_free);
714 out_buf = pcm_buffer_queue_pop(&ssrmod.out_buf);
715
716 memcpy(in_buf->buffer.data, ssrmod.surround_raw_buffer, in_buf->buffer.length);
717 pcm_buffer_queue_push(&ssrmod.in_buf, in_buf);
718
719 memcpy(buffer, out_buf->buffer.data, bytes);
720 pcm_buffer_queue_push(&ssrmod.out_buf_free, out_buf);
721
722 pthread_cond_signal(&ssrmod.cond_process);
723
724 pthread_mutex_unlock(&ssrmod.ssr_process_lock);
725
726 ALOGV("%s: exit", __func__);
Mingming Yin49be8032013-12-19 12:51:25 -0800727 return ret;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700728}
Mingming Yin49be8032013-12-19 12:51:25 -0800729
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700730void audio_extn_ssr_set_parameters(struct audio_device *adev __unused,
731 struct str_parms *parms)
732{
733 int err;
734 char value[4096] = {0};
735
736 //Do not update SSR mode during recording
737 if ( !ssrmod.surround_obj) {
738 int ret = 0;
739 ret = str_parms_get_str(parms, AUDIO_PARAMETER_SSRMODE_ON, value,
740 sizeof(value));
741 if (ret >= 0) {
742 if (strcmp(value, "true") == 0) {
743 ALOGD("Received SSR session request..setting SSR mode to true");
744 ssrmod.is_ssr_mode_on = true;
745 } else {
746 ALOGD("resetting SSR mode to false");
747 ssrmod.is_ssr_mode_on = false;
748 }
749 }
750 }
751 if (ssrmod.ssr_3mic && ssrmod.surround_obj) {
752 const set_param_data_t *set_params = ssrmod.surround_rec_get_set_param_data();
753 if (set_params != NULL) {
754 while (set_params->name != NULL && set_params->set_param_fn != NULL) {
755 err = str_parms_get_str(parms, set_params->name, value, sizeof(value));
756 if (err >= 0) {
757 ALOGV("Set %s to %s\n", set_params->name, value);
758 set_params->set_param_fn(ssrmod.surround_obj, value);
759 }
760 set_params++;
761 }
762 }
763 }
764}
765
766void audio_extn_ssr_get_parameters(const struct audio_device *adev __unused,
767 struct str_parms *parms,
768 struct str_parms *reply)
769{
770 int err;
771 char value[4096] = {0};
772
773 if (ssrmod.ssr_3mic && ssrmod.surround_obj) {
774 const get_param_data_t *get_params = ssrmod.surround_rec_get_get_param_data();
775 int get_all = 0;
776 err = str_parms_get_str(parms, "ssr.all", value, sizeof(value));
777 if (err >= 0) {
778 get_all = 1;
779 }
780 if (get_params != NULL) {
781 while (get_params->name != NULL && get_params->get_param_fn != NULL) {
782 err = str_parms_get_str(parms, get_params->name, value, sizeof(value));
783 if (get_all || (err >= 0)) {
784 ALOGV("Getting parameter %s", get_params->name);
785 char *val = get_params->get_param_fn(ssrmod.surround_obj);
786 if (val != NULL) {
787 str_parms_add_str(reply, get_params->name, val);
788 free(val);
789 }
790 }
791 get_params++;
792 }
793 }
794 }
795}
796
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700797#endif /* SSR_ENABLED */