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