Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 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> |
Linus Nilsson | 800793f | 2020-07-31 16:16:38 -0700 | [diff] [blame] | 26 | #include <media/NdkCommon.h> |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 27 | #include <media/VideoTrackTranscoder.h> |
| 28 | #include <utils/Timers.h> |
| 29 | |
| 30 | #include "TrackTranscoderTestUtils.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: |
| 41 | VideoTrackTranscoderTests() { LOG(DEBUG) << "VideoTrackTranscoderTests created"; } |
| 42 | |
| 43 | void SetUp() override { |
| 44 | LOG(DEBUG) << "VideoTrackTranscoderTests set up"; |
| 45 | const char* sourcePath = |
hkuang | 2ef2b43 | 2020-06-15 18:33:11 -0700 | [diff] [blame] | 46 | "/data/local/tmp/TranscodingTestAssets/cubicle_avc_480x240_aac_24KHz.mp4"; |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 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 | |
Linus Nilsson | 800793f | 2020-07-31 16:16:38 -0700 | [diff] [blame] | 70 | mSourceFormat = std::shared_ptr<AMediaFormat>(trackFormat, &AMediaFormat_delete); |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 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 | |
Linus Nilsson | 443f16c | 2020-07-30 13:13:21 -0700 | [diff] [blame] | 95 | TEST_F(VideoTrackTranscoderTests, SampleSoundness) { |
| 96 | LOG(DEBUG) << "Testing SampleSoundness"; |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 97 | std::shared_ptr<TestCallback> callback = std::make_shared<TestCallback>(); |
Linus Nilsson | e4716f2 | 2020-07-10 16:07:57 -0700 | [diff] [blame] | 98 | auto transcoder = VideoTrackTranscoder::create(callback); |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 99 | |
Linus Nilsson | 6233fed | 2020-08-13 15:15:14 -0700 | [diff] [blame] | 100 | EXPECT_EQ(mMediaSampleReader->selectTrack(mTrackIndex), AMEDIA_OK); |
Linus Nilsson | e4716f2 | 2020-07-10 16:07:57 -0700 | [diff] [blame] | 101 | EXPECT_EQ(transcoder->configure(mMediaSampleReader, mTrackIndex, mDestinationFormat), |
| 102 | AMEDIA_OK); |
| 103 | ASSERT_TRUE(transcoder->start()); |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 104 | |
Linus Nilsson | c31d249 | 2020-09-23 12:30:00 -0700 | [diff] [blame] | 105 | 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 Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 110 | |
Linus Nilsson | c31d249 | 2020-09-23 12:30:00 -0700 | [diff] [blame] | 111 | 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 Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 122 | } |
Linus Nilsson | c31d249 | 2020-09-23 12:30:00 -0700 | [diff] [blame] | 123 | |
| 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 Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 136 | |
| 137 | EXPECT_EQ(callback->waitUntilFinished(), AMEDIA_OK); |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 138 | } |
| 139 | |
Linus Nilsson | 800793f | 2020-07-31 16:16:38 -0700 | [diff] [blame] | 140 | TEST_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 Nilsson | 6233fed | 2020-08-13 15:15:14 -0700 | [diff] [blame] | 149 | EXPECT_EQ(mMediaSampleReader->selectTrack(mTrackIndex), AMEDIA_OK); |
| 150 | |
| 151 | int32_t srcBitrate; |
| 152 | EXPECT_EQ(mMediaSampleReader->getEstimatedBitrateForTrack(mTrackIndex, &srcBitrate), AMEDIA_OK); |
| 153 | |
Linus Nilsson | 800793f | 2020-07-31 16:16:38 -0700 | [diff] [blame] | 154 | 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 Nilsson | fdb3e33 | 2020-09-18 17:11:41 -0700 | [diff] [blame] | 162 | transcoder->stop(); |
| 163 | EXPECT_EQ(callback->waitUntilFinished(), AMEDIA_OK); |
Linus Nilsson | 800793f | 2020-07-31 16:16:38 -0700 | [diff] [blame] | 164 | |
| 165 | int32_t outBitrate; |
| 166 | EXPECT_TRUE(AMediaFormat_getInt32(outputFormat.get(), AMEDIAFORMAT_KEY_BIT_RATE, &outBitrate)); |
| 167 | |
Linus Nilsson | 800793f | 2020-07-31 16:16:38 -0700 | [diff] [blame] | 168 | EXPECT_EQ(srcBitrate, outBitrate); |
| 169 | } |
| 170 | |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 171 | // VideoTrackTranscoder needs a valid destination format. |
| 172 | TEST_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 Nilsson | e4716f2 | 2020-07-10 16:07:57 -0700 | [diff] [blame] | 177 | auto transcoder = VideoTrackTranscoder::create(callback); |
| 178 | EXPECT_EQ(transcoder->configure(mMediaSampleReader, 0 /* trackIndex */, nullFormat), |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 179 | AMEDIA_ERROR_INVALID_PARAMETER); |
| 180 | } |
| 181 | |
Linus Nilsson | e4716f2 | 2020-07-10 16:07:57 -0700 | [diff] [blame] | 182 | TEST_F(VideoTrackTranscoderTests, LingeringEncoder) { |
Linus Nilsson | c31d249 | 2020-09-23 12:30:00 -0700 | [diff] [blame] | 183 | OneShotSemaphore semaphore; |
Linus Nilsson | e4716f2 | 2020-07-10 16:07:57 -0700 | [diff] [blame] | 184 | auto callback = std::make_shared<TestCallback>(); |
| 185 | auto transcoder = VideoTrackTranscoder::create(callback); |
| 186 | |
Linus Nilsson | 6233fed | 2020-08-13 15:15:14 -0700 | [diff] [blame] | 187 | EXPECT_EQ(mMediaSampleReader->selectTrack(mTrackIndex), AMEDIA_OK); |
Linus Nilsson | e4716f2 | 2020-07-10 16:07:57 -0700 | [diff] [blame] | 188 | EXPECT_EQ(transcoder->configure(mMediaSampleReader, mTrackIndex, mDestinationFormat), |
| 189 | AMEDIA_OK); |
| 190 | ASSERT_TRUE(transcoder->start()); |
| 191 | |
Linus Nilsson | e4716f2 | 2020-07-10 16:07:57 -0700 | [diff] [blame] | 192 | std::vector<std::shared_ptr<MediaSample>> samples; |
Linus Nilsson | c31d249 | 2020-09-23 12:30:00 -0700 | [diff] [blame] | 193 | transcoder->setSampleConsumer( |
| 194 | [&samples, &semaphore](const std::shared_ptr<MediaSample>& sample) { |
| 195 | if (samples.size() >= 4) return; |
Linus Nilsson | e4716f2 | 2020-07-10 16:07:57 -0700 | [diff] [blame] | 196 | |
Linus Nilsson | c31d249 | 2020-09-23 12:30:00 -0700 | [diff] [blame] | 197 | ASSERT_NE(sample, nullptr); |
| 198 | samples.push_back(sample); |
Linus Nilsson | e4716f2 | 2020-07-10 16:07:57 -0700 | [diff] [blame] | 199 | |
Linus Nilsson | c31d249 | 2020-09-23 12:30:00 -0700 | [diff] [blame] | 200 | if (samples.size() == 4 || sample->info.flags & SAMPLE_FLAG_END_OF_STREAM) { |
| 201 | semaphore.signal(); |
| 202 | } |
| 203 | }); |
Linus Nilsson | e4716f2 | 2020-07-10 16:07:57 -0700 | [diff] [blame] | 204 | |
| 205 | // Wait for the encoder to output samples before stopping and releasing the transcoder. |
| 206 | semaphore.wait(); |
| 207 | |
Linus Nilsson | fdb3e33 | 2020-09-18 17:11:41 -0700 | [diff] [blame] | 208 | transcoder->stop(); |
| 209 | EXPECT_EQ(callback->waitUntilFinished(), AMEDIA_OK); |
Linus Nilsson | e4716f2 | 2020-07-10 16:07:57 -0700 | [diff] [blame] | 210 | transcoder.reset(); |
Linus Nilsson | e4716f2 | 2020-07-10 16:07:57 -0700 | [diff] [blame] | 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 | |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 220 | } // namespace android |
| 221 | |
| 222 | int main(int argc, char** argv) { |
| 223 | ::testing::InitGoogleTest(&argc, argv); |
| 224 | return RUN_ALL_TESTS(); |
| 225 | } |