summaryrefslogtreecommitdiff
path: root/libTVaudio/audio/aml_audio.c (plain)
blob: 326dcaa682e1947e25e7c74ba22f0af709ff80ae
1/*
2 ** aml_audio.c
3 **
4 ** This program is designed for TV application.
5 ** author: Wang Zhe
6 ** Email: Zhe.Wang@amlogic.com
7 **
8 */
9
10#define LOG_TAG "aml_audio"
11
12#include <stdbool.h>
13#include <stdio.h>
14#include <string.h>
15#include <stdlib.h>
16#include <stdint.h>
17#include <signal.h>
18#include <pthread.h>
19#include <unistd.h>
20#include <math.h>
21#include <fcntl.h>
22#include <errno.h>
23#include <sys/mman.h>
24#include <sys/ioctl.h>
25#include <sys/stat.h>
26#include <sys/prctl.h>
27#include <cutils/log.h>
28#include <cutils/properties.h>
29#include "tinyalsa/asoundlib.h"
30
31#include "aml_shelf.h"
32#include "android_out.h"
33#include "aml_audio.h"
34#include "../../audio_virtual_effect.h"
35
36#define ANDROID_OUT_BUFFER_SIZE (2048*8*2) //in byte
37#define DDP_OUT_BUFFER_SIZE (2048*8*2*2*2) //in byte
38#define DD_61937_BUFFER_SIZE (2048*8*2*2*2)
39#define DEFAULT_OUT_SAMPLE_RATE (48000)
40#define DEFAULT_IN_SAMPLE_RATE (48000)
41#define PLAYBACK_PERIOD_SIZE (512)
42#define CAPTURE_PERIOD_SIZE (512)
43#define PLAYBACK_PERIOD_COUNT (4)
44#define CAPTURE_PERIOD_COUNT (4)
45#define TEMP_BUFFER_SIZE (PLAYBACK_PERIOD_SIZE * 4 * 3)
46//output device ID from audio.h
47#define AUDIO_DEVICE_OUT_SPEAKER (0x2)
48#define AUDIO_DEVICE_OUT_REMOTE_SUBMIX (0x8000)
49
50static const struct pcm_config pcm_config_out = {
51 .channels = 2,
52 .rate = DEFAULT_OUT_SAMPLE_RATE,
53 .period_size = PLAYBACK_PERIOD_SIZE,
54 .period_count = PLAYBACK_PERIOD_COUNT,
55 .format = PCM_FORMAT_S16_LE,
56 .stop_threshold = PLAYBACK_PERIOD_SIZE*PLAYBACK_PERIOD_COUNT,
57};
58
59static const struct pcm_config pcm_config_in = {
60 .channels = 2,
61 .rate = DEFAULT_IN_SAMPLE_RATE,
62 .period_size = CAPTURE_PERIOD_SIZE,
63 .period_count = CAPTURE_PERIOD_COUNT,
64 .format = PCM_FORMAT_S16_LE,
65 .stop_threshold = CAPTURE_PERIOD_SIZE*CAPTURE_PERIOD_COUNT*10,
66};
67
68struct buffer_status {
69 unsigned char *start_add;
70 size_t size;
71 size_t level;
72 unsigned int rd;
73 unsigned int wr;
74};
75
76struct resample_para {
77 unsigned int FractionStep;
78 unsigned int SampleFraction;
79 short lastsample_left;
80 short lastsample_right;
81};
82
83struct aml_stream_in {
84 pthread_mutex_t lock;
85 struct pcm_config config;
86 struct pcm *pcm;
87 int card;
88 int device;
89 int standby;
90 int resample_request;
91 void *resample_temp_buffer;
92 struct resample_para resample;
93 int max_bytes;
94 void *temp_buffer;
95 void *write_buffer;
96 int delay_time;
97 int last_delay_time;
98 struct circle_buffer delay_buf;
99 float pre_gain;
100 uint pre_mute;
101};
102
103struct aml_stream_out {
104 pthread_mutex_t lock;
105 struct pcm_config config;
106 struct pcm *pcm;
107 int card;
108 int device;
109 int standby;
110 void *temp_buffer;
111 void *read_buffer;
112 int output_device;
113 int amAudio_OutHandle;
114 struct buffer_status playback_buf;
115 int user_set_device;
116 int is_tv_platform;
117 int32_t *tmp_buffer_8ch;
118 void *audioeffect_tmp_buffer;
119};
120
121struct aml_dev {
122 struct aml_stream_in in;
123 struct aml_stream_out out;
124 pthread_t aml_Audio_ThreadID;
125 int aml_Audio_ThreadTurnOnFlag;
126 int aml_Audio_ThreadExecFlag;
127 int has_EQ_lib;
128 int has_SRS_lib;
129 int has_aml_IIR_lib;
130 int has_Virtualizer;
131 int output_mode;
132 pthread_t android_check_ThreadID;
133};
134
135static struct aml_dev gmAmlDevice = {
136 .in = {
137 .lock = PTHREAD_MUTEX_INITIALIZER,
138 .config = {
139 .channels = 2,
140 .rate = DEFAULT_IN_SAMPLE_RATE,
141 .period_size = CAPTURE_PERIOD_SIZE,
142 .period_count = CAPTURE_PERIOD_COUNT,
143 .format = PCM_FORMAT_S16_LE,
144 .stop_threshold = CAPTURE_PERIOD_SIZE*CAPTURE_PERIOD_COUNT*10,
145 },
146 .pcm = NULL,
147 .card = 0,
148 .device = 0,
149 .standby = 0,
150 .resample_request = 0,
151 .resample_temp_buffer = NULL,
152 .resample = {
153 .FractionStep = 0,
154 .SampleFraction = 0,
155 .lastsample_left = 0,
156 .lastsample_right = 0,
157 },
158 .max_bytes = 0,
159 .temp_buffer = NULL,
160 .write_buffer = NULL,
161 .delay_time = 0,
162 .last_delay_time = 0,
163 .delay_buf = {
164 .lock = PTHREAD_MUTEX_INITIALIZER,
165 .start_add = NULL,
166 .rd = NULL,
167 .wr = NULL,
168 .size = 0,
169 },
170 .pre_gain = 1.0,
171 },
172
173 .out = {
174 .lock = PTHREAD_MUTEX_INITIALIZER,
175 .config = {
176 .channels = 2,
177 .rate = DEFAULT_OUT_SAMPLE_RATE,
178 .period_size = PLAYBACK_PERIOD_SIZE,
179 .period_count = PLAYBACK_PERIOD_COUNT,
180 .format = PCM_FORMAT_S16_LE,
181 .stop_threshold = PLAYBACK_PERIOD_SIZE*PLAYBACK_PERIOD_COUNT,
182 },
183 .pcm = NULL,
184 .card = 0,
185 .device = 0,
186 .standby = 0,
187 .temp_buffer = NULL,
188 .read_buffer = NULL,
189 .output_device = 0,
190 .amAudio_OutHandle = 0,
191 .playback_buf = {
192 .start_add = NULL,
193 .size = 0,
194 .level = 0,
195 .rd = 0,
196 .wr = 0,
197 },
198 .user_set_device = 0,
199 .is_tv_platform = 0,
200 .tmp_buffer_8ch = NULL,
201 .audioeffect_tmp_buffer = NULL,
202 },
203
204 .aml_Audio_ThreadID = 0,
205 .aml_Audio_ThreadTurnOnFlag = 0,
206 .aml_Audio_ThreadExecFlag = 0,
207 .has_EQ_lib = 0,
208 .has_SRS_lib = 0,
209 .has_aml_IIR_lib = 0,
210 .has_Virtualizer = 0,
211 .output_mode = MODEAMAUDIO,
212 .android_check_ThreadID = 0,
213};
214
215struct circle_buffer android_out_buffer = {
216 .lock = PTHREAD_MUTEX_INITIALIZER,
217 .start_add = NULL,
218 .rd = NULL,
219 .wr = NULL,
220 .size = 0,
221};
222
223struct circle_buffer DDP_out_buffer = {
224 .lock = PTHREAD_MUTEX_INITIALIZER,
225 .start_add = NULL,
226 .rd = NULL,
227 .wr = NULL,
228 .size = 0,
229};
230
231struct circle_buffer DD_out_buffer = {
232 .lock = PTHREAD_MUTEX_INITIALIZER,
233 .start_add = NULL,
234 .rd = NULL,
235 .wr = NULL,
236 .size = 0,
237};
238
239static void *start_temp_buffer = NULL;
240static struct aml_dev *gpAmlDevice = NULL;
241static pthread_mutex_t amaudio_dev_op_mutex = PTHREAD_MUTEX_INITIALIZER;
242static unsigned int gUSBCheckLastFlag = 0;
243static unsigned int gUSBCheckFlag = 0;
244
245extern int omx_codec_init(void);
246extern int omx_codec_dts_init(void);
247extern void omx_codec_close(void);
248extern void omx_codec_dts_close(void);
249
250extern int I2S_state;
251
252#define I2S_IN_AUDIO_TYPE "I2SIN Audio Type"
253#define SPDIF_IN_AUDIO_TYPE "SPDIFIN Audio Type"
254#define Audio_In_Source_TYPE "Audio In Source"
255#define HW_RESAMPLE_ENABLE "Hardware resample enable"
256#define AMAUDIO_IN "/dev/amaudio2_in"
257#define AMAUDIO_OUT "/dev/amaudio2_out"
258#define AMAUDIO2_PREENABLE "/sys/class/amaudio2/aml_amaudio2_enable"
259#define AMAUDIO2_INPUTDEVICE "/sys/class/amaudio2/aml_input_device"
260
261#define AMAUDIO_IOC_MAGIC 'A'
262#define AMAUDIO_IOC_GET_SIZE _IOW(AMAUDIO_IOC_MAGIC, 0x00, int)
263#define AMAUDIO_IOC_GET_PTR _IOW(AMAUDIO_IOC_MAGIC, 0x01, int)
264#define AMAUDIO_IOC_RESET _IOW(AMAUDIO_IOC_MAGIC, 0x02, int)
265#define AMAUDIO_IOC_UPDATE_APP_PTR _IOW(AMAUDIO_IOC_MAGIC, 0x03, int)
266#define AMAUDIO_IOC_AUDIO_OUT_MODE _IOW(AMAUDIO_IOC_MAGIC, 0x04, int)
267#define AMAUDIO_IOC_MIC_LEFT_GAIN _IOW(AMAUDIO_IOC_MAGIC, 0x05, int)
268#define AMAUDIO_IOC_MIC_RIGHT_GAIN _IOW(AMAUDIO_IOC_MAGIC, 0x06, int)
269#define AMAUDIO_IOC_MUSIC_GAIN _IOW(AMAUDIO_IOC_MAGIC, 0x07, int)
270
271#define CC_DUMP_SRC_TYPE_INPUT (0)
272#define CC_DUMP_SRC_TYPE_OUTPUT (1)
273#define CC_DUMP_SRC_TYPE_IN_OUT (2)
274#define CC_DUMP_SRC_TYPE_OUT_IN (3)
275
276static int amaudio2_out_handle = -1;
277static int gDumpDataFlag = 0;
278static int gDumpDataFd1 = -1;
279static int gDumpDataFd2 = -1;
280static int audioin_type = 0;
281static int omx_started = 0;
282static int raw_data_counter = 0;
283static int pcm_data_counter = 0;
284static int digital_raw_enable = 0;
285int output_record_enable = 0;
286int spdif_audio_type = LPCM;
287int type_AUDIO_IN = -1;
288extern int virtual_para_buf[2];
289extern int eq_gain_buf[5];
290
291static void DoDumpData(void *data_buf, int size, int aud_src_type);
292static int audio_effect_process(short* buffer, int frame_size);
293static int audio_effect_load_para(struct aml_dev *device);
294
295static int getprop_bool(const char * path)
296{
297 char buf[PROPERTY_VALUE_MAX];
298 int ret = -1;
299
300 ret = property_get(path, buf, NULL);
301 if (ret > 0) {
302 if (strcasecmp(buf, "true") == 0 || strcmp(buf, "1") == 0)
303 return 1;
304 }
305
306 return 0;
307}
308
309inline int GetWriteSpace(char *WritePoint, char *ReadPoint, int buffer_size) {
310 int bytes;
311
312 if (WritePoint >= ReadPoint) {
313 bytes = buffer_size - (WritePoint - ReadPoint);
314 } else {
315 bytes = ReadPoint - WritePoint;
316 }
317 return bytes;
318}
319
320inline size_t GetReadSpace(char *WritePoint, char *ReadPoint, int buffer_size) {
321 int bytes;
322
323 if (WritePoint >= ReadPoint) {
324 bytes = WritePoint - ReadPoint;
325 } else {
326 bytes = buffer_size - (ReadPoint - WritePoint);
327 }
328 return bytes;
329}
330
331inline int write_to_buffer(char *current_pointer, char *buffer, int bytes,
332 char *start_buffer, int buffer_size) {
333 int left_bytes = start_buffer + buffer_size - current_pointer;
334
335 if (left_bytes >= bytes) {
336 memcpy(current_pointer, buffer, bytes);
337 } else {
338 memcpy(current_pointer, buffer, left_bytes);
339 memcpy(start_buffer, buffer + left_bytes, bytes - left_bytes);
340 }
341 return 0;
342}
343
344inline int read_from_buffer(char *current_pointer, char *buffer, int bytes,
345 char *start_buffer, int buffer_size) {
346 int left_bytes = start_buffer + buffer_size - current_pointer;
347
348 if (left_bytes >= bytes) {
349 memcpy(buffer, current_pointer, bytes);
350 } else {
351 memcpy(buffer, current_pointer, left_bytes);
352 memcpy(buffer + left_bytes, start_buffer, bytes - left_bytes);
353 }
354 return 0;
355}
356
357inline void* update_pointer(char *current_pointer, int bytes,
358 char *start_buffer, int buffer_size) {
359 current_pointer += bytes;
360 if (current_pointer >= start_buffer + buffer_size) {
361 current_pointer -= buffer_size;
362 }
363 return current_pointer;
364}
365
366//Clip from 16.16 fixed-point to 0.15 fixed-point.
367inline static short clip(int x) {
368 if (x < -32768) {
369 return -32768;
370 } else if (x > 32767) {
371 return 32767;
372 } else {
373 return x;
374 }
375}
376
377static int resampler_init(struct aml_stream_in *in) {
378 ALOGD("%s, Init Resampler!\n", __FUNCTION__);
379
380 static const double kPhaseMultiplier = 1L << 28;
381
382 in->resample.FractionStep = (unsigned int) (in->config.rate
383 * kPhaseMultiplier / pcm_config_in.rate);
384 in->resample.SampleFraction = 0;
385
386 size_t buffer_size = in->config.period_size * 4;
387 in->resample_temp_buffer = malloc(buffer_size);
388 if (in->resample_temp_buffer == NULL) {
389 ALOGE("%s, Malloc resample buffer failed!\n", __FUNCTION__);
390 return -1;
391 }
392 in->max_bytes = (in->config.period_size * pcm_config_in.rate
393 / in->config.rate + 1) << 2;
394 return 0;
395}
396
397static int resample_process(struct aml_stream_in *in, unsigned int in_frame,
398 short* input, short* output) {
399 unsigned int inputIndex = 0;
400 unsigned int outputIndex = 0;
401 unsigned int FractionStep = in->resample.FractionStep;
402
403 static const uint32_t kPhaseMask = (1LU << 28) - 1;
404 unsigned int frac = in->resample.SampleFraction;
405 short lastsample_left = in->resample.lastsample_left;
406 short lastsample_right = in->resample.lastsample_right;
407
408 while (inputIndex == 0) {
409 *output++ = clip(
410 (int) lastsample_left
411 + ((((int) input[0] - (int) lastsample_left)
412 * ((int) frac >> 13)) >> 15));
413 *output++ = clip(
414 (int) lastsample_right
415 + ((((int) input[1] - (int) lastsample_right)
416 * ((int) frac >> 13)) >> 15));
417
418 frac += FractionStep;
419 inputIndex += (frac >> 28);
420 frac = (frac & kPhaseMask);
421 outputIndex++;
422 }
423
424 while (inputIndex < in_frame) {
425 *output++ = clip(
426 (int) input[2 * inputIndex - 2]
427 + ((((int) input[2 * inputIndex]
428 - (int) input[2 * inputIndex - 2])
429 * ((int) frac >> 13)) >> 15));
430 *output++ = clip(
431 (int) input[2 * inputIndex - 1]
432 + ((((int) input[2 * inputIndex + 1]
433 - (int) input[2 * inputIndex - 1])
434 * ((int) frac >> 13)) >> 15));
435
436 frac += FractionStep;
437 inputIndex += (frac >> 28);
438 frac = (frac & kPhaseMask);
439 outputIndex++;
440 }
441
442 in->resample.lastsample_left = input[2 * in_frame - 2];
443 in->resample.lastsample_right = input[2 * in_frame - 1];
444 in->resample.SampleFraction = frac;
445
446 return outputIndex;
447}
448
449static int tmp_buffer_init(struct circle_buffer *tmp, int buffer_size) {
450 struct circle_buffer *buf = tmp;
451 pthread_mutex_lock(&buf->lock);
452
453 buf->size = buffer_size;
454 buf->start_add = malloc(buffer_size * sizeof(char));
455 if (buf->start_add == NULL) {
456 ALOGD("%s, Malloc android out buffer error!\n", __FUNCTION__);
457 pthread_mutex_unlock(&buf->lock);
458 return -1;
459 }
460 buf->rd = buf->start_add;
461 buf->wr = buf->start_add + buf->size / 2;
462
463 pthread_mutex_unlock(&buf->lock);
464 return 0;
465}
466
467static int tmp_buffer_release(struct circle_buffer *tmp) {
468 struct circle_buffer *buf = tmp;
469 pthread_mutex_lock(&buf->lock);
470
471 if (buf->start_add != NULL) {
472 free(buf->start_add);
473 buf->start_add = NULL;
474 }
475 buf->rd = NULL;
476 buf->wr = NULL;
477 buf->size = 0;
478
479 pthread_mutex_unlock(&buf->lock);
480 return 0;
481}
482
483static int tmp_buffer_reset(struct circle_buffer *tmp) {
484 struct circle_buffer *buf = tmp;
485 buf->rd = buf->wr + buf->size / 2;
486 if (buf->rd >= (buf->start_add + buf->size))
487 buf->rd -= buf->size;
488 return 0;
489}
490
491int buffer_write(struct circle_buffer *tmp, char* buffer, size_t bytes) {
492 struct circle_buffer *buf = tmp;
493 pthread_mutex_lock(&buf->lock);
494 if (buf->start_add == NULL || buf->wr == NULL || buf->wr == NULL
495 || buf->size == 0) {
496 ALOGE("%s, Buffer malloc fail!\n", __FUNCTION__);
497 pthread_mutex_unlock(&buf->lock);
498 return -1;
499 }
500 size_t write_space = GetWriteSpace(buf->wr, buf->rd, buf->size);
501 if (write_space < bytes) {
502 pthread_mutex_unlock(&buf->lock);
503 return -1;
504 }
505 write_to_buffer(buf->wr, buffer, bytes, buf->start_add, buf->size);
506 buf->wr = update_pointer(buf->wr, bytes, buf->start_add, buf->size);
507 pthread_mutex_unlock(&buf->lock);
508 return bytes;
509}
510
511int buffer_read(struct circle_buffer *tmp, char* buffer, size_t bytes) {
512 struct circle_buffer *buf = tmp;
513 pthread_mutex_lock(&buf->lock);
514 if (buf->start_add == NULL || buf->wr == NULL || buf->wr == NULL
515 || buf->size == 0) {
516 ALOGE("%s, Buffer malloc fail!\n", __FUNCTION__);
517 pthread_mutex_unlock(&buf->lock);
518 return -1;
519 }
520 size_t read_space = GetReadSpace(buf->wr, buf->rd, buf->size);
521 if (read_space < bytes) {
522 pthread_mutex_unlock(&buf->lock);
523 return -1;
524 }
525 read_from_buffer(buf->rd, buffer, bytes, buf->start_add, buf->size);
526 buf->rd = update_pointer(buf->rd, bytes, buf->start_add, buf->size);
527 pthread_mutex_unlock(&buf->lock);
528 return bytes;
529}
530
531static int get_output_deviceID(void);
532
533int GetOutputdevice(void) {
534 return get_output_deviceID();
535}
536
537static int set_input_stream_sample_rate(unsigned int sr,
538 struct aml_stream_in *in) {
539 if (check_input_stream_sr(sr) == 0) {
540 in->config.rate = sr;
541 } else {
542 in->config.rate = pcm_config_in.rate;
543 }
544 return 0;
545}
546
547static int get_aml_card(void) {
548 int card = -1, err = -1;
549 int fd = -1;
550 unsigned fileSize = 512;
551 char *read_buf = NULL, *pd = NULL;
552 static const char * const SOUND_CARDS_PATH = "/proc/asound/cards";
553 fd = open(SOUND_CARDS_PATH, O_RDONLY);
554 if (fd < 0) {
555 ALOGE("ERROR: failed to open config file %s error: %d\n",
556 SOUND_CARDS_PATH, errno);
557 return -EINVAL;
558 }
559
560 read_buf = (char *) malloc(fileSize);
561 if (!read_buf) {
562 ALOGE("Failed to malloc read_buf");
563 close(fd);
564 return -ENOMEM;
565 }
566 memset(read_buf, 0x0, fileSize);
567 err = read(fd, read_buf, fileSize);
568 if (err < 0) {
569 ALOGE("ERROR: failed to read config file %s error: %d\n",
570 SOUND_CARDS_PATH, errno);
571 close(fd);
572 free(read_buf);
573 return -EINVAL;
574 }
575 pd = strstr(read_buf, "AML");
576 card = *(pd - 3) - '0';
577
578 free(read_buf);
579 close(fd);
580 return card;
581}
582
583static int get_aml_device(int device_ID) {
584 int port = -1, err = 0;
585 int fd = -1;
586 unsigned fileSize = 512;
587 char *read_buf = NULL, *pd = NULL;
588 static const char *const SOUND_PCM_PATH = "/proc/asound/pcm";
589 fd = open(SOUND_PCM_PATH, O_RDONLY);
590 if (fd < 0) {
591 ALOGE("ERROR: failed to open config file %s error: %d\n", SOUND_PCM_PATH, errno);
592 close(fd);
593 return -EINVAL;
594 }
595
596 read_buf = (char *)malloc(fileSize);
597 if (!read_buf) {
598 ALOGE("Failed to malloc read_buf");
599 close(fd);
600 return -ENOMEM;
601 }
602 memset(read_buf, 0x0, fileSize);
603 err = read(fd, read_buf, fileSize);
604 if (fd < 0) {
605 ALOGE("ERROR: failed to read config file %s error: %d\n", SOUND_PCM_PATH, errno);
606 close(fd);
607 return -EINVAL;
608 }
609
610 if (device_ID == 1) {
611 pd = strstr(read_buf, "SPDIF");
612 port = *(pd -3) - '0';
613 } else if (device_ID == 0){
614 pd = strstr(read_buf, "I2S");
615 port = *(pd -3) - '0';
616 }
617OUT:
618 free(read_buf);
619 close(fd);
620 return port;
621}
622
623static int alsa_in_open(struct aml_stream_in *in) {
624 in->config.channels = pcm_config_in.channels;
625 in->config.period_size = pcm_config_in.period_size;
626 in->config.period_count = pcm_config_in.period_count;
627 in->config.format = pcm_config_in.format;
628 in->config.stop_threshold = CAPTURE_PERIOD_SIZE * CAPTURE_PERIOD_COUNT * 10;
629 in->standby = 1;
630 in->resample_request = 0;
631 in->resample_temp_buffer = NULL;
632 in->max_bytes = in->config.period_size << 2;
633 in->pre_gain = 1.0;
634 in->pre_mute = 0;
635
636 if (in->config.rate == 0) {
637 in->config.rate = pcm_config_in.rate;
638 }
639
640 if (in->config.rate != pcm_config_in.rate) {
641 in->resample_request = 1;
642 int ret = resampler_init(in);
643 if (ret < 0) {
644 return -1;
645 }
646 }
647
648 pthread_mutex_lock(&in->lock);
649 in->card = get_aml_card();
650 in->pcm = pcm_open(in->card, in->device, PCM_IN, &(in->config));
651 if (!pcm_is_ready(in->pcm)) {
652 ALOGE("%s, Unable to open PCM device in: %s\n", __FUNCTION__,
653 pcm_get_error(in->pcm));
654 pcm_close(in->pcm);
655 pthread_mutex_unlock(&in->lock);
656 return -1;
657 }
658
659 in->standby = 0;
660 ALOGD("%s, Input device is opened: card(%d), device(%d)\n", __FUNCTION__,
661 in->card, in->device);
662 pthread_mutex_unlock(&in->lock);
663 return 0;
664}
665
666static int alsa_in_close(struct aml_stream_in *in) {
667 ALOGD("%s, Do input close!\n", __FUNCTION__);
668
669 pthread_mutex_lock(&in->lock);
670 if (!in->standby) {
671 pcm_close(in->pcm);
672 in->pcm = NULL;
673 in->standby = 1;
674 }
675 if (in->resample_request && (in->resample_temp_buffer != NULL)) {
676 free(in->resample_temp_buffer);
677 in->resample_temp_buffer = NULL;
678 }
679 pthread_mutex_unlock(&in->lock);
680 return 0;
681}
682
683static int get_in_framesize(struct aml_stream_in *in) {
684 int sample_format = 0;
685 if (in->config.format == PCM_FORMAT_S16_LE) {
686 sample_format = 2;
687 }
688 return sample_format * in->config.channels;
689}
690
691static void apply_stream_volume_and_pregain(float vol, float gain, char *buf, int size) {
692 uint i;
693 short *sample = (short*)buf;
694 for (i = 0; i < size/sizeof(short); i++)
695 sample[i] = gain*vol*sample[i];
696}
697
698static int alsa_in_read(struct aml_stream_in *in, void* buffer, size_t bytes) {
699 int ret;
700 int resample_request = in->resample_request;
701
702 pthread_mutex_lock(&in->lock);
703 if (in->standby) {
704 pthread_mutex_unlock(&in->lock);
705 ALOGD("%s, Input device is closed!\n", __FUNCTION__);
706 return 0;
707 }
708 //if raw data in HDMI-in, no need to resample
709 if (GetOutputdevice() == 2) {
710 resample_request = 0;
711 }
712
713 int output_size = 0;
714 if (resample_request == 1) {
715 ret = pcm_read(in->pcm, in->resample_temp_buffer, bytes);
716 if (ret < 0) {
717 //wait for next frame
718 usleep(bytes * 1000000 / get_in_framesize(in) / in->config.rate);
719 pthread_mutex_unlock(&in->lock);
720 return ret;
721 }
722
723 if (GetOutputdevice() != 2 &&
724 (gUSBCheckFlag & AUDIO_DEVICE_OUT_SPEAKER) != 0) {
725 float vol = get_android_stream_volume();
726 float gain = in->pre_gain;
727 uint pre_mute = in->pre_mute;
728 if (!pre_mute)
729 apply_stream_volume_and_pregain(vol,gain,in->resample_temp_buffer,bytes);
730 else
731 memset(in->resample_temp_buffer, 0, bytes);
732 }
733 DoDumpData(in->resample_temp_buffer, bytes, CC_DUMP_SRC_TYPE_INPUT);
734
735 output_size = resample_process(in, bytes >> 2,
736 (short *) in->resample_temp_buffer, (short *) buffer) << 2;
737 } else {
738 ret = pcm_read(in->pcm, buffer, bytes);
739 if (ret < 0) {
740 //wait for next frame
741 usleep(bytes * 1000000 / get_in_framesize(in) / in->config.rate);
742 ALOGE("Can't read data from alsa!\n");
743 pthread_mutex_unlock(&in->lock);
744 return ret;
745 }
746
747 if (GetOutputdevice() != 2 &&
748 (gUSBCheckFlag & AUDIO_DEVICE_OUT_SPEAKER) != 0) {
749 float vol = get_android_stream_volume();
750 float gain = in->pre_gain;
751 uint pre_mute = in->pre_mute;
752 if (!pre_mute)
753 apply_stream_volume_and_pregain(vol,gain,buffer,bytes);
754 else
755 memset(buffer, 0, bytes);
756 }
757 /*if (type_AUDIO_IN == 2 && GetOutputdevice() != 2) {
758 short *ptr = buffer;
759 short data;
760 int i = 0;
761 int frame_size = bytes >> 2;
762 for (i = 0; i < frame_size; i++) {
763 data = (short)audio_IIR_process((int)(*ptr), 0);
764 *ptr++ = data;
765 data = (short)audio_IIR_process((int)(*ptr), 1);
766 *ptr++ = data;
767 }
768 }*/
769 DoDumpData(buffer, bytes, CC_DUMP_SRC_TYPE_INPUT);
770
771 output_size = bytes;
772 }
773 pthread_mutex_unlock(&in->lock);
774 return output_size;
775}
776
777static int alsa_out_open(struct aml_stream_out *out) {
778 out->config.period_size = pcm_config_out.period_size;
779 out->config.rate = pcm_config_out.rate;
780 out->config.period_count = pcm_config_out.period_count;
781 out->standby = 1;
782 if (getprop_bool("ro.platform.is.tv")) {
783 out->config.channels = 8;
784 out->config.format = PCM_FORMAT_S32_LE;
785 out->tmp_buffer_8ch = malloc(out->config.period_size * 4 * 8); /*8 channel, 32bit*/
786 if (out->tmp_buffer_8ch == NULL) {
787 ALOGE("cannot malloc memory for out->tmp_buffer_8ch");
788 return -ENOMEM;
789 }
790 out->audioeffect_tmp_buffer = malloc(out->config.period_size * 6);
791 if (out->audioeffect_tmp_buffer == NULL) {
792 ALOGE("cannot malloc memory for audioeffect_tmp_buffer");
793 return -ENOMEM;
794 }
795 out->is_tv_platform = 1;
796 }else {
797 out->config.channels = pcm_config_out.channels;
798 out->config.format = pcm_config_out.format;
799 out->is_tv_platform = 0;
800 }
801
802 pthread_mutex_lock(&out->lock);
803 out->card = get_aml_card();
804 out->pcm = pcm_open(out->card, out->device, PCM_OUT, &(out->config));
805 if (!pcm_is_ready(out->pcm)) {
806 ALOGE("%s, Unable to open PCM device out: %s\n", __FUNCTION__,
807 pcm_get_error(out->pcm));
808 pcm_close(out->pcm);
809 pthread_mutex_unlock(&out->lock);
810 return -1;
811 }
812 out->standby = 0;
813 ALOGD("%s, Output device is opened: card(%d), device(%d)\n", __FUNCTION__,
814 out->card, out->device);
815 pthread_mutex_unlock(&out->lock);
816 return 0;
817}
818
819static int alsa_out_close(struct aml_stream_out *out) {
820 ALOGD("%s, Do output close!\n", __FUNCTION__);
821
822 pthread_mutex_lock(&out->lock);
823 if (out->is_tv_platform == 1) {
824 free(out->tmp_buffer_8ch);
825 free(out->audioeffect_tmp_buffer);
826 out->is_tv_platform = 0;
827 }
828 if (!out->standby) {
829 pcm_close(out->pcm);
830 out->pcm = NULL;
831 out->standby = 1;
832 }
833 pthread_mutex_unlock(&out->lock);
834 return 0;
835}
836
837static int get_out_framesize(struct aml_stream_out *out) {
838 int sample_format = 0;
839 if (out->config.format == PCM_FORMAT_S16_LE)
840 sample_format = 2;
841 return sample_format * out->config.channels;
842}
843
844static int alsa_out_write(struct aml_stream_out *out, void* buffer,
845 size_t bytes) {
846 int ret;
847 int input_frames = bytes >> 2;
848
849 pthread_mutex_lock(&out->lock);
850 if (out->standby) {
851 pthread_mutex_unlock(&out->lock);
852 ALOGD("%s, Output device is closed!\n", __FUNCTION__);
853 return 0;
854 }
855
856 if (out->is_tv_platform == 1) {
857 int16_t *tmp_buffer = (int16_t *)out->audioeffect_tmp_buffer;
858 int16_t *in_buffer = (int16_t *)buffer;
859 int out_byte = input_frames * 32;
860 int i = 0;
861 memcpy((void *)tmp_buffer, buffer, bytes);
862 audio_effect_process(tmp_buffer, input_frames);
863 for (i = 0; i < input_frames; i ++) {
864 out->tmp_buffer_8ch[8*i] = ((int32_t)(in_buffer[2*i])) << 16;
865 out->tmp_buffer_8ch[8*i + 1] = ((int32_t)(in_buffer[2*i + 1])) << 16;
866 out->tmp_buffer_8ch[8*i + 2] = ((int32_t)(tmp_buffer[2*i])) << 16;
867 out->tmp_buffer_8ch[8*i + 3] = ((int32_t)(tmp_buffer[2*i + 1])) << 16;
868 out->tmp_buffer_8ch[8*i + 4] = 0;
869 out->tmp_buffer_8ch[8*i + 5] = 0;
870 out->tmp_buffer_8ch[8*i + 6] = 0;
871 out->tmp_buffer_8ch[8*i + 7] = 0;
872 }
873 ret = pcm_write(out->pcm, out->tmp_buffer_8ch, out_byte);
874 } else {
875 audio_effect_process((short *)buffer, input_frames);
876 ret = pcm_write(out->pcm, buffer, bytes);
877 }
878
879 if (ret < 0) {
880 usleep(bytes * 1000000 / get_out_framesize(out) / out->config.rate);
881 pthread_mutex_unlock(&out->lock);
882 return ret;
883 }
884
885 pthread_mutex_unlock(&out->lock);
886 return bytes;
887}
888
889static int reset_amaudio(struct aml_stream_out *out, int delay_size) {
890 struct buffer_status *buf = &out->playback_buf;
891 buf->rd = 0;
892 buf->wr = 0;
893 buf->level = buf->size;
894 int ret = ioctl(out->amAudio_OutHandle, AMAUDIO_IOC_RESET, delay_size);
895 if (ret < 0) {
896 ALOGE("%s, amaudio reset delay_size error!\n", __FUNCTION__);
897 return -1;
898 }
899 return 0;
900}
901
902static int set_amaudio2_enable(int flag) {
903 int fd = 0;
904 char string[16];
905 fd = open(AMAUDIO2_PREENABLE, O_CREAT | O_RDWR, 0664);
906 if (fd < 0) {
907 ALOGE("unable to open file %s \n", AMAUDIO2_PREENABLE);
908 return -1;
909 }
910 sprintf(string, "%d", flag);
911 write(fd, string, strlen(string));
912 close(fd);
913 return 0;
914}
915
916static int set_input_device(int flag) {
917 int fd = 0;
918 char string[16];
919 fd = open(AMAUDIO2_INPUTDEVICE, O_CREAT | O_RDWR, 0664);
920 if (fd < 0) {
921 ALOGE("unable to open file %s \n", AMAUDIO2_INPUTDEVICE);
922 return -1;
923 }
924 sprintf(string, "%d", flag);
925 write(fd, string, strlen(string));
926 close(fd);
927 return 0;
928}
929
930static int new_audiotrack(struct aml_stream_out *out) {
931 int i = 0, ret = 0, times = 0;
932 int dly_tm = 10000, dly_cnt = 200, retry_times = 5; //2s * 5times
933
934 pthread_mutex_lock(&out->lock);
935 if (gpAmlDevice == NULL) {
936 ALOGE("%s, aml audio is not open, must open it first!\n", __FUNCTION__);
937 pthread_mutex_unlock(&out->lock);
938 return -1;
939 }
940 set_amaudio2_enable(1);
941
942renew_audiotrack:
943 ret = new_android_audiotrack();
944 if (ret < 0) {
945 ALOGE("%s, New an audio track is fail!\n", __FUNCTION__);
946 pthread_mutex_unlock(&out->lock);
947 return -1;
948 }
949
950 /* amaudio needs alsa running first to get the right params, so wait to make sure track is on */
951 if (out->user_set_device == CC_OUT_USE_AMAUDIO) {
952 while (I2S_state < 5 && gpAmlDevice->aml_Audio_ThreadTurnOnFlag == 1) {
953 usleep(dly_tm);
954 i++;
955 if (i >= dly_cnt) {
956 release_android_audiotrack();
957 if (times < retry_times) {
958 i = 0;
959 times++;
960 goto renew_audiotrack;
961 }
962 pthread_mutex_unlock(&out->lock);
963 ALOGE("%s, Time out error: wait %d ms for waiting I2S ready. I2S_state = %d\n",
964 __FUNCTION__, i * dly_tm * retry_times/1000, I2S_state);
965 return -1;
966 }
967 }
968 ALOGD("%s, sucess: wait %d ms for waiting I2S ready. retry_times = %d\n",
969 __FUNCTION__, i * dly_tm / 1000, times);
970 }
971 pthread_mutex_unlock(&out->lock);
972 return 0;
973}
974
975static int release_audiotrack(struct aml_stream_out *out) {
976 ALOGD("%s, Release audio track!\n", __FUNCTION__);
977 pthread_mutex_lock(&out->lock);
978 int ret = release_android_audiotrack();
979 if (ret < 0) {
980 ALOGE("%s, Delete audio track is fail!\n", __FUNCTION__);
981 }
982 ret = release_raw_audio_track();
983 if (ret < 0) {
984 ALOGE("%s, Delete raw audio track is fail!\n", __FUNCTION__);
985 }
986 set_amaudio2_enable(0);
987 pthread_mutex_unlock(&out->lock);
988 return 0;
989}
990
991static int amaudio_out_open(struct aml_stream_out *out) {
992 out->config.period_size = pcm_config_out.period_size;
993 out->config.rate = pcm_config_out.rate;
994 out->config.period_count = pcm_config_out.period_count;
995 out->standby = 1;
996 if (getprop_bool("ro.platform.is.tv")) {
997 out->config.channels = 8;
998 out->config.format = PCM_FORMAT_S32_LE;
999 out->tmp_buffer_8ch = malloc(out->config.period_size * 4 * 8); /*8 channel, 32bit*/
1000 if (out->tmp_buffer_8ch == NULL) {
1001 ALOGE("cannot malloc memory for out->tmp_buffer_8ch");
1002 return -ENOMEM;
1003 }
1004 out->audioeffect_tmp_buffer = malloc(out->config.period_size * 6);
1005 if (out->audioeffect_tmp_buffer == NULL) {
1006 ALOGE("cannot malloc memory for audioeffect_tmp_buffer");
1007 return -ENOMEM;
1008 }
1009 out->is_tv_platform = 1;
1010 }else {
1011 out->config.channels = pcm_config_out.channels;
1012 out->config.format = pcm_config_out.format;
1013 out->is_tv_platform = 0;
1014 }
1015
1016 pthread_mutex_lock(&out->lock);
1017 out->amAudio_OutHandle = -1;
1018 out->amAudio_OutHandle = open(AMAUDIO_OUT, O_RDWR);
1019 if (out->amAudio_OutHandle < 0) {
1020 close(out->amAudio_OutHandle);
1021 out->amAudio_OutHandle = -1;
1022 release_android_audiotrack();
1023 pthread_mutex_unlock(&out->lock);
1024 ALOGE("%s, The device amaudio_out cant't be opened!\n", __FUNCTION__);
1025 return -1;
1026 }
1027
1028 struct buffer_status *buf = &out->playback_buf;
1029 buf->size = ioctl(out->amAudio_OutHandle, AMAUDIO_IOC_GET_SIZE);
1030 buf->start_add = (unsigned char*) mmap(NULL, buf->size, PROT_READ | PROT_WRITE,
1031 MAP_FILE | MAP_SHARED, out->amAudio_OutHandle, 0);
1032 if (buf->start_add == 0) {
1033 close(out->amAudio_OutHandle);
1034 out->amAudio_OutHandle = -1;
1035 release_android_audiotrack();
1036 pthread_mutex_unlock(&out->lock);
1037 ALOGE("%s, Error create mmap!\n", __FUNCTION__);
1038 return -1;
1039 }
1040
1041 out->standby = 0;
1042 pthread_mutex_unlock(&out->lock);
1043 ALOGD("%s, Amaudio device is opened!\n", __FUNCTION__);
1044 return 0;
1045}
1046
1047static int amaudio_out_close(struct aml_stream_out *out) {
1048 ALOGD("%s, Do amaudio device close!\n", __FUNCTION__);
1049 pthread_mutex_lock(&out->lock);
1050 if (out->is_tv_platform == 1) {
1051 free(out->tmp_buffer_8ch);
1052 out->tmp_buffer_8ch = NULL;
1053 free(out->audioeffect_tmp_buffer);
1054 out->audioeffect_tmp_buffer = NULL;
1055 }
1056 if (out->amAudio_OutHandle > 0) {
1057 close(out->amAudio_OutHandle);
1058 out->amAudio_OutHandle = -1;
1059 munmap(out->playback_buf.start_add, out->playback_buf.size);
1060 }
1061 pthread_mutex_unlock(&out->lock);
1062 return 0;
1063}
1064
1065static int amaudio_out_write(struct aml_stream_out *out, void* buffer,
1066 size_t bytes) {
1067 struct buffer_status *buf = &out->playback_buf;
1068 int input_frames = bytes >> 2;
1069 unsigned char *out_buffer = NULL;
1070
1071 if (!out->tmp_buffer_8ch || !out->audioeffect_tmp_buffer) {
1072 ALOGE("buffer NULL,!!!!check\n");
1073 return -1;
1074 }
1075 pthread_mutex_lock(&out->lock);
1076
1077 if (out->is_tv_platform == 1) {
1078 int16_t *tmp_buffer = (int16_t *)out->audioeffect_tmp_buffer;
1079 int16_t *in_buffer = (int16_t *)buffer;
1080 size_t out_byte = input_frames * 32;
1081 int i = 0;
1082
1083 memcpy((void *)tmp_buffer, buffer, bytes);
1084 audio_effect_process(tmp_buffer, input_frames);
1085 for (i = 0; i < input_frames; i ++) {
1086 out->tmp_buffer_8ch[8*i] = ((int32_t)(in_buffer[2*i])) << 16;
1087 out->tmp_buffer_8ch[8*i + 1] = ((int32_t)(in_buffer[2*i + 1])) << 16;
1088 out->tmp_buffer_8ch[8*i + 2] = ((int32_t)(tmp_buffer[2*i])) << 16;
1089 out->tmp_buffer_8ch[8*i + 3] = ((int32_t)(tmp_buffer[2*i + 1])) << 16;
1090 out->tmp_buffer_8ch[8*i + 4] = 0;
1091 out->tmp_buffer_8ch[8*i + 5] = 0;
1092 out->tmp_buffer_8ch[8*i + 6] = 0;
1093 out->tmp_buffer_8ch[8*i + 7] = 0;
1094 }
1095
1096 //get rd ptr, and calculate write space
1097 buf->rd = ioctl(out->amAudio_OutHandle, AMAUDIO_IOC_GET_PTR);
1098 buf->level = buf->size - ((buf->size + buf->wr - buf->rd) % buf->size);
1099
1100 if (buf->level <= out_byte) {
1101 ALOGD("Reset amaudio: buf->level=%x,buf->rd = %x,buf->wr=%x\n",
1102 buf->level, buf->rd, buf->wr);
1103 pthread_mutex_unlock(&out->lock);
1104 return -1;
1105 }
1106 out_buffer = buf->start_add + buf->wr;
1107 memcpy((void *)out_buffer, (void *)out->tmp_buffer_8ch, out_byte);
1108
1109 // update the write pointer and write space
1110 buf->wr = (buf->wr + out_byte) % buf->size;
1111 buf->level = buf->size - ((buf->size + buf->wr - buf->rd) % buf->size);
1112 ioctl(out->amAudio_OutHandle, AMAUDIO_IOC_UPDATE_APP_PTR, buf->wr);
1113
1114 } else {
1115 audio_effect_process((short *)buffer, input_frames);
1116
1117 //get rd ptr, and calculate write space
1118 buf->rd = ioctl(out->amAudio_OutHandle, AMAUDIO_IOC_GET_PTR);
1119 buf->level = buf->size - ((buf->size + buf->wr - buf->rd) % buf->size);
1120
1121 if (buf->level <= bytes) {
1122 ALOGD("Reset amaudio: buf->level=%x,buf->rd = %x,buf->wr=%x\n",
1123 buf->level, buf->rd, buf->wr);
1124 pthread_mutex_unlock(&out->lock);
1125 return -1;
1126 }
1127 out_buffer = buf->start_add + buf->wr;
1128 memcpy((void *)out_buffer, buffer, bytes);
1129
1130 // update the write pointer and write space
1131 buf->wr = (buf->wr + bytes) % buf->size;
1132 buf->level = buf->size - ((buf->size + buf->wr - buf->rd) % buf->size);
1133 ioctl(out->amAudio_OutHandle, AMAUDIO_IOC_UPDATE_APP_PTR, buf->wr);
1134 }
1135
1136 pthread_mutex_unlock(&out->lock);
1137 return bytes;
1138}
1139
1140static int malloc_buffer(struct aml_dev *device) {
1141 void *buffer = NULL;
1142 struct aml_stream_in *in = &device->in;
1143 struct aml_stream_out *out = &device->out;
1144
1145 buffer = malloc(TEMP_BUFFER_SIZE);
1146 if (buffer == NULL) {
1147 ALOGD("%s, Malloc temp buffer failed!\n", __FUNCTION__);
1148 return -1;
1149 }
1150 start_temp_buffer = buffer;
1151 in->write_buffer = buffer;
1152 out->read_buffer = buffer;
1153
1154 in->temp_buffer = malloc(in->max_bytes);
1155 if (in->temp_buffer == NULL) {
1156 ALOGD("%s, Malloc input temp buffer failed!\n", __FUNCTION__);
1157 return -1;
1158 }
1159
1160 out->temp_buffer = malloc(pcm_config_out.period_size << 2);
1161 if (out->temp_buffer == NULL) {
1162 ALOGD("%s, Malloc output temp buffer failed!\n", __FUNCTION__);
1163 return -1;
1164 }
1165
1166 return 0;
1167}
1168
1169static int release_buffer(struct aml_dev *device) {
1170 struct aml_stream_in *in = &device->in;
1171 struct aml_stream_out *out = &device->out;
1172
1173 if (start_temp_buffer != NULL) {
1174 free(start_temp_buffer);
1175 start_temp_buffer = NULL;
1176 in->write_buffer = NULL;
1177 out->read_buffer = NULL;
1178 }
1179 if (in->temp_buffer != NULL) {
1180 free(in->temp_buffer);
1181 in->temp_buffer = NULL;
1182 }
1183 if (out->temp_buffer != NULL) {
1184 free(out->temp_buffer);
1185 out->temp_buffer = NULL;
1186 }
1187 return 0;
1188}
1189
1190static int audio_effect_release() {
1191 unload_EQ_lib();
1192 unload_SRS_lib();
1193 unload_aml_IIR_lib();
1194 Virtualizer_release();
1195 return 0;
1196}
1197
1198static int set_output_deviceID(int deviceID) {
1199 int ret;
1200
1201 if (gpAmlDevice == NULL) {
1202 ALOGE("%s, aml audio is not open, must open it first!\n", __FUNCTION__);
1203 return -1;
1204 }
1205
1206 gpAmlDevice->output_mode = deviceID;
1207 ALOGE("%s, set output device ID: %d!\n", __FUNCTION__, deviceID);
1208 return 0;
1209}
1210
1211static int get_output_deviceID(void) {
1212 if (gpAmlDevice == NULL) {
1213 ALOGE("%s, aml audio is not open, must open it first!\n", __FUNCTION__);
1214 return -1;
1215 }
1216 return gpAmlDevice->output_mode;
1217}
1218
1219static int aml_device_init(struct aml_dev *device) {
1220 int ret;
1221
1222 ALOGD("%s, start to open Devices!\n", __FUNCTION__);
1223
1224 //Malloc temp buffer for audiotrak out
1225 ret = tmp_buffer_init(&android_out_buffer, ANDROID_OUT_BUFFER_SIZE);
1226 if (ret < 0) {
1227 ALOGE("%s, malloc temp buffer error!\n", __FUNCTION__);
1228 goto error1;
1229 }
1230
1231 ret = tmp_buffer_init(&DDP_out_buffer, DDP_OUT_BUFFER_SIZE);
1232 if (ret < 0) {
1233 ALOGE("%s, malloc ddp buffer failed!\n", __FUNCTION__);
1234 goto error2;
1235 }
1236 // add a temp buffer to store dd 61937 audio frame
1237 ret = tmp_buffer_init(&DD_out_buffer, DD_61937_BUFFER_SIZE);
1238 if (ret < 0) {
1239 ALOGE("%s, malloc dd 61937 buffer failed!\n", __FUNCTION__);
1240 goto error3;
1241 }
1242 //open input device of tinyalsa
1243 ret = alsa_in_open(&device->in);
1244 if (ret < 0) {
1245 ALOGE("%s, open alsa in device open error!\n", __FUNCTION__);
1246 goto error3;
1247 }
1248
1249 //Malloc temp buffer for input and output
1250 ret = malloc_buffer(device);
1251 if (ret < 0) {
1252 ALOGE("%s, malloc buffer error!\n", __FUNCTION__);
1253 goto error4;
1254 }
1255
1256 if (device->out.user_set_device == CC_OUT_USE_ALSA) {
1257 set_output_deviceID(MODEAMAUDIO);
1258 //open output device of tinyalsa
1259 ret = alsa_out_open(&device->out);
1260 if (ret < 0) {
1261 ALOGE("%s, open alsa out device open error!\n", __FUNCTION__);
1262 goto error5;
1263 }
1264 } else if (device->out.user_set_device == CC_OUT_USE_AMAUDIO) {
1265 set_output_deviceID(MODEAMAUDIO);
1266 //open output device of amaudio
1267 ret = new_audiotrack(&device->out);
1268 if (ret < 0) {
1269 ALOGE("%s, new audiotrack error!\n", __FUNCTION__);
1270 goto error5;
1271 }
1272 ret = amaudio_out_open(&device->out);
1273 if (ret < 0) {
1274 release_audiotrack(&device->out);
1275 ALOGE("%s, open amaudio out device error!\n", __FUNCTION__);
1276 goto error5;
1277 }
1278 } else if (device->out.user_set_device == CC_OUT_USE_ANDROID) {
1279 set_output_deviceID(MODEANDROID);
1280 ret = new_audiotrack(&device->out);
1281 if (ret < 0) {
1282 ALOGE("%s, open android out device error!\n", __FUNCTION__);
1283 goto error5;
1284 }
1285 }
1286
1287 //EQ lib load and init EQ
1288 ret = load_EQ_lib();
1289 if (ret < 0) {
1290 ALOGE("%s, Load EQ lib fail!\n", __FUNCTION__);
1291 device->has_EQ_lib = 0;
1292 } else {
1293 ret = HPEQ_init();
1294 if (ret < 0) {
1295 device->has_EQ_lib = 0;
1296 } else {
1297 device->has_EQ_lib = 1;
1298 }
1299 HPEQ_enable(1);
1300 }
1301
1302 //load srs lib and init it. SRS is behand resampling, so sample rate is as default sr.
1303 ret = load_SRS_lib();
1304 if (ret < 0) {
1305 ALOGE("%s, Load SRS lib fail!\n", __FUNCTION__);
1306 device->has_SRS_lib = 0;
1307 } else {
1308 ret = srs_init(device->out.config.rate);
1309 if (ret < 0) {
1310 device->has_SRS_lib = 0;
1311 } else {
1312 device->has_SRS_lib = 1;
1313 }
1314 }
1315
1316 //load aml_IIR lib
1317 ret = load_aml_IIR_lib();
1318 if (ret < 0) {
1319 ALOGE("%s, Load aml_IIR lib fail!\n", __FUNCTION__);
1320 device->has_aml_IIR_lib = 0;
1321 } else {
1322 aml_IIR_init(0);
1323 device->has_aml_IIR_lib = 1;
1324 }
1325
1326 ret = Virtualizer_init();
1327 if (ret == 0) {
1328 device->has_Virtualizer = 1;
1329 } else {
1330 ALOGE("%s, init Virtualizer fail!\n", __FUNCTION__);
1331 device->has_Virtualizer = 0;
1332 }
1333
1334 //audio_IIR_init();
1335 audio_effect_load_para(device);
1336 ALOGD("%s, exiting...\n", __FUNCTION__);
1337 return 0;
1338
1339 error5: release_buffer(device);
1340 error4: alsa_in_close(&device->in);
1341 error3: tmp_buffer_release (&DDP_out_buffer);
1342 tmp_buffer_release (&DD_out_buffer);
1343 error2: tmp_buffer_release (&android_out_buffer);
1344 error1: return ret;
1345
1346}
1347
1348static int audio_effect_load_para(struct aml_dev *device) {
1349 int i;
1350 int temp_eq_buf[5] = {0,0,0,0,0};
1351 int temp_virtual_buf[2] = {0, 0};
1352 for (i = 0; i < 5; ++i) {
1353 temp_eq_buf[i] = eq_gain_buf[i];
1354 }
1355 temp_virtual_buf[0] = virtual_para_buf[0];
1356 temp_virtual_buf[1] = virtual_para_buf[1];
1357
1358 if (device->has_EQ_lib)
1359 HPEQ_setParameter(temp_eq_buf[0], temp_eq_buf[1], temp_eq_buf[2], temp_eq_buf[3], temp_eq_buf[4]);
1360 if (device->has_Virtualizer)
1361 Virtualizer_control(temp_virtual_buf[0], temp_virtual_buf[1]);
1362
1363 return 0;
1364}
1365
1366static int aml_device_close(struct aml_dev *device) {
1367 struct aml_stream_in *in = &device->in;
1368 struct aml_stream_out *out = &device->out;
1369
1370 alsa_in_close(in);
1371
1372 if (in->delay_buf.size != 0) {
1373 free(in->delay_buf.start_add);
1374 }
1375
1376 //omx_codec_close();
1377 //omx_codec_dts_close();
1378 omx_started = 0;
1379
1380 if (out->output_device == CC_OUT_USE_ALSA) {
1381 alsa_out_close(out);
1382 } else if (out->output_device == CC_OUT_USE_AMAUDIO) {
1383 amaudio_out_close(out);
1384 release_audiotrack(out);
1385 } else if (out->output_device == CC_OUT_USE_ANDROID) {
1386 release_audiotrack(out);
1387 }
1388
1389 tmp_buffer_release (&DDP_out_buffer);
1390 tmp_buffer_release (&DD_out_buffer);
1391 tmp_buffer_release (&android_out_buffer);
1392 release_buffer(device);
1393 audio_effect_release();
1394 return 0;
1395}
1396
1397static void USB_check(struct aml_stream_out *out) {
1398
1399 gUSBCheckFlag = GetUsbAudioCheckFlag();
1400 if (gUSBCheckLastFlag == gUSBCheckFlag) {
1401 return;
1402 }
1403
1404 ALOGI("Audio Device is changed from %x to %x!\n", gUSBCheckLastFlag, gUSBCheckFlag);
1405
1406 //if audio record from submix, don't change device
1407 if ((gUSBCheckFlag & AUDIO_DEVICE_OUT_REMOTE_SUBMIX) != 0) {
1408 gUSBCheckLastFlag = gUSBCheckFlag;
1409 set_output_record_enable(1);
1410 return;
1411 } else if((gUSBCheckLastFlag & AUDIO_DEVICE_OUT_REMOTE_SUBMIX) != 0) {
1412 gUSBCheckLastFlag = gUSBCheckFlag;
1413 set_output_record_enable(0);
1414 return;
1415 }
1416
1417 if ((gUSBCheckFlag & AUDIO_DEVICE_OUT_SPEAKER) == 0) {
1418 if (out->output_device == CC_OUT_USE_AMAUDIO) {
1419 amaudio_out_close(out);
1420 set_output_deviceID(MODEANDROID);
1421 out->output_device = CC_OUT_USE_ANDROID;
1422 tmp_buffer_reset(&android_out_buffer);
1423 } else if (out->output_device == CC_OUT_USE_ALSA) {
1424 alsa_out_close(out);
1425 new_audiotrack(out);
1426 set_output_deviceID(MODEANDROID);
1427 out->output_device = CC_OUT_USE_ANDROID;
1428 tmp_buffer_reset(&android_out_buffer);
1429 }
1430 ALOGI("%s, USB audio playback device is in.\n", __FUNCTION__);
1431 } else if ((gUSBCheckFlag & AUDIO_DEVICE_OUT_SPEAKER) != 0 && gUSBCheckLastFlag != 0) {
1432 if (out->user_set_device == CC_OUT_USE_AMAUDIO) {
1433 amaudio_out_open(out);
1434 set_output_deviceID(MODEAMAUDIO);
1435 out->output_device = CC_OUT_USE_AMAUDIO;
1436 } else if (out->user_set_device == CC_OUT_USE_ALSA) {
1437 release_audiotrack(out);
1438 alsa_out_open(out);
1439 set_output_deviceID(MODEAMAUDIO);
1440 out->output_device = CC_OUT_USE_ALSA;
1441 }
1442 ALOGI("%s, USB audio playback device is out.\n", __FUNCTION__);
1443 }
1444 gUSBCheckLastFlag = gUSBCheckFlag;
1445 return;
1446}
1447
1448static int get_channel_status(void) {
1449 struct mixer *pmixer;
1450 struct mixer_ctl *pctl;
1451 int card_id;
1452 int type_I2S = -1;
1453 int type_SPDIF = -1;
1454
1455 card_id = get_aml_card();
1456 pmixer = mixer_open(card_id);
1457 if (NULL == pmixer) {
1458 ALOGE("[%s:%d] Failed to open mixer\n", __FUNCTION__, __LINE__);
1459 goto err_exit;
1460 }
1461 pctl = mixer_get_ctl_by_name(pmixer, Audio_In_Source_TYPE);
1462 if (NULL != pctl) {
1463 type_AUDIO_IN = mixer_ctl_get_value(pctl, 0);
1464 if (type_AUDIO_IN != 2) {
1465 mixer_close(pmixer);
1466 return LPCM;
1467 }
1468 }
1469
1470 pctl = mixer_get_ctl_by_name(pmixer, SPDIF_IN_AUDIO_TYPE);
1471 if (NULL != pctl) {
1472 type_SPDIF = mixer_ctl_get_value(pctl, 0);
1473 }
1474
1475 pctl = mixer_get_ctl_by_name(pmixer, I2S_IN_AUDIO_TYPE);
1476 if (NULL != pctl) {
1477 type_I2S = mixer_ctl_get_value(pctl, 0);
1478 }
1479
1480 if (type_SPDIF == LPCM && type_I2S == AC3) {
1481 mixer_close(pmixer);
1482 return MUTE;
1483 }
1484
1485 mixer_close(pmixer);
1486 return type_SPDIF;
1487
1488err_exit:
1489 if (NULL != pmixer) {
1490 mixer_close(pmixer);
1491 }
1492 return -1;
1493}
1494
1495static int set_Hardware_resample(int enable) {
1496 struct mixer *pmixer;
1497 struct mixer_ctl *pctl;
1498 int card_id;
1499 card_id = get_aml_card();
1500 pmixer = mixer_open(card_id);
1501 if (NULL == pmixer) {
1502 ALOGE("[%s:%d] Failed to open mixer\n", __FUNCTION__, __LINE__);
1503 goto err_exit;
1504 }
1505 pctl = mixer_get_ctl_by_name(pmixer, HW_RESAMPLE_ENABLE);
1506 if (NULL != pctl) {
1507 mixer_ctl_set_value(pctl, 0, enable);
1508 }
1509err_exit:
1510 if (NULL != pmixer) {
1511 mixer_close(pmixer);
1512 }
1513 return -1;
1514 }
1515
1516 static int set_rawdata_in_enable(struct aml_stream_out *out) {
1517 if (out->output_device == CC_OUT_USE_AMAUDIO) {
1518 amaudio_out_close(out);
1519 } else if (out->output_device == CC_OUT_USE_ALSA) {
1520 alsa_out_close(out);
1521 new_audiotrack(out);
1522 }
1523 digital_raw_enable = amsysfs_get_sysfs_int("/sys/class/audiodsp/digital_raw");
1524 tmp_buffer_reset(&android_out_buffer);
1525 set_output_deviceID(MODERAW);
1526 out->output_device = CC_OUT_USE_ANDROID;
1527 set_Hardware_resample(4);
1528 if (audioin_type == AC3 || audioin_type == EAC3) {
1529 //omx_codec_init();
1530 }
1531 if (audioin_type == DTS || audioin_type == DTSHD) {
1532 //omx_codec_dts_init();
1533 }
1534 return 0;
1535}
1536
1537static int set_rawdata_in_disable(struct aml_stream_out *out) {
1538
1539 //omx_codec_close();
1540 //omx_codec_dts_close();
1541
1542 if ((gUSBCheckFlag & AUDIO_DEVICE_OUT_SPEAKER) != 0) {
1543 if (out->user_set_device == CC_OUT_USE_AMAUDIO) {
1544 set_output_deviceID(MODEAMAUDIO);
1545 amaudio_out_open(out);
1546 out->output_device = CC_OUT_USE_AMAUDIO;
1547 } else if (out->user_set_device == CC_OUT_USE_ANDROID) {
1548 set_output_deviceID(MODEANDROID);
1549 out->output_device = CC_OUT_USE_ANDROID;
1550 } else if (out->user_set_device == CC_OUT_USE_ALSA) {
1551 release_audiotrack(out);
1552 alsa_out_open(out);
1553 set_output_deviceID(MODEAMAUDIO);
1554 out->output_device = CC_OUT_USE_ALSA;
1555 }
1556 } else {
1557 tmp_buffer_reset(&android_out_buffer);
1558 set_output_deviceID(MODEANDROID);
1559 out->output_device = CC_OUT_USE_ANDROID;
1560 }
1561 set_Hardware_resample(5);
1562 return 0;
1563}
1564
1565int set_output_record_enable(int enable) {
1566 if (enable == 0) {
1567 output_record_enable = 0;
1568 ALOGI("%s, set output record disable!\n", __FUNCTION__);
1569 } else if (enable == 1) {
1570 output_record_enable = 1;
1571 ALOGI("%s, set output record enable\n", __FUNCTION__);
1572 } else {
1573 ALOGE("%s, invalid setting!\n", __FUNCTION__);
1574 }
1575 return 0;
1576}
1577
1578static int check_audio_type(struct aml_stream_out *out) {
1579 audioin_type = get_channel_status();
1580 if (audioin_type == MUTE)
1581 return MUTE;
1582 spdif_audio_type = audioin_type;
1583 if (audioin_type > LPCM && omx_started == 0) {
1584 raw_data_counter++;
1585 }
1586 if (audioin_type == LPCM && omx_started == 1) {
1587 pcm_data_counter++;
1588 }
1589 //temp disable raw stream check as the dts/dolby decoder is not ready
1590 if (0 && raw_data_counter >= 1 && omx_started == 0) {
1591 ALOGI("%s, audio type is changed to RAW data input!,type %d\n", __FUNCTION__,audioin_type);
1592 set_rawdata_in_enable(out);
1593 omx_started = 1;
1594 raw_data_counter = 0;
1595 } else if (pcm_data_counter >= 1 && omx_started == 1) {
1596 ALOGI("%s, audio type is changed to PCM data input!,type %d\n", __FUNCTION__,audioin_type);
1597 set_rawdata_in_disable(out);
1598 omx_started = 0;
1599 pcm_data_counter = 0;
1600 }
1601 /*
1602 if omx ddp decoder has been started, but user configure pcm ->raw output
1603 we need reset decoder to enable decoder to dd/dd+ converter
1604 */
1605 else if (omx_started == 1) {
1606 int digtal_out = amsysfs_get_sysfs_int("/sys/class/audiodsp/digital_raw");
1607 int need_reset_config = 0;
1608 if ((audioin_type == DTS ||audioin_type == EAC3) && digtal_out != digital_raw_enable) {
1609 ALOGI("DD+ passthrough flag changed from %d to %d\n",digital_raw_enable,digtal_out);
1610 need_reset_config = 1;
1611 }
1612 else if (digtal_out > 0 && digital_raw_enable == 0) {
1613 ALOGI("PCM output changed to RAW pass through\n");
1614 need_reset_config = 1;
1615 }
1616 if (need_reset_config) {
1617 ALOGI("pcm to pass through,decoder to reset \n");
1618 set_rawdata_in_disable(out);
1619 set_rawdata_in_enable(out);
1620 //omx_started = 0;
1621 }
1622 }
1623 return 0;
1624}
1625
1626static int audio_effect_process(short* buffer, int frame_size) {
1627 int output_size = frame_size << 2;
1628 if (gpAmlDevice->has_SRS_lib) {
1629 output_size = srs_process(buffer, buffer, frame_size);
1630 }
1631 if (gpAmlDevice->has_Virtualizer) {
1632 Virtualizer_process(buffer, buffer, frame_size);
1633 }
1634 if (gpAmlDevice->has_EQ_lib) {
1635 HPEQ_process(buffer, buffer, frame_size);
1636 }
1637 if (gpAmlDevice->has_aml_IIR_lib) {
1638 short *ptr = buffer;
1639 short data;
1640 int i;
1641 for (i = 0; i < frame_size; i++) {
1642 data = (short)aml_IIR_process((int)(*ptr), 0);
1643 *ptr++ = data;
1644 data = (short)aml_IIR_process((int)(*ptr), 1);
1645 *ptr++ = data;
1646 }
1647 }
1648 return output_size;
1649}
1650
1651static int set_delay(struct aml_stream_in *in, int frame_size) {
1652 unsigned char *buffer_ptr = NULL;
1653 int delay_buffer_size = in->delay_time * 192;
1654 int buffer_size = delay_buffer_size + frame_size;
1655
1656 if (in->delay_buf.size < buffer_size) {
1657 in->delay_buf.start_add = (char *)realloc(
1658 in->delay_buf.start_add, buffer_size * sizeof(char));
1659 if (!in->delay_buf.start_add) {
1660 ALOGE("realloc delay buffer failed\n");
1661 return -1;
1662 }
1663 memset(in->delay_buf.start_add, 0, in->delay_buf.size);
1664 in->delay_buf.size = buffer_size;
1665 in->delay_buf.rd = in->delay_buf.start_add;
1666 in->delay_buf.wr = in->delay_buf.start_add + delay_buffer_size;
1667 ALOGI("realloc delay buffer size %d byte\n", buffer_size);
1668 }
1669
1670 if (in->last_delay_time != in->delay_time) {
1671 in->delay_buf.wr = in->delay_buf.rd + delay_buffer_size;
1672 if (in->delay_buf.wr >= (in->delay_buf.start_add + in->delay_buf.size))
1673 in->delay_buf.wr -= in->delay_buf.size;
1674 in->last_delay_time = in->delay_time;
1675 }
1676
1677 write_to_buffer(in->delay_buf.wr, in->temp_buffer, frame_size,
1678 in->delay_buf.start_add, in->delay_buf.size);
1679 in->delay_buf.wr = update_pointer(in->delay_buf.wr, frame_size,
1680 in->delay_buf.start_add, in->delay_buf.size);
1681
1682 read_from_buffer(in->delay_buf.rd, in->temp_buffer, frame_size,
1683 in->delay_buf.start_add, in->delay_buf.size);
1684 in->delay_buf.rd = update_pointer(in->delay_buf.rd, frame_size,
1685 in->delay_buf.start_add, in->delay_buf.size);
1686
1687 return 0;
1688}
1689
1690static void* aml_audio_threadloop(void *data __unused) {
1691 struct aml_stream_in *in = NULL;
1692 struct aml_stream_out *out = NULL;
1693 int output_size = 0;
1694 int i = 0, ret;
1695
1696 if (gpAmlDevice == NULL) {
1697 ALOGE("%s, gpAmlDevice is NULL\n", __FUNCTION__);
1698 return ((void *) 0);
1699 }
1700
1701 in = &gpAmlDevice->in;
1702 out = &gpAmlDevice->out;
1703
1704 gUSBCheckLastFlag = 0;
1705 gUSBCheckFlag = 0;
1706
1707 gpAmlDevice->aml_Audio_ThreadExecFlag = 1;
1708 prctl(PR_SET_NAME, (unsigned long)"aml_TV_audio");
1709 ret = aml_device_init(gpAmlDevice);
1710 if (ret < 0) {
1711 gpAmlDevice->aml_Audio_ThreadExecFlag = 0;
1712 ALOGE("%s, Devices fail opened!\n", __FUNCTION__);
1713 return NULL;
1714 }
1715
1716 while (gpAmlDevice != NULL && gpAmlDevice->aml_Audio_ThreadTurnOnFlag) {
1717 //exit threadloop
1718 if (gpAmlDevice->aml_Audio_ThreadTurnOnFlag == 0) {
1719 ALOGD("%s, aml_Audio_ThreadTurnOnFlag is 0 break now.\n",
1720 __FUNCTION__);
1721 break;
1722 }
1723 if (GetWriteSpace((char *) in->write_buffer, (char *) out->read_buffer,
1724 TEMP_BUFFER_SIZE) > in->max_bytes) {
1725 output_size = alsa_in_read(in, in->temp_buffer,
1726 in->config.period_size * 4);
1727 if (output_size < 0) {
1728 //ALOGE("%s, alsa_in_read fail!\n", __FUNCTION__);
1729 } else {
1730 if (check_audio_type(out) == MUTE)
1731 memset((char *) in->temp_buffer, 0, output_size);
1732 if (in->delay_time != 0 && get_output_deviceID() == 0) {
1733 set_delay(in, output_size);
1734 }
1735 write_to_buffer((char *) in->write_buffer,
1736 (char *) in->temp_buffer, output_size,
1737 (char *) start_temp_buffer, TEMP_BUFFER_SIZE);
1738 in->write_buffer = update_pointer((char *) in->write_buffer,
1739 output_size, (char *) start_temp_buffer,
1740 TEMP_BUFFER_SIZE);
1741 }
1742
1743 }
1744
1745 USB_check(out);
1746
1747 if (GetReadSpace((char *) in->write_buffer, (char *) out->read_buffer,
1748 TEMP_BUFFER_SIZE) > pcm_config_out.period_size << 2) {
1749 read_from_buffer((char *) out->read_buffer,
1750 (char *) out->temp_buffer, pcm_config_out.period_size << 2,
1751 (char *) start_temp_buffer, TEMP_BUFFER_SIZE);
1752
1753 output_size = pcm_config_out.period_size << 2;
1754 if (gpAmlDevice->out.output_device == CC_OUT_USE_ALSA) {
1755 output_size = alsa_out_write(out, out->temp_buffer,
1756 output_size);
1757 } else if (gpAmlDevice->out.output_device == CC_OUT_USE_AMAUDIO) {
1758 output_size = amaudio_out_write(out, out->temp_buffer,
1759 output_size);
1760 if (output_size < 0) {
1761 amaudio_out_close(out);
1762 set_output_deviceID(MODEAMAUDIO);
1763 amaudio_out_open(out);
1764 reset_amaudio(out, 4096);
1765 }
1766 if (output_record_enable == 1) {
1767 buffer_write(&android_out_buffer, out->temp_buffer,
1768 output_size);
1769 }
1770 } else if (gpAmlDevice->out.output_device == CC_OUT_USE_ANDROID) {
1771 output_size = buffer_write(&android_out_buffer,
1772 out->temp_buffer, output_size);
1773 if (output_size < 0) {
1774 usleep(200*1000);
1775 }
1776 }
1777
1778 if (output_size > 0) {
1779 out->read_buffer = update_pointer((char *) out->read_buffer,
1780 output_size, (char *) start_temp_buffer,
1781 TEMP_BUFFER_SIZE);
1782 DoDumpData(out->temp_buffer, output_size,
1783 CC_DUMP_SRC_TYPE_OUTPUT);
1784 memset(out->temp_buffer, 0, output_size);
1785 }
1786 }
1787 }
1788
1789 if (gpAmlDevice != NULL) {
1790 gpAmlDevice->aml_Audio_ThreadTurnOnFlag = 0;
1791 ALOGD("%s, set aml_Audio_ThreadTurnOnFlag as 0.\n", __FUNCTION__);
1792 gpAmlDevice->aml_Audio_ThreadExecFlag = 0;
1793 ALOGD("%s, set aml_Audio_ThreadExecFlag as 0.\n", __FUNCTION__);
1794 }
1795
1796 if (gpAmlDevice != NULL) {
1797 aml_device_close(gpAmlDevice);
1798 }
1799
1800 ALOGD("%s, exiting...\n", __FUNCTION__);
1801 return ((void *) 0);
1802}
1803
1804static int clrDevice(struct aml_dev *device) {
1805 memset((void *) device, 0, sizeof(struct aml_dev));
1806
1807 device->in.config.channels = 2;
1808 device->in.config.rate = DEFAULT_IN_SAMPLE_RATE;
1809 device->in.config.period_size = CAPTURE_PERIOD_SIZE;
1810 device->in.config.period_count = CAPTURE_PERIOD_COUNT;
1811 device->in.config.format = PCM_FORMAT_S16_LE;
1812
1813 device->out.config.channels = 2;
1814 device->out.config.rate = DEFAULT_OUT_SAMPLE_RATE;
1815 device->out.config.period_size = PLAYBACK_PERIOD_SIZE;
1816 device->out.config.period_count = PLAYBACK_PERIOD_COUNT;
1817 device->out.config.format = PCM_FORMAT_S16_LE;
1818
1819 return 0;
1820}
1821
1822int aml_audio_open(unsigned int sr, int input_device, int output_device) {
1823 pthread_attr_t attr;
1824 struct sched_param param;
1825 int ret;
1826
1827 ALOGD("%s, sr = %d, input_device = %d, output_device = %d\n",
1828 __FUNCTION__, sr, input_device, output_device);
1829
1830 aml_audio_close();
1831
1832 pthread_mutex_lock(&amaudio_dev_op_mutex);
1833
1834 gpAmlDevice = &gmAmlDevice;
1835 clrDevice(gpAmlDevice);
1836
1837 ret = set_input_stream_sample_rate(sr, &gpAmlDevice->in);
1838 if (ret < 0) {
1839 ALOGE("%s, set_input_stream_sample_rate fail!\n", __FUNCTION__);
1840 clrDevice(gpAmlDevice);
1841 gpAmlDevice = NULL;
1842 pthread_mutex_unlock(&amaudio_dev_op_mutex);
1843 return -1;
1844 }
1845
1846 gpAmlDevice->out.output_device = output_device;
1847 gpAmlDevice->out.user_set_device = output_device;
1848 if (gpAmlDevice->out.user_set_device == CC_OUT_USE_ALSA) {
1849 ALOGD("%s,Use tinyalsa as output device!\n", __FUNCTION__);
1850 } else if (gpAmlDevice->out.user_set_device == CC_OUT_USE_AMAUDIO) {
1851 ALOGD("%s, Use amlogic amaudio as output device!\n", __FUNCTION__);
1852 } else if (gpAmlDevice->out.user_set_device == CC_OUT_USE_ANDROID) {
1853 ALOGD("%s, Use amlogic android as output device!\n", __FUNCTION__);
1854 } else {
1855 ALOGE("%s, Unkown output device, use default amaudio\n", __FUNCTION__);
1856 gpAmlDevice->out.user_set_device = CC_OUT_USE_AMAUDIO;
1857 }
1858
1859 ret = set_input_device(input_device);
1860 if (ret < 0) {
1861 ALOGE("Fail to set input device for HW resample!\n");
1862 }
1863
1864 gpAmlDevice->in.device = get_aml_device(input_device);
1865
1866 pthread_attr_init(&attr);
1867 pthread_attr_setschedpolicy(&attr, SCHED_RR);
1868 param.sched_priority = sched_get_priority_max(SCHED_RR);
1869 /*pthread_attr_setschedpolicy(&attr, SCHED_FIFO);
1870 param.sched_priority = sched_get_priority_max(SCHED_FIFO);
1871 ALOGD("%s, aml_audio thread has %d priority!\n",
1872 __FUNCTION__, param.sched_priority);*/
1873 pthread_attr_setschedparam(&attr, &param);
1874 gpAmlDevice->aml_Audio_ThreadTurnOnFlag = 1;
1875 gpAmlDevice->aml_Audio_ThreadExecFlag = 0;
1876 ret = pthread_create(&gpAmlDevice->aml_Audio_ThreadID, &attr,
1877 &aml_audio_threadloop, NULL);
1878 pthread_attr_destroy(&attr);
1879 if (ret != 0) {
1880 ALOGE("%s, Create thread fail!\n", __FUNCTION__);
1881 aml_device_close(gpAmlDevice);
1882 clrDevice(gpAmlDevice);
1883 gpAmlDevice = NULL;
1884 pthread_mutex_unlock(&amaudio_dev_op_mutex);
1885 return -1;
1886 }
1887
1888 creat_pthread_for_android_check(&gpAmlDevice->android_check_ThreadID);
1889
1890 pthread_mutex_unlock(&amaudio_dev_op_mutex);
1891
1892 ALOGD("%s, exiting...\n", __FUNCTION__);
1893 return 0;
1894}
1895
1896int aml_audio_close(void) {
1897 int i = 0, tmp_timeout_count = 1000;
1898
1899 ALOGD("%s, gpAmlDevice = %p\n", __FUNCTION__, gpAmlDevice);
1900
1901 pthread_mutex_lock(&amaudio_dev_op_mutex);
1902
1903 if (gpAmlDevice != NULL) {
1904 gpAmlDevice->aml_Audio_ThreadTurnOnFlag = 0;
1905 ALOGD("%s, set aml_Audio_ThreadTurnOnFlag as 0.\n", __FUNCTION__);
1906 while (1) {
1907 if (gpAmlDevice->aml_Audio_ThreadExecFlag == 0) {
1908 break;
1909 }
1910 if (i >= tmp_timeout_count) {
1911 break;
1912 }
1913 i++;
1914 usleep(10 * 1000);
1915 }
1916
1917 if (i >= tmp_timeout_count) {
1918 ALOGE("%s, we have try %d times, but the aml audio thread's exec flag is still(%d)!!!\n",
1919 __FUNCTION__, tmp_timeout_count,
1920 gpAmlDevice->aml_Audio_ThreadExecFlag);
1921 } else {
1922 ALOGD("%s, kill aml audio thread success after try %d times.\n",
1923 __FUNCTION__, i);
1924 }
1925
1926 pthread_join(gpAmlDevice->aml_Audio_ThreadID, NULL);
1927 gpAmlDevice->aml_Audio_ThreadID = 0;
1928
1929 exit_pthread_for_android_check(gpAmlDevice->android_check_ThreadID);
1930 gpAmlDevice->android_check_ThreadID = 0;
1931
1932 clrDevice(gpAmlDevice);
1933 gpAmlDevice = NULL;
1934
1935 ALOGD("%s, aml audio close success.\n", __FUNCTION__);
1936 }
1937
1938 pthread_mutex_unlock(&amaudio_dev_op_mutex);
1939 return 0;
1940}
1941
1942int check_input_stream_sr(unsigned int sr) {
1943 if (sr >= 8000 && sr <= 48000) {
1944 return 0;
1945 }
1946 return -1;
1947}
1948
1949int set_output_mode(int mode) {
1950 if (gpAmlDevice == NULL) {
1951 ALOGE("%s, aml audio is not open, must open it first!\n", __FUNCTION__);
1952 return -1;
1953 }
1954
1955 if (mode < CC_OUT_MODE_DIRECT || mode > CC_OUT_MODE_DIRECT_MIX) {
1956 ALOGE("%s, mode error: mode = %d!\n", __FUNCTION__, mode);
1957 return -1;
1958 }
1959
1960 int OutHandle = gpAmlDevice->out.amAudio_OutHandle;
1961 if (OutHandle < 0) {
1962 ALOGE("%s, amaudio out handle error!\n", __FUNCTION__);
1963 return -1;
1964 }
1965
1966 pthread_mutex_lock(&gpAmlDevice->out.lock);
1967 ioctl(OutHandle, AMAUDIO_IOC_AUDIO_OUT_MODE, mode);
1968 pthread_mutex_unlock(&gpAmlDevice->out.lock);
1969 return 0;
1970}
1971
1972int set_music_gain(int gain) {
1973 if (gpAmlDevice == NULL) {
1974 ALOGE("%s, aml audio is not open, must open it first!\n", __FUNCTION__);
1975 return -1;
1976 }
1977
1978 int OutHandle = gpAmlDevice->out.amAudio_OutHandle;
1979 if (OutHandle < 0) {
1980 ALOGE("%s, amaudio out handle error!\n", __FUNCTION__);
1981 return -1;
1982 }
1983
1984 pthread_mutex_lock(&gpAmlDevice->out.lock);
1985 if (gain > 256) {
1986 gain = 256;
1987 }
1988 if (gain < 0) {
1989 gain = 0;
1990 }
1991 ioctl(OutHandle, AMAUDIO_IOC_MUSIC_GAIN, gain);
1992 ALOGD("%s, music gain :%d!\n", __FUNCTION__, gain);
1993 pthread_mutex_unlock(&gpAmlDevice->out.lock);
1994 return 0;
1995}
1996
1997int set_left_gain(int left_gain) {
1998 if (gpAmlDevice == NULL) {
1999 ALOGE("%s, aml audio is not open, must open it first!\n", __FUNCTION__);
2000 return -1;
2001 }
2002
2003 int OutHandle = gpAmlDevice->out.amAudio_OutHandle;
2004 if (OutHandle < 0) {
2005 ALOGE("%s, amaudio out handle error!\n", __FUNCTION__);
2006 return -1;
2007 }
2008 pthread_mutex_lock(&gpAmlDevice->out.lock);
2009 if (left_gain > 256) {
2010 left_gain = 256;
2011 }
2012 if (left_gain < 0) {
2013 left_gain = 0;
2014 }
2015 ioctl(OutHandle, AMAUDIO_IOC_MIC_LEFT_GAIN, left_gain);
2016 ALOGD("%s, left mic gain :%d!\n", __FUNCTION__, left_gain);
2017 pthread_mutex_unlock(&gpAmlDevice->out.lock);
2018 return 0;
2019}
2020
2021int set_right_gain(int right_gain) {
2022 if (gpAmlDevice == NULL) {
2023 ALOGE("%s, aml audio is not open, must open it first!\n", __FUNCTION__);
2024 return -1;
2025 }
2026 int OutHandle = gpAmlDevice->out.amAudio_OutHandle;
2027 if (OutHandle < 0) {
2028 ALOGE("%s, amaudio out handle error!\n", __FUNCTION__);
2029 return -1;
2030 }
2031 pthread_mutex_lock(&gpAmlDevice->out.lock);
2032 if (right_gain > 256) {
2033 right_gain = 256;
2034 }
2035 if (right_gain < 0) {
2036 right_gain = 0;
2037 }
2038 ioctl(OutHandle, AMAUDIO_IOC_MIC_RIGHT_GAIN, right_gain);
2039 ALOGD("%s, right mic gain :%d!\n", __FUNCTION__, right_gain);
2040 pthread_mutex_unlock(&gpAmlDevice->out.lock);
2041 return 0;
2042}
2043
2044int set_audio_delay(int delay_ms) {
2045 gpAmlDevice->in.delay_time = delay_ms;
2046 ALOGI("Set audio delay time %d ms!\n", delay_ms);
2047 return 0;
2048}
2049
2050int get_audio_delay(void) {
2051 return gpAmlDevice->in.delay_time;
2052}
2053
2054int SetDumpDataFlag(int tmp_flag) {
2055 int tmp_val;
2056 tmp_val = gDumpDataFlag;
2057 gDumpDataFlag = tmp_flag;
2058 return tmp_val;
2059}
2060
2061int GetDumpDataFlag(void) {
2062 int tmp_val = 0;
2063 tmp_val = gDumpDataFlag;
2064 return tmp_val;
2065}
2066
2067static void DoDumpData(void *data_buf, int size, int aud_src_type) {
2068 int tmp_type = 0;
2069 char prop_value[PROPERTY_VALUE_MAX] = { 0 };
2070 char file_path_01[PROPERTY_VALUE_MAX] = { 0 };
2071 char file_path_02[PROPERTY_VALUE_MAX] = { 0 };
2072
2073 if (GetDumpDataFlag() == 0) {
2074 return;
2075 }
2076
2077 memset(prop_value, '\0', PROPERTY_VALUE_MAX);
2078 property_get("audio.dumpdata.en", prop_value, "null");
2079 if (strcasecmp(prop_value, "null") == 0
2080 || strcasecmp(prop_value, "0") == 0) {
2081 if (gDumpDataFd1 >= 0) {
2082 close(gDumpDataFd1);
2083 gDumpDataFd1 = -1;
2084 }
2085 if (gDumpDataFd2 >= 0) {
2086 close(gDumpDataFd2);
2087 gDumpDataFd2 = -1;
2088 }
2089
2090 return;
2091 }
2092
2093 tmp_type = CC_DUMP_SRC_TYPE_INPUT;
2094 property_get("audio.dumpdata.src", prop_value, "null");
2095 if (strcasecmp(prop_value, "null") == 0
2096 || strcasecmp(prop_value, "input") == 0) {
2097 tmp_type = CC_DUMP_SRC_TYPE_INPUT;
2098 } else if (strcasecmp(prop_value, "output") == 0) {
2099 tmp_type = CC_DUMP_SRC_TYPE_OUTPUT;
2100 } else if (strcasecmp(prop_value, "input,output") == 0) {
2101 tmp_type = CC_DUMP_SRC_TYPE_IN_OUT;
2102 } else if (strcasecmp(prop_value, "output,input") == 0) {
2103 tmp_type = CC_DUMP_SRC_TYPE_OUT_IN;
2104 }
2105
2106 if (tmp_type == CC_DUMP_SRC_TYPE_INPUT
2107 || tmp_type == CC_DUMP_SRC_TYPE_OUTPUT) {
2108 if (tmp_type != aud_src_type) {
2109 return;
2110 }
2111 }
2112
2113 memset(file_path_01, '\0', PROPERTY_VALUE_MAX);
2114 property_get("audio.dumpdata.path", file_path_01, "null");
2115 if (strcasecmp(file_path_01, "null") == 0) {
2116 file_path_01[0] = '\0';
2117 }
2118
2119 if (tmp_type == CC_DUMP_SRC_TYPE_IN_OUT
2120 || tmp_type == CC_DUMP_SRC_TYPE_OUT_IN) {
2121 memset(file_path_02, '\0', PROPERTY_VALUE_MAX);
2122 property_get("audio.dumpdata.path2", file_path_02, "null");
2123 if (strcasecmp(file_path_02, "null") == 0) {
2124 file_path_02[0] = '\0';
2125 }
2126 }
2127
2128 if (gDumpDataFd1 < 0 && file_path_01[0] != '\0') {
2129 if (access(file_path_01, 0) == 0) {
2130 gDumpDataFd1 = open(file_path_01, O_RDWR | O_SYNC);
2131 if (gDumpDataFd1 < 0) {
2132 ALOGE("%s, Open device file \"%s\" error: %s.\n", __FUNCTION__,
2133 file_path_01, strerror(errno));
2134 }
2135 } else {
2136 gDumpDataFd1 = open(file_path_01, O_WRONLY | O_CREAT | O_EXCL,
2137 S_IRUSR | S_IWUSR);
2138 if (gDumpDataFd1 < 0) {
2139 ALOGE("%s, Create device file \"%s\" error: %s.\n",
2140 __FUNCTION__, file_path_01, strerror(errno));
2141 }
2142 }
2143 }
2144
2145 if (gDumpDataFd2 < 0 && file_path_02[0] != '\0'
2146 && (tmp_type == CC_DUMP_SRC_TYPE_IN_OUT
2147 || tmp_type == CC_DUMP_SRC_TYPE_OUT_IN)) {
2148 if (access(file_path_02, 0) == 0) {
2149 gDumpDataFd2 = open(file_path_02, O_RDWR | O_SYNC);
2150 if (gDumpDataFd2 < 0) {
2151 ALOGE("%s, Open device file \"%s\" error: %s.\n", __FUNCTION__,
2152 file_path_02, strerror(errno));
2153 }
2154 } else {
2155 gDumpDataFd2 = open(file_path_02, O_WRONLY | O_CREAT | O_EXCL,
2156 S_IRUSR | S_IWUSR);
2157 if (gDumpDataFd2 < 0) {
2158 ALOGE("%s, Create device file \"%s\" error: %s.\n",
2159 __FUNCTION__, file_path_02, strerror(errno));
2160 }
2161 }
2162 }
2163
2164 if (tmp_type == CC_DUMP_SRC_TYPE_IN_OUT) {
2165 if (aud_src_type == CC_DUMP_SRC_TYPE_INPUT && gDumpDataFd1 >= 0) {
2166 write(gDumpDataFd1, data_buf, size);
2167 } else if (aud_src_type == CC_DUMP_SRC_TYPE_OUTPUT
2168 && gDumpDataFd2 >= 0) {
2169 write(gDumpDataFd2, data_buf, size);
2170 }
2171 } else if (tmp_type == CC_DUMP_SRC_TYPE_OUT_IN) {
2172 if (aud_src_type == CC_DUMP_SRC_TYPE_OUTPUT && gDumpDataFd1 >= 0) {
2173 write(gDumpDataFd1, data_buf, size);
2174 } else if (aud_src_type == CC_DUMP_SRC_TYPE_INPUT
2175 && gDumpDataFd2 >= 0) {
2176 write(gDumpDataFd2, data_buf, size);
2177 }
2178 } else {
2179 if (gDumpDataFd1 >= 0) {
2180 write(gDumpDataFd1, data_buf, size);
2181 }
2182 }
2183}
2184
2185int aml_audio_set_pregain(float gain)
2186{
2187 ALOGD("%s, pre-gain = %f dB\n", __FUNCTION__, gain);
2188
2189 pthread_mutex_lock(&amaudio_dev_op_mutex);
2190
2191 if (gpAmlDevice != NULL) {
2192 gpAmlDevice->in.pre_gain = powf(10, gain/20);
2193 }
2194
2195 pthread_mutex_unlock(&amaudio_dev_op_mutex);
2196
2197 return 0;
2198}
2199
2200int aml_audio_get_pregain(float *gain)
2201{
2202 if (gpAmlDevice != NULL) {
2203 *gain = 20*log10f(gpAmlDevice->in.pre_gain);
2204 return 0;
2205 }
2206
2207 ALOGE("%s, no active gpAmlDevice!\n", __FUNCTION__);
2208 return -1;
2209}
2210
2211int aml_audio_set_pre_mute(uint mute)
2212{
2213 ALOGD("%s, mute = %d\n", __FUNCTION__, mute);
2214
2215 pthread_mutex_lock(&amaudio_dev_op_mutex);
2216
2217 if (gpAmlDevice != NULL) {
2218 gpAmlDevice->in.pre_mute = mute;
2219 }
2220
2221 pthread_mutex_unlock(&amaudio_dev_op_mutex);
2222
2223 return 0;
2224}
2225
2226int aml_audio_get_pre_mute(uint *mute)
2227{
2228 if (gpAmlDevice != NULL) {
2229 *mute = gpAmlDevice->in.pre_mute;
2230 return 0;
2231 }
2232
2233 ALOGE("%s, no active gpAmlDevice!\n", __FUNCTION__);
2234 return -1;
2235}
2236