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> |
Linus Nilsson | 93cf913 | 2020-09-24 12:12:48 -0700 | [diff] [blame] | 23 | #include <android/binder_process.h> |
Chong Zhang | b55c545 | 2020-06-26 14:32:12 -0700 | [diff] [blame] | 24 | #include <fcntl.h> |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 25 | #include <gtest/gtest.h> |
Chong Zhang | 5855ee5 | 2020-06-22 11:41:25 -0700 | [diff] [blame] | 26 | #include <media/MediaSampleReaderNDK.h> |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 27 | #include <media/MediaTranscoder.h> |
Chong Zhang | 5855ee5 | 2020-06-22 11:41:25 -0700 | [diff] [blame] | 28 | #include <media/NdkCommon.h> |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 29 | |
Linus Nilsson | 52df71c | 2021-03-08 13:01:11 -0800 | [diff] [blame] | 30 | #include "TranscoderTestUtils.h" |
| 31 | |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 32 | namespace android { |
| 33 | |
Chong Zhang | 5855ee5 | 2020-06-22 11:41:25 -0700 | [diff] [blame] | 34 | #define DEFINE_FORMAT_VALUE_EQUAL_FUNC(_type, _typeName) \ |
| 35 | static bool equal##_typeName(const char* key, AMediaFormat* src, AMediaFormat* dst) { \ |
| 36 | _type srcVal, dstVal; \ |
| 37 | bool srcPresent = AMediaFormat_get##_typeName(src, key, &srcVal); \ |
| 38 | bool dstPresent = AMediaFormat_get##_typeName(dst, key, &dstVal); \ |
| 39 | return (srcPresent == dstPresent) && (!srcPresent || (srcVal == dstVal)); \ |
| 40 | } |
| 41 | |
| 42 | DEFINE_FORMAT_VALUE_EQUAL_FUNC(int64_t, Int64); |
| 43 | DEFINE_FORMAT_VALUE_EQUAL_FUNC(int32_t, Int32); |
| 44 | |
| 45 | struct FormatVerifierEntry { |
| 46 | const char* key; |
Chong Zhang | d6e4aec | 2020-06-22 14:13:07 -0700 | [diff] [blame] | 47 | std::function<bool(const char*, AMediaFormat*, AMediaFormat*)> equal; |
Chong Zhang | 5855ee5 | 2020-06-22 11:41:25 -0700 | [diff] [blame] | 48 | }; |
| 49 | |
| 50 | static const FormatVerifierEntry kFieldsToPreserve[] = { |
| 51 | {AMEDIAFORMAT_KEY_DURATION, equalInt64}, {AMEDIAFORMAT_KEY_WIDTH, equalInt32}, |
| 52 | {AMEDIAFORMAT_KEY_HEIGHT, equalInt32}, {AMEDIAFORMAT_KEY_FRAME_RATE, equalInt32}, |
| 53 | {AMEDIAFORMAT_KEY_FRAME_COUNT, equalInt32}, {AMEDIAFORMAT_KEY_DISPLAY_WIDTH, equalInt32}, |
| 54 | {AMEDIAFORMAT_KEY_DISPLAY_HEIGHT, equalInt32}, {AMEDIAFORMAT_KEY_SAR_WIDTH, equalInt32}, |
| 55 | {AMEDIAFORMAT_KEY_SAR_HEIGHT, equalInt32}, {AMEDIAFORMAT_KEY_ROTATION, equalInt32}, |
| 56 | }; |
| 57 | |
Chong Zhang | 308e91f | 2020-06-10 15:27:56 -0700 | [diff] [blame] | 58 | // Write-only, create file if non-existent, don't overwrite existing file. |
| 59 | static constexpr int kOpenFlags = O_WRONLY | O_CREAT | O_EXCL; |
| 60 | // User R+W permission. |
| 61 | static constexpr int kFileMode = S_IRUSR | S_IWUSR; |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 62 | |
| 63 | class MediaTranscoderTests : public ::testing::Test { |
| 64 | public: |
| 65 | MediaTranscoderTests() { LOG(DEBUG) << "MediaTranscoderTests created"; } |
| 66 | ~MediaTranscoderTests() { LOG(DEBUG) << "MediaTranscoderTests destroyed"; } |
| 67 | |
| 68 | void SetUp() override { |
| 69 | LOG(DEBUG) << "MediaTranscoderTests set up"; |
Linus Nilsson | 52df71c | 2021-03-08 13:01:11 -0800 | [diff] [blame] | 70 | mCallbacks = std::make_shared<TestTranscoderCallbacks>(); |
Linus Nilsson | 93cf913 | 2020-09-24 12:12:48 -0700 | [diff] [blame] | 71 | ABinderProcess_startThreadPool(); |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | void TearDown() override { |
| 75 | LOG(DEBUG) << "MediaTranscoderTests tear down"; |
| 76 | mCallbacks.reset(); |
| 77 | } |
| 78 | |
| 79 | void deleteFile(const char* path) { unlink(path); } |
| 80 | |
Linus Nilsson | 800793f | 2020-07-31 16:16:38 -0700 | [diff] [blame] | 81 | float getFileSizeDiffPercent(const char* path1, const char* path2, bool absolute = false) { |
| 82 | struct stat s1, s2; |
| 83 | EXPECT_EQ(stat(path1, &s1), 0); |
| 84 | EXPECT_EQ(stat(path2, &s2), 0); |
| 85 | |
| 86 | int64_t diff = s2.st_size - s1.st_size; |
| 87 | if (absolute && diff < 0) diff = -diff; |
| 88 | |
| 89 | return (float)diff * 100.0f / s1.st_size; |
| 90 | } |
| 91 | |
Linus Nilsson | 93cf913 | 2020-09-24 12:12:48 -0700 | [diff] [blame] | 92 | typedef enum { |
| 93 | kRunToCompletion, |
Chong Zhang | 457c689 | 2021-02-01 15:34:20 -0800 | [diff] [blame] | 94 | kCheckHeartBeat, |
Linus Nilsson | 93cf913 | 2020-09-24 12:12:48 -0700 | [diff] [blame] | 95 | kCancelAfterProgress, |
| 96 | kCancelAfterStart, |
Linus Nilsson | fdb3e33 | 2020-09-18 17:11:41 -0700 | [diff] [blame] | 97 | kPauseAfterProgress, |
| 98 | kPauseAfterStart, |
Linus Nilsson | 93cf913 | 2020-09-24 12:12:48 -0700 | [diff] [blame] | 99 | } TranscodeExecutionControl; |
| 100 | |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 101 | using FormatConfigurationCallback = std::function<AMediaFormat*(AMediaFormat*)>; |
Chong Zhang | 5855ee5 | 2020-06-22 11:41:25 -0700 | [diff] [blame] | 102 | media_status_t transcodeHelper(const char* srcPath, const char* destPath, |
Linus Nilsson | 93cf913 | 2020-09-24 12:12:48 -0700 | [diff] [blame] | 103 | FormatConfigurationCallback formatCallback, |
Chong Zhang | 457c689 | 2021-02-01 15:34:20 -0800 | [diff] [blame] | 104 | TranscodeExecutionControl executionControl = kRunToCompletion, |
| 105 | int64_t heartBeatIntervalUs = -1) { |
| 106 | auto transcoder = MediaTranscoder::create(mCallbacks, heartBeatIntervalUs); |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 107 | EXPECT_NE(transcoder, nullptr); |
| 108 | |
Chong Zhang | 5855ee5 | 2020-06-22 11:41:25 -0700 | [diff] [blame] | 109 | const int srcFd = open(srcPath, O_RDONLY); |
Chong Zhang | 308e91f | 2020-06-10 15:27:56 -0700 | [diff] [blame] | 110 | EXPECT_EQ(transcoder->configureSource(srcFd), AMEDIA_OK); |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 111 | |
| 112 | std::vector<std::shared_ptr<AMediaFormat>> trackFormats = transcoder->getTrackFormats(); |
| 113 | EXPECT_GT(trackFormats.size(), 0); |
| 114 | |
| 115 | for (int i = 0; i < trackFormats.size(); ++i) { |
| 116 | AMediaFormat* format = formatCallback(trackFormats[i].get()); |
| 117 | EXPECT_EQ(transcoder->configureTrackFormat(i, format), AMEDIA_OK); |
Chong Zhang | 5855ee5 | 2020-06-22 11:41:25 -0700 | [diff] [blame] | 118 | |
| 119 | // Save original video track format for verification. |
| 120 | const char* mime = nullptr; |
| 121 | AMediaFormat_getString(trackFormats[i].get(), AMEDIAFORMAT_KEY_MIME, &mime); |
| 122 | if (strncmp(mime, "video/", 6) == 0) { |
| 123 | mSourceVideoFormat = trackFormats[i]; |
| 124 | } |
| 125 | |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 126 | if (format != nullptr) { |
| 127 | AMediaFormat_delete(format); |
| 128 | } |
| 129 | } |
| 130 | deleteFile(destPath); |
Chong Zhang | 308e91f | 2020-06-10 15:27:56 -0700 | [diff] [blame] | 131 | const int dstFd = open(destPath, kOpenFlags, kFileMode); |
| 132 | EXPECT_EQ(transcoder->configureDestination(dstFd), AMEDIA_OK); |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 133 | |
| 134 | media_status_t startStatus = transcoder->start(); |
| 135 | EXPECT_EQ(startStatus, AMEDIA_OK); |
Linus Nilsson | fdb3e33 | 2020-09-18 17:11:41 -0700 | [diff] [blame] | 136 | |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 137 | if (startStatus == AMEDIA_OK) { |
Linus Nilsson | fdb3e33 | 2020-09-18 17:11:41 -0700 | [diff] [blame] | 138 | std::shared_ptr<ndk::ScopedAParcel> pausedState; |
| 139 | |
Linus Nilsson | 93cf913 | 2020-09-24 12:12:48 -0700 | [diff] [blame] | 140 | switch (executionControl) { |
| 141 | case kCancelAfterProgress: |
| 142 | mCallbacks->waitForProgressMade(); |
| 143 | FALLTHROUGH_INTENDED; |
| 144 | case kCancelAfterStart: |
| 145 | transcoder->cancel(); |
| 146 | break; |
Linus Nilsson | fdb3e33 | 2020-09-18 17:11:41 -0700 | [diff] [blame] | 147 | case kPauseAfterProgress: |
| 148 | mCallbacks->waitForProgressMade(); |
| 149 | FALLTHROUGH_INTENDED; |
| 150 | case kPauseAfterStart: |
| 151 | transcoder->pause(&pausedState); |
| 152 | break; |
Chong Zhang | 457c689 | 2021-02-01 15:34:20 -0800 | [diff] [blame] | 153 | case kCheckHeartBeat: { |
| 154 | mCallbacks->waitForProgressMade(); |
| 155 | auto startTime = std::chrono::system_clock::now(); |
| 156 | mCallbacks->waitForTranscodingFinished(); |
| 157 | auto finishTime = std::chrono::system_clock::now(); |
| 158 | int32_t expectedCount = |
| 159 | (finishTime - startTime) / std::chrono::microseconds(heartBeatIntervalUs); |
| 160 | // Here we relax the expected count by 1, in case the last heart-beat just |
| 161 | // missed the window, other than that the count should be exact. |
| 162 | EXPECT_GE(mCallbacks->mHeartBeatCount, expectedCount - 1); |
| 163 | break; |
| 164 | } |
Linus Nilsson | 93cf913 | 2020-09-24 12:12:48 -0700 | [diff] [blame] | 165 | case kRunToCompletion: |
| 166 | default: |
| 167 | mCallbacks->waitForTranscodingFinished(); |
| 168 | break; |
| 169 | } |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 170 | } |
Chong Zhang | 308e91f | 2020-06-10 15:27:56 -0700 | [diff] [blame] | 171 | close(srcFd); |
| 172 | close(dstFd); |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 173 | |
| 174 | return mCallbacks->mStatus; |
| 175 | } |
| 176 | |
Linus Nilsson | 800793f | 2020-07-31 16:16:38 -0700 | [diff] [blame] | 177 | void testTranscodeVideo(const char* srcPath, const char* destPath, const char* dstMime, |
| 178 | int32_t bitrate = 0) { |
| 179 | EXPECT_EQ(transcodeHelper(srcPath, destPath, |
| 180 | [dstMime, bitrate](AMediaFormat* sourceFormat) { |
| 181 | AMediaFormat* format = nullptr; |
| 182 | const char* mime = nullptr; |
| 183 | AMediaFormat_getString(sourceFormat, AMEDIAFORMAT_KEY_MIME, |
| 184 | &mime); |
Chong Zhang | d6e4aec | 2020-06-22 14:13:07 -0700 | [diff] [blame] | 185 | |
Linus Nilsson | 800793f | 2020-07-31 16:16:38 -0700 | [diff] [blame] | 186 | if (strncmp(mime, "video/", 6) == 0 && |
| 187 | (bitrate > 0 || dstMime != nullptr)) { |
| 188 | format = AMediaFormat_new(); |
Chong Zhang | d6e4aec | 2020-06-22 14:13:07 -0700 | [diff] [blame] | 189 | |
Linus Nilsson | 800793f | 2020-07-31 16:16:38 -0700 | [diff] [blame] | 190 | if (bitrate > 0) { |
| 191 | AMediaFormat_setInt32( |
| 192 | format, AMEDIAFORMAT_KEY_BIT_RATE, bitrate); |
| 193 | } |
Chong Zhang | d6e4aec | 2020-06-22 14:13:07 -0700 | [diff] [blame] | 194 | |
Linus Nilsson | 800793f | 2020-07-31 16:16:38 -0700 | [diff] [blame] | 195 | if (dstMime != nullptr) { |
| 196 | AMediaFormat_setString(format, AMEDIAFORMAT_KEY_MIME, |
| 197 | dstMime); |
| 198 | } |
| 199 | } |
| 200 | return format; |
| 201 | }), |
| 202 | AMEDIA_OK); |
Chong Zhang | d6e4aec | 2020-06-22 14:13:07 -0700 | [diff] [blame] | 203 | |
| 204 | if (dstMime != nullptr) { |
| 205 | std::vector<FormatVerifierEntry> extraVerifiers = { |
| 206 | {AMEDIAFORMAT_KEY_MIME, |
| 207 | [dstMime](const char* key, AMediaFormat* src __unused, AMediaFormat* dst) { |
| 208 | const char* mime = nullptr; |
| 209 | AMediaFormat_getString(dst, key, &mime); |
| 210 | return !strcmp(mime, dstMime); |
| 211 | }}, |
| 212 | }; |
| 213 | verifyOutputFormat(destPath, &extraVerifiers); |
| 214 | } else { |
| 215 | verifyOutputFormat(destPath); |
| 216 | } |
| 217 | } |
| 218 | |
Chong Zhang | 5855ee5 | 2020-06-22 11:41:25 -0700 | [diff] [blame] | 219 | void verifyOutputFormat(const char* destPath, |
| 220 | const std::vector<FormatVerifierEntry>* extraVerifiers = nullptr) { |
| 221 | int dstFd = open(destPath, O_RDONLY); |
| 222 | EXPECT_GT(dstFd, 0); |
| 223 | ssize_t fileSize = lseek(dstFd, 0, SEEK_END); |
| 224 | lseek(dstFd, 0, SEEK_SET); |
| 225 | |
| 226 | std::shared_ptr<MediaSampleReader> sampleReader = |
| 227 | MediaSampleReaderNDK::createFromFd(dstFd, 0, fileSize); |
Linus Nilsson | 42a971b | 2020-07-01 16:41:11 -0700 | [diff] [blame] | 228 | ASSERT_NE(sampleReader, nullptr); |
Chong Zhang | 5855ee5 | 2020-06-22 11:41:25 -0700 | [diff] [blame] | 229 | |
| 230 | std::shared_ptr<AMediaFormat> videoFormat; |
| 231 | const size_t trackCount = sampleReader->getTrackCount(); |
| 232 | for (size_t trackIndex = 0; trackIndex < trackCount; ++trackIndex) { |
| 233 | AMediaFormat* trackFormat = sampleReader->getTrackFormat(static_cast<int>(trackIndex)); |
| 234 | if (trackFormat != nullptr) { |
| 235 | const char* mime = nullptr; |
| 236 | AMediaFormat_getString(trackFormat, AMEDIAFORMAT_KEY_MIME, &mime); |
Linus Nilsson | 42a971b | 2020-07-01 16:41:11 -0700 | [diff] [blame] | 237 | |
Chong Zhang | 5855ee5 | 2020-06-22 11:41:25 -0700 | [diff] [blame] | 238 | if (strncmp(mime, "video/", 6) == 0) { |
| 239 | LOG(INFO) << "Track # " << trackIndex << ": " |
| 240 | << AMediaFormat_toString(trackFormat); |
| 241 | videoFormat = std::shared_ptr<AMediaFormat>(trackFormat, &AMediaFormat_delete); |
| 242 | break; |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | EXPECT_NE(videoFormat, nullptr); |
Linus Nilsson | af4a321 | 2020-12-15 08:18:25 -0800 | [diff] [blame] | 248 | if (videoFormat != nullptr) { |
| 249 | LOG(INFO) << "source video format: " << AMediaFormat_toString(mSourceVideoFormat.get()); |
| 250 | LOG(INFO) << "transcoded video format: " << AMediaFormat_toString(videoFormat.get()); |
Chong Zhang | 5855ee5 | 2020-06-22 11:41:25 -0700 | [diff] [blame] | 251 | |
Linus Nilsson | af4a321 | 2020-12-15 08:18:25 -0800 | [diff] [blame] | 252 | for (int i = 0; i < (sizeof(kFieldsToPreserve) / sizeof(kFieldsToPreserve[0])); ++i) { |
| 253 | EXPECT_TRUE(kFieldsToPreserve[i].equal(kFieldsToPreserve[i].key, |
| 254 | mSourceVideoFormat.get(), videoFormat.get())) |
| 255 | << "Failed at key " << kFieldsToPreserve[i].key; |
| 256 | } |
Chong Zhang | 5855ee5 | 2020-06-22 11:41:25 -0700 | [diff] [blame] | 257 | |
Linus Nilsson | af4a321 | 2020-12-15 08:18:25 -0800 | [diff] [blame] | 258 | if (extraVerifiers != nullptr) { |
| 259 | for (int i = 0; i < extraVerifiers->size(); ++i) { |
| 260 | const FormatVerifierEntry& entry = (*extraVerifiers)[i]; |
| 261 | EXPECT_TRUE( |
| 262 | entry.equal(entry.key, mSourceVideoFormat.get(), videoFormat.get())); |
| 263 | } |
Chong Zhang | 5855ee5 | 2020-06-22 11:41:25 -0700 | [diff] [blame] | 264 | } |
| 265 | } |
| 266 | |
| 267 | close(dstFd); |
| 268 | } |
| 269 | |
Linus Nilsson | 52df71c | 2021-03-08 13:01:11 -0800 | [diff] [blame] | 270 | std::shared_ptr<TestTranscoderCallbacks> mCallbacks; |
Chong Zhang | 5855ee5 | 2020-06-22 11:41:25 -0700 | [diff] [blame] | 271 | std::shared_ptr<AMediaFormat> mSourceVideoFormat; |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 272 | }; |
| 273 | |
| 274 | TEST_F(MediaTranscoderTests, TestPassthrough) { |
Chong Zhang | d6e4aec | 2020-06-22 14:13:07 -0700 | [diff] [blame] | 275 | 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] | 276 | const char* destPath = "/data/local/tmp/MediaTranscoder_Passthrough.MP4"; |
Linus Nilsson | 800793f | 2020-07-31 16:16:38 -0700 | [diff] [blame] | 277 | testTranscodeVideo(srcPath, destPath, nullptr); |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 278 | } |
| 279 | |
Chong Zhang | d6e4aec | 2020-06-22 14:13:07 -0700 | [diff] [blame] | 280 | TEST_F(MediaTranscoderTests, TestVideoTranscode_AvcToAvc_Basic) { |
| 281 | const char* srcPath = "/data/local/tmp/TranscodingTestAssets/cubicle_avc_480x240_aac_24KHz.mp4"; |
| 282 | const char* destPath = "/data/local/tmp/MediaTranscoder_VideoTranscode_AvcToAvc_Basic.MP4"; |
Linus Nilsson | 800793f | 2020-07-31 16:16:38 -0700 | [diff] [blame] | 283 | testTranscodeVideo(srcPath, destPath, AMEDIA_MIMETYPE_VIDEO_AVC); |
Chong Zhang | 5855ee5 | 2020-06-22 11:41:25 -0700 | [diff] [blame] | 284 | } |
| 285 | |
Chong Zhang | d6e4aec | 2020-06-22 14:13:07 -0700 | [diff] [blame] | 286 | TEST_F(MediaTranscoderTests, TestVideoTranscode_HevcToAvc_Basic) { |
| 287 | const char* srcPath = "/data/local/tmp/TranscodingTestAssets/jets_hevc_1280x720_20Mbps.mp4"; |
| 288 | const char* destPath = "/data/local/tmp/MediaTranscoder_VideoTranscode_HevcToAvc_Basic.MP4"; |
| 289 | testTranscodeVideo(srcPath, destPath, AMEDIA_MIMETYPE_VIDEO_AVC); |
| 290 | } |
Chong Zhang | 5855ee5 | 2020-06-22 11:41:25 -0700 | [diff] [blame] | 291 | |
Chong Zhang | d6e4aec | 2020-06-22 14:13:07 -0700 | [diff] [blame] | 292 | TEST_F(MediaTranscoderTests, TestVideoTranscode_HevcToAvc_Rotation) { |
| 293 | const char* srcPath = |
| 294 | "/data/local/tmp/TranscodingTestAssets/desk_hevc_1920x1080_aac_48KHz_rot90.mp4"; |
| 295 | const char* destPath = "/data/local/tmp/MediaTranscoder_VideoTranscode_HevcToAvc_Rotation.MP4"; |
| 296 | testTranscodeVideo(srcPath, destPath, AMEDIA_MIMETYPE_VIDEO_AVC); |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 297 | } |
| 298 | |
Linus Nilsson | 91ea1f4 | 2020-12-16 11:35:27 -0800 | [diff] [blame] | 299 | TEST_F(MediaTranscoderTests, TestVideoTranscode_4K) { |
| 300 | #if defined(__i386__) || defined(__x86_64__) |
| 301 | LOG(WARNING) << "Skipping 4K test on x86 as SW encoder does not support 4K."; |
| 302 | return; |
| 303 | #else |
| 304 | const char* srcPath = "/data/local/tmp/TranscodingTestAssets/Video_4K_HEVC_10Frames_Audio.mp4"; |
| 305 | const char* destPath = "/data/local/tmp/MediaTranscoder_4K.MP4"; |
| 306 | testTranscodeVideo(srcPath, destPath, AMEDIA_MIMETYPE_VIDEO_AVC); |
| 307 | #endif |
| 308 | } |
| 309 | |
Linus Nilsson | 800793f | 2020-07-31 16:16:38 -0700 | [diff] [blame] | 310 | TEST_F(MediaTranscoderTests, TestPreserveBitrate) { |
| 311 | const char* srcPath = "/data/local/tmp/TranscodingTestAssets/cubicle_avc_480x240_aac_24KHz.mp4"; |
| 312 | const char* destPath = "/data/local/tmp/MediaTranscoder_PreserveBitrate.MP4"; |
| 313 | testTranscodeVideo(srcPath, destPath, AMEDIA_MIMETYPE_VIDEO_AVC); |
| 314 | |
Linus Nilsson | 24ba921 | 2020-12-02 11:46:39 -0800 | [diff] [blame] | 315 | // Require maximum of 25% difference in file size. |
| 316 | // TODO(b/174678336): Find a better test asset to tighten the threshold. |
| 317 | EXPECT_LT(getFileSizeDiffPercent(srcPath, destPath, true /* absolute*/), 25); |
Linus Nilsson | 800793f | 2020-07-31 16:16:38 -0700 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | TEST_F(MediaTranscoderTests, TestCustomBitrate) { |
| 321 | const char* srcPath = "/data/local/tmp/TranscodingTestAssets/cubicle_avc_480x240_aac_24KHz.mp4"; |
| 322 | const char* destPath1 = "/data/local/tmp/MediaTranscoder_CustomBitrate_2Mbps.MP4"; |
| 323 | const char* destPath2 = "/data/local/tmp/MediaTranscoder_CustomBitrate_8Mbps.MP4"; |
| 324 | testTranscodeVideo(srcPath, destPath1, AMEDIA_MIMETYPE_VIDEO_AVC, 2 * 1000 * 1000); |
Linus Nilsson | 52df71c | 2021-03-08 13:01:11 -0800 | [diff] [blame] | 325 | mCallbacks = std::make_shared<TestTranscoderCallbacks>(); |
Linus Nilsson | 800793f | 2020-07-31 16:16:38 -0700 | [diff] [blame] | 326 | testTranscodeVideo(srcPath, destPath2, AMEDIA_MIMETYPE_VIDEO_AVC, 8 * 1000 * 1000); |
| 327 | |
| 328 | // The source asset is very short and heavily compressed from the beginning so don't expect the |
Linus Nilsson | 24ba921 | 2020-12-02 11:46:39 -0800 | [diff] [blame] | 329 | // requested bitrate to be exactly matched. However the 8mbps should at least be larger. |
| 330 | // TODO(b/174678336): Find a better test asset to tighten the threshold. |
| 331 | EXPECT_GT(getFileSizeDiffPercent(destPath1, destPath2), 10); |
Linus Nilsson | 800793f | 2020-07-31 16:16:38 -0700 | [diff] [blame] | 332 | } |
| 333 | |
Linus Nilsson | 93cf913 | 2020-09-24 12:12:48 -0700 | [diff] [blame] | 334 | static AMediaFormat* getAVCVideoFormat(AMediaFormat* sourceFormat) { |
| 335 | AMediaFormat* format = nullptr; |
| 336 | const char* mime = nullptr; |
| 337 | AMediaFormat_getString(sourceFormat, AMEDIAFORMAT_KEY_MIME, &mime); |
| 338 | |
| 339 | if (strncmp(mime, "video/", 6) == 0) { |
| 340 | format = AMediaFormat_new(); |
| 341 | AMediaFormat_setString(format, AMEDIAFORMAT_KEY_MIME, AMEDIA_MIMETYPE_VIDEO_AVC); |
| 342 | } |
| 343 | |
| 344 | return format; |
| 345 | } |
| 346 | |
| 347 | TEST_F(MediaTranscoderTests, TestCancelAfterProgress) { |
| 348 | const char* srcPath = "/data/local/tmp/TranscodingTestAssets/longtest_15s.mp4"; |
| 349 | const char* destPath = "/data/local/tmp/MediaTranscoder_Cancel.MP4"; |
| 350 | |
Linus Nilsson | fdb3e33 | 2020-09-18 17:11:41 -0700 | [diff] [blame] | 351 | for (int i = 0; i < 20; ++i) { |
Linus Nilsson | 93cf913 | 2020-09-24 12:12:48 -0700 | [diff] [blame] | 352 | EXPECT_EQ(transcodeHelper(srcPath, destPath, getAVCVideoFormat, kCancelAfterProgress), |
| 353 | AMEDIA_OK); |
Linus Nilsson | fdb3e33 | 2020-09-18 17:11:41 -0700 | [diff] [blame] | 354 | EXPECT_FALSE(mCallbacks->mFinished); |
Linus Nilsson | 52df71c | 2021-03-08 13:01:11 -0800 | [diff] [blame] | 355 | mCallbacks = std::make_shared<TestTranscoderCallbacks>(); |
Linus Nilsson | 93cf913 | 2020-09-24 12:12:48 -0700 | [diff] [blame] | 356 | } |
| 357 | } |
| 358 | |
| 359 | TEST_F(MediaTranscoderTests, TestCancelAfterStart) { |
| 360 | const char* srcPath = "/data/local/tmp/TranscodingTestAssets/longtest_15s.mp4"; |
| 361 | const char* destPath = "/data/local/tmp/MediaTranscoder_Cancel.MP4"; |
| 362 | |
Linus Nilsson | fdb3e33 | 2020-09-18 17:11:41 -0700 | [diff] [blame] | 363 | for (int i = 0; i < 20; ++i) { |
Linus Nilsson | 93cf913 | 2020-09-24 12:12:48 -0700 | [diff] [blame] | 364 | EXPECT_EQ(transcodeHelper(srcPath, destPath, getAVCVideoFormat, kCancelAfterStart), |
| 365 | AMEDIA_OK); |
Linus Nilsson | fdb3e33 | 2020-09-18 17:11:41 -0700 | [diff] [blame] | 366 | EXPECT_FALSE(mCallbacks->mFinished); |
Linus Nilsson | 52df71c | 2021-03-08 13:01:11 -0800 | [diff] [blame] | 367 | mCallbacks = std::make_shared<TestTranscoderCallbacks>(); |
Linus Nilsson | fdb3e33 | 2020-09-18 17:11:41 -0700 | [diff] [blame] | 368 | } |
| 369 | } |
| 370 | |
| 371 | TEST_F(MediaTranscoderTests, TestPauseAfterProgress) { |
| 372 | const char* srcPath = "/data/local/tmp/TranscodingTestAssets/longtest_15s.mp4"; |
| 373 | const char* destPath = "/data/local/tmp/MediaTranscoder_Pause.MP4"; |
| 374 | |
| 375 | for (int i = 0; i < 20; ++i) { |
| 376 | EXPECT_EQ(transcodeHelper(srcPath, destPath, getAVCVideoFormat, kPauseAfterProgress), |
| 377 | AMEDIA_OK); |
| 378 | EXPECT_FALSE(mCallbacks->mFinished); |
Linus Nilsson | 52df71c | 2021-03-08 13:01:11 -0800 | [diff] [blame] | 379 | mCallbacks = std::make_shared<TestTranscoderCallbacks>(); |
Linus Nilsson | fdb3e33 | 2020-09-18 17:11:41 -0700 | [diff] [blame] | 380 | } |
| 381 | } |
| 382 | |
| 383 | TEST_F(MediaTranscoderTests, TestPauseAfterStart) { |
| 384 | const char* srcPath = "/data/local/tmp/TranscodingTestAssets/longtest_15s.mp4"; |
| 385 | const char* destPath = "/data/local/tmp/MediaTranscoder_Pause.MP4"; |
| 386 | |
| 387 | for (int i = 0; i < 20; ++i) { |
| 388 | EXPECT_EQ(transcodeHelper(srcPath, destPath, getAVCVideoFormat, kPauseAfterStart), |
| 389 | AMEDIA_OK); |
| 390 | EXPECT_FALSE(mCallbacks->mFinished); |
Linus Nilsson | 52df71c | 2021-03-08 13:01:11 -0800 | [diff] [blame] | 391 | mCallbacks = std::make_shared<TestTranscoderCallbacks>(); |
Linus Nilsson | 93cf913 | 2020-09-24 12:12:48 -0700 | [diff] [blame] | 392 | } |
| 393 | } |
| 394 | |
Chong Zhang | 457c689 | 2021-02-01 15:34:20 -0800 | [diff] [blame] | 395 | TEST_F(MediaTranscoderTests, TestHeartBeat) { |
| 396 | const char* srcPath = "/data/local/tmp/TranscodingTestAssets/longtest_15s.mp4"; |
| 397 | const char* destPath = "/data/local/tmp/MediaTranscoder_HeartBeat.MP4"; |
| 398 | |
| 399 | // Use a shorter value of 500ms than the default 1000ms to get more heart beat for testing. |
| 400 | const int64_t heartBeatIntervalUs = 500000LL; |
| 401 | EXPECT_EQ(transcodeHelper(srcPath, destPath, getAVCVideoFormat, kCheckHeartBeat, |
| 402 | heartBeatIntervalUs), |
| 403 | AMEDIA_OK); |
| 404 | EXPECT_TRUE(mCallbacks->mFinished); |
| 405 | } |
| 406 | |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 407 | } // namespace android |
| 408 | |
| 409 | int main(int argc, char** argv) { |
| 410 | ::testing::InitGoogleTest(&argc, argv); |
| 411 | return RUN_ALL_TESTS(); |
| 412 | } |