blob: 4ede97f0362920b14c9b7149bfe9bb9274a539cf [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 Nilssonc31d2492020-09-23 12:30:00 -0700105 bool eos = false;
106 uint64_t sampleCount = 0;
107 transcoder->setSampleConsumer([&sampleCount, &eos](const std::shared_ptr<MediaSample>& sample) {
108 ASSERT_NE(sample, nullptr);
109 const uint32_t flags = sample->info.flags;
Linus Nilsson0da327a2020-01-31 16:22:18 -0800110
Linus Nilssonc31d2492020-09-23 12:30:00 -0700111 if (sampleCount == 0) {
112 // Expect first sample to be a codec config.
113 EXPECT_TRUE((flags & SAMPLE_FLAG_CODEC_CONFIG) != 0);
114 EXPECT_TRUE((flags & SAMPLE_FLAG_SYNC_SAMPLE) == 0);
115 EXPECT_TRUE((flags & SAMPLE_FLAG_END_OF_STREAM) == 0);
116 EXPECT_TRUE((flags & SAMPLE_FLAG_PARTIAL_FRAME) == 0);
117 } else if (sampleCount == 1) {
118 // Expect second sample to be a sync sample.
119 EXPECT_TRUE((flags & SAMPLE_FLAG_CODEC_CONFIG) == 0);
120 EXPECT_TRUE((flags & SAMPLE_FLAG_SYNC_SAMPLE) != 0);
121 EXPECT_TRUE((flags & SAMPLE_FLAG_END_OF_STREAM) == 0);
Linus Nilsson0da327a2020-01-31 16:22:18 -0800122 }
Linus Nilssonc31d2492020-09-23 12:30:00 -0700123
124 if (!(flags & SAMPLE_FLAG_END_OF_STREAM)) {
125 // Expect a valid buffer unless it is EOS.
126 EXPECT_NE(sample->buffer, nullptr);
127 EXPECT_NE(sample->bufferId, 0xBAADF00D);
128 EXPECT_GT(sample->info.size, 0);
129 } else {
130 EXPECT_FALSE(eos);
131 eos = true;
132 }
133
134 ++sampleCount;
135 });
Linus Nilsson0da327a2020-01-31 16:22:18 -0800136
137 EXPECT_EQ(callback->waitUntilFinished(), AMEDIA_OK);
Linus Nilsson0da327a2020-01-31 16:22:18 -0800138}
139
Linus Nilsson800793f2020-07-31 16:16:38 -0700140TEST_F(VideoTrackTranscoderTests, PreserveBitrate) {
141 LOG(DEBUG) << "Testing PreserveBitrate";
142 std::shared_ptr<TestCallback> callback = std::make_shared<TestCallback>();
143 std::shared_ptr<MediaTrackTranscoder> transcoder = VideoTrackTranscoder::create(callback);
144
145 auto destFormat = TrackTranscoderTestUtils::getDefaultVideoDestinationFormat(
146 mSourceFormat.get(), false /* includeBitrate*/);
147 EXPECT_NE(destFormat, nullptr);
148
Linus Nilsson6233fed2020-08-13 15:15:14 -0700149 EXPECT_EQ(mMediaSampleReader->selectTrack(mTrackIndex), AMEDIA_OK);
150
151 int32_t srcBitrate;
152 EXPECT_EQ(mMediaSampleReader->getEstimatedBitrateForTrack(mTrackIndex, &srcBitrate), AMEDIA_OK);
153
Linus Nilsson800793f2020-07-31 16:16:38 -0700154 ASSERT_EQ(transcoder->configure(mMediaSampleReader, mTrackIndex, destFormat), AMEDIA_OK);
155 ASSERT_TRUE(transcoder->start());
156
157 callback->waitUntilTrackFormatAvailable();
158
159 auto outputFormat = transcoder->getOutputFormat();
160 ASSERT_NE(outputFormat, nullptr);
161
Linus Nilssonfdb3e332020-09-18 17:11:41 -0700162 transcoder->stop();
163 EXPECT_EQ(callback->waitUntilFinished(), AMEDIA_OK);
Linus Nilsson800793f2020-07-31 16:16:38 -0700164
165 int32_t outBitrate;
166 EXPECT_TRUE(AMediaFormat_getInt32(outputFormat.get(), AMEDIAFORMAT_KEY_BIT_RATE, &outBitrate));
167
Linus Nilsson800793f2020-07-31 16:16:38 -0700168 EXPECT_EQ(srcBitrate, outBitrate);
169}
170
Linus Nilsson0da327a2020-01-31 16:22:18 -0800171// VideoTrackTranscoder needs a valid destination format.
172TEST_F(VideoTrackTranscoderTests, NullDestinationFormat) {
173 LOG(DEBUG) << "Testing NullDestinationFormat";
174 std::shared_ptr<TestCallback> callback = std::make_shared<TestCallback>();
175 std::shared_ptr<AMediaFormat> nullFormat;
176
Linus Nilssone4716f22020-07-10 16:07:57 -0700177 auto transcoder = VideoTrackTranscoder::create(callback);
178 EXPECT_EQ(transcoder->configure(mMediaSampleReader, 0 /* trackIndex */, nullFormat),
Linus Nilsson0da327a2020-01-31 16:22:18 -0800179 AMEDIA_ERROR_INVALID_PARAMETER);
180}
181
Linus Nilssone4716f22020-07-10 16:07:57 -0700182TEST_F(VideoTrackTranscoderTests, LingeringEncoder) {
Linus Nilssonc31d2492020-09-23 12:30:00 -0700183 OneShotSemaphore semaphore;
Linus Nilssone4716f22020-07-10 16:07:57 -0700184 auto callback = std::make_shared<TestCallback>();
185 auto transcoder = VideoTrackTranscoder::create(callback);
186
Linus Nilsson6233fed2020-08-13 15:15:14 -0700187 EXPECT_EQ(mMediaSampleReader->selectTrack(mTrackIndex), AMEDIA_OK);
Linus Nilssone4716f22020-07-10 16:07:57 -0700188 EXPECT_EQ(transcoder->configure(mMediaSampleReader, mTrackIndex, mDestinationFormat),
189 AMEDIA_OK);
190 ASSERT_TRUE(transcoder->start());
191
Linus Nilssone4716f22020-07-10 16:07:57 -0700192 std::vector<std::shared_ptr<MediaSample>> samples;
Linus Nilssonc31d2492020-09-23 12:30:00 -0700193 transcoder->setSampleConsumer(
194 [&samples, &semaphore](const std::shared_ptr<MediaSample>& sample) {
195 if (samples.size() >= 4) return;
Linus Nilssone4716f22020-07-10 16:07:57 -0700196
Linus Nilssonc31d2492020-09-23 12:30:00 -0700197 ASSERT_NE(sample, nullptr);
198 samples.push_back(sample);
Linus Nilssone4716f22020-07-10 16:07:57 -0700199
Linus Nilssonc31d2492020-09-23 12:30:00 -0700200 if (samples.size() == 4 || sample->info.flags & SAMPLE_FLAG_END_OF_STREAM) {
201 semaphore.signal();
202 }
203 });
Linus Nilssone4716f22020-07-10 16:07:57 -0700204
205 // Wait for the encoder to output samples before stopping and releasing the transcoder.
206 semaphore.wait();
207
Linus Nilssonfdb3e332020-09-18 17:11:41 -0700208 transcoder->stop();
209 EXPECT_EQ(callback->waitUntilFinished(), AMEDIA_OK);
Linus Nilssone4716f22020-07-10 16:07:57 -0700210 transcoder.reset();
Linus Nilssone4716f22020-07-10 16:07:57 -0700211
212 // Return buffers to the codec so that it can resume processing, but keep one buffer to avoid
213 // the codec being released.
214 samples.resize(1);
215
216 // Wait for async codec events.
217 std::this_thread::sleep_for(std::chrono::seconds(1));
218}
219
Linus Nilsson0da327a2020-01-31 16:22:18 -0800220} // namespace android
221
222int main(int argc, char** argv) {
223 ::testing::InitGoogleTest(&argc, argv);
224 return RUN_ALL_TESTS();
225}