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> |
| 26 | #include <media/VideoTrackTranscoder.h> |
| 27 | #include <utils/Timers.h> |
| 28 | |
| 29 | #include "TrackTranscoderTestUtils.h" |
| 30 | |
| 31 | namespace android { |
| 32 | |
| 33 | // TODO(b/155304421): Implement more advanced video specific tests: |
| 34 | // - Codec conversions (HEVC -> AVC). |
| 35 | // - Bitrate validation. |
| 36 | // - Output frame validation through PSNR. |
| 37 | |
| 38 | class VideoTrackTranscoderTests : public ::testing::Test { |
| 39 | public: |
| 40 | VideoTrackTranscoderTests() { LOG(DEBUG) << "VideoTrackTranscoderTests created"; } |
| 41 | |
| 42 | void SetUp() override { |
| 43 | LOG(DEBUG) << "VideoTrackTranscoderTests set up"; |
| 44 | const char* sourcePath = |
| 45 | "/data/local/tmp/TranscoderTestAssets/cubicle_avc_480x240_aac_24KHz.mp4"; |
| 46 | |
| 47 | const int sourceFd = open(sourcePath, O_RDONLY); |
| 48 | ASSERT_GT(sourceFd, 0); |
| 49 | |
| 50 | const off_t fileSize = lseek(sourceFd, 0, SEEK_END); |
| 51 | lseek(sourceFd, 0, SEEK_SET); |
| 52 | |
| 53 | mMediaSampleReader = MediaSampleReaderNDK::createFromFd(sourceFd, 0, fileSize); |
| 54 | ASSERT_NE(mMediaSampleReader, nullptr); |
| 55 | close(sourceFd); |
| 56 | |
| 57 | for (size_t trackIndex = 0; trackIndex < mMediaSampleReader->getTrackCount(); |
| 58 | ++trackIndex) { |
| 59 | AMediaFormat* trackFormat = mMediaSampleReader->getTrackFormat(trackIndex); |
| 60 | ASSERT_NE(trackFormat, nullptr); |
| 61 | |
| 62 | const char* mime = nullptr; |
| 63 | AMediaFormat_getString(trackFormat, AMEDIAFORMAT_KEY_MIME, &mime); |
| 64 | ASSERT_NE(mime, nullptr); |
| 65 | |
| 66 | if (strncmp(mime, "video/", 6) == 0) { |
| 67 | mTrackIndex = trackIndex; |
| 68 | |
| 69 | mSourceFormat = std::shared_ptr<AMediaFormat>( |
| 70 | trackFormat, std::bind(AMediaFormat_delete, std::placeholders::_1)); |
| 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 | |
| 95 | TEST_F(VideoTrackTranscoderTests, SampleSanity) { |
| 96 | LOG(DEBUG) << "Testing SampleSanity"; |
| 97 | std::shared_ptr<TestCallback> callback = std::make_shared<TestCallback>(); |
| 98 | VideoTrackTranscoder transcoder{callback}; |
| 99 | |
| 100 | EXPECT_EQ(transcoder.configure(mMediaSampleReader, mTrackIndex, mDestinationFormat), AMEDIA_OK); |
| 101 | ASSERT_TRUE(transcoder.start()); |
| 102 | |
| 103 | std::thread sampleConsumerThread{[&transcoder] { |
| 104 | uint64_t sampleCount = 0; |
| 105 | std::shared_ptr<MediaSample> sample; |
| 106 | while (!transcoder.mOutputQueue.dequeue(&sample)) { |
| 107 | ASSERT_NE(sample, nullptr); |
| 108 | const uint32_t flags = sample->info.flags; |
| 109 | |
| 110 | if (sampleCount == 0) { |
| 111 | // Expect first sample to be a codec config. |
| 112 | EXPECT_TRUE((flags & SAMPLE_FLAG_CODEC_CONFIG) != 0); |
| 113 | EXPECT_TRUE((flags & SAMPLE_FLAG_SYNC_SAMPLE) == 0); |
| 114 | EXPECT_TRUE((flags & SAMPLE_FLAG_END_OF_STREAM) == 0); |
| 115 | EXPECT_TRUE((flags & SAMPLE_FLAG_PARTIAL_FRAME) == 0); |
| 116 | } else if (sampleCount == 1) { |
| 117 | // Expect second sample to be a sync sample. |
| 118 | EXPECT_TRUE((flags & SAMPLE_FLAG_CODEC_CONFIG) == 0); |
| 119 | EXPECT_TRUE((flags & SAMPLE_FLAG_SYNC_SAMPLE) != 0); |
| 120 | EXPECT_TRUE((flags & SAMPLE_FLAG_END_OF_STREAM) == 0); |
| 121 | } |
| 122 | |
| 123 | if (!(flags & SAMPLE_FLAG_END_OF_STREAM)) { |
| 124 | // Expect a valid buffer unless it is EOS. |
| 125 | EXPECT_NE(sample->buffer, nullptr); |
| 126 | EXPECT_NE(sample->bufferId, 0xBAADF00D); |
| 127 | EXPECT_GT(sample->info.size, 0); |
| 128 | } |
| 129 | |
| 130 | ++sampleCount; |
| 131 | if (sample->info.flags & SAMPLE_FLAG_END_OF_STREAM) { |
| 132 | break; |
| 133 | } |
| 134 | sample.reset(); |
| 135 | } |
| 136 | }}; |
| 137 | |
| 138 | EXPECT_EQ(callback->waitUntilFinished(), AMEDIA_OK); |
| 139 | EXPECT_TRUE(transcoder.stop()); |
| 140 | |
| 141 | sampleConsumerThread.join(); |
| 142 | } |
| 143 | |
| 144 | // VideoTrackTranscoder needs a valid destination format. |
| 145 | TEST_F(VideoTrackTranscoderTests, NullDestinationFormat) { |
| 146 | LOG(DEBUG) << "Testing NullDestinationFormat"; |
| 147 | std::shared_ptr<TestCallback> callback = std::make_shared<TestCallback>(); |
| 148 | std::shared_ptr<AMediaFormat> nullFormat; |
| 149 | |
| 150 | VideoTrackTranscoder transcoder{callback}; |
| 151 | EXPECT_EQ(transcoder.configure(mMediaSampleReader, 0 /* trackIndex */, nullFormat), |
| 152 | AMEDIA_ERROR_INVALID_PARAMETER); |
| 153 | } |
| 154 | |
| 155 | } // namespace android |
| 156 | |
| 157 | int main(int argc, char** argv) { |
| 158 | ::testing::InitGoogleTest(&argc, argv); |
| 159 | return RUN_ALL_TESTS(); |
| 160 | } |