blob: 6945da3be4389a021664ec7e8dfb39bd59e78548
1 | #define LOG_TAG "DTSHD_Media_Source" |
2 | |
3 | #include <stdio.h> |
4 | #include <stdarg.h> |
5 | #include <string.h> |
6 | #include <android/log.h> |
7 | #include <cutils/properties.h> |
8 | #include <pthread.h> |
9 | |
10 | //code here for sys write service |
11 | #ifdef USE_SYS_WRITE_SERVICE |
12 | #include <binder/Binder.h> |
13 | #include <binder/IServiceManager.h> |
14 | #include <utils/Atomic.h> |
15 | #include <utils/Log.h> |
16 | #include <utils/RefBase.h> |
17 | #include <utils/String8.h> |
18 | #include <utils/String16.h> |
19 | #include <utils/threads.h> |
20 | #include <unistd.h> |
21 | #if ANDROID_PLATFORM_SDK_VERSION >= 21 //5.0 |
22 | #include <systemcontrol/ISystemControlService.h> |
23 | #else |
24 | #include <systemwrite/ISystemWriteService.h> |
25 | #endif |
26 | // code end |
27 | #endif |
28 | #include <media/stagefright/SimpleDecodingSource.h> |
29 | |
30 | #include "DTSHD_media_source.h" |
31 | #include "aml_audio.h" |
32 | extern struct circle_buffer android_out_buffer; |
33 | extern struct circle_buffer DDP_out_buffer; |
34 | extern struct circle_buffer DD_out_buffer; |
35 | extern int spdif_audio_type; |
36 | namespace android { |
37 | |
38 | #ifdef USE_SYS_WRITE_SERVICE |
39 | //code here for system write service |
40 | class DeathNotifier: public IBinder::DeathRecipient |
41 | { |
42 | public: |
43 | DeathNotifier() { |
44 | } |
45 | |
46 | void binderDied(__unused const wp<IBinder>& who) { |
47 | ALOGW("system_write died!"); |
48 | } |
49 | }; |
50 | |
51 | |
52 | #if ANDROID_PLATFORM_SDK_VERSION >= 21 //5.0 |
53 | //used ISystemControlService |
54 | #define SYST_SERVICES_NAME "system_control" |
55 | #else |
56 | //used amSystemWriteService |
57 | #define ISystemControlService ISystemWriteService |
58 | #define SYST_SERVICES_NAME "system_write" |
59 | #endif |
60 | |
61 | static sp<ISystemControlService> amSystemWriteService; |
62 | static sp<DeathNotifier> amDeathNotifier; |
63 | static Mutex amLock; |
64 | static Mutex amgLock; |
65 | |
66 | const sp<ISystemControlService>& getSystemWriteServiceDts() |
67 | { |
68 | Mutex::Autolock _l(amgLock); |
69 | if (amSystemWriteService.get() == 0) { |
70 | sp<IServiceManager> sm = defaultServiceManager(); |
71 | #if 0 |
72 | sp<IBinder> binder; |
73 | do { |
74 | binder = sm->getService(String16("system_write")); |
75 | if (binder != 0) |
76 | break; |
77 | ALOGW("SystemWriteService not published, waiting..."); |
78 | usleep(500000); // 0.5 s |
79 | } while(true); |
80 | if (amDeathNotifier == NULL) { |
81 | amDeathNotifier = new DeathNotifier(); |
82 | } |
83 | binder->linkToDeath(amDeathNotifier); |
84 | amSystemWriteService = interface_cast<ISystemWriteService>(binder); |
85 | #endif |
86 | |
87 | |
88 | amSystemWriteService = interface_cast<ISystemControlService>(sm->getService(String16(SYST_SERVICES_NAME))); |
89 | |
90 | } |
91 | ALOGE_IF(amSystemWriteService==0, "no SystemWrite Service!?"); |
92 | |
93 | return amSystemWriteService; |
94 | } |
95 | void amSystemWriteSetPropertyDts(const char* key, const char* value) |
96 | { |
97 | const sp<ISystemControlService>& sws = getSystemWriteServiceDts(); |
98 | if (sws != 0) { |
99 | sws->setProperty(String16(key), String16(value)); |
100 | } |
101 | } |
102 | //code end for system write service |
103 | #endif |
104 | |
105 | //----------------------------DTS Media Source---------------------------------------------- |
106 | |
107 | static pthread_mutex_t decode_dev_op_mutex = PTHREAD_MUTEX_INITIALIZER; |
108 | static int decode_ThreadExitFlag = 0; //0:exit from thread; 1:thread looping |
109 | static int decode_ThreadStopFlag = 1; //0:start; 1: stop |
110 | static pthread_t decode_ThreadID = 0; |
111 | |
112 | Dtshd_Media_Source::Dtshd_Media_Source(void) { |
113 | ALOGI("[%s: %d]\n", __FUNCTION__, __LINE__); |
114 | mStarted = false; |
115 | mMeta = new MetaData; |
116 | mGroup = NULL; |
117 | mSample_rate = 0; |
118 | mChNum = 0; |
119 | mFrame_size = 0; |
120 | mStop_ReadBuf_Flag = 0; //0:start 1:stop |
121 | |
122 | mDataSource=NULL; |
123 | mBytesReaded=0; |
124 | mCurrentTimeUs=0; |
125 | bytes_readed_sum_pre=0; |
126 | bytes_readed_sum=0; |
127 | } |
128 | |
129 | Dtshd_Media_Source::~Dtshd_Media_Source() { |
130 | ALOGI("%s %d \n",__FUNCTION__,__LINE__); |
131 | if (mStarted) { |
132 | stop(); |
133 | } |
134 | } |
135 | |
136 | int Dtshd_Media_Source::GetSampleRate() { |
137 | ALOGI("[Dtshd_Media_Source::%s: %d]\n", __FUNCTION__, __LINE__); |
138 | return mSample_rate; |
139 | } |
140 | int Dtshd_Media_Source::GetChNum() { |
141 | ALOGI("[Dtshd_Media_Source::%s: %d]\n", __FUNCTION__, __LINE__); |
142 | return mChNum; |
143 | } |
144 | |
145 | int Dtshd_Media_Source::Get_Stop_ReadBuf_Flag() { |
146 | return mStop_ReadBuf_Flag; |
147 | } |
148 | |
149 | int Dtshd_Media_Source::Set_Stop_ReadBuf_Flag(int Stop) { |
150 | mStop_ReadBuf_Flag = Stop; |
151 | return 0; |
152 | } |
153 | |
154 | sp<MetaData> Dtshd_Media_Source::getFormat() { |
155 | ALOGI("[Dtshd_Media_Source::%s: %d]\n", __FUNCTION__, __LINE__); |
156 | return mMeta; |
157 | } |
158 | |
159 | status_t Dtshd_Media_Source::start(__unused MetaData *params) { |
160 | ALOGI("[Dtshd_Media_Source::%s: %d]\n", __FUNCTION__, __LINE__); |
161 | mGroup = new MediaBufferGroup; |
162 | mGroup->add_buffer(new MediaBuffer(4096*2)); |
163 | mStarted = true; |
164 | return OK; |
165 | } |
166 | |
167 | status_t Dtshd_Media_Source::stop() { |
168 | ALOGI("[Dtshd_Media_Source::%s: %d]\n", __FUNCTION__, __LINE__); |
169 | delete mGroup; |
170 | mGroup = NULL; |
171 | mStarted = false; |
172 | return OK; |
173 | } |
174 | |
175 | int Dtshd_Media_Source::MediaSourceRead_buffer(unsigned char *buffer, int size) { |
176 | int readcnt = -1; |
177 | int sleep_time = 0; |
178 | if ((mStop_ReadBuf_Flag == 1) || (decode_ThreadStopFlag == 1)) { |
179 | ALOGI("[Dtshd_Media_Source::%s] dtshd mediasource stop!\n ", __FUNCTION__); |
180 | return -1; |
181 | } |
182 | while ((readcnt < size) && (mStop_ReadBuf_Flag == 0) |
183 | && (decode_ThreadStopFlag == 0)) { |
184 | readcnt = buffer_read(&android_out_buffer, (char*) buffer, size); |
185 | //ALOGI("readcnt:%d,sleep_time:%d,size:%d",readcnt,sleep_time,size); |
186 | if (readcnt < 0) { |
187 | sleep_time++; |
188 | usleep(1000); //1ms |
189 | } |
190 | if (sleep_time > 2000) { //wait for max 1s to get audio data |
191 | ALOGE("[%s] time out to read audio buffer data! wait for 2s\n ", |
192 | __FUNCTION__); |
193 | return -1; |
194 | } |
195 | } |
196 | return readcnt; |
197 | } |
198 | status_t Dtshd_Media_Source::read(MediaBuffer **out, __unused const ReadOptions *options) { |
199 | *out = NULL; |
200 | unsigned char ptr_head[4] = { 0 }; |
201 | unsigned char ptr_head2[IEC61937_DTS_HEAD_PTR -4] = { 0 }; |
202 | int SyncFlag = 0; |
203 | int readedbytes = 0; |
204 | int i = 0 ; |
205 | if (MediaSourceRead_buffer(&ptr_head[0], 4) < 4) { |
206 | readedbytes = 4; |
207 | ALOGI("WARNING:read %d bytes failed [%s %d]!\n", readedbytes, |
208 | __FUNCTION__, __LINE__); |
209 | return ERROR_END_OF_STREAM; |
210 | } |
211 | |
212 | mFrame_size = 0; |
213 | SyncFlag = 0; |
214 | |
215 | if ((mStop_ReadBuf_Flag == 1) || (decode_ThreadStopFlag == 1)) { |
216 | ALOGI("Stop Flag is set, stop read_buf [%s %d]", __FUNCTION__, |
217 | __LINE__); |
218 | return ERROR_END_OF_STREAM; |
219 | } |
220 | if ((mStop_ReadBuf_Flag == 1) || (decode_ThreadStopFlag == 1)) { |
221 | ALOGI("Stop Flag is set, stop read_buf [%s %d]", __FUNCTION__, |
222 | __LINE__); |
223 | return ERROR_END_OF_STREAM; |
224 | } |
225 | while (!SyncFlag) { |
226 | int i =0; |
227 | //DTS_SYNCWORD_IEC61937 : 0xF8724E1F |
228 | if ((ptr_head[0] == 0x72 && ptr_head[ 1] == 0xf8 |
229 | &&ptr_head[2] == 0x1f && ptr_head[3] == 0x4e)|| |
230 | (ptr_head[0] == 0xf8 && ptr_head[1] == 0x72 |
231 | &&ptr_head[2] == 0x4e && ptr_head[3] == 0x1f)) { |
232 | |
233 | if ((mStop_ReadBuf_Flag == 1) || (decode_ThreadStopFlag == 1)) { |
234 | ALOGI("Stop Flag is set, stop read_buf [%s %d]", |
235 | __FUNCTION__, __LINE__); |
236 | return ERROR_END_OF_STREAM; |
237 | } |
238 | SyncFlag = 1; |
239 | } |
240 | if (SyncFlag == 0) { |
241 | ptr_head[0] =ptr_head[1]; |
242 | ptr_head[1] =ptr_head[2]; |
243 | ptr_head[2] =ptr_head[3]; |
244 | if (MediaSourceRead_buffer(&ptr_head[3], 1) < 1) { |
245 | readedbytes = 1; |
246 | ALOGI("WARNING: read %d bytes failed [%s %d]!\n", |
247 | readedbytes, __FUNCTION__, __LINE__); |
248 | return ERROR_END_OF_STREAM; |
249 | } |
250 | |
251 | if ((mStop_ReadBuf_Flag == 1) || (decode_ThreadStopFlag == 1)) { |
252 | ALOGI("Stop Flag is set, stop read_buf [%s %d]", __FUNCTION__, |
253 | __LINE__); |
254 | return ERROR_END_OF_STREAM; |
255 | } |
256 | } |
257 | } |
258 | if (MediaSourceRead_buffer(&ptr_head2[0], 4) < 4) { |
259 | readedbytes = 4; |
260 | ALOGI("WARNING:read %d bytes failed [%s %d]!\n", readedbytes, |
261 | __FUNCTION__, __LINE__); |
262 | return ERROR_END_OF_STREAM; |
263 | } |
264 | //memcpy(&mFrame_size,ptr_head2+IEC61937_DTS_HEAD_PTR-6,2); |
265 | //ALOGI("mFrame_size:%d",mFrame_size); |
266 | //memcpy(&mFrame_size,ptr_head2+IEC61937_DTS_HEAD_PTR-6,2); |
267 | //ado-no lib only pack dts core data |
268 | mFrame_size = (ptr_head2[2] | ptr_head2[3] << 8)/8; |
269 | //ALOGI("mFrame_size:%d",mFrame_size); |
270 | MediaBuffer *buffer; |
271 | int8_t tmp; |
272 | unsigned char *ptr; |
273 | status_t err = mGroup->acquire_buffer(&buffer); |
274 | if (err != OK) { |
275 | return err; |
276 | } |
277 | if (MediaSourceRead_buffer( |
278 | (unsigned char*) (buffer->data()), |
279 | mFrame_size) != mFrame_size ) { |
280 | ALOGI("[%s %d]stream read failed:bytes_req/%d\n", __FUNCTION__, |
281 | __LINE__, mFrame_size); |
282 | buffer->release(); |
283 | buffer = NULL; |
284 | return ERROR_END_OF_STREAM; |
285 | } |
286 | |
287 | ptr = (unsigned char *)buffer->data(); |
288 | for (i = 0;i < mFrame_size;i = i+2 ) { |
289 | tmp = ptr[i]; |
290 | ptr[i] = ptr[i+1]; |
291 | ptr[i+1] = tmp; |
292 | } |
293 | |
294 | buffer->set_range(0, mFrame_size); |
295 | buffer->meta_data()->setInt64(kKeyTime, 0); |
296 | buffer->meta_data()->setInt32(kKeyIsSyncFrame, 1); |
297 | |
298 | *out = buffer; |
299 | return OK; |
300 | } |
301 | |
302 | |
303 | //-------------------------------OMX codec------------------------------------------------ |
304 | |
305 | const char *MEDIA_MIMETYPE_AUDIO_DTS = "audio/dtshd"; |
306 | Aml_OMX_DtsCodec::Aml_OMX_DtsCodec(void) { |
307 | ALOGI("[Aml_OMX_DtsCodec::%s: %d],atype %d\n", __FUNCTION__, __LINE__,spdif_audio_type); |
308 | m_codec = NULL; |
309 | status_t m_OMXClientConnectStatus = m_OMXClient.connect(); |
310 | lock_init(); |
311 | locked(); |
312 | buf_decode_offset = 0; |
313 | buf_decode_offset_pre = 0; |
314 | if (m_OMXClientConnectStatus != OK) { |
315 | ALOGE("Err:omx client connect error\n"); |
316 | } else { |
317 | const char *mine_type = NULL; |
318 | if (spdif_audio_type == DTSHD || spdif_audio_type == DTS) |
319 | mine_type = MEDIA_MIMETYPE_AUDIO_DTS; |
320 | |
321 | m_OMXMediaSource = new Dtshd_Media_Source(); |
322 | sp < MetaData > metadata = m_OMXMediaSource->getFormat(); |
323 | metadata->setCString(kKeyMIMEType, mine_type); |
324 | m_codec = SimpleDecodingSource::Create(m_OMXMediaSource, 0, 0); |
325 | |
326 | if (m_codec != NULL) { |
327 | ALOGI("OMXCodec::Create success %s %d \n", __FUNCTION__, __LINE__); |
328 | } else { |
329 | ALOGE("Err: OMXCodec::Create failed %s %d \n", __FUNCTION__, |
330 | __LINE__); |
331 | } |
332 | } |
333 | unlocked(); |
334 | } |
335 | |
336 | Aml_OMX_DtsCodec::~Aml_OMX_DtsCodec() { |
337 | } |
338 | |
339 | status_t Aml_OMX_DtsCodec::read(unsigned char *buf, unsigned *size, int *exit) { |
340 | MediaBuffer *srcBuffer; |
341 | status_t status; |
342 | m_OMXMediaSource->Set_Stop_ReadBuf_Flag(*exit); |
343 | |
344 | if (*exit) { |
345 | ALOGI("NOTE:exit flag enabled! [%s %d] \n", __FUNCTION__, __LINE__); |
346 | *size = 0; |
347 | return OK; |
348 | } |
349 | |
350 | status = m_codec->read(&srcBuffer, NULL); |
351 | |
352 | if (srcBuffer == NULL) { |
353 | *size = 0; |
354 | return OK; |
355 | } |
356 | |
357 | *size = srcBuffer->range_length(); |
358 | |
359 | if (status == OK && (*size != 0)) { |
360 | memcpy((unsigned char *) buf, |
361 | (unsigned char *) srcBuffer->data() + srcBuffer->range_offset(), |
362 | *size); |
363 | srcBuffer->set_range(srcBuffer->range_offset() + (*size), |
364 | srcBuffer->range_length() - (*size)); |
365 | srcBuffer->meta_data()->findInt64(kKeyTime, &buf_decode_offset); |
366 | } |
367 | |
368 | if (srcBuffer->range_length() == 0) { |
369 | srcBuffer->release(); |
370 | srcBuffer = NULL; |
371 | } |
372 | |
373 | return OK; |
374 | } |
375 | |
376 | status_t Aml_OMX_DtsCodec::start() { |
377 | ALOGI("[Aml_OMX_DtsCodec::%s %d] enter!\n", __FUNCTION__, __LINE__); |
378 | status_t status = m_codec->start(); |
379 | if (status != OK) { |
380 | ALOGE("Err:OMX client can't start OMX decoder! status=%d (0x%08x)\n", |
381 | (int) status, (int) status); |
382 | m_codec.clear(); |
383 | } |
384 | return status; |
385 | } |
386 | |
387 | void Aml_OMX_DtsCodec::stop() { |
388 | ALOGI("[Aml_OMX_DtsCodec::%s %d] enter!\n", __FUNCTION__, __LINE__); |
389 | if (m_codec != NULL) { |
390 | if (m_OMXMediaSource->Get_Stop_ReadBuf_Flag()) |
391 | m_OMXMediaSource->Set_Stop_ReadBuf_Flag(1); |
392 | m_codec->pause(); |
393 | m_codec->stop(); |
394 | wp < MediaSource > tmp = m_codec; |
395 | m_codec.clear(); |
396 | while (tmp.promote() != NULL) { |
397 | ALOGI("[Aml_OMX_DtsCodec::%s %d]wait m_codec free OK!\n", __FUNCTION__, |
398 | __LINE__); |
399 | usleep(1000); |
400 | } |
401 | m_OMXClient.disconnect(); |
402 | m_OMXMediaSource.clear(); |
403 | } else |
404 | ALOGE("m_codec==NULL, m_codec->stop() failed! [%s %d] \n", __FUNCTION__, |
405 | __LINE__); |
406 | } |
407 | |
408 | void Aml_OMX_DtsCodec::pause() { |
409 | ALOGI("[Aml_OMX_DtsCodec::%s %d] \n", __FUNCTION__, __LINE__); |
410 | if (m_codec != NULL) |
411 | m_codec->pause(); |
412 | else |
413 | ALOGE("m_codec==NULL, m_codec->pause() failed! [%s %d] \n", |
414 | __FUNCTION__, __LINE__); |
415 | } |
416 | |
417 | int Aml_OMX_DtsCodec::GetDecBytes() { |
418 | int used_len = 0; |
419 | used_len = buf_decode_offset - buf_decode_offset_pre; |
420 | buf_decode_offset_pre = buf_decode_offset; |
421 | return used_len; |
422 | } |
423 | |
424 | void Aml_OMX_DtsCodec::lock_init() { |
425 | pthread_mutexattr_t attr; |
426 | pthread_mutexattr_init(&attr); |
427 | pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED); |
428 | pthread_mutex_init(&lock, &attr); |
429 | pthread_mutexattr_destroy(&attr); |
430 | } |
431 | void Aml_OMX_DtsCodec::locked() { |
432 | pthread_mutex_lock (&lock); |
433 | } |
434 | void Aml_OMX_DtsCodec::unlocked() { |
435 | pthread_mutex_unlock (&lock); |
436 | } |
437 | |
438 | //--------------------------------OMX_Codec_local API------------------------------------------------- |
439 | |
440 | Aml_OMX_DtsCodec *arm_dts_omx_codec = NULL; |
441 | |
442 | void omx_dts_codec_pause() { |
443 | if (arm_dts_omx_codec != NULL) { |
444 | arm_dts_omx_codec->locked(); |
445 | arm_dts_omx_codec->pause(); |
446 | arm_dts_omx_codec->unlocked(); |
447 | } else |
448 | ALOGE("arm_dts_omx_codec==NULL arm_dts_omx_codec->pause failed! %s %d \n", |
449 | __FUNCTION__, __LINE__); |
450 | } |
451 | |
452 | void omx_dts_codec_read(unsigned char *buf, unsigned *size, int *exit) { |
453 | if (arm_dts_omx_codec != NULL) { |
454 | arm_dts_omx_codec->locked(); |
455 | arm_dts_omx_codec->read(buf, size, exit); |
456 | arm_dts_omx_codec->unlocked(); |
457 | } else |
458 | ALOGE("arm_dts_omx_codec==NULL arm_dts_omx_codec->read failed! %s %d \n", |
459 | __FUNCTION__, __LINE__); |
460 | } |
461 | |
462 | int omx_dts_codec_get_declen() { |
463 | int declen = 0; |
464 | if (arm_dts_omx_codec != NULL) { |
465 | arm_dts_omx_codec->locked(); |
466 | declen = arm_dts_omx_codec->GetDecBytes(); |
467 | arm_dts_omx_codec->unlocked(); |
468 | } else { |
469 | ALOGI( |
470 | "NOTE:arm_dts_omx_codec==NULL arm_dts_omx_codec_get_declen() return 0! %s %d \n", |
471 | __FUNCTION__, __LINE__); |
472 | } |
473 | return declen; |
474 | } |
475 | |
476 | int omx_dts_codec_get_FS() { |
477 | if (arm_dts_omx_codec != NULL) { |
478 | return arm_dts_omx_codec->m_OMXMediaSource->GetSampleRate(); |
479 | |
480 | } else { |
481 | ALOGI( |
482 | "NOTE:arm_dts_omx_codec==NULL arm_dts_omx_codec_get_FS() return 0! %s %d \n", |
483 | __FUNCTION__, __LINE__); |
484 | return 0; |
485 | } |
486 | } |
487 | |
488 | int omx_dts_codec_get_Nch() { |
489 | if (arm_dts_omx_codec != NULL) { |
490 | return arm_dts_omx_codec->m_OMXMediaSource->GetChNum(); |
491 | } else { |
492 | ALOGI( |
493 | "NOTE:arm_dts_omx_codec==NULL arm_dts_omx_codec_get_Nch() return 0! %s %d \n", |
494 | __FUNCTION__, __LINE__); |
495 | return 0; |
496 | } |
497 | } |
498 | |
499 | //--------------------------------------Decoder ThreadLoop-------------------------------------------- |
500 | |
501 | void *dts_decode_threadloop(__unused void *args) { |
502 | unsigned int outlen = 0; |
503 | unsigned int outlen_raw = 0; |
504 | unsigned int outlen_pcm = 0; |
505 | int write_sucessed = 1; |
506 | int ret = 0; |
507 | char *tmp = NULL; |
508 | tmp = (char*)malloc(6144*4+6144+8); |
509 | if (tmp == NULL) { |
510 | ALOGE("malloc buffer failed\n"); |
511 | return NULL; |
512 | } |
513 | ALOGI("[%s %d] enter!\n", __FUNCTION__, __LINE__); |
514 | while (decode_ThreadStopFlag == 0) { |
515 | if (write_sucessed == 1) { |
516 | outlen = 0; |
517 | outlen_raw = 0; |
518 | outlen_pcm = 0; |
519 | omx_dts_codec_read((unsigned char*)tmp, &outlen, &(decode_ThreadStopFlag)); |
520 | } |
521 | if (decode_ThreadStopFlag == 1) { |
522 | ALOGD("%s, exit threadloop! \n", __FUNCTION__); |
523 | break; |
524 | } |
525 | if (outlen > 8) { |
526 | memcpy(&outlen_pcm,tmp,4); |
527 | memcpy(&outlen_raw,tmp+4+outlen_pcm,4); |
528 | if (outlen_pcm > 0) { |
529 | ret = buffer_write(&DDP_out_buffer, tmp+4, outlen_pcm); |
530 | if (ret < 0) { |
531 | write_sucessed = 0; |
532 | usleep(10 * 1000); //10ms |
533 | } else { |
534 | write_sucessed = 1; |
535 | } |
536 | } |
537 | if (outlen_raw > 0) { |
538 | //ALOGI("raw data size %d\n",outlen_raw); |
539 | ret = buffer_write(&DD_out_buffer, tmp+4+outlen_pcm+4, outlen_raw); |
540 | if (ret < 0) { |
541 | write_sucessed = 0; |
542 | ALOGI("raw data write failed\n"); |
543 | usleep(10 * 1000); //10ms |
544 | } else { |
545 | #if 0 |
546 | FILE *fp1=fopen("/data/audio_raw.wav","a+"); |
547 | if (fp1) { |
548 | int flen=fwrite((char *)tmp+4+outlen_pcm+4,1,outlen_raw,fp1); |
549 | ALOGI("flen = %d---bytes_raw=%d ", flen, outlen_raw); |
550 | fclose(fp1); |
551 | }else{ |
552 | ALOGI("could not open file:audio_out.pcm"); |
553 | } |
554 | #endif |
555 | write_sucessed = 1; |
556 | } |
557 | } |
558 | } |
559 | } |
560 | decode_ThreadExitFlag = 0; |
561 | if (tmp) { |
562 | free(tmp); |
563 | } |
564 | ALOGD("%s, exiting...\n", __FUNCTION__); |
565 | return NULL; |
566 | } |
567 | |
568 | static int start_decode_thread_omx(void) { |
569 | pthread_attr_t attr; |
570 | struct sched_param param; |
571 | int ret = 0; |
572 | |
573 | ALOGI("[%s %d] enter!\n", __FUNCTION__, __LINE__); |
574 | pthread_mutex_lock(&decode_dev_op_mutex); |
575 | pthread_attr_init(&attr); |
576 | pthread_attr_setschedpolicy(&attr, SCHED_RR); |
577 | param.sched_priority = sched_get_priority_max(SCHED_RR); |
578 | pthread_attr_setschedparam(&attr, ¶m); |
579 | decode_ThreadStopFlag = 0; |
580 | decode_ThreadExitFlag = 1; |
581 | ret = pthread_create(&decode_ThreadID, &attr, &dts_decode_threadloop, NULL); |
582 | pthread_attr_destroy(&attr); |
583 | if (ret != 0) { |
584 | ALOGE("%s, Create thread fail!\n", __FUNCTION__); |
585 | pthread_mutex_unlock(&decode_dev_op_mutex); |
586 | return -1; |
587 | } |
588 | pthread_mutex_unlock(&decode_dev_op_mutex); |
589 | ALOGD("[%s] exiting...\n", __FUNCTION__); |
590 | return 0; |
591 | } |
592 | |
593 | static int stop_decode_thread_omx(void) { |
594 | int i = 0, tmp_timeout_count = 1000; |
595 | |
596 | ALOGI("[%s %d] enter!\n", __FUNCTION__, __LINE__); |
597 | pthread_mutex_lock(&decode_dev_op_mutex); |
598 | |
599 | decode_ThreadStopFlag = 1; |
600 | while (1) { |
601 | if (decode_ThreadExitFlag == 0) |
602 | break; |
603 | if (i >= tmp_timeout_count) |
604 | break; |
605 | i++; |
606 | usleep(1000); |
607 | } |
608 | if (i >= tmp_timeout_count) { |
609 | ALOGE( |
610 | "%s, Timeout: we have try %d ms, but the aml audio thread's exec flag is still(%d)!!!\n", |
611 | __FUNCTION__, tmp_timeout_count, decode_ThreadExitFlag); |
612 | } else { |
613 | ALOGD("%s, kill decode thread success after try %d ms.\n", __FUNCTION__, |
614 | i); |
615 | } |
616 | |
617 | pthread_join(decode_ThreadID, NULL); |
618 | decode_ThreadID = 0; |
619 | ALOGD("%s, aml audio close success.\n", __FUNCTION__); |
620 | pthread_mutex_unlock(&decode_dev_op_mutex); |
621 | return 0; |
622 | } |
623 | |
624 | //-------------------------------------external OMX_codec_api----------------------------------------- |
625 | extern "C" { |
626 | int omx_codec_dts_init(void) { |
627 | int ret = 0; |
628 | ALOGI("omx_codec_init!\n"); |
629 | #ifndef USE_SYS_WRITE_SERVICE |
630 | ret=property_set("media.libplayer.dtsopt0", "1"); |
631 | ALOGI("property_set<media.libplayer.dtsopt0> ret/%d\n",ret); |
632 | #else |
633 | amSystemWriteSetPropertyDts("media.libplayer.dtsopt0", "1"); |
634 | #endif |
635 | arm_dts_omx_codec = new android::Aml_OMX_DtsCodec(); |
636 | if (arm_dts_omx_codec == NULL) { |
637 | ALOGE("Err:arm_dts_omx_codec_init failed!\n"); |
638 | return -1; |
639 | } |
640 | |
641 | arm_dts_omx_codec->locked(); |
642 | ret = arm_dts_omx_codec->start(); |
643 | arm_dts_omx_codec->unlocked(); |
644 | if (ret < 0) { |
645 | goto Exit; |
646 | } |
647 | |
648 | ret = start_decode_thread_omx(); |
649 | if (ret == 0) |
650 | return 0; |
651 | Exit: arm_dts_omx_codec->stop(); |
652 | delete arm_dts_omx_codec; |
653 | arm_dts_omx_codec = NULL; |
654 | return -1; |
655 | } |
656 | |
657 | void omx_codec_dts_close(void) { |
658 | int ret = 0; |
659 | #ifndef USE_SYS_WRITE_SERVICE |
660 | ret=property_set("media.libplayer.dtsopt0", "0"); |
661 | ALOGI("property_set<media.libplayer.dtsopt0> ret/%d\n",ret); |
662 | #else |
663 | amSystemWriteSetPropertyDts("media.libplayer.dtsopt0", "0"); |
664 | #endif |
665 | if (arm_dts_omx_codec == NULL) { |
666 | ALOGI( |
667 | "NOTE:arm_dts_omx_codec==NULL arm_dts_omx_codec_close() do nothing! %s %d \n", |
668 | __FUNCTION__, __LINE__); |
669 | return; |
670 | } |
671 | ALOGI("omx_codec_close!\n"); |
672 | stop_decode_thread_omx(); |
673 | arm_dts_omx_codec->locked(); |
674 | arm_dts_omx_codec->stop(); |
675 | arm_dts_omx_codec->unlocked(); |
676 | delete arm_dts_omx_codec; |
677 | arm_dts_omx_codec = NULL; |
678 | return; |
679 | } |
680 | |
681 | } |
682 | } |
683 | |
684 |