blob: c4a67bb65d926ffb72fb617e7f422f91387f2a52 [file] [log] [blame]
Linus Nilssoncab39d82020-05-14 16:32:21 -07001/*
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
26namespace android {
27
28class TestCallbacks : public MediaTranscoder::CallbackInterface {
29public:
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
63private:
64 std::mutex mMutex;
65 std::condition_variable mCondition;
66 bool mFinished = false;
67};
68
69static const char* SOURCE_PATH =
70 "/data/local/tmp/TranscoderTestAssets/cubicle_avc_480x240_aac_24KHz.mp4";
71
72class MediaTranscoderTests : public ::testing::Test {
73public:
74 MediaTranscoderTests() { LOG(DEBUG) << "MediaTranscoderTests created"; }
75 ~MediaTranscoderTests() { LOG(DEBUG) << "MediaTranscoderTests destroyed"; }
76
77 void SetUp() override {
78 LOG(DEBUG) << "MediaTranscoderTests set up";
79 mCallbacks = std::make_shared<TestCallbacks>();
80 }
81
82 void TearDown() override {
83 LOG(DEBUG) << "MediaTranscoderTests tear down";
84 mCallbacks.reset();
85 }
86
87 void deleteFile(const char* path) { unlink(path); }
88
89 using FormatConfigurationCallback = std::function<AMediaFormat*(AMediaFormat*)>;
90 media_status_t transcodeHelper(const char* destPath,
91 FormatConfigurationCallback formatCallback) {
92 auto transcoder = MediaTranscoder::create(mCallbacks, nullptr);
93 EXPECT_NE(transcoder, nullptr);
94
95 EXPECT_EQ(transcoder->configureSource(SOURCE_PATH), AMEDIA_OK);
96
97 std::vector<std::shared_ptr<AMediaFormat>> trackFormats = transcoder->getTrackFormats();
98 EXPECT_GT(trackFormats.size(), 0);
99
100 for (int i = 0; i < trackFormats.size(); ++i) {
101 AMediaFormat* format = formatCallback(trackFormats[i].get());
102 EXPECT_EQ(transcoder->configureTrackFormat(i, format), AMEDIA_OK);
103 if (format != nullptr) {
104 AMediaFormat_delete(format);
105 }
106 }
107 deleteFile(destPath);
108 EXPECT_EQ(transcoder->configureDestination(destPath), AMEDIA_OK);
109
110 media_status_t startStatus = transcoder->start();
111 EXPECT_EQ(startStatus, AMEDIA_OK);
112 if (startStatus == AMEDIA_OK) {
113 mCallbacks->waitForTranscodingFinished();
114 }
115
116 return mCallbacks->mStatus;
117 }
118
119 std::shared_ptr<TestCallbacks> mCallbacks;
120};
121
122TEST_F(MediaTranscoderTests, TestPassthrough) {
123 const char* destPath = "/data/local/tmp/MediaTranscoder_Passthrough.MP4";
124
125 EXPECT_EQ(transcodeHelper(destPath, [](AMediaFormat*) { return nullptr; }), AMEDIA_OK);
126
127 // TODO: Validate output file
128}
129
130TEST_F(MediaTranscoderTests, TestBasicVideoTranscode) {
131 const char* destPath = "/data/local/tmp/MediaTranscoder_VideoTranscode.MP4";
132
133 EXPECT_EQ(transcodeHelper(
134 destPath,
135 [](AMediaFormat* sourceFormat) {
136 AMediaFormat* format = nullptr;
137 const char* mime = nullptr;
138 AMediaFormat_getString(sourceFormat, AMEDIAFORMAT_KEY_MIME, &mime);
139
140 if (strncmp(mime, "video/", 6) == 0) {
141 const int32_t kBitRate = 8 * 1000 * 1000; // 8Mbs
142 format = AMediaFormat_new();
143 AMediaFormat_setInt32(format, AMEDIAFORMAT_KEY_BIT_RATE, kBitRate);
144 }
145 return format;
146 }),
147 AMEDIA_OK);
148
149 // TODO: Validate output file
150}
151
152} // namespace android
153
154int main(int argc, char** argv) {
155 ::testing::InitGoogleTest(&argc, argv);
156 return RUN_ALL_TESTS();
157}