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 | |
| 118 | using FormatConfigurationCallback = std::function<AMediaFormat*(AMediaFormat*)>; |
Chong Zhang | 5855ee5 | 2020-06-22 11:41:25 -0700 | [diff] [blame] | 119 | media_status_t transcodeHelper(const char* srcPath, const char* destPath, |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 120 | FormatConfigurationCallback formatCallback) { |
| 121 | auto transcoder = MediaTranscoder::create(mCallbacks, nullptr); |
| 122 | EXPECT_NE(transcoder, nullptr); |
| 123 | |
Chong Zhang | 5855ee5 | 2020-06-22 11:41:25 -0700 | [diff] [blame] | 124 | const int srcFd = open(srcPath, O_RDONLY); |
Chong Zhang | 308e91f | 2020-06-10 15:27:56 -0700 | [diff] [blame] | 125 | EXPECT_EQ(transcoder->configureSource(srcFd), AMEDIA_OK); |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 126 | |
| 127 | std::vector<std::shared_ptr<AMediaFormat>> trackFormats = transcoder->getTrackFormats(); |
| 128 | EXPECT_GT(trackFormats.size(), 0); |
| 129 | |
| 130 | for (int i = 0; i < trackFormats.size(); ++i) { |
| 131 | AMediaFormat* format = formatCallback(trackFormats[i].get()); |
| 132 | EXPECT_EQ(transcoder->configureTrackFormat(i, format), AMEDIA_OK); |
Chong Zhang | 5855ee5 | 2020-06-22 11:41:25 -0700 | [diff] [blame] | 133 | |
| 134 | // Save original video track format for verification. |
| 135 | const char* mime = nullptr; |
| 136 | AMediaFormat_getString(trackFormats[i].get(), AMEDIAFORMAT_KEY_MIME, &mime); |
| 137 | if (strncmp(mime, "video/", 6) == 0) { |
| 138 | mSourceVideoFormat = trackFormats[i]; |
| 139 | } |
| 140 | |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 141 | if (format != nullptr) { |
| 142 | AMediaFormat_delete(format); |
| 143 | } |
| 144 | } |
| 145 | deleteFile(destPath); |
Chong Zhang | 308e91f | 2020-06-10 15:27:56 -0700 | [diff] [blame] | 146 | const int dstFd = open(destPath, kOpenFlags, kFileMode); |
| 147 | EXPECT_EQ(transcoder->configureDestination(dstFd), AMEDIA_OK); |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 148 | |
| 149 | media_status_t startStatus = transcoder->start(); |
| 150 | EXPECT_EQ(startStatus, AMEDIA_OK); |
| 151 | if (startStatus == AMEDIA_OK) { |
| 152 | mCallbacks->waitForTranscodingFinished(); |
| 153 | } |
Chong Zhang | 308e91f | 2020-06-10 15:27:56 -0700 | [diff] [blame] | 154 | close(srcFd); |
| 155 | close(dstFd); |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 156 | |
| 157 | return mCallbacks->mStatus; |
| 158 | } |
| 159 | |
Chong Zhang | d6e4aec | 2020-06-22 14:13:07 -0700 | [diff] [blame] | 160 | void testTranscodeVideo(const char* srcPath, const char* destPath, const char* dstMime) { |
| 161 | const int32_t kBitRate = 8 * 1000 * 1000; // 8Mbs |
| 162 | |
| 163 | EXPECT_EQ( |
| 164 | transcodeHelper( |
| 165 | srcPath, destPath, |
| 166 | [dstMime](AMediaFormat* sourceFormat) { |
| 167 | AMediaFormat* format = nullptr; |
| 168 | const char* mime = nullptr; |
| 169 | AMediaFormat_getString(sourceFormat, AMEDIAFORMAT_KEY_MIME, &mime); |
| 170 | |
| 171 | if (strncmp(mime, "video/", 6) == 0) { |
| 172 | format = AMediaFormat_new(); |
| 173 | AMediaFormat_setInt32(format, AMEDIAFORMAT_KEY_BIT_RATE, kBitRate); |
| 174 | |
| 175 | if (dstMime != nullptr) { |
| 176 | AMediaFormat_setString(format, AMEDIAFORMAT_KEY_MIME, dstMime); |
| 177 | } |
| 178 | } |
| 179 | return format; |
| 180 | }), |
| 181 | AMEDIA_OK); |
| 182 | |
| 183 | if (dstMime != nullptr) { |
| 184 | std::vector<FormatVerifierEntry> extraVerifiers = { |
| 185 | {AMEDIAFORMAT_KEY_MIME, |
| 186 | [dstMime](const char* key, AMediaFormat* src __unused, AMediaFormat* dst) { |
| 187 | const char* mime = nullptr; |
| 188 | AMediaFormat_getString(dst, key, &mime); |
| 189 | return !strcmp(mime, dstMime); |
| 190 | }}, |
| 191 | }; |
| 192 | verifyOutputFormat(destPath, &extraVerifiers); |
| 193 | } else { |
| 194 | verifyOutputFormat(destPath); |
| 195 | } |
| 196 | } |
| 197 | |
Chong Zhang | 5855ee5 | 2020-06-22 11:41:25 -0700 | [diff] [blame] | 198 | void verifyOutputFormat(const char* destPath, |
| 199 | const std::vector<FormatVerifierEntry>* extraVerifiers = nullptr) { |
| 200 | int dstFd = open(destPath, O_RDONLY); |
| 201 | EXPECT_GT(dstFd, 0); |
| 202 | ssize_t fileSize = lseek(dstFd, 0, SEEK_END); |
| 203 | lseek(dstFd, 0, SEEK_SET); |
| 204 | |
| 205 | std::shared_ptr<MediaSampleReader> sampleReader = |
| 206 | MediaSampleReaderNDK::createFromFd(dstFd, 0, fileSize); |
Linus Nilsson | 42a971b | 2020-07-01 16:41:11 -0700 | [diff] [blame] | 207 | ASSERT_NE(sampleReader, nullptr); |
Chong Zhang | 5855ee5 | 2020-06-22 11:41:25 -0700 | [diff] [blame] | 208 | |
| 209 | std::shared_ptr<AMediaFormat> videoFormat; |
| 210 | const size_t trackCount = sampleReader->getTrackCount(); |
| 211 | for (size_t trackIndex = 0; trackIndex < trackCount; ++trackIndex) { |
| 212 | AMediaFormat* trackFormat = sampleReader->getTrackFormat(static_cast<int>(trackIndex)); |
| 213 | if (trackFormat != nullptr) { |
| 214 | const char* mime = nullptr; |
| 215 | AMediaFormat_getString(trackFormat, AMEDIAFORMAT_KEY_MIME, &mime); |
Linus Nilsson | 42a971b | 2020-07-01 16:41:11 -0700 | [diff] [blame] | 216 | |
Chong Zhang | 5855ee5 | 2020-06-22 11:41:25 -0700 | [diff] [blame] | 217 | if (strncmp(mime, "video/", 6) == 0) { |
| 218 | LOG(INFO) << "Track # " << trackIndex << ": " |
| 219 | << AMediaFormat_toString(trackFormat); |
| 220 | videoFormat = std::shared_ptr<AMediaFormat>(trackFormat, &AMediaFormat_delete); |
| 221 | break; |
| 222 | } |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | EXPECT_NE(videoFormat, nullptr); |
| 227 | |
| 228 | LOG(INFO) << "source video format: " << AMediaFormat_toString(mSourceVideoFormat.get()); |
| 229 | LOG(INFO) << "transcoded video format: " << AMediaFormat_toString(videoFormat.get()); |
| 230 | |
| 231 | for (int i = 0; i < (sizeof(kFieldsToPreserve) / sizeof(kFieldsToPreserve[0])); ++i) { |
| 232 | EXPECT_TRUE(kFieldsToPreserve[i].equal(kFieldsToPreserve[i].key, |
Chong Zhang | d6e4aec | 2020-06-22 14:13:07 -0700 | [diff] [blame] | 233 | mSourceVideoFormat.get(), videoFormat.get())) |
| 234 | << "Failed at key " << kFieldsToPreserve[i].key; |
Chong Zhang | 5855ee5 | 2020-06-22 11:41:25 -0700 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | if (extraVerifiers != nullptr) { |
| 238 | for (int i = 0; i < extraVerifiers->size(); ++i) { |
| 239 | const FormatVerifierEntry& entry = (*extraVerifiers)[i]; |
| 240 | EXPECT_TRUE(entry.equal(entry.key, mSourceVideoFormat.get(), videoFormat.get())); |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | close(dstFd); |
| 245 | } |
| 246 | |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 247 | std::shared_ptr<TestCallbacks> mCallbacks; |
Chong Zhang | 5855ee5 | 2020-06-22 11:41:25 -0700 | [diff] [blame] | 248 | std::shared_ptr<AMediaFormat> mSourceVideoFormat; |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 249 | }; |
| 250 | |
| 251 | TEST_F(MediaTranscoderTests, TestPassthrough) { |
Chong Zhang | d6e4aec | 2020-06-22 14:13:07 -0700 | [diff] [blame] | 252 | 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] | 253 | const char* destPath = "/data/local/tmp/MediaTranscoder_Passthrough.MP4"; |
| 254 | |
Chong Zhang | d6e4aec | 2020-06-22 14:13:07 -0700 | [diff] [blame] | 255 | EXPECT_EQ(transcodeHelper(srcPath, destPath, [](AMediaFormat*) { return nullptr; }), AMEDIA_OK); |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 256 | |
Chong Zhang | 5855ee5 | 2020-06-22 11:41:25 -0700 | [diff] [blame] | 257 | verifyOutputFormat(destPath); |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 258 | } |
| 259 | |
Chong Zhang | d6e4aec | 2020-06-22 14:13:07 -0700 | [diff] [blame] | 260 | TEST_F(MediaTranscoderTests, TestVideoTranscode_AvcToAvc_Basic) { |
| 261 | const char* srcPath = "/data/local/tmp/TranscodingTestAssets/cubicle_avc_480x240_aac_24KHz.mp4"; |
| 262 | const char* destPath = "/data/local/tmp/MediaTranscoder_VideoTranscode_AvcToAvc_Basic.MP4"; |
| 263 | testTranscodeVideo(srcPath, destPath, nullptr /*dstMime*/); |
Chong Zhang | 5855ee5 | 2020-06-22 11:41:25 -0700 | [diff] [blame] | 264 | } |
| 265 | |
Chong Zhang | d6e4aec | 2020-06-22 14:13:07 -0700 | [diff] [blame] | 266 | TEST_F(MediaTranscoderTests, TestVideoTranscode_HevcToAvc_Basic) { |
| 267 | const char* srcPath = "/data/local/tmp/TranscodingTestAssets/jets_hevc_1280x720_20Mbps.mp4"; |
| 268 | const char* destPath = "/data/local/tmp/MediaTranscoder_VideoTranscode_HevcToAvc_Basic.MP4"; |
| 269 | testTranscodeVideo(srcPath, destPath, AMEDIA_MIMETYPE_VIDEO_AVC); |
| 270 | } |
Chong Zhang | 5855ee5 | 2020-06-22 11:41:25 -0700 | [diff] [blame] | 271 | |
Chong Zhang | d6e4aec | 2020-06-22 14:13:07 -0700 | [diff] [blame] | 272 | TEST_F(MediaTranscoderTests, TestVideoTranscode_HevcToAvc_Rotation) { |
| 273 | const char* srcPath = |
| 274 | "/data/local/tmp/TranscodingTestAssets/desk_hevc_1920x1080_aac_48KHz_rot90.mp4"; |
| 275 | const char* destPath = "/data/local/tmp/MediaTranscoder_VideoTranscode_HevcToAvc_Rotation.MP4"; |
| 276 | testTranscodeVideo(srcPath, destPath, AMEDIA_MIMETYPE_VIDEO_AVC); |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | } // namespace android |
| 280 | |
| 281 | int main(int argc, char** argv) { |
| 282 | ::testing::InitGoogleTest(&argc, argv); |
| 283 | return RUN_ALL_TESTS(); |
| 284 | } |