Linus Nilsson | 478df7e | 2020-01-29 15:34:24 -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 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 Nilsson | c31d249 | 2020-09-23 12:30:00 -0700 | [diff] [blame] | 29 | |
Linus Nilsson | 800793f | 2020-07-31 16:16:38 -0700 | [diff] [blame] | 30 | #include <cmath> |
Linus Nilsson | 6233fed | 2020-08-13 15:15:14 -0700 | [diff] [blame] | 31 | #include <mutex> |
| 32 | #include <thread> |
Linus Nilsson | 800793f | 2020-07-31 16:16:38 -0700 | [diff] [blame] | 33 | |
Linus Nilsson | 478df7e | 2020-01-29 15:34:24 -0800 | [diff] [blame] | 34 | // TODO(b/153453392): Test more asset types and validate sample data from readSampleDataForTrack. |
Linus Nilsson | 6233fed | 2020-08-13 15:15:14 -0700 | [diff] [blame] | 35 | // 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 Nilsson | 478df7e | 2020-01-29 15:34:24 -0800 | [diff] [blame] | 38 | |
| 39 | namespace android { |
| 40 | |
| 41 | #define SEC_TO_USEC(s) ((s)*1000 * 1000) |
| 42 | |
| 43 | class MediaSampleReaderNDKTests : public ::testing::Test { |
| 44 | public: |
| 45 | MediaSampleReaderNDKTests() { LOG(DEBUG) << "MediaSampleReaderNDKTests created"; } |
| 46 | |
| 47 | void SetUp() override { |
| 48 | LOG(DEBUG) << "MediaSampleReaderNDKTests set up"; |
| 49 | const char* sourcePath = |
hkuang | 2ef2b43 | 2020-06-15 18:33:11 -0700 | [diff] [blame] | 50 | "/data/local/tmp/TranscodingTestAssets/cubicle_avc_480x240_aac_24KHz.mp4"; |
Linus Nilsson | 478df7e | 2020-01-29 15:34:24 -0800 | [diff] [blame] | 51 | |
| 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 Nilsson | 800793f | 2020-07-31 16:16:38 -0700 | [diff] [blame] | 80 | |
| 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 Nilsson | 478df7e | 2020-01-29 15:34:24 -0800 | [diff] [blame] | 105 | } |
| 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 | |
| 122 | TEST_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 Nilsson | 6233fed | 2020-08-13 15:15:14 -0700 | [diff] [blame] | 129 | for (int trackIndex = 0; trackIndex < mTrackCount; trackIndex++) { |
| 130 | EXPECT_EQ(sampleReader->selectTrack(trackIndex), AMEDIA_OK); |
| 131 | } |
Linus Nilsson | 478df7e | 2020-01-29 15:34:24 -0800 | [diff] [blame] | 132 | |
| 133 | // Initialize the extractor timestamps. |
| 134 | initExtractorTimestamps(); |
| 135 | |
Linus Nilsson | 6233fed | 2020-08-13 15:15:14 -0700 | [diff] [blame] | 136 | std::mutex timestampMutex; |
| 137 | std::vector<std::thread> trackThreads; |
| 138 | std::vector<std::vector<int64_t>> readerTimestamps(mTrackCount); |
Linus Nilsson | 478df7e | 2020-01-29 15:34:24 -0800 | [diff] [blame] | 139 | |
Linus Nilsson | 6233fed | 2020-08-13 15:15:14 -0700 | [diff] [blame] | 140 | for (int trackIndex = 0; trackIndex < mTrackCount; trackIndex++) { |
| 141 | trackThreads.emplace_back([sampleReader, trackIndex, ×tampMutex, &readerTimestamps] { |
| 142 | MediaSampleInfo info; |
| 143 | while (true) { |
Linus Nilsson | 478df7e | 2020-01-29 15:34:24 -0800 | [diff] [blame] | 144 | media_status_t status = sampleReader->getSampleInfoForTrack(trackIndex, &info); |
| 145 | if (status != AMEDIA_OK) { |
Linus Nilsson | 6233fed | 2020-08-13 15:15:14 -0700 | [diff] [blame] | 146 | EXPECT_EQ(status, AMEDIA_ERROR_END_OF_STREAM); |
| 147 | EXPECT_TRUE((info.flags & SAMPLE_FLAG_END_OF_STREAM) != 0); |
Linus Nilsson | 478df7e | 2020-01-29 15:34:24 -0800 | [diff] [blame] | 148 | break; |
| 149 | } |
| 150 | ASSERT_TRUE((info.flags & SAMPLE_FLAG_END_OF_STREAM) == 0); |
Linus Nilsson | 6233fed | 2020-08-13 15:15:14 -0700 | [diff] [blame] | 151 | timestampMutex.lock(); |
Linus Nilsson | 478df7e | 2020-01-29 15:34:24 -0800 | [diff] [blame] | 152 | readerTimestamps[trackIndex].push_back(info.presentationTimeUs); |
Linus Nilsson | 6233fed | 2020-08-13 15:15:14 -0700 | [diff] [blame] | 153 | timestampMutex.unlock(); |
Linus Nilsson | 478df7e | 2020-01-29 15:34:24 -0800 | [diff] [blame] | 154 | sampleReader->advanceTrack(trackIndex); |
Linus Nilsson | 6233fed | 2020-08-13 15:15:14 -0700 | [diff] [blame] | 155 | } |
| 156 | }); |
| 157 | } |
| 158 | |
| 159 | for (auto& thread : trackThreads) { |
| 160 | thread.join(); |
Linus Nilsson | 478df7e | 2020-01-29 15:34:24 -0800 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | for (int trackIndex = 0; trackIndex < mTrackCount; trackIndex++) { |
| 164 | LOG(DEBUG) << "Track " << trackIndex << ", comparing " |
| 165 | << readerTimestamps[trackIndex].size() << " samples."; |
Linus Nilsson | 6233fed | 2020-08-13 15:15:14 -0700 | [diff] [blame] | 166 | EXPECT_EQ(readerTimestamps[trackIndex].size(), mExtractorTimestamps[trackIndex].size()); |
Linus Nilsson | 478df7e | 2020-01-29 15:34:24 -0800 | [diff] [blame] | 167 | for (size_t sampleIndex = 0; sampleIndex < readerTimestamps[trackIndex].size(); |
| 168 | sampleIndex++) { |
Linus Nilsson | 6233fed | 2020-08-13 15:15:14 -0700 | [diff] [blame] | 169 | EXPECT_EQ(readerTimestamps[trackIndex][sampleIndex], |
Linus Nilsson | 478df7e | 2020-01-29 15:34:24 -0800 | [diff] [blame] | 170 | mExtractorTimestamps[trackIndex][sampleIndex]); |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | |
Linus Nilsson | 800793f | 2020-07-31 16:16:38 -0700 | [diff] [blame] | 175 | TEST_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 Nilsson | 6233fed | 2020-08-13 15:15:14 -0700 | [diff] [blame] | 185 | EXPECT_EQ(sampleReader->selectTrack(trackIndex), AMEDIA_OK); |
| 186 | |
Linus Nilsson | 800793f | 2020-07-31 16:16:38 -0700 | [diff] [blame] | 187 | 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 Nilsson | 478df7e | 2020-01-29 15:34:24 -0800 | [diff] [blame] | 199 | TEST_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 | |
| 208 | TEST_F(MediaSampleReaderNDKTests, TestZeroSize) { |
| 209 | std::shared_ptr<MediaSampleReader> sampleReader = |
| 210 | MediaSampleReaderNDK::createFromFd(mSourceFd, 0, 0); |
| 211 | ASSERT_TRUE(sampleReader == nullptr); |
| 212 | } |
| 213 | |
| 214 | TEST_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 | |
| 222 | int main(int argc, char** argv) { |
| 223 | ::testing::InitGoogleTest(&argc, argv); |
| 224 | return RUN_ALL_TESTS(); |
| 225 | } |