• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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 "NativeMuxerTest"
19 #include <log/log.h>
20 
21 #include <dlfcn.h>
22 #include <fcntl.h>
23 #include <jni.h>
24 #include <media/NdkMediaExtractor.h>
25 #include <media/NdkMediaFormat.h>
26 #include <media/NdkMediaMuxer.h>
27 #include <sys/stat.h>
28 #include <unistd.h>
29 
30 #include <cmath>
31 #include <cstring>
32 #include <fstream>
33 #include <map>
34 #include <vector>
35 
36 #include "NativeMediaCommon.h"
37 
38 // Transcoding arrived in Android 12/S, which is api 31.
39 #define __TRANSCODING_MIN_API__ 31
40 
41 /**
42  * MuxerNativeTestHelper breaks a media file to elements that a muxer can use to rebuild its clone.
43  * While testing muxer, if the test doesn't use MediaCodecs class to generate elementary stream,
44  * but uses MediaExtractor, this class will be handy
45  */
46 class MuxerNativeTestHelper {
47   public:
MuxerNativeTestHelper(const char * srcPath,const char * mediaType=nullptr,int frameLimit=-1)48     explicit MuxerNativeTestHelper(const char* srcPath, const char* mediaType = nullptr,
49                                    int frameLimit = -1)
50         : mSrcPath(srcPath), mMediaType(mediaType), mTrackCount(0), mBuffer(nullptr) {
51         mFrameLimit = frameLimit < 0 ? INT_MAX : frameLimit;
52         splitMediaToMuxerParameters();
53     }
54 
~MuxerNativeTestHelper()55     ~MuxerNativeTestHelper() {
56         for (auto format : mFormat) AMediaFormat_delete(format);
57         delete[] mBuffer;
58         for (const auto& buffInfoTrack : mBufferInfo) {
59             for (auto info : buffInfoTrack) delete info;
60         }
61     }
62 
getTrackCount()63     int getTrackCount() { return mTrackCount; }
64 
getSampleCount(size_t trackId)65     size_t getSampleCount(size_t trackId) {
66         if (trackId >= mTrackCount) {
67             return 0;
68         }
69         return mBufferInfo[(mInpIndexMap.at(trackId))].size();
70     }
71 
getTrackFormat(size_t trackId)72     AMediaFormat* getTrackFormat(size_t trackId) {
73         if (trackId >= mTrackCount) {
74             return nullptr;
75         }
76         return mFormat[trackId];
77     }
78 
79     bool registerTrack(AMediaMuxer* muxer);
80 
81     bool insertSampleData(AMediaMuxer* muxer);
82 
83     bool writeAFewSamplesData(AMediaMuxer* muxer, uint32_t fromIndex, uint32_t toIndex);
84 
85     bool writeAFewSamplesDataFromTime(AMediaMuxer* muxer, int64_t* fromTime, uint32_t numSamples,
86                                       bool lastSplit);
87 
88     bool muxMedia(AMediaMuxer* muxer);
89 
90     bool appendMedia(AMediaMuxer *muxer, uint32_t fromIndex, uint32_t toIndex);
91 
92     bool appendMediaFromTime(AMediaMuxer *muxer, int64_t *appendFromTime,
93                                     uint32_t numSamples, bool lastSplit);
94 
95     bool combineMedias(AMediaMuxer* muxer, MuxerNativeTestHelper* that, const int* repeater);
96 
97     bool isSubsetOf(MuxerNativeTestHelper* that);
98 
99     void offsetTimeStamp(int64_t tsAudioOffsetUs, int64_t tsVideoOffsetUs, int sampleOffset);
100 
101   private:
102     void splitMediaToMuxerParameters();
103 
104     static const int STTS_TOLERANCE_US = 100;
105     const char* mSrcPath;
106     const char* mMediaType;
107     int mTrackCount;
108     std::vector<AMediaFormat*> mFormat;
109     uint8_t* mBuffer;
110     std::vector<std::vector<AMediaCodecBufferInfo*>> mBufferInfo;
111     std::map<int, int> mInpIndexMap;
112     std::vector<int> mTrackIdxOrder;
113     int mFrameLimit;
114     // combineMedias() uses local version of this variable
115     std::map<int, int> mOutIndexMap;
116 };
117 
splitMediaToMuxerParameters()118 void MuxerNativeTestHelper::splitMediaToMuxerParameters() {
119     FILE* ifp = fopen(mSrcPath, "rbe");
120     int fd;
121     int fileSize;
122     if (ifp) {
123         struct stat buf {};
124         stat(mSrcPath, &buf);
125         fileSize = buf.st_size;
126         fd = fileno(ifp);
127     } else {
128         return;
129     }
130     AMediaExtractor* extractor = AMediaExtractor_new();
131     if (extractor == nullptr) {
132         fclose(ifp);
133         return;
134     }
135     // Set up MediaExtractor to read from the source.
136     media_status_t status = AMediaExtractor_setDataSourceFd(extractor, fd, 0, fileSize);
137     if (AMEDIA_OK != status) {
138         AMediaExtractor_delete(extractor);
139         fclose(ifp);
140         return;
141     }
142 
143     // Set up MediaFormat
144     int index = 0;
145     for (size_t trackID = 0; trackID < AMediaExtractor_getTrackCount(extractor); trackID++) {
146         AMediaExtractor_selectTrack(extractor, trackID);
147         AMediaFormat* format = AMediaExtractor_getTrackFormat(extractor, trackID);
148         if (mMediaType == nullptr) {
149             mTrackCount++;
150             mFormat.push_back(format);
151             mInpIndexMap[trackID] = index++;
152         } else {
153             const char* mediaType;
154             bool hasKey = AMediaFormat_getString(format, AMEDIAFORMAT_KEY_MIME, &mediaType);
155             if (hasKey && !strcmp(mediaType, mMediaType)) {
156                 mTrackCount++;
157                 mFormat.push_back(format);
158                 mInpIndexMap[trackID] = index;
159                 break;
160             } else {
161                 AMediaFormat_delete(format);
162                 AMediaExtractor_unselectTrack(extractor, trackID);
163             }
164         }
165     }
166 
167     if (mTrackCount <= 0) {
168         AMediaExtractor_delete(extractor);
169         fclose(ifp);
170         return;
171     }
172 
173     // Set up location for elementary stream
174     int bufferSize = ((fileSize + 127) >> 7) << 7;
175     // Ideally, Sum of return values of extractor.readSampleData(...) should not exceed
176     // source file size. But in case of Vorbis, aosp extractor appends an additional 4 bytes to
177     // the data at every readSampleData() call. bufferSize <<= 1 empirically large enough to
178     // hold the excess 4 bytes per read call
179     bufferSize <<= 1;
180     mBuffer = new uint8_t[bufferSize];
181     if (mBuffer == nullptr) {
182         mTrackCount = 0;
183         AMediaExtractor_delete(extractor);
184         fclose(ifp);
185         return;
186     }
187 
188     // Let MediaExtractor do its thing
189     bool sawEOS = false;
190     int frameCount = 0;
191     int offset = 0;
192     mBufferInfo.resize(mTrackCount);
193     while (!sawEOS && frameCount < mFrameLimit) {
194         auto* bufferInfo = new AMediaCodecBufferInfo();
195         bufferInfo->offset = offset;
196         bufferInfo->size =
197                 AMediaExtractor_readSampleData(extractor, mBuffer + offset, (bufferSize - offset));
198         if (bufferInfo->size < 0) {
199             sawEOS = true;
200         } else {
201             bufferInfo->presentationTimeUs = AMediaExtractor_getSampleTime(extractor);
202             bufferInfo->flags = AMediaExtractor_getSampleFlags(extractor);
203             int trackID = AMediaExtractor_getSampleTrackIndex(extractor);
204             mTrackIdxOrder.push_back(trackID);
205             mBufferInfo[(mInpIndexMap.at(trackID))].push_back(bufferInfo);
206             AMediaExtractor_advance(extractor);
207             frameCount++;
208         }
209         offset += bufferInfo->size;
210     }
211     ALOGV("frameCount:%d", frameCount);
212     AMediaExtractor_delete(extractor);
213     fclose(ifp);
214 }
215 
registerTrack(AMediaMuxer * muxer)216 bool MuxerNativeTestHelper::registerTrack(AMediaMuxer* muxer) {
217     for (int trackID = 0; trackID < mTrackCount; trackID++) {
218         int dstIndex = AMediaMuxer_addTrack(muxer, mFormat[trackID]);
219         if (dstIndex < 0) return false;
220         mOutIndexMap[trackID] = dstIndex;
221     }
222     return true;
223 }
224 
insertSampleData(AMediaMuxer * muxer)225 bool MuxerNativeTestHelper::insertSampleData(AMediaMuxer* muxer) {
226     // write all registered tracks in interleaved order
227     int* frameCount = new int[mTrackCount]{0};
228     for (int trackID : mTrackIdxOrder) {
229         int index = mInpIndexMap.at(trackID);
230         AMediaCodecBufferInfo* info = mBufferInfo[index][frameCount[index]];
231         if (AMediaMuxer_writeSampleData(muxer, mOutIndexMap.at(index), mBuffer, info) !=
232             AMEDIA_OK) {
233             delete[] frameCount;
234             return false;
235         }
236         ALOGV("Track: %d Timestamp: %" PRId64 "", trackID, info->presentationTimeUs);
237         frameCount[index]++;
238     }
239     delete[] frameCount;
240     ALOGV("Total track samples %zu", mTrackIdxOrder.size());
241     return true;
242 }
243 
writeAFewSamplesData(AMediaMuxer * muxer,uint32_t fromIndex,uint32_t toIndex)244 bool MuxerNativeTestHelper::writeAFewSamplesData(AMediaMuxer* muxer, uint32_t fromIndex,
245                                                  uint32_t toIndex) {
246     ALOGV("fromIndex:%u, toIndex:%u", fromIndex, toIndex);
247     // write all registered tracks in interleaved order
248     ALOGV("mTrackIdxOrder.size:%zu", mTrackIdxOrder.size());
249     if (fromIndex > toIndex || toIndex >= mTrackIdxOrder.size()) {
250         ALOGE("wrong index");
251         return false;
252     }
253     int* frameCount = new int[mTrackCount]{0};
254     for (int i = 0; i < fromIndex; ++i) {
255         ++frameCount[mInpIndexMap.at(mTrackIdxOrder[i])];
256     }
257     ALOGV("Initial samples skipped:");
258     for (int i = 0; i < mTrackCount; ++i) {
259         ALOGV("i:%d:%d", i, frameCount[i]);
260     }
261     for (int i = fromIndex; i <= toIndex; ++i) {
262         int trackID = mTrackIdxOrder[i];
263         int trackIndex = mInpIndexMap.at(trackID);
264         ALOGV("trackID:%d, trackIndex:%d, frameCount:%d", trackID, trackIndex,
265               frameCount[trackIndex]);
266         AMediaCodecBufferInfo* info = mBufferInfo[trackIndex][frameCount[trackIndex]];
267         ALOGV("Got info offset:%d, size:%d", info->offset, info->size);
268         if (AMediaMuxer_writeSampleData(muxer, mOutIndexMap.at(trackIndex), mBuffer, info) !=
269             AMEDIA_OK) {
270             delete[] frameCount;
271             return false;
272         }
273         ALOGV("Track: %d Timestamp: %" PRId64 "", trackID, info->presentationTimeUs);
274         ++frameCount[trackIndex];
275     }
276     ALOGV("Last sample counts:");
277     for (int i = 0; i < mTrackCount; ++i) {
278         ALOGV("i:%d", frameCount[i]);
279     }
280     delete[] frameCount;
281     return true;
282 }
283 
writeAFewSamplesDataFromTime(AMediaMuxer * muxer,int64_t * fromTime,uint32_t numSamples,bool lastSplit)284 bool MuxerNativeTestHelper::writeAFewSamplesDataFromTime(AMediaMuxer* muxer, int64_t* fromTime,
285                                                          uint32_t numSamples, bool lastSplit) {
286     for (int tc = 0; tc < mTrackCount; ++tc) {
287         ALOGV("fromTime[%d]:%lld", tc, (long long)fromTime[tc]);
288     }
289     ALOGV("numSamples:%u", numSamples);
290     uint32_t samplesWritten = 0;
291     uint32_t i = 0;
292     int* frameCount = new int[mTrackCount]{0};
293     do {
294         int trackID = mTrackIdxOrder[i++];
295         int trackIndex = mInpIndexMap.at(trackID);
296         ALOGV("trackID:%d, trackIndex:%d, frameCount:%d", trackID, trackIndex,
297               frameCount[trackIndex]);
298         AMediaCodecBufferInfo* info = mBufferInfo[trackIndex][frameCount[trackIndex]];
299         ++frameCount[trackIndex];
300         ALOGV("Got info offset:%d, size:%d, PTS:%" PRId64 "", info->offset, info->size,
301               info->presentationTimeUs);
302         if (info->presentationTimeUs < fromTime[trackID]) {
303             ALOGV("skipped");
304             continue;
305         }
306         if (AMediaMuxer_writeSampleData(muxer, mOutIndexMap.at(trackIndex), mBuffer, info) !=
307             AMEDIA_OK) {
308             delete[] frameCount;
309             return false;
310         } else {
311             ++samplesWritten;
312         }
313     } while ((lastSplit) ? (i < mTrackIdxOrder.size())
314                          : ((samplesWritten < numSamples) && (i < mTrackIdxOrder.size())));
315     ALOGV("samplesWritten:%u", samplesWritten);
316 
317     delete[] frameCount;
318     return true;
319 }
320 
muxMedia(AMediaMuxer * muxer)321 bool MuxerNativeTestHelper::muxMedia(AMediaMuxer* muxer) {
322     return (registerTrack(muxer) && (AMediaMuxer_start(muxer) == AMEDIA_OK) &&
323             insertSampleData(muxer) && (AMediaMuxer_stop(muxer) == AMEDIA_OK));
324 }
325 
appendMedia(AMediaMuxer * muxer,uint32_t fromIndex,uint32_t toIndex)326 bool MuxerNativeTestHelper::appendMedia(AMediaMuxer *muxer, uint32_t fromIndex, uint32_t toIndex) {
327     if (__builtin_available(android __TRANSCODING_MIN_API__, *)) {
328         ALOGV("fromIndex:%u, toIndex:%u", fromIndex, toIndex);
329         if (fromIndex == 0) {
330             registerTrack(muxer);
331         } else {
332             size_t trackCount = AMediaMuxer_getTrackCount(muxer);
333             ALOGV("appendMedia:trackCount:%zu", trackCount);
334             for(size_t i = 0; i < trackCount; ++i) {
335                 ALOGV("track i:%zu", i);
336                 ALOGV("%s", AMediaFormat_toString(AMediaMuxer_getTrackFormat(muxer, i)));
337                 ALOGV("%s", AMediaFormat_toString(mFormat[i]));
338                 for(size_t j = 0; j < mFormat.size(); ++j) {
339                     const char* thatMediaType = nullptr;
340                     AMediaFormat_getString(AMediaMuxer_getTrackFormat(muxer, i),
341                                                 AMEDIAFORMAT_KEY_MIME, &thatMediaType);
342                     const char* thisMediaType = nullptr;
343                     AMediaFormat_getString(mFormat[j], AMEDIAFORMAT_KEY_MIME, &thisMediaType);
344                     ALOGV("strlen(thisMediaType)%zu", strlen(thisMediaType));
345                     if (strcmp(thatMediaType, thisMediaType) == 0) {
346                         ALOGV("appendMedia:i:%zu, j:%zu", i, j);
347                         mOutIndexMap[j]=i;
348                     }
349                 }
350             }
351         }
352         AMediaMuxer_start(muxer);
353         bool res = writeAFewSamplesData(muxer, fromIndex, toIndex);
354         AMediaMuxer_stop(muxer);
355         return res;
356     } else {
357         return false;
358     }
359 }
360 
appendMediaFromTime(AMediaMuxer * muxer,int64_t * appendFromTime,uint32_t numSamples,bool lastSplit)361 bool MuxerNativeTestHelper::appendMediaFromTime(AMediaMuxer *muxer, int64_t *appendFromTime,
362                                                         uint32_t numSamples, bool lastSplit) {
363     if (__builtin_available(android __TRANSCODING_MIN_API__, *)) {
364         size_t trackCount = AMediaMuxer_getTrackCount(muxer);
365         ALOGV("appendMediaFromTime:trackCount:%zu", trackCount);
366         for(size_t i = 0; i < trackCount; ++i) {
367             ALOGV("track i:%zu", i);
368             ALOGV("%s", AMediaFormat_toString(AMediaMuxer_getTrackFormat(muxer, i)));
369             ALOGV("%s", AMediaFormat_toString(mFormat[i]));
370             for(size_t j = 0; j < mFormat.size(); ++j) {
371                 const char* thatMediaType = nullptr;
372                 AMediaFormat_getString(AMediaMuxer_getTrackFormat(muxer, i),
373                                        AMEDIAFORMAT_KEY_MIME, &thatMediaType);
374                 const char* thisMediaType = nullptr;
375                 AMediaFormat_getString(mFormat[j], AMEDIAFORMAT_KEY_MIME, &thisMediaType);
376                 ALOGV("strlen(thisMediaType)%zu", strlen(thisMediaType));
377                 if (strcmp(thatMediaType, thisMediaType) == 0) {
378                     ALOGV("appendMediaFromTime:i:%zu, j:%zu", i, j);
379                     mOutIndexMap[j]=i;
380                 }
381             }
382         }
383         AMediaMuxer_start(muxer);
384         bool res = writeAFewSamplesDataFromTime(muxer, appendFromTime, numSamples, lastSplit);
385         AMediaMuxer_stop(muxer);
386         return res;
387     } else {
388         return false;
389     }
390 }
combineMedias(AMediaMuxer * muxer,MuxerNativeTestHelper * that,const int * repeater)391 bool MuxerNativeTestHelper::combineMedias(AMediaMuxer* muxer, MuxerNativeTestHelper* that,
392                                           const int* repeater) {
393     if (that == nullptr) return false;
394     if (repeater == nullptr) return false;
395 
396     // register tracks
397     int totalTracksToAdd = repeater[0] * this->mTrackCount + repeater[1] * that->mTrackCount;
398     int outIndexMap[totalTracksToAdd];
399     MuxerNativeTestHelper* group[2]{this, that};
400     for (int k = 0, idx = 0; k < 2; k++) {
401         for (int j = 0; j < repeater[k]; j++) {
402             for (AMediaFormat* format : group[k]->mFormat) {
403                 int dstIndex = AMediaMuxer_addTrack(muxer, format);
404                 if (dstIndex < 0) return false;
405                 outIndexMap[idx++] = dstIndex;
406             }
407         }
408     }
409     // start
410     if (AMediaMuxer_start(muxer) != AMEDIA_OK) return false;
411     // write sample data
412     // write all registered tracks in planar order viz all samples of a track A then all
413     // samples of track B, ...
414     for (int k = 0, idx = 0; k < 2; k++) {
415         for (int j = 0; j < repeater[k]; j++) {
416             for (int i = 0; i < group[k]->mTrackCount; i++) {
417                 for (int p = 0; p < group[k]->mBufferInfo[i].size(); p++) {
418                     AMediaCodecBufferInfo* info = group[k]->mBufferInfo[i][p];
419                     if (AMediaMuxer_writeSampleData(muxer, outIndexMap[idx], group[k]->mBuffer,
420                                                     info) != AMEDIA_OK) {
421                         return false;
422                     }
423                     ALOGV("Track: %d Timestamp: %" PRId64 "", outIndexMap[idx],
424                           info->presentationTimeUs);
425                 }
426                 idx++;
427             }
428         }
429     }
430     // stop
431     return (AMediaMuxer_stop(muxer) == AMEDIA_OK);
432 }
433 
434 // returns true if 'this' stream is a subset of 'that'. That is all tracks in current media
435 // stream are present in ref media stream
isSubsetOf(MuxerNativeTestHelper * that)436 bool MuxerNativeTestHelper::isSubsetOf(MuxerNativeTestHelper* that) {
437     if (this == that) return true;
438     if (that == nullptr) return false;
439 
440     for (int i = 0; i < mTrackCount; i++) {
441         AMediaFormat* thisFormat = mFormat[i];
442         const char* thisMediaType = nullptr;
443         AMediaFormat_getString(thisFormat, AMEDIAFORMAT_KEY_MIME, &thisMediaType);
444         int tolerance = !strncmp(thisMediaType, "video/", strlen("video/")) ? STTS_TOLERANCE_US : 0;
445         tolerance += 1;  // rounding error
446         int j = 0;
447         for (; j < that->mTrackCount; j++) {
448             AMediaFormat* thatFormat = that->mFormat[j];
449             const char* thatMediaType = nullptr;
450             AMediaFormat_getString(thatFormat, AMEDIAFORMAT_KEY_MIME, &thatMediaType);
451             if (thisMediaType != nullptr && thatMediaType != nullptr &&
452                 !strcmp(thisMediaType, thatMediaType)) {
453                 if (!isFormatSimilar(thisFormat, thatFormat)) continue;
454                 if (mBufferInfo[i].size() <= that->mBufferInfo[j].size()) {
455                     int tolerance = !strncmp(thisMediaType, "video/", strlen("video/"))
456                                             ? STTS_TOLERANCE_US
457                                             : 0;
458                     tolerance += 1;  // rounding error
459                     int k = 0;
460                     for (; k < mBufferInfo[i].size(); k++) {
461                         ALOGV("k:%d", k);
462                         AMediaCodecBufferInfo* thisInfo = mBufferInfo[i][k];
463                         AMediaCodecBufferInfo* thatInfo = that->mBufferInfo[j][k];
464                         if (thisInfo->flags != thatInfo->flags) {
465                             ALOGD("flags this:%u, that:%u", thisInfo->flags, thatInfo->flags);
466                             break;
467                         }
468                         if (thisInfo->size != thatInfo->size) {
469                             ALOGD("size  this:%d, that:%d", thisInfo->size, thatInfo->size);
470                             break;
471                         } else if (memcmp(mBuffer + thisInfo->offset,
472                                           that->mBuffer + thatInfo->offset, thisInfo->size)) {
473                             ALOGD("memcmp failed");
474                             break;
475                         }
476                         if (abs(thisInfo->presentationTimeUs - thatInfo->presentationTimeUs) >
477                             tolerance) {
478                             ALOGD("time this:%lld, that:%lld",
479                                   (long long)thisInfo->presentationTimeUs,
480                                   (long long)thatInfo->presentationTimeUs);
481                             break;
482                         }
483                     }
484                     if (k == mBufferInfo[i].size()) break;
485                 }
486             }
487         }
488         if (j == that->mTrackCount) {
489             AMediaFormat_getString(thisFormat, AMEDIAFORMAT_KEY_MIME, &thisMediaType);
490             ALOGV("For media type %s, Couldn't find a match", thisMediaType);
491             return false;
492         }
493     }
494     return true;
495 }
496 
offsetTimeStamp(int64_t tsAudioOffsetUs,int64_t tsVideoOffsetUs,int sampleOffset)497 void MuxerNativeTestHelper::offsetTimeStamp(int64_t tsAudioOffsetUs, int64_t tsVideoOffsetUs,
498                                             int sampleOffset) {
499     // offset pts of samples from index sampleOffset till the end by tsOffset for each audio and
500     // video track
501     for (int trackID = 0; trackID < mTrackCount; trackID++) {
502         int64_t tsOffsetUs = 0;
503         const char* thisMediaType = nullptr;
504         AMediaFormat_getString(mFormat[trackID], AMEDIAFORMAT_KEY_MIME, &thisMediaType);
505         if (thisMediaType != nullptr) {
506             if (strncmp(thisMediaType, "video/", strlen("video/")) == 0) {
507                 tsOffsetUs = tsVideoOffsetUs;
508             } else if (strncmp(thisMediaType, "audio/", strlen("audio/")) == 0) {
509                 tsOffsetUs = tsAudioOffsetUs;
510             }
511             for (int i = sampleOffset; i < mBufferInfo[trackID].size(); i++) {
512                 AMediaCodecBufferInfo* info = mBufferInfo[trackID][i];
513                 info->presentationTimeUs += tsOffsetUs;
514             }
515         }
516     }
517 }
518 
isCodecContainerPairValid(MuxerFormat format,const char * mediaType)519 static bool isCodecContainerPairValid(MuxerFormat format, const char* mediaType) {
520     static const std::map<MuxerFormat, std::vector<const char*>> codecListforType = {
521             {OUTPUT_FORMAT_MPEG_4,
522              {AMEDIA_MIMETYPE_VIDEO_MPEG4, AMEDIA_MIMETYPE_VIDEO_H263, AMEDIA_MIMETYPE_VIDEO_AVC,
523               AMEDIA_MIMETYPE_VIDEO_HEVC, AMEDIA_MIMETYPE_AUDIO_AAC}},
524             {OUTPUT_FORMAT_WEBM,
525              {AMEDIA_MIMETYPE_VIDEO_VP8, AMEDIA_MIMETYPE_VIDEO_VP9, AMEDIA_MIMETYPE_AUDIO_VORBIS,
526               AMEDIA_MIMETYPE_AUDIO_OPUS}},
527             {OUTPUT_FORMAT_THREE_GPP,
528              {AMEDIA_MIMETYPE_VIDEO_MPEG4, AMEDIA_MIMETYPE_VIDEO_H263, AMEDIA_MIMETYPE_VIDEO_AVC,
529               AMEDIA_MIMETYPE_AUDIO_AAC, AMEDIA_MIMETYPE_AUDIO_AMR_NB,
530               AMEDIA_MIMETYPE_AUDIO_AMR_WB}},
531             {OUTPUT_FORMAT_OGG, {AMEDIA_MIMETYPE_AUDIO_OPUS}},
532     };
533 
534     if (format == OUTPUT_FORMAT_MPEG_4 &&
535         strncmp(mediaType, "application/", strlen("application/")) == 0)
536         return true;
537 
538     auto it = codecListforType.find(format);
539     if (it != codecListforType.end())
540         for (auto it2 : it->second)
541             if (strcmp(it2, mediaType) == 0) return true;
542 
543     return false;
544 }
545 
nativeTestSetLocation(JNIEnv * env,jobject,jint jformat,jstring jsrcPath,jstring jdstPath)546 static jboolean nativeTestSetLocation(JNIEnv* env, jobject, jint jformat, jstring jsrcPath,
547                                       jstring jdstPath) {
548     bool isPass = true;
549     bool isGeoDataSupported;
550     const float atlanticLat = 14.59f;
551     const float atlanticLong = 28.67f;
552     const float tooFarNorth = 90.5f;
553     const float tooFarWest = -180.5f;
554     const float tooFarSouth = -90.5f;
555     const float tooFarEast = 180.5f;
556     const float annapurnaLat = 28.59f;
557     const float annapurnaLong = 83.82f;
558     const char* cdstPath = env->GetStringUTFChars(jdstPath, nullptr);
559     FILE* ofp = fopen(cdstPath, "wbe+");
560     if (ofp) {
561         AMediaMuxer* muxer = AMediaMuxer_new(fileno(ofp), (OutputFormat)jformat);
562         media_status_t status = AMediaMuxer_setLocation(muxer, tooFarNorth, atlanticLong);
563         if (status == AMEDIA_OK) {
564             isPass = false;
565             ALOGE("setLocation succeeds on bad args: (%f, %f)", tooFarNorth, atlanticLong);
566         }
567         status = AMediaMuxer_setLocation(muxer, tooFarSouth, atlanticLong);
568         if (status == AMEDIA_OK) {
569             isPass = false;
570             ALOGE("setLocation succeeds on bad args: (%f, %f)", tooFarSouth, atlanticLong);
571         }
572         status = AMediaMuxer_setLocation(muxer, atlanticLat, tooFarWest);
573         if (status == AMEDIA_OK) {
574             isPass = false;
575             ALOGE("setLocation succeeds on bad args: (%f, %f)", atlanticLat, tooFarWest);
576         }
577         status = AMediaMuxer_setLocation(muxer, atlanticLat, tooFarEast);
578         if (status == AMEDIA_OK) {
579             isPass = false;
580             ALOGE("setLocation succeeds on bad args: (%f, %f)", atlanticLat, tooFarEast);
581         }
582         status = AMediaMuxer_setLocation(muxer, tooFarNorth, tooFarWest);
583         if (status == AMEDIA_OK) {
584             isPass = false;
585             ALOGE("setLocation succeeds on bad args: (%f, %f)", tooFarNorth, tooFarWest);
586         }
587         status = AMediaMuxer_setLocation(muxer, atlanticLat, atlanticLong);
588         isGeoDataSupported = (status == AMEDIA_OK);
589         if (isGeoDataSupported) {
590             status = AMediaMuxer_setLocation(muxer, annapurnaLat, annapurnaLong);
591             if (status != AMEDIA_OK) {
592                 isPass = false;
593                 ALOGE("setLocation fails on args: (%f, %f)", annapurnaLat, annapurnaLong);
594             }
595         } else {
596             isPass &= ((MuxerFormat)jformat != OUTPUT_FORMAT_MPEG_4 &&
597                        (MuxerFormat)jformat != OUTPUT_FORMAT_THREE_GPP);
598         }
599         const char* csrcPath = env->GetStringUTFChars(jsrcPath, nullptr);
600         auto* mediaInfo = new MuxerNativeTestHelper(csrcPath);
601         if (mediaInfo->registerTrack(muxer) && AMediaMuxer_start(muxer) == AMEDIA_OK) {
602             status = AMediaMuxer_setLocation(muxer, atlanticLat, atlanticLong);
603             if (status == AMEDIA_OK) {
604                 isPass = false;
605                 ALOGE("setLocation succeeds after starting the muxer");
606             }
607             if (mediaInfo->insertSampleData(muxer) && AMediaMuxer_stop(muxer) == AMEDIA_OK) {
608                 status = AMediaMuxer_setLocation(muxer, atlanticLat, atlanticLong);
609                 if (status == AMEDIA_OK) {
610                     isPass = false;
611                     ALOGE("setLocation succeeds after stopping the muxer");
612                 }
613             } else {
614                 isPass = false;
615                 ALOGE("failed to writeSampleData or stop muxer");
616             }
617         } else {
618             isPass = false;
619             ALOGE("failed to addTrack or start muxer");
620         }
621         delete mediaInfo;
622         env->ReleaseStringUTFChars(jsrcPath, csrcPath);
623         AMediaMuxer_delete(muxer);
624         fclose(ofp);
625     } else {
626         isPass = false;
627         ALOGE("failed to open output file %s", cdstPath);
628     }
629     env->ReleaseStringUTFChars(jdstPath, cdstPath);
630     return static_cast<jboolean>(isPass);
631 }
632 
nativeTestSetOrientationHint(JNIEnv * env,jobject,jint jformat,jstring jsrcPath,jstring jdstPath)633 static jboolean nativeTestSetOrientationHint(JNIEnv* env, jobject, jint jformat, jstring jsrcPath,
634                                              jstring jdstPath) {
635     bool isPass = true;
636     bool isOrientationSupported;
637     const int badRotation[] = {360, 45, -90};
638     const int oldRotation = 90;
639     const int currRotation = 180;
640     const char* cdstPath = env->GetStringUTFChars(jdstPath, nullptr);
641     FILE* ofp = fopen(cdstPath, "wbe+");
642     if (ofp) {
643         AMediaMuxer* muxer = AMediaMuxer_new(fileno(ofp), (OutputFormat)jformat);
644         media_status_t status;
645         for (int degrees : badRotation) {
646             status = AMediaMuxer_setOrientationHint(muxer, degrees);
647             if (status == AMEDIA_OK) {
648                 isPass = false;
649                 ALOGE("setOrientationHint succeeds on bad args: %d", degrees);
650             }
651         }
652         status = AMediaMuxer_setOrientationHint(muxer, oldRotation);
653         isOrientationSupported = (status == AMEDIA_OK);
654         if (isOrientationSupported) {
655             status = AMediaMuxer_setOrientationHint(muxer, currRotation);
656             if (status != AMEDIA_OK) {
657                 isPass = false;
658                 ALOGE("setOrientationHint fails on args: %d", currRotation);
659             }
660         } else {
661             isPass &= ((MuxerFormat)jformat != OUTPUT_FORMAT_MPEG_4 &&
662                        (MuxerFormat)jformat != OUTPUT_FORMAT_THREE_GPP);
663         }
664         const char* csrcPath = env->GetStringUTFChars(jsrcPath, nullptr);
665         auto* mediaInfo = new MuxerNativeTestHelper(csrcPath);
666         if (mediaInfo->registerTrack(muxer) && AMediaMuxer_start(muxer) == AMEDIA_OK) {
667             status = AMediaMuxer_setOrientationHint(muxer, currRotation);
668             if (status == AMEDIA_OK) {
669                 isPass = false;
670                 ALOGE("setOrientationHint succeeds after starting the muxer");
671             }
672             if (mediaInfo->insertSampleData(muxer) && AMediaMuxer_stop(muxer) == AMEDIA_OK) {
673                 status = AMediaMuxer_setOrientationHint(muxer, currRotation);
674                 if (status == AMEDIA_OK) {
675                     isPass = false;
676                     ALOGE("setOrientationHint succeeds after stopping the muxer");
677                 }
678             } else {
679                 isPass = false;
680                 ALOGE("failed to writeSampleData or stop muxer");
681             }
682         } else {
683             isPass = false;
684             ALOGE("failed to addTrack or start muxer");
685         }
686         delete mediaInfo;
687         env->ReleaseStringUTFChars(jsrcPath, csrcPath);
688         AMediaMuxer_delete(muxer);
689         fclose(ofp);
690     } else {
691         isPass = false;
692         ALOGE("failed to open output file %s", cdstPath);
693     }
694     env->ReleaseStringUTFChars(jdstPath, cdstPath);
695     return static_cast<jboolean>(isPass);
696 }
697 
nativeTestMultiTrack(JNIEnv * env,jobject,jint jformat,jstring jsrcPathA,jstring jsrcPathB,jstring jrefPath,jstring jdstPath)698 static jboolean nativeTestMultiTrack(JNIEnv* env, jobject, jint jformat, jstring jsrcPathA,
699                                      jstring jsrcPathB, jstring jrefPath, jstring jdstPath) {
700     bool isPass = true;
701     const char* csrcPathA = env->GetStringUTFChars(jsrcPathA, nullptr);
702     const char* csrcPathB = env->GetStringUTFChars(jsrcPathB, nullptr);
703     auto* mediaInfoA = new MuxerNativeTestHelper(csrcPathA);
704     auto* mediaInfoB = new MuxerNativeTestHelper(csrcPathB);
705     if (mediaInfoA->getTrackCount() == 1 && mediaInfoB->getTrackCount() == 1) {
706         const char* crefPath = env->GetStringUTFChars(jrefPath, nullptr);
707         // number of times to repeat {mSrcFileA, mSrcFileB} in Output
708         // values should be in sync with testMultiTrack
709         static const int numTracks[][2] = {{1, 1}, {2, 0}, {0, 2}, {1, 2}, {2, 1}, {2, 2}};
710         // prepare reference
711         FILE* rfp = fopen(crefPath, "wbe+");
712         if (rfp) {
713             AMediaMuxer* muxer = AMediaMuxer_new(fileno(rfp), (OutputFormat)jformat);
714             bool muxStatus = mediaInfoA->combineMedias(muxer, mediaInfoB, numTracks[0]);
715             AMediaMuxer_delete(muxer);
716             fclose(rfp);
717             if (muxStatus) {
718                 auto* refInfo = new MuxerNativeTestHelper(crefPath);
719                 if (!mediaInfoA->isSubsetOf(refInfo) || !mediaInfoB->isSubsetOf(refInfo)) {
720                     isPass = false;
721                     ALOGE("testMultiTrack: inputs: %s %s, fmt: %d, error ! muxing src A and src B "
722                           "failed", csrcPathA, csrcPathB, jformat);
723                 } else {
724                     const char* cdstPath = env->GetStringUTFChars(jdstPath, nullptr);
725                     for (int i = 1; i < sizeof(numTracks) / sizeof(numTracks[0]) && isPass; i++) {
726                         FILE* ofp = fopen(cdstPath, "wbe+");
727                         if (ofp) {
728                             muxer = AMediaMuxer_new(fileno(ofp), (OutputFormat)jformat);
729                             bool status =
730                                     mediaInfoA->combineMedias(muxer, mediaInfoB, numTracks[i]);
731                             AMediaMuxer_delete(muxer);
732                             fclose(ofp);
733                             if (status) {
734                                 auto* dstInfo = new MuxerNativeTestHelper(cdstPath);
735                                 if (!dstInfo->isSubsetOf(refInfo)) {
736                                     isPass = false;
737                                     ALOGE("testMultiTrack: inputs: %s %s, fmt: %d, error ! muxing "
738                                           "src A: %d, src B: %d failed", csrcPathA, csrcPathB,
739                                           jformat, numTracks[i][0], numTracks[i][1]);
740                                 }
741                                 delete dstInfo;
742                             } else {
743                                 if ((MuxerFormat)jformat != OUTPUT_FORMAT_MPEG_4) {
744                                     isPass = false;
745                                     ALOGE("testMultiTrack: inputs: %s %s, fmt: %d, error ! muxing "
746                                           "src A: %d, src B: %d failed", csrcPathA, csrcPathB,
747                                           jformat, numTracks[i][0], numTracks[i][1]);
748                                 }
749                             }
750                         } else {
751                             isPass = false;
752                             ALOGE("failed to open output file %s", cdstPath);
753                         }
754                     }
755                     env->ReleaseStringUTFChars(jdstPath, cdstPath);
756                 }
757                 delete refInfo;
758             } else {
759                 if ((MuxerFormat)jformat != OUTPUT_FORMAT_OGG) {
760                     isPass = false;
761                     ALOGE("testMultiTrack: inputs: %s %s, fmt: %d, error ! muxing src A and src B "
762                           "failed", csrcPathA, csrcPathB, jformat);
763                 }
764             }
765         } else {
766             isPass = false;
767             ALOGE("failed to open reference output file %s", crefPath);
768         }
769         env->ReleaseStringUTFChars(jrefPath, crefPath);
770     } else {
771         isPass = false;
772         if (mediaInfoA->getTrackCount() != 1) {
773             ALOGE("error: file %s, track count exp/rec - %d/%d", csrcPathA, 1,
774                   mediaInfoA->getTrackCount());
775         }
776         if (mediaInfoB->getTrackCount() != 1) {
777             ALOGE("error: file %s, track count exp/rec - %d/%d", csrcPathB, 1,
778                   mediaInfoB->getTrackCount());
779         }
780     }
781     env->ReleaseStringUTFChars(jsrcPathA, csrcPathA);
782     env->ReleaseStringUTFChars(jsrcPathB, csrcPathB);
783     delete mediaInfoA;
784     delete mediaInfoB;
785     return static_cast<jboolean>(isPass);
786 }
787 
nativeTestOffsetPts(JNIEnv * env,jobject,jint format,jstring jsrcPath,jstring jdstPath,jintArray joffsetIndices)788 static jboolean nativeTestOffsetPts(JNIEnv* env, jobject, jint format, jstring jsrcPath,
789                                     jstring jdstPath, jintArray joffsetIndices) {
790     bool isPass = true;
791     // values should be in sync with testOffsetPresentationTime
792     static const int64_t OFFSET_TS_AUDIO_US[4] = {-23220LL, 0LL, 200000LL, 400000LL};
793     static const int64_t OFFSET_TS_VIDEO_US[3] = {0LL, 200000LL, 400000LL};
794     jsize len = env->GetArrayLength(joffsetIndices);
795     jint* coffsetIndices = env->GetIntArrayElements(joffsetIndices, nullptr);
796     const char* csrcPath = env->GetStringUTFChars(jsrcPath, nullptr);
797     const char* cdstPath = env->GetStringUTFChars(jdstPath, nullptr);
798     auto* mediaInfo = new MuxerNativeTestHelper(csrcPath);
799     if (mediaInfo->getTrackCount() != 0) {
800         for (int64_t audioOffsetUs : OFFSET_TS_AUDIO_US) {
801             for (int64_t videoOffsetUs : OFFSET_TS_VIDEO_US) {
802                 for (int i = 0; i < len; i++) {
803                     mediaInfo->offsetTimeStamp(audioOffsetUs, videoOffsetUs, coffsetIndices[i]);
804                 }
805                 FILE* ofp = fopen(cdstPath, "wbe+");
806                 if (ofp) {
807                     AMediaMuxer* muxer = AMediaMuxer_new(fileno(ofp), (OutputFormat)format);
808                     mediaInfo->muxMedia(muxer);
809                     AMediaMuxer_delete(muxer);
810                     fclose(ofp);
811                     auto* outInfo = new MuxerNativeTestHelper(cdstPath);
812                     isPass = mediaInfo->isSubsetOf(outInfo);
813                     if (!isPass) {
814                         ALOGE("Validation failed after adding timestamp offsets audio: %lld,"
815                               " video: %lld", (long long) audioOffsetUs, (long long) videoOffsetUs);
816                     }
817                     delete outInfo;
818                 } else {
819                     isPass = false;
820                     ALOGE("failed to open output file %s", cdstPath);
821                 }
822                 for (int i = len - 1; i >= 0; i--) {
823                     mediaInfo->offsetTimeStamp(-audioOffsetUs, -videoOffsetUs, coffsetIndices[i]);
824                 }
825             }
826         }
827     } else {
828         isPass = false;
829         ALOGE("no valid track found in input file %s", csrcPath);
830     }
831     env->ReleaseStringUTFChars(jdstPath, cdstPath);
832     env->ReleaseStringUTFChars(jsrcPath, csrcPath);
833     env->ReleaseIntArrayElements(joffsetIndices, coffsetIndices, 0);
834     delete mediaInfo;
835     return static_cast<jboolean>(isPass);
836 }
837 
nativeTestSimpleMux(JNIEnv * env,jobject,jstring jsrcPath,jstring jdstPath,jstring jMediaType,jstring jselector)838 static jboolean nativeTestSimpleMux(JNIEnv* env, jobject, jstring jsrcPath, jstring jdstPath,
839                                     jstring jMediaType, jstring jselector) {
840     bool isPass = true;
841     const char* cMediaType = env->GetStringUTFChars(jMediaType, nullptr);
842     const char* csrcPath = env->GetStringUTFChars(jsrcPath, nullptr);
843     const char* cselector = env->GetStringUTFChars(jselector, nullptr);
844     auto* mediaInfo = new MuxerNativeTestHelper(csrcPath, cMediaType);
845     static const std::map<MuxerFormat, const char*> formatStringPair = {
846             {OUTPUT_FORMAT_MPEG_4, "mp4"},
847             {OUTPUT_FORMAT_WEBM, "webm"},
848             {OUTPUT_FORMAT_THREE_GPP, "3gp"},
849             {OUTPUT_FORMAT_HEIF, "heif"},
850             {OUTPUT_FORMAT_OGG, "ogg"}};
851     if (mediaInfo->getTrackCount() == 1) {
852         const char* cdstPath = env->GetStringUTFChars(jdstPath, nullptr);
853         for (int fmt = OUTPUT_FORMAT_START; fmt <= OUTPUT_FORMAT_LIST_END && isPass; fmt++) {
854             auto it = formatStringPair.find((MuxerFormat)fmt);
855             if (it == formatStringPair.end() || strstr(cselector, it->second) == nullptr) {
856                 continue;
857             }
858             if (fmt == OUTPUT_FORMAT_WEBM) continue;  // TODO(b/146923551)
859             FILE* ofp = fopen(cdstPath, "wbe+");
860             if (ofp) {
861                 AMediaMuxer* muxer = AMediaMuxer_new(fileno(ofp), (OutputFormat)fmt);
862                 bool muxStatus = mediaInfo->muxMedia(muxer);
863                 bool result = true;
864                 AMediaMuxer_delete(muxer);
865                 fclose(ofp);
866                 if (muxStatus) {
867                     auto* outInfo = new MuxerNativeTestHelper(cdstPath, cMediaType);
868                     result = mediaInfo->isSubsetOf(outInfo);
869                     delete outInfo;
870                 }
871                 if ((muxStatus && !result) ||
872                     (!muxStatus && isCodecContainerPairValid((MuxerFormat)fmt, cMediaType))) {
873                     isPass = false;
874                     ALOGE("error: file %s, mediaType %s, output != clone(input) for format %d",
875                           csrcPath, cMediaType, fmt);
876                 }
877             } else {
878                 isPass = false;
879                 ALOGE("error: file %s, mediaType %s, failed to open output file %s", csrcPath,
880                       cMediaType, cdstPath);
881             }
882         }
883         env->ReleaseStringUTFChars(jdstPath, cdstPath);
884     } else {
885         isPass = false;
886         ALOGE("error: file %s, mediaType %s, track count exp/rec - %d/%d", csrcPath, cMediaType, 1,
887               mediaInfo->getTrackCount());
888     }
889     env->ReleaseStringUTFChars(jselector, cselector);
890     env->ReleaseStringUTFChars(jsrcPath, csrcPath);
891     env->ReleaseStringUTFChars(jMediaType, cMediaType);
892     delete mediaInfo;
893     return static_cast<jboolean>(isPass);
894 }
895 
896 /* Check whether AMediaMuxer_getTrackCount works as expected.
897  */
nativeTestGetTrackCount(JNIEnv * env,jobject,jstring jsrcPath,jstring jdstPath,jint jformat,jint jtrackCount)898 static jboolean nativeTestGetTrackCount(JNIEnv* env, jobject, jstring jsrcPath, jstring jdstPath,
899                                             jint jformat, jint jtrackCount) {
900     bool isPass = true;
901     if (__builtin_available(android __TRANSCODING_MIN_API__, *)) {
902         const char* csrcPath = env->GetStringUTFChars(jsrcPath, nullptr);
903         const char* cdstPath = env->GetStringUTFChars(jdstPath, nullptr);
904         FILE* ofp = fopen(cdstPath, "w+");
905         if (ofp) {
906             AMediaMuxer *muxer = AMediaMuxer_new(fileno(ofp), (OutputFormat)jformat);
907             if (muxer) {
908                 auto* mediaInfo = new MuxerNativeTestHelper(csrcPath);
909                 if (!mediaInfo->registerTrack(muxer)) {
910                     isPass = false;
911                     ALOGE("register track failed");
912                 }
913                 if (AMediaMuxer_getTrackCount(muxer) != jtrackCount) {
914                     isPass = false;
915                     ALOGE("track counts are not equal");
916                 }
917                 delete mediaInfo;
918                 AMediaMuxer_delete(muxer);
919             } else {
920                 isPass = false;
921                 ALOGE("Failed to create muxer");
922             }
923             fclose(ofp);
924         } else {
925             isPass = false;
926             ALOGE("file open error: file  %s", csrcPath);
927         }
928         env->ReleaseStringUTFChars(jsrcPath, csrcPath);
929         env->ReleaseStringUTFChars(jdstPath, cdstPath);
930     } else {
931         isPass = false;
932     }
933     return static_cast<jboolean>(isPass);
934 }
935 
936 /* Check whether AMediaMuxer_getTrackCount works as expected when the file is opened in
937  * append mode.
938  */
nativeTestAppendGetTrackCount(JNIEnv * env,jobject,jstring jsrcPath,jint jtrackCount)939 static jboolean nativeTestAppendGetTrackCount(JNIEnv* env, jobject, jstring jsrcPath,
940                                                         jint jtrackCount) {
941     bool isPass = true;
942     if (__builtin_available(android __TRANSCODING_MIN_API__, *)) {
943         const char* csrcPath = env->GetStringUTFChars(jsrcPath, nullptr);
944         for (unsigned int mode = AMEDIAMUXER_APPEND_IGNORE_LAST_VIDEO_GOP;
945                     mode <= AMEDIAMUXER_APPEND_TO_EXISTING_DATA; ++mode) {
946             ALOGV("mode:%u", mode);
947             FILE* ofp = fopen(csrcPath, "r");
948             if (ofp) {
949                 AMediaMuxer *muxer = AMediaMuxer_append(fileno(ofp), (AppendMode)mode);
950                 if (muxer) {
951                     ssize_t trackCount = AMediaMuxer_getTrackCount(muxer);
952                     if ( trackCount != jtrackCount) {
953                         isPass = false;
954                         ALOGE("trackcounts are not equal, trackCount:%ld vs jtrackCount:%d",
955                                     (long)trackCount, jtrackCount);
956                     }
957                     AMediaMuxer_delete(muxer);
958                 } else {
959                     isPass = false;
960                     ALOGE("Failed to create muxer");
961                 }
962                 fclose(ofp);
963                 ofp = nullptr;
964             } else {
965                 isPass = false;
966                 ALOGE("file open error: file  %s", csrcPath);
967             }
968         }
969         env->ReleaseStringUTFChars(jsrcPath, csrcPath);
970     } else {
971         isPass = false;
972     }
973     return static_cast<jboolean>(isPass);
974 }
975 
976 /* Checks whether AMediaMuxer_getTrackFormat works as expected in muxer mode.
977  */
nativeTestGetTrackFormat(JNIEnv * env,jobject,jstring jsrcPath,jstring jdstPath,jint joutFormat)978 static jboolean nativeTestGetTrackFormat(JNIEnv* env, jobject, jstring jsrcPath, jstring jdstPath,
979                                                     jint joutFormat) {
980     bool isPass = true;
981     if (__builtin_available(android __TRANSCODING_MIN_API__, *)) {
982         const char* csrcPath = env->GetStringUTFChars(jsrcPath, nullptr);
983         const char* cdstPath = env->GetStringUTFChars(jdstPath, nullptr);
984         FILE* ofp = fopen(cdstPath, "w+");
985         if (ofp) {
986             AMediaMuxer *muxer = AMediaMuxer_new(fileno(ofp), (OutputFormat)joutFormat);
987             if (muxer) {
988                 auto* mediaInfo = new MuxerNativeTestHelper(csrcPath);
989                 if (!mediaInfo->registerTrack(muxer)) {
990                     isPass = false;
991                     ALOGE("register track failed");
992                 }
993                 for(int i = 0 ; i < mediaInfo->getTrackCount(); ++i ) {
994                     if (!isFormatSimilar(mediaInfo->getTrackFormat(i),
995                             AMediaMuxer_getTrackFormat(muxer, i))) {
996                         isPass = false;
997                         ALOGE("track formats are not similar");
998                     }
999                 }
1000                 delete mediaInfo;
1001                 AMediaMuxer_delete(muxer);
1002             } else {
1003                 isPass = false;
1004                 ALOGE("Failed to create muxer");
1005             }
1006             fclose(ofp);
1007         } else {
1008             isPass = false;
1009             ALOGE("file open error: file  %s", csrcPath);
1010         }
1011         env->ReleaseStringUTFChars(jsrcPath, csrcPath);
1012         env->ReleaseStringUTFChars(jdstPath, cdstPath);
1013     } else {
1014         isPass = false;
1015     }
1016     return static_cast<jboolean>(isPass);
1017 }
1018 
1019 /* Checks whether AMediaMuxer_getTrackFormat works as expected when the file is opened in
1020  * append mode.
1021  */
nativeTestAppendGetTrackFormat(JNIEnv * env,jobject,jstring jsrcPath)1022 static jboolean nativeTestAppendGetTrackFormat(JNIEnv* env, jobject, jstring jsrcPath) {
1023     bool isPass = true;
1024     if (__builtin_available(android __TRANSCODING_MIN_API__, *)) {
1025         const char* csrcPath = env->GetStringUTFChars(jsrcPath, nullptr);
1026         for (unsigned int mode = AMEDIAMUXER_APPEND_IGNORE_LAST_VIDEO_GOP;
1027                     mode <= AMEDIAMUXER_APPEND_TO_EXISTING_DATA; ++mode) {
1028             ALOGV("mode:%u", mode);
1029             FILE* ofp = fopen(csrcPath, "r");
1030             if (ofp) {
1031                 AMediaMuxer *muxer = AMediaMuxer_append(fileno(ofp), (AppendMode)mode);
1032                 if (muxer) {
1033                     auto* mediaInfo = new MuxerNativeTestHelper(csrcPath);
1034                     for(int i = 0 ; i < mediaInfo->getTrackCount(); ++i ) {
1035                         if (!isFormatSimilar(mediaInfo->getTrackFormat(i),
1036                                 AMediaMuxer_getTrackFormat(muxer, i))) {
1037                             isPass = false;
1038                             ALOGE("track formats are not similar");
1039                         }
1040                     }
1041                     delete mediaInfo;
1042                     AMediaMuxer_delete(muxer);
1043                 } else {
1044                     isPass = false;
1045                     ALOGE("Failed to create muxer");
1046                 }
1047                 fclose(ofp);
1048             } else {
1049                 isPass = false;
1050                 ALOGE("file open error: file  %s", csrcPath);
1051             }
1052         }
1053         env->ReleaseStringUTFChars(jsrcPath, csrcPath);
1054     }
1055     else {
1056         isPass = false;
1057     }
1058     return static_cast<jboolean>(isPass);
1059 }
1060 
1061 /*
1062  * Checks if appending media data to the end of existing media data in a file works good.
1063  * Mode : AMEDIAMUXER_APPEND_TO_EXISTING_DATA.  Splits the contents of source file equally
1064  * starting from one and increasing the number of splits by one for every iteration.  Starts
1065  * with writing first split into a new file and appends the rest of the contents split by split.
1066  */
nativeTestSimpleAppend(JNIEnv * env,jobject,jint joutFormat,jstring jsrcPath,jstring jdstPath)1067 static jboolean nativeTestSimpleAppend(JNIEnv* env, jobject, jint joutFormat, jstring jsrcPath,
1068                         jstring jdstPath) {
1069     bool isPass = true;
1070     if (__builtin_available(android __TRANSCODING_MIN_API__, *)) {
1071         const char* csrcPath = env->GetStringUTFChars(jsrcPath, nullptr);
1072         const char* cdstPath = env->GetStringUTFChars(jdstPath, nullptr);
1073         ALOGV("csrcPath:%s", csrcPath);
1074         ALOGV("cdstPath:%s", cdstPath);
1075         auto* mediaInfo = new MuxerNativeTestHelper(csrcPath);
1076         for (int numSplits = 1; numSplits <= 5; ++numSplits) {
1077             ALOGV("numSplits:%d", numSplits);
1078             size_t totalSampleCount = 0;
1079             AMediaMuxer *muxer = nullptr;
1080             // Start by writing first split into a new file.
1081             FILE* ofp = fopen(cdstPath, "w+");
1082             if (ofp) {
1083                 muxer = AMediaMuxer_new(fileno(ofp), (OutputFormat)joutFormat);
1084                 if (muxer) {
1085                     for(int i = 0 ; i < mediaInfo->getTrackCount(); ++i ) {
1086                         ALOGV("getSampleCount:%d:%zu", i, mediaInfo->getSampleCount(i));
1087                         totalSampleCount += mediaInfo->getSampleCount(i);
1088                     }
1089                     mediaInfo->appendMedia(muxer, 0, (totalSampleCount/numSplits)-1);
1090                     AMediaMuxer_delete(muxer);
1091                 } else {
1092                     isPass = false;
1093                     ALOGE("Failed to create muxer");
1094                 }
1095                 fclose(ofp);
1096                 ofp = nullptr;
1097                 // Check if the contents in the new file is as same as in the source file.
1098                 if (isPass) {
1099                     auto* mediaInfoDest = new MuxerNativeTestHelper(cdstPath, nullptr);
1100                     isPass = mediaInfoDest->isSubsetOf(mediaInfo);
1101                     delete mediaInfoDest;
1102                 }
1103             } else {
1104                 isPass = false;
1105                 ALOGE("failed to open output file %s", cdstPath);
1106             }
1107 
1108             // Append rest of the contents from the source file to the new file split by split.
1109             int curSplit = 1;
1110             while (curSplit < numSplits && isPass) {
1111                 ofp = fopen(cdstPath, "r+");
1112                 if (ofp) {
1113                     muxer = AMediaMuxer_append(fileno(ofp), AMEDIAMUXER_APPEND_TO_EXISTING_DATA);
1114                     if (muxer) {
1115                         ssize_t trackCount = AMediaMuxer_getTrackCount(muxer);
1116                         if (trackCount > 0) {
1117                             decltype(trackCount) tc = 0;
1118                             while(tc < trackCount) {
1119                                 AMediaFormat* format = AMediaMuxer_getTrackFormat(muxer, tc);
1120                                 int64_t val = 0;
1121                                 if (AMediaFormat_getInt64(format,
1122                                             AMEDIAFORMAT_KEY_SAMPLE_TIME_BEFORE_APPEND, &val)) {
1123                                     ALOGV("sample-time-before-append:%lld", (long long)val);
1124                                 }
1125                                 ++tc;
1126                             }
1127                             mediaInfo->appendMedia(muxer, totalSampleCount*curSplit/numSplits,
1128                                                         totalSampleCount*(curSplit+1)/numSplits-1);
1129                         } else {
1130                             isPass = false;
1131                             ALOGE("no tracks in the file");
1132                         }
1133                         AMediaMuxer_delete(muxer);
1134                     } else {
1135                         isPass = false;
1136                         ALOGE("failed to create muxer");
1137                     }
1138                     fclose(ofp);
1139                     ofp = nullptr;
1140                     if (isPass) {
1141                         auto* mediaInfoDest = new MuxerNativeTestHelper(cdstPath, nullptr);
1142                         isPass = mediaInfoDest->isSubsetOf(mediaInfo);
1143                         delete mediaInfoDest;
1144                     }
1145                 } else {
1146                     isPass = false;
1147                     ALOGE("failed to open output file %s", cdstPath);
1148                 }
1149                 ++curSplit;
1150             }
1151         }
1152         delete mediaInfo;
1153         env->ReleaseStringUTFChars(jdstPath, cdstPath);
1154         env->ReleaseStringUTFChars(jsrcPath, csrcPath);
1155     } else {
1156         isPass = false;
1157     }
1158     return static_cast<jboolean>(isPass);
1159 }
1160 
1161 /* Checks if opening a file to append data and closing it without actually appending data
1162  * works good in all append modes.
1163  */
nativeTestNoSamples(JNIEnv * env,jobject,jint joutFormat,jstring jinPath,jstring joutPath)1164 static jboolean nativeTestNoSamples(JNIEnv* env, jobject, jint joutFormat, jstring jinPath,
1165                                         jstring joutPath) {
1166     bool isPass = true;
1167     if (__builtin_available(android __TRANSCODING_MIN_API__, *)) {
1168         const char* cinPath = env->GetStringUTFChars(jinPath, nullptr);
1169         const char* coutPath = env->GetStringUTFChars(joutPath, nullptr);
1170         ALOGV("cinPath:%s", cinPath);
1171         ALOGV("coutPath:%s", coutPath);
1172         auto* mediaInfo = new MuxerNativeTestHelper(cinPath);
1173         for (unsigned int mode = AMEDIAMUXER_APPEND_IGNORE_LAST_VIDEO_GOP;
1174                     mode <= AMEDIAMUXER_APPEND_TO_EXISTING_DATA; ++mode) {
1175             if (mediaInfo->getTrackCount() != 0) {
1176                 // Create a new file and write media data to it.
1177                 FILE *ofp = fopen(coutPath, "wbe+");
1178                 if (ofp) {
1179                     AMediaMuxer *muxer = AMediaMuxer_new(fileno(ofp), (OutputFormat) joutFormat);
1180                     mediaInfo->muxMedia(muxer);
1181                     AMediaMuxer_delete(muxer);
1182                     fclose(ofp);
1183                 } else {
1184                     isPass = false;
1185                     ALOGE("failed to open output file %s", coutPath);
1186                 }
1187             } else {
1188                 isPass = false;
1189                 ALOGE("no tracks in input file");
1190             }
1191             ALOGV("after file close");
1192             FILE* ofp = fopen(coutPath, "r+");
1193             if (ofp) {
1194                 ALOGV("create append muxer");
1195                 // Open the new file in one of the append modes and close it without writing data.
1196                 AMediaMuxer *muxer = AMediaMuxer_append(fileno(ofp), (AppendMode)mode);
1197                 if (muxer) {
1198                     AMediaMuxer_start(muxer);
1199                     AMediaMuxer_stop(muxer);
1200                     ALOGV("delete append muxer");
1201                     AMediaMuxer_delete(muxer);
1202                 } else {
1203                     isPass = false;
1204                     ALOGE("failed to create muxer");
1205                 }
1206                 fclose(ofp);
1207                 ofp = nullptr;
1208             } else {
1209                 isPass = false;
1210                 ALOGE("failed to open output file to append %s", coutPath);
1211             }
1212             // Check if contents written in the new file match with contents in the original file.
1213             auto* mediaInfoOut = new MuxerNativeTestHelper(coutPath, nullptr);
1214             isPass = mediaInfoOut->isSubsetOf(mediaInfo);
1215             delete mediaInfoOut;
1216         }
1217         delete mediaInfo;
1218         env->ReleaseStringUTFChars(jinPath, cinPath);
1219         env->ReleaseStringUTFChars(joutPath, coutPath);
1220     } else {
1221         isPass = false;
1222     }
1223     return static_cast<jboolean>(isPass);
1224 }
1225 
1226 /*
1227  * Checks if appending media data in AMEDIAMUXER_APPEND_IGNORE_LAST_VIDEO_GOP mode works good.
1228  * Splits the contents of source file equally starting from one and increasing the number of
1229  * splits by one for every iteration.  Starts with writing first split into a new file and
1230  * appends the rest of the contents split by split.
1231  */
nativeTestIgnoreLastGOPAppend(JNIEnv * env,jobject,jint joutFormat,jstring jsrcPath,jstring jdstPath)1232 static jboolean nativeTestIgnoreLastGOPAppend(JNIEnv* env, jobject, jint joutFormat,
1233                                     jstring jsrcPath, jstring jdstPath) {
1234     bool isPass = true;
1235     if (__builtin_available(android __TRANSCODING_MIN_API__, *)) {
1236         const char* csrcPath = env->GetStringUTFChars(jsrcPath, nullptr);
1237         const char* cdstPath = env->GetStringUTFChars(jdstPath, nullptr);
1238         ALOGV("csrcPath:%s", csrcPath);
1239         ALOGV("cdstPath:%s", cdstPath);
1240         auto* mediaInfo = new MuxerNativeTestHelper(csrcPath);
1241         for (int numSplits = 1; numSplits <= 5 && isPass; ++numSplits) {
1242             ALOGV("numSplits:%d", numSplits);
1243             size_t totalSampleCount = 0;
1244             size_t totalSamplesWritten = 0;
1245             AMediaMuxer *muxer = nullptr;
1246             FILE* ofp = fopen(cdstPath, "w+");
1247             if (ofp) {
1248                 // Start by writing first split into a new file.
1249                 muxer = AMediaMuxer_new(fileno(ofp), (OutputFormat)joutFormat);
1250                 if (muxer) {
1251                     for(int i = 0 ; i < mediaInfo->getTrackCount(); ++i ) {
1252                         ALOGV("getSampleCount:%d:%zu", i, mediaInfo->getSampleCount(i));
1253                         totalSampleCount += mediaInfo->getSampleCount(i);
1254                     }
1255                     mediaInfo->appendMedia(muxer, 0, (totalSampleCount/numSplits)-1);
1256                     totalSamplesWritten += (totalSampleCount/numSplits);
1257                     AMediaMuxer_delete(muxer);
1258                 } else {
1259                     isPass = false;
1260                     ALOGE("Failed to create muxer");
1261                 }
1262                 fclose(ofp);
1263                 ofp = nullptr;
1264                 if (isPass) {
1265                     // Check if the contents in the new file is as same as in the source file.
1266                     auto* mediaInfoDest = new MuxerNativeTestHelper(cdstPath, nullptr);
1267                     isPass = mediaInfoDest->isSubsetOf(mediaInfo);
1268                     delete mediaInfoDest;
1269                 }
1270             } else {
1271                 isPass = false;
1272                 ALOGE("failed to open output file %s", cdstPath);
1273             }
1274 
1275             // Append rest of the contents from the source file to the new file split by split.
1276             int curSplit = 1;
1277             while (curSplit < numSplits && isPass) {
1278                 ofp = fopen(cdstPath, "r+");
1279                 if (ofp) {
1280                     muxer = AMediaMuxer_append(fileno(ofp),
1281                                 AMEDIAMUXER_APPEND_IGNORE_LAST_VIDEO_GOP);
1282                     if (muxer) {
1283                         auto trackCount = AMediaMuxer_getTrackCount(muxer);
1284                         if (trackCount > 0) {
1285                             decltype(trackCount) tc = 0;
1286                             int64_t* appendFromTime = new int64_t[trackCount]{0};
1287                             while(tc < trackCount) {
1288                                 AMediaFormat* format = AMediaMuxer_getTrackFormat(muxer, tc);
1289                                 int64_t val = 0;
1290                                 if (AMediaFormat_getInt64(format,
1291                                             AMEDIAFORMAT_KEY_SAMPLE_TIME_BEFORE_APPEND, &val)) {
1292                                     ALOGV("sample-time-before-append:%lld", (long long)val);
1293                                     appendFromTime[tc] = val;
1294                                 }
1295                                 ++tc;
1296                             }
1297                             bool lastSplit = (curSplit == numSplits-1) ? true : false;
1298                             mediaInfo->appendMediaFromTime(muxer, appendFromTime,
1299                                 totalSampleCount/numSplits + ((curSplit-1) * 30), lastSplit);
1300                             delete[] appendFromTime;
1301                         } else {
1302                             isPass = false;
1303                             ALOGE("no tracks in the file");
1304                         }
1305                         AMediaMuxer_delete(muxer);
1306                     } else {
1307                         isPass = false;
1308                         ALOGE("failed to create muxer");
1309                     }
1310                     fclose(ofp);
1311                     ofp = nullptr;
1312                     if (isPass) {
1313                         auto* mediaInfoDest = new MuxerNativeTestHelper(cdstPath, nullptr);
1314                         isPass = mediaInfoDest->isSubsetOf(mediaInfo);
1315                         delete mediaInfoDest;
1316                     }
1317                 } else {
1318                     isPass = false;
1319                     ALOGE("failed to open output file %s", cdstPath);
1320                 }
1321                 ++curSplit;
1322             }
1323         }
1324         delete mediaInfo;
1325         env->ReleaseStringUTFChars(jdstPath, cdstPath);
1326         env->ReleaseStringUTFChars(jsrcPath, csrcPath);
1327     } else {
1328         isPass = false;
1329     }
1330     return static_cast<jboolean>(isPass);
1331 }
1332 
registerAndroidMediaV2CtsMuxerTestApi(JNIEnv * env)1333 int registerAndroidMediaV2CtsMuxerTestApi(JNIEnv* env) {
1334     const JNINativeMethod methodTable[] = {
1335             {"nativeTestSetOrientationHint", "(ILjava/lang/String;Ljava/lang/String;)Z",
1336              (void*)nativeTestSetOrientationHint},
1337             {"nativeTestSetLocation", "(ILjava/lang/String;Ljava/lang/String;)Z",
1338              (void*)nativeTestSetLocation},
1339             {"nativeTestGetTrackCount", "(Ljava/lang/String;Ljava/lang/String;II)Z",
1340              (void*)nativeTestGetTrackCount},
1341             {"nativeTestGetTrackFormat", "(Ljava/lang/String;Ljava/lang/String;I)Z",
1342              (void*)nativeTestGetTrackFormat},
1343     };
1344     jclass c = env->FindClass("android/mediav2/cts/MuxerTest$TestApi");
1345     return env->RegisterNatives(c, methodTable, sizeof(methodTable) / sizeof(JNINativeMethod));
1346 }
1347 
registerAndroidMediaV2CtsMuxerTestMultiTrack(JNIEnv * env)1348 int registerAndroidMediaV2CtsMuxerTestMultiTrack(JNIEnv* env) {
1349     const JNINativeMethod methodTable[] = {
1350             {"nativeTestMultiTrack",
1351              "(ILjava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Z",
1352              (void*)nativeTestMultiTrack},
1353     };
1354     jclass c = env->FindClass("android/mediav2/cts/MuxerTest$TestMultiTrack");
1355     return env->RegisterNatives(c, methodTable, sizeof(methodTable) / sizeof(JNINativeMethod));
1356 }
1357 
registerAndroidMediaV2CtsMuxerTestOffsetPts(JNIEnv * env)1358 int registerAndroidMediaV2CtsMuxerTestOffsetPts(JNIEnv* env) {
1359     const JNINativeMethod methodTable[] = {
1360             {"nativeTestOffsetPts", "(ILjava/lang/String;Ljava/lang/String;[I)Z",
1361              (void*)nativeTestOffsetPts},
1362     };
1363     jclass c = env->FindClass("android/mediav2/cts/MuxerTest$TestOffsetPts");
1364     return env->RegisterNatives(c, methodTable, sizeof(methodTable) / sizeof(JNINativeMethod));
1365 }
1366 
registerAndroidMediaV2CtsMuxerTestSimpleMux(JNIEnv * env)1367 int registerAndroidMediaV2CtsMuxerTestSimpleMux(JNIEnv* env) {
1368     const JNINativeMethod methodTable[] = {
1369             {"nativeTestSimpleMux",
1370              "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Z",
1371              (void*)nativeTestSimpleMux},
1372     };
1373     jclass c = env->FindClass("android/mediav2/cts/MuxerTest$TestSimpleMux");
1374     return env->RegisterNatives(c, methodTable, sizeof(methodTable) / sizeof(JNINativeMethod));
1375 }
1376 
registerAndroidMediaV2CtsMuxerTestSimpleAppend(JNIEnv * env)1377 int registerAndroidMediaV2CtsMuxerTestSimpleAppend(JNIEnv* env) {
1378     const JNINativeMethod methodTable[] = {
1379             {"nativeTestSimpleAppend",
1380              "(ILjava/lang/String;Ljava/lang/String;)Z",
1381              (void*)nativeTestSimpleAppend},
1382             {"nativeTestAppendGetTrackCount",
1383              "(Ljava/lang/String;I)Z",
1384              (void*)nativeTestAppendGetTrackCount},
1385             {"nativeTestAppendGetTrackFormat", "(Ljava/lang/String;)Z",
1386              (void*)nativeTestAppendGetTrackFormat},
1387              {"nativeTestNoSamples",
1388               "(ILjava/lang/String;Ljava/lang/String;)Z",
1389               (void*)nativeTestNoSamples},
1390             {"nativeTestIgnoreLastGOPAppend",
1391              "(ILjava/lang/String;Ljava/lang/String;)Z",
1392              (void*)nativeTestIgnoreLastGOPAppend},
1393     };
1394     jclass c = env->FindClass("android/mediav2/cts/MuxerTest$TestSimpleAppend");
1395     return env->RegisterNatives(c, methodTable, sizeof(methodTable) / sizeof(JNINativeMethod));
1396 }
1397 
1398 extern int registerAndroidMediaV2CtsMuxerUnitTestApi(JNIEnv* env);
1399 
JNI_OnLoad(JavaVM * vm,void *)1400 extern "C" JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void*) {
1401     JNIEnv* env;
1402     if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) return JNI_ERR;
1403     if (registerAndroidMediaV2CtsMuxerTestApi(env) != JNI_OK) return JNI_ERR;
1404     if (registerAndroidMediaV2CtsMuxerTestMultiTrack(env) != JNI_OK) return JNI_ERR;
1405     if (registerAndroidMediaV2CtsMuxerTestOffsetPts(env) != JNI_OK) return JNI_ERR;
1406     if (registerAndroidMediaV2CtsMuxerTestSimpleMux(env) != JNI_OK) return JNI_ERR;
1407     if (registerAndroidMediaV2CtsMuxerTestSimpleAppend(env) != JNI_OK) return JNI_ERR;
1408     if (registerAndroidMediaV2CtsMuxerUnitTestApi(env) != JNI_OK) return JNI_ERR;
1409     return JNI_VERSION_1_6;
1410 }
1411