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> |
| 23 | #include <gtest/gtest.h> |
Chong Zhang | 5855ee5 | 2020-06-22 11:41:25 -0700 | [diff] [blame] | 24 | #include <media/MediaSampleReaderNDK.h> |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 25 | #include <media/MediaTranscoder.h> |
Chong Zhang | 5855ee5 | 2020-06-22 11:41:25 -0700 | [diff] [blame] | 26 | #include <media/NdkCommon.h> |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 27 | |
| 28 | namespace android { |
| 29 | |
Chong Zhang | 5855ee5 | 2020-06-22 11:41:25 -0700 | [diff] [blame] | 30 | #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 | |
| 38 | DEFINE_FORMAT_VALUE_EQUAL_FUNC(int64_t, Int64); |
| 39 | DEFINE_FORMAT_VALUE_EQUAL_FUNC(int32_t, Int32); |
| 40 | |
| 41 | struct FormatVerifierEntry { |
| 42 | const char* key; |
Chong Zhang | d6e4aec | 2020-06-22 14:13:07 -0700 | [diff] [blame] | 43 | std::function<bool(const char*, AMediaFormat*, AMediaFormat*)> equal; |
Chong Zhang | 5855ee5 | 2020-06-22 11:41:25 -0700 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | static 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 Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 54 | class TestCallbacks : public MediaTranscoder::CallbackInterface { |
| 55 | public: |
| 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 | |
| 89 | private: |
| 90 | std::mutex mMutex; |
| 91 | std::condition_variable mCondition; |
| 92 | bool mFinished = false; |
| 93 | }; |
| 94 | |
Chong Zhang | 308e91f | 2020-06-10 15:27:56 -0700 | [diff] [blame] | 95 | // Write-only, create file if non-existent, don't overwrite existing file. |
| 96 | static constexpr int kOpenFlags = O_WRONLY | O_CREAT | O_EXCL; |
| 97 | // User R+W permission. |
| 98 | static constexpr int kFileMode = S_IRUSR | S_IWUSR; |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 99 | |
| 100 | class MediaTranscoderTests : public ::testing::Test { |
| 101 | public: |
| 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 Zhang | 5855ee5 | 2020-06-22 11:41:25 -0700 | [diff] [blame] | 118 | media_status_t transcodeHelper(const char* srcPath, const char* destPath, |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 119 | FormatConfigurationCallback formatCallback) { |
| 120 | auto transcoder = MediaTranscoder::create(mCallbacks, nullptr); |
| 121 | EXPECT_NE(transcoder, nullptr); |
| 122 | |
Chong Zhang | 5855ee5 | 2020-06-22 11:41:25 -0700 | [diff] [blame] | 123 | const int srcFd = open(srcPath, O_RDONLY); |
Chong Zhang | 308e91f | 2020-06-10 15:27:56 -0700 | [diff] [blame] | 124 | EXPECT_EQ(transcoder->configureSource(srcFd), AMEDIA_OK); |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 125 | |
| 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 Zhang | 5855ee5 | 2020-06-22 11:41:25 -0700 | [diff] [blame] | 132 | |
| 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 Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 140 | if (format != nullptr) { |
| 141 | AMediaFormat_delete(format); |
| 142 | } |
| 143 | } |
| 144 | deleteFile(destPath); |
Chong Zhang | 308e91f | 2020-06-10 15:27:56 -0700 | [diff] [blame] | 145 | const int dstFd = open(destPath, kOpenFlags, kFileMode); |
| 146 | EXPECT_EQ(transcoder->configureDestination(dstFd), AMEDIA_OK); |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 147 | |
| 148 | media_status_t startStatus = transcoder->start(); |
| 149 | EXPECT_EQ(startStatus, AMEDIA_OK); |
| 150 | if (startStatus == AMEDIA_OK) { |
| 151 | mCallbacks->waitForTranscodingFinished(); |
| 152 | } |
Chong Zhang | 308e91f | 2020-06-10 15:27:56 -0700 | [diff] [blame] | 153 | close(srcFd); |
| 154 | close(dstFd); |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 155 | |
| 156 | return mCallbacks->mStatus; |
| 157 | } |
| 158 | |
Chong Zhang | d6e4aec | 2020-06-22 14:13:07 -0700 | [diff] [blame] | 159 | 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 Zhang | 5855ee5 | 2020-06-22 11:41:25 -0700 | [diff] [blame] | 197 | 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 Zhang | d6e4aec | 2020-06-22 14:13:07 -0700 | [diff] [blame] | 230 | mSourceVideoFormat.get(), videoFormat.get())) |
| 231 | << "Failed at key " << kFieldsToPreserve[i].key; |
Chong Zhang | 5855ee5 | 2020-06-22 11:41:25 -0700 | [diff] [blame] | 232 | } |
| 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 Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 244 | std::shared_ptr<TestCallbacks> mCallbacks; |
Chong Zhang | 5855ee5 | 2020-06-22 11:41:25 -0700 | [diff] [blame] | 245 | std::shared_ptr<AMediaFormat> mSourceVideoFormat; |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 246 | }; |
| 247 | |
| 248 | TEST_F(MediaTranscoderTests, TestPassthrough) { |
Chong Zhang | d6e4aec | 2020-06-22 14:13:07 -0700 | [diff] [blame] | 249 | 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] | 250 | const char* destPath = "/data/local/tmp/MediaTranscoder_Passthrough.MP4"; |
| 251 | |
Chong Zhang | d6e4aec | 2020-06-22 14:13:07 -0700 | [diff] [blame] | 252 | EXPECT_EQ(transcodeHelper(srcPath, destPath, [](AMediaFormat*) { return nullptr; }), AMEDIA_OK); |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 253 | |
Chong Zhang | 5855ee5 | 2020-06-22 11:41:25 -0700 | [diff] [blame] | 254 | verifyOutputFormat(destPath); |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 255 | } |
| 256 | |
Chong Zhang | d6e4aec | 2020-06-22 14:13:07 -0700 | [diff] [blame] | 257 | TEST_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 Zhang | 5855ee5 | 2020-06-22 11:41:25 -0700 | [diff] [blame] | 261 | } |
| 262 | |
Chong Zhang | d6e4aec | 2020-06-22 14:13:07 -0700 | [diff] [blame] | 263 | TEST_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 Zhang | 5855ee5 | 2020-06-22 11:41:25 -0700 | [diff] [blame] | 268 | |
Chong Zhang | d6e4aec | 2020-06-22 14:13:07 -0700 | [diff] [blame] | 269 | TEST_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 Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | } // namespace android |
| 277 | |
| 278 | int main(int argc, char** argv) { |
| 279 | ::testing::InitGoogleTest(&argc, argv); |
| 280 | return RUN_ALL_TESTS(); |
| 281 | } |