blob: 51a6a268bda2950305dcf5cacaacb24b106b27fe [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
Divya Narayanan Poojary45f19192016-09-30 18:52:13 +0530334bool audio_extn_ssr_check_usecase(struct stream_in *in) {
335 int ret = false;
Naresh Tanniruc9093982015-10-16 18:05:29 +0530336 int channel_count = audio_channel_count_from_in_mask(in->channel_mask);
337 audio_devices_t devices = in->device;
338 audio_source_t source = in->source;
339
Divya Narayanan Poojary45f19192016-09-30 18:52:13 +0530340 if ((audio_extn_ssr_get_enabled()) &&
341 ((channel_count == 2) || (channel_count == 6)) &&
342 ((AUDIO_SOURCE_MIC == source) || (AUDIO_SOURCE_CAMCORDER == source)) &&
343 ((AUDIO_DEVICE_IN_BUILTIN_MIC == devices) || (AUDIO_DEVICE_IN_BACK_MIC == devices)) &&
344 (in->format == AUDIO_FORMAT_PCM_16_BIT)) {
345 ALOGD("%s: SSR enabled with channel_count :%d",
Naresh Tanniruc9093982015-10-16 18:05:29 +0530346 __func__, channel_count);
Divya Narayanan Poojary45f19192016-09-30 18:52:13 +0530347 ret = true;
348 }
349 return ret;
350}
Naresh Tanniruc9093982015-10-16 18:05:29 +0530351
Divya Narayanan Poojary45f19192016-09-30 18:52:13 +0530352int audio_extn_ssr_set_usecase(struct stream_in *in,
353 struct audio_config *config,
354 bool *update_params)
355{
356 int ret = -EINVAL;
357 int channel_count = audio_channel_count_from_in_mask(in->channel_mask);
358 audio_channel_representation_t representation =
359 audio_channel_mask_get_representation(in->channel_mask);
360 *update_params = false;
361
362 if (audio_extn_ssr_check_usecase(in)) {
363
364 if (representation == AUDIO_CHANNEL_REPRESENTATION_INDEX) {
365 /* update params in case channel representation index.
366 * on returning error, flinger will retry with supported representation passed
367 */
368 ALOGD("%s: SSR supports only channel representation position, channel_mask(%#x)"
369 ,__func__, config->channel_mask);
370 config->channel_mask = AUDIO_CHANNEL_IN_5POINT1;
Naresh Tanniruc9093982015-10-16 18:05:29 +0530371 ret = 0;
Divya Narayanan Poojary45f19192016-09-30 18:52:13 +0530372 *update_params = true;
Naresh Tanniruc9093982015-10-16 18:05:29 +0530373 } else {
Divya Narayanan Poojary45f19192016-09-30 18:52:13 +0530374 if (!audio_extn_ssr_init(in, channel_count)) {
375 ALOGD("%s: Created SSR session succesfully", __func__);
376 ret = 0;
377 } else {
378 ALOGE("%s: Unable to start SSR record session", __func__);
379 }
Naresh Tanniruc9093982015-10-16 18:05:29 +0530380 }
Divya Narayanan Poojary45f19192016-09-30 18:52:13 +0530381 }
382 return ret;
Naresh Tanniruc9093982015-10-16 18:05:29 +0530383}
384
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700385static void pcm_buffer_queue_push(struct pcm_buffer_queue **queue,
386 struct pcm_buffer_queue *node)
387{
388 struct pcm_buffer_queue *iter;
389
390 node->next = NULL;
391 if ((*queue) == NULL) {
392 *queue = node;
393 } else {
394 iter = *queue;
395 while (iter->next) {
396 iter = iter->next;
397 }
398 iter->next = node;
399 }
400}
401
402static struct pcm_buffer_queue *pcm_buffer_queue_pop(struct pcm_buffer_queue **queue)
403{
404 struct pcm_buffer_queue *node = (*queue);
405 if (node != NULL) {
406 *queue = node->next;
407 node->next = NULL;
408 }
409 return node;
410}
411
412static void deinit_ssr_process_thread()
413{
414 pthread_mutex_lock(&ssrmod.ssr_process_lock);
415 ssrmod.ssr_process_thread_stop = 1;
Naresh Tanniruc9093982015-10-16 18:05:29 +0530416
417 if(ssrmod.in_buf_data != NULL)
418 free(ssrmod.in_buf_data);
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700419 ssrmod.in_buf_data = NULL;
420 ssrmod.in_buf = NULL;
421 ssrmod.in_buf_free = NULL;
Naresh Tanniruc9093982015-10-16 18:05:29 +0530422
423 if(ssrmod.out_buf_data != NULL)
424 free(ssrmod.out_buf_data);
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700425 ssrmod.out_buf_data = NULL;
426 ssrmod.out_buf = NULL;
427 ssrmod.out_buf_free = NULL;
428 pthread_cond_broadcast(&ssrmod.cond_process);
429 pthread_cond_broadcast(&ssrmod.cond_read);
430 pthread_mutex_unlock(&ssrmod.ssr_process_lock);
431 if (ssrmod.ssr_process_thread_started) {
432 pthread_join(ssrmod.ssr_process_thread, (void **)NULL);
433 ssrmod.ssr_process_thread_started = 0;
434 }
435}
436
437struct stream_in *audio_extn_ssr_get_stream()
438{
439 return ssrmod.in;
440}
441
442int32_t audio_extn_ssr_init(struct stream_in *in, int num_out_chan)
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700443{
Naresh Tanniruc9093982015-10-16 18:05:29 +0530444 uint32_t ret = -1;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700445 char c_multi_ch_dump[128] = {0};
446 uint32_t buffer_size;
447
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700448 ALOGD("%s: ssr case, sample rate %d", __func__, in->config.rate);
449
450 if (ssrmod.surround_obj != NULL) {
451 ALOGV("%s: reinitializing surround sound library", __func__);
452 audio_extn_ssr_deinit();
453 }
454
Naresh Tanniruc9093982015-10-16 18:05:29 +0530455 if (audio_extn_ssr_get_enabled()) {
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700456 ssrmod.ssr_3mic = 1;
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700457 } else {
Naresh Tanniruc9093982015-10-16 18:05:29 +0530458 ALOGE(" Rejecting SSR -- init is called without enabling SSR");
459 goto fail;
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700460 }
461
Naresh Tanniruc9093982015-10-16 18:05:29 +0530462 /* buffer size equals to period_size * period_count */
463 buffer_size = SSR_PERIOD_SIZE * NUM_IN_CHANNELS * sizeof(int16_t);
Mingming Yin49be8032013-12-19 12:51:25 -0800464 ALOGV("%s: buffer_size: %d", __func__, buffer_size);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700465
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700466 if (ssrmod.ssr_3mic != 0) {
Naresh Tanniruc9093982015-10-16 18:05:29 +0530467 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 -0700468 if (0 != ret) {
469 ALOGE("%s: ssr_init_surround_sound_3mic_lib failed: %d "
470 "buffer_size:%d", __func__, ret, buffer_size);
471 goto fail;
472 }
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700473 }
474
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700475 /* Initialize DRC if available */
476 ret = drc_init_lib(num_out_chan, in->config.rate);
477 if (0 != ret) {
478 ALOGE("%s: drc_init_lib failed, ret %d", __func__, ret);
479 }
480
481 pthread_mutex_lock(&ssrmod.ssr_process_lock);
482 if (!ssrmod.ssr_process_thread_started) {
483 int i;
484 int output_buf_size = SSR_PERIOD_SIZE * sizeof(int16_t) * num_out_chan;
485
486 ssrmod.in_buf_data = (void *)calloc(buffer_size, NUM_IN_BUFS);
487 if (ssrmod.in_buf_data == NULL) {
488 ALOGE("%s: failed to allocate input buffer", __func__);
489 pthread_mutex_unlock(&ssrmod.ssr_process_lock);
490 ret = -ENOMEM;
491 goto fail;
492 }
493 ssrmod.out_buf_data = (void *)calloc(output_buf_size, NUM_OUT_BUFS);
494 if (ssrmod.out_buf_data == NULL) {
495 ALOGE("%s: failed to allocate output buffer", __func__);
496 pthread_mutex_unlock(&ssrmod.ssr_process_lock);
497 ret = -ENOMEM;
498 // ssrmod.in_buf_data will be freed in deinit_ssr_process_thread()
499 goto fail;
500 }
501
502 ssrmod.in_buf = NULL;
503 ssrmod.in_buf_free = NULL;
504 ssrmod.out_buf = NULL;
505 ssrmod.out_buf_free = NULL;
506
507 for (i=0; i < NUM_IN_BUFS; i++) {
508 struct pcm_buffer_queue *buf = &ssrmod.in_buf_nodes[i];
509 buf->buffer.data = &(((char *)ssrmod.in_buf_data)[i*buffer_size]);
510 buf->buffer.length = buffer_size;
511 pcm_buffer_queue_push(&ssrmod.in_buf_free, buf);
512 }
513
514 for (i=0; i < NUM_OUT_BUFS; i++) {
515 struct pcm_buffer_queue *buf = &ssrmod.out_buf_nodes[i];
516 buf->buffer.data = &(((char *)ssrmod.out_buf_data)[i*output_buf_size]);
517 buf->buffer.length = output_buf_size;
518 pcm_buffer_queue_push(&ssrmod.out_buf, buf);
519 }
520
521 ssrmod.ssr_process_thread_stop = 0;
522 ALOGV("%s: creating thread", __func__);
523 ret = pthread_create(&ssrmod.ssr_process_thread,
524 (const pthread_attr_t *) NULL,
525 ssr_process_thread, NULL);
526 if (ret != 0) {
527 ALOGE("%s: failed to create thread for surround sound recording.",
528 __func__);
529 pthread_mutex_unlock(&ssrmod.ssr_process_lock);
530 goto fail;
531 }
532
533 ssrmod.ssr_process_thread_started = 1;
534 ALOGV("%s: done creating thread", __func__);
535 }
Naresh Tanniruc9093982015-10-16 18:05:29 +0530536
537 in->config.channels = NUM_IN_CHANNELS;
538 in->config.period_size = SSR_PERIOD_SIZE;
539 in->config.period_count = in->config.channels * sizeof(int16_t);
540
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700541 pthread_mutex_unlock(&ssrmod.ssr_process_lock);
542
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700543 property_get("ssr.pcmdump",c_multi_ch_dump,"0");
Mingming Yin49be8032013-12-19 12:51:25 -0800544 if (0 == strncmp("true", c_multi_ch_dump, sizeof("ssr.dump-pcm"))) {
545 /* Remember to change file system permission of data(e.g. chmod 777 data/),
546 otherwise, fopen may fail */
Naresh Tanniruc9093982015-10-16 18:05:29 +0530547 if ( !ssrmod.fp_input) {
548 ALOGD("%s: Opening ssr input dump file \n", __func__);
549 ssrmod.fp_input = fopen("/data/misc/audio/ssr_input_3ch.pcm", "wb");
550 }
551
552 if ( !ssrmod.fp_output) {
553 if(ssrmod.num_out_chan == 6) {
554 ALOGD("%s: Opening ssr input dump file for 6 channel\n", __func__);
555 ssrmod.fp_output = fopen("/data/misc/audio/ssr_output_6ch.pcm", "wb");
556 } else {
557 ALOGD("%s: Opening ssr input dump file for 2 channel\n", __func__);
558 ssrmod.fp_output = fopen("/data/misc/audio/ssr_output_2ch.pcm", "wb");
559 }
560 }
561
562 if ((!ssrmod.fp_input) || (!ssrmod.fp_output)) {
563 ALOGE("%s: input dump or ouput dump open failed: mfp_4ch:%p mfp_6ch:%p",
564 __func__, ssrmod.fp_input, ssrmod.fp_output);
565 }
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700566 }
567
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700568 ssrmod.in = in;
569
570 ALOGV("%s: exit", __func__);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700571 return 0;
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700572
573fail:
574 (void) audio_extn_ssr_deinit();
575 return ret;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700576}
577
578int32_t audio_extn_ssr_deinit()
579{
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700580 ALOGV("%s: entry", __func__);
581 deinit_ssr_process_thread();
582
583 if (ssrmod.drc_obj) {
584 ssrmod.drc_deinit(ssrmod.drc_obj);
585 ssrmod.drc_obj = NULL;
586 }
587
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700588 if (ssrmod.surround_obj) {
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700589
590 if (ssrmod.ssr_3mic) {
591 ssrmod.surround_rec_deinit(ssrmod.surround_obj);
592 ssrmod.surround_obj = NULL;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700593 }
Mingming Yin49be8032013-12-19 12:51:25 -0800594 if (ssrmod.surround_raw_buffer) {
595 free(ssrmod.surround_raw_buffer);
596 ssrmod.surround_raw_buffer = NULL;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700597 }
Naresh Tanniruc9093982015-10-16 18:05:29 +0530598 if (ssrmod.fp_input)
599 fclose(ssrmod.fp_input);
600 if (ssrmod.fp_output)
601 fclose(ssrmod.fp_output);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700602 }
603
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700604 if(ssrmod.drc_handle) {
605 dlclose(ssrmod.drc_handle);
606 ssrmod.drc_handle = NULL;
607 }
608
609 if(ssrmod.surround_rec_handle) {
610 dlclose(ssrmod.surround_rec_handle);
611 ssrmod.surround_rec_handle = NULL;
612 }
613
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700614 ssrmod.in = NULL;
Naresh Tanniruc9093982015-10-16 18:05:29 +0530615 //SSR session can be closed due to device switch
616 //Do not force reset ssr mode
617
618 //ssrmod.is_ssr_mode_on = false;
Mingming Yin49be8032013-12-19 12:51:25 -0800619 ALOGV("%s: exit", __func__);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700620
621 return 0;
622}
623
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700624static void *ssr_process_thread(void *context __unused)
625{
626 int32_t ret;
627
628 ALOGV("%s: enter", __func__);
629
630 setpriority(PRIO_PROCESS, 0, ANDROID_PRIORITY_URGENT_AUDIO);
631 set_sched_policy(0, SP_FOREGROUND);
632
633 pthread_mutex_lock(&ssrmod.ssr_process_lock);
634 while (!ssrmod.ssr_process_thread_stop) {
635 struct pcm_buffer_queue *out_buf;
636 struct pcm_buffer_queue *in_buf;
637
638 while ((!ssrmod.ssr_process_thread_stop) &&
639 ((ssrmod.out_buf_free == NULL) ||
640 (ssrmod.in_buf == NULL))) {
641 ALOGV("%s: waiting for buffers", __func__);
642 pthread_cond_wait(&ssrmod.cond_process, &ssrmod.ssr_process_lock);
643 }
644 if (ssrmod.ssr_process_thread_stop) {
645 break;
646 }
647 ALOGV("%s: got buffers", __func__);
648
649 out_buf = pcm_buffer_queue_pop(&ssrmod.out_buf_free);
650 in_buf = pcm_buffer_queue_pop(&ssrmod.in_buf);
651
652 pthread_mutex_unlock(&ssrmod.ssr_process_lock);
653
654 /* apply ssr libs to convert 4ch to 6ch */
655 if (ssrmod.ssr_3mic) {
656 ssrmod.surround_rec_process(ssrmod.surround_obj,
657 (int16_t *) in_buf->buffer.data,
658 (int16_t *) out_buf->buffer.data);
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700659 }
660
661 /* Run DRC if initialized */
662 if (ssrmod.drc_obj != NULL) {
663 ALOGV("%s: Running DRC", __func__);
664 ret = ssrmod.drc_process(ssrmod.drc_obj, out_buf->buffer.data, out_buf->buffer.data);
665 if (ret != 0) {
666 ALOGE("%s: drc_process returned %d", __func__, ret);
667 }
668 }
669
670 /*dump for raw pcm data*/
Naresh Tanniruc9093982015-10-16 18:05:29 +0530671 if (ssrmod.fp_input)
672 fwrite(in_buf->buffer.data, 1, in_buf->buffer.length, ssrmod.fp_input);
673 if (ssrmod.fp_output)
674 fwrite(out_buf->buffer.data, 1, out_buf->buffer.length, ssrmod.fp_output);
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700675
676 pthread_mutex_lock(&ssrmod.ssr_process_lock);
677
678 pcm_buffer_queue_push(&ssrmod.out_buf, out_buf);
679 pcm_buffer_queue_push(&ssrmod.in_buf_free, in_buf);
680
681 /* Read thread should go on without waiting for condition
682 * variable. If it has to wait (due to this thread not keeping
683 * up with the read requests), let this thread use the remainder
684 * of its buffers before waking up the read thread. */
685 if (ssrmod.in_buf == NULL) {
686 pthread_cond_signal(&ssrmod.cond_read);
687 }
688 }
689 pthread_mutex_unlock(&ssrmod.ssr_process_lock);
690
691 ALOGV("%s: exit", __func__);
692
693 pthread_exit(NULL);
694}
695
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700696int32_t audio_extn_ssr_read(struct audio_stream_in *stream,
697 void *buffer, size_t bytes)
698{
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700699 struct stream_in *in = (struct stream_in *)stream;
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700700 int32_t ret = 0;
701 struct pcm_buffer_queue *in_buf;
702 struct pcm_buffer_queue *out_buf;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700703
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700704 ALOGV("%s: entry", __func__);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700705
Mingming Yin49be8032013-12-19 12:51:25 -0800706 if (!ssrmod.surround_obj) {
707 ALOGE("%s: surround_obj not initialized", __func__);
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700708 return -ENOMEM;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700709 }
Mingming Yin49be8032013-12-19 12:51:25 -0800710
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700711 ret = pcm_read(in->pcm, ssrmod.surround_raw_buffer, ssrmod.surround_raw_buffer_size);
Mingming Yin49be8032013-12-19 12:51:25 -0800712 if (ret < 0) {
713 ALOGE("%s: %s ret:%d", __func__, pcm_get_error(in->pcm),ret);
714 return ret;
715 }
716
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700717 pthread_mutex_lock(&ssrmod.ssr_process_lock);
Mingming Yin49be8032013-12-19 12:51:25 -0800718
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700719 if (!ssrmod.ssr_process_thread_started) {
720 pthread_mutex_unlock(&ssrmod.ssr_process_lock);
721 ALOGV("%s: ssr_process_thread not initialized", __func__);
722 return -EINVAL;
723 }
Mingming Yin49be8032013-12-19 12:51:25 -0800724
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700725 if ((ssrmod.in_buf_free == NULL) || (ssrmod.out_buf == NULL)) {
726 ALOGE("%s: waiting for buffers", __func__);
727 pthread_cond_wait(&ssrmod.cond_read, &ssrmod.ssr_process_lock);
728 if ((ssrmod.in_buf_free == NULL) || (ssrmod.out_buf == NULL)) {
729 pthread_mutex_unlock(&ssrmod.ssr_process_lock);
730 ALOGE("%s: failed to acquire buffers", __func__);
731 return -EINVAL;
732 }
733 }
734
735 in_buf = pcm_buffer_queue_pop(&ssrmod.in_buf_free);
736 out_buf = pcm_buffer_queue_pop(&ssrmod.out_buf);
737
738 memcpy(in_buf->buffer.data, ssrmod.surround_raw_buffer, in_buf->buffer.length);
739 pcm_buffer_queue_push(&ssrmod.in_buf, in_buf);
740
741 memcpy(buffer, out_buf->buffer.data, bytes);
742 pcm_buffer_queue_push(&ssrmod.out_buf_free, out_buf);
743
744 pthread_cond_signal(&ssrmod.cond_process);
745
746 pthread_mutex_unlock(&ssrmod.ssr_process_lock);
747
748 ALOGV("%s: exit", __func__);
Mingming Yin49be8032013-12-19 12:51:25 -0800749 return ret;
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700750}
Mingming Yin49be8032013-12-19 12:51:25 -0800751
Shiv Maliyappanahalli5a10aea2015-07-02 10:36:23 -0700752void audio_extn_ssr_set_parameters(struct audio_device *adev __unused,
753 struct str_parms *parms)
754{
755 int err;
756 char value[4096] = {0};
757
758 //Do not update SSR mode during recording
759 if ( !ssrmod.surround_obj) {
760 int ret = 0;
761 ret = str_parms_get_str(parms, AUDIO_PARAMETER_SSRMODE_ON, value,
762 sizeof(value));
763 if (ret >= 0) {
764 if (strcmp(value, "true") == 0) {
765 ALOGD("Received SSR session request..setting SSR mode to true");
766 ssrmod.is_ssr_mode_on = true;
767 } else {
768 ALOGD("resetting SSR mode to false");
769 ssrmod.is_ssr_mode_on = false;
770 }
771 }
772 }
773 if (ssrmod.ssr_3mic && ssrmod.surround_obj) {
774 const set_param_data_t *set_params = ssrmod.surround_rec_get_set_param_data();
775 if (set_params != NULL) {
776 while (set_params->name != NULL && set_params->set_param_fn != NULL) {
777 err = str_parms_get_str(parms, set_params->name, value, sizeof(value));
778 if (err >= 0) {
779 ALOGV("Set %s to %s\n", set_params->name, value);
780 set_params->set_param_fn(ssrmod.surround_obj, value);
781 }
782 set_params++;
783 }
784 }
785 }
786}
787
788void audio_extn_ssr_get_parameters(const struct audio_device *adev __unused,
789 struct str_parms *parms,
790 struct str_parms *reply)
791{
792 int err;
793 char value[4096] = {0};
794
795 if (ssrmod.ssr_3mic && ssrmod.surround_obj) {
796 const get_param_data_t *get_params = ssrmod.surround_rec_get_get_param_data();
797 int get_all = 0;
798 err = str_parms_get_str(parms, "ssr.all", value, sizeof(value));
799 if (err >= 0) {
800 get_all = 1;
801 }
802 if (get_params != NULL) {
803 while (get_params->name != NULL && get_params->get_param_fn != NULL) {
804 err = str_parms_get_str(parms, get_params->name, value, sizeof(value));
805 if (get_all || (err >= 0)) {
806 ALOGV("Getting parameter %s", get_params->name);
807 char *val = get_params->get_param_fn(ssrmod.surround_obj);
808 if (val != NULL) {
809 str_parms_add_str(reply, get_params->name, val);
810 free(val);
811 }
812 }
813 get_params++;
814 }
815 }
816 }
817}
818
Apoorv Raghuvanshi6178a3f2013-10-19 12:38:54 -0700819#endif /* SSR_ENABLED */