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