Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 1 | /* |
| 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 Zhang | b55c545 | 2020-06-26 14:32:12 -0700 | [diff] [blame] | 23 | #include <fcntl.h> |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 24 | #include <gtest/gtest.h> |
Chong Zhang | 5855ee5 | 2020-06-22 11:41:25 -0700 | [diff] [blame] | 25 | #include <media/MediaSampleReaderNDK.h> |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 26 | #include <media/MediaTranscoder.h> |
Chong Zhang | 5855ee5 | 2020-06-22 11:41:25 -0700 | [diff] [blame] | 27 | #include <media/NdkCommon.h> |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 28 | |
| 29 | namespace android { |
| 30 | |
Chong Zhang | 5855ee5 | 2020-06-22 11:41:25 -0700 | [diff] [blame] | 31 | #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 | |
| 39 | DEFINE_FORMAT_VALUE_EQUAL_FUNC(int64_t, Int64); |
| 40 | DEFINE_FORMAT_VALUE_EQUAL_FUNC(int32_t, Int32); |
| 41 | |
| 42 | struct FormatVerifierEntry { |
| 43 | const char* key; |
Chong Zhang | d6e4aec | 2020-06-22 14:13:07 -0700 | [diff] [blame] | 44 | std::function<bool(const char*, AMediaFormat*, AMediaFormat*)> equal; |
Chong Zhang | 5855ee5 | 2020-06-22 11:41:25 -0700 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | static 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 Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 55 | class TestCallbacks : public MediaTranscoder::CallbackInterface { |
| 56 | public: |
| 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 Zhang | b55c545 | 2020-06-26 14:32:12 -0700 | [diff] [blame] | 78 | const std::shared_ptr<const Parcel>& pausedState |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 79 | __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 | |
| 90 | private: |
| 91 | std::mutex mMutex; |
| 92 | std::condition_variable mCondition; |
| 93 | bool mFinished = false; |
| 94 | }; |
| 95 | |
Chong Zhang | 308e91f | 2020-06-10 15:27:56 -0700 | [diff] [blame] | 96 | // Write-only, create file if non-existent, don't overwrite existing file. |
| 97 | static constexpr int kOpenFlags = O_WRONLY | O_CREAT | O_EXCL; |
| 98 | // User R+W permission. |
| 99 | static constexpr int kFileMode = S_IRUSR | S_IWUSR; |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 100 | |
| 101 | class MediaTranscoderTests : public ::testing::Test { |
| 102 | public: |
| 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 Nilsson | 800793f | 2020-07-31 16:16:38 -0700 | [diff] [blame] | 118 | 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 Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 129 | using FormatConfigurationCallback = std::function<AMediaFormat*(AMediaFormat*)>; |
Chong Zhang | 5855ee5 | 2020-06-22 11:41:25 -0700 | [diff] [blame] | 130 | media_status_t transcodeHelper(const char* srcPath, const char* destPath, |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 131 | FormatConfigurationCallback formatCallback) { |
| 132 | auto transcoder = MediaTranscoder::create(mCallbacks, nullptr); |
| 133 | EXPECT_NE(transcoder, nullptr); |
| 134 | |
Chong Zhang | 5855ee5 | 2020-06-22 11:41:25 -0700 | [diff] [blame] | 135 | const int srcFd = open(srcPath, O_RDONLY); |
Chong Zhang | 308e91f | 2020-06-10 15:27:56 -0700 | [diff] [blame] | 136 | EXPECT_EQ(transcoder->configureSource(srcFd), AMEDIA_OK); |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 137 | |
| 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 Zhang | 5855ee5 | 2020-06-22 11:41:25 -0700 | [diff] [blame] | 144 | |
| 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 Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 152 | if (format != nullptr) { |
| 153 | AMediaFormat_delete(format); |
| 154 | } |
| 155 | } |
| 156 | deleteFile(destPath); |
Chong Zhang | 308e91f | 2020-06-10 15:27:56 -0700 | [diff] [blame] | 157 | const int dstFd = open(destPath, kOpenFlags, kFileMode); |
| 158 | EXPECT_EQ(transcoder->configureDestination(dstFd), AMEDIA_OK); |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 159 | |
| 160 | media_status_t startStatus = transcoder->start(); |
| 161 | EXPECT_EQ(startStatus, AMEDIA_OK); |
| 162 | if (startStatus == AMEDIA_OK) { |
| 163 | mCallbacks->waitForTranscodingFinished(); |
| 164 | } |
Chong Zhang | 308e91f | 2020-06-10 15:27:56 -0700 | [diff] [blame] | 165 | close(srcFd); |
| 166 | close(dstFd); |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 167 | |
| 168 | return mCallbacks->mStatus; |
| 169 | } |
| 170 | |
Linus Nilsson | 800793f | 2020-07-31 16:16:38 -0700 | [diff] [blame] | 171 | 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 Zhang | d6e4aec | 2020-06-22 14:13:07 -0700 | [diff] [blame] | 179 | |
Linus Nilsson | 800793f | 2020-07-31 16:16:38 -0700 | [diff] [blame] | 180 | if (strncmp(mime, "video/", 6) == 0 && |
| 181 | (bitrate > 0 || dstMime != nullptr)) { |
| 182 | format = AMediaFormat_new(); |
Chong Zhang | d6e4aec | 2020-06-22 14:13:07 -0700 | [diff] [blame] | 183 | |
Linus Nilsson | 800793f | 2020-07-31 16:16:38 -0700 | [diff] [blame] | 184 | if (bitrate > 0) { |
| 185 | AMediaFormat_setInt32( |
| 186 | format, AMEDIAFORMAT_KEY_BIT_RATE, bitrate); |
| 187 | } |
Chong Zhang | d6e4aec | 2020-06-22 14:13:07 -0700 | [diff] [blame] | 188 | |
Linus Nilsson | 800793f | 2020-07-31 16:16:38 -0700 | [diff] [blame] | 189 | if (dstMime != nullptr) { |
| 190 | AMediaFormat_setString(format, AMEDIAFORMAT_KEY_MIME, |
| 191 | dstMime); |
| 192 | } |
| 193 | } |
| 194 | return format; |
| 195 | }), |
| 196 | AMEDIA_OK); |
Chong Zhang | d6e4aec | 2020-06-22 14:13:07 -0700 | [diff] [blame] | 197 | |
| 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 Zhang | 5855ee5 | 2020-06-22 11:41:25 -0700 | [diff] [blame] | 213 | 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 Nilsson | 42a971b | 2020-07-01 16:41:11 -0700 | [diff] [blame] | 222 | ASSERT_NE(sampleReader, nullptr); |
Chong Zhang | 5855ee5 | 2020-06-22 11:41:25 -0700 | [diff] [blame] | 223 | |
| 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 Nilsson | 42a971b | 2020-07-01 16:41:11 -0700 | [diff] [blame] | 231 | |
Chong Zhang | 5855ee5 | 2020-06-22 11:41:25 -0700 | [diff] [blame] | 232 | 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 Zhang | d6e4aec | 2020-06-22 14:13:07 -0700 | [diff] [blame] | 248 | mSourceVideoFormat.get(), videoFormat.get())) |
| 249 | << "Failed at key " << kFieldsToPreserve[i].key; |
Chong Zhang | 5855ee5 | 2020-06-22 11:41:25 -0700 | [diff] [blame] | 250 | } |
| 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 Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 262 | std::shared_ptr<TestCallbacks> mCallbacks; |
Chong Zhang | 5855ee5 | 2020-06-22 11:41:25 -0700 | [diff] [blame] | 263 | std::shared_ptr<AMediaFormat> mSourceVideoFormat; |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 264 | }; |
| 265 | |
| 266 | TEST_F(MediaTranscoderTests, TestPassthrough) { |
Chong Zhang | d6e4aec | 2020-06-22 14:13:07 -0700 | [diff] [blame] | 267 | const char* srcPath = "/data/local/tmp/TranscodingTestAssets/cubicle_avc_480x240_aac_24KHz.mp4"; |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 268 | const char* destPath = "/data/local/tmp/MediaTranscoder_Passthrough.MP4"; |
Linus Nilsson | 800793f | 2020-07-31 16:16:38 -0700 | [diff] [blame] | 269 | testTranscodeVideo(srcPath, destPath, nullptr); |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 270 | } |
| 271 | |
Chong Zhang | d6e4aec | 2020-06-22 14:13:07 -0700 | [diff] [blame] | 272 | TEST_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 Nilsson | 800793f | 2020-07-31 16:16:38 -0700 | [diff] [blame] | 275 | testTranscodeVideo(srcPath, destPath, AMEDIA_MIMETYPE_VIDEO_AVC); |
Chong Zhang | 5855ee5 | 2020-06-22 11:41:25 -0700 | [diff] [blame] | 276 | } |
| 277 | |
Chong Zhang | d6e4aec | 2020-06-22 14:13:07 -0700 | [diff] [blame] | 278 | TEST_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 Zhang | 5855ee5 | 2020-06-22 11:41:25 -0700 | [diff] [blame] | 283 | |
Chong Zhang | d6e4aec | 2020-06-22 14:13:07 -0700 | [diff] [blame] | 284 | TEST_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 Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 289 | } |
| 290 | |
Linus Nilsson | 800793f | 2020-07-31 16:16:38 -0700 | [diff] [blame] | 291 | TEST_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 | |
| 300 | TEST_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 Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 313 | } // namespace android |
| 314 | |
| 315 | int main(int argc, char** argv) { |
| 316 | ::testing::InitGoogleTest(&argc, argv); |
| 317 | return RUN_ALL_TESTS(); |
| 318 | } |