summaryrefslogtreecommitdiff
path: root/libTVaudio/audio/android_out.cpp (plain)
blob: 406f8590e68a3af2867aaa1d5b1e52a86a30c844
1#define LOG_TAG "android_out"
2
3#include <stdio.h>
4#include <string.h>
5#include <stdlib.h>
6#include <pthread.h>
7#include <fcntl.h>
8#include <errno.h>
9#include <strings.h>
10#include <sys/ioctl.h>
11#include <cutils/log.h>
12#include <cutils/properties.h>
13#include <media/AudioTrack.h>
14
15#include "audio_usb_check.h"
16#include "android_out.h"
17#include "aml_audio.h"
18#include "DDP_media_source.h"
19
20using namespace android;
21
22static AudioTrack *glpTracker = NULL;
23static AudioTrack *glpTracker_raw = NULL;
24
25static sp<AudioTrack> gmpAudioTracker;
26static sp<AudioTrack> gmpAudioTracker_raw;
27
28extern struct circle_buffer android_out_buffer;
29extern struct circle_buffer DDP_out_buffer;
30extern struct circle_buffer DD_out_buffer;
31extern int output_record_enable;
32extern int spdif_audio_type;
33
34int I2S_state = 0;
35static int raw_start_flag = 0;
36static int mute_raw_data_size = 0;
37static audio_format_t last_aformat = AUDIO_FORMAT_AC3;
38//static int last_raw_flag = 0;
39static int RawAudioTrackRelease(void);
40static int RawAudioTrackInit(audio_format_t aformat,int sr);
41static int amsysfs_set_sysfs_int(const char *path, int val) {
42 int fd;
43 int bytes;
44 char bcmd[16];
45 fd = open(path, O_CREAT | O_RDWR | O_TRUNC, 0644);
46 if (fd >= 0) {
47 sprintf(bcmd, "%d", val);
48 bytes = write(fd, bcmd, strlen(bcmd));
49 close(fd);
50 return 0;
51 } else {
52 ALOGE("unable to open file %s,err: %s", path, strerror(errno));
53 }
54 return -1;
55}
56
57int amsysfs_get_sysfs_int(const char *path) {
58 int fd;
59 int val = 0;
60 char bcmd[16];
61 fd = open(path, O_RDONLY);
62 if (fd >= 0) {
63 read(fd, bcmd, sizeof(bcmd));
64 val = strtol(bcmd, NULL, 10);
65 close(fd);
66 }else {
67 ALOGE("unable to open file %s,err: %s", path, strerror(errno));
68 }
69 return val;
70}
71static void RawAudioTrackCallback(int event, void* user, void *info) {
72 AudioTrack::Buffer *buffer = static_cast<AudioTrack::Buffer *>(info);
73 int bytes_raw = 0;
74 if (event != AudioTrack::EVENT_MORE_DATA) {
75 ALOGD("%s, audio track envent = %d!\n", __FUNCTION__, event);
76 return;
77 }
78 if (buffer == NULL || buffer->size == 0) {
79 return;
80 }
81 bytes_raw = buffer_read(&DD_out_buffer,(char *) buffer->raw,
82 buffer->size);
83 //ALOGI("raw read got %d\n",bytes_raw);
84 if ( bytes_raw > 0) {
85 buffer->size = bytes_raw;
86 }
87 else
88 buffer->size = 0;
89 if (buffer->size > 0 && mute_raw_data_size < 32*1024) {
90 memset((char *)(buffer->i16),0,buffer->size);
91 mute_raw_data_size += buffer->size;
92 }
93 return;
94}
95static void AudioTrackCallback(int event, void* user, void *info) {
96 AudioTrack::Buffer *buffer = static_cast<AudioTrack::Buffer *>(info);
97
98 if (event != AudioTrack::EVENT_MORE_DATA) {
99 ALOGD("%s, audio track envent = %d!\n", __FUNCTION__, event);
100 return;
101 }
102 if (buffer == NULL || buffer->size == 0) {
103 return;
104 }
105 int bytes = 0;
106
107// code for raw data start
108 audio_format_t aformat = AUDIO_FORMAT_INVALID;
109 int user_raw_enable = amsysfs_get_sysfs_int("/sys/class/audiodsp/digital_raw");
110 //ALOGI("afmat %x,spdif_audio_type %x\n",aformat,spdif_audio_type);
111 int ddp_passth = (user_raw_enable == 2)&&(spdif_audio_type == EAC3);
112 if (user_raw_enable) {
113 if (ddp_passth) {
114 aformat = AUDIO_FORMAT_E_AC3;
115 }
116 else if (spdif_audio_type == AC3 || spdif_audio_type == EAC3 ) {
117 aformat = AUDIO_FORMAT_AC3;
118 }
119 else if (spdif_audio_type == DTS) {
120 aformat = AUDIO_FORMAT_DTS;
121 }
122 if (aformat != last_aformat && aformat != AUDIO_FORMAT_INVALID) {
123 ALOGI("raw aformat changed from %x to %x\n",last_aformat,aformat);
124 RawAudioTrackRelease();
125 if (RawAudioTrackInit(aformat,48000/*TODO*/)) {
126 ALOGE("RawAudioTrackInit failed\n");
127 return;
128 }
129 }
130 }
131// raw data end
132 if (GetOutputdevice() == MODEANDROID) { // output PCM output when PCM data in
133//raw data start
134 RawAudioTrackRelease();
135// raw data end
136 bytes = buffer_read(&android_out_buffer, (char *) buffer->raw,
137 buffer->size);
138 if (bytes < 0)
139 buffer->size = 0;
140 } else if (GetOutputdevice() == MODERAW) {
141//raw data start
142 if (user_raw_enable == 0) {
143 RawAudioTrackRelease();
144 }
145// raw date end
146 bytes = buffer_read(&DDP_out_buffer, (char *) buffer->raw,
147 buffer->size);
148 if (bytes < 0)
149 buffer->size = 0;
150 } else {
151 if (output_record_enable == 1) {
152 bytes = buffer_read(&android_out_buffer, (char *) buffer->raw,
153 buffer->size);
154 if (bytes < 0)
155 buffer->size = 0;
156 } else {
157 memset(buffer->i16, 0, buffer->size);
158 }
159 }
160
161 I2S_state += 1;
162 return;
163}
164static int RawAudioTrackRelease(void) {
165 //raw here
166 if (glpTracker_raw != NULL ) {
167 if (raw_start_flag == 1)
168 glpTracker_raw->stop();
169 raw_start_flag = 0;
170 glpTracker_raw = NULL;
171 }
172 if (gmpAudioTracker_raw != NULL ) {
173 gmpAudioTracker_raw.clear();
174 gmpAudioTracker_raw = NULL;
175 ALOGI("RawAudioTrackRelease done\n");
176 }
177
178 // raw end
179#if 0
180 if (last_raw_flag == 2) {
181 ALOGI("change back digital raw to 2 for hdmi pass through\n");
182 amsysfs_set_sysfs_int("/sys/class/audiodsp/digital_raw",2);
183 last_raw_flag = 0;
184 }
185#endif
186 last_aformat = AUDIO_FORMAT_INVALID;
187 return 0;
188}
189static int AudioTrackRelease(void) {
190 if (glpTracker != NULL ) {
191 glpTracker->stop();
192 glpTracker = NULL;
193 }
194
195 if (gmpAudioTracker != NULL ) {
196 gmpAudioTracker.clear();
197 gmpAudioTracker = NULL;
198 }
199
200 return 0;
201}
202static int RawAudioTrackInit(audio_format_t aformat,int sr)
203{
204 status_t Status;
205 int user_raw_enable = amsysfs_get_sysfs_int("/sys/class/audiodsp/digital_raw");
206 int ddp_passth = (user_raw_enable == 2)&&(spdif_audio_type == EAC3);
207 int ret;
208
209 ALOGD("%s, entering...,aformat %x,sr %d\n", __FUNCTION__,aformat,sr);
210 //raw here
211 if (gmpAudioTracker_raw != NULL) {
212 glpTracker_raw = gmpAudioTracker_raw.get();
213 } else {
214 gmpAudioTracker_raw = new AudioTrack();
215 if (gmpAudioTracker_raw == NULL) {
216 ALOGE("%s, new gmpAudioTracker_raw failed.\n", __FUNCTION__);
217 return -1;
218 }
219 glpTracker_raw = gmpAudioTracker_raw.get();
220 }
221
222 Status = glpTracker_raw->set(AUDIO_STREAM_MUSIC, sr, aformat,
223 AUDIO_CHANNEL_OUT_STEREO, 0,
224 (audio_output_flags_t)(AUDIO_OUTPUT_FLAG_DIRECT
225 | AUDIO_OUTPUT_FLAG_IEC958_NONAUDIO),
226 RawAudioTrackCallback, NULL,
227 0, 0, false, (audio_session_t)0);
228 if (Status != NO_ERROR) {
229 ALOGE("%s, AudioTrack raw set failed.\n", __FUNCTION__);
230 if (gmpAudioTracker_raw != NULL ) {
231 gmpAudioTracker_raw.clear();
232 glpTracker_raw = NULL;
233 }
234 return -1;
235 }
236 Status = glpTracker_raw->initCheck();
237 if (Status != NO_ERROR) {
238 ALOGE("%s, AudioTrack raw initCheck failed.\n", __FUNCTION__);
239 RawAudioTrackRelease();
240 return -1;
241 }
242 //raw end
243#if 0
244 int digital_raw = amsysfs_get_sysfs_int("/sys/class/audiodsp/digital_raw");
245 if (digital_raw == 2) {
246 ALOGI("change digital raw to 2 for spdif pass through\n");
247 amsysfs_set_sysfs_int("/sys/class/audiodsp/digital_raw",1);
248 last_raw_flag = 2;
249 }
250#endif
251 glpTracker_raw->start();
252 ALOGI("RawAudioTrackInit done\n");
253 last_aformat = aformat;
254 mute_raw_data_size = 0;
255 return 0;
256}
257static int AudioTrackInit(void) {
258 status_t Status;
259
260 ALOGD("%s, entering...\n", __FUNCTION__);
261
262 I2S_state = 0;
263
264 if (gmpAudioTracker != NULL) {
265 glpTracker = gmpAudioTracker.get();
266 } else {
267 gmpAudioTracker = new AudioTrack();
268 if (gmpAudioTracker == NULL) {
269 ALOGE("%s, new AudioTrack failed.\n", __FUNCTION__);
270 return -1;
271 }
272 glpTracker = gmpAudioTracker.get();
273 }
274
275 Status = glpTracker->set(AUDIO_STREAM_MUSIC, 48000, AUDIO_FORMAT_PCM_16_BIT,
276 AUDIO_CHANNEL_OUT_STEREO, 0, AUDIO_OUTPUT_FLAG_NONE,
277 AudioTrackCallback, NULL, 0, 0, false, (audio_session_t)0);
278
279 if (Status != NO_ERROR) {
280 ALOGE("%s, AudioTrack set failed.\n", __FUNCTION__);
281 if (gmpAudioTracker != NULL ) {
282 gmpAudioTracker.clear();
283 glpTracker = NULL;
284 }
285 return -1;
286 }
287
288 Status = glpTracker->initCheck();
289 if (Status != NO_ERROR) {
290 ALOGE("%s, AudioTrack initCheck failed.\n", __FUNCTION__);
291 AudioTrackRelease();
292 return -1;
293 }
294
295 glpTracker->start();
296
297 Status = glpTracker->setVolume(1.0, 1.0);
298 if (Status != NO_ERROR) {
299 ALOGE("%s, AudioTrack setVolume failed.\n", __FUNCTION__);
300 AudioTrackRelease();
301 return -1;
302 }
303 ALOGD("%s, exit...\n", __FUNCTION__);
304 return 0;//RawAudioTrackInit(AUDIO_FORMAT_AC3,48000);
305}
306
307int new_android_audiotrack(void) {
308 return AudioTrackInit();
309}
310
311int release_android_audiotrack(void) {
312 amsysfs_set_sysfs_int("/sys/class/audiodsp/digital_codec",0);
313 return AudioTrackRelease();
314}
315
316int release_raw_audio_track(void) {
317 amsysfs_set_sysfs_int("/sys/class/audiodsp/digital_codec", 0);
318 return RawAudioTrackRelease();
319}
320