1 /*
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/NdkCommon.h>
27 #include <media/VideoTrackTranscoder.h>
28 #include <utils/Timers.h>
29
30 #include "TranscoderTestUtils.h"
31
32 namespace 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
39 class VideoTrackTranscoderTests : public ::testing::Test {
40 public:
VideoTrackTranscoderTests()41 VideoTrackTranscoderTests() { LOG(DEBUG) << "VideoTrackTranscoderTests created"; }
42
SetUp()43 void SetUp() override {
44 LOG(DEBUG) << "VideoTrackTranscoderTests set up";
45 const char* sourcePath =
46 "/data/local/tmp/TranscodingTestAssets/cubicle_avc_480x240_aac_24KHz.mp4";
47
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
70 mSourceFormat = std::shared_ptr<AMediaFormat>(trackFormat, &AMediaFormat_delete);
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
TearDown()85 void TearDown() override { LOG(DEBUG) << "VideoTrackTranscoderTests tear down"; }
86
~VideoTrackTranscoderTests()87 ~VideoTrackTranscoderTests() { LOG(DEBUG) << "VideoTrackTranscoderTests destroyed"; }
88
getConfiguredBitrate(const std::shared_ptr<VideoTrackTranscoder> & transcoder)89 static int32_t getConfiguredBitrate(const std::shared_ptr<VideoTrackTranscoder>& transcoder) {
90 return transcoder->mConfiguredBitrate;
91 }
92
93 std::shared_ptr<MediaSampleReader> mMediaSampleReader;
94 int mTrackIndex;
95 std::shared_ptr<AMediaFormat> mSourceFormat;
96 std::shared_ptr<AMediaFormat> mDestinationFormat;
97 };
98
TEST_F(VideoTrackTranscoderTests,SampleSoundness)99 TEST_F(VideoTrackTranscoderTests, SampleSoundness) {
100 LOG(DEBUG) << "Testing SampleSoundness";
101 auto callback = std::make_shared<TestTrackTranscoderCallback>();
102 auto transcoder = VideoTrackTranscoder::create(callback);
103
104 EXPECT_EQ(mMediaSampleReader->selectTrack(mTrackIndex), AMEDIA_OK);
105 EXPECT_EQ(transcoder->configure(mMediaSampleReader, mTrackIndex, mDestinationFormat),
106 AMEDIA_OK);
107 ASSERT_TRUE(transcoder->start());
108
109 bool eos = false;
110 uint64_t sampleCount = 0;
111 transcoder->setSampleConsumer([&sampleCount, &eos](const std::shared_ptr<MediaSample>& sample) {
112 ASSERT_NE(sample, nullptr);
113 const uint32_t flags = sample->info.flags;
114
115 if (sampleCount == 0) {
116 // Expect first sample to be a codec config.
117 EXPECT_TRUE((flags & SAMPLE_FLAG_CODEC_CONFIG) != 0);
118 EXPECT_TRUE((flags & SAMPLE_FLAG_SYNC_SAMPLE) == 0);
119 EXPECT_TRUE((flags & SAMPLE_FLAG_END_OF_STREAM) == 0);
120 EXPECT_TRUE((flags & SAMPLE_FLAG_PARTIAL_FRAME) == 0);
121 } else if (sampleCount == 1) {
122 // Expect second sample to be a sync sample.
123 EXPECT_TRUE((flags & SAMPLE_FLAG_CODEC_CONFIG) == 0);
124 EXPECT_TRUE((flags & SAMPLE_FLAG_SYNC_SAMPLE) != 0);
125 EXPECT_TRUE((flags & SAMPLE_FLAG_END_OF_STREAM) == 0);
126 }
127
128 if (!(flags & SAMPLE_FLAG_END_OF_STREAM)) {
129 // Expect a valid buffer unless it is EOS.
130 EXPECT_NE(sample->buffer, nullptr);
131 EXPECT_NE(sample->bufferId, 0xBAADF00D);
132 EXPECT_GT(sample->info.size, 0);
133 } else {
134 EXPECT_FALSE(eos);
135 eos = true;
136 }
137
138 ++sampleCount;
139 });
140
141 EXPECT_EQ(callback->waitUntilFinished(), AMEDIA_OK);
142 }
143
TEST_F(VideoTrackTranscoderTests,PreserveBitrate)144 TEST_F(VideoTrackTranscoderTests, PreserveBitrate) {
145 LOG(DEBUG) << "Testing PreserveBitrate";
146 auto callback = std::make_shared<TestTrackTranscoderCallback>();
147 auto transcoder = VideoTrackTranscoder::create(callback);
148
149 auto destFormat = TrackTranscoderTestUtils::getDefaultVideoDestinationFormat(
150 mSourceFormat.get(), false /* includeBitrate*/);
151 EXPECT_NE(destFormat, nullptr);
152
153 EXPECT_EQ(mMediaSampleReader->selectTrack(mTrackIndex), AMEDIA_OK);
154
155 int32_t srcBitrate;
156 EXPECT_EQ(mMediaSampleReader->getEstimatedBitrateForTrack(mTrackIndex, &srcBitrate), AMEDIA_OK);
157
158 ASSERT_EQ(transcoder->configure(mMediaSampleReader, mTrackIndex, destFormat), AMEDIA_OK);
159 ASSERT_TRUE(transcoder->start());
160
161 callback->waitUntilTrackFormatAvailable();
162 transcoder->stop();
163 EXPECT_EQ(callback->waitUntilFinished(), AMEDIA_OK);
164
165 int32_t outBitrate = getConfiguredBitrate(transcoder);
166 ASSERT_GT(outBitrate, 0);
167
168 EXPECT_EQ(srcBitrate, outBitrate);
169 }
170
171 // VideoTrackTranscoder needs a valid destination format.
TEST_F(VideoTrackTranscoderTests,NullDestinationFormat)172 TEST_F(VideoTrackTranscoderTests, NullDestinationFormat) {
173 LOG(DEBUG) << "Testing NullDestinationFormat";
174 auto callback = std::make_shared<TestTrackTranscoderCallback>();
175 std::shared_ptr<AMediaFormat> nullFormat;
176
177 auto transcoder = VideoTrackTranscoder::create(callback);
178 EXPECT_EQ(transcoder->configure(mMediaSampleReader, 0 /* trackIndex */, nullFormat),
179 AMEDIA_ERROR_INVALID_PARAMETER);
180 }
181
TEST_F(VideoTrackTranscoderTests,LingeringEncoder)182 TEST_F(VideoTrackTranscoderTests, LingeringEncoder) {
183 OneShotSemaphore semaphore;
184 auto callback = std::make_shared<TestTrackTranscoderCallback>();
185 auto transcoder = VideoTrackTranscoder::create(callback);
186
187 EXPECT_EQ(mMediaSampleReader->selectTrack(mTrackIndex), AMEDIA_OK);
188 EXPECT_EQ(transcoder->configure(mMediaSampleReader, mTrackIndex, mDestinationFormat),
189 AMEDIA_OK);
190 ASSERT_TRUE(transcoder->start());
191
192 std::vector<std::shared_ptr<MediaSample>> samples;
193 transcoder->setSampleConsumer(
194 [&samples, &semaphore](const std::shared_ptr<MediaSample>& sample) {
195 if (samples.size() >= 4) return;
196
197 ASSERT_NE(sample, nullptr);
198 samples.push_back(sample);
199
200 if (samples.size() == 4 || sample->info.flags & SAMPLE_FLAG_END_OF_STREAM) {
201 semaphore.signal();
202 }
203 });
204
205 // Wait for the encoder to output samples before stopping and releasing the transcoder.
206 semaphore.wait();
207
208 transcoder->stop();
209 EXPECT_EQ(callback->waitUntilFinished(), AMEDIA_OK);
210 transcoder.reset();
211
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
220 } // namespace android
221
main(int argc,char ** argv)222 int main(int argc, char** argv) {
223 ::testing::InitGoogleTest(&argc, argv);
224 return RUN_ALL_TESTS();
225 }
226