blob: 9c9c8b5152a8e5f38362b3a9640a862d06d4c586 [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 Nilssonc31d2492020-09-23 12:30:00 -070029
Linus Nilsson800793f2020-07-31 16:16:38 -070030#include <cmath>
Linus Nilsson6233fed2020-08-13 15:15:14 -070031#include <mutex>
32#include <thread>
Linus Nilsson800793f2020-07-31 16:16:38 -070033
Linus Nilsson478df7e2020-01-29 15:34:24 -080034// TODO(b/153453392): Test more asset types and validate sample data from readSampleDataForTrack.
Linus Nilsson6233fed2020-08-13 15:15:14 -070035// TODO(b/153453392): Test for sequential and parallel (single thread and multi thread) access.
36// TODO(b/153453392): Test for switching between sequential and parallel access in different points
37// of time.
Linus Nilsson478df7e2020-01-29 15:34:24 -080038
39namespace android {
40
41#define SEC_TO_USEC(s) ((s)*1000 * 1000)
42
43class MediaSampleReaderNDKTests : public ::testing::Test {
44public:
45 MediaSampleReaderNDKTests() { LOG(DEBUG) << "MediaSampleReaderNDKTests created"; }
46
47 void SetUp() override {
48 LOG(DEBUG) << "MediaSampleReaderNDKTests set up";
49 const char* sourcePath =
hkuang2ef2b432020-06-15 18:33:11 -070050 "/data/local/tmp/TranscodingTestAssets/cubicle_avc_480x240_aac_24KHz.mp4";
Linus Nilsson478df7e2020-01-29 15:34:24 -080051
52 mExtractor = AMediaExtractor_new();
53 ASSERT_NE(mExtractor, nullptr);
54
55 mSourceFd = open(sourcePath, O_RDONLY);
56 ASSERT_GT(mSourceFd, 0);
57
58 mFileSize = lseek(mSourceFd, 0, SEEK_END);
59 lseek(mSourceFd, 0, SEEK_SET);
60
61 media_status_t status =
62 AMediaExtractor_setDataSourceFd(mExtractor, mSourceFd, 0, mFileSize);
63 ASSERT_EQ(status, AMEDIA_OK);
64
65 mTrackCount = AMediaExtractor_getTrackCount(mExtractor);
66 for (size_t trackIndex = 0; trackIndex < mTrackCount; trackIndex++) {
67 AMediaExtractor_selectTrack(mExtractor, trackIndex);
68 }
69 }
70
71 void initExtractorTimestamps() {
72 // Save all sample timestamps, per track, as reported by the extractor.
73 mExtractorTimestamps.resize(mTrackCount);
74 do {
75 const int trackIndex = AMediaExtractor_getSampleTrackIndex(mExtractor);
76 const int64_t sampleTime = AMediaExtractor_getSampleTime(mExtractor);
77
78 mExtractorTimestamps[trackIndex].push_back(sampleTime);
79 } while (AMediaExtractor_advance(mExtractor));
Linus Nilsson800793f2020-07-31 16:16:38 -070080
81 AMediaExtractor_seekTo(mExtractor, 0, AMEDIAEXTRACTOR_SEEK_PREVIOUS_SYNC);
82 }
83
84 std::vector<int32_t> getTrackBitrates() {
85 size_t totalSize[mTrackCount];
86 memset(totalSize, 0, sizeof(totalSize));
87
88 do {
89 const int trackIndex = AMediaExtractor_getSampleTrackIndex(mExtractor);
90 totalSize[trackIndex] += AMediaExtractor_getSampleSize(mExtractor);
91 } while (AMediaExtractor_advance(mExtractor));
92
93 AMediaExtractor_seekTo(mExtractor, 0, AMEDIAEXTRACTOR_SEEK_PREVIOUS_SYNC);
94
95 std::vector<int32_t> bitrates;
96 for (int trackIndex = 0; trackIndex < mTrackCount; trackIndex++) {
97 int64_t durationUs;
98 AMediaFormat* trackFormat = AMediaExtractor_getTrackFormat(mExtractor, trackIndex);
99 EXPECT_NE(trackFormat, nullptr);
100 EXPECT_TRUE(AMediaFormat_getInt64(trackFormat, AMEDIAFORMAT_KEY_DURATION, &durationUs));
101 bitrates.push_back(roundf((float)totalSize[trackIndex] * 8 * 1000000 / durationUs));
102 }
103
104 return bitrates;
Linus Nilsson478df7e2020-01-29 15:34:24 -0800105 }
106
107 void TearDown() override {
108 LOG(DEBUG) << "MediaSampleReaderNDKTests tear down";
109 AMediaExtractor_delete(mExtractor);
110 close(mSourceFd);
111 }
112
113 ~MediaSampleReaderNDKTests() { LOG(DEBUG) << "MediaSampleReaderNDKTests destroyed"; }
114
115 AMediaExtractor* mExtractor = nullptr;
116 size_t mTrackCount;
117 int mSourceFd;
118 size_t mFileSize;
119 std::vector<std::vector<int64_t>> mExtractorTimestamps;
120};
121
122TEST_F(MediaSampleReaderNDKTests, TestSampleTimes) {
123 LOG(DEBUG) << "TestSampleTimes Starts";
124
125 std::shared_ptr<MediaSampleReader> sampleReader =
126 MediaSampleReaderNDK::createFromFd(mSourceFd, 0, mFileSize);
127 ASSERT_TRUE(sampleReader);
128
Linus Nilsson6233fed2020-08-13 15:15:14 -0700129 for (int trackIndex = 0; trackIndex < mTrackCount; trackIndex++) {
130 EXPECT_EQ(sampleReader->selectTrack(trackIndex), AMEDIA_OK);
131 }
Linus Nilsson478df7e2020-01-29 15:34:24 -0800132
133 // Initialize the extractor timestamps.
134 initExtractorTimestamps();
135
Linus Nilsson6233fed2020-08-13 15:15:14 -0700136 std::mutex timestampMutex;
137 std::vector<std::thread> trackThreads;
138 std::vector<std::vector<int64_t>> readerTimestamps(mTrackCount);
Linus Nilsson478df7e2020-01-29 15:34:24 -0800139
Linus Nilsson6233fed2020-08-13 15:15:14 -0700140 for (int trackIndex = 0; trackIndex < mTrackCount; trackIndex++) {
141 trackThreads.emplace_back([sampleReader, trackIndex, &timestampMutex, &readerTimestamps] {
142 MediaSampleInfo info;
143 while (true) {
Linus Nilsson478df7e2020-01-29 15:34:24 -0800144 media_status_t status = sampleReader->getSampleInfoForTrack(trackIndex, &info);
145 if (status != AMEDIA_OK) {
Linus Nilsson6233fed2020-08-13 15:15:14 -0700146 EXPECT_EQ(status, AMEDIA_ERROR_END_OF_STREAM);
147 EXPECT_TRUE((info.flags & SAMPLE_FLAG_END_OF_STREAM) != 0);
Linus Nilsson478df7e2020-01-29 15:34:24 -0800148 break;
149 }
150 ASSERT_TRUE((info.flags & SAMPLE_FLAG_END_OF_STREAM) == 0);
Linus Nilsson6233fed2020-08-13 15:15:14 -0700151 timestampMutex.lock();
Linus Nilsson478df7e2020-01-29 15:34:24 -0800152 readerTimestamps[trackIndex].push_back(info.presentationTimeUs);
Linus Nilsson6233fed2020-08-13 15:15:14 -0700153 timestampMutex.unlock();
Linus Nilsson478df7e2020-01-29 15:34:24 -0800154 sampleReader->advanceTrack(trackIndex);
Linus Nilsson6233fed2020-08-13 15:15:14 -0700155 }
156 });
157 }
158
159 for (auto& thread : trackThreads) {
160 thread.join();
Linus Nilsson478df7e2020-01-29 15:34:24 -0800161 }
162
163 for (int trackIndex = 0; trackIndex < mTrackCount; trackIndex++) {
164 LOG(DEBUG) << "Track " << trackIndex << ", comparing "
165 << readerTimestamps[trackIndex].size() << " samples.";
Linus Nilsson6233fed2020-08-13 15:15:14 -0700166 EXPECT_EQ(readerTimestamps[trackIndex].size(), mExtractorTimestamps[trackIndex].size());
Linus Nilsson478df7e2020-01-29 15:34:24 -0800167 for (size_t sampleIndex = 0; sampleIndex < readerTimestamps[trackIndex].size();
168 sampleIndex++) {
Linus Nilsson6233fed2020-08-13 15:15:14 -0700169 EXPECT_EQ(readerTimestamps[trackIndex][sampleIndex],
Linus Nilsson478df7e2020-01-29 15:34:24 -0800170 mExtractorTimestamps[trackIndex][sampleIndex]);
171 }
172 }
173}
174
Linus Nilsson800793f2020-07-31 16:16:38 -0700175TEST_F(MediaSampleReaderNDKTests, TestEstimatedBitrateAccuracy) {
176 // Just put a somewhat reasonable upper bound on the estimated bitrate expected in our test
177 // assets. This is mostly to make sure the estimation is not way off.
178 static constexpr int32_t kMaxEstimatedBitrate = 100 * 1000 * 1000; // 100 Mbps
179
180 auto sampleReader = MediaSampleReaderNDK::createFromFd(mSourceFd, 0, mFileSize);
181 ASSERT_TRUE(sampleReader);
182
183 std::vector<int32_t> actualTrackBitrates = getTrackBitrates();
184 for (int trackIndex = 0; trackIndex < mTrackCount; ++trackIndex) {
Linus Nilsson6233fed2020-08-13 15:15:14 -0700185 EXPECT_EQ(sampleReader->selectTrack(trackIndex), AMEDIA_OK);
186
Linus Nilsson800793f2020-07-31 16:16:38 -0700187 int32_t bitrate;
188 EXPECT_EQ(sampleReader->getEstimatedBitrateForTrack(trackIndex, &bitrate), AMEDIA_OK);
189 EXPECT_GT(bitrate, 0);
190 EXPECT_LT(bitrate, kMaxEstimatedBitrate);
191
192 // Note: The test asset currently used in this test is shorter than the sampling duration
193 // used to estimate the bitrate in the sample reader. So for now the estimation should be
194 // exact but if/when a longer asset is used a reasonable delta needs to be defined.
195 EXPECT_EQ(bitrate, actualTrackBitrates[trackIndex]);
196 }
197}
198
Linus Nilsson478df7e2020-01-29 15:34:24 -0800199TEST_F(MediaSampleReaderNDKTests, TestInvalidFd) {
200 std::shared_ptr<MediaSampleReader> sampleReader =
201 MediaSampleReaderNDK::createFromFd(0, 0, mFileSize);
202 ASSERT_TRUE(sampleReader == nullptr);
203
204 sampleReader = MediaSampleReaderNDK::createFromFd(-1, 0, mFileSize);
205 ASSERT_TRUE(sampleReader == nullptr);
206}
207
208TEST_F(MediaSampleReaderNDKTests, TestZeroSize) {
209 std::shared_ptr<MediaSampleReader> sampleReader =
210 MediaSampleReaderNDK::createFromFd(mSourceFd, 0, 0);
211 ASSERT_TRUE(sampleReader == nullptr);
212}
213
214TEST_F(MediaSampleReaderNDKTests, TestInvalidOffset) {
215 std::shared_ptr<MediaSampleReader> sampleReader =
216 MediaSampleReaderNDK::createFromFd(mSourceFd, mFileSize, mFileSize);
217 ASSERT_TRUE(sampleReader == nullptr);
218}
219
220} // namespace android
221
222int main(int argc, char** argv) {
223 ::testing::InitGoogleTest(&argc, argv);
224 return RUN_ALL_TESTS();
225}