blob: 6b1f64022bb98d38e8d24d501ea240cf259b784d [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>
26#include <media/VideoTrackTranscoder.h>
27#include <utils/Timers.h>
28
29#include "TrackTranscoderTestUtils.h"
30
31namespace 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
38class VideoTrackTranscoderTests : public ::testing::Test {
39public:
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
95TEST_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
Linus Nilssoncab39d82020-05-14 16:32:21 -0700103 std::shared_ptr<MediaSampleQueue> outputQueue = transcoder.getOutputQueue();
104 std::thread sampleConsumerThread{[&outputQueue] {
Linus Nilsson0da327a2020-01-31 16:22:18 -0800105 uint64_t sampleCount = 0;
106 std::shared_ptr<MediaSample> sample;
Linus Nilssoncab39d82020-05-14 16:32:21 -0700107 while (!outputQueue->dequeue(&sample)) {
Linus Nilsson0da327a2020-01-31 16:22:18 -0800108 ASSERT_NE(sample, nullptr);
109 const uint32_t flags = sample->info.flags;
110
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);
122 }
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 }
130
131 ++sampleCount;
132 if (sample->info.flags & SAMPLE_FLAG_END_OF_STREAM) {
133 break;
134 }
135 sample.reset();
136 }
137 }};
138
139 EXPECT_EQ(callback->waitUntilFinished(), AMEDIA_OK);
140 EXPECT_TRUE(transcoder.stop());
141
142 sampleConsumerThread.join();
143}
144
145// VideoTrackTranscoder needs a valid destination format.
146TEST_F(VideoTrackTranscoderTests, NullDestinationFormat) {
147 LOG(DEBUG) << "Testing NullDestinationFormat";
148 std::shared_ptr<TestCallback> callback = std::make_shared<TestCallback>();
149 std::shared_ptr<AMediaFormat> nullFormat;
150
151 VideoTrackTranscoder transcoder{callback};
152 EXPECT_EQ(transcoder.configure(mMediaSampleReader, 0 /* trackIndex */, nullFormat),
153 AMEDIA_ERROR_INVALID_PARAMETER);
154}
155
156} // namespace android
157
158int main(int argc, char** argv) {
159 ::testing::InitGoogleTest(&argc, argv);
160 return RUN_ALL_TESTS();
161}