blob: 67f1ca19604ef710a85d4aaf3507f8f294feee3e [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>
23#include <gtest/gtest.h>
Chong Zhang5855ee52020-06-22 11:41:25 -070024#include <media/MediaSampleReaderNDK.h>
Linus Nilssoncab39d82020-05-14 16:32:21 -070025#include <media/MediaTranscoder.h>
Chong Zhang5855ee52020-06-22 11:41:25 -070026#include <media/NdkCommon.h>
Linus Nilssoncab39d82020-05-14 16:32:21 -070027
28namespace android {
29
Chong Zhang5855ee52020-06-22 11:41:25 -070030#define DEFINE_FORMAT_VALUE_EQUAL_FUNC(_type, _typeName) \
31 static bool equal##_typeName(const char* key, AMediaFormat* src, AMediaFormat* dst) { \
32 _type srcVal, dstVal; \
33 bool srcPresent = AMediaFormat_get##_typeName(src, key, &srcVal); \
34 bool dstPresent = AMediaFormat_get##_typeName(dst, key, &dstVal); \
35 return (srcPresent == dstPresent) && (!srcPresent || (srcVal == dstVal)); \
36 }
37
38DEFINE_FORMAT_VALUE_EQUAL_FUNC(int64_t, Int64);
39DEFINE_FORMAT_VALUE_EQUAL_FUNC(int32_t, Int32);
40
41struct FormatVerifierEntry {
42 const char* key;
Chong Zhangd6e4aec2020-06-22 14:13:07 -070043 std::function<bool(const char*, AMediaFormat*, AMediaFormat*)> equal;
Chong Zhang5855ee52020-06-22 11:41:25 -070044};
45
46static const FormatVerifierEntry kFieldsToPreserve[] = {
47 {AMEDIAFORMAT_KEY_DURATION, equalInt64}, {AMEDIAFORMAT_KEY_WIDTH, equalInt32},
48 {AMEDIAFORMAT_KEY_HEIGHT, equalInt32}, {AMEDIAFORMAT_KEY_FRAME_RATE, equalInt32},
49 {AMEDIAFORMAT_KEY_FRAME_COUNT, equalInt32}, {AMEDIAFORMAT_KEY_DISPLAY_WIDTH, equalInt32},
50 {AMEDIAFORMAT_KEY_DISPLAY_HEIGHT, equalInt32}, {AMEDIAFORMAT_KEY_SAR_WIDTH, equalInt32},
51 {AMEDIAFORMAT_KEY_SAR_HEIGHT, equalInt32}, {AMEDIAFORMAT_KEY_ROTATION, equalInt32},
52};
53
Linus Nilssoncab39d82020-05-14 16:32:21 -070054class TestCallbacks : public MediaTranscoder::CallbackInterface {
55public:
56 virtual void onFinished(const MediaTranscoder* transcoder __unused) override {
57 std::unique_lock<std::mutex> lock(mMutex);
58 EXPECT_FALSE(mFinished);
59 mFinished = true;
60 mCondition.notify_all();
61 }
62
63 virtual void onError(const MediaTranscoder* transcoder __unused,
64 media_status_t error) override {
65 std::unique_lock<std::mutex> lock(mMutex);
66 EXPECT_NE(error, AMEDIA_OK);
67 EXPECT_FALSE(mFinished);
68 mFinished = true;
69 mStatus = error;
70 mCondition.notify_all();
71 }
72
73 virtual void onProgressUpdate(const MediaTranscoder* transcoder __unused,
74 int32_t progress __unused) override {}
75
76 virtual void onCodecResourceLost(const MediaTranscoder* transcoder __unused,
77 const std::shared_ptr<const Parcelable>& pausedState
78 __unused) override {}
79
80 void waitForTranscodingFinished() {
81 std::unique_lock<std::mutex> lock(mMutex);
82 while (!mFinished) {
83 mCondition.wait(lock);
84 }
85 }
86
87 media_status_t mStatus = AMEDIA_OK;
88
89private:
90 std::mutex mMutex;
91 std::condition_variable mCondition;
92 bool mFinished = false;
93};
94
Chong Zhang308e91f2020-06-10 15:27:56 -070095// Write-only, create file if non-existent, don't overwrite existing file.
96static constexpr int kOpenFlags = O_WRONLY | O_CREAT | O_EXCL;
97// User R+W permission.
98static constexpr int kFileMode = S_IRUSR | S_IWUSR;
Linus Nilssoncab39d82020-05-14 16:32:21 -070099
100class MediaTranscoderTests : public ::testing::Test {
101public:
102 MediaTranscoderTests() { LOG(DEBUG) << "MediaTranscoderTests created"; }
103 ~MediaTranscoderTests() { LOG(DEBUG) << "MediaTranscoderTests destroyed"; }
104
105 void SetUp() override {
106 LOG(DEBUG) << "MediaTranscoderTests set up";
107 mCallbacks = std::make_shared<TestCallbacks>();
108 }
109
110 void TearDown() override {
111 LOG(DEBUG) << "MediaTranscoderTests tear down";
112 mCallbacks.reset();
113 }
114
115 void deleteFile(const char* path) { unlink(path); }
116
117 using FormatConfigurationCallback = std::function<AMediaFormat*(AMediaFormat*)>;
Chong Zhang5855ee52020-06-22 11:41:25 -0700118 media_status_t transcodeHelper(const char* srcPath, const char* destPath,
Linus Nilssoncab39d82020-05-14 16:32:21 -0700119 FormatConfigurationCallback formatCallback) {
120 auto transcoder = MediaTranscoder::create(mCallbacks, nullptr);
121 EXPECT_NE(transcoder, nullptr);
122
Chong Zhang5855ee52020-06-22 11:41:25 -0700123 const int srcFd = open(srcPath, O_RDONLY);
Chong Zhang308e91f2020-06-10 15:27:56 -0700124 EXPECT_EQ(transcoder->configureSource(srcFd), AMEDIA_OK);
Linus Nilssoncab39d82020-05-14 16:32:21 -0700125
126 std::vector<std::shared_ptr<AMediaFormat>> trackFormats = transcoder->getTrackFormats();
127 EXPECT_GT(trackFormats.size(), 0);
128
129 for (int i = 0; i < trackFormats.size(); ++i) {
130 AMediaFormat* format = formatCallback(trackFormats[i].get());
131 EXPECT_EQ(transcoder->configureTrackFormat(i, format), AMEDIA_OK);
Chong Zhang5855ee52020-06-22 11:41:25 -0700132
133 // Save original video track format for verification.
134 const char* mime = nullptr;
135 AMediaFormat_getString(trackFormats[i].get(), AMEDIAFORMAT_KEY_MIME, &mime);
136 if (strncmp(mime, "video/", 6) == 0) {
137 mSourceVideoFormat = trackFormats[i];
138 }
139
Linus Nilssoncab39d82020-05-14 16:32:21 -0700140 if (format != nullptr) {
141 AMediaFormat_delete(format);
142 }
143 }
144 deleteFile(destPath);
Chong Zhang308e91f2020-06-10 15:27:56 -0700145 const int dstFd = open(destPath, kOpenFlags, kFileMode);
146 EXPECT_EQ(transcoder->configureDestination(dstFd), AMEDIA_OK);
Linus Nilssoncab39d82020-05-14 16:32:21 -0700147
148 media_status_t startStatus = transcoder->start();
149 EXPECT_EQ(startStatus, AMEDIA_OK);
150 if (startStatus == AMEDIA_OK) {
151 mCallbacks->waitForTranscodingFinished();
152 }
Chong Zhang308e91f2020-06-10 15:27:56 -0700153 close(srcFd);
154 close(dstFd);
Linus Nilssoncab39d82020-05-14 16:32:21 -0700155
156 return mCallbacks->mStatus;
157 }
158
Chong Zhangd6e4aec2020-06-22 14:13:07 -0700159 void testTranscodeVideo(const char* srcPath, const char* destPath, const char* dstMime) {
160 const int32_t kBitRate = 8 * 1000 * 1000; // 8Mbs
161
162 EXPECT_EQ(
163 transcodeHelper(
164 srcPath, destPath,
165 [dstMime](AMediaFormat* sourceFormat) {
166 AMediaFormat* format = nullptr;
167 const char* mime = nullptr;
168 AMediaFormat_getString(sourceFormat, AMEDIAFORMAT_KEY_MIME, &mime);
169
170 if (strncmp(mime, "video/", 6) == 0) {
171 format = AMediaFormat_new();
172 AMediaFormat_setInt32(format, AMEDIAFORMAT_KEY_BIT_RATE, kBitRate);
173
174 if (dstMime != nullptr) {
175 AMediaFormat_setString(format, AMEDIAFORMAT_KEY_MIME, dstMime);
176 }
177 }
178 return format;
179 }),
180 AMEDIA_OK);
181
182 if (dstMime != nullptr) {
183 std::vector<FormatVerifierEntry> extraVerifiers = {
184 {AMEDIAFORMAT_KEY_MIME,
185 [dstMime](const char* key, AMediaFormat* src __unused, AMediaFormat* dst) {
186 const char* mime = nullptr;
187 AMediaFormat_getString(dst, key, &mime);
188 return !strcmp(mime, dstMime);
189 }},
190 };
191 verifyOutputFormat(destPath, &extraVerifiers);
192 } else {
193 verifyOutputFormat(destPath);
194 }
195 }
196
Chong Zhang5855ee52020-06-22 11:41:25 -0700197 void verifyOutputFormat(const char* destPath,
198 const std::vector<FormatVerifierEntry>* extraVerifiers = nullptr) {
199 int dstFd = open(destPath, O_RDONLY);
200 EXPECT_GT(dstFd, 0);
201 ssize_t fileSize = lseek(dstFd, 0, SEEK_END);
202 lseek(dstFd, 0, SEEK_SET);
203
204 std::shared_ptr<MediaSampleReader> sampleReader =
205 MediaSampleReaderNDK::createFromFd(dstFd, 0, fileSize);
206
207 std::shared_ptr<AMediaFormat> videoFormat;
208 const size_t trackCount = sampleReader->getTrackCount();
209 for (size_t trackIndex = 0; trackIndex < trackCount; ++trackIndex) {
210 AMediaFormat* trackFormat = sampleReader->getTrackFormat(static_cast<int>(trackIndex));
211 if (trackFormat != nullptr) {
212 const char* mime = nullptr;
213 AMediaFormat_getString(trackFormat, AMEDIAFORMAT_KEY_MIME, &mime);
214 if (strncmp(mime, "video/", 6) == 0) {
215 LOG(INFO) << "Track # " << trackIndex << ": "
216 << AMediaFormat_toString(trackFormat);
217 videoFormat = std::shared_ptr<AMediaFormat>(trackFormat, &AMediaFormat_delete);
218 break;
219 }
220 }
221 }
222
223 EXPECT_NE(videoFormat, nullptr);
224
225 LOG(INFO) << "source video format: " << AMediaFormat_toString(mSourceVideoFormat.get());
226 LOG(INFO) << "transcoded video format: " << AMediaFormat_toString(videoFormat.get());
227
228 for (int i = 0; i < (sizeof(kFieldsToPreserve) / sizeof(kFieldsToPreserve[0])); ++i) {
229 EXPECT_TRUE(kFieldsToPreserve[i].equal(kFieldsToPreserve[i].key,
Chong Zhangd6e4aec2020-06-22 14:13:07 -0700230 mSourceVideoFormat.get(), videoFormat.get()))
231 << "Failed at key " << kFieldsToPreserve[i].key;
Chong Zhang5855ee52020-06-22 11:41:25 -0700232 }
233
234 if (extraVerifiers != nullptr) {
235 for (int i = 0; i < extraVerifiers->size(); ++i) {
236 const FormatVerifierEntry& entry = (*extraVerifiers)[i];
237 EXPECT_TRUE(entry.equal(entry.key, mSourceVideoFormat.get(), videoFormat.get()));
238 }
239 }
240
241 close(dstFd);
242 }
243
Linus Nilssoncab39d82020-05-14 16:32:21 -0700244 std::shared_ptr<TestCallbacks> mCallbacks;
Chong Zhang5855ee52020-06-22 11:41:25 -0700245 std::shared_ptr<AMediaFormat> mSourceVideoFormat;
Linus Nilssoncab39d82020-05-14 16:32:21 -0700246};
247
248TEST_F(MediaTranscoderTests, TestPassthrough) {
Chong Zhangd6e4aec2020-06-22 14:13:07 -0700249 const char* srcPath = "/data/local/tmp/TranscodingTestAssets/cubicle_avc_480x240_aac_24KHz.mp4";
Linus Nilssoncab39d82020-05-14 16:32:21 -0700250 const char* destPath = "/data/local/tmp/MediaTranscoder_Passthrough.MP4";
251
Chong Zhangd6e4aec2020-06-22 14:13:07 -0700252 EXPECT_EQ(transcodeHelper(srcPath, destPath, [](AMediaFormat*) { return nullptr; }), AMEDIA_OK);
Linus Nilssoncab39d82020-05-14 16:32:21 -0700253
Chong Zhang5855ee52020-06-22 11:41:25 -0700254 verifyOutputFormat(destPath);
Linus Nilssoncab39d82020-05-14 16:32:21 -0700255}
256
Chong Zhangd6e4aec2020-06-22 14:13:07 -0700257TEST_F(MediaTranscoderTests, TestVideoTranscode_AvcToAvc_Basic) {
258 const char* srcPath = "/data/local/tmp/TranscodingTestAssets/cubicle_avc_480x240_aac_24KHz.mp4";
259 const char* destPath = "/data/local/tmp/MediaTranscoder_VideoTranscode_AvcToAvc_Basic.MP4";
260 testTranscodeVideo(srcPath, destPath, nullptr /*dstMime*/);
Chong Zhang5855ee52020-06-22 11:41:25 -0700261}
262
Chong Zhangd6e4aec2020-06-22 14:13:07 -0700263TEST_F(MediaTranscoderTests, TestVideoTranscode_HevcToAvc_Basic) {
264 const char* srcPath = "/data/local/tmp/TranscodingTestAssets/jets_hevc_1280x720_20Mbps.mp4";
265 const char* destPath = "/data/local/tmp/MediaTranscoder_VideoTranscode_HevcToAvc_Basic.MP4";
266 testTranscodeVideo(srcPath, destPath, AMEDIA_MIMETYPE_VIDEO_AVC);
267}
Chong Zhang5855ee52020-06-22 11:41:25 -0700268
Chong Zhangd6e4aec2020-06-22 14:13:07 -0700269TEST_F(MediaTranscoderTests, TestVideoTranscode_HevcToAvc_Rotation) {
270 const char* srcPath =
271 "/data/local/tmp/TranscodingTestAssets/desk_hevc_1920x1080_aac_48KHz_rot90.mp4";
272 const char* destPath = "/data/local/tmp/MediaTranscoder_VideoTranscode_HevcToAvc_Rotation.MP4";
273 testTranscodeVideo(srcPath, destPath, AMEDIA_MIMETYPE_VIDEO_AVC);
Linus Nilssoncab39d82020-05-14 16:32:21 -0700274}
275
276} // namespace android
277
278int main(int argc, char** argv) {
279 ::testing::InitGoogleTest(&argc, argv);
280 return RUN_ALL_TESTS();
281}