blob: b432553d33d03a83e21ca2ffba3fade54ad7c119 [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>
Linus Nilsson800793f2020-07-31 16:16:38 -070026#include <media/NdkCommon.h>
Linus Nilsson0da327a2020-01-31 16:22:18 -080027#include <media/VideoTrackTranscoder.h>
28#include <utils/Timers.h>
29
30#include "TrackTranscoderTestUtils.h"
31
32namespace android {
33
34// TODO(b/155304421): Implement more advanced video specific tests:
35// - Codec conversions (HEVC -> AVC).
36// - Bitrate validation.
37// - Output frame validation through PSNR.
38
39class VideoTrackTranscoderTests : public ::testing::Test {
40public:
41 VideoTrackTranscoderTests() { LOG(DEBUG) << "VideoTrackTranscoderTests created"; }
42
43 void SetUp() override {
44 LOG(DEBUG) << "VideoTrackTranscoderTests set up";
45 const char* sourcePath =
hkuang2ef2b432020-06-15 18:33:11 -070046 "/data/local/tmp/TranscodingTestAssets/cubicle_avc_480x240_aac_24KHz.mp4";
Linus Nilsson0da327a2020-01-31 16:22:18 -080047
48 const int sourceFd = open(sourcePath, O_RDONLY);
49 ASSERT_GT(sourceFd, 0);
50
51 const off_t fileSize = lseek(sourceFd, 0, SEEK_END);
52 lseek(sourceFd, 0, SEEK_SET);
53
54 mMediaSampleReader = MediaSampleReaderNDK::createFromFd(sourceFd, 0, fileSize);
55 ASSERT_NE(mMediaSampleReader, nullptr);
56 close(sourceFd);
57
58 for (size_t trackIndex = 0; trackIndex < mMediaSampleReader->getTrackCount();
59 ++trackIndex) {
60 AMediaFormat* trackFormat = mMediaSampleReader->getTrackFormat(trackIndex);
61 ASSERT_NE(trackFormat, nullptr);
62
63 const char* mime = nullptr;
64 AMediaFormat_getString(trackFormat, AMEDIAFORMAT_KEY_MIME, &mime);
65 ASSERT_NE(mime, nullptr);
66
67 if (strncmp(mime, "video/", 6) == 0) {
68 mTrackIndex = trackIndex;
69
Linus Nilsson800793f2020-07-31 16:16:38 -070070 mSourceFormat = std::shared_ptr<AMediaFormat>(trackFormat, &AMediaFormat_delete);
Linus Nilsson0da327a2020-01-31 16:22:18 -080071 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
Linus Nilsson443f16c2020-07-30 13:13:21 -070095TEST_F(VideoTrackTranscoderTests, SampleSoundness) {
96 LOG(DEBUG) << "Testing SampleSoundness";
Linus Nilsson0da327a2020-01-31 16:22:18 -080097 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
Linus Nilsson800793f2020-07-31 16:16:38 -0700146TEST_F(VideoTrackTranscoderTests, PreserveBitrate) {
147 LOG(DEBUG) << "Testing PreserveBitrate";
148 std::shared_ptr<TestCallback> callback = std::make_shared<TestCallback>();
149 std::shared_ptr<MediaTrackTranscoder> transcoder = VideoTrackTranscoder::create(callback);
150
151 auto destFormat = TrackTranscoderTestUtils::getDefaultVideoDestinationFormat(
152 mSourceFormat.get(), false /* includeBitrate*/);
153 EXPECT_NE(destFormat, nullptr);
154
155 ASSERT_EQ(transcoder->configure(mMediaSampleReader, mTrackIndex, destFormat), AMEDIA_OK);
156 ASSERT_TRUE(transcoder->start());
157
158 callback->waitUntilTrackFormatAvailable();
159
160 auto outputFormat = transcoder->getOutputFormat();
161 ASSERT_NE(outputFormat, nullptr);
162
163 ASSERT_TRUE(transcoder->stop());
164 transcoder->getOutputQueue()->abort();
165
166 int32_t outBitrate;
167 EXPECT_TRUE(AMediaFormat_getInt32(outputFormat.get(), AMEDIAFORMAT_KEY_BIT_RATE, &outBitrate));
168
169 int32_t srcBitrate;
170 EXPECT_EQ(mMediaSampleReader->getEstimatedBitrateForTrack(mTrackIndex, &srcBitrate), AMEDIA_OK);
171
172 EXPECT_EQ(srcBitrate, outBitrate);
173}
174
Linus Nilsson0da327a2020-01-31 16:22:18 -0800175// VideoTrackTranscoder needs a valid destination format.
176TEST_F(VideoTrackTranscoderTests, NullDestinationFormat) {
177 LOG(DEBUG) << "Testing NullDestinationFormat";
178 std::shared_ptr<TestCallback> callback = std::make_shared<TestCallback>();
179 std::shared_ptr<AMediaFormat> nullFormat;
180
Linus Nilssone4716f22020-07-10 16:07:57 -0700181 auto transcoder = VideoTrackTranscoder::create(callback);
182 EXPECT_EQ(transcoder->configure(mMediaSampleReader, 0 /* trackIndex */, nullFormat),
Linus Nilsson0da327a2020-01-31 16:22:18 -0800183 AMEDIA_ERROR_INVALID_PARAMETER);
184}
185
Linus Nilssone4716f22020-07-10 16:07:57 -0700186TEST_F(VideoTrackTranscoderTests, LingeringEncoder) {
187 struct {
188 void wait() {
189 std::unique_lock<std::mutex> lock(mMutex);
190 while (!mSignaled) {
191 mCondition.wait(lock);
192 }
193 }
194
195 void signal() {
196 std::unique_lock<std::mutex> lock(mMutex);
197 mSignaled = true;
198 mCondition.notify_all();
199 }
200
201 std::mutex mMutex;
202 std::condition_variable mCondition;
203 bool mSignaled = false;
204 } semaphore;
205
206 auto callback = std::make_shared<TestCallback>();
207 auto transcoder = VideoTrackTranscoder::create(callback);
208
209 EXPECT_EQ(transcoder->configure(mMediaSampleReader, mTrackIndex, mDestinationFormat),
210 AMEDIA_OK);
211 ASSERT_TRUE(transcoder->start());
212
213 std::shared_ptr<MediaSampleQueue> outputQueue = transcoder->getOutputQueue();
214 std::vector<std::shared_ptr<MediaSample>> samples;
215 std::thread sampleConsumerThread([&outputQueue, &samples, &semaphore] {
216 std::shared_ptr<MediaSample> sample;
217 while (samples.size() < 10 && !outputQueue->dequeue(&sample)) {
218 ASSERT_NE(sample, nullptr);
219 samples.push_back(sample);
220
221 if (sample->info.flags & SAMPLE_FLAG_END_OF_STREAM) {
222 break;
223 }
224 sample.reset();
225 }
226
227 semaphore.signal();
228 });
229
230 // Wait for the encoder to output samples before stopping and releasing the transcoder.
231 semaphore.wait();
232
233 EXPECT_TRUE(transcoder->stop());
234 transcoder.reset();
235 sampleConsumerThread.join();
236
237 // Return buffers to the codec so that it can resume processing, but keep one buffer to avoid
238 // the codec being released.
239 samples.resize(1);
240
241 // Wait for async codec events.
242 std::this_thread::sleep_for(std::chrono::seconds(1));
243}
244
Linus Nilsson0da327a2020-01-31 16:22:18 -0800245} // namespace android
246
247int main(int argc, char** argv) {
248 ::testing::InitGoogleTest(&argc, argv);
249 return RUN_ALL_TESTS();
250}