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