blob: 5f2cd126891641f0a4ca0f3ac9ae6990683c469f [file] [log] [blame]
Linus Nilsson0da327a2020-01-31 16:22:18 -08001/*
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 VideoTrackTranscoder
18
19// #define LOG_NDEBUG 0
20#define LOG_TAG "VideoTrackTranscoderTests"
21
22#include <android-base/logging.h>
23#include <fcntl.h>
24#include <gtest/gtest.h>
25#include <media/MediaSampleReaderNDK.h>
26#include <media/VideoTrackTranscoder.h>
27#include <utils/Timers.h>
28
29#include "TrackTranscoderTestUtils.h"
30
31namespace android {
32
33// TODO(b/155304421): Implement more advanced video specific tests:
34// - Codec conversions (HEVC -> AVC).
35// - Bitrate validation.
36// - Output frame validation through PSNR.
37
38class VideoTrackTranscoderTests : public ::testing::Test {
39public:
40 VideoTrackTranscoderTests() { LOG(DEBUG) << "VideoTrackTranscoderTests created"; }
41
42 void SetUp() override {
43 LOG(DEBUG) << "VideoTrackTranscoderTests set up";
44 const char* sourcePath =
hkuang2ef2b432020-06-15 18:33:11 -070045 "/data/local/tmp/TranscodingTestAssets/cubicle_avc_480x240_aac_24KHz.mp4";
Linus Nilsson0da327a2020-01-31 16:22:18 -080046
47 const int sourceFd = open(sourcePath, O_RDONLY);
48 ASSERT_GT(sourceFd, 0);
49
50 const off_t fileSize = lseek(sourceFd, 0, SEEK_END);
51 lseek(sourceFd, 0, SEEK_SET);
52
53 mMediaSampleReader = MediaSampleReaderNDK::createFromFd(sourceFd, 0, fileSize);
54 ASSERT_NE(mMediaSampleReader, nullptr);
55 close(sourceFd);
56
57 for (size_t trackIndex = 0; trackIndex < mMediaSampleReader->getTrackCount();
58 ++trackIndex) {
59 AMediaFormat* trackFormat = mMediaSampleReader->getTrackFormat(trackIndex);
60 ASSERT_NE(trackFormat, nullptr);
61
62 const char* mime = nullptr;
63 AMediaFormat_getString(trackFormat, AMEDIAFORMAT_KEY_MIME, &mime);
64 ASSERT_NE(mime, nullptr);
65
66 if (strncmp(mime, "video/", 6) == 0) {
67 mTrackIndex = trackIndex;
68
69 mSourceFormat = std::shared_ptr<AMediaFormat>(
70 trackFormat, std::bind(AMediaFormat_delete, std::placeholders::_1));
71 ASSERT_NE(mSourceFormat, nullptr);
72
73 mDestinationFormat =
74 TrackTranscoderTestUtils::getDefaultVideoDestinationFormat(trackFormat);
75 ASSERT_NE(mDestinationFormat, nullptr);
76 break;
77 }
78
79 AMediaFormat_delete(trackFormat);
80 }
81
82 ASSERT_NE(mSourceFormat, nullptr);
83 }
84
85 void TearDown() override { LOG(DEBUG) << "VideoTrackTranscoderTests tear down"; }
86
87 ~VideoTrackTranscoderTests() { LOG(DEBUG) << "VideoTrackTranscoderTests destroyed"; }
88
89 std::shared_ptr<MediaSampleReader> mMediaSampleReader;
90 int mTrackIndex;
91 std::shared_ptr<AMediaFormat> mSourceFormat;
92 std::shared_ptr<AMediaFormat> mDestinationFormat;
93};
94
95TEST_F(VideoTrackTranscoderTests, SampleSanity) {
96 LOG(DEBUG) << "Testing SampleSanity";
97 std::shared_ptr<TestCallback> callback = std::make_shared<TestCallback>();
Linus Nilssone4716f22020-07-10 16:07:57 -070098 auto transcoder = VideoTrackTranscoder::create(callback);
Linus Nilsson0da327a2020-01-31 16:22:18 -080099
Linus Nilssone4716f22020-07-10 16:07:57 -0700100 EXPECT_EQ(transcoder->configure(mMediaSampleReader, mTrackIndex, mDestinationFormat),
101 AMEDIA_OK);
102 ASSERT_TRUE(transcoder->start());
Linus Nilsson0da327a2020-01-31 16:22:18 -0800103
Linus Nilssone4716f22020-07-10 16:07:57 -0700104 std::shared_ptr<MediaSampleQueue> outputQueue = transcoder->getOutputQueue();
Linus Nilssoncab39d82020-05-14 16:32:21 -0700105 std::thread sampleConsumerThread{[&outputQueue] {
Linus Nilsson0da327a2020-01-31 16:22:18 -0800106 uint64_t sampleCount = 0;
107 std::shared_ptr<MediaSample> sample;
Linus Nilssoncab39d82020-05-14 16:32:21 -0700108 while (!outputQueue->dequeue(&sample)) {
Linus Nilsson0da327a2020-01-31 16:22:18 -0800109 ASSERT_NE(sample, nullptr);
110 const uint32_t flags = sample->info.flags;
111
112 if (sampleCount == 0) {
113 // Expect first sample to be a codec config.
114 EXPECT_TRUE((flags & SAMPLE_FLAG_CODEC_CONFIG) != 0);
115 EXPECT_TRUE((flags & SAMPLE_FLAG_SYNC_SAMPLE) == 0);
116 EXPECT_TRUE((flags & SAMPLE_FLAG_END_OF_STREAM) == 0);
117 EXPECT_TRUE((flags & SAMPLE_FLAG_PARTIAL_FRAME) == 0);
118 } else if (sampleCount == 1) {
119 // Expect second sample to be a sync sample.
120 EXPECT_TRUE((flags & SAMPLE_FLAG_CODEC_CONFIG) == 0);
121 EXPECT_TRUE((flags & SAMPLE_FLAG_SYNC_SAMPLE) != 0);
122 EXPECT_TRUE((flags & SAMPLE_FLAG_END_OF_STREAM) == 0);
123 }
124
125 if (!(flags & SAMPLE_FLAG_END_OF_STREAM)) {
126 // Expect a valid buffer unless it is EOS.
127 EXPECT_NE(sample->buffer, nullptr);
128 EXPECT_NE(sample->bufferId, 0xBAADF00D);
129 EXPECT_GT(sample->info.size, 0);
130 }
131
132 ++sampleCount;
133 if (sample->info.flags & SAMPLE_FLAG_END_OF_STREAM) {
134 break;
135 }
136 sample.reset();
137 }
138 }};
139
140 EXPECT_EQ(callback->waitUntilFinished(), AMEDIA_OK);
Linus Nilssone4716f22020-07-10 16:07:57 -0700141 EXPECT_TRUE(transcoder->stop());
Linus Nilsson0da327a2020-01-31 16:22:18 -0800142
143 sampleConsumerThread.join();
144}
145
146// VideoTrackTranscoder needs a valid destination format.
147TEST_F(VideoTrackTranscoderTests, NullDestinationFormat) {
148 LOG(DEBUG) << "Testing NullDestinationFormat";
149 std::shared_ptr<TestCallback> callback = std::make_shared<TestCallback>();
150 std::shared_ptr<AMediaFormat> nullFormat;
151
Linus Nilssone4716f22020-07-10 16:07:57 -0700152 auto transcoder = VideoTrackTranscoder::create(callback);
153 EXPECT_EQ(transcoder->configure(mMediaSampleReader, 0 /* trackIndex */, nullFormat),
Linus Nilsson0da327a2020-01-31 16:22:18 -0800154 AMEDIA_ERROR_INVALID_PARAMETER);
155}
156
Linus Nilssone4716f22020-07-10 16:07:57 -0700157TEST_F(VideoTrackTranscoderTests, LingeringEncoder) {
158 struct {
159 void wait() {
160 std::unique_lock<std::mutex> lock(mMutex);
161 while (!mSignaled) {
162 mCondition.wait(lock);
163 }
164 }
165
166 void signal() {
167 std::unique_lock<std::mutex> lock(mMutex);
168 mSignaled = true;
169 mCondition.notify_all();
170 }
171
172 std::mutex mMutex;
173 std::condition_variable mCondition;
174 bool mSignaled = false;
175 } semaphore;
176
177 auto callback = std::make_shared<TestCallback>();
178 auto transcoder = VideoTrackTranscoder::create(callback);
179
180 EXPECT_EQ(transcoder->configure(mMediaSampleReader, mTrackIndex, mDestinationFormat),
181 AMEDIA_OK);
182 ASSERT_TRUE(transcoder->start());
183
184 std::shared_ptr<MediaSampleQueue> outputQueue = transcoder->getOutputQueue();
185 std::vector<std::shared_ptr<MediaSample>> samples;
186 std::thread sampleConsumerThread([&outputQueue, &samples, &semaphore] {
187 std::shared_ptr<MediaSample> sample;
188 while (samples.size() < 10 && !outputQueue->dequeue(&sample)) {
189 ASSERT_NE(sample, nullptr);
190 samples.push_back(sample);
191
192 if (sample->info.flags & SAMPLE_FLAG_END_OF_STREAM) {
193 break;
194 }
195 sample.reset();
196 }
197
198 semaphore.signal();
199 });
200
201 // Wait for the encoder to output samples before stopping and releasing the transcoder.
202 semaphore.wait();
203
204 EXPECT_TRUE(transcoder->stop());
205 transcoder.reset();
206 sampleConsumerThread.join();
207
208 // Return buffers to the codec so that it can resume processing, but keep one buffer to avoid
209 // the codec being released.
210 samples.resize(1);
211
212 // Wait for async codec events.
213 std::this_thread::sleep_for(std::chrono::seconds(1));
214}
215
Linus Nilsson0da327a2020-01-31 16:22:18 -0800216} // namespace android
217
218int main(int argc, char** argv) {
219 ::testing::InitGoogleTest(&argc, argv);
220 return RUN_ALL_TESTS();
221}