Linus Nilsson | a85df7f | 2020-02-20 16:32:04 -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 | // #define LOG_NDEBUG 0 |
| 18 | #define LOG_TAG "MediaSampleWriter" |
| 19 | |
| 20 | #include <android-base/logging.h> |
| 21 | #include <media/MediaSampleWriter.h> |
| 22 | #include <media/NdkMediaMuxer.h> |
Linus Nilsson | 22df0f2 | 2021-04-21 15:11:27 -0700 | [diff] [blame] | 23 | #include <sys/prctl.h> |
Linus Nilsson | f5df0cc | 2021-04-22 15:40:32 -0700 | [diff] [blame^] | 24 | #include <utils/AndroidThreads.h> |
Linus Nilsson | a85df7f | 2020-02-20 16:32:04 -0800 | [diff] [blame] | 25 | |
| 26 | namespace android { |
| 27 | |
| 28 | class DefaultMuxer : public MediaSampleWriterMuxerInterface { |
| 29 | public: |
| 30 | // MediaSampleWriterMuxerInterface |
Chong Zhang | d6e4aec | 2020-06-22 14:13:07 -0700 | [diff] [blame] | 31 | ssize_t addTrack(AMediaFormat* trackFormat) override { |
| 32 | // If the track format has rotation, need to call AMediaMuxer_setOrientationHint |
| 33 | // to set the rotation. Muxer doesn't take rotation specified on the track. |
| 34 | const char* mime; |
| 35 | if (AMediaFormat_getString(trackFormat, AMEDIAFORMAT_KEY_MIME, &mime) && |
| 36 | strncmp(mime, "video/", 6) == 0) { |
| 37 | int32_t rotation; |
| 38 | if (AMediaFormat_getInt32(trackFormat, AMEDIAFORMAT_KEY_ROTATION, &rotation) && |
| 39 | (rotation != 0)) { |
| 40 | AMediaMuxer_setOrientationHint(mMuxer, rotation); |
| 41 | } |
| 42 | } |
| 43 | |
Linus Nilsson | a85df7f | 2020-02-20 16:32:04 -0800 | [diff] [blame] | 44 | return AMediaMuxer_addTrack(mMuxer, trackFormat); |
| 45 | } |
| 46 | media_status_t start() override { return AMediaMuxer_start(mMuxer); } |
| 47 | media_status_t writeSampleData(size_t trackIndex, const uint8_t* data, |
| 48 | const AMediaCodecBufferInfo* info) override { |
| 49 | return AMediaMuxer_writeSampleData(mMuxer, trackIndex, data, info); |
| 50 | } |
| 51 | media_status_t stop() override { return AMediaMuxer_stop(mMuxer); } |
| 52 | // ~MediaSampleWriterMuxerInterface |
| 53 | |
| 54 | static std::shared_ptr<DefaultMuxer> create(int fd) { |
| 55 | AMediaMuxer* ndkMuxer = AMediaMuxer_new(fd, AMEDIAMUXER_OUTPUT_FORMAT_MPEG_4); |
| 56 | if (ndkMuxer == nullptr) { |
| 57 | LOG(ERROR) << "Unable to create AMediaMuxer"; |
| 58 | return nullptr; |
| 59 | } |
| 60 | |
| 61 | return std::make_shared<DefaultMuxer>(ndkMuxer); |
| 62 | } |
| 63 | |
| 64 | ~DefaultMuxer() { |
| 65 | if (mMuxer != nullptr) { |
| 66 | AMediaMuxer_delete(mMuxer); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | DefaultMuxer(AMediaMuxer* muxer) : mMuxer(muxer){}; |
| 71 | DefaultMuxer() = delete; |
| 72 | |
| 73 | private: |
| 74 | AMediaMuxer* mMuxer; |
| 75 | }; |
| 76 | |
Linus Nilsson | c31d249 | 2020-09-23 12:30:00 -0700 | [diff] [blame] | 77 | // static |
| 78 | std::shared_ptr<MediaSampleWriter> MediaSampleWriter::Create() { |
| 79 | return std::shared_ptr<MediaSampleWriter>(new MediaSampleWriter()); |
| 80 | } |
| 81 | |
Linus Nilsson | a85df7f | 2020-02-20 16:32:04 -0800 | [diff] [blame] | 82 | MediaSampleWriter::~MediaSampleWriter() { |
| 83 | if (mState == STARTED) { |
Linus Nilsson | fdb3e33 | 2020-09-18 17:11:41 -0700 | [diff] [blame] | 84 | stop(); |
Linus Nilsson | a85df7f | 2020-02-20 16:32:04 -0800 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | |
Chong Zhang | 457c689 | 2021-02-01 15:34:20 -0800 | [diff] [blame] | 88 | bool MediaSampleWriter::init(int fd, const std::weak_ptr<CallbackInterface>& callbacks, |
| 89 | int64_t heartBeatIntervalUs) { |
| 90 | return init(DefaultMuxer::create(fd), callbacks, heartBeatIntervalUs); |
Linus Nilsson | a85df7f | 2020-02-20 16:32:04 -0800 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | bool MediaSampleWriter::init(const std::shared_ptr<MediaSampleWriterMuxerInterface>& muxer, |
Chong Zhang | 457c689 | 2021-02-01 15:34:20 -0800 | [diff] [blame] | 94 | const std::weak_ptr<CallbackInterface>& callbacks, |
| 95 | int64_t heartBeatIntervalUs) { |
Linus Nilsson | e2cdd1f | 2020-07-07 17:29:26 -0700 | [diff] [blame] | 96 | if (callbacks.lock() == nullptr) { |
| 97 | LOG(ERROR) << "Callback object cannot be null"; |
Linus Nilsson | a85df7f | 2020-02-20 16:32:04 -0800 | [diff] [blame] | 98 | return false; |
| 99 | } else if (muxer == nullptr) { |
| 100 | LOG(ERROR) << "Muxer cannot be null"; |
| 101 | return false; |
| 102 | } |
| 103 | |
Linus Nilsson | c31d249 | 2020-09-23 12:30:00 -0700 | [diff] [blame] | 104 | std::scoped_lock lock(mMutex); |
Linus Nilsson | a85df7f | 2020-02-20 16:32:04 -0800 | [diff] [blame] | 105 | if (mState != UNINITIALIZED) { |
| 106 | LOG(ERROR) << "Sample writer is already initialized"; |
| 107 | return false; |
| 108 | } |
| 109 | |
| 110 | mState = INITIALIZED; |
| 111 | mMuxer = muxer; |
Linus Nilsson | e2cdd1f | 2020-07-07 17:29:26 -0700 | [diff] [blame] | 112 | mCallbacks = callbacks; |
Chong Zhang | 457c689 | 2021-02-01 15:34:20 -0800 | [diff] [blame] | 113 | mHeartBeatIntervalUs = heartBeatIntervalUs; |
Linus Nilsson | a85df7f | 2020-02-20 16:32:04 -0800 | [diff] [blame] | 114 | return true; |
| 115 | } |
| 116 | |
Linus Nilsson | c31d249 | 2020-09-23 12:30:00 -0700 | [diff] [blame] | 117 | MediaSampleWriter::MediaSampleConsumerFunction MediaSampleWriter::addTrack( |
| 118 | const std::shared_ptr<AMediaFormat>& trackFormat) { |
| 119 | if (trackFormat == nullptr) { |
| 120 | LOG(ERROR) << "Track format must be non-null"; |
| 121 | return nullptr; |
Linus Nilsson | a85df7f | 2020-02-20 16:32:04 -0800 | [diff] [blame] | 122 | } |
| 123 | |
Linus Nilsson | c31d249 | 2020-09-23 12:30:00 -0700 | [diff] [blame] | 124 | std::scoped_lock lock(mMutex); |
Linus Nilsson | a85df7f | 2020-02-20 16:32:04 -0800 | [diff] [blame] | 125 | if (mState != INITIALIZED) { |
| 126 | LOG(ERROR) << "Muxer needs to be initialized when adding tracks."; |
Linus Nilsson | c31d249 | 2020-09-23 12:30:00 -0700 | [diff] [blame] | 127 | return nullptr; |
Linus Nilsson | a85df7f | 2020-02-20 16:32:04 -0800 | [diff] [blame] | 128 | } |
Linus Nilsson | c31d249 | 2020-09-23 12:30:00 -0700 | [diff] [blame] | 129 | ssize_t trackIndexOrError = mMuxer->addTrack(trackFormat.get()); |
| 130 | if (trackIndexOrError < 0) { |
| 131 | LOG(ERROR) << "Failed to add media track to muxer: " << trackIndexOrError; |
| 132 | return nullptr; |
Linus Nilsson | a85df7f | 2020-02-20 16:32:04 -0800 | [diff] [blame] | 133 | } |
Linus Nilsson | c31d249 | 2020-09-23 12:30:00 -0700 | [diff] [blame] | 134 | const size_t trackIndex = static_cast<size_t>(trackIndexOrError); |
Linus Nilsson | a85df7f | 2020-02-20 16:32:04 -0800 | [diff] [blame] | 135 | |
Linus Nilsson | 42a971b | 2020-07-01 16:41:11 -0700 | [diff] [blame] | 136 | int64_t durationUs; |
| 137 | if (!AMediaFormat_getInt64(trackFormat.get(), AMEDIAFORMAT_KEY_DURATION, &durationUs)) { |
| 138 | durationUs = 0; |
| 139 | } |
| 140 | |
Linus Nilsson | c31d249 | 2020-09-23 12:30:00 -0700 | [diff] [blame] | 141 | mTracks.emplace(trackIndex, durationUs); |
| 142 | std::shared_ptr<MediaSampleWriter> thisWriter = shared_from_this(); |
| 143 | |
| 144 | return [self = shared_from_this(), trackIndex](const std::shared_ptr<MediaSample>& sample) { |
| 145 | self->addSampleToTrack(trackIndex, sample); |
| 146 | }; |
| 147 | } |
| 148 | |
| 149 | void MediaSampleWriter::addSampleToTrack(size_t trackIndex, |
| 150 | const std::shared_ptr<MediaSample>& sample) { |
| 151 | if (sample == nullptr) return; |
| 152 | |
| 153 | bool wasEmpty; |
| 154 | { |
| 155 | std::scoped_lock lock(mMutex); |
| 156 | wasEmpty = mSampleQueue.empty(); |
| 157 | mSampleQueue.push(std::make_pair(trackIndex, sample)); |
| 158 | } |
| 159 | |
| 160 | if (wasEmpty) { |
| 161 | mSampleSignal.notify_one(); |
| 162 | } |
Linus Nilsson | a85df7f | 2020-02-20 16:32:04 -0800 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | bool MediaSampleWriter::start() { |
Linus Nilsson | c31d249 | 2020-09-23 12:30:00 -0700 | [diff] [blame] | 166 | std::scoped_lock lock(mMutex); |
Linus Nilsson | a85df7f | 2020-02-20 16:32:04 -0800 | [diff] [blame] | 167 | |
Linus Nilsson | c31d249 | 2020-09-23 12:30:00 -0700 | [diff] [blame] | 168 | if (mTracks.size() == 0) { |
Linus Nilsson | a85df7f | 2020-02-20 16:32:04 -0800 | [diff] [blame] | 169 | LOG(ERROR) << "No tracks to write."; |
| 170 | return false; |
| 171 | } else if (mState != INITIALIZED) { |
| 172 | LOG(ERROR) << "Sample writer is not initialized"; |
| 173 | return false; |
| 174 | } |
| 175 | |
Linus Nilsson | c31d249 | 2020-09-23 12:30:00 -0700 | [diff] [blame] | 176 | mState = STARTED; |
Linus Nilsson | fdb3e33 | 2020-09-18 17:11:41 -0700 | [diff] [blame] | 177 | std::thread([this] { |
Linus Nilsson | f5df0cc | 2021-04-22 15:40:32 -0700 | [diff] [blame^] | 178 | androidSetThreadPriority(0 /* tid (0 = current) */, ANDROID_PRIORITY_BACKGROUND); |
Linus Nilsson | 22df0f2 | 2021-04-21 15:11:27 -0700 | [diff] [blame] | 179 | prctl(PR_SET_NAME, (unsigned long)"SampleWriterTrd", 0, 0, 0); |
| 180 | |
Linus Nilsson | fdb3e33 | 2020-09-18 17:11:41 -0700 | [diff] [blame] | 181 | bool wasStopped = false; |
| 182 | media_status_t status = writeSamples(&wasStopped); |
Linus Nilsson | e2cdd1f | 2020-07-07 17:29:26 -0700 | [diff] [blame] | 183 | if (auto callbacks = mCallbacks.lock()) { |
Linus Nilsson | fdb3e33 | 2020-09-18 17:11:41 -0700 | [diff] [blame] | 184 | if (wasStopped && status == AMEDIA_OK) { |
| 185 | callbacks->onStopped(this); |
| 186 | } else { |
| 187 | callbacks->onFinished(this, status); |
| 188 | } |
Linus Nilsson | e2cdd1f | 2020-07-07 17:29:26 -0700 | [diff] [blame] | 189 | } |
Linus Nilsson | fdb3e33 | 2020-09-18 17:11:41 -0700 | [diff] [blame] | 190 | }).detach(); |
Linus Nilsson | a85df7f | 2020-02-20 16:32:04 -0800 | [diff] [blame] | 191 | return true; |
| 192 | } |
| 193 | |
Linus Nilsson | fdb3e33 | 2020-09-18 17:11:41 -0700 | [diff] [blame] | 194 | void MediaSampleWriter::stop() { |
Linus Nilsson | c31d249 | 2020-09-23 12:30:00 -0700 | [diff] [blame] | 195 | { |
| 196 | std::scoped_lock lock(mMutex); |
| 197 | if (mState != STARTED) { |
| 198 | LOG(ERROR) << "Sample writer is not started."; |
Linus Nilsson | fdb3e33 | 2020-09-18 17:11:41 -0700 | [diff] [blame] | 199 | return; |
Linus Nilsson | c31d249 | 2020-09-23 12:30:00 -0700 | [diff] [blame] | 200 | } |
| 201 | mState = STOPPED; |
Linus Nilsson | a85df7f | 2020-02-20 16:32:04 -0800 | [diff] [blame] | 202 | } |
| 203 | |
Linus Nilsson | c31d249 | 2020-09-23 12:30:00 -0700 | [diff] [blame] | 204 | mSampleSignal.notify_all(); |
Linus Nilsson | a85df7f | 2020-02-20 16:32:04 -0800 | [diff] [blame] | 205 | } |
| 206 | |
Linus Nilsson | fdb3e33 | 2020-09-18 17:11:41 -0700 | [diff] [blame] | 207 | media_status_t MediaSampleWriter::writeSamples(bool* wasStopped) { |
Linus Nilsson | a85df7f | 2020-02-20 16:32:04 -0800 | [diff] [blame] | 208 | media_status_t muxerStatus = mMuxer->start(); |
| 209 | if (muxerStatus != AMEDIA_OK) { |
| 210 | LOG(ERROR) << "Error starting muxer: " << muxerStatus; |
| 211 | return muxerStatus; |
| 212 | } |
| 213 | |
Linus Nilsson | fdb3e33 | 2020-09-18 17:11:41 -0700 | [diff] [blame] | 214 | media_status_t writeStatus = runWriterLoop(wasStopped); |
Linus Nilsson | a85df7f | 2020-02-20 16:32:04 -0800 | [diff] [blame] | 215 | if (writeStatus != AMEDIA_OK) { |
| 216 | LOG(ERROR) << "Error writing samples: " << writeStatus; |
| 217 | } |
| 218 | |
| 219 | muxerStatus = mMuxer->stop(); |
| 220 | if (muxerStatus != AMEDIA_OK) { |
| 221 | LOG(ERROR) << "Error stopping muxer: " << muxerStatus; |
| 222 | } |
| 223 | |
| 224 | return writeStatus != AMEDIA_OK ? writeStatus : muxerStatus; |
| 225 | } |
| 226 | |
Linus Nilsson | fdb3e33 | 2020-09-18 17:11:41 -0700 | [diff] [blame] | 227 | media_status_t MediaSampleWriter::runWriterLoop(bool* wasStopped) NO_THREAD_SAFETY_ANALYSIS { |
Linus Nilsson | a85df7f | 2020-02-20 16:32:04 -0800 | [diff] [blame] | 228 | AMediaCodecBufferInfo bufferInfo; |
Linus Nilsson | e2cdd1f | 2020-07-07 17:29:26 -0700 | [diff] [blame] | 229 | int32_t lastProgressUpdate = 0; |
Chong Zhang | 457c689 | 2021-02-01 15:34:20 -0800 | [diff] [blame] | 230 | bool progressSinceLastReport = false; |
Linus Nilsson | c31d249 | 2020-09-23 12:30:00 -0700 | [diff] [blame] | 231 | int trackEosCount = 0; |
Linus Nilsson | e2cdd1f | 2020-07-07 17:29:26 -0700 | [diff] [blame] | 232 | |
| 233 | // Set the "primary" track that will be used to determine progress to the track with longest |
| 234 | // duration. |
| 235 | int primaryTrackIndex = -1; |
| 236 | int64_t longestDurationUs = 0; |
Linus Nilsson | c31d249 | 2020-09-23 12:30:00 -0700 | [diff] [blame] | 237 | for (auto it = mTracks.begin(); it != mTracks.end(); ++it) { |
| 238 | if (it->second.mDurationUs > longestDurationUs) { |
| 239 | primaryTrackIndex = it->first; |
| 240 | longestDurationUs = it->second.mDurationUs; |
Linus Nilsson | e2cdd1f | 2020-07-07 17:29:26 -0700 | [diff] [blame] | 241 | } |
| 242 | } |
Linus Nilsson | a85df7f | 2020-02-20 16:32:04 -0800 | [diff] [blame] | 243 | |
Chong Zhang | 457c689 | 2021-02-01 15:34:20 -0800 | [diff] [blame] | 244 | std::chrono::microseconds updateInterval(mHeartBeatIntervalUs); |
Chong Zhang | 87d199c | 2021-03-01 19:02:18 -0800 | [diff] [blame] | 245 | std::chrono::steady_clock::time_point nextUpdateTime = |
| 246 | std::chrono::steady_clock::now() + updateInterval; |
Chong Zhang | 457c689 | 2021-02-01 15:34:20 -0800 | [diff] [blame] | 247 | |
Linus Nilsson | b09aac2 | 2020-07-29 11:56:53 -0700 | [diff] [blame] | 248 | while (true) { |
Linus Nilsson | c31d249 | 2020-09-23 12:30:00 -0700 | [diff] [blame] | 249 | if (trackEosCount >= mTracks.size()) { |
Linus Nilsson | b09aac2 | 2020-07-29 11:56:53 -0700 | [diff] [blame] | 250 | break; |
Linus Nilsson | a85df7f | 2020-02-20 16:32:04 -0800 | [diff] [blame] | 251 | } |
| 252 | |
Linus Nilsson | c31d249 | 2020-09-23 12:30:00 -0700 | [diff] [blame] | 253 | size_t trackIndex; |
Linus Nilsson | b09aac2 | 2020-07-29 11:56:53 -0700 | [diff] [blame] | 254 | std::shared_ptr<MediaSample> sample; |
Linus Nilsson | c31d249 | 2020-09-23 12:30:00 -0700 | [diff] [blame] | 255 | { |
| 256 | std::unique_lock lock(mMutex); |
| 257 | while (mSampleQueue.empty() && mState == STARTED) { |
Chong Zhang | 457c689 | 2021-02-01 15:34:20 -0800 | [diff] [blame] | 258 | if (mHeartBeatIntervalUs <= 0) { |
| 259 | mSampleSignal.wait(lock); |
| 260 | continue; |
| 261 | } |
| 262 | |
| 263 | if (mSampleSignal.wait_until(lock, nextUpdateTime) == std::cv_status::timeout) { |
| 264 | // Send heart-beat if there is any progress since last update time. |
| 265 | if (progressSinceLastReport) { |
| 266 | if (auto callbacks = mCallbacks.lock()) { |
| 267 | callbacks->onHeartBeat(this); |
| 268 | } |
| 269 | progressSinceLastReport = false; |
| 270 | } |
| 271 | nextUpdateTime += updateInterval; |
| 272 | } |
Linus Nilsson | c31d249 | 2020-09-23 12:30:00 -0700 | [diff] [blame] | 273 | } |
| 274 | |
Linus Nilsson | fdb3e33 | 2020-09-18 17:11:41 -0700 | [diff] [blame] | 275 | if (mState == STOPPED) { |
| 276 | *wasStopped = true; |
| 277 | return AMEDIA_OK; |
Linus Nilsson | c31d249 | 2020-09-23 12:30:00 -0700 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | auto& topEntry = mSampleQueue.top(); |
| 281 | trackIndex = topEntry.first; |
| 282 | sample = topEntry.second; |
| 283 | mSampleQueue.pop(); |
| 284 | } |
| 285 | |
| 286 | TrackRecord& track = mTracks[trackIndex]; |
| 287 | |
| 288 | if (sample->info.flags & SAMPLE_FLAG_END_OF_STREAM) { |
| 289 | if (track.mReachedEos) { |
| 290 | continue; |
| 291 | } |
| 292 | |
Linus Nilsson | b09aac2 | 2020-07-29 11:56:53 -0700 | [diff] [blame] | 293 | // Track reached end of stream. |
Linus Nilsson | c31d249 | 2020-09-23 12:30:00 -0700 | [diff] [blame] | 294 | track.mReachedEos = true; |
| 295 | trackEosCount++; |
Linus Nilsson | b09aac2 | 2020-07-29 11:56:53 -0700 | [diff] [blame] | 296 | |
| 297 | // Preserve source track duration by setting the appropriate timestamp on the |
| 298 | // empty End-Of-Stream sample. |
Linus Nilsson | c31d249 | 2020-09-23 12:30:00 -0700 | [diff] [blame] | 299 | if (track.mDurationUs > 0 && track.mFirstSampleTimeSet) { |
| 300 | sample->info.presentationTimeUs = track.mDurationUs + track.mFirstSampleTimeUs; |
Linus Nilsson | b09aac2 | 2020-07-29 11:56:53 -0700 | [diff] [blame] | 301 | } |
| 302 | } |
| 303 | |
Linus Nilsson | c31d249 | 2020-09-23 12:30:00 -0700 | [diff] [blame] | 304 | track.mPrevSampleTimeUs = sample->info.presentationTimeUs; |
| 305 | if (!track.mFirstSampleTimeSet) { |
Linus Nilsson | b09aac2 | 2020-07-29 11:56:53 -0700 | [diff] [blame] | 306 | // Record the first sample's timestamp in order to translate duration to EOS |
| 307 | // time for tracks that does not start at 0. |
Linus Nilsson | c31d249 | 2020-09-23 12:30:00 -0700 | [diff] [blame] | 308 | track.mFirstSampleTimeUs = sample->info.presentationTimeUs; |
| 309 | track.mFirstSampleTimeSet = true; |
Linus Nilsson | b09aac2 | 2020-07-29 11:56:53 -0700 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | bufferInfo.offset = sample->dataOffset; |
| 313 | bufferInfo.size = sample->info.size; |
| 314 | bufferInfo.flags = sample->info.flags; |
| 315 | bufferInfo.presentationTimeUs = sample->info.presentationTimeUs; |
| 316 | |
Linus Nilsson | c31d249 | 2020-09-23 12:30:00 -0700 | [diff] [blame] | 317 | media_status_t status = mMuxer->writeSampleData(trackIndex, sample->buffer, &bufferInfo); |
Linus Nilsson | b09aac2 | 2020-07-29 11:56:53 -0700 | [diff] [blame] | 318 | if (status != AMEDIA_OK) { |
| 319 | LOG(ERROR) << "writeSampleData returned " << status; |
| 320 | return status; |
| 321 | } |
| 322 | sample.reset(); |
| 323 | |
| 324 | // TODO(lnilsson): Add option to toggle progress reporting on/off. |
Linus Nilsson | c31d249 | 2020-09-23 12:30:00 -0700 | [diff] [blame] | 325 | if (trackIndex == primaryTrackIndex) { |
| 326 | const int64_t elapsed = track.mPrevSampleTimeUs - track.mFirstSampleTimeUs; |
| 327 | int32_t progress = (elapsed * 100) / track.mDurationUs; |
Linus Nilsson | e2cdd1f | 2020-07-07 17:29:26 -0700 | [diff] [blame] | 328 | progress = std::clamp(progress, 0, 100); |
| 329 | |
| 330 | if (progress > lastProgressUpdate) { |
| 331 | if (auto callbacks = mCallbacks.lock()) { |
| 332 | callbacks->onProgressUpdate(this, progress); |
| 333 | } |
| 334 | lastProgressUpdate = progress; |
| 335 | } |
| 336 | } |
Linus Nilsson | d2bef93 | 2021-03-23 21:55:33 -0700 | [diff] [blame] | 337 | progressSinceLastReport = true; |
Linus Nilsson | a85df7f | 2020-02-20 16:32:04 -0800 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | return AMEDIA_OK; |
| 341 | } |
Chong Zhang | d6e4aec | 2020-06-22 14:13:07 -0700 | [diff] [blame] | 342 | } // namespace android |