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> |
| 24 | #include <media/MediaTranscoder.h> |
| 25 | |
| 26 | namespace android { |
| 27 | |
| 28 | class TestCallbacks : public MediaTranscoder::CallbackInterface { |
| 29 | public: |
| 30 | virtual void onFinished(const MediaTranscoder* transcoder __unused) override { |
| 31 | std::unique_lock<std::mutex> lock(mMutex); |
| 32 | EXPECT_FALSE(mFinished); |
| 33 | mFinished = true; |
| 34 | mCondition.notify_all(); |
| 35 | } |
| 36 | |
| 37 | virtual void onError(const MediaTranscoder* transcoder __unused, |
| 38 | media_status_t error) override { |
| 39 | std::unique_lock<std::mutex> lock(mMutex); |
| 40 | EXPECT_NE(error, AMEDIA_OK); |
| 41 | EXPECT_FALSE(mFinished); |
| 42 | mFinished = true; |
| 43 | mStatus = error; |
| 44 | mCondition.notify_all(); |
| 45 | } |
| 46 | |
| 47 | virtual void onProgressUpdate(const MediaTranscoder* transcoder __unused, |
| 48 | int32_t progress __unused) override {} |
| 49 | |
| 50 | virtual void onCodecResourceLost(const MediaTranscoder* transcoder __unused, |
| 51 | const std::shared_ptr<const Parcelable>& pausedState |
| 52 | __unused) override {} |
| 53 | |
| 54 | void waitForTranscodingFinished() { |
| 55 | std::unique_lock<std::mutex> lock(mMutex); |
| 56 | while (!mFinished) { |
| 57 | mCondition.wait(lock); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | media_status_t mStatus = AMEDIA_OK; |
| 62 | |
| 63 | private: |
| 64 | std::mutex mMutex; |
| 65 | std::condition_variable mCondition; |
| 66 | bool mFinished = false; |
| 67 | }; |
| 68 | |
| 69 | static const char* SOURCE_PATH = |
hkuang | 2ef2b43 | 2020-06-15 18:33:11 -0700 | [diff] [blame] | 70 | "/data/local/tmp/TranscodingTestAssets/cubicle_avc_480x240_aac_24KHz.mp4"; |
Chong Zhang | 308e91f | 2020-06-10 15:27:56 -0700 | [diff] [blame] | 71 | // Write-only, create file if non-existent, don't overwrite existing file. |
| 72 | static constexpr int kOpenFlags = O_WRONLY | O_CREAT | O_EXCL; |
| 73 | // User R+W permission. |
| 74 | static constexpr int kFileMode = S_IRUSR | S_IWUSR; |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 75 | |
| 76 | class MediaTranscoderTests : public ::testing::Test { |
| 77 | public: |
| 78 | MediaTranscoderTests() { LOG(DEBUG) << "MediaTranscoderTests created"; } |
| 79 | ~MediaTranscoderTests() { LOG(DEBUG) << "MediaTranscoderTests destroyed"; } |
| 80 | |
| 81 | void SetUp() override { |
| 82 | LOG(DEBUG) << "MediaTranscoderTests set up"; |
| 83 | mCallbacks = std::make_shared<TestCallbacks>(); |
| 84 | } |
| 85 | |
| 86 | void TearDown() override { |
| 87 | LOG(DEBUG) << "MediaTranscoderTests tear down"; |
| 88 | mCallbacks.reset(); |
| 89 | } |
| 90 | |
| 91 | void deleteFile(const char* path) { unlink(path); } |
| 92 | |
| 93 | using FormatConfigurationCallback = std::function<AMediaFormat*(AMediaFormat*)>; |
| 94 | media_status_t transcodeHelper(const char* destPath, |
| 95 | FormatConfigurationCallback formatCallback) { |
| 96 | auto transcoder = MediaTranscoder::create(mCallbacks, nullptr); |
| 97 | EXPECT_NE(transcoder, nullptr); |
| 98 | |
Chong Zhang | 308e91f | 2020-06-10 15:27:56 -0700 | [diff] [blame] | 99 | const int srcFd = open(SOURCE_PATH, O_RDONLY); |
| 100 | EXPECT_EQ(transcoder->configureSource(srcFd), AMEDIA_OK); |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 101 | |
| 102 | std::vector<std::shared_ptr<AMediaFormat>> trackFormats = transcoder->getTrackFormats(); |
| 103 | EXPECT_GT(trackFormats.size(), 0); |
| 104 | |
| 105 | for (int i = 0; i < trackFormats.size(); ++i) { |
| 106 | AMediaFormat* format = formatCallback(trackFormats[i].get()); |
| 107 | EXPECT_EQ(transcoder->configureTrackFormat(i, format), AMEDIA_OK); |
| 108 | if (format != nullptr) { |
| 109 | AMediaFormat_delete(format); |
| 110 | } |
| 111 | } |
| 112 | deleteFile(destPath); |
Chong Zhang | 308e91f | 2020-06-10 15:27:56 -0700 | [diff] [blame] | 113 | const int dstFd = open(destPath, kOpenFlags, kFileMode); |
| 114 | EXPECT_EQ(transcoder->configureDestination(dstFd), AMEDIA_OK); |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 115 | |
| 116 | media_status_t startStatus = transcoder->start(); |
| 117 | EXPECT_EQ(startStatus, AMEDIA_OK); |
| 118 | if (startStatus == AMEDIA_OK) { |
| 119 | mCallbacks->waitForTranscodingFinished(); |
| 120 | } |
Chong Zhang | 308e91f | 2020-06-10 15:27:56 -0700 | [diff] [blame] | 121 | close(srcFd); |
| 122 | close(dstFd); |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 123 | |
| 124 | return mCallbacks->mStatus; |
| 125 | } |
| 126 | |
| 127 | std::shared_ptr<TestCallbacks> mCallbacks; |
| 128 | }; |
| 129 | |
| 130 | TEST_F(MediaTranscoderTests, TestPassthrough) { |
| 131 | const char* destPath = "/data/local/tmp/MediaTranscoder_Passthrough.MP4"; |
| 132 | |
| 133 | EXPECT_EQ(transcodeHelper(destPath, [](AMediaFormat*) { return nullptr; }), AMEDIA_OK); |
| 134 | |
| 135 | // TODO: Validate output file |
| 136 | } |
| 137 | |
| 138 | TEST_F(MediaTranscoderTests, TestBasicVideoTranscode) { |
| 139 | const char* destPath = "/data/local/tmp/MediaTranscoder_VideoTranscode.MP4"; |
| 140 | |
| 141 | EXPECT_EQ(transcodeHelper( |
| 142 | destPath, |
| 143 | [](AMediaFormat* sourceFormat) { |
| 144 | AMediaFormat* format = nullptr; |
| 145 | const char* mime = nullptr; |
| 146 | AMediaFormat_getString(sourceFormat, AMEDIAFORMAT_KEY_MIME, &mime); |
| 147 | |
| 148 | if (strncmp(mime, "video/", 6) == 0) { |
| 149 | const int32_t kBitRate = 8 * 1000 * 1000; // 8Mbs |
| 150 | format = AMediaFormat_new(); |
| 151 | AMediaFormat_setInt32(format, AMEDIAFORMAT_KEY_BIT_RATE, kBitRate); |
| 152 | } |
| 153 | return format; |
| 154 | }), |
| 155 | AMEDIA_OK); |
| 156 | |
| 157 | // TODO: Validate output file |
| 158 | } |
| 159 | |
| 160 | } // namespace android |
| 161 | |
| 162 | int main(int argc, char** argv) { |
| 163 | ::testing::InitGoogleTest(&argc, argv); |
| 164 | return RUN_ALL_TESTS(); |
| 165 | } |