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> |
| 29 | |
Linus Nilsson | 800793f | 2020-07-31 16:16:38 -0700 | [diff] [blame] | 30 | #include <cmath> |
| 31 | |
Linus Nilsson | 478df7e | 2020-01-29 15:34:24 -0800 | [diff] [blame] | 32 | // TODO(b/153453392): Test more asset types and validate sample data from readSampleDataForTrack. |
| 33 | |
| 34 | namespace android { |
| 35 | |
| 36 | #define SEC_TO_USEC(s) ((s)*1000 * 1000) |
| 37 | |
| 38 | class MediaSampleReaderNDKTests : public ::testing::Test { |
| 39 | public: |
| 40 | MediaSampleReaderNDKTests() { LOG(DEBUG) << "MediaSampleReaderNDKTests created"; } |
| 41 | |
| 42 | void SetUp() override { |
| 43 | LOG(DEBUG) << "MediaSampleReaderNDKTests set up"; |
| 44 | const char* sourcePath = |
hkuang | 2ef2b43 | 2020-06-15 18:33:11 -0700 | [diff] [blame] | 45 | "/data/local/tmp/TranscodingTestAssets/cubicle_avc_480x240_aac_24KHz.mp4"; |
Linus Nilsson | 478df7e | 2020-01-29 15:34:24 -0800 | [diff] [blame] | 46 | |
| 47 | mExtractor = AMediaExtractor_new(); |
| 48 | ASSERT_NE(mExtractor, nullptr); |
| 49 | |
| 50 | mSourceFd = open(sourcePath, O_RDONLY); |
| 51 | ASSERT_GT(mSourceFd, 0); |
| 52 | |
| 53 | mFileSize = lseek(mSourceFd, 0, SEEK_END); |
| 54 | lseek(mSourceFd, 0, SEEK_SET); |
| 55 | |
| 56 | media_status_t status = |
| 57 | AMediaExtractor_setDataSourceFd(mExtractor, mSourceFd, 0, mFileSize); |
| 58 | ASSERT_EQ(status, AMEDIA_OK); |
| 59 | |
| 60 | mTrackCount = AMediaExtractor_getTrackCount(mExtractor); |
| 61 | for (size_t trackIndex = 0; trackIndex < mTrackCount; trackIndex++) { |
| 62 | AMediaExtractor_selectTrack(mExtractor, trackIndex); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | void initExtractorTimestamps() { |
| 67 | // Save all sample timestamps, per track, as reported by the extractor. |
| 68 | mExtractorTimestamps.resize(mTrackCount); |
| 69 | do { |
| 70 | const int trackIndex = AMediaExtractor_getSampleTrackIndex(mExtractor); |
| 71 | const int64_t sampleTime = AMediaExtractor_getSampleTime(mExtractor); |
| 72 | |
| 73 | mExtractorTimestamps[trackIndex].push_back(sampleTime); |
| 74 | } while (AMediaExtractor_advance(mExtractor)); |
Linus Nilsson | 800793f | 2020-07-31 16:16:38 -0700 | [diff] [blame] | 75 | |
| 76 | AMediaExtractor_seekTo(mExtractor, 0, AMEDIAEXTRACTOR_SEEK_PREVIOUS_SYNC); |
| 77 | } |
| 78 | |
| 79 | std::vector<int32_t> getTrackBitrates() { |
| 80 | size_t totalSize[mTrackCount]; |
| 81 | memset(totalSize, 0, sizeof(totalSize)); |
| 82 | |
| 83 | do { |
| 84 | const int trackIndex = AMediaExtractor_getSampleTrackIndex(mExtractor); |
| 85 | totalSize[trackIndex] += AMediaExtractor_getSampleSize(mExtractor); |
| 86 | } while (AMediaExtractor_advance(mExtractor)); |
| 87 | |
| 88 | AMediaExtractor_seekTo(mExtractor, 0, AMEDIAEXTRACTOR_SEEK_PREVIOUS_SYNC); |
| 89 | |
| 90 | std::vector<int32_t> bitrates; |
| 91 | for (int trackIndex = 0; trackIndex < mTrackCount; trackIndex++) { |
| 92 | int64_t durationUs; |
| 93 | AMediaFormat* trackFormat = AMediaExtractor_getTrackFormat(mExtractor, trackIndex); |
| 94 | EXPECT_NE(trackFormat, nullptr); |
| 95 | EXPECT_TRUE(AMediaFormat_getInt64(trackFormat, AMEDIAFORMAT_KEY_DURATION, &durationUs)); |
| 96 | bitrates.push_back(roundf((float)totalSize[trackIndex] * 8 * 1000000 / durationUs)); |
| 97 | } |
| 98 | |
| 99 | return bitrates; |
Linus Nilsson | 478df7e | 2020-01-29 15:34:24 -0800 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | void TearDown() override { |
| 103 | LOG(DEBUG) << "MediaSampleReaderNDKTests tear down"; |
| 104 | AMediaExtractor_delete(mExtractor); |
| 105 | close(mSourceFd); |
| 106 | } |
| 107 | |
| 108 | ~MediaSampleReaderNDKTests() { LOG(DEBUG) << "MediaSampleReaderNDKTests destroyed"; } |
| 109 | |
| 110 | AMediaExtractor* mExtractor = nullptr; |
| 111 | size_t mTrackCount; |
| 112 | int mSourceFd; |
| 113 | size_t mFileSize; |
| 114 | std::vector<std::vector<int64_t>> mExtractorTimestamps; |
| 115 | }; |
| 116 | |
| 117 | TEST_F(MediaSampleReaderNDKTests, TestSampleTimes) { |
| 118 | LOG(DEBUG) << "TestSampleTimes Starts"; |
| 119 | |
| 120 | std::shared_ptr<MediaSampleReader> sampleReader = |
| 121 | MediaSampleReaderNDK::createFromFd(mSourceFd, 0, mFileSize); |
| 122 | ASSERT_TRUE(sampleReader); |
| 123 | |
| 124 | MediaSampleInfo info; |
| 125 | int trackEosCount = 0; |
| 126 | std::vector<bool> trackReachedEos(mTrackCount, false); |
| 127 | std::vector<std::vector<int64_t>> readerTimestamps(mTrackCount); |
| 128 | |
| 129 | // Initialize the extractor timestamps. |
| 130 | initExtractorTimestamps(); |
| 131 | |
| 132 | // Read 5s of each track at a time. |
| 133 | const int64_t chunkDurationUs = SEC_TO_USEC(5); |
| 134 | int64_t chunkEndTimeUs = chunkDurationUs; |
| 135 | |
| 136 | // Loop until all tracks have reached End Of Stream. |
| 137 | while (trackEosCount < mTrackCount) { |
| 138 | for (int trackIndex = 0; trackIndex < mTrackCount; trackIndex++) { |
| 139 | if (trackReachedEos[trackIndex]) continue; |
| 140 | |
| 141 | // Advance current track to next chunk end time. |
| 142 | do { |
| 143 | media_status_t status = sampleReader->getSampleInfoForTrack(trackIndex, &info); |
| 144 | if (status != AMEDIA_OK) { |
| 145 | ASSERT_EQ(status, AMEDIA_ERROR_END_OF_STREAM); |
| 146 | ASSERT_TRUE((info.flags & SAMPLE_FLAG_END_OF_STREAM) != 0); |
| 147 | trackReachedEos[trackIndex] = true; |
| 148 | trackEosCount++; |
| 149 | break; |
| 150 | } |
| 151 | ASSERT_TRUE((info.flags & SAMPLE_FLAG_END_OF_STREAM) == 0); |
| 152 | readerTimestamps[trackIndex].push_back(info.presentationTimeUs); |
| 153 | sampleReader->advanceTrack(trackIndex); |
| 154 | } while (info.presentationTimeUs < chunkEndTimeUs); |
| 155 | } |
| 156 | chunkEndTimeUs += chunkDurationUs; |
| 157 | } |
| 158 | |
| 159 | for (int trackIndex = 0; trackIndex < mTrackCount; trackIndex++) { |
| 160 | LOG(DEBUG) << "Track " << trackIndex << ", comparing " |
| 161 | << readerTimestamps[trackIndex].size() << " samples."; |
| 162 | ASSERT_EQ(readerTimestamps[trackIndex].size(), mExtractorTimestamps[trackIndex].size()); |
| 163 | for (size_t sampleIndex = 0; sampleIndex < readerTimestamps[trackIndex].size(); |
| 164 | sampleIndex++) { |
| 165 | ASSERT_EQ(readerTimestamps[trackIndex][sampleIndex], |
| 166 | mExtractorTimestamps[trackIndex][sampleIndex]); |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | |
Linus Nilsson | 800793f | 2020-07-31 16:16:38 -0700 | [diff] [blame] | 171 | TEST_F(MediaSampleReaderNDKTests, TestEstimatedBitrateAccuracy) { |
| 172 | // Just put a somewhat reasonable upper bound on the estimated bitrate expected in our test |
| 173 | // assets. This is mostly to make sure the estimation is not way off. |
| 174 | static constexpr int32_t kMaxEstimatedBitrate = 100 * 1000 * 1000; // 100 Mbps |
| 175 | |
| 176 | auto sampleReader = MediaSampleReaderNDK::createFromFd(mSourceFd, 0, mFileSize); |
| 177 | ASSERT_TRUE(sampleReader); |
| 178 | |
| 179 | std::vector<int32_t> actualTrackBitrates = getTrackBitrates(); |
| 180 | for (int trackIndex = 0; trackIndex < mTrackCount; ++trackIndex) { |
| 181 | int32_t bitrate; |
| 182 | EXPECT_EQ(sampleReader->getEstimatedBitrateForTrack(trackIndex, &bitrate), AMEDIA_OK); |
| 183 | EXPECT_GT(bitrate, 0); |
| 184 | EXPECT_LT(bitrate, kMaxEstimatedBitrate); |
| 185 | |
| 186 | // Note: The test asset currently used in this test is shorter than the sampling duration |
| 187 | // used to estimate the bitrate in the sample reader. So for now the estimation should be |
| 188 | // exact but if/when a longer asset is used a reasonable delta needs to be defined. |
| 189 | EXPECT_EQ(bitrate, actualTrackBitrates[trackIndex]); |
| 190 | } |
| 191 | } |
| 192 | |
Linus Nilsson | 478df7e | 2020-01-29 15:34:24 -0800 | [diff] [blame] | 193 | TEST_F(MediaSampleReaderNDKTests, TestInvalidFd) { |
| 194 | std::shared_ptr<MediaSampleReader> sampleReader = |
| 195 | MediaSampleReaderNDK::createFromFd(0, 0, mFileSize); |
| 196 | ASSERT_TRUE(sampleReader == nullptr); |
| 197 | |
| 198 | sampleReader = MediaSampleReaderNDK::createFromFd(-1, 0, mFileSize); |
| 199 | ASSERT_TRUE(sampleReader == nullptr); |
| 200 | } |
| 201 | |
| 202 | TEST_F(MediaSampleReaderNDKTests, TestZeroSize) { |
| 203 | std::shared_ptr<MediaSampleReader> sampleReader = |
| 204 | MediaSampleReaderNDK::createFromFd(mSourceFd, 0, 0); |
| 205 | ASSERT_TRUE(sampleReader == nullptr); |
| 206 | } |
| 207 | |
| 208 | TEST_F(MediaSampleReaderNDKTests, TestInvalidOffset) { |
| 209 | std::shared_ptr<MediaSampleReader> sampleReader = |
| 210 | MediaSampleReaderNDK::createFromFd(mSourceFd, mFileSize, mFileSize); |
| 211 | ASSERT_TRUE(sampleReader == nullptr); |
| 212 | } |
| 213 | |
| 214 | } // namespace android |
| 215 | |
| 216 | int main(int argc, char** argv) { |
| 217 | ::testing::InitGoogleTest(&argc, argv); |
| 218 | return RUN_ALL_TESTS(); |
| 219 | } |