blob: 53bd2a20696c1d8954e654dca7034d2fe53fda8d [file] [log] [blame]
Linus Nilssoncab39d82020-05-14 16:32:21 -07001/*
2 * Copyright (C) 2020 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// Unit Test for MediaTranscoder
18
19// #define LOG_NDEBUG 0
20#define LOG_TAG "MediaTranscoderTests"
21
22#include <android-base/logging.h>
Chong Zhangb55c5452020-06-26 14:32:12 -070023#include <fcntl.h>
Linus Nilssoncab39d82020-05-14 16:32:21 -070024#include <gtest/gtest.h>
Chong Zhang5855ee52020-06-22 11:41:25 -070025#include <media/MediaSampleReaderNDK.h>
Linus Nilssoncab39d82020-05-14 16:32:21 -070026#include <media/MediaTranscoder.h>
Chong Zhang5855ee52020-06-22 11:41:25 -070027#include <media/NdkCommon.h>
Linus Nilssoncab39d82020-05-14 16:32:21 -070028
29namespace android {
30
Chong Zhang5855ee52020-06-22 11:41:25 -070031#define DEFINE_FORMAT_VALUE_EQUAL_FUNC(_type, _typeName) \
32 static bool equal##_typeName(const char* key, AMediaFormat* src, AMediaFormat* dst) { \
33 _type srcVal, dstVal; \
34 bool srcPresent = AMediaFormat_get##_typeName(src, key, &srcVal); \
35 bool dstPresent = AMediaFormat_get##_typeName(dst, key, &dstVal); \
36 return (srcPresent == dstPresent) && (!srcPresent || (srcVal == dstVal)); \
37 }
38
39DEFINE_FORMAT_VALUE_EQUAL_FUNC(int64_t, Int64);
40DEFINE_FORMAT_VALUE_EQUAL_FUNC(int32_t, Int32);
41
42struct FormatVerifierEntry {
43 const char* key;
Chong Zhangd6e4aec2020-06-22 14:13:07 -070044 std::function<bool(const char*, AMediaFormat*, AMediaFormat*)> equal;
Chong Zhang5855ee52020-06-22 11:41:25 -070045};
46
47static const FormatVerifierEntry kFieldsToPreserve[] = {
48 {AMEDIAFORMAT_KEY_DURATION, equalInt64}, {AMEDIAFORMAT_KEY_WIDTH, equalInt32},
49 {AMEDIAFORMAT_KEY_HEIGHT, equalInt32}, {AMEDIAFORMAT_KEY_FRAME_RATE, equalInt32},
50 {AMEDIAFORMAT_KEY_FRAME_COUNT, equalInt32}, {AMEDIAFORMAT_KEY_DISPLAY_WIDTH, equalInt32},
51 {AMEDIAFORMAT_KEY_DISPLAY_HEIGHT, equalInt32}, {AMEDIAFORMAT_KEY_SAR_WIDTH, equalInt32},
52 {AMEDIAFORMAT_KEY_SAR_HEIGHT, equalInt32}, {AMEDIAFORMAT_KEY_ROTATION, equalInt32},
53};
54
Linus Nilssoncab39d82020-05-14 16:32:21 -070055class TestCallbacks : public MediaTranscoder::CallbackInterface {
56public:
57 virtual void onFinished(const MediaTranscoder* transcoder __unused) override {
58 std::unique_lock<std::mutex> lock(mMutex);
59 EXPECT_FALSE(mFinished);
60 mFinished = true;
61 mCondition.notify_all();
62 }
63
64 virtual void onError(const MediaTranscoder* transcoder __unused,
65 media_status_t error) override {
66 std::unique_lock<std::mutex> lock(mMutex);
67 EXPECT_NE(error, AMEDIA_OK);
68 EXPECT_FALSE(mFinished);
69 mFinished = true;
70 mStatus = error;
71 mCondition.notify_all();
72 }
73
74 virtual void onProgressUpdate(const MediaTranscoder* transcoder __unused,
75 int32_t progress __unused) override {}
76
77 virtual void onCodecResourceLost(const MediaTranscoder* transcoder __unused,
Chong Zhangb55c5452020-06-26 14:32:12 -070078 const std::shared_ptr<const Parcel>& pausedState
Linus Nilssoncab39d82020-05-14 16:32:21 -070079 __unused) override {}
80
81 void waitForTranscodingFinished() {
82 std::unique_lock<std::mutex> lock(mMutex);
83 while (!mFinished) {
84 mCondition.wait(lock);
85 }
86 }
87
88 media_status_t mStatus = AMEDIA_OK;
89
90private:
91 std::mutex mMutex;
92 std::condition_variable mCondition;
93 bool mFinished = false;
94};
95
Chong Zhang308e91f2020-06-10 15:27:56 -070096// Write-only, create file if non-existent, don't overwrite existing file.
97static constexpr int kOpenFlags = O_WRONLY | O_CREAT | O_EXCL;
98// User R+W permission.
99static constexpr int kFileMode = S_IRUSR | S_IWUSR;
Linus Nilssoncab39d82020-05-14 16:32:21 -0700100
101class MediaTranscoderTests : public ::testing::Test {
102public:
103 MediaTranscoderTests() { LOG(DEBUG) << "MediaTranscoderTests created"; }
104 ~MediaTranscoderTests() { LOG(DEBUG) << "MediaTranscoderTests destroyed"; }
105
106 void SetUp() override {
107 LOG(DEBUG) << "MediaTranscoderTests set up";
108 mCallbacks = std::make_shared<TestCallbacks>();
109 }
110
111 void TearDown() override {
112 LOG(DEBUG) << "MediaTranscoderTests tear down";
113 mCallbacks.reset();
114 }
115
116 void deleteFile(const char* path) { unlink(path); }
117
Linus Nilsson800793f2020-07-31 16:16:38 -0700118 float getFileSizeDiffPercent(const char* path1, const char* path2, bool absolute = false) {
119 struct stat s1, s2;
120 EXPECT_EQ(stat(path1, &s1), 0);
121 EXPECT_EQ(stat(path2, &s2), 0);
122
123 int64_t diff = s2.st_size - s1.st_size;
124 if (absolute && diff < 0) diff = -diff;
125
126 return (float)diff * 100.0f / s1.st_size;
127 }
128
Linus Nilssoncab39d82020-05-14 16:32:21 -0700129 using FormatConfigurationCallback = std::function<AMediaFormat*(AMediaFormat*)>;
Chong Zhang5855ee52020-06-22 11:41:25 -0700130 media_status_t transcodeHelper(const char* srcPath, const char* destPath,
Linus Nilssoncab39d82020-05-14 16:32:21 -0700131 FormatConfigurationCallback formatCallback) {
132 auto transcoder = MediaTranscoder::create(mCallbacks, nullptr);
133 EXPECT_NE(transcoder, nullptr);
134
Chong Zhang5855ee52020-06-22 11:41:25 -0700135 const int srcFd = open(srcPath, O_RDONLY);
Chong Zhang308e91f2020-06-10 15:27:56 -0700136 EXPECT_EQ(transcoder->configureSource(srcFd), AMEDIA_OK);
Linus Nilssoncab39d82020-05-14 16:32:21 -0700137
138 std::vector<std::shared_ptr<AMediaFormat>> trackFormats = transcoder->getTrackFormats();
139 EXPECT_GT(trackFormats.size(), 0);
140
141 for (int i = 0; i < trackFormats.size(); ++i) {
142 AMediaFormat* format = formatCallback(trackFormats[i].get());
143 EXPECT_EQ(transcoder->configureTrackFormat(i, format), AMEDIA_OK);
Chong Zhang5855ee52020-06-22 11:41:25 -0700144
145 // Save original video track format for verification.
146 const char* mime = nullptr;
147 AMediaFormat_getString(trackFormats[i].get(), AMEDIAFORMAT_KEY_MIME, &mime);
148 if (strncmp(mime, "video/", 6) == 0) {
149 mSourceVideoFormat = trackFormats[i];
150 }
151
Linus Nilssoncab39d82020-05-14 16:32:21 -0700152 if (format != nullptr) {
153 AMediaFormat_delete(format);
154 }
155 }
156 deleteFile(destPath);
Chong Zhang308e91f2020-06-10 15:27:56 -0700157 const int dstFd = open(destPath, kOpenFlags, kFileMode);
158 EXPECT_EQ(transcoder->configureDestination(dstFd), AMEDIA_OK);
Linus Nilssoncab39d82020-05-14 16:32:21 -0700159
160 media_status_t startStatus = transcoder->start();
161 EXPECT_EQ(startStatus, AMEDIA_OK);
162 if (startStatus == AMEDIA_OK) {
163 mCallbacks->waitForTranscodingFinished();
164 }
Chong Zhang308e91f2020-06-10 15:27:56 -0700165 close(srcFd);
166 close(dstFd);
Linus Nilssoncab39d82020-05-14 16:32:21 -0700167
168 return mCallbacks->mStatus;
169 }
170
Linus Nilsson800793f2020-07-31 16:16:38 -0700171 void testTranscodeVideo(const char* srcPath, const char* destPath, const char* dstMime,
172 int32_t bitrate = 0) {
173 EXPECT_EQ(transcodeHelper(srcPath, destPath,
174 [dstMime, bitrate](AMediaFormat* sourceFormat) {
175 AMediaFormat* format = nullptr;
176 const char* mime = nullptr;
177 AMediaFormat_getString(sourceFormat, AMEDIAFORMAT_KEY_MIME,
178 &mime);
Chong Zhangd6e4aec2020-06-22 14:13:07 -0700179
Linus Nilsson800793f2020-07-31 16:16:38 -0700180 if (strncmp(mime, "video/", 6) == 0 &&
181 (bitrate > 0 || dstMime != nullptr)) {
182 format = AMediaFormat_new();
Chong Zhangd6e4aec2020-06-22 14:13:07 -0700183
Linus Nilsson800793f2020-07-31 16:16:38 -0700184 if (bitrate > 0) {
185 AMediaFormat_setInt32(
186 format, AMEDIAFORMAT_KEY_BIT_RATE, bitrate);
187 }
Chong Zhangd6e4aec2020-06-22 14:13:07 -0700188
Linus Nilsson800793f2020-07-31 16:16:38 -0700189 if (dstMime != nullptr) {
190 AMediaFormat_setString(format, AMEDIAFORMAT_KEY_MIME,
191 dstMime);
192 }
193 }
194 return format;
195 }),
196 AMEDIA_OK);
Chong Zhangd6e4aec2020-06-22 14:13:07 -0700197
198 if (dstMime != nullptr) {
199 std::vector<FormatVerifierEntry> extraVerifiers = {
200 {AMEDIAFORMAT_KEY_MIME,
201 [dstMime](const char* key, AMediaFormat* src __unused, AMediaFormat* dst) {
202 const char* mime = nullptr;
203 AMediaFormat_getString(dst, key, &mime);
204 return !strcmp(mime, dstMime);
205 }},
206 };
207 verifyOutputFormat(destPath, &extraVerifiers);
208 } else {
209 verifyOutputFormat(destPath);
210 }
211 }
212
Chong Zhang5855ee52020-06-22 11:41:25 -0700213 void verifyOutputFormat(const char* destPath,
214 const std::vector<FormatVerifierEntry>* extraVerifiers = nullptr) {
215 int dstFd = open(destPath, O_RDONLY);
216 EXPECT_GT(dstFd, 0);
217 ssize_t fileSize = lseek(dstFd, 0, SEEK_END);
218 lseek(dstFd, 0, SEEK_SET);
219
220 std::shared_ptr<MediaSampleReader> sampleReader =
221 MediaSampleReaderNDK::createFromFd(dstFd, 0, fileSize);
Linus Nilsson42a971b2020-07-01 16:41:11 -0700222 ASSERT_NE(sampleReader, nullptr);
Chong Zhang5855ee52020-06-22 11:41:25 -0700223
224 std::shared_ptr<AMediaFormat> videoFormat;
225 const size_t trackCount = sampleReader->getTrackCount();
226 for (size_t trackIndex = 0; trackIndex < trackCount; ++trackIndex) {
227 AMediaFormat* trackFormat = sampleReader->getTrackFormat(static_cast<int>(trackIndex));
228 if (trackFormat != nullptr) {
229 const char* mime = nullptr;
230 AMediaFormat_getString(trackFormat, AMEDIAFORMAT_KEY_MIME, &mime);
Linus Nilsson42a971b2020-07-01 16:41:11 -0700231
Chong Zhang5855ee52020-06-22 11:41:25 -0700232 if (strncmp(mime, "video/", 6) == 0) {
233 LOG(INFO) << "Track # " << trackIndex << ": "
234 << AMediaFormat_toString(trackFormat);
235 videoFormat = std::shared_ptr<AMediaFormat>(trackFormat, &AMediaFormat_delete);
236 break;
237 }
238 }
239 }
240
241 EXPECT_NE(videoFormat, nullptr);
242
243 LOG(INFO) << "source video format: " << AMediaFormat_toString(mSourceVideoFormat.get());
244 LOG(INFO) << "transcoded video format: " << AMediaFormat_toString(videoFormat.get());
245
246 for (int i = 0; i < (sizeof(kFieldsToPreserve) / sizeof(kFieldsToPreserve[0])); ++i) {
247 EXPECT_TRUE(kFieldsToPreserve[i].equal(kFieldsToPreserve[i].key,
Chong Zhangd6e4aec2020-06-22 14:13:07 -0700248 mSourceVideoFormat.get(), videoFormat.get()))
249 << "Failed at key " << kFieldsToPreserve[i].key;
Chong Zhang5855ee52020-06-22 11:41:25 -0700250 }
251
252 if (extraVerifiers != nullptr) {
253 for (int i = 0; i < extraVerifiers->size(); ++i) {
254 const FormatVerifierEntry& entry = (*extraVerifiers)[i];
255 EXPECT_TRUE(entry.equal(entry.key, mSourceVideoFormat.get(), videoFormat.get()));
256 }
257 }
258
259 close(dstFd);
260 }
261
Linus Nilssoncab39d82020-05-14 16:32:21 -0700262 std::shared_ptr<TestCallbacks> mCallbacks;
Chong Zhang5855ee52020-06-22 11:41:25 -0700263 std::shared_ptr<AMediaFormat> mSourceVideoFormat;
Linus Nilssoncab39d82020-05-14 16:32:21 -0700264};
265
266TEST_F(MediaTranscoderTests, TestPassthrough) {
Chong Zhangd6e4aec2020-06-22 14:13:07 -0700267 const char* srcPath = "/data/local/tmp/TranscodingTestAssets/cubicle_avc_480x240_aac_24KHz.mp4";
Linus Nilssoncab39d82020-05-14 16:32:21 -0700268 const char* destPath = "/data/local/tmp/MediaTranscoder_Passthrough.MP4";
Linus Nilsson800793f2020-07-31 16:16:38 -0700269 testTranscodeVideo(srcPath, destPath, nullptr);
Linus Nilssoncab39d82020-05-14 16:32:21 -0700270}
271
Chong Zhangd6e4aec2020-06-22 14:13:07 -0700272TEST_F(MediaTranscoderTests, TestVideoTranscode_AvcToAvc_Basic) {
273 const char* srcPath = "/data/local/tmp/TranscodingTestAssets/cubicle_avc_480x240_aac_24KHz.mp4";
274 const char* destPath = "/data/local/tmp/MediaTranscoder_VideoTranscode_AvcToAvc_Basic.MP4";
Linus Nilsson800793f2020-07-31 16:16:38 -0700275 testTranscodeVideo(srcPath, destPath, AMEDIA_MIMETYPE_VIDEO_AVC);
Chong Zhang5855ee52020-06-22 11:41:25 -0700276}
277
Chong Zhangd6e4aec2020-06-22 14:13:07 -0700278TEST_F(MediaTranscoderTests, TestVideoTranscode_HevcToAvc_Basic) {
279 const char* srcPath = "/data/local/tmp/TranscodingTestAssets/jets_hevc_1280x720_20Mbps.mp4";
280 const char* destPath = "/data/local/tmp/MediaTranscoder_VideoTranscode_HevcToAvc_Basic.MP4";
281 testTranscodeVideo(srcPath, destPath, AMEDIA_MIMETYPE_VIDEO_AVC);
282}
Chong Zhang5855ee52020-06-22 11:41:25 -0700283
Chong Zhangd6e4aec2020-06-22 14:13:07 -0700284TEST_F(MediaTranscoderTests, TestVideoTranscode_HevcToAvc_Rotation) {
285 const char* srcPath =
286 "/data/local/tmp/TranscodingTestAssets/desk_hevc_1920x1080_aac_48KHz_rot90.mp4";
287 const char* destPath = "/data/local/tmp/MediaTranscoder_VideoTranscode_HevcToAvc_Rotation.MP4";
288 testTranscodeVideo(srcPath, destPath, AMEDIA_MIMETYPE_VIDEO_AVC);
Linus Nilssoncab39d82020-05-14 16:32:21 -0700289}
290
Linus Nilsson800793f2020-07-31 16:16:38 -0700291TEST_F(MediaTranscoderTests, TestPreserveBitrate) {
292 const char* srcPath = "/data/local/tmp/TranscodingTestAssets/cubicle_avc_480x240_aac_24KHz.mp4";
293 const char* destPath = "/data/local/tmp/MediaTranscoder_PreserveBitrate.MP4";
294 testTranscodeVideo(srcPath, destPath, AMEDIA_MIMETYPE_VIDEO_AVC);
295
296 // Require maximum of 10% difference in file size.
297 EXPECT_LT(getFileSizeDiffPercent(srcPath, destPath, true /* absolute*/), 10);
298}
299
300TEST_F(MediaTranscoderTests, TestCustomBitrate) {
301 const char* srcPath = "/data/local/tmp/TranscodingTestAssets/cubicle_avc_480x240_aac_24KHz.mp4";
302 const char* destPath1 = "/data/local/tmp/MediaTranscoder_CustomBitrate_2Mbps.MP4";
303 const char* destPath2 = "/data/local/tmp/MediaTranscoder_CustomBitrate_8Mbps.MP4";
304 testTranscodeVideo(srcPath, destPath1, AMEDIA_MIMETYPE_VIDEO_AVC, 2 * 1000 * 1000);
305 mCallbacks = std::make_shared<TestCallbacks>();
306 testTranscodeVideo(srcPath, destPath2, AMEDIA_MIMETYPE_VIDEO_AVC, 8 * 1000 * 1000);
307
308 // The source asset is very short and heavily compressed from the beginning so don't expect the
309 // requested bitrate to be exactly matched. However 40% difference seems reasonable.
310 EXPECT_GT(getFileSizeDiffPercent(destPath1, destPath2), 40);
311}
312
Linus Nilssoncab39d82020-05-14 16:32:21 -0700313} // namespace android
314
315int main(int argc, char** argv) {
316 ::testing::InitGoogleTest(&argc, argv);
317 return RUN_ALL_TESTS();
318}