blob: cf393c5cb9f627daac6b4eb78c6cfe001492e4b8 [file] [log] [blame]
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -07001/*
Naresh Tanniruc9093982015-10-16 18:05:29 +05302 * Copyright (c) 2013-2015, 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
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700158/* Use AAC/DTS channel mapping as default channel mapping: C,FL,FR,Ls,Rs,LFE */
159static const int chan_map[] = { 1, 2, 4, 3, 0, 5};
160
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700161
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700162static int32_t drc_init_lib(int num_chan, int sample_rate __unused)
163{
164 int ret = 0;
165 const char *cfgFileName = "";
166
167 if (ssrmod.drc_obj) {
168 ALOGE("%s: DRC library is already initialized", __func__);
169 return 0;
170 }
171
172 ssrmod.drc_handle = dlopen(LIB_DRC, RTLD_NOW);
173 if (ssrmod.drc_handle == NULL) {
174 ALOGE("%s: DLOPEN failed for %s", __func__, LIB_DRC);
175 ret = -ENOSYS;
176 goto init_fail;
177 }
178
179 ALOGV("%s: DLOPEN successful for %s", __func__, LIB_DRC);
180 ssrmod.drc_init = (drc_init_t)
181 dlsym(ssrmod.drc_handle, "DRC_init");
182 ssrmod.drc_deinit = (drc_deinit_t)
183 dlsym(ssrmod.drc_handle, "DRC_deinit");
184 ssrmod.drc_process = (drc_process_t)
185 dlsym(ssrmod.drc_handle, "DRC_process");
186
187 if (!ssrmod.drc_init ||
188 !ssrmod.drc_deinit ||
189 !ssrmod.drc_process){
190 ALOGW("%s: Could not find one of the symbols from %s",
191 __func__, LIB_DRC);
192 ret = -ENOSYS;
193 goto init_fail;
194 }
195
196 /* TO DO: different config files for different sample rates */
197 if (num_chan == 6) {
198 cfgFileName = "/system/etc/drc/drc_cfg_5.1.txt";
199 } else if (num_chan == 2) {
200 cfgFileName = "/system/etc/drc/drc_cfg_AZ.txt";
201 }
202
203 ALOGV("%s: Calling drc_init: num ch: %d, period: %d, cfg file: %s", __func__, num_chan, SSR_PERIOD_SIZE, cfgFileName);
204 ret = ssrmod.drc_init(&ssrmod.drc_obj, num_chan, SSR_PERIOD_SIZE, cfgFileName);
205 if (ret) {
206 ALOGE("drc_init failed with ret:%d",ret);
207 ret = -EINVAL;
208 goto init_fail;
209 }
210
211 return 0;
212
213init_fail:
214 if (ssrmod.drc_obj) {
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700215 ssrmod.drc_obj = NULL;
216 }
217 if(ssrmod.drc_handle) {
218 dlclose(ssrmod.drc_handle);
219 ssrmod.drc_handle = NULL;
220 }
221 return ret;
222}
223
224static int32_t ssr_init_surround_sound_3mic_lib(unsigned long buffersize, int num_in_chan, int num_out_chan, int sample_rate)
225{
226 int ret = 0;
227 const char *cfgFileName = NULL;
228
229 if ( ssrmod.surround_obj ) {
230 ALOGE("%s: surround sound library is already initialized", __func__);
231 return 0;
232 }
233
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700234 ssrmod.surround_rec_handle = dlopen(LIB_SURROUND_3MIC_PROC, RTLD_NOW);
235 if (ssrmod.surround_rec_handle == NULL) {
236 ALOGE("%s: DLOPEN failed for %s", __func__, LIB_SURROUND_3MIC_PROC);
Naresh Tanniruc9093982015-10-16 18:05:29 +0530237 goto init_fail;
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700238 } else {
239 ALOGV("%s: DLOPEN successful for %s", __func__, LIB_SURROUND_3MIC_PROC);
240 ssrmod.surround_rec_get_get_param_data = (surround_rec_get_get_param_data_t)
241 dlsym(ssrmod.surround_rec_handle, "surround_rec_get_get_param_data");
242
243 ssrmod.surround_rec_get_set_param_data = (surround_rec_get_set_param_data_t)
244 dlsym(ssrmod.surround_rec_handle, "surround_rec_get_set_param_data");
245 ssrmod.surround_rec_init = (surround_rec_init_t)
246 dlsym(ssrmod.surround_rec_handle, "surround_rec_init");
247 ssrmod.surround_rec_deinit = (surround_rec_deinit_t)
248 dlsym(ssrmod.surround_rec_handle, "surround_rec_deinit");
249 ssrmod.surround_rec_process = (surround_rec_process_t)
250 dlsym(ssrmod.surround_rec_handle, "surround_rec_process");
251
252 if (!ssrmod.surround_rec_get_get_param_data ||
253 !ssrmod.surround_rec_get_set_param_data ||
254 !ssrmod.surround_rec_init ||
255 !ssrmod.surround_rec_deinit ||
256 !ssrmod.surround_rec_process){
257 ALOGW("%s: Could not find the one of the symbols from %s",
258 __func__, LIB_SURROUND_3MIC_PROC);
259 ret = -ENOSYS;
260 goto init_fail;
261 }
262 }
263
Naresh Tanniruc9093982015-10-16 18:05:29 +0530264 /* Allocate memory for input buffer */
265 ssrmod.surround_raw_buffer = (Word16 *) calloc(buffersize,
266 sizeof(Word16));
267 if (!ssrmod.surround_raw_buffer) {
268 ALOGE("%s: Memory allocation failure. Not able to allocate "
269 "memory for surroundInputBuffer", __func__);
270 ret = -ENOMEM;
271 goto init_fail;
272 }
273
274 ssrmod.surround_raw_buffer_size = buffersize;
275
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700276 ssrmod.num_out_chan = num_out_chan;
277
278 if (num_out_chan == 6) {
279 cfgFileName = "/system/etc/surround_sound_3mic/surround_sound_rec_5.1.cfg";
280 } else if (num_out_chan == 2) {
281 cfgFileName = "/system/etc/surround_sound_3mic/surround_sound_rec_AZ.cfg";
282 } else {
283 ALOGE("%s: No cfg file for num_out_chan: %d", __func__, num_out_chan);
284 }
285
286 ALOGV("%s: Calling surround_rec_init: in ch: %d, out ch: %d, period: %d, sample rate: %d, cfg file: %s",
287 __func__, num_in_chan, num_out_chan, SSR_PERIOD_SIZE, sample_rate, cfgFileName);
288 ret = ssrmod.surround_rec_init(&ssrmod.surround_obj,
289 num_in_chan, num_out_chan, SSR_PERIOD_SIZE, sample_rate, cfgFileName);
290 if (ret) {
291 ALOGE("surround_rec_init failed with ret:%d",ret);
292 ret = -EINVAL;
293 goto init_fail;
294 }
295
296 return 0;
297
298init_fail:
299 if (ssrmod.surround_obj) {
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700300 ssrmod.surround_obj = NULL;
301 }
302 if (ssrmod.surround_raw_buffer) {
303 free(ssrmod.surround_raw_buffer);
304 ssrmod.surround_raw_buffer = NULL;
305 ssrmod.surround_raw_buffer_size = 0;
306 }
307 if(ssrmod.surround_rec_handle) {
308 dlclose(ssrmod.surround_rec_handle);
309 ssrmod.surround_rec_handle = NULL;
310 }
311 return ret;
312}
313
Mingming Yin49be8032013-12-19 12:51:25 -0800314void audio_extn_ssr_update_enabled()
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700315{
316 char ssr_enabled[PROPERTY_VALUE_MAX] = "false";
317
318 property_get("ro.qc.sdk.audio.ssr",ssr_enabled,"0");
319 if (!strncmp("true", ssr_enabled, 4)) {
320 ALOGD("%s: surround sound recording is supported", __func__);
321 ssrmod.is_ssr_enabled = true;
322 } else {
323 ALOGD("%s: surround sound recording is not supported", __func__);
324 ssrmod.is_ssr_enabled = false;
325 }
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700326}
327
328bool audio_extn_ssr_get_enabled()
329{
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700330 ALOGV("%s: is_ssr_enabled:%d is_ssr_mode_on:%d ", __func__, ssrmod.is_ssr_enabled, ssrmod.is_ssr_mode_on);
331
332 if(ssrmod.is_ssr_enabled && ssrmod.is_ssr_mode_on)
333 return true;
334
335 return false;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700336}
337
Naresh Tanniruc9093982015-10-16 18:05:29 +0530338int audio_extn_ssr_check_and_set_usecase(struct stream_in *in)
339{
340 int ret = -1;
341 int channel_count = audio_channel_count_from_in_mask(in->channel_mask);
342 audio_devices_t devices = in->device;
343 audio_source_t source = in->source;
344
345 /* validate input params
346 * only stereo and 5:1 channel config is supported
347 * only AUDIO_DEVICE_IN_BUILTIN_MIC, AUDIO_DEVICE_IN_BACK_MIC supports 3 mics */
348 if (audio_extn_ssr_get_enabled() &&
349 ((channel_count == 2) || (channel_count == 6)) &&
350 ((AUDIO_SOURCE_MIC == source) || (AUDIO_SOURCE_CAMCORDER == source)) &&
351 ((AUDIO_DEVICE_IN_BUILTIN_MIC == devices) || (AUDIO_DEVICE_IN_BACK_MIC == devices))) {
352
353 ALOGD("%s: Found SSR use case starting SSR lib with channel_count :%d",
354 __func__, channel_count);
355
356 if (!audio_extn_ssr_init(in, channel_count)) {
357 ALOGD("%s: Created SSR session succesfully", __func__);
358 ret = 0;
359 } else {
360 ALOGE("%s: Unable to start SSR record session", __func__);
361 }
362 }
363 return ret;
364}
365
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700366static void pcm_buffer_queue_push(struct pcm_buffer_queue **queue,
367 struct pcm_buffer_queue *node)
368{
369 struct pcm_buffer_queue *iter;
370
371 node->next = NULL;
372 if ((*queue) == NULL) {
373 *queue = node;
374 } else {
375 iter = *queue;
376 while (iter->next) {
377 iter = iter->next;
378 }
379 iter->next = node;
380 }
381}
382
383static struct pcm_buffer_queue *pcm_buffer_queue_pop(struct pcm_buffer_queue **queue)
384{
385 struct pcm_buffer_queue *node = (*queue);
386 if (node != NULL) {
387 *queue = node->next;
388 node->next = NULL;
389 }
390 return node;
391}
392
393static void deinit_ssr_process_thread()
394{
395 pthread_mutex_lock(&ssrmod.ssr_process_lock);
396 ssrmod.ssr_process_thread_stop = 1;
Naresh Tanniruc9093982015-10-16 18:05:29 +0530397
398 if(ssrmod.in_buf_data != NULL)
399 free(ssrmod.in_buf_data);
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700400 ssrmod.in_buf_data = NULL;
401 ssrmod.in_buf = NULL;
402 ssrmod.in_buf_free = NULL;
Naresh Tanniruc9093982015-10-16 18:05:29 +0530403
404 if(ssrmod.out_buf_data != NULL)
405 free(ssrmod.out_buf_data);
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700406 ssrmod.out_buf_data = NULL;
407 ssrmod.out_buf = NULL;
408 ssrmod.out_buf_free = NULL;
409 pthread_cond_broadcast(&ssrmod.cond_process);
410 pthread_cond_broadcast(&ssrmod.cond_read);
411 pthread_mutex_unlock(&ssrmod.ssr_process_lock);
412 if (ssrmod.ssr_process_thread_started) {
413 pthread_join(ssrmod.ssr_process_thread, (void **)NULL);
414 ssrmod.ssr_process_thread_started = 0;
415 }
416}
417
418struct stream_in *audio_extn_ssr_get_stream()
419{
420 return ssrmod.in;
421}
422
423int32_t audio_extn_ssr_init(struct stream_in *in, int num_out_chan)
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700424{
Naresh Tanniruc9093982015-10-16 18:05:29 +0530425 uint32_t ret = -1;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700426 char c_multi_ch_dump[128] = {0};
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700427 char c_ssr_3mic[128] = {0};
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700428 uint32_t buffer_size;
429
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700430 ALOGD("%s: ssr case, sample rate %d", __func__, in->config.rate);
431
432 if (ssrmod.surround_obj != NULL) {
433 ALOGV("%s: reinitializing surround sound library", __func__);
434 audio_extn_ssr_deinit();
435 }
436
Naresh Tanniruc9093982015-10-16 18:05:29 +0530437 if (audio_extn_ssr_get_enabled()) {
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700438 ssrmod.ssr_3mic = 1;
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700439 } else {
Naresh Tanniruc9093982015-10-16 18:05:29 +0530440 ALOGE(" Rejecting SSR -- init is called without enabling SSR");
441 goto fail;
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700442 }
443
Naresh Tanniruc9093982015-10-16 18:05:29 +0530444 /* buffer size equals to period_size * period_count */
445 buffer_size = SSR_PERIOD_SIZE * NUM_IN_CHANNELS * sizeof(int16_t);
Mingming Yin49be8032013-12-19 12:51:25 -0800446 ALOGV("%s: buffer_size: %d", __func__, buffer_size);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700447
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700448 if (ssrmod.ssr_3mic != 0) {
Naresh Tanniruc9093982015-10-16 18:05:29 +0530449 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 -0700450 if (0 != ret) {
451 ALOGE("%s: ssr_init_surround_sound_3mic_lib failed: %d "
452 "buffer_size:%d", __func__, ret, buffer_size);
453 goto fail;
454 }
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700455 }
456
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700457 /* Initialize DRC if available */
458 ret = drc_init_lib(num_out_chan, in->config.rate);
459 if (0 != ret) {
460 ALOGE("%s: drc_init_lib failed, ret %d", __func__, ret);
461 }
462
463 pthread_mutex_lock(&ssrmod.ssr_process_lock);
464 if (!ssrmod.ssr_process_thread_started) {
465 int i;
466 int output_buf_size = SSR_PERIOD_SIZE * sizeof(int16_t) * num_out_chan;
467
468 ssrmod.in_buf_data = (void *)calloc(buffer_size, NUM_IN_BUFS);
469 if (ssrmod.in_buf_data == NULL) {
470 ALOGE("%s: failed to allocate input buffer", __func__);
471 pthread_mutex_unlock(&ssrmod.ssr_process_lock);
472 ret = -ENOMEM;
473 goto fail;
474 }
475 ssrmod.out_buf_data = (void *)calloc(output_buf_size, NUM_OUT_BUFS);
476 if (ssrmod.out_buf_data == NULL) {
477 ALOGE("%s: failed to allocate output buffer", __func__);
478 pthread_mutex_unlock(&ssrmod.ssr_process_lock);
479 ret = -ENOMEM;
480 // ssrmod.in_buf_data will be freed in deinit_ssr_process_thread()
481 goto fail;
482 }
483
484 ssrmod.in_buf = NULL;
485 ssrmod.in_buf_free = NULL;
486 ssrmod.out_buf = NULL;
487 ssrmod.out_buf_free = NULL;
488
489 for (i=0; i < NUM_IN_BUFS; i++) {
490 struct pcm_buffer_queue *buf = &ssrmod.in_buf_nodes[i];
491 buf->buffer.data = &(((char *)ssrmod.in_buf_data)[i*buffer_size]);
492 buf->buffer.length = buffer_size;
493 pcm_buffer_queue_push(&ssrmod.in_buf_free, buf);
494 }
495
496 for (i=0; i < NUM_OUT_BUFS; i++) {
497 struct pcm_buffer_queue *buf = &ssrmod.out_buf_nodes[i];
498 buf->buffer.data = &(((char *)ssrmod.out_buf_data)[i*output_buf_size]);
499 buf->buffer.length = output_buf_size;
500 pcm_buffer_queue_push(&ssrmod.out_buf, buf);
501 }
502
503 ssrmod.ssr_process_thread_stop = 0;
504 ALOGV("%s: creating thread", __func__);
505 ret = pthread_create(&ssrmod.ssr_process_thread,
506 (const pthread_attr_t *) NULL,
507 ssr_process_thread, NULL);
508 if (ret != 0) {
509 ALOGE("%s: failed to create thread for surround sound recording.",
510 __func__);
511 pthread_mutex_unlock(&ssrmod.ssr_process_lock);
512 goto fail;
513 }
514
515 ssrmod.ssr_process_thread_started = 1;
516 ALOGV("%s: done creating thread", __func__);
517 }
Naresh Tanniruc9093982015-10-16 18:05:29 +0530518
519 in->config.channels = NUM_IN_CHANNELS;
520 in->config.period_size = SSR_PERIOD_SIZE;
521 in->config.period_count = in->config.channels * sizeof(int16_t);
522
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700523 pthread_mutex_unlock(&ssrmod.ssr_process_lock);
524
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700525 property_get("ssr.pcmdump",c_multi_ch_dump,"0");
Mingming Yin49be8032013-12-19 12:51:25 -0800526 if (0 == strncmp("true", c_multi_ch_dump, sizeof("ssr.dump-pcm"))) {
527 /* Remember to change file system permission of data(e.g. chmod 777 data/),
528 otherwise, fopen may fail */
Naresh Tanniruc9093982015-10-16 18:05:29 +0530529 if ( !ssrmod.fp_input) {
530 ALOGD("%s: Opening ssr input dump file \n", __func__);
531 ssrmod.fp_input = fopen("/data/misc/audio/ssr_input_3ch.pcm", "wb");
532 }
533
534 if ( !ssrmod.fp_output) {
535 if(ssrmod.num_out_chan == 6) {
536 ALOGD("%s: Opening ssr input dump file for 6 channel\n", __func__);
537 ssrmod.fp_output = fopen("/data/misc/audio/ssr_output_6ch.pcm", "wb");
538 } else {
539 ALOGD("%s: Opening ssr input dump file for 2 channel\n", __func__);
540 ssrmod.fp_output = fopen("/data/misc/audio/ssr_output_2ch.pcm", "wb");
541 }
542 }
543
544 if ((!ssrmod.fp_input) || (!ssrmod.fp_output)) {
545 ALOGE("%s: input dump or ouput dump open failed: mfp_4ch:%p mfp_6ch:%p",
546 __func__, ssrmod.fp_input, ssrmod.fp_output);
547 }
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700548 }
549
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700550 ssrmod.in = in;
551
552 ALOGV("%s: exit", __func__);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700553 return 0;
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700554
555fail:
556 (void) audio_extn_ssr_deinit();
557 return ret;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700558}
559
560int32_t audio_extn_ssr_deinit()
561{
562 int i;
563
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700564 ALOGV("%s: entry", __func__);
565 deinit_ssr_process_thread();
566
567 if (ssrmod.drc_obj) {
568 ssrmod.drc_deinit(ssrmod.drc_obj);
569 ssrmod.drc_obj = NULL;
570 }
571
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700572 if (ssrmod.surround_obj) {
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700573
574 if (ssrmod.ssr_3mic) {
575 ssrmod.surround_rec_deinit(ssrmod.surround_obj);
576 ssrmod.surround_obj = NULL;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700577 }
Mingming Yin49be8032013-12-19 12:51:25 -0800578 if (ssrmod.surround_raw_buffer) {
579 free(ssrmod.surround_raw_buffer);
580 ssrmod.surround_raw_buffer = NULL;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700581 }
Naresh Tanniruc9093982015-10-16 18:05:29 +0530582 if (ssrmod.fp_input)
583 fclose(ssrmod.fp_input);
584 if (ssrmod.fp_output)
585 fclose(ssrmod.fp_output);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700586 }
587
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700588 if(ssrmod.drc_handle) {
589 dlclose(ssrmod.drc_handle);
590 ssrmod.drc_handle = NULL;
591 }
592
593 if(ssrmod.surround_rec_handle) {
594 dlclose(ssrmod.surround_rec_handle);
595 ssrmod.surround_rec_handle = NULL;
596 }
597
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700598 ssrmod.in = NULL;
Naresh Tanniruc9093982015-10-16 18:05:29 +0530599 //SSR session can be closed due to device switch
600 //Do not force reset ssr mode
601
602 //ssrmod.is_ssr_mode_on = false;
Mingming Yin49be8032013-12-19 12:51:25 -0800603 ALOGV("%s: exit", __func__);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700604
605 return 0;
606}
607
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700608static void *ssr_process_thread(void *context __unused)
609{
610 int32_t ret;
611
612 ALOGV("%s: enter", __func__);
613
614 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_URGENT_AUDIO);
615 set_sched_policy(0, SP_FOREGROUND);
616
617 pthread_mutex_lock(&ssrmod.ssr_process_lock);
618 while (!ssrmod.ssr_process_thread_stop) {
619 struct pcm_buffer_queue *out_buf;
620 struct pcm_buffer_queue *in_buf;
621
622 while ((!ssrmod.ssr_process_thread_stop) &&
623 ((ssrmod.out_buf_free == NULL) ||
624 (ssrmod.in_buf == NULL))) {
625 ALOGV("%s: waiting for buffers", __func__);
626 pthread_cond_wait(&ssrmod.cond_process, &ssrmod.ssr_process_lock);
627 }
628 if (ssrmod.ssr_process_thread_stop) {
629 break;
630 }
631 ALOGV("%s: got buffers", __func__);
632
633 out_buf = pcm_buffer_queue_pop(&ssrmod.out_buf_free);
634 in_buf = pcm_buffer_queue_pop(&ssrmod.in_buf);
635
636 pthread_mutex_unlock(&ssrmod.ssr_process_lock);
637
638 /* apply ssr libs to convert 4ch to 6ch */
639 if (ssrmod.ssr_3mic) {
640 ssrmod.surround_rec_process(ssrmod.surround_obj,
641 (int16_t *) in_buf->buffer.data,
642 (int16_t *) out_buf->buffer.data);
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700643 }
644
645 /* Run DRC if initialized */
646 if (ssrmod.drc_obj != NULL) {
647 ALOGV("%s: Running DRC", __func__);
648 ret = ssrmod.drc_process(ssrmod.drc_obj, out_buf->buffer.data, out_buf->buffer.data);
649 if (ret != 0) {
650 ALOGE("%s: drc_process returned %d", __func__, ret);
651 }
652 }
653
654 /*dump for raw pcm data*/
Naresh Tanniruc9093982015-10-16 18:05:29 +0530655 if (ssrmod.fp_input)
656 fwrite(in_buf->buffer.data, 1, in_buf->buffer.length, ssrmod.fp_input);
657 if (ssrmod.fp_output)
658 fwrite(out_buf->buffer.data, 1, out_buf->buffer.length, ssrmod.fp_output);
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700659
660 pthread_mutex_lock(&ssrmod.ssr_process_lock);
661
662 pcm_buffer_queue_push(&ssrmod.out_buf, out_buf);
663 pcm_buffer_queue_push(&ssrmod.in_buf_free, in_buf);
664
665 /* Read thread should go on without waiting for condition
666 * variable. If it has to wait (due to this thread not keeping
667 * up with the read requests), let this thread use the remainder
668 * of its buffers before waking up the read thread. */
669 if (ssrmod.in_buf == NULL) {
670 pthread_cond_signal(&ssrmod.cond_read);
671 }
672 }
673 pthread_mutex_unlock(&ssrmod.ssr_process_lock);
674
675 ALOGV("%s: exit", __func__);
676
677 pthread_exit(NULL);
678}
679
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700680int32_t audio_extn_ssr_read(struct audio_stream_in *stream,
681 void *buffer, size_t bytes)
682{
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700683 struct stream_in *in = (struct stream_in *)stream;
684 struct audio_device *adev = in->dev;
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700685 int32_t ret = 0;
686 struct pcm_buffer_queue *in_buf;
687 struct pcm_buffer_queue *out_buf;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700688
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700689 ALOGV("%s: entry", __func__);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700690
Mingming Yin49be8032013-12-19 12:51:25 -0800691 if (!ssrmod.surround_obj) {
692 ALOGE("%s: surround_obj not initialized", __func__);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700693 return -ENOMEM;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700694 }
Mingming Yin49be8032013-12-19 12:51:25 -0800695
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700696 ret = pcm_read(in->pcm, ssrmod.surround_raw_buffer, ssrmod.surround_raw_buffer_size);
Mingming Yin49be8032013-12-19 12:51:25 -0800697 if (ret < 0) {
698 ALOGE("%s: %s ret:%d", __func__, pcm_get_error(in->pcm),ret);
699 return ret;
700 }
701
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700702 pthread_mutex_lock(&ssrmod.ssr_process_lock);
Mingming Yin49be8032013-12-19 12:51:25 -0800703
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700704 if (!ssrmod.ssr_process_thread_started) {
705 pthread_mutex_unlock(&ssrmod.ssr_process_lock);
706 ALOGV("%s: ssr_process_thread not initialized", __func__);
707 return -EINVAL;
708 }
Mingming Yin49be8032013-12-19 12:51:25 -0800709
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700710 if ((ssrmod.in_buf_free == NULL) || (ssrmod.out_buf == NULL)) {
711 ALOGE("%s: waiting for buffers", __func__);
712 pthread_cond_wait(&ssrmod.cond_read, &ssrmod.ssr_process_lock);
713 if ((ssrmod.in_buf_free == NULL) || (ssrmod.out_buf == NULL)) {
714 pthread_mutex_unlock(&ssrmod.ssr_process_lock);
715 ALOGE("%s: failed to acquire buffers", __func__);
716 return -EINVAL;
717 }
718 }
719
720 in_buf = pcm_buffer_queue_pop(&ssrmod.in_buf_free);
721 out_buf = pcm_buffer_queue_pop(&ssrmod.out_buf);
722
723 memcpy(in_buf->buffer.data, ssrmod.surround_raw_buffer, in_buf->buffer.length);
724 pcm_buffer_queue_push(&ssrmod.in_buf, in_buf);
725
726 memcpy(buffer, out_buf->buffer.data, bytes);
727 pcm_buffer_queue_push(&ssrmod.out_buf_free, out_buf);
728
729 pthread_cond_signal(&ssrmod.cond_process);
730
731 pthread_mutex_unlock(&ssrmod.ssr_process_lock);
732
733 ALOGV("%s: exit", __func__);
Mingming Yin49be8032013-12-19 12:51:25 -0800734 return ret;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700735}
Mingming Yin49be8032013-12-19 12:51:25 -0800736
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700737void audio_extn_ssr_set_parameters(struct audio_device *adev __unused,
738 struct str_parms *parms)
739{
740 int err;
741 char value[4096] = {0};
742
743 //Do not update SSR mode during recording
744 if ( !ssrmod.surround_obj) {
745 int ret = 0;
746 ret = str_parms_get_str(parms, AUDIO_PARAMETER_SSRMODE_ON, value,
747 sizeof(value));
748 if (ret >= 0) {
749 if (strcmp(value, "true") == 0) {
750 ALOGD("Received SSR session request..setting SSR mode to true");
751 ssrmod.is_ssr_mode_on = true;
752 } else {
753 ALOGD("resetting SSR mode to false");
754 ssrmod.is_ssr_mode_on = false;
755 }
756 }
757 }
758 if (ssrmod.ssr_3mic && ssrmod.surround_obj) {
759 const set_param_data_t *set_params = ssrmod.surround_rec_get_set_param_data();
760 if (set_params != NULL) {
761 while (set_params->name != NULL && set_params->set_param_fn != NULL) {
762 err = str_parms_get_str(parms, set_params->name, value, sizeof(value));
763 if (err >= 0) {
764 ALOGV("Set %s to %s\n", set_params->name, value);
765 set_params->set_param_fn(ssrmod.surround_obj, value);
766 }
767 set_params++;
768 }
769 }
770 }
771}
772
773void audio_extn_ssr_get_parameters(const struct audio_device *adev __unused,
774 struct str_parms *parms,
775 struct str_parms *reply)
776{
777 int err;
778 char value[4096] = {0};
779
780 if (ssrmod.ssr_3mic && ssrmod.surround_obj) {
781 const get_param_data_t *get_params = ssrmod.surround_rec_get_get_param_data();
782 int get_all = 0;
783 err = str_parms_get_str(parms, "ssr.all", value, sizeof(value));
784 if (err >= 0) {
785 get_all = 1;
786 }
787 if (get_params != NULL) {
788 while (get_params->name != NULL && get_params->get_param_fn != NULL) {
789 err = str_parms_get_str(parms, get_params->name, value, sizeof(value));
790 if (get_all || (err >= 0)) {
791 ALOGV("Getting parameter %s", get_params->name);
792 char *val = get_params->get_param_fn(ssrmod.surround_obj);
793 if (val != NULL) {
794 str_parms_add_str(reply, get_params->name, val);
795 free(val);
796 }
797 }
798 get_params++;
799 }
800 }
801 }
802}
803
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700804#endif /* SSR_ENABLED */