summaryrefslogtreecommitdiff
path: root/amadec/omx_audio/THD_mediasource.cpp (plain)
blob: e29df93addcad7c888f4824684192f15c2dcbd06
1#include <stdio.h>
2#include <stdarg.h>
3#include <string.h>
4#include <android/log.h>
5#include <cutils/properties.h>
6#include "THD_mediasource.h"
7
8extern "C" int read_buffer(unsigned char *buffer,int size);
9
10#define LOG_TAG "THD_Medissource"
11#define ALOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
12#define ALOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
13
14namespace android {
15
16THD_MediaSource::THD_MediaSource(void *read_buffer)
17{
18 ALOGI("%s %d \n",__FUNCTION__,__LINE__);
19 mStarted=false;
20 mMeta=new MetaData;
21 mDataSource=NULL;
22 mGroup=NULL;
23 mBytesReaded=0;
24 mCurrentTimeUs=0;
25 pStop_ReadBuf_Flag=NULL;
26 fpread_buffer=(fp_read_buffer)read_buffer;
27 sample_rate=48000;//96000;
28 ChNum=2;
29 frame_size=0;
30 bytes_readed_sum_pre=0;
31 bytes_readed_sum=0;
32 mSyncMain = false;
33 mMeta->setInt32(kKeyChannelCount, 2);
34 mMeta->setInt32(kKeySampleRate, 48000);
35}
36
37THD_MediaSource::~THD_MediaSource()
38{
39 ALOGI("%s %d \n",__FUNCTION__,__LINE__);
40 if (mStarted) {
41 stop();
42 }
43}
44
45int THD_MediaSource::GetSampleRate()
46{
47 return sample_rate;
48}
49
50int THD_MediaSource::GetChNum()
51{
52 return ChNum;
53}
54
55int* THD_MediaSource::Get_pStop_ReadBuf_Flag()
56{
57 return pStop_ReadBuf_Flag;
58}
59
60int THD_MediaSource::Set_pStop_ReadBuf_Flag(int *pStop)
61{
62 pStop_ReadBuf_Flag = pStop;
63
64 return 0;
65}
66
67int THD_MediaSource::SetReadedBytes(int size)
68{
69 mBytesReaded=size;
70 return 0;
71}
72
73int THD_MediaSource::GetReadedBytes()
74{
75 int bytes_used;
76 bytes_used=bytes_readed_sum-bytes_readed_sum_pre;
77 if(bytes_used<0)
78 {
79 ALOGI("[%s]bytes_readed_sum(%lld) < bytes_readed_sum_pre(%lld) \n",__FUNCTION__,bytes_readed_sum,bytes_readed_sum_pre);
80 bytes_used=0;
81 }
82 bytes_readed_sum_pre=bytes_readed_sum;
83 return bytes_used;
84}
85
86sp<MetaData> THD_MediaSource::getFormat() {
87 ALOGI("%s %d \n",__FUNCTION__,__LINE__);
88 return mMeta;
89}
90
91status_t THD_MediaSource::start(MetaData *params)
92{
93 ALOGI("%s %d \n",__FUNCTION__,__LINE__);
94 mGroup = new MediaBufferGroup;
95 mGroup->add_buffer(new MediaBuffer(4096));
96 mStarted = true;
97 return OK;
98}
99
100status_t THD_MediaSource::stop()
101{
102 ALOGI("%s %d \n",__FUNCTION__,__LINE__);
103 delete mGroup;
104 mGroup = NULL;
105 mStarted = false;
106 return OK;
107}
108
109int THD_MediaSource::MediaSourceRead_buffer(unsigned char *buffer,int size)
110{
111 int readcnt=0;
112 int readsum=0;
113 if(fpread_buffer!=NULL)
114 {
115 int sleep_time=0;
116 while((readsum<size)&& (*pStop_ReadBuf_Flag==0))
117 {
118 readcnt=fpread_buffer(buffer+readsum,size-readsum);
119 if(readcnt<(size-readsum)){
120 sleep_time++;
121 usleep(10000);
122 }
123 readsum+=readcnt;
124 if((sleep_time>0) && (sleep_time%100==0)){ //wait for max 10s to get audio data
125 ALOGE("[%s] Can't get data from audiobuffer,wait for %d ms\n ", __FUNCTION__,sleep_time*10);
126 }
127 }
128 bytes_readed_sum +=readsum;
129 if(*pStop_ReadBuf_Flag==1)
130 {
131 ALOGI("[%s] End of Stream: *pStop_ReadBuf_Flag==1\n ", __FUNCTION__);
132 }
133 return readsum;
134 }else{
135 ALOGE("[%s]ERR: fpread_buffer=NULL\n ", __FUNCTION__);
136 return 0;
137 }
138}
139
140status_t THD_MediaSource::read(MediaBuffer **out, const ReadOptions *options)
141{
142 *out = NULL;
143 uint fourheader = 0; //au size and crc check and timing
144 uint syncheader = 0; //truehd sync words(word:four bytes)
145 uchar byte;
146 uchar byte2[2];
147 uint ausize; //access unit size(byte)
148 uint i = 0;
149
150 if(!mSyncMain){
151 while(1)//max frame length is 4000 bytes
152 {
153 ALOGI("=======find sync main=====\n");
154 if(MediaSourceRead_buffer((uchar*)&byte, 1) < 1){
155 ALOGI("[%s,%d]read raw data from buffer failed\n",__FUNCTION__,__LINE__);
156 return ERROR_END_OF_STREAM;
157 }
158 fourheader = (fourheader << 8) | (syncheader >> 24);
159 syncheader = (syncheader << 8) | byte;
160 if(syncheader == TRUEHDSYNC){
161 ausize = ((fourheader >> 16) & 0x0fff) << 1;
162 ChNum = 2;
163 sample_rate = 48000;//96000;
164 mSyncMain = true;
165 ALOGI("truehd: main sync word find ok\n");
166 break;
167 }
168 }
169#if 0
170 if(i >= MAX_AUSIZE)
171 {
172 ALOGI("ERROR: can not find truehd main sync\n");
173 return ERROR_UNSUPPORTED;
174 }
175#endif
176 }else{
177 if(MediaSourceRead_buffer((uchar*)&byte2, 2) < 2){
178 ALOGI("[%s,%d]read raw data from buffer failed\n",__FUNCTION__,__LINE__);
179 return ERROR_END_OF_STREAM;
180 }
181 ausize = (((byte2[0]<< 8) | byte2[1]) & 0x0fff) << 1;
182 }
183
184 if((ausize < MIN_AUSIZE) || (ausize > MAX_AUSIZE)){
185 ALOGI("ERROR: access unit size can not accept!!!\n");
186 return ERROR_UNSUPPORTED;
187 }
188
189
190 MediaBuffer *buffer;
191 status_t err = mGroup->acquire_buffer(&buffer);
192 if (err != OK) {
193 ALOGI("acquire_buffer buffer failed.\n");
194 return err;
195 }
196
197 if(syncheader == TRUEHDSYNC){
198 memcpy((uchar *)(buffer->data()), &fourheader, 4);
199 memcpy((uchar *)((unsigned long)buffer->data() + 4), &syncheader, 4);
200 if (MediaSourceRead_buffer((uchar*)((unsigned long)buffer->data() + 8), ausize - 8) != (ausize - 8)) {
201 ALOGI("[%s %d]stream read failed\n",__FUNCTION__,__LINE__);
202 buffer->release();
203 buffer = NULL;
204 return ERROR_END_OF_STREAM;
205 }
206 char *ptr = (char *)((unsigned long)buffer->data() + 8);
207 sample_rate = ((ptr[0] >> 4) & 0xf) & 8 ? 44100 : 48000;
208 ALOGI("[%s:%d] sample_rate = %d\n", __FUNCTION__, __LINE__, sample_rate);
209 }else{
210 memcpy((uchar *)(buffer->data()), &byte2, 2);
211 if (MediaSourceRead_buffer((uchar*)((unsigned long)buffer->data() + 2), ausize - 2) != (ausize - 2)) {
212 ALOGI("[%s %d]stream read failed\n",__FUNCTION__,__LINE__);
213 buffer->release();
214 buffer = NULL;
215 return ERROR_END_OF_STREAM;
216 }
217 }
218
219
220 buffer->set_range(0, ausize);
221 buffer->meta_data()->setInt64(kKeyTime, mCurrentTimeUs);
222 buffer->meta_data()->setInt32(kKeyIsSyncFrame, 1);
223
224 *out = buffer;
225 return OK;
226}
227
228} // namespace android
229
230