blob: 991644be55350f7b2b08540d05df9ccff80527e1 [file] [log] [blame]
Rakesh Kumarcde25512019-09-04 19:44:38 +05301
2/*
3 * Copyright (C) 2019 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18//#define LOG_NDEBUG 0
19#define LOG_TAG "muxerTest"
20
21#include <fstream>
22#include <iostream>
23
Rakesh Kumarcde25512019-09-04 19:44:38 +053024#include "BenchmarkTestEnvironment.h"
Manisha Jajoo2d931a82019-12-20 15:49:52 +053025#include "Muxer.h"
Rakesh Kumarcde25512019-09-04 19:44:38 +053026
27#define OUTPUT_FILE_NAME "/data/local/tmp/mux.out"
28
29static BenchmarkTestEnvironment *gEnv = nullptr;
30
31class MuxerTest : public ::testing::TestWithParam<pair<string, string>> {};
32
33static MUXER_OUTPUT_T getMuxerOutFormat(string fmt) {
34 static const struct {
35 string name;
36 MUXER_OUTPUT_T value;
37 } kFormatMaps[] = {{"mp4", MUXER_OUTPUT_FORMAT_MPEG_4},
38 {"webm", MUXER_OUTPUT_FORMAT_WEBM},
39 {"3gpp", MUXER_OUTPUT_FORMAT_3GPP},
40 {"ogg", MUXER_OUTPUT_FORMAT_OGG}};
41
42 MUXER_OUTPUT_T format = MUXER_OUTPUT_FORMAT_INVALID;
43 for (size_t i = 0; i < sizeof(kFormatMaps) / sizeof(kFormatMaps[0]); ++i) {
44 if (!fmt.compare(kFormatMaps[i].name)) {
45 format = kFormatMaps[i].value;
46 break;
47 }
48 }
49 return format;
50}
51
52TEST_P(MuxerTest, Mux) {
53 ALOGV("Mux the samples given by extractor");
54 string inputFile = gEnv->getRes() + GetParam().first;
55 FILE *inputFp = fopen(inputFile.c_str(), "rb");
Manisha Jajoo2d931a82019-12-20 15:49:52 +053056 ASSERT_NE(inputFp, nullptr) << "Unable to open " << inputFile << " file for reading";
57
Rakesh Kumarcde25512019-09-04 19:44:38 +053058 string fmt = GetParam().second;
59 MUXER_OUTPUT_T outputFormat = getMuxerOutFormat(fmt);
Manisha Jajoo2d931a82019-12-20 15:49:52 +053060 ASSERT_NE(outputFormat, MUXER_OUTPUT_FORMAT_INVALID) << "Invalid muxer output format";
Rakesh Kumarcde25512019-09-04 19:44:38 +053061
62 Muxer *muxerObj = new Muxer();
Manisha Jajoo2d931a82019-12-20 15:49:52 +053063 ASSERT_NE(muxerObj, nullptr) << "Muxer creation failed";
64
Rakesh Kumarcde25512019-09-04 19:44:38 +053065 Extractor *extractor = muxerObj->getExtractor();
Manisha Jajoo2d931a82019-12-20 15:49:52 +053066 ASSERT_NE(extractor, nullptr) << "Extractor creation failed";
Rakesh Kumarcde25512019-09-04 19:44:38 +053067
68 // Read file properties
Manisha Jajoo2d931a82019-12-20 15:49:52 +053069 struct stat buf;
70 stat(inputFile.c_str(), &buf);
71 size_t fileSize = buf.st_size;
Rakesh Kumarcde25512019-09-04 19:44:38 +053072 int32_t fd = fileno(inputFp);
73
74 int32_t trackCount = extractor->initExtractor(fd, fileSize);
Manisha Jajoo2d931a82019-12-20 15:49:52 +053075 ASSERT_GT(trackCount, 0) << "initExtractor failed";
Rakesh Kumarcde25512019-09-04 19:44:38 +053076
77 for (int curTrack = 0; curTrack < trackCount; curTrack++) {
78 int32_t status = extractor->setupTrackFormat(curTrack);
Manisha Jajoo2d931a82019-12-20 15:49:52 +053079 ASSERT_EQ(status, 0) << "Track Format invalid";
Rakesh Kumarcde25512019-09-04 19:44:38 +053080
81 uint8_t *inputBuffer = (uint8_t *)malloc(kMaxBufferSize);
Manisha Jajoo2d931a82019-12-20 15:49:52 +053082 ASSERT_NE(inputBuffer, nullptr) << "Insufficient memory";
83
Rakesh Kumarcde25512019-09-04 19:44:38 +053084 // AMediaCodecBufferInfo : <size of frame> <flags> <presentationTimeUs> <offset>
85 vector<AMediaCodecBufferInfo> frameInfos;
86 AMediaCodecBufferInfo info;
87 uint32_t inputBufferOffset = 0;
88
89 // Get Frame Data
90 while (1) {
91 status = extractor->getFrameSample(info);
92 if (status || !info.size) break;
93 // copy the meta data and buffer to be passed to muxer
Manisha Jajoo2d931a82019-12-20 15:49:52 +053094 ASSERT_LE(inputBufferOffset + info.size, kMaxBufferSize)
95 << "Memory allocated not sufficient";
96
Rakesh Kumarcde25512019-09-04 19:44:38 +053097 memcpy(inputBuffer + inputBufferOffset, extractor->getFrameBuf(), info.size);
98 info.offset = inputBufferOffset;
99 frameInfos.push_back(info);
100 inputBufferOffset += info.size;
101 }
102
103 string outputFileName = OUTPUT_FILE_NAME;
104 FILE *outputFp = fopen(outputFileName.c_str(), "w+b");
Manisha Jajoo2d931a82019-12-20 15:49:52 +0530105 ASSERT_NE(outputFp, nullptr)
106 << "Unable to open output file" << outputFileName << " for writing";
107
Rakesh Kumarcde25512019-09-04 19:44:38 +0530108 int32_t fd = fileno(outputFp);
109 status = muxerObj->initMuxer(fd, outputFormat);
Manisha Jajoo2d931a82019-12-20 15:49:52 +0530110 ASSERT_EQ(status, 0) << "initMuxer failed";
Rakesh Kumarcde25512019-09-04 19:44:38 +0530111
112 status = muxerObj->mux(inputBuffer, frameInfos);
Manisha Jajoo2d931a82019-12-20 15:49:52 +0530113 ASSERT_EQ(status, 0) << "Mux failed";
114
Rakesh Kumarcde25512019-09-04 19:44:38 +0530115 muxerObj->deInitMuxer();
Neelkamal Semwal588d8352020-01-16 12:26:51 +0530116 muxerObj->dumpStatistics(GetParam().first + "." + fmt.c_str(), fmt, gEnv->getStatsFile());
Rakesh Kumarcde25512019-09-04 19:44:38 +0530117 free(inputBuffer);
118 fclose(outputFp);
119 muxerObj->resetMuxer();
120 }
121 fclose(inputFp);
122 extractor->deInitExtractor();
123 delete muxerObj;
124}
125
126INSTANTIATE_TEST_SUITE_P(
127 MuxerTestAll, MuxerTest,
128 ::testing::Values(make_pair("crowd_1920x1080_25fps_4000kbps_vp8.webm", "webm"),
129 make_pair("crowd_1920x1080_25fps_4000kbps_vp9.webm", "webm"),
130 make_pair("crowd_1920x1080_25fps_6000kbps_mpeg4.mp4", "mp4"),
131 make_pair("crowd_352x288_25fps_6000kbps_h263.3gp", "mp4"),
132 make_pair("crowd_1920x1080_25fps_6700kbps_h264.ts", "mp4"),
133 make_pair("crowd_1920x1080_25fps_4000kbps_h265.mkv", "mp4"),
134 make_pair("crowd_1920x1080_25fps_6000kbps_mpeg4.mp4", "3gpp"),
135 make_pair("crowd_352x288_25fps_6000kbps_h263.3gp", "3gpp"),
136 make_pair("crowd_1920x1080_25fps_6700kbps_h264.ts", "3gpp"),
137 make_pair("crowd_1920x1080_25fps_4000kbps_h265.mkv", "3gpp"),
138 make_pair("bbb_48000hz_2ch_100kbps_opus_5mins.webm", "ogg"),
Manisha Jajoo8ff51af2020-01-30 14:41:38 +0530139 make_pair("bbb_44100hz_2ch_80kbps_vorbis_5mins.webm", "webm"),
Rakesh Kumarcde25512019-09-04 19:44:38 +0530140 make_pair("bbb_48000hz_2ch_100kbps_opus_5mins.webm", "webm"),
141 make_pair("bbb_44100hz_2ch_128kbps_aac_5mins.mp4", "mp4"),
142 make_pair("bbb_8000hz_1ch_8kbps_amrnb_5mins.3gp", "mp4"),
143 make_pair("bbb_16000hz_1ch_9kbps_amrwb_5mins.3gp", "mp4"),
144 make_pair("bbb_44100hz_2ch_128kbps_aac_5mins.mp4", "3gpp"),
145 make_pair("bbb_8000hz_1ch_8kbps_amrnb_5mins.3gp", "3gpp"),
146 make_pair("bbb_16000hz_1ch_9kbps_amrwb_5mins.3gp", "3gpp")));
147
148int main(int argc, char **argv) {
149 gEnv = new BenchmarkTestEnvironment();
150 ::testing::AddGlobalTestEnvironment(gEnv);
151 ::testing::InitGoogleTest(&argc, argv);
152 int status = gEnv->initFromOptions(argc, argv);
153 if (status == 0) {
Neelkamal Semwal588d8352020-01-16 12:26:51 +0530154 gEnv->setStatsFile("Muxer.csv");
155 status = gEnv->writeStatsHeader();
156 ALOGV("Stats file = %d\n", status);
Rakesh Kumarcde25512019-09-04 19:44:38 +0530157 status = RUN_ALL_TESTS();
Neelkamal Semwal588d8352020-01-16 12:26:51 +0530158 ALOGV("Muxer Test result = %d\n", status);
Rakesh Kumarcde25512019-09-04 19:44:38 +0530159 }
160 return status;
161}