• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 //#define LOG_NDEBUG 0
18 #define LOG_TAG "NdkMediaExtractor"
19 
20 
21 #include <media/NdkMediaError.h>
22 #include <media/NdkMediaExtractor.h>
23 #include <media/NdkMediaErrorPriv.h>
24 #include <media/NdkMediaFormatPriv.h>
25 #include "NdkJavaVMHelperPriv.h"
26 #include "NdkMediaDataSourcePriv.h"
27 
28 
29 #include <inttypes.h>
30 #include <utils/Log.h>
31 #include <utils/StrongPointer.h>
32 #include <media/hardware/CryptoAPI.h>
33 #include <media/stagefright/foundation/ABuffer.h>
34 #include <media/stagefright/foundation/AMessage.h>
35 #include <media/stagefright/MetaData.h>
36 #include <media/stagefright/NuMediaExtractor.h>
37 #include <media/IMediaHTTPService.h>
38 #include <android_util_Binder.h>
39 
40 #include <jni.h>
41 
42 using namespace android;
43 
44 struct AMediaExtractor {
45     sp<NuMediaExtractor> mImpl;
46     sp<ABuffer> mPsshBuf;
47 };
48 
U32ArrayToSizeBuf(size_t numSubSamples,uint32_t * data)49 sp<ABuffer> U32ArrayToSizeBuf(size_t numSubSamples, uint32_t *data) {
50     if (numSubSamples >  SIZE_MAX / sizeof(size_t)) {
51         return NULL;
52     }
53     sp<ABuffer> sizebuf = new ABuffer(numSubSamples * sizeof(size_t));
54     size_t *sizes = (size_t *)sizebuf->data();
55     for (size_t i = 0; sizes != NULL && i < numSubSamples; i++) {
56         sizes[i] = data[i];
57     }
58     return sizebuf;
59 }
60 
61 extern "C" {
62 
63 EXPORT
AMediaExtractor_new()64 AMediaExtractor* AMediaExtractor_new() {
65     ALOGV("ctor");
66     AMediaExtractor *mData = new AMediaExtractor();
67     mData->mImpl = new NuMediaExtractor(
68         NdkJavaVMHelper::getJNIEnv() != nullptr
69                 ? NuMediaExtractor::EntryPoint::NDK_WITH_JVM
70                 : NuMediaExtractor::EntryPoint::NDK_NO_JVM );
71     return mData;
72 }
73 
74 EXPORT
AMediaExtractor_delete(AMediaExtractor * mData)75 media_status_t AMediaExtractor_delete(AMediaExtractor *mData) {
76     ALOGV("dtor");
77     delete mData;
78     return AMEDIA_OK;
79 }
80 
81 EXPORT
AMediaExtractor_setDataSourceFd(AMediaExtractor * mData,int fd,off64_t offset,off64_t length)82 media_status_t AMediaExtractor_setDataSourceFd(AMediaExtractor *mData, int fd, off64_t offset,
83         off64_t length) {
84     ALOGV("setDataSource(%d, %" PRId64 ", %" PRId64 ")", fd, offset, length);
85     return translate_error(mData->mImpl->setDataSource(fd, offset, length));
86 }
87 
AMediaExtractor_setDataSourceWithHeaders(AMediaExtractor * mData,const char * uri,int numheaders,const char * const * keys,const char * const * values)88 media_status_t AMediaExtractor_setDataSourceWithHeaders(AMediaExtractor *mData,
89         const char *uri,
90         int numheaders,
91         const char * const *keys,
92         const char * const *values) {
93 
94     ALOGV("setDataSource(%s)", uri);
95 
96     sp<MediaHTTPService> httpService = createMediaHttpService(uri);
97     if (httpService == NULL) {
98         ALOGE("can't create http service");
99         return AMEDIA_ERROR_UNSUPPORTED;
100     }
101 
102     KeyedVector<String8, String8> headers;
103     for (int i = 0; i < numheaders; ++i) {
104         String8 key8(keys[i]);
105         String8 value8(values[i]);
106         headers.add(key8, value8);
107     }
108 
109     status_t err;
110     err = mData->mImpl->setDataSource(httpService, uri, numheaders > 0 ? &headers : NULL);
111     return translate_error(err);
112 }
113 
114 EXPORT
AMediaExtractor_setDataSource(AMediaExtractor * mData,const char * location)115 media_status_t AMediaExtractor_setDataSource(AMediaExtractor *mData, const char *location) {
116     return AMediaExtractor_setDataSourceWithHeaders(mData, location, 0, NULL, NULL);
117 }
118 
119 EXPORT
AMediaExtractor_setDataSourceCustom(AMediaExtractor * mData,AMediaDataSource * src)120 media_status_t AMediaExtractor_setDataSourceCustom(AMediaExtractor* mData, AMediaDataSource *src) {
121     return translate_error(mData->mImpl->setDataSource(new NdkDataSource(src)));
122 }
123 
124 EXPORT
AMediaExtractor_getFileFormat(AMediaExtractor * mData)125 AMediaFormat* AMediaExtractor_getFileFormat(AMediaExtractor *mData) {
126     sp<AMessage> format;
127     mData->mImpl->getFileFormat(&format);
128     // ignore any error, we want to return the empty format
129     return AMediaFormat_fromMsg(&format);
130 }
131 
132 EXPORT
AMediaExtractor_getTrackCount(AMediaExtractor * mData)133 size_t AMediaExtractor_getTrackCount(AMediaExtractor *mData) {
134     return mData->mImpl->countTracks();
135 }
136 
137 EXPORT
AMediaExtractor_getTrackFormat(AMediaExtractor * mData,size_t idx)138 AMediaFormat* AMediaExtractor_getTrackFormat(AMediaExtractor *mData, size_t idx) {
139     sp<AMessage> format;
140     mData->mImpl->getTrackFormat(idx, &format);
141     return AMediaFormat_fromMsg(&format);
142 }
143 
144 EXPORT
AMediaExtractor_selectTrack(AMediaExtractor * mData,size_t idx)145 media_status_t AMediaExtractor_selectTrack(AMediaExtractor *mData, size_t idx) {
146     ALOGV("selectTrack(%zu)", idx);
147     return translate_error(mData->mImpl->selectTrack(idx));
148 }
149 
150 EXPORT
AMediaExtractor_unselectTrack(AMediaExtractor * mData,size_t idx)151 media_status_t AMediaExtractor_unselectTrack(AMediaExtractor *mData, size_t idx) {
152     ALOGV("unselectTrack(%zu)", idx);
153     return translate_error(mData->mImpl->unselectTrack(idx));
154 }
155 
156 EXPORT
AMediaExtractor_advance(AMediaExtractor * mData)157 bool AMediaExtractor_advance(AMediaExtractor *mData) {
158     //ALOGV("advance");
159     status_t err = mData->mImpl->advance();
160     if (err == ERROR_END_OF_STREAM) {
161         return false;
162     } else if (err != OK) {
163         ALOGE("sf error code: %d", err);
164         return false;
165     }
166     return true;
167 }
168 
169 EXPORT
AMediaExtractor_seekTo(AMediaExtractor * ex,int64_t seekPosUs,SeekMode mode)170 media_status_t AMediaExtractor_seekTo(AMediaExtractor *ex, int64_t seekPosUs, SeekMode mode) {
171     android::MediaSource::ReadOptions::SeekMode sfmode;
172     if (mode == AMEDIAEXTRACTOR_SEEK_PREVIOUS_SYNC) {
173         sfmode = android::MediaSource::ReadOptions::SEEK_PREVIOUS_SYNC;
174     } else if (mode == AMEDIAEXTRACTOR_SEEK_CLOSEST_SYNC) {
175         sfmode = android::MediaSource::ReadOptions::SEEK_CLOSEST_SYNC;
176     } else {
177         sfmode = android::MediaSource::ReadOptions::SEEK_NEXT_SYNC;
178     }
179 
180     return translate_error(ex->mImpl->seekTo(seekPosUs, sfmode));
181 }
182 
183 EXPORT
AMediaExtractor_readSampleData(AMediaExtractor * mData,uint8_t * buffer,size_t capacity)184 ssize_t AMediaExtractor_readSampleData(AMediaExtractor *mData, uint8_t *buffer, size_t capacity) {
185     //ALOGV("readSampleData");
186     sp<ABuffer> tmp = new ABuffer(buffer, capacity);
187     if (mData->mImpl->readSampleData(tmp) == OK) {
188         return tmp->size();
189     }
190     return -1;
191 }
192 
193 EXPORT
AMediaExtractor_getSampleSize(AMediaExtractor * mData)194 ssize_t AMediaExtractor_getSampleSize(AMediaExtractor *mData) {
195     size_t sampleSize;
196     status_t err = mData->mImpl->getSampleSize(&sampleSize);
197     if (err != OK) {
198         return -1;
199     }
200     return sampleSize;
201 }
202 
203 EXPORT
AMediaExtractor_getSampleFlags(AMediaExtractor * mData)204 uint32_t AMediaExtractor_getSampleFlags(AMediaExtractor *mData) {
205     int sampleFlags = 0;
206     sp<MetaData> meta;
207     status_t err = mData->mImpl->getSampleMeta(&meta);
208     if (err != OK) {
209         return -1;
210     }
211     int32_t val;
212     if (meta->findInt32(kKeyIsSyncFrame, &val) && val != 0) {
213         sampleFlags |= AMEDIAEXTRACTOR_SAMPLE_FLAG_SYNC;
214     }
215 
216     uint32_t type;
217     const void *data;
218     size_t size;
219     if (meta->findData(kKeyEncryptedSizes, &type, &data, &size)) {
220         sampleFlags |= AMEDIAEXTRACTOR_SAMPLE_FLAG_ENCRYPTED;
221     }
222     return sampleFlags;
223 }
224 
225 EXPORT
AMediaExtractor_getSampleTrackIndex(AMediaExtractor * mData)226 int AMediaExtractor_getSampleTrackIndex(AMediaExtractor *mData) {
227     size_t idx;
228     if (mData->mImpl->getSampleTrackIndex(&idx) != OK) {
229         return -1;
230     }
231     return idx;
232 }
233 
234 EXPORT
AMediaExtractor_getSampleTime(AMediaExtractor * mData)235 int64_t AMediaExtractor_getSampleTime(AMediaExtractor *mData) {
236     int64_t time;
237     if (mData->mImpl->getSampleTime(&time) != OK) {
238         return -1;
239     }
240     return time;
241 }
242 
243 EXPORT
AMediaExtractor_getPsshInfo(AMediaExtractor * ex)244 PsshInfo* AMediaExtractor_getPsshInfo(AMediaExtractor *ex) {
245 
246     if (ex->mPsshBuf != NULL) {
247         return (PsshInfo*) ex->mPsshBuf->data();
248     }
249 
250     sp<AMessage> format;
251     if (ex->mImpl->getFileFormat(&format) != OK) {
252         android_errorWriteWithInfoLog(0x534e4554, "243222985", -1, nullptr, 0);
253         return NULL;
254     }
255     sp<ABuffer> buffer;
256     if(!format->findBuffer("pssh", &buffer)) {
257         return NULL;
258     }
259 
260     // the format of the buffer is 1 or more of:
261     //    {
262     //        16 byte uuid
263     //        4 byte data length N
264     //        N bytes of data
265     //    }
266 
267     // Determine the number of entries in the source data.
268     // Since we got the data from stagefright, we trust it is valid and properly formatted.
269     const uint8_t* data = buffer->data();
270     size_t len = buffer->size();
271     size_t numentries = 0;
272     while (len > 0) {
273         numentries++;
274 
275         if (len < 16) {
276             ALOGE("invalid PSSH data");
277             return NULL;
278         }
279         // skip uuid
280         data += 16;
281         len -= 16;
282 
283         // get data length
284         if (len < 4) {
285             ALOGE("invalid PSSH data");
286             return NULL;
287         }
288         uint32_t datalen = *((uint32_t*)data);
289         data += 4;
290         len -= 4;
291 
292         if (len < datalen) {
293             ALOGE("invalid PSSH data");
294             return NULL;
295         }
296         // skip the data
297         data += datalen;
298         len -= datalen;
299     }
300 
301     // there are <numentries> in the source buffer, we need
302     // (source buffer size) - (sizeof(uint32_t) * numentries) + sizeof(size_t)
303     //  + ((sizeof(void*) + sizeof(size_t)) * numentries) bytes for the PsshInfo structure
304     // Or in other words, the data lengths in the source structure are replaced by size_t
305     // (which may be the same size or larger, for 64 bit), and in addition there is an
306     // extra pointer for each entry, and an extra size_t for the entire PsshInfo.
307     size_t newsize = buffer->size() - (sizeof(uint32_t) * numentries) + sizeof(size_t)
308             + ((sizeof(void*) + sizeof(size_t)) * numentries);
309     if (newsize <= buffer->size()) {
310         ALOGE("invalid PSSH data");
311         return NULL;
312     }
313     ex->mPsshBuf = new ABuffer(newsize);
314     ex->mPsshBuf->setRange(0, newsize);
315 
316     // copy data
317     const uint8_t* src = buffer->data();
318     uint8_t* dst = ex->mPsshBuf->data();
319     uint8_t* dstdata = dst + sizeof(size_t) + numentries * sizeof(PsshEntry);
320     *((size_t*)dst) = numentries;
321     dst += sizeof(size_t);
322     for (size_t i = 0; i < numentries; i++) {
323         // copy uuid
324         memcpy(dst, src, 16);
325         src += 16;
326         dst += 16;
327 
328         // get/copy data length
329         uint32_t datalen = *((uint32_t*)src);
330         *((size_t*)dst) = datalen;
331         src += sizeof(uint32_t);
332         dst += sizeof(size_t);
333 
334         // the next entry in the destination is a pointer to the actual data, which we store
335         // after the array of PsshEntry
336         *((void**)dst) = dstdata;
337         dst += sizeof(void*);
338 
339         // copy the actual data
340         memcpy(dstdata, src, datalen);
341         dstdata += datalen;
342         src += datalen;
343     }
344 
345     return (PsshInfo*) ex->mPsshBuf->data();
346 }
347 
348 EXPORT
AMediaExtractor_getSampleCryptoInfo(AMediaExtractor * ex)349 AMediaCodecCryptoInfo *AMediaExtractor_getSampleCryptoInfo(AMediaExtractor *ex) {
350     sp<MetaData> meta;
351     if(ex->mImpl->getSampleMeta(&meta) != 0) {
352         return NULL;
353     }
354 
355     uint32_t type;
356     const void *crypteddata;
357     size_t cryptedsize;
358     if (!meta->findData(kKeyEncryptedSizes, &type, &crypteddata, &cryptedsize)) {
359         return NULL;
360     }
361     size_t numSubSamples = cryptedsize / sizeof(uint32_t);
362 
363     const void *cleardata;
364     size_t clearsize;
365     if (meta->findData(kKeyPlainSizes, &type, &cleardata, &clearsize)) {
366         if (clearsize != cryptedsize) {
367             // The two must be of the same length.
368             return NULL;
369         }
370     }
371 
372     const void *key;
373     size_t keysize;
374     if (meta->findData(kKeyCryptoKey, &type, &key, &keysize)) {
375         if (keysize != 16) {
376             // Keys must be 16 bytes in length.
377             return NULL;
378         }
379     }
380 
381     const void *iv;
382     size_t ivsize;
383     if (meta->findData(kKeyCryptoIV, &type, &iv, &ivsize)) {
384         if (ivsize != 16) {
385             // IVs must be 16 bytes in length.
386             return NULL;
387         }
388     }
389 
390     int32_t mode;
391     if (!meta->findInt32(kKeyCryptoMode, &mode)) {
392         mode = CryptoPlugin::kMode_AES_CTR;
393     }
394 
395     sp<ABuffer> clearbuf;
396     sp<ABuffer> cryptedbuf;
397     if (sizeof(uint32_t) != sizeof(size_t)) {
398         clearbuf   = U32ArrayToSizeBuf(numSubSamples, (uint32_t *)cleardata);
399         cryptedbuf = U32ArrayToSizeBuf(numSubSamples, (uint32_t *)crypteddata);
400         cleardata   = clearbuf    == NULL ? NULL : clearbuf->data();
401         crypteddata = crypteddata == NULL ? NULL : cryptedbuf->data();
402         if(crypteddata == NULL || cleardata == NULL) {
403             return NULL;
404         }
405     }
406 
407     return AMediaCodecCryptoInfo_new(
408             numSubSamples,
409             (uint8_t*) key,
410             (uint8_t*) iv,
411             (cryptoinfo_mode_t) mode,
412             (size_t*) cleardata,
413             (size_t*) crypteddata);
414 }
415 
416 EXPORT
AMediaExtractor_getCachedDuration(AMediaExtractor * ex)417 int64_t AMediaExtractor_getCachedDuration(AMediaExtractor *ex) {
418     bool eos;
419     int64_t durationUs;
420     if (ex->mImpl->getCachedDuration(&durationUs, &eos)) {
421         return durationUs;
422     }
423     return -1;
424 }
425 
426 EXPORT
AMediaExtractor_getSampleFormat(AMediaExtractor * ex,AMediaFormat * fmt)427 media_status_t AMediaExtractor_getSampleFormat(AMediaExtractor *ex, AMediaFormat *fmt) {
428     ALOGV("AMediaExtractor_getSampleFormat");
429     if (fmt == NULL) {
430         return AMEDIA_ERROR_INVALID_PARAMETER;
431     }
432 
433     sp<MetaData> sampleMeta;
434     status_t err = ex->mImpl->getSampleMeta(&sampleMeta);
435     if (err != OK) {
436         return translate_error(err);
437     }
438 #ifdef LOG_NDEBUG
439     sampleMeta->dumpToLog();
440 #endif
441 
442     sp<AMessage> meta;
443     AMediaFormat_getFormat(fmt, &meta);
444     meta->clear();
445 
446     int32_t layerId;
447     if (sampleMeta->findInt32(kKeyTemporalLayerId, &layerId)) {
448         meta->setInt32(AMEDIAFORMAT_KEY_TEMPORAL_LAYER_ID, layerId);
449     }
450 
451     size_t trackIndex;
452     err = ex->mImpl->getSampleTrackIndex(&trackIndex);
453     if (err == OK) {
454         meta->setInt32(AMEDIAFORMAT_KEY_TRACK_INDEX, trackIndex);
455         sp<AMessage> trackFormat;
456         AString mime;
457         err = ex->mImpl->getTrackFormat(trackIndex, &trackFormat);
458         if (err == OK
459                 && trackFormat != NULL
460                 && trackFormat->findString(AMEDIAFORMAT_KEY_MIME, &mime)) {
461             meta->setString(AMEDIAFORMAT_KEY_MIME, mime);
462         }
463     }
464 
465     int64_t durationUs;
466     if (sampleMeta->findInt64(kKeyDuration, &durationUs)) {
467         meta->setInt64(AMEDIAFORMAT_KEY_DURATION, durationUs);
468     }
469 
470     uint32_t dataType; // unused
471     const void *seiData;
472     size_t seiLength;
473     if (sampleMeta->findData(kKeySEI, &dataType, &seiData, &seiLength)) {
474         sp<ABuffer> sei = ABuffer::CreateAsCopy(seiData, seiLength);;
475         meta->setBuffer(AMEDIAFORMAT_KEY_SEI, sei);
476     }
477 
478     const void *mpegUserDataPointer;
479     size_t mpegUserDataLength;
480     if (sampleMeta->findData(
481             kKeyMpegUserData, &dataType, &mpegUserDataPointer, &mpegUserDataLength)) {
482         sp<ABuffer> mpegUserData = ABuffer::CreateAsCopy(mpegUserDataPointer, mpegUserDataLength);
483         meta->setBuffer(AMEDIAFORMAT_KEY_MPEG_USER_DATA, mpegUserData);
484     }
485 
486     const void *audioPresentationsPointer;
487     size_t audioPresentationsLength;
488     if (sampleMeta->findData(
489             kKeyAudioPresentationInfo, &dataType,
490             &audioPresentationsPointer, &audioPresentationsLength)) {
491         sp<ABuffer> audioPresentationsData = ABuffer::CreateAsCopy(
492                 audioPresentationsPointer, audioPresentationsLength);
493         meta->setBuffer(AMEDIAFORMAT_KEY_AUDIO_PRESENTATION_INFO, audioPresentationsData);
494     }
495 
496     int64_t val64;
497     if (sampleMeta->findInt64(kKeySampleFileOffset, &val64)) {
498         meta->setInt64("sample-file-offset", val64);
499         ALOGV("SampleFileOffset Found");
500     }
501     if (sampleMeta->findInt64(kKeyLastSampleIndexInChunk, &val64)) {
502         meta->setInt64("last-sample-index-in-chunk" /*AMEDIAFORMAT_KEY_LAST_SAMPLE_INDEX_IN_CHUNK*/,
503                        val64);
504         ALOGV("kKeyLastSampleIndexInChunk Found");
505     }
506 
507     ALOGV("AMediaFormat_toString:%s", AMediaFormat_toString(fmt));
508 
509     return AMEDIA_OK;
510 }
511 
512 } // extern "C"
513 
514