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) { |
| 82 | stop(); // Join thread. |
| 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 | a85df7f | 2020-02-20 16:32:04 -0800 | [diff] [blame] | 172 | mThread = std::thread([this] { |
| 173 | media_status_t status = writeSamples(); |
Linus Nilsson | e2cdd1f | 2020-07-07 17:29:26 -0700 | [diff] [blame] | 174 | if (auto callbacks = mCallbacks.lock()) { |
| 175 | callbacks->onFinished(this, status); |
| 176 | } |
Linus Nilsson | a85df7f | 2020-02-20 16:32:04 -0800 | [diff] [blame] | 177 | }); |
Linus Nilsson | a85df7f | 2020-02-20 16:32:04 -0800 | [diff] [blame] | 178 | return true; |
| 179 | } |
| 180 | |
| 181 | bool MediaSampleWriter::stop() { |
Linus Nilsson | c31d249 | 2020-09-23 12:30:00 -0700 | [diff] [blame] | 182 | { |
| 183 | std::scoped_lock lock(mMutex); |
| 184 | if (mState != STARTED) { |
| 185 | LOG(ERROR) << "Sample writer is not started."; |
| 186 | return false; |
| 187 | } |
| 188 | mState = STOPPED; |
Linus Nilsson | a85df7f | 2020-02-20 16:32:04 -0800 | [diff] [blame] | 189 | } |
| 190 | |
Linus Nilsson | c31d249 | 2020-09-23 12:30:00 -0700 | [diff] [blame] | 191 | mSampleSignal.notify_all(); |
Linus Nilsson | a85df7f | 2020-02-20 16:32:04 -0800 | [diff] [blame] | 192 | mThread.join(); |
Linus Nilsson | a85df7f | 2020-02-20 16:32:04 -0800 | [diff] [blame] | 193 | return true; |
| 194 | } |
| 195 | |
| 196 | media_status_t MediaSampleWriter::writeSamples() { |
| 197 | media_status_t muxerStatus = mMuxer->start(); |
| 198 | if (muxerStatus != AMEDIA_OK) { |
| 199 | LOG(ERROR) << "Error starting muxer: " << muxerStatus; |
| 200 | return muxerStatus; |
| 201 | } |
| 202 | |
| 203 | media_status_t writeStatus = runWriterLoop(); |
| 204 | if (writeStatus != AMEDIA_OK) { |
| 205 | LOG(ERROR) << "Error writing samples: " << writeStatus; |
| 206 | } |
| 207 | |
| 208 | muxerStatus = mMuxer->stop(); |
| 209 | if (muxerStatus != AMEDIA_OK) { |
| 210 | LOG(ERROR) << "Error stopping muxer: " << muxerStatus; |
| 211 | } |
| 212 | |
| 213 | return writeStatus != AMEDIA_OK ? writeStatus : muxerStatus; |
| 214 | } |
| 215 | |
Linus Nilsson | c31d249 | 2020-09-23 12:30:00 -0700 | [diff] [blame] | 216 | media_status_t MediaSampleWriter::runWriterLoop() NO_THREAD_SAFETY_ANALYSIS { |
Linus Nilsson | a85df7f | 2020-02-20 16:32:04 -0800 | [diff] [blame] | 217 | AMediaCodecBufferInfo bufferInfo; |
Linus Nilsson | e2cdd1f | 2020-07-07 17:29:26 -0700 | [diff] [blame] | 218 | int32_t lastProgressUpdate = 0; |
Linus Nilsson | c31d249 | 2020-09-23 12:30:00 -0700 | [diff] [blame] | 219 | int trackEosCount = 0; |
Linus Nilsson | e2cdd1f | 2020-07-07 17:29:26 -0700 | [diff] [blame] | 220 | |
| 221 | // Set the "primary" track that will be used to determine progress to the track with longest |
| 222 | // duration. |
| 223 | int primaryTrackIndex = -1; |
| 224 | int64_t longestDurationUs = 0; |
Linus Nilsson | c31d249 | 2020-09-23 12:30:00 -0700 | [diff] [blame] | 225 | for (auto it = mTracks.begin(); it != mTracks.end(); ++it) { |
| 226 | if (it->second.mDurationUs > longestDurationUs) { |
| 227 | primaryTrackIndex = it->first; |
| 228 | longestDurationUs = it->second.mDurationUs; |
Linus Nilsson | e2cdd1f | 2020-07-07 17:29:26 -0700 | [diff] [blame] | 229 | } |
| 230 | } |
Linus Nilsson | a85df7f | 2020-02-20 16:32:04 -0800 | [diff] [blame] | 231 | |
Linus Nilsson | b09aac2 | 2020-07-29 11:56:53 -0700 | [diff] [blame] | 232 | while (true) { |
Linus Nilsson | c31d249 | 2020-09-23 12:30:00 -0700 | [diff] [blame] | 233 | if (trackEosCount >= mTracks.size()) { |
Linus Nilsson | b09aac2 | 2020-07-29 11:56:53 -0700 | [diff] [blame] | 234 | break; |
Linus Nilsson | a85df7f | 2020-02-20 16:32:04 -0800 | [diff] [blame] | 235 | } |
| 236 | |
Linus Nilsson | c31d249 | 2020-09-23 12:30:00 -0700 | [diff] [blame] | 237 | size_t trackIndex; |
Linus Nilsson | b09aac2 | 2020-07-29 11:56:53 -0700 | [diff] [blame] | 238 | std::shared_ptr<MediaSample> sample; |
Linus Nilsson | c31d249 | 2020-09-23 12:30:00 -0700 | [diff] [blame] | 239 | { |
| 240 | std::unique_lock lock(mMutex); |
| 241 | while (mSampleQueue.empty() && mState == STARTED) { |
| 242 | mSampleSignal.wait(lock); |
| 243 | } |
| 244 | |
| 245 | if (mState != STARTED) { |
| 246 | return AMEDIA_ERROR_UNKNOWN; // TODO(lnilsson): Custom error code. |
| 247 | } |
| 248 | |
| 249 | auto& topEntry = mSampleQueue.top(); |
| 250 | trackIndex = topEntry.first; |
| 251 | sample = topEntry.second; |
| 252 | mSampleQueue.pop(); |
| 253 | } |
| 254 | |
| 255 | TrackRecord& track = mTracks[trackIndex]; |
| 256 | |
| 257 | if (sample->info.flags & SAMPLE_FLAG_END_OF_STREAM) { |
| 258 | if (track.mReachedEos) { |
| 259 | continue; |
| 260 | } |
| 261 | |
Linus Nilsson | b09aac2 | 2020-07-29 11:56:53 -0700 | [diff] [blame] | 262 | // Track reached end of stream. |
Linus Nilsson | c31d249 | 2020-09-23 12:30:00 -0700 | [diff] [blame] | 263 | track.mReachedEos = true; |
| 264 | trackEosCount++; |
Linus Nilsson | b09aac2 | 2020-07-29 11:56:53 -0700 | [diff] [blame] | 265 | |
| 266 | // Preserve source track duration by setting the appropriate timestamp on the |
| 267 | // empty End-Of-Stream sample. |
Linus Nilsson | c31d249 | 2020-09-23 12:30:00 -0700 | [diff] [blame] | 268 | if (track.mDurationUs > 0 && track.mFirstSampleTimeSet) { |
| 269 | sample->info.presentationTimeUs = track.mDurationUs + track.mFirstSampleTimeUs; |
Linus Nilsson | b09aac2 | 2020-07-29 11:56:53 -0700 | [diff] [blame] | 270 | } |
| 271 | } |
| 272 | |
Linus Nilsson | c31d249 | 2020-09-23 12:30:00 -0700 | [diff] [blame] | 273 | track.mPrevSampleTimeUs = sample->info.presentationTimeUs; |
| 274 | if (!track.mFirstSampleTimeSet) { |
Linus Nilsson | b09aac2 | 2020-07-29 11:56:53 -0700 | [diff] [blame] | 275 | // Record the first sample's timestamp in order to translate duration to EOS |
| 276 | // time for tracks that does not start at 0. |
Linus Nilsson | c31d249 | 2020-09-23 12:30:00 -0700 | [diff] [blame] | 277 | track.mFirstSampleTimeUs = sample->info.presentationTimeUs; |
| 278 | track.mFirstSampleTimeSet = true; |
Linus Nilsson | b09aac2 | 2020-07-29 11:56:53 -0700 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | bufferInfo.offset = sample->dataOffset; |
| 282 | bufferInfo.size = sample->info.size; |
| 283 | bufferInfo.flags = sample->info.flags; |
| 284 | bufferInfo.presentationTimeUs = sample->info.presentationTimeUs; |
| 285 | |
Linus Nilsson | c31d249 | 2020-09-23 12:30:00 -0700 | [diff] [blame] | 286 | media_status_t status = mMuxer->writeSampleData(trackIndex, sample->buffer, &bufferInfo); |
Linus Nilsson | b09aac2 | 2020-07-29 11:56:53 -0700 | [diff] [blame] | 287 | if (status != AMEDIA_OK) { |
| 288 | LOG(ERROR) << "writeSampleData returned " << status; |
| 289 | return status; |
| 290 | } |
| 291 | sample.reset(); |
| 292 | |
| 293 | // TODO(lnilsson): Add option to toggle progress reporting on/off. |
Linus Nilsson | c31d249 | 2020-09-23 12:30:00 -0700 | [diff] [blame] | 294 | if (trackIndex == primaryTrackIndex) { |
| 295 | const int64_t elapsed = track.mPrevSampleTimeUs - track.mFirstSampleTimeUs; |
| 296 | int32_t progress = (elapsed * 100) / track.mDurationUs; |
Linus Nilsson | e2cdd1f | 2020-07-07 17:29:26 -0700 | [diff] [blame] | 297 | progress = std::clamp(progress, 0, 100); |
| 298 | |
| 299 | if (progress > lastProgressUpdate) { |
| 300 | if (auto callbacks = mCallbacks.lock()) { |
| 301 | callbacks->onProgressUpdate(this, progress); |
| 302 | } |
| 303 | lastProgressUpdate = progress; |
| 304 | } |
| 305 | } |
Linus Nilsson | a85df7f | 2020-02-20 16:32:04 -0800 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | return AMEDIA_OK; |
| 309 | } |
Chong Zhang | d6e4aec | 2020-06-22 14:13:07 -0700 | [diff] [blame] | 310 | } // namespace android |