blob: 8127e1aa31b4dd64f94efd44defefefb0889ec0d
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 | if (MediaSourceRead_buffer(&ptr_head[0], 4) < 4) { |
205 | readedbytes = 4; |
206 | ALOGI("WARNING:read %d bytes failed [%s %d]!\n", readedbytes, |
207 | __FUNCTION__, __LINE__); |
208 | return ERROR_END_OF_STREAM; |
209 | } |
210 | |
211 | mFrame_size = 0; |
212 | SyncFlag = 0; |
213 | |
214 | if ((mStop_ReadBuf_Flag == 1) || (decode_ThreadStopFlag == 1)) { |
215 | ALOGI("Stop Flag is set, stop read_buf [%s %d]", __FUNCTION__, |
216 | __LINE__); |
217 | return ERROR_END_OF_STREAM; |
218 | } |
219 | if ((mStop_ReadBuf_Flag == 1) || (decode_ThreadStopFlag == 1)) { |
220 | ALOGI("Stop Flag is set, stop read_buf [%s %d]", __FUNCTION__, |
221 | __LINE__); |
222 | return ERROR_END_OF_STREAM; |
223 | } |
224 | while (!SyncFlag) { |
225 | int i =0; |
226 | //DTS_SYNCWORD_IEC61937 : 0xF8724E1F |
227 | if ((ptr_head[0] == 0x72 && ptr_head[ 1] == 0xf8 |
228 | &&ptr_head[2] == 0x1f && ptr_head[3] == 0x4e)|| |
229 | (ptr_head[0] == 0xf8 && ptr_head[1] == 0x72 |
230 | &&ptr_head[2] == 0x4e && ptr_head[3] == 0x1f)) { |
231 | |
232 | if ((mStop_ReadBuf_Flag == 1) || (decode_ThreadStopFlag == 1)) { |
233 | ALOGI("Stop Flag is set, stop read_buf [%s %d]", |
234 | __FUNCTION__, __LINE__); |
235 | return ERROR_END_OF_STREAM; |
236 | } |
237 | SyncFlag = 1; |
238 | } |
239 | if (SyncFlag == 0) { |
240 | ptr_head[0] =ptr_head[1]; |
241 | ptr_head[1] =ptr_head[2]; |
242 | ptr_head[2] =ptr_head[3]; |
243 | if (MediaSourceRead_buffer(&ptr_head[3], 1) < 1) { |
244 | readedbytes = 1; |
245 | ALOGI("WARNING: read %d bytes failed [%s %d]!\n", |
246 | readedbytes, __FUNCTION__, __LINE__); |
247 | return ERROR_END_OF_STREAM; |
248 | } |
249 | |
250 | if ((mStop_ReadBuf_Flag == 1) || (decode_ThreadStopFlag == 1)) { |
251 | ALOGI("Stop Flag is set, stop read_buf [%s %d]", __FUNCTION__, |
252 | __LINE__); |
253 | return ERROR_END_OF_STREAM; |
254 | } |
255 | } |
256 | } |
257 | if (MediaSourceRead_buffer(&ptr_head2[0], 4) < 4) { |
258 | readedbytes = 4; |
259 | ALOGI("WARNING:read %d bytes failed [%s %d]!\n", readedbytes, |
260 | __FUNCTION__, __LINE__); |
261 | return ERROR_END_OF_STREAM; |
262 | } |
263 | //memcpy(&mFrame_size,ptr_head2+IEC61937_DTS_HEAD_PTR-6,2); |
264 | //ALOGI("mFrame_size:%d",mFrame_size); |
265 | //memcpy(&mFrame_size,ptr_head2+IEC61937_DTS_HEAD_PTR-6,2); |
266 | //ado-no lib only pack dts core data |
267 | mFrame_size = 2040; |
268 | MediaBuffer *buffer; |
269 | status_t err = mGroup->acquire_buffer(&buffer); |
270 | if (err != OK) { |
271 | return err; |
272 | } |
273 | if (MediaSourceRead_buffer( |
274 | (unsigned char*) (buffer->data()), |
275 | mFrame_size) != mFrame_size ) { |
276 | ALOGI("[%s %d]stream read failed:bytes_req/%d\n", __FUNCTION__, |
277 | __LINE__, mFrame_size); |
278 | buffer->release(); |
279 | buffer = NULL; |
280 | return ERROR_END_OF_STREAM; |
281 | } |
282 | |
283 | buffer->set_range(0, mFrame_size); |
284 | buffer->meta_data()->setInt64(kKeyTime, 0); |
285 | buffer->meta_data()->setInt32(kKeyIsSyncFrame, 1); |
286 | |
287 | *out = buffer; |
288 | return OK; |
289 | } |
290 | |
291 | |
292 | //-------------------------------OMX codec------------------------------------------------ |
293 | |
294 | const char *MEDIA_MIMETYPE_AUDIO_DTS = "audio/dtshd"; |
295 | Aml_OMX_DtsCodec::Aml_OMX_DtsCodec(void) { |
296 | ALOGI("[Aml_OMX_DtsCodec::%s: %d],atype %d\n", __FUNCTION__, __LINE__,spdif_audio_type); |
297 | m_codec = NULL; |
298 | status_t m_OMXClientConnectStatus = m_OMXClient.connect(); |
299 | lock_init(); |
300 | locked(); |
301 | buf_decode_offset = 0; |
302 | buf_decode_offset_pre = 0; |
303 | if (m_OMXClientConnectStatus != OK) { |
304 | ALOGE("Err:omx client connect error\n"); |
305 | } else { |
306 | const char *mine_type = NULL; |
307 | if (spdif_audio_type == DTSHD || spdif_audio_type == DTS) |
308 | mine_type = MEDIA_MIMETYPE_AUDIO_DTS; |
309 | |
310 | m_OMXMediaSource = new Dtshd_Media_Source(); |
311 | sp < MetaData > metadata = m_OMXMediaSource->getFormat(); |
312 | metadata->setCString(kKeyMIMEType, mine_type); |
313 | m_codec = SimpleDecodingSource::Create(m_OMXMediaSource, 0, 0); |
314 | |
315 | if (m_codec != NULL) { |
316 | ALOGI("OMXCodec::Create success %s %d \n", __FUNCTION__, __LINE__); |
317 | } else { |
318 | ALOGE("Err: OMXCodec::Create failed %s %d \n", __FUNCTION__, |
319 | __LINE__); |
320 | } |
321 | } |
322 | unlocked(); |
323 | } |
324 | |
325 | Aml_OMX_DtsCodec::~Aml_OMX_DtsCodec() { |
326 | } |
327 | |
328 | status_t Aml_OMX_DtsCodec::read(unsigned char *buf, unsigned *size, int *exit) { |
329 | MediaBuffer *srcBuffer; |
330 | status_t status; |
331 | m_OMXMediaSource->Set_Stop_ReadBuf_Flag(*exit); |
332 | |
333 | if (*exit) { |
334 | ALOGI("NOTE:exit flag enabled! [%s %d] \n", __FUNCTION__, __LINE__); |
335 | *size = 0; |
336 | return OK; |
337 | } |
338 | |
339 | status = m_codec->read(&srcBuffer, NULL); |
340 | |
341 | if (srcBuffer == NULL) { |
342 | *size = 0; |
343 | return OK; |
344 | } |
345 | |
346 | *size = srcBuffer->range_length(); |
347 | |
348 | if (status == OK && (*size != 0)) { |
349 | memcpy((unsigned char *) buf, |
350 | (unsigned char *) srcBuffer->data() + srcBuffer->range_offset(), |
351 | *size); |
352 | srcBuffer->set_range(srcBuffer->range_offset() + (*size), |
353 | srcBuffer->range_length() - (*size)); |
354 | srcBuffer->meta_data()->findInt64(kKeyTime, &buf_decode_offset); |
355 | } |
356 | |
357 | if (srcBuffer->range_length() == 0) { |
358 | srcBuffer->release(); |
359 | srcBuffer = NULL; |
360 | } |
361 | |
362 | return OK; |
363 | } |
364 | |
365 | status_t Aml_OMX_DtsCodec::start() { |
366 | ALOGI("[Aml_OMX_DtsCodec::%s %d] enter!\n", __FUNCTION__, __LINE__); |
367 | status_t status = m_codec->start(); |
368 | if (status != OK) { |
369 | ALOGE("Err:OMX client can't start OMX decoder! status=%d (0x%08x)\n", |
370 | (int) status, (int) status); |
371 | m_codec.clear(); |
372 | } |
373 | return status; |
374 | } |
375 | |
376 | void Aml_OMX_DtsCodec::stop() { |
377 | ALOGI("[Aml_OMX_DtsCodec::%s %d] enter!\n", __FUNCTION__, __LINE__); |
378 | if (m_codec != NULL) { |
379 | if (m_OMXMediaSource->Get_Stop_ReadBuf_Flag()) |
380 | m_OMXMediaSource->Set_Stop_ReadBuf_Flag(1); |
381 | m_codec->pause(); |
382 | m_codec->stop(); |
383 | wp < MediaSource > tmp = m_codec; |
384 | m_codec.clear(); |
385 | while (tmp.promote() != NULL) { |
386 | ALOGI("[Aml_OMX_DtsCodec::%s %d]wait m_codec free OK!\n", __FUNCTION__, |
387 | __LINE__); |
388 | usleep(1000); |
389 | } |
390 | m_OMXClient.disconnect(); |
391 | m_OMXMediaSource.clear(); |
392 | } else |
393 | ALOGE("m_codec==NULL, m_codec->stop() failed! [%s %d] \n", __FUNCTION__, |
394 | __LINE__); |
395 | } |
396 | |
397 | void Aml_OMX_DtsCodec::pause() { |
398 | ALOGI("[Aml_OMX_DtsCodec::%s %d] \n", __FUNCTION__, __LINE__); |
399 | if (m_codec != NULL) |
400 | m_codec->pause(); |
401 | else |
402 | ALOGE("m_codec==NULL, m_codec->pause() failed! [%s %d] \n", |
403 | __FUNCTION__, __LINE__); |
404 | } |
405 | |
406 | int Aml_OMX_DtsCodec::GetDecBytes() { |
407 | int used_len = 0; |
408 | used_len = buf_decode_offset - buf_decode_offset_pre; |
409 | buf_decode_offset_pre = buf_decode_offset; |
410 | return used_len; |
411 | } |
412 | |
413 | void Aml_OMX_DtsCodec::lock_init() { |
414 | pthread_mutexattr_t attr; |
415 | pthread_mutexattr_init(&attr); |
416 | pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED); |
417 | pthread_mutex_init(&lock, &attr); |
418 | pthread_mutexattr_destroy(&attr); |
419 | } |
420 | void Aml_OMX_DtsCodec::locked() { |
421 | pthread_mutex_lock (&lock); |
422 | } |
423 | void Aml_OMX_DtsCodec::unlocked() { |
424 | pthread_mutex_unlock (&lock); |
425 | } |
426 | |
427 | //--------------------------------OMX_Codec_local API------------------------------------------------- |
428 | |
429 | Aml_OMX_DtsCodec *arm_dts_omx_codec = NULL; |
430 | |
431 | void omx_dts_codec_pause() { |
432 | if (arm_dts_omx_codec != NULL) { |
433 | arm_dts_omx_codec->locked(); |
434 | arm_dts_omx_codec->pause(); |
435 | arm_dts_omx_codec->unlocked(); |
436 | } else |
437 | ALOGE("arm_dts_omx_codec==NULL arm_dts_omx_codec->pause failed! %s %d \n", |
438 | __FUNCTION__, __LINE__); |
439 | } |
440 | |
441 | void omx_dts_codec_read(unsigned char *buf, unsigned *size, int *exit) { |
442 | if (arm_dts_omx_codec != NULL) { |
443 | arm_dts_omx_codec->locked(); |
444 | arm_dts_omx_codec->read(buf, size, exit); |
445 | arm_dts_omx_codec->unlocked(); |
446 | } else |
447 | ALOGE("arm_dts_omx_codec==NULL arm_dts_omx_codec->read failed! %s %d \n", |
448 | __FUNCTION__, __LINE__); |
449 | } |
450 | |
451 | int omx_dts_codec_get_declen() { |
452 | int declen = 0; |
453 | if (arm_dts_omx_codec != NULL) { |
454 | arm_dts_omx_codec->locked(); |
455 | declen = arm_dts_omx_codec->GetDecBytes(); |
456 | arm_dts_omx_codec->unlocked(); |
457 | } else { |
458 | ALOGI( |
459 | "NOTE:arm_dts_omx_codec==NULL arm_dts_omx_codec_get_declen() return 0! %s %d \n", |
460 | __FUNCTION__, __LINE__); |
461 | } |
462 | return declen; |
463 | } |
464 | |
465 | int omx_dts_codec_get_FS() { |
466 | if (arm_dts_omx_codec != NULL) { |
467 | return arm_dts_omx_codec->m_OMXMediaSource->GetSampleRate(); |
468 | |
469 | } else { |
470 | ALOGI( |
471 | "NOTE:arm_dts_omx_codec==NULL arm_dts_omx_codec_get_FS() return 0! %s %d \n", |
472 | __FUNCTION__, __LINE__); |
473 | return 0; |
474 | } |
475 | } |
476 | |
477 | int omx_dts_codec_get_Nch() { |
478 | if (arm_dts_omx_codec != NULL) { |
479 | return arm_dts_omx_codec->m_OMXMediaSource->GetChNum(); |
480 | } else { |
481 | ALOGI( |
482 | "NOTE:arm_dts_omx_codec==NULL arm_dts_omx_codec_get_Nch() return 0! %s %d \n", |
483 | __FUNCTION__, __LINE__); |
484 | return 0; |
485 | } |
486 | } |
487 | |
488 | //--------------------------------------Decoder ThreadLoop-------------------------------------------- |
489 | |
490 | void *dts_decode_threadloop(__unused void *args) { |
491 | unsigned int outlen = 0; |
492 | unsigned int outlen_raw = 0; |
493 | unsigned int outlen_pcm = 0; |
494 | int write_sucessed = 1; |
495 | int ret = 0; |
496 | char *tmp = NULL; |
497 | tmp = (char*)malloc(6144*4+6144+8); |
498 | if (tmp == NULL) { |
499 | ALOGE("malloc buffer failed\n"); |
500 | return NULL; |
501 | } |
502 | ALOGI("[%s %d] enter!\n", __FUNCTION__, __LINE__); |
503 | while (decode_ThreadStopFlag == 0) { |
504 | if (write_sucessed == 1) { |
505 | outlen = 0; |
506 | outlen_raw = 0; |
507 | outlen_pcm = 0; |
508 | omx_dts_codec_read((unsigned char*)tmp, &outlen, &(decode_ThreadStopFlag)); |
509 | } |
510 | if (decode_ThreadStopFlag == 1) { |
511 | ALOGD("%s, exit threadloop! \n", __FUNCTION__); |
512 | break; |
513 | } |
514 | if (outlen > 8) { |
515 | memcpy(&outlen_pcm,tmp,4); |
516 | memcpy(&outlen_raw,tmp+4+outlen_pcm,4); |
517 | if (outlen_pcm > 0) { |
518 | ret = buffer_write(&DDP_out_buffer, tmp+4, outlen_pcm); |
519 | if (ret < 0) { |
520 | write_sucessed = 0; |
521 | usleep(10 * 1000); //10ms |
522 | } else { |
523 | write_sucessed = 1; |
524 | } |
525 | } |
526 | if (outlen_raw > 0) { |
527 | //ALOGI("raw data size %d\n",outlen_raw); |
528 | ret = buffer_write(&DD_out_buffer, tmp+4+outlen_pcm+4, outlen_raw); |
529 | if (ret < 0) { |
530 | write_sucessed = 0; |
531 | ALOGI("raw data write failed\n"); |
532 | usleep(10 * 1000); //10ms |
533 | } else { |
534 | #if 0 |
535 | FILE *fp1=fopen("/data/audio_raw.wav","a+"); |
536 | if (fp1) { |
537 | int flen=fwrite((char *)tmp+4+outlen_pcm+4,1,outlen_raw,fp1); |
538 | ALOGI("flen = %d---bytes_raw=%d ", flen, outlen_raw); |
539 | fclose(fp1); |
540 | }else{ |
541 | ALOGI("could not open file:audio_out.pcm"); |
542 | } |
543 | #endif |
544 | write_sucessed = 1; |
545 | } |
546 | } |
547 | } |
548 | } |
549 | decode_ThreadExitFlag = 0; |
550 | if (tmp) { |
551 | free(tmp); |
552 | } |
553 | ALOGD("%s, exiting...\n", __FUNCTION__); |
554 | return NULL; |
555 | } |
556 | |
557 | static int start_decode_thread_omx(void) { |
558 | pthread_attr_t attr; |
559 | struct sched_param param; |
560 | int ret = 0; |
561 | |
562 | ALOGI("[%s %d] enter!\n", __FUNCTION__, __LINE__); |
563 | pthread_mutex_lock(&decode_dev_op_mutex); |
564 | pthread_attr_init(&attr); |
565 | pthread_attr_setschedpolicy(&attr, SCHED_RR); |
566 | param.sched_priority = sched_get_priority_max(SCHED_RR); |
567 | pthread_attr_setschedparam(&attr, ¶m); |
568 | decode_ThreadStopFlag = 0; |
569 | decode_ThreadExitFlag = 1; |
570 | ret = pthread_create(&decode_ThreadID, &attr, &dts_decode_threadloop, NULL); |
571 | pthread_attr_destroy(&attr); |
572 | if (ret != 0) { |
573 | ALOGE("%s, Create thread fail!\n", __FUNCTION__); |
574 | pthread_mutex_unlock(&decode_dev_op_mutex); |
575 | return -1; |
576 | } |
577 | pthread_mutex_unlock(&decode_dev_op_mutex); |
578 | ALOGD("[%s] exiting...\n", __FUNCTION__); |
579 | return 0; |
580 | } |
581 | |
582 | static int stop_decode_thread_omx(void) { |
583 | int i = 0, tmp_timeout_count = 1000; |
584 | |
585 | ALOGI("[%s %d] enter!\n", __FUNCTION__, __LINE__); |
586 | pthread_mutex_lock(&decode_dev_op_mutex); |
587 | |
588 | decode_ThreadStopFlag = 1; |
589 | while (1) { |
590 | if (decode_ThreadExitFlag == 0) |
591 | break; |
592 | if (i >= tmp_timeout_count) |
593 | break; |
594 | i++; |
595 | usleep(1000); |
596 | } |
597 | if (i >= tmp_timeout_count) { |
598 | ALOGE( |
599 | "%s, Timeout: we have try %d ms, but the aml audio thread's exec flag is still(%d)!!!\n", |
600 | __FUNCTION__, tmp_timeout_count, decode_ThreadExitFlag); |
601 | } else { |
602 | ALOGD("%s, kill decode thread success after try %d ms.\n", __FUNCTION__, |
603 | i); |
604 | } |
605 | |
606 | pthread_join(decode_ThreadID, NULL); |
607 | decode_ThreadID = 0; |
608 | ALOGD("%s, aml audio close success.\n", __FUNCTION__); |
609 | pthread_mutex_unlock(&decode_dev_op_mutex); |
610 | return 0; |
611 | } |
612 | |
613 | //-------------------------------------external OMX_codec_api----------------------------------------- |
614 | extern "C" { |
615 | int omx_codec_dts_init(void) { |
616 | int ret = 0; |
617 | ALOGI("omx_codec_init!\n"); |
618 | #ifndef USE_SYS_WRITE_SERVICE |
619 | ret=property_set("media.libplayer.dtsopt0", "1"); |
620 | ALOGI("property_set<media.libplayer.dtsopt0> ret/%d\n",ret); |
621 | #else |
622 | amSystemWriteSetPropertyDts("media.libplayer.dtsopt0", "1"); |
623 | #endif |
624 | arm_dts_omx_codec = new android::Aml_OMX_DtsCodec(); |
625 | if (arm_dts_omx_codec == NULL) { |
626 | ALOGE("Err:arm_dts_omx_codec_init failed!\n"); |
627 | return -1; |
628 | } |
629 | |
630 | arm_dts_omx_codec->locked(); |
631 | ret = arm_dts_omx_codec->start(); |
632 | arm_dts_omx_codec->unlocked(); |
633 | if (ret < 0) { |
634 | goto Exit; |
635 | } |
636 | |
637 | ret = start_decode_thread_omx(); |
638 | if (ret == 0) |
639 | return 0; |
640 | Exit: arm_dts_omx_codec->stop(); |
641 | delete arm_dts_omx_codec; |
642 | arm_dts_omx_codec = NULL; |
643 | return -1; |
644 | } |
645 | |
646 | void omx_codec_dts_close(void) { |
647 | int ret = 0; |
648 | #ifndef USE_SYS_WRITE_SERVICE |
649 | ret=property_set("media.libplayer.dtsopt0", "0"); |
650 | ALOGI("property_set<media.libplayer.dtsopt0> ret/%d\n",ret); |
651 | #else |
652 | amSystemWriteSetPropertyDts("media.libplayer.dtsopt0", "0"); |
653 | #endif |
654 | if (arm_dts_omx_codec == NULL) { |
655 | ALOGI( |
656 | "NOTE:arm_dts_omx_codec==NULL arm_dts_omx_codec_close() do nothing! %s %d \n", |
657 | __FUNCTION__, __LINE__); |
658 | return; |
659 | } |
660 | ALOGI("omx_codec_close!\n"); |
661 | stop_decode_thread_omx(); |
662 | arm_dts_omx_codec->locked(); |
663 | arm_dts_omx_codec->stop(); |
664 | arm_dts_omx_codec->unlocked(); |
665 | delete arm_dts_omx_codec; |
666 | arm_dts_omx_codec = NULL; |
667 | return; |
668 | } |
669 | |
670 | } |
671 | } |
672 | |
673 |