blob: e809cbd2a01a060a8b4abc689d4a39b4ef1ea61d [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 Nilsson6233fed2020-08-13 15:15:14 -0700100 EXPECT_EQ(mMediaSampleReader->selectTrack(mTrackIndex), AMEDIA_OK);
Linus Nilssone4716f22020-07-10 16:07:57 -0700101 EXPECT_EQ(transcoder->configure(mMediaSampleReader, mTrackIndex, mDestinationFormat),
102 AMEDIA_OK);
103 ASSERT_TRUE(transcoder->start());
Linus Nilsson0da327a2020-01-31 16:22:18 -0800104
Linus Nilssone4716f22020-07-10 16:07:57 -0700105 std::shared_ptr<MediaSampleQueue> outputQueue = transcoder->getOutputQueue();
Linus Nilssoncab39d82020-05-14 16:32:21 -0700106 std::thread sampleConsumerThread{[&outputQueue] {
Linus Nilsson0da327a2020-01-31 16:22:18 -0800107 uint64_t sampleCount = 0;
108 std::shared_ptr<MediaSample> sample;
Linus Nilssoncab39d82020-05-14 16:32:21 -0700109 while (!outputQueue->dequeue(&sample)) {
Linus Nilsson0da327a2020-01-31 16:22:18 -0800110 ASSERT_NE(sample, nullptr);
111 const uint32_t flags = sample->info.flags;
112
113 if (sampleCount == 0) {
114 // Expect first sample to be a codec config.
115 EXPECT_TRUE((flags & SAMPLE_FLAG_CODEC_CONFIG) != 0);
116 EXPECT_TRUE((flags & SAMPLE_FLAG_SYNC_SAMPLE) == 0);
117 EXPECT_TRUE((flags & SAMPLE_FLAG_END_OF_STREAM) == 0);
118 EXPECT_TRUE((flags & SAMPLE_FLAG_PARTIAL_FRAME) == 0);
119 } else if (sampleCount == 1) {
120 // Expect second sample to be a sync sample.
121 EXPECT_TRUE((flags & SAMPLE_FLAG_CODEC_CONFIG) == 0);
122 EXPECT_TRUE((flags & SAMPLE_FLAG_SYNC_SAMPLE) != 0);
123 EXPECT_TRUE((flags & SAMPLE_FLAG_END_OF_STREAM) == 0);
124 }
125
126 if (!(flags & SAMPLE_FLAG_END_OF_STREAM)) {
127 // Expect a valid buffer unless it is EOS.
128 EXPECT_NE(sample->buffer, nullptr);
129 EXPECT_NE(sample->bufferId, 0xBAADF00D);
130 EXPECT_GT(sample->info.size, 0);
131 }
132
133 ++sampleCount;
134 if (sample->info.flags & SAMPLE_FLAG_END_OF_STREAM) {
135 break;
136 }
137 sample.reset();
138 }
139 }};
140
141 EXPECT_EQ(callback->waitUntilFinished(), AMEDIA_OK);
Linus Nilssone4716f22020-07-10 16:07:57 -0700142 EXPECT_TRUE(transcoder->stop());
Linus Nilsson0da327a2020-01-31 16:22:18 -0800143
144 sampleConsumerThread.join();
145}
146
Linus Nilsson800793f2020-07-31 16:16:38 -0700147TEST_F(VideoTrackTranscoderTests, PreserveBitrate) {
148 LOG(DEBUG) << "Testing PreserveBitrate";
149 std::shared_ptr<TestCallback> callback = std::make_shared<TestCallback>();
150 std::shared_ptr<MediaTrackTranscoder> transcoder = VideoTrackTranscoder::create(callback);
151
152 auto destFormat = TrackTranscoderTestUtils::getDefaultVideoDestinationFormat(
153 mSourceFormat.get(), false /* includeBitrate*/);
154 EXPECT_NE(destFormat, nullptr);
155
Linus Nilsson6233fed2020-08-13 15:15:14 -0700156 EXPECT_EQ(mMediaSampleReader->selectTrack(mTrackIndex), AMEDIA_OK);
157
158 int32_t srcBitrate;
159 EXPECT_EQ(mMediaSampleReader->getEstimatedBitrateForTrack(mTrackIndex, &srcBitrate), AMEDIA_OK);
160
Linus Nilsson800793f2020-07-31 16:16:38 -0700161 ASSERT_EQ(transcoder->configure(mMediaSampleReader, mTrackIndex, destFormat), AMEDIA_OK);
162 ASSERT_TRUE(transcoder->start());
163
164 callback->waitUntilTrackFormatAvailable();
165
166 auto outputFormat = transcoder->getOutputFormat();
167 ASSERT_NE(outputFormat, nullptr);
168
169 ASSERT_TRUE(transcoder->stop());
170 transcoder->getOutputQueue()->abort();
171
172 int32_t outBitrate;
173 EXPECT_TRUE(AMediaFormat_getInt32(outputFormat.get(), AMEDIAFORMAT_KEY_BIT_RATE, &outBitrate));
174
Linus Nilsson800793f2020-07-31 16:16:38 -0700175 EXPECT_EQ(srcBitrate, outBitrate);
176}
177
Linus Nilsson0da327a2020-01-31 16:22:18 -0800178// VideoTrackTranscoder needs a valid destination format.
179TEST_F(VideoTrackTranscoderTests, NullDestinationFormat) {
180 LOG(DEBUG) << "Testing NullDestinationFormat";
181 std::shared_ptr<TestCallback> callback = std::make_shared<TestCallback>();
182 std::shared_ptr<AMediaFormat> nullFormat;
183
Linus Nilssone4716f22020-07-10 16:07:57 -0700184 auto transcoder = VideoTrackTranscoder::create(callback);
185 EXPECT_EQ(transcoder->configure(mMediaSampleReader, 0 /* trackIndex */, nullFormat),
Linus Nilsson0da327a2020-01-31 16:22:18 -0800186 AMEDIA_ERROR_INVALID_PARAMETER);
187}
188
Linus Nilssone4716f22020-07-10 16:07:57 -0700189TEST_F(VideoTrackTranscoderTests, LingeringEncoder) {
190 struct {
191 void wait() {
192 std::unique_lock<std::mutex> lock(mMutex);
193 while (!mSignaled) {
194 mCondition.wait(lock);
195 }
196 }
197
198 void signal() {
199 std::unique_lock<std::mutex> lock(mMutex);
200 mSignaled = true;
201 mCondition.notify_all();
202 }
203
204 std::mutex mMutex;
205 std::condition_variable mCondition;
206 bool mSignaled = false;
207 } semaphore;
208
209 auto callback = std::make_shared<TestCallback>();
210 auto transcoder = VideoTrackTranscoder::create(callback);
211
Linus Nilsson6233fed2020-08-13 15:15:14 -0700212 EXPECT_EQ(mMediaSampleReader->selectTrack(mTrackIndex), AMEDIA_OK);
Linus Nilssone4716f22020-07-10 16:07:57 -0700213 EXPECT_EQ(transcoder->configure(mMediaSampleReader, mTrackIndex, mDestinationFormat),
214 AMEDIA_OK);
215 ASSERT_TRUE(transcoder->start());
216
217 std::shared_ptr<MediaSampleQueue> outputQueue = transcoder->getOutputQueue();
218 std::vector<std::shared_ptr<MediaSample>> samples;
219 std::thread sampleConsumerThread([&outputQueue, &samples, &semaphore] {
220 std::shared_ptr<MediaSample> sample;
Linus Nilsson6233fed2020-08-13 15:15:14 -0700221 while (samples.size() < 4 && !outputQueue->dequeue(&sample)) {
Linus Nilssone4716f22020-07-10 16:07:57 -0700222 ASSERT_NE(sample, nullptr);
223 samples.push_back(sample);
224
225 if (sample->info.flags & SAMPLE_FLAG_END_OF_STREAM) {
226 break;
227 }
228 sample.reset();
229 }
230
231 semaphore.signal();
232 });
233
234 // Wait for the encoder to output samples before stopping and releasing the transcoder.
235 semaphore.wait();
236
237 EXPECT_TRUE(transcoder->stop());
238 transcoder.reset();
239 sampleConsumerThread.join();
240
241 // Return buffers to the codec so that it can resume processing, but keep one buffer to avoid
242 // the codec being released.
243 samples.resize(1);
244
245 // Wait for async codec events.
246 std::this_thread::sleep_for(std::chrono::seconds(1));
247}
248
Linus Nilsson0da327a2020-01-31 16:22:18 -0800249} // namespace android
250
251int main(int argc, char** argv) {
252 ::testing::InitGoogleTest(&argc, argv);
253 return RUN_ALL_TESTS();
254}