blob: ccb2b4755ffffcba4818db5d5b953c33a396175a
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 | |
50 | static 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 | |
59 | static 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 | |
68 | struct 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 | |
76 | struct resample_para { |
77 | unsigned int FractionStep; |
78 | unsigned int SampleFraction; |
79 | short lastsample_left; |
80 | short lastsample_right; |
81 | }; |
82 | |
83 | struct 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 | |
103 | struct 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 | |
121 | struct 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 | |
135 | static 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 | |
215 | struct 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 | |
223 | struct 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 | |
231 | struct 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 | |
239 | static void *start_temp_buffer = NULL; |
240 | static struct aml_dev *gpAmlDevice = NULL; |
241 | static pthread_mutex_t amaudio_dev_op_mutex = PTHREAD_MUTEX_INITIALIZER; |
242 | static unsigned int gUSBCheckLastFlag = 0; |
243 | static unsigned int gUSBCheckFlag = 0; |
244 | |
245 | extern int omx_codec_init(void); |
246 | extern int omx_codec_dts_init(void); |
247 | extern void omx_codec_close(void); |
248 | extern void omx_codec_dts_close(void); |
249 | |
250 | extern 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 | |
276 | static int amaudio2_out_handle = -1; |
277 | static int gDumpDataFlag = 0; |
278 | static int gDumpDataFd1 = -1; |
279 | static int gDumpDataFd2 = -1; |
280 | static int audioin_type = 0; |
281 | static int omx_started = 0; |
282 | static int raw_data_counter = 0; |
283 | static int pcm_data_counter = 0; |
284 | static int digital_raw_enable = 0; |
285 | int output_record_enable = 0; |
286 | int spdif_audio_type = LPCM; |
287 | int type_AUDIO_IN = -1; |
288 | extern int virtual_para_buf[2]; |
289 | extern int eq_gain_buf[5]; |
290 | |
291 | static void DoDumpData(void *data_buf, int size, int aud_src_type); |
292 | static int audio_effect_process(short* buffer, int frame_size); |
293 | static int audio_effect_load_para(struct aml_dev *device); |
294 | |
295 | static 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 | |
309 | inline 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 | |
320 | inline 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 | |
331 | inline 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 | |
344 | inline 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 | |
357 | inline 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. |
367 | inline 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 | |
377 | static 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 | |
397 | static 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 | |
449 | static 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 | |
467 | static 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 | |
483 | static 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 | |
491 | int 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 | |
511 | int 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 | |
531 | static int get_output_deviceID(void); |
532 | |
533 | int GetOutputdevice(void) { |
534 | return get_output_deviceID(); |
535 | } |
536 | |
537 | static 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 | |
547 | static 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 | |
583 | static 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 | } |
617 | OUT: |
618 | free(read_buf); |
619 | close(fd); |
620 | return port; |
621 | } |
622 | |
623 | static 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 | |
666 | static 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 | |
683 | static 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 | |
691 | static 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 | |
698 | static 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 | |
777 | static 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.has.tvuimode")) { |
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 | |
819 | static 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 | |
837 | static 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 | |
844 | static 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 | |
889 | static 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 | |
902 | static 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 | |
916 | static 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 | |
930 | static 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 | |
942 | renew_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 | |
975 | static 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 | |
991 | static 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.has.tvuimode")) { |
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 | |
1047 | static 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 | |
1065 | static 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 | |
1140 | static 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 | |
1169 | static 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 | |
1190 | static 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 | |
1198 | static 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 | |
1211 | static 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 | |
1219 | static 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 | |
1348 | static 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 | |
1366 | static 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 | |
1397 | static 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 | |
1448 | static 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 | |
1488 | err_exit: |
1489 | if (NULL != pmixer) { |
1490 | mixer_close(pmixer); |
1491 | } |
1492 | return -1; |
1493 | } |
1494 | |
1495 | static 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 | } |
1509 | err_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 | if (audioin_type == DTS || audioin_type == DTSHD) |
1531 | omx_codec_dts_init(); |
1532 | return 0; |
1533 | } |
1534 | |
1535 | static int set_rawdata_in_disable(struct aml_stream_out *out) { |
1536 | |
1537 | omx_codec_close(); |
1538 | omx_codec_dts_close(); |
1539 | |
1540 | if ((gUSBCheckFlag & AUDIO_DEVICE_OUT_SPEAKER) != 0) { |
1541 | if (out->user_set_device == CC_OUT_USE_AMAUDIO) { |
1542 | set_output_deviceID(MODEAMAUDIO); |
1543 | amaudio_out_open(out); |
1544 | out->output_device = CC_OUT_USE_AMAUDIO; |
1545 | } else if (out->user_set_device == CC_OUT_USE_ANDROID) { |
1546 | set_output_deviceID(MODEANDROID); |
1547 | out->output_device = CC_OUT_USE_ANDROID; |
1548 | } else if (out->user_set_device == CC_OUT_USE_ALSA) { |
1549 | release_audiotrack(out); |
1550 | alsa_out_open(out); |
1551 | set_output_deviceID(MODEAMAUDIO); |
1552 | out->output_device = CC_OUT_USE_ALSA; |
1553 | } |
1554 | } else { |
1555 | tmp_buffer_reset(&android_out_buffer); |
1556 | set_output_deviceID(MODEANDROID); |
1557 | out->output_device = CC_OUT_USE_ANDROID; |
1558 | } |
1559 | set_Hardware_resample(5); |
1560 | return 0; |
1561 | } |
1562 | |
1563 | int set_output_record_enable(int enable) { |
1564 | if (enable == 0) { |
1565 | output_record_enable = 0; |
1566 | ALOGI("%s, set output record disable!\n", __FUNCTION__); |
1567 | } else if (enable == 1) { |
1568 | output_record_enable = 1; |
1569 | ALOGI("%s, set output record enable\n", __FUNCTION__); |
1570 | } else { |
1571 | ALOGE("%s, invalid setting!\n", __FUNCTION__); |
1572 | } |
1573 | return 0; |
1574 | } |
1575 | |
1576 | static int check_audio_type(struct aml_stream_out *out) { |
1577 | audioin_type = get_channel_status(); |
1578 | if (audioin_type == MUTE) |
1579 | return MUTE; |
1580 | spdif_audio_type = audioin_type; |
1581 | if (audioin_type > LPCM && omx_started == 0) { |
1582 | raw_data_counter++; |
1583 | } |
1584 | if (audioin_type == LPCM && omx_started == 1) { |
1585 | pcm_data_counter++; |
1586 | } |
1587 | if (raw_data_counter >= 1 && omx_started == 0) { |
1588 | ALOGI("%s, audio type is changed to RAW data input!,type %d\n", __FUNCTION__,audioin_type); |
1589 | set_rawdata_in_enable(out); |
1590 | omx_started = 1; |
1591 | raw_data_counter = 0; |
1592 | } else if (pcm_data_counter >= 1 && omx_started == 1) { |
1593 | ALOGI("%s, audio type is changed to PCM data input!,type %d\n", __FUNCTION__,audioin_type); |
1594 | set_rawdata_in_disable(out); |
1595 | omx_started = 0; |
1596 | pcm_data_counter = 0; |
1597 | } |
1598 | /* |
1599 | if omx ddp decoder has been started, but user configure pcm ->raw output |
1600 | we need reset decoder to enable decoder to dd/dd+ converter |
1601 | */ |
1602 | else if (omx_started == 1) { |
1603 | int digtal_out = amsysfs_get_sysfs_int("/sys/class/audiodsp/digital_raw"); |
1604 | int need_reset_config = 0; |
1605 | if ((audioin_type == DTS ||audioin_type == EAC3) && digtal_out != digital_raw_enable) { |
1606 | ALOGI("DD+ passthrough flag changed from %d to %d\n",digital_raw_enable,digtal_out); |
1607 | need_reset_config = 1; |
1608 | } |
1609 | else if (digtal_out > 0 && digital_raw_enable == 0) { |
1610 | ALOGI("PCM output changed to RAW pass through\n"); |
1611 | need_reset_config = 1; |
1612 | } |
1613 | if (need_reset_config) { |
1614 | ALOGI("pcm to pass through,decoder to reset \n"); |
1615 | set_rawdata_in_disable(out); |
1616 | set_rawdata_in_enable(out); |
1617 | //omx_started = 0; |
1618 | } |
1619 | } |
1620 | return 0; |
1621 | } |
1622 | |
1623 | static int audio_effect_process(short* buffer, int frame_size) { |
1624 | int output_size = frame_size << 2; |
1625 | if (gpAmlDevice->has_SRS_lib) { |
1626 | output_size = srs_process(buffer, buffer, frame_size); |
1627 | } |
1628 | if (gpAmlDevice->has_Virtualizer) { |
1629 | Virtualizer_process(buffer, buffer, frame_size); |
1630 | } |
1631 | if (gpAmlDevice->has_EQ_lib) { |
1632 | HPEQ_process(buffer, buffer, frame_size); |
1633 | } |
1634 | if (gpAmlDevice->has_aml_IIR_lib) { |
1635 | short *ptr = buffer; |
1636 | short data; |
1637 | int i; |
1638 | for (i = 0; i < frame_size; i++) { |
1639 | data = (short)aml_IIR_process((int)(*ptr), 0); |
1640 | *ptr++ = data; |
1641 | data = (short)aml_IIR_process((int)(*ptr), 1); |
1642 | *ptr++ = data; |
1643 | } |
1644 | } |
1645 | return output_size; |
1646 | } |
1647 | |
1648 | static int set_delay(struct aml_stream_in *in, int frame_size) { |
1649 | unsigned char *buffer_ptr = NULL; |
1650 | int delay_buffer_size = in->delay_time * 192; |
1651 | int buffer_size = delay_buffer_size + frame_size; |
1652 | |
1653 | if (in->delay_buf.size < buffer_size) { |
1654 | in->delay_buf.start_add = (char *)realloc( |
1655 | in->delay_buf.start_add, buffer_size * sizeof(char)); |
1656 | if (!in->delay_buf.start_add) { |
1657 | ALOGE("realloc delay buffer failed\n"); |
1658 | return -1; |
1659 | } |
1660 | memset(in->delay_buf.start_add, 0, in->delay_buf.size); |
1661 | in->delay_buf.size = buffer_size; |
1662 | in->delay_buf.rd = in->delay_buf.start_add; |
1663 | in->delay_buf.wr = in->delay_buf.start_add + delay_buffer_size; |
1664 | ALOGI("realloc delay buffer size %d byte\n", buffer_size); |
1665 | } |
1666 | |
1667 | if (in->last_delay_time != in->delay_time) { |
1668 | in->delay_buf.wr = in->delay_buf.rd + delay_buffer_size; |
1669 | if (in->delay_buf.wr >= (in->delay_buf.start_add + in->delay_buf.size)) |
1670 | in->delay_buf.wr -= in->delay_buf.size; |
1671 | in->last_delay_time = in->delay_time; |
1672 | } |
1673 | |
1674 | write_to_buffer(in->delay_buf.wr, in->temp_buffer, frame_size, |
1675 | in->delay_buf.start_add, in->delay_buf.size); |
1676 | in->delay_buf.wr = update_pointer(in->delay_buf.wr, frame_size, |
1677 | in->delay_buf.start_add, in->delay_buf.size); |
1678 | |
1679 | read_from_buffer(in->delay_buf.rd, in->temp_buffer, frame_size, |
1680 | in->delay_buf.start_add, in->delay_buf.size); |
1681 | in->delay_buf.rd = update_pointer(in->delay_buf.rd, frame_size, |
1682 | in->delay_buf.start_add, in->delay_buf.size); |
1683 | |
1684 | return 0; |
1685 | } |
1686 | |
1687 | static void* aml_audio_threadloop(void *data __unused) { |
1688 | struct aml_stream_in *in = NULL; |
1689 | struct aml_stream_out *out = NULL; |
1690 | int output_size = 0; |
1691 | int i = 0, ret; |
1692 | |
1693 | if (gpAmlDevice == NULL) { |
1694 | ALOGE("%s, gpAmlDevice is NULL\n", __FUNCTION__); |
1695 | return ((void *) 0); |
1696 | } |
1697 | |
1698 | in = &gpAmlDevice->in; |
1699 | out = &gpAmlDevice->out; |
1700 | |
1701 | gUSBCheckLastFlag = 0; |
1702 | gUSBCheckFlag = 0; |
1703 | |
1704 | gpAmlDevice->aml_Audio_ThreadExecFlag = 1; |
1705 | prctl(PR_SET_NAME, (unsigned long)"aml_TV_audio"); |
1706 | ret = aml_device_init(gpAmlDevice); |
1707 | if (ret < 0) { |
1708 | gpAmlDevice->aml_Audio_ThreadExecFlag = 0; |
1709 | ALOGE("%s, Devices fail opened!\n", __FUNCTION__); |
1710 | return NULL; |
1711 | } |
1712 | |
1713 | while (gpAmlDevice != NULL && gpAmlDevice->aml_Audio_ThreadTurnOnFlag) { |
1714 | //exit threadloop |
1715 | if (gpAmlDevice->aml_Audio_ThreadTurnOnFlag == 0) { |
1716 | ALOGD("%s, aml_Audio_ThreadTurnOnFlag is 0 break now.\n", |
1717 | __FUNCTION__); |
1718 | break; |
1719 | } |
1720 | if (GetWriteSpace((char *) in->write_buffer, (char *) out->read_buffer, |
1721 | TEMP_BUFFER_SIZE) > in->max_bytes) { |
1722 | output_size = alsa_in_read(in, in->temp_buffer, |
1723 | in->config.period_size * 4); |
1724 | if (output_size < 0) { |
1725 | //ALOGE("%s, alsa_in_read fail!\n", __FUNCTION__); |
1726 | } else { |
1727 | if (check_audio_type(out) == MUTE) |
1728 | memset((char *) in->temp_buffer, 0, output_size); |
1729 | if (in->delay_time != 0 && get_output_deviceID() == 0) { |
1730 | set_delay(in, output_size); |
1731 | } |
1732 | write_to_buffer((char *) in->write_buffer, |
1733 | (char *) in->temp_buffer, output_size, |
1734 | (char *) start_temp_buffer, TEMP_BUFFER_SIZE); |
1735 | in->write_buffer = update_pointer((char *) in->write_buffer, |
1736 | output_size, (char *) start_temp_buffer, |
1737 | TEMP_BUFFER_SIZE); |
1738 | } |
1739 | |
1740 | } |
1741 | |
1742 | USB_check(out); |
1743 | |
1744 | if (GetReadSpace((char *) in->write_buffer, (char *) out->read_buffer, |
1745 | TEMP_BUFFER_SIZE) > pcm_config_out.period_size << 2) { |
1746 | read_from_buffer((char *) out->read_buffer, |
1747 | (char *) out->temp_buffer, pcm_config_out.period_size << 2, |
1748 | (char *) start_temp_buffer, TEMP_BUFFER_SIZE); |
1749 | |
1750 | output_size = pcm_config_out.period_size << 2; |
1751 | if (gpAmlDevice->out.output_device == CC_OUT_USE_ALSA) { |
1752 | output_size = alsa_out_write(out, out->temp_buffer, |
1753 | output_size); |
1754 | } else if (gpAmlDevice->out.output_device == CC_OUT_USE_AMAUDIO) { |
1755 | output_size = amaudio_out_write(out, out->temp_buffer, |
1756 | output_size); |
1757 | if (output_size < 0) { |
1758 | amaudio_out_close(out); |
1759 | set_output_deviceID(MODEAMAUDIO); |
1760 | amaudio_out_open(out); |
1761 | reset_amaudio(out, 4096); |
1762 | } |
1763 | if (output_record_enable == 1) { |
1764 | buffer_write(&android_out_buffer, out->temp_buffer, |
1765 | output_size); |
1766 | } |
1767 | } else if (gpAmlDevice->out.output_device == CC_OUT_USE_ANDROID) { |
1768 | output_size = buffer_write(&android_out_buffer, |
1769 | out->temp_buffer, output_size); |
1770 | if (output_size < 0) { |
1771 | usleep(200*1000); |
1772 | } |
1773 | } |
1774 | |
1775 | if (output_size > 0) { |
1776 | out->read_buffer = update_pointer((char *) out->read_buffer, |
1777 | output_size, (char *) start_temp_buffer, |
1778 | TEMP_BUFFER_SIZE); |
1779 | DoDumpData(out->temp_buffer, output_size, |
1780 | CC_DUMP_SRC_TYPE_OUTPUT); |
1781 | memset(out->temp_buffer, 0, output_size); |
1782 | } |
1783 | } |
1784 | } |
1785 | |
1786 | if (gpAmlDevice != NULL) { |
1787 | gpAmlDevice->aml_Audio_ThreadTurnOnFlag = 0; |
1788 | ALOGD("%s, set aml_Audio_ThreadTurnOnFlag as 0.\n", __FUNCTION__); |
1789 | gpAmlDevice->aml_Audio_ThreadExecFlag = 0; |
1790 | ALOGD("%s, set aml_Audio_ThreadExecFlag as 0.\n", __FUNCTION__); |
1791 | } |
1792 | |
1793 | if (gpAmlDevice != NULL) { |
1794 | aml_device_close(gpAmlDevice); |
1795 | } |
1796 | |
1797 | ALOGD("%s, exiting...\n", __FUNCTION__); |
1798 | return ((void *) 0); |
1799 | } |
1800 | |
1801 | static int clrDevice(struct aml_dev *device) { |
1802 | memset((void *) device, 0, sizeof(struct aml_dev)); |
1803 | |
1804 | device->in.config.channels = 2; |
1805 | device->in.config.rate = DEFAULT_IN_SAMPLE_RATE; |
1806 | device->in.config.period_size = CAPTURE_PERIOD_SIZE; |
1807 | device->in.config.period_count = CAPTURE_PERIOD_COUNT; |
1808 | device->in.config.format = PCM_FORMAT_S16_LE; |
1809 | |
1810 | device->out.config.channels = 2; |
1811 | device->out.config.rate = DEFAULT_OUT_SAMPLE_RATE; |
1812 | device->out.config.period_size = PLAYBACK_PERIOD_SIZE; |
1813 | device->out.config.period_count = PLAYBACK_PERIOD_COUNT; |
1814 | device->out.config.format = PCM_FORMAT_S16_LE; |
1815 | |
1816 | return 0; |
1817 | } |
1818 | |
1819 | int aml_audio_open(unsigned int sr, int input_device, int output_device) { |
1820 | pthread_attr_t attr; |
1821 | struct sched_param param; |
1822 | int ret; |
1823 | |
1824 | ALOGD("%s, sr = %d, input_device = %d, output_device = %d\n", |
1825 | __FUNCTION__, sr, input_device, output_device); |
1826 | |
1827 | aml_audio_close(); |
1828 | |
1829 | pthread_mutex_lock(&amaudio_dev_op_mutex); |
1830 | |
1831 | gpAmlDevice = &gmAmlDevice; |
1832 | clrDevice(gpAmlDevice); |
1833 | |
1834 | ret = set_input_stream_sample_rate(sr, &gpAmlDevice->in); |
1835 | if (ret < 0) { |
1836 | ALOGE("%s, set_input_stream_sample_rate fail!\n", __FUNCTION__); |
1837 | clrDevice(gpAmlDevice); |
1838 | gpAmlDevice = NULL; |
1839 | pthread_mutex_unlock(&amaudio_dev_op_mutex); |
1840 | return -1; |
1841 | } |
1842 | |
1843 | gpAmlDevice->out.output_device = output_device; |
1844 | gpAmlDevice->out.user_set_device = output_device; |
1845 | if (gpAmlDevice->out.user_set_device == CC_OUT_USE_ALSA) { |
1846 | ALOGD("%s,Use tinyalsa as output device!\n", __FUNCTION__); |
1847 | } else if (gpAmlDevice->out.user_set_device == CC_OUT_USE_AMAUDIO) { |
1848 | ALOGD("%s, Use amlogic amaudio as output device!\n", __FUNCTION__); |
1849 | } else if (gpAmlDevice->out.user_set_device == CC_OUT_USE_ANDROID) { |
1850 | ALOGD("%s, Use amlogic android as output device!\n", __FUNCTION__); |
1851 | } else { |
1852 | ALOGE("%s, Unkown output device, use default amaudio\n", __FUNCTION__); |
1853 | gpAmlDevice->out.user_set_device = CC_OUT_USE_AMAUDIO; |
1854 | } |
1855 | |
1856 | ret = set_input_device(input_device); |
1857 | if (ret < 0) { |
1858 | ALOGE("Fail to set input device for HW resample!\n"); |
1859 | } |
1860 | |
1861 | gpAmlDevice->in.device = get_aml_device(input_device); |
1862 | |
1863 | pthread_attr_init(&attr); |
1864 | pthread_attr_setschedpolicy(&attr, SCHED_RR); |
1865 | param.sched_priority = sched_get_priority_max(SCHED_RR); |
1866 | /*pthread_attr_setschedpolicy(&attr, SCHED_FIFO); |
1867 | param.sched_priority = sched_get_priority_max(SCHED_FIFO); |
1868 | ALOGD("%s, aml_audio thread has %d priority!\n", |
1869 | __FUNCTION__, param.sched_priority);*/ |
1870 | pthread_attr_setschedparam(&attr, ¶m); |
1871 | gpAmlDevice->aml_Audio_ThreadTurnOnFlag = 1; |
1872 | gpAmlDevice->aml_Audio_ThreadExecFlag = 0; |
1873 | ret = pthread_create(&gpAmlDevice->aml_Audio_ThreadID, &attr, |
1874 | &aml_audio_threadloop, NULL); |
1875 | pthread_attr_destroy(&attr); |
1876 | if (ret != 0) { |
1877 | ALOGE("%s, Create thread fail!\n", __FUNCTION__); |
1878 | aml_device_close(gpAmlDevice); |
1879 | clrDevice(gpAmlDevice); |
1880 | gpAmlDevice = NULL; |
1881 | pthread_mutex_unlock(&amaudio_dev_op_mutex); |
1882 | return -1; |
1883 | } |
1884 | |
1885 | creat_pthread_for_android_check(&gpAmlDevice->android_check_ThreadID); |
1886 | |
1887 | pthread_mutex_unlock(&amaudio_dev_op_mutex); |
1888 | |
1889 | ALOGD("%s, exiting...\n", __FUNCTION__); |
1890 | return 0; |
1891 | } |
1892 | |
1893 | int aml_audio_close(void) { |
1894 | int i = 0, tmp_timeout_count = 1000; |
1895 | |
1896 | ALOGD("%s, gpAmlDevice = %p\n", __FUNCTION__, gpAmlDevice); |
1897 | |
1898 | pthread_mutex_lock(&amaudio_dev_op_mutex); |
1899 | |
1900 | if (gpAmlDevice != NULL) { |
1901 | gpAmlDevice->aml_Audio_ThreadTurnOnFlag = 0; |
1902 | ALOGD("%s, set aml_Audio_ThreadTurnOnFlag as 0.\n", __FUNCTION__); |
1903 | while (1) { |
1904 | if (gpAmlDevice->aml_Audio_ThreadExecFlag == 0) { |
1905 | break; |
1906 | } |
1907 | if (i >= tmp_timeout_count) { |
1908 | break; |
1909 | } |
1910 | i++; |
1911 | usleep(10 * 1000); |
1912 | } |
1913 | |
1914 | if (i >= tmp_timeout_count) { |
1915 | ALOGE("%s, we have try %d times, but the aml audio thread's exec flag is still(%d)!!!\n", |
1916 | __FUNCTION__, tmp_timeout_count, |
1917 | gpAmlDevice->aml_Audio_ThreadExecFlag); |
1918 | } else { |
1919 | ALOGD("%s, kill aml audio thread success after try %d times.\n", |
1920 | __FUNCTION__, i); |
1921 | } |
1922 | |
1923 | pthread_join(gpAmlDevice->aml_Audio_ThreadID, NULL); |
1924 | gpAmlDevice->aml_Audio_ThreadID = 0; |
1925 | |
1926 | exit_pthread_for_android_check(gpAmlDevice->android_check_ThreadID); |
1927 | gpAmlDevice->android_check_ThreadID = 0; |
1928 | |
1929 | clrDevice(gpAmlDevice); |
1930 | gpAmlDevice = NULL; |
1931 | |
1932 | ALOGD("%s, aml audio close success.\n", __FUNCTION__); |
1933 | } |
1934 | |
1935 | pthread_mutex_unlock(&amaudio_dev_op_mutex); |
1936 | return 0; |
1937 | } |
1938 | |
1939 | int check_input_stream_sr(unsigned int sr) { |
1940 | if (sr >= 8000 && sr <= 48000) { |
1941 | return 0; |
1942 | } |
1943 | return -1; |
1944 | } |
1945 | |
1946 | int set_output_mode(int mode) { |
1947 | if (gpAmlDevice == NULL) { |
1948 | ALOGE("%s, aml audio is not open, must open it first!\n", __FUNCTION__); |
1949 | return -1; |
1950 | } |
1951 | |
1952 | if (mode < CC_OUT_MODE_DIRECT || mode > CC_OUT_MODE_DIRECT_MIX) { |
1953 | ALOGE("%s, mode error: mode = %d!\n", __FUNCTION__, mode); |
1954 | return -1; |
1955 | } |
1956 | |
1957 | int OutHandle = gpAmlDevice->out.amAudio_OutHandle; |
1958 | if (OutHandle < 0) { |
1959 | ALOGE("%s, amaudio out handle error!\n", __FUNCTION__); |
1960 | return -1; |
1961 | } |
1962 | |
1963 | pthread_mutex_lock(&gpAmlDevice->out.lock); |
1964 | ioctl(OutHandle, AMAUDIO_IOC_AUDIO_OUT_MODE, mode); |
1965 | pthread_mutex_unlock(&gpAmlDevice->out.lock); |
1966 | return 0; |
1967 | } |
1968 | |
1969 | int set_music_gain(int gain) { |
1970 | if (gpAmlDevice == NULL) { |
1971 | ALOGE("%s, aml audio is not open, must open it first!\n", __FUNCTION__); |
1972 | return -1; |
1973 | } |
1974 | |
1975 | int OutHandle = gpAmlDevice->out.amAudio_OutHandle; |
1976 | if (OutHandle < 0) { |
1977 | ALOGE("%s, amaudio out handle error!\n", __FUNCTION__); |
1978 | return -1; |
1979 | } |
1980 | |
1981 | pthread_mutex_lock(&gpAmlDevice->out.lock); |
1982 | if (gain > 256) { |
1983 | gain = 256; |
1984 | } |
1985 | if (gain < 0) { |
1986 | gain = 0; |
1987 | } |
1988 | ioctl(OutHandle, AMAUDIO_IOC_MUSIC_GAIN, gain); |
1989 | ALOGD("%s, music gain :%d!\n", __FUNCTION__, gain); |
1990 | pthread_mutex_unlock(&gpAmlDevice->out.lock); |
1991 | return 0; |
1992 | } |
1993 | |
1994 | int set_left_gain(int left_gain) { |
1995 | if (gpAmlDevice == NULL) { |
1996 | ALOGE("%s, aml audio is not open, must open it first!\n", __FUNCTION__); |
1997 | return -1; |
1998 | } |
1999 | |
2000 | int OutHandle = gpAmlDevice->out.amAudio_OutHandle; |
2001 | if (OutHandle < 0) { |
2002 | ALOGE("%s, amaudio out handle error!\n", __FUNCTION__); |
2003 | return -1; |
2004 | } |
2005 | pthread_mutex_lock(&gpAmlDevice->out.lock); |
2006 | if (left_gain > 256) { |
2007 | left_gain = 256; |
2008 | } |
2009 | if (left_gain < 0) { |
2010 | left_gain = 0; |
2011 | } |
2012 | ioctl(OutHandle, AMAUDIO_IOC_MIC_LEFT_GAIN, left_gain); |
2013 | ALOGD("%s, left mic gain :%d!\n", __FUNCTION__, left_gain); |
2014 | pthread_mutex_unlock(&gpAmlDevice->out.lock); |
2015 | return 0; |
2016 | } |
2017 | |
2018 | int set_right_gain(int right_gain) { |
2019 | if (gpAmlDevice == NULL) { |
2020 | ALOGE("%s, aml audio is not open, must open it first!\n", __FUNCTION__); |
2021 | return -1; |
2022 | } |
2023 | int OutHandle = gpAmlDevice->out.amAudio_OutHandle; |
2024 | if (OutHandle < 0) { |
2025 | ALOGE("%s, amaudio out handle error!\n", __FUNCTION__); |
2026 | return -1; |
2027 | } |
2028 | pthread_mutex_lock(&gpAmlDevice->out.lock); |
2029 | if (right_gain > 256) { |
2030 | right_gain = 256; |
2031 | } |
2032 | if (right_gain < 0) { |
2033 | right_gain = 0; |
2034 | } |
2035 | ioctl(OutHandle, AMAUDIO_IOC_MIC_RIGHT_GAIN, right_gain); |
2036 | ALOGD("%s, right mic gain :%d!\n", __FUNCTION__, right_gain); |
2037 | pthread_mutex_unlock(&gpAmlDevice->out.lock); |
2038 | return 0; |
2039 | } |
2040 | |
2041 | int set_audio_delay(int delay_ms) { |
2042 | gpAmlDevice->in.delay_time = delay_ms; |
2043 | ALOGI("Set audio delay time %d ms!\n", delay_ms); |
2044 | return 0; |
2045 | } |
2046 | |
2047 | int get_audio_delay(void) { |
2048 | return gpAmlDevice->in.delay_time; |
2049 | } |
2050 | |
2051 | int SetDumpDataFlag(int tmp_flag) { |
2052 | int tmp_val; |
2053 | tmp_val = gDumpDataFlag; |
2054 | gDumpDataFlag = tmp_flag; |
2055 | return tmp_val; |
2056 | } |
2057 | |
2058 | int GetDumpDataFlag(void) { |
2059 | int tmp_val = 0; |
2060 | tmp_val = gDumpDataFlag; |
2061 | return tmp_val; |
2062 | } |
2063 | |
2064 | static void DoDumpData(void *data_buf, int size, int aud_src_type) { |
2065 | int tmp_type = 0; |
2066 | char prop_value[PROPERTY_VALUE_MAX] = { 0 }; |
2067 | char file_path_01[PROPERTY_VALUE_MAX] = { 0 }; |
2068 | char file_path_02[PROPERTY_VALUE_MAX] = { 0 }; |
2069 | |
2070 | if (GetDumpDataFlag() == 0) { |
2071 | return; |
2072 | } |
2073 | |
2074 | memset(prop_value, '\0', PROPERTY_VALUE_MAX); |
2075 | property_get("audio.dumpdata.en", prop_value, "null"); |
2076 | if (strcasecmp(prop_value, "null") == 0 |
2077 | || strcasecmp(prop_value, "0") == 0) { |
2078 | if (gDumpDataFd1 >= 0) { |
2079 | close(gDumpDataFd1); |
2080 | gDumpDataFd1 = -1; |
2081 | } |
2082 | if (gDumpDataFd2 >= 0) { |
2083 | close(gDumpDataFd2); |
2084 | gDumpDataFd2 = -1; |
2085 | } |
2086 | |
2087 | return; |
2088 | } |
2089 | |
2090 | tmp_type = CC_DUMP_SRC_TYPE_INPUT; |
2091 | property_get("audio.dumpdata.src", prop_value, "null"); |
2092 | if (strcasecmp(prop_value, "null") == 0 |
2093 | || strcasecmp(prop_value, "input") == 0) { |
2094 | tmp_type = CC_DUMP_SRC_TYPE_INPUT; |
2095 | } else if (strcasecmp(prop_value, "output") == 0) { |
2096 | tmp_type = CC_DUMP_SRC_TYPE_OUTPUT; |
2097 | } else if (strcasecmp(prop_value, "input,output") == 0) { |
2098 | tmp_type = CC_DUMP_SRC_TYPE_IN_OUT; |
2099 | } else if (strcasecmp(prop_value, "output,input") == 0) { |
2100 | tmp_type = CC_DUMP_SRC_TYPE_OUT_IN; |
2101 | } |
2102 | |
2103 | if (tmp_type == CC_DUMP_SRC_TYPE_INPUT |
2104 | || tmp_type == CC_DUMP_SRC_TYPE_OUTPUT) { |
2105 | if (tmp_type != aud_src_type) { |
2106 | return; |
2107 | } |
2108 | } |
2109 | |
2110 | memset(file_path_01, '\0', PROPERTY_VALUE_MAX); |
2111 | property_get("audio.dumpdata.path", file_path_01, "null"); |
2112 | if (strcasecmp(file_path_01, "null") == 0) { |
2113 | file_path_01[0] = '\0'; |
2114 | } |
2115 | |
2116 | if (tmp_type == CC_DUMP_SRC_TYPE_IN_OUT |
2117 | || tmp_type == CC_DUMP_SRC_TYPE_OUT_IN) { |
2118 | memset(file_path_02, '\0', PROPERTY_VALUE_MAX); |
2119 | property_get("audio.dumpdata.path2", file_path_02, "null"); |
2120 | if (strcasecmp(file_path_02, "null") == 0) { |
2121 | file_path_02[0] = '\0'; |
2122 | } |
2123 | } |
2124 | |
2125 | if (gDumpDataFd1 < 0 && file_path_01[0] != '\0') { |
2126 | if (access(file_path_01, 0) == 0) { |
2127 | gDumpDataFd1 = open(file_path_01, O_RDWR | O_SYNC); |
2128 | if (gDumpDataFd1 < 0) { |
2129 | ALOGE("%s, Open device file \"%s\" error: %s.\n", __FUNCTION__, |
2130 | file_path_01, strerror(errno)); |
2131 | } |
2132 | } else { |
2133 | gDumpDataFd1 = open(file_path_01, O_WRONLY | O_CREAT | O_EXCL, |
2134 | S_IRUSR | S_IWUSR); |
2135 | if (gDumpDataFd1 < 0) { |
2136 | ALOGE("%s, Create device file \"%s\" error: %s.\n", |
2137 | __FUNCTION__, file_path_01, strerror(errno)); |
2138 | } |
2139 | } |
2140 | } |
2141 | |
2142 | if (gDumpDataFd2 < 0 && file_path_02[0] != '\0' |
2143 | && (tmp_type == CC_DUMP_SRC_TYPE_IN_OUT |
2144 | || tmp_type == CC_DUMP_SRC_TYPE_OUT_IN)) { |
2145 | if (access(file_path_02, 0) == 0) { |
2146 | gDumpDataFd2 = open(file_path_02, O_RDWR | O_SYNC); |
2147 | if (gDumpDataFd2 < 0) { |
2148 | ALOGE("%s, Open device file \"%s\" error: %s.\n", __FUNCTION__, |
2149 | file_path_02, strerror(errno)); |
2150 | } |
2151 | } else { |
2152 | gDumpDataFd2 = open(file_path_02, O_WRONLY | O_CREAT | O_EXCL, |
2153 | S_IRUSR | S_IWUSR); |
2154 | if (gDumpDataFd2 < 0) { |
2155 | ALOGE("%s, Create device file \"%s\" error: %s.\n", |
2156 | __FUNCTION__, file_path_02, strerror(errno)); |
2157 | } |
2158 | } |
2159 | } |
2160 | |
2161 | if (tmp_type == CC_DUMP_SRC_TYPE_IN_OUT) { |
2162 | if (aud_src_type == CC_DUMP_SRC_TYPE_INPUT && gDumpDataFd1 >= 0) { |
2163 | write(gDumpDataFd1, data_buf, size); |
2164 | } else if (aud_src_type == CC_DUMP_SRC_TYPE_OUTPUT |
2165 | && gDumpDataFd2 >= 0) { |
2166 | write(gDumpDataFd2, data_buf, size); |
2167 | } |
2168 | } else if (tmp_type == CC_DUMP_SRC_TYPE_OUT_IN) { |
2169 | if (aud_src_type == CC_DUMP_SRC_TYPE_OUTPUT && gDumpDataFd1 >= 0) { |
2170 | write(gDumpDataFd1, data_buf, size); |
2171 | } else if (aud_src_type == CC_DUMP_SRC_TYPE_INPUT |
2172 | && gDumpDataFd2 >= 0) { |
2173 | write(gDumpDataFd2, data_buf, size); |
2174 | } |
2175 | } else { |
2176 | if (gDumpDataFd1 >= 0) { |
2177 | write(gDumpDataFd1, data_buf, size); |
2178 | } |
2179 | } |
2180 | } |
2181 | |
2182 | int aml_audio_set_pregain(float gain) |
2183 | { |
2184 | ALOGD("%s, pre-gain = %f dB\n", __FUNCTION__, gain); |
2185 | |
2186 | pthread_mutex_lock(&amaudio_dev_op_mutex); |
2187 | |
2188 | if (gpAmlDevice != NULL) { |
2189 | gpAmlDevice->in.pre_gain = powf(10, gain/20); |
2190 | } |
2191 | |
2192 | pthread_mutex_unlock(&amaudio_dev_op_mutex); |
2193 | |
2194 | return 0; |
2195 | } |
2196 | |
2197 | int aml_audio_get_pregain(float *gain) |
2198 | { |
2199 | if (gpAmlDevice != NULL) { |
2200 | *gain = 20*log10f(gpAmlDevice->in.pre_gain); |
2201 | return 0; |
2202 | } |
2203 | |
2204 | ALOGE("%s, no active gpAmlDevice!\n", __FUNCTION__); |
2205 | return -1; |
2206 | } |
2207 | |
2208 | int aml_audio_set_pre_mute(uint mute) |
2209 | { |
2210 | ALOGD("%s, mute = %d\n", __FUNCTION__, mute); |
2211 | |
2212 | pthread_mutex_lock(&amaudio_dev_op_mutex); |
2213 | |
2214 | if (gpAmlDevice != NULL) { |
2215 | gpAmlDevice->in.pre_mute = mute; |
2216 | } |
2217 | |
2218 | pthread_mutex_unlock(&amaudio_dev_op_mutex); |
2219 | |
2220 | return 0; |
2221 | } |
2222 | |
2223 | int aml_audio_get_pre_mute(uint *mute) |
2224 | { |
2225 | if (gpAmlDevice != NULL) { |
2226 | *mute = gpAmlDevice->in.pre_mute; |
2227 | return 0; |
2228 | } |
2229 | |
2230 | ALOGE("%s, no active gpAmlDevice!\n", __FUNCTION__); |
2231 | return -1; |
2232 | } |
2233 |