blob: e8acd48530c2a28add51991dad1b7a78269512ab [file] [log] [blame]
Linus Nilsson478df7e2020-01-29 15:34:24 -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 MediaSampleReaderNDK
18
19// #define LOG_NDEBUG 0
20#define LOG_TAG "MediaSampleReaderNDKTests"
21
22#include <android-base/logging.h>
23#include <android/binder_manager.h>
24#include <android/binder_process.h>
25#include <fcntl.h>
26#include <gtest/gtest.h>
27#include <media/MediaSampleReaderNDK.h>
28#include <utils/Timers.h>
Linus Nilsson800793f2020-07-31 16:16:38 -070029#include <cmath>
Linus Nilsson6233fed2020-08-13 15:15:14 -070030#include <mutex>
31#include <thread>
Linus Nilsson800793f2020-07-31 16:16:38 -070032
Linus Nilsson478df7e2020-01-29 15:34:24 -080033// TODO(b/153453392): Test more asset types and validate sample data from readSampleDataForTrack.
Linus Nilsson6233fed2020-08-13 15:15:14 -070034// TODO(b/153453392): Test for sequential and parallel (single thread and multi thread) access.
35// TODO(b/153453392): Test for switching between sequential and parallel access in different points
36// of time.
Linus Nilsson478df7e2020-01-29 15:34:24 -080037
38namespace android {
39
40#define SEC_TO_USEC(s) ((s)*1000 * 1000)
41
42class MediaSampleReaderNDKTests : public ::testing::Test {
43public:
44 MediaSampleReaderNDKTests() { LOG(DEBUG) << "MediaSampleReaderNDKTests created"; }
45
46 void SetUp() override {
47 LOG(DEBUG) << "MediaSampleReaderNDKTests set up";
48 const char* sourcePath =
hkuang2ef2b432020-06-15 18:33:11 -070049 "/data/local/tmp/TranscodingTestAssets/cubicle_avc_480x240_aac_24KHz.mp4";
Linus Nilsson478df7e2020-01-29 15:34:24 -080050
51 mExtractor = AMediaExtractor_new();
52 ASSERT_NE(mExtractor, nullptr);
53
54 mSourceFd = open(sourcePath, O_RDONLY);
55 ASSERT_GT(mSourceFd, 0);
56
57 mFileSize = lseek(mSourceFd, 0, SEEK_END);
58 lseek(mSourceFd, 0, SEEK_SET);
59
60 media_status_t status =
61 AMediaExtractor_setDataSourceFd(mExtractor, mSourceFd, 0, mFileSize);
62 ASSERT_EQ(status, AMEDIA_OK);
63
64 mTrackCount = AMediaExtractor_getTrackCount(mExtractor);
65 for (size_t trackIndex = 0; trackIndex < mTrackCount; trackIndex++) {
66 AMediaExtractor_selectTrack(mExtractor, trackIndex);
67 }
68 }
69
70 void initExtractorTimestamps() {
71 // Save all sample timestamps, per track, as reported by the extractor.
72 mExtractorTimestamps.resize(mTrackCount);
73 do {
74 const int trackIndex = AMediaExtractor_getSampleTrackIndex(mExtractor);
75 const int64_t sampleTime = AMediaExtractor_getSampleTime(mExtractor);
76
77 mExtractorTimestamps[trackIndex].push_back(sampleTime);
78 } while (AMediaExtractor_advance(mExtractor));
Linus Nilsson800793f2020-07-31 16:16:38 -070079
80 AMediaExtractor_seekTo(mExtractor, 0, AMEDIAEXTRACTOR_SEEK_PREVIOUS_SYNC);
81 }
82
83 std::vector<int32_t> getTrackBitrates() {
84 size_t totalSize[mTrackCount];
85 memset(totalSize, 0, sizeof(totalSize));
86
87 do {
88 const int trackIndex = AMediaExtractor_getSampleTrackIndex(mExtractor);
89 totalSize[trackIndex] += AMediaExtractor_getSampleSize(mExtractor);
90 } while (AMediaExtractor_advance(mExtractor));
91
92 AMediaExtractor_seekTo(mExtractor, 0, AMEDIAEXTRACTOR_SEEK_PREVIOUS_SYNC);
93
94 std::vector<int32_t> bitrates;
95 for (int trackIndex = 0; trackIndex < mTrackCount; trackIndex++) {
96 int64_t durationUs;
97 AMediaFormat* trackFormat = AMediaExtractor_getTrackFormat(mExtractor, trackIndex);
98 EXPECT_NE(trackFormat, nullptr);
99 EXPECT_TRUE(AMediaFormat_getInt64(trackFormat, AMEDIAFORMAT_KEY_DURATION, &durationUs));
100 bitrates.push_back(roundf((float)totalSize[trackIndex] * 8 * 1000000 / durationUs));
101 }
102
103 return bitrates;
Linus Nilsson478df7e2020-01-29 15:34:24 -0800104 }
105
106 void TearDown() override {
107 LOG(DEBUG) << "MediaSampleReaderNDKTests tear down";
108 AMediaExtractor_delete(mExtractor);
109 close(mSourceFd);
110 }
111
112 ~MediaSampleReaderNDKTests() { LOG(DEBUG) << "MediaSampleReaderNDKTests destroyed"; }
113
114 AMediaExtractor* mExtractor = nullptr;
115 size_t mTrackCount;
116 int mSourceFd;
117 size_t mFileSize;
118 std::vector<std::vector<int64_t>> mExtractorTimestamps;
119};
120
121TEST_F(MediaSampleReaderNDKTests, TestSampleTimes) {
122 LOG(DEBUG) << "TestSampleTimes Starts";
123
124 std::shared_ptr<MediaSampleReader> sampleReader =
125 MediaSampleReaderNDK::createFromFd(mSourceFd, 0, mFileSize);
126 ASSERT_TRUE(sampleReader);
127
Linus Nilsson6233fed2020-08-13 15:15:14 -0700128 for (int trackIndex = 0; trackIndex < mTrackCount; trackIndex++) {
129 EXPECT_EQ(sampleReader->selectTrack(trackIndex), AMEDIA_OK);
130 }
Linus Nilsson478df7e2020-01-29 15:34:24 -0800131
132 // Initialize the extractor timestamps.
133 initExtractorTimestamps();
134
Linus Nilsson6233fed2020-08-13 15:15:14 -0700135 std::mutex timestampMutex;
136 std::vector<std::thread> trackThreads;
137 std::vector<std::vector<int64_t>> readerTimestamps(mTrackCount);
Linus Nilsson478df7e2020-01-29 15:34:24 -0800138
Linus Nilsson6233fed2020-08-13 15:15:14 -0700139 for (int trackIndex = 0; trackIndex < mTrackCount; trackIndex++) {
140 trackThreads.emplace_back([sampleReader, trackIndex, &timestampMutex, &readerTimestamps] {
141 MediaSampleInfo info;
142 while (true) {
Linus Nilsson478df7e2020-01-29 15:34:24 -0800143 media_status_t status = sampleReader->getSampleInfoForTrack(trackIndex, &info);
144 if (status != AMEDIA_OK) {
Linus Nilsson6233fed2020-08-13 15:15:14 -0700145 EXPECT_EQ(status, AMEDIA_ERROR_END_OF_STREAM);
146 EXPECT_TRUE((info.flags & SAMPLE_FLAG_END_OF_STREAM) != 0);
Linus Nilsson478df7e2020-01-29 15:34:24 -0800147 break;
148 }
149 ASSERT_TRUE((info.flags & SAMPLE_FLAG_END_OF_STREAM) == 0);
Linus Nilsson6233fed2020-08-13 15:15:14 -0700150 timestampMutex.lock();
Linus Nilsson478df7e2020-01-29 15:34:24 -0800151 readerTimestamps[trackIndex].push_back(info.presentationTimeUs);
Linus Nilsson6233fed2020-08-13 15:15:14 -0700152 timestampMutex.unlock();
Linus Nilsson478df7e2020-01-29 15:34:24 -0800153 sampleReader->advanceTrack(trackIndex);
Linus Nilsson6233fed2020-08-13 15:15:14 -0700154 }
155 });
156 }
157
158 for (auto& thread : trackThreads) {
159 thread.join();
Linus Nilsson478df7e2020-01-29 15:34:24 -0800160 }
161
162 for (int trackIndex = 0; trackIndex < mTrackCount; trackIndex++) {
163 LOG(DEBUG) << "Track " << trackIndex << ", comparing "
164 << readerTimestamps[trackIndex].size() << " samples.";
Linus Nilsson6233fed2020-08-13 15:15:14 -0700165 EXPECT_EQ(readerTimestamps[trackIndex].size(), mExtractorTimestamps[trackIndex].size());
Linus Nilsson478df7e2020-01-29 15:34:24 -0800166 for (size_t sampleIndex = 0; sampleIndex < readerTimestamps[trackIndex].size();
167 sampleIndex++) {
Linus Nilsson6233fed2020-08-13 15:15:14 -0700168 EXPECT_EQ(readerTimestamps[trackIndex][sampleIndex],
Linus Nilsson478df7e2020-01-29 15:34:24 -0800169 mExtractorTimestamps[trackIndex][sampleIndex]);
170 }
171 }
172}
173
Linus Nilsson800793f2020-07-31 16:16:38 -0700174TEST_F(MediaSampleReaderNDKTests, TestEstimatedBitrateAccuracy) {
175 // Just put a somewhat reasonable upper bound on the estimated bitrate expected in our test
176 // assets. This is mostly to make sure the estimation is not way off.
177 static constexpr int32_t kMaxEstimatedBitrate = 100 * 1000 * 1000; // 100 Mbps
178
179 auto sampleReader = MediaSampleReaderNDK::createFromFd(mSourceFd, 0, mFileSize);
180 ASSERT_TRUE(sampleReader);
181
182 std::vector<int32_t> actualTrackBitrates = getTrackBitrates();
183 for (int trackIndex = 0; trackIndex < mTrackCount; ++trackIndex) {
Linus Nilsson6233fed2020-08-13 15:15:14 -0700184 EXPECT_EQ(sampleReader->selectTrack(trackIndex), AMEDIA_OK);
185
Linus Nilsson800793f2020-07-31 16:16:38 -0700186 int32_t bitrate;
187 EXPECT_EQ(sampleReader->getEstimatedBitrateForTrack(trackIndex, &bitrate), AMEDIA_OK);
188 EXPECT_GT(bitrate, 0);
189 EXPECT_LT(bitrate, kMaxEstimatedBitrate);
190
191 // Note: The test asset currently used in this test is shorter than the sampling duration
192 // used to estimate the bitrate in the sample reader. So for now the estimation should be
193 // exact but if/when a longer asset is used a reasonable delta needs to be defined.
194 EXPECT_EQ(bitrate, actualTrackBitrates[trackIndex]);
195 }
196}
197
Linus Nilsson478df7e2020-01-29 15:34:24 -0800198TEST_F(MediaSampleReaderNDKTests, TestInvalidFd) {
199 std::shared_ptr<MediaSampleReader> sampleReader =
200 MediaSampleReaderNDK::createFromFd(0, 0, mFileSize);
201 ASSERT_TRUE(sampleReader == nullptr);
202
203 sampleReader = MediaSampleReaderNDK::createFromFd(-1, 0, mFileSize);
204 ASSERT_TRUE(sampleReader == nullptr);
205}
206
207TEST_F(MediaSampleReaderNDKTests, TestZeroSize) {
208 std::shared_ptr<MediaSampleReader> sampleReader =
209 MediaSampleReaderNDK::createFromFd(mSourceFd, 0, 0);
210 ASSERT_TRUE(sampleReader == nullptr);
211}
212
213TEST_F(MediaSampleReaderNDKTests, TestInvalidOffset) {
214 std::shared_ptr<MediaSampleReader> sampleReader =
215 MediaSampleReaderNDK::createFromFd(mSourceFd, mFileSize, mFileSize);
216 ASSERT_TRUE(sampleReader == nullptr);
217}
218
219} // namespace android
220
221int main(int argc, char** argv) {
222 ::testing::InitGoogleTest(&argc, argv);
223 return RUN_ALL_TESTS();
224}