blob: f74bc1582c6fcca8d55ee126d32d62373c42c854
1 | #include <stdio.h> |
2 | #include <stdarg.h> |
3 | #include <string.h> |
4 | #include <android/log.h> |
5 | #include <cutils/properties.h> |
6 | #include "Vorbis_mediasource.h" |
7 | #include "audio-dec.h" |
8 | |
9 | |
10 | #define LOG_TAG "Vorbis_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 | extern "C" int read_buffer(unsigned char *buffer, int size); |
14 | namespace android |
15 | { |
16 | |
17 | Vorbis_MediaSource::Vorbis_MediaSource(void *read_buffer, aml_audio_dec_t *audec) |
18 | { |
19 | ALOGI("%s %d \n", __FUNCTION__, __LINE__); |
20 | |
21 | mStarted = false; |
22 | mMeta = new MetaData; |
23 | mDataSource = NULL; |
24 | mGroup = NULL; |
25 | mBytesReaded = 0; |
26 | mCurrentTimeUs = 0; |
27 | pStop_ReadBuf_Flag = NULL; |
28 | fpread_buffer = (fp_read_buffer)read_buffer; |
29 | sample_rate = audec->samplerate; |
30 | ChNum = audec->channels > 2 ? 2 : audec->channels; |
31 | bytes_readed_sum_pre = 0; |
32 | bytes_readed_sum = 0; |
33 | FrameNumReaded = 0; |
34 | packt_size = 0; |
35 | mMeta->setInt32(kKeyChannelCount, audec->channels > 0 ? audec->channels : 2); |
36 | mMeta->setInt32(kKeySampleRate, audec->samplerate > 0 ? audec->samplerate : 48000); |
37 | |
38 | } |
39 | |
40 | |
41 | Vorbis_MediaSource::~Vorbis_MediaSource() |
42 | { |
43 | ALOGI("%s %d \n", __FUNCTION__, __LINE__); |
44 | if (mStarted) { |
45 | stop(); |
46 | } |
47 | } |
48 | |
49 | |
50 | int Vorbis_MediaSource::GetSampleRate() |
51 | { |
52 | return sample_rate; |
53 | } |
54 | |
55 | int Vorbis_MediaSource::GetChNum() |
56 | { |
57 | return ChNum; |
58 | } |
59 | |
60 | int* Vorbis_MediaSource::Get_pStop_ReadBuf_Flag() |
61 | { |
62 | return pStop_ReadBuf_Flag; |
63 | } |
64 | |
65 | int Vorbis_MediaSource::Set_pStop_ReadBuf_Flag(int *pStop) |
66 | { |
67 | pStop_ReadBuf_Flag = pStop; |
68 | return 0; |
69 | } |
70 | |
71 | int Vorbis_MediaSource::GetReadedBytes() |
72 | { |
73 | if (1/*FrameNumReaded<2*/) { |
74 | int bytes_used; |
75 | bytes_used = bytes_readed_sum - bytes_readed_sum_pre; |
76 | if (bytes_used < 0) { |
77 | ALOGI("[%s]bytes_readed_sum(%lld) < bytes_readed_sum_pre(%lld) \n", __FUNCTION__, bytes_readed_sum, bytes_readed_sum_pre); |
78 | bytes_used = 0; |
79 | } |
80 | bytes_readed_sum_pre = bytes_readed_sum; |
81 | return bytes_used; |
82 | } else { |
83 | return packt_size; |
84 | } |
85 | } |
86 | |
87 | sp<MetaData> Vorbis_MediaSource::getFormat() |
88 | { |
89 | ALOGI("%s %d \n", __FUNCTION__, __LINE__); |
90 | return mMeta; |
91 | } |
92 | |
93 | status_t Vorbis_MediaSource::start(MetaData *params) |
94 | { |
95 | ALOGI("%s %d \n", __FUNCTION__, __LINE__); |
96 | mGroup = new MediaBufferGroup; |
97 | mGroup->add_buffer(new MediaBuffer(8192)); |
98 | mStarted = true; |
99 | return OK; |
100 | } |
101 | |
102 | status_t Vorbis_MediaSource::stop() |
103 | { |
104 | ALOGI("%s %d \n", __FUNCTION__, __LINE__); |
105 | delete mGroup; |
106 | mGroup = NULL; |
107 | mStarted = false; |
108 | return OK; |
109 | } |
110 | |
111 | |
112 | int Vorbis_MediaSource::MediaSourceRead_buffer(unsigned char *buffer, int size) |
113 | { |
114 | int readcnt = 0; |
115 | int readsum = 0; |
116 | if (fpread_buffer != NULL) { |
117 | int sleep_time = 0; |
118 | while ((readsum < size) && (*pStop_ReadBuf_Flag == 0)) { |
119 | readcnt = fpread_buffer(buffer + readsum, size - readsum); |
120 | if (readcnt < (size - readsum)) { |
121 | sleep_time++; |
122 | usleep(10000); |
123 | } |
124 | readsum += readcnt; |
125 | if (sleep_time > 200) { //wait for max 2s to get audio data |
126 | ALOGE("[%s] Can't get data from audiobuffer,wait for %d ms\n ", __FUNCTION__, sleep_time * 10); |
127 | return 0; |
128 | } |
129 | } |
130 | |
131 | if (*pStop_ReadBuf_Flag == 1) { |
132 | ALOGI("[%s] End of Stream: *pStop_ReadBuf_Flag==1\n ", __FUNCTION__); |
133 | } |
134 | return readsum; |
135 | } else { |
136 | ALOGE("[%s]ERR: fpread_buffer=NULL\n ", __FUNCTION__); |
137 | return 0; |
138 | } |
139 | } |
140 | |
141 | static void dump_pcm_bin(char *path, char *buf, int size) |
142 | { |
143 | FILE *fp = fopen(path, "ab+"); |
144 | if (fp != NULL) { |
145 | fwrite(buf, 1, size, fp); |
146 | fclose(fp); |
147 | } |
148 | } |
149 | |
150 | status_t Vorbis_MediaSource::read(MediaBuffer **out, const ReadOptions *options) |
151 | { |
152 | |
153 | *out = NULL; |
154 | int read_bytes_per_time; |
155 | MediaBuffer *buffer; |
156 | int byte_readed = 0; |
157 | status_t err; |
158 | |
159 | int SyncFlag = 0, readedbytes = 0; |
160 | int pkt_size = 0; |
161 | uint8_t ptr_head[4] = {0}; |
162 | int buf_getted = 0; |
163 | //for android N, need player to insert the numPageSamples at the end of input buffer |
164 | //android M and before,which is done by OMXCodec itself. |
165 | int32_t numPageSamples = -1; |
166 | //android N and later |
167 | #if ANDROID_PLATFORM_SDK_VERSION >= 24 |
168 | int32_t insert_byte = 4; |
169 | #else |
170 | int32_t insert_byte = 0; |
171 | #endif |
172 | re_read: |
173 | |
174 | //---------------------------------- |
175 | if (MediaSourceRead_buffer(&ptr_head[0], 4) < 4) { |
176 | ALOGI("WARNING: fpread_buffer readbytes failed [%s %d]!\n", __FUNCTION__, __LINE__); |
177 | return ERROR_END_OF_STREAM; |
178 | } |
179 | |
180 | while (!SyncFlag) { |
181 | if (ptr_head[0] == 'H' && ptr_head[1] == 'E' && ptr_head[2] == 'A' && ptr_head[3] == 'D') { |
182 | SyncFlag = 1; |
183 | break; |
184 | } |
185 | ptr_head[0] = ptr_head[1]; |
186 | ptr_head[1] = ptr_head[2]; |
187 | ptr_head[2] = ptr_head[3]; |
188 | if (MediaSourceRead_buffer(&ptr_head[3], 1) < 1) { |
189 | ALOGI("WARNING: fpread_buffer readbytes failed [%s %d]!\n", __FUNCTION__, __LINE__); |
190 | return ERROR_END_OF_STREAM; |
191 | } |
192 | |
193 | if (*pStop_ReadBuf_Flag == 1) { |
194 | ALOGI("Stop_ReadBuf_Flag==1 stop read_buf [%s %d]", __FUNCTION__, __LINE__); |
195 | return ERROR_END_OF_STREAM; |
196 | } |
197 | } |
198 | |
199 | if (MediaSourceRead_buffer((unsigned char*)(&pkt_size), 4) < 4) { |
200 | ALOGI("WARNING: fpread_buffer readbytes failed [%s %d]!\n", __FUNCTION__, __LINE__); |
201 | return ERROR_END_OF_STREAM; |
202 | } |
203 | |
204 | if (!buf_getted) { |
205 | buf_getted = 1; |
206 | err = mGroup->acquire_buffer(&buffer); |
207 | if (err != OK) { |
208 | ALOGE("[%s %d] mGroup->acquire_buffer ERR!\n", __FUNCTION__, __LINE__); |
209 | return err; |
210 | } |
211 | } |
212 | //mediasource should wait abuf data util player finish it |
213 | while (MediaSourceRead_buffer((unsigned char*)(buffer->data()), pkt_size) < pkt_size && (*pStop_ReadBuf_Flag == 0)) { |
214 | ALOGW("WARNING: fpread_buffer readbytes failed [%s %d]!\n", __FUNCTION__, __LINE__); |
215 | } |
216 | if (*pStop_ReadBuf_Flag == 1) { |
217 | buffer->release(); |
218 | buffer = NULL; |
219 | return ERROR_END_OF_STREAM; |
220 | } |
221 | |
222 | if (insert_byte > 0) { |
223 | buffer->set_range(0, pkt_size + insert_byte); |
224 | memcpy((unsigned char*)(buffer->data()) + pkt_size, &numPageSamples, sizeof(numPageSamples)); |
225 | } |
226 | buffer->meta_data()->setInt64(kKeyTime, mCurrentTimeUs); |
227 | buffer->meta_data()->setInt32(kKeyIsSyncFrame, 1); |
228 | bytes_readed_sum += (pkt_size + 8); |
229 | ALOGV("vorbis package size %d,num %d\n", pkt_size, FrameNumReaded); |
230 | FrameNumReaded++; |
231 | if (FrameNumReaded == 2) { |
232 | goto re_read; |
233 | } |
234 | if (FrameNumReaded > 3) { |
235 | packt_size = pkt_size; |
236 | buffer->meta_data()->setInt32(kKeyValidSamples, -1); |
237 | } |
238 | *out = buffer; |
239 | return OK; |
240 | } |
241 | |
242 | |
243 | } // namespace android |
244 | |
245 | |
246 |