Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [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 "MediaTranscoder" |
| 19 | |
| 20 | #include <android-base/logging.h> |
| 21 | #include <fcntl.h> |
| 22 | #include <media/MediaSampleReaderNDK.h> |
Chong Zhang | 308e91f | 2020-06-10 15:27:56 -0700 | [diff] [blame] | 23 | #include <media/MediaSampleWriter.h> |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 24 | #include <media/MediaTranscoder.h> |
Linus Nilsson | 16d772b | 2020-09-29 19:21:11 -0700 | [diff] [blame] | 25 | #include <media/NdkCommon.h> |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 26 | #include <media/PassthroughTrackTranscoder.h> |
| 27 | #include <media/VideoTrackTranscoder.h> |
| 28 | #include <unistd.h> |
| 29 | |
| 30 | namespace android { |
| 31 | |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 32 | static AMediaFormat* mergeMediaFormats(AMediaFormat* base, AMediaFormat* overlay) { |
| 33 | if (base == nullptr || overlay == nullptr) { |
| 34 | LOG(ERROR) << "Cannot merge null formats"; |
| 35 | return nullptr; |
| 36 | } |
| 37 | |
| 38 | AMediaFormat* format = AMediaFormat_new(); |
| 39 | if (AMediaFormat_copy(format, base) != AMEDIA_OK) { |
| 40 | AMediaFormat_delete(format); |
| 41 | return nullptr; |
| 42 | } |
| 43 | |
| 44 | // Note: AMediaFormat does not expose a function for appending values from another format or for |
| 45 | // iterating over all values and keys in a format. Instead we define a static list of known keys |
| 46 | // along with their value types and copy the ones that are present. A better solution would be |
| 47 | // to either implement required functions in NDK or to parse the overlay format's string |
| 48 | // representation and copy all existing keys. |
Linus Nilsson | 16d772b | 2020-09-29 19:21:11 -0700 | [diff] [blame] | 49 | static const AMediaFormatUtils::EntryCopier kSupportedFormatEntries[] = { |
| 50 | ENTRY_COPIER(AMEDIAFORMAT_KEY_MIME, String), |
| 51 | ENTRY_COPIER(AMEDIAFORMAT_KEY_DURATION, Int64), |
| 52 | ENTRY_COPIER(AMEDIAFORMAT_KEY_WIDTH, Int32), |
| 53 | ENTRY_COPIER(AMEDIAFORMAT_KEY_HEIGHT, Int32), |
| 54 | ENTRY_COPIER(AMEDIAFORMAT_KEY_BIT_RATE, Int32), |
| 55 | ENTRY_COPIER(AMEDIAFORMAT_KEY_PROFILE, Int32), |
| 56 | ENTRY_COPIER(AMEDIAFORMAT_KEY_LEVEL, Int32), |
| 57 | ENTRY_COPIER(AMEDIAFORMAT_KEY_COLOR_FORMAT, Int32), |
| 58 | ENTRY_COPIER(AMEDIAFORMAT_KEY_COLOR_RANGE, Int32), |
| 59 | ENTRY_COPIER(AMEDIAFORMAT_KEY_COLOR_STANDARD, Int32), |
| 60 | ENTRY_COPIER(AMEDIAFORMAT_KEY_COLOR_TRANSFER, Int32), |
| 61 | ENTRY_COPIER(AMEDIAFORMAT_KEY_FRAME_RATE, Int32), |
| 62 | ENTRY_COPIER(AMEDIAFORMAT_KEY_I_FRAME_INTERVAL, Int32), |
| 63 | ENTRY_COPIER(AMEDIAFORMAT_KEY_PRIORITY, Int32), |
| 64 | ENTRY_COPIER2(AMEDIAFORMAT_KEY_OPERATING_RATE, Float, Int32), |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 65 | }; |
Linus Nilsson | 16d772b | 2020-09-29 19:21:11 -0700 | [diff] [blame] | 66 | const size_t entryCount = sizeof(kSupportedFormatEntries) / sizeof(kSupportedFormatEntries[0]); |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 67 | |
Linus Nilsson | 16d772b | 2020-09-29 19:21:11 -0700 | [diff] [blame] | 68 | AMediaFormatUtils::CopyFormatEntries(overlay, format, kSupportedFormatEntries, entryCount); |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 69 | return format; |
| 70 | } |
| 71 | |
Linus Nilsson | fdb3e33 | 2020-09-18 17:11:41 -0700 | [diff] [blame] | 72 | void MediaTranscoder::onThreadFinished(const void* thread, media_status_t threadStatus, |
| 73 | bool threadStopped) { |
| 74 | LOG(DEBUG) << "Thread " << thread << " finished with status " << threadStatus << " stopped " |
| 75 | << threadStopped; |
| 76 | |
| 77 | // Stop all threads if one reports an error. |
| 78 | if (threadStatus != AMEDIA_OK) { |
| 79 | requestStop(false /* stopOnSync */); |
| 80 | } |
| 81 | |
| 82 | std::scoped_lock lock{mThreadStateMutex}; |
| 83 | |
| 84 | // Record the change. |
| 85 | mThreadStates[thread] = DONE; |
| 86 | if (threadStatus != AMEDIA_OK && mTranscoderStatus == AMEDIA_OK) { |
| 87 | mTranscoderStatus = threadStatus; |
| 88 | } |
| 89 | |
| 90 | mTranscoderStopped |= threadStopped; |
| 91 | |
| 92 | // Check if all threads are done. Note that if all transcoders have stopped but the sample |
| 93 | // writer has not yet started, it never will. |
| 94 | bool transcodersDone = true; |
| 95 | ThreadState sampleWriterState = PENDING; |
| 96 | for (const auto& it : mThreadStates) { |
| 97 | LOG(DEBUG) << " Thread " << it.first << " state" << it.second; |
| 98 | if (it.first == static_cast<const void*>(mSampleWriter.get())) { |
| 99 | sampleWriterState = it.second; |
| 100 | } else { |
| 101 | transcodersDone &= (it.second == DONE); |
| 102 | } |
| 103 | } |
| 104 | if (!transcodersDone || sampleWriterState == RUNNING) { |
Chong Zhang | b55c545 | 2020-06-26 14:32:12 -0700 | [diff] [blame] | 105 | return; |
| 106 | } |
| 107 | |
Linus Nilsson | fdb3e33 | 2020-09-18 17:11:41 -0700 | [diff] [blame] | 108 | // All done. Send callback asynchronously and wake up threads waiting in cancel/pause. |
| 109 | mThreadsDone = true; |
| 110 | if (!mCallbackSent) { |
| 111 | std::thread asyncNotificationThread{[this, self = shared_from_this(), |
| 112 | status = mTranscoderStatus, |
| 113 | stopped = mTranscoderStopped] { |
| 114 | // If the transcoder was stopped that means a caller is waiting in stop or pause |
| 115 | // in which case we don't send a callback. |
| 116 | if (status != AMEDIA_OK) { |
| 117 | mCallbacks->onError(this, status); |
| 118 | } else if (!stopped) { |
| 119 | mCallbacks->onFinished(this); |
| 120 | } |
| 121 | mThreadsDoneSignal.notify_all(); |
| 122 | }}; |
| 123 | asyncNotificationThread.detach(); |
| 124 | mCallbackSent = true; |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | |
Chong Zhang | a2cc86b | 2020-06-17 16:56:49 -0700 | [diff] [blame] | 128 | void MediaTranscoder::onTrackFormatAvailable(const MediaTrackTranscoder* transcoder) { |
Linus Nilsson | fdb3e33 | 2020-09-18 17:11:41 -0700 | [diff] [blame] | 129 | LOG(DEBUG) << "TrackTranscoder " << transcoder << " format available."; |
Chong Zhang | a2cc86b | 2020-06-17 16:56:49 -0700 | [diff] [blame] | 130 | |
| 131 | std::scoped_lock lock{mTracksAddedMutex}; |
Linus Nilsson | fdb3e33 | 2020-09-18 17:11:41 -0700 | [diff] [blame] | 132 | const void* sampleWriterPtr = static_cast<const void*>(mSampleWriter.get()); |
Chong Zhang | a2cc86b | 2020-06-17 16:56:49 -0700 | [diff] [blame] | 133 | |
| 134 | // Ignore duplicate format change. |
| 135 | if (mTracksAdded.count(transcoder) > 0) { |
| 136 | return; |
| 137 | } |
| 138 | |
| 139 | // Add track to the writer. |
Linus Nilsson | c31d249 | 2020-09-23 12:30:00 -0700 | [diff] [blame] | 140 | auto consumer = mSampleWriter->addTrack(transcoder->getOutputFormat()); |
| 141 | if (consumer == nullptr) { |
Chong Zhang | a2cc86b | 2020-06-17 16:56:49 -0700 | [diff] [blame] | 142 | LOG(ERROR) << "Unable to add track to sample writer."; |
Linus Nilsson | fdb3e33 | 2020-09-18 17:11:41 -0700 | [diff] [blame] | 143 | onThreadFinished(sampleWriterPtr, AMEDIA_ERROR_UNKNOWN, false /* stopped */); |
Chong Zhang | a2cc86b | 2020-06-17 16:56:49 -0700 | [diff] [blame] | 144 | return; |
| 145 | } |
| 146 | |
Linus Nilsson | c31d249 | 2020-09-23 12:30:00 -0700 | [diff] [blame] | 147 | MediaTrackTranscoder* mutableTranscoder = const_cast<MediaTrackTranscoder*>(transcoder); |
| 148 | mutableTranscoder->setSampleConsumer(consumer); |
| 149 | |
Chong Zhang | a2cc86b | 2020-06-17 16:56:49 -0700 | [diff] [blame] | 150 | mTracksAdded.insert(transcoder); |
Linus Nilsson | fdb3e33 | 2020-09-18 17:11:41 -0700 | [diff] [blame] | 151 | bool errorStarting = false; |
Chong Zhang | a2cc86b | 2020-06-17 16:56:49 -0700 | [diff] [blame] | 152 | if (mTracksAdded.size() == mTrackTranscoders.size()) { |
Linus Nilsson | 6233fed | 2020-08-13 15:15:14 -0700 | [diff] [blame] | 153 | // Enable sequential access mode on the sample reader to achieve optimal read performance. |
| 154 | // This has to wait until all tracks have delivered their output formats and the sample |
| 155 | // writer is started. Otherwise the tracks will not get their output sample queues drained |
| 156 | // and the transcoder could hang due to one track running out of buffers and blocking the |
| 157 | // other tracks from reading source samples before they could output their formats. |
Linus Nilsson | fdb3e33 | 2020-09-18 17:11:41 -0700 | [diff] [blame] | 158 | |
| 159 | std::scoped_lock lock{mThreadStateMutex}; |
| 160 | // Don't start the sample writer if a stop already has been requested. |
| 161 | if (!mSampleWriterStopped) { |
| 162 | if (!mCancelled) { |
| 163 | mSampleReader->setEnforceSequentialAccess(true); |
| 164 | } |
| 165 | LOG(DEBUG) << "Starting sample writer."; |
| 166 | errorStarting = !mSampleWriter->start(); |
| 167 | if (!errorStarting) { |
| 168 | mThreadStates[sampleWriterPtr] = RUNNING; |
| 169 | } |
Chong Zhang | a2cc86b | 2020-06-17 16:56:49 -0700 | [diff] [blame] | 170 | } |
| 171 | } |
Linus Nilsson | fdb3e33 | 2020-09-18 17:11:41 -0700 | [diff] [blame] | 172 | |
| 173 | if (errorStarting) { |
| 174 | LOG(ERROR) << "Unable to start sample writer."; |
| 175 | onThreadFinished(sampleWriterPtr, AMEDIA_ERROR_UNKNOWN, false /* stopped */); |
| 176 | } |
Chong Zhang | a2cc86b | 2020-06-17 16:56:49 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 179 | void MediaTranscoder::onTrackFinished(const MediaTrackTranscoder* transcoder) { |
| 180 | LOG(DEBUG) << "TrackTranscoder " << transcoder << " finished"; |
Linus Nilsson | fdb3e33 | 2020-09-18 17:11:41 -0700 | [diff] [blame] | 181 | onThreadFinished(static_cast<const void*>(transcoder), AMEDIA_OK, false /* stopped */); |
| 182 | } |
| 183 | |
| 184 | void MediaTranscoder::onTrackStopped(const MediaTrackTranscoder* transcoder) { |
| 185 | LOG(DEBUG) << "TrackTranscoder " << transcoder << " stopped"; |
| 186 | onThreadFinished(static_cast<const void*>(transcoder), AMEDIA_OK, true /* stopped */); |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | void MediaTranscoder::onTrackError(const MediaTrackTranscoder* transcoder, media_status_t status) { |
Linus Nilsson | 93cf913 | 2020-09-24 12:12:48 -0700 | [diff] [blame] | 190 | LOG(ERROR) << "TrackTranscoder " << transcoder << " returned error " << status; |
Linus Nilsson | fdb3e33 | 2020-09-18 17:11:41 -0700 | [diff] [blame] | 191 | onThreadFinished(static_cast<const void*>(transcoder), status, false /* stopped */); |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Linus Nilsson | fdb3e33 | 2020-09-18 17:11:41 -0700 | [diff] [blame] | 194 | void MediaTranscoder::onFinished(const MediaSampleWriter* writer, media_status_t status) { |
| 195 | LOG(status == AMEDIA_OK ? DEBUG : ERROR) << "Sample writer finished with status " << status; |
| 196 | onThreadFinished(static_cast<const void*>(writer), status, false /* stopped */); |
| 197 | } |
| 198 | |
| 199 | void MediaTranscoder::onStopped(const MediaSampleWriter* writer) { |
| 200 | LOG(DEBUG) << "Sample writer " << writer << " stopped"; |
| 201 | onThreadFinished(static_cast<const void*>(writer), AMEDIA_OK, true /* stopped */); |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 202 | } |
| 203 | |
Linus Nilsson | e2cdd1f | 2020-07-07 17:29:26 -0700 | [diff] [blame] | 204 | void MediaTranscoder::onProgressUpdate(const MediaSampleWriter* writer __unused, int32_t progress) { |
| 205 | // Dispatch progress updated to the client. |
| 206 | mCallbacks->onProgressUpdate(this, progress); |
| 207 | } |
| 208 | |
Chong Zhang | 457c689 | 2021-02-01 15:34:20 -0800 | [diff] [blame] | 209 | void MediaTranscoder::onHeartBeat(const MediaSampleWriter* writer __unused) { |
| 210 | // Signal heart-beat to the client. |
| 211 | mCallbacks->onHeartBeat(this); |
| 212 | } |
| 213 | |
| 214 | MediaTranscoder::MediaTranscoder(const std::shared_ptr<CallbackInterface>& callbacks, |
| 215 | int64_t heartBeatIntervalUs, pid_t pid, uid_t uid) |
| 216 | : mCallbacks(callbacks), mHeartBeatIntervalUs(heartBeatIntervalUs), mPid(pid), mUid(uid) {} |
Chong Zhang | 308e91f | 2020-06-10 15:27:56 -0700 | [diff] [blame] | 217 | |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 218 | std::shared_ptr<MediaTranscoder> MediaTranscoder::create( |
Chong Zhang | 457c689 | 2021-02-01 15:34:20 -0800 | [diff] [blame] | 219 | const std::shared_ptr<CallbackInterface>& callbacks, int64_t heartBeatIntervalUs, pid_t pid, |
| 220 | uid_t uid, const std::shared_ptr<ndk::ScopedAParcel>& pausedState) { |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 221 | if (pausedState != nullptr) { |
Chong Zhang | b55c545 | 2020-06-26 14:32:12 -0700 | [diff] [blame] | 222 | LOG(INFO) << "Initializing from paused state."; |
| 223 | } |
| 224 | if (callbacks == nullptr) { |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 225 | LOG(ERROR) << "Callbacks cannot be null"; |
| 226 | return nullptr; |
| 227 | } |
| 228 | |
Chong Zhang | 457c689 | 2021-02-01 15:34:20 -0800 | [diff] [blame] | 229 | return std::shared_ptr<MediaTranscoder>( |
| 230 | new MediaTranscoder(callbacks, heartBeatIntervalUs, pid, uid)); |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 231 | } |
| 232 | |
Chong Zhang | 308e91f | 2020-06-10 15:27:56 -0700 | [diff] [blame] | 233 | media_status_t MediaTranscoder::configureSource(int fd) { |
| 234 | if (fd < 0) { |
| 235 | LOG(ERROR) << "Invalid source fd: " << fd; |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 236 | return AMEDIA_ERROR_INVALID_PARAMETER; |
| 237 | } |
| 238 | |
| 239 | const size_t fileSize = lseek(fd, 0, SEEK_END); |
| 240 | lseek(fd, 0, SEEK_SET); |
| 241 | |
| 242 | mSampleReader = MediaSampleReaderNDK::createFromFd(fd, 0 /* offset */, fileSize); |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 243 | if (mSampleReader == nullptr) { |
Chong Zhang | 308e91f | 2020-06-10 15:27:56 -0700 | [diff] [blame] | 244 | LOG(ERROR) << "Unable to parse source fd: " << fd; |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 245 | return AMEDIA_ERROR_UNSUPPORTED; |
| 246 | } |
| 247 | |
| 248 | const size_t trackCount = mSampleReader->getTrackCount(); |
| 249 | for (size_t trackIndex = 0; trackIndex < trackCount; ++trackIndex) { |
| 250 | AMediaFormat* trackFormat = mSampleReader->getTrackFormat(static_cast<int>(trackIndex)); |
| 251 | if (trackFormat == nullptr) { |
| 252 | LOG(ERROR) << "Track #" << trackIndex << " has no format"; |
| 253 | return AMEDIA_ERROR_MALFORMED; |
| 254 | } |
| 255 | |
| 256 | mSourceTrackFormats.emplace_back(trackFormat, &AMediaFormat_delete); |
| 257 | } |
| 258 | |
| 259 | return AMEDIA_OK; |
| 260 | } |
| 261 | |
| 262 | std::vector<std::shared_ptr<AMediaFormat>> MediaTranscoder::getTrackFormats() const { |
| 263 | // Return a deep copy of the formats to avoid the caller modifying our internal formats. |
| 264 | std::vector<std::shared_ptr<AMediaFormat>> trackFormats; |
| 265 | for (const std::shared_ptr<AMediaFormat>& sourceFormat : mSourceTrackFormats) { |
| 266 | AMediaFormat* copy = AMediaFormat_new(); |
| 267 | AMediaFormat_copy(copy, sourceFormat.get()); |
| 268 | trackFormats.emplace_back(copy, &AMediaFormat_delete); |
| 269 | } |
| 270 | return trackFormats; |
| 271 | } |
| 272 | |
| 273 | media_status_t MediaTranscoder::configureTrackFormat(size_t trackIndex, AMediaFormat* trackFormat) { |
| 274 | if (mSampleReader == nullptr) { |
| 275 | LOG(ERROR) << "Source must be configured before tracks"; |
| 276 | return AMEDIA_ERROR_INVALID_OPERATION; |
| 277 | } else if (trackIndex >= mSourceTrackFormats.size()) { |
| 278 | LOG(ERROR) << "Track index " << trackIndex |
| 279 | << " is out of bounds. Track count: " << mSourceTrackFormats.size(); |
| 280 | return AMEDIA_ERROR_INVALID_PARAMETER; |
| 281 | } |
| 282 | |
Linus Nilsson | e4716f2 | 2020-07-10 16:07:57 -0700 | [diff] [blame] | 283 | std::shared_ptr<MediaTrackTranscoder> transcoder; |
| 284 | std::shared_ptr<AMediaFormat> format; |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 285 | |
| 286 | if (trackFormat == nullptr) { |
Linus Nilsson | e4716f2 | 2020-07-10 16:07:57 -0700 | [diff] [blame] | 287 | transcoder = std::make_shared<PassthroughTrackTranscoder>(shared_from_this()); |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 288 | } else { |
| 289 | const char* srcMime = nullptr; |
| 290 | if (!AMediaFormat_getString(mSourceTrackFormats[trackIndex].get(), AMEDIAFORMAT_KEY_MIME, |
| 291 | &srcMime)) { |
| 292 | LOG(ERROR) << "Source track #" << trackIndex << " has no mime type"; |
| 293 | return AMEDIA_ERROR_MALFORMED; |
| 294 | } |
| 295 | |
| 296 | if (strncmp(srcMime, "video/", 6) != 0) { |
| 297 | LOG(ERROR) << "Only video tracks are supported for transcoding. Unable to configure " |
| 298 | "track #" |
| 299 | << trackIndex << " with mime " << srcMime; |
| 300 | return AMEDIA_ERROR_UNSUPPORTED; |
| 301 | } |
| 302 | |
| 303 | const char* dstMime = nullptr; |
| 304 | if (AMediaFormat_getString(trackFormat, AMEDIAFORMAT_KEY_MIME, &dstMime)) { |
| 305 | if (strncmp(dstMime, "video/", 6) != 0) { |
| 306 | LOG(ERROR) << "Unable to convert media types for track #" << trackIndex << ", from " |
| 307 | << srcMime << " to " << dstMime; |
| 308 | return AMEDIA_ERROR_UNSUPPORTED; |
| 309 | } |
| 310 | } |
| 311 | |
Chong Zhang | bbb4eac | 2020-11-18 11:12:06 -0800 | [diff] [blame] | 312 | transcoder = VideoTrackTranscoder::create(shared_from_this(), mPid, mUid); |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 313 | |
| 314 | AMediaFormat* mergedFormat = |
| 315 | mergeMediaFormats(mSourceTrackFormats[trackIndex].get(), trackFormat); |
| 316 | if (mergedFormat == nullptr) { |
| 317 | LOG(ERROR) << "Unable to merge source and destination formats"; |
| 318 | return AMEDIA_ERROR_UNKNOWN; |
| 319 | } |
| 320 | |
| 321 | format = std::shared_ptr<AMediaFormat>(mergedFormat, &AMediaFormat_delete); |
| 322 | } |
| 323 | |
Linus Nilsson | af4a321 | 2020-12-15 08:18:25 -0800 | [diff] [blame] | 324 | media_status_t status = mSampleReader->selectTrack(trackIndex); |
| 325 | if (status != AMEDIA_OK) { |
| 326 | LOG(ERROR) << "Unable to select track " << trackIndex; |
| 327 | return status; |
| 328 | } |
| 329 | |
Chong Zhang | c8c88cc | 2020-09-17 12:50:49 -0700 | [diff] [blame] | 330 | status = transcoder->configure(mSampleReader, trackIndex, format); |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 331 | if (status != AMEDIA_OK) { |
| 332 | LOG(ERROR) << "Configure track transcoder for track #" << trackIndex << " returned error " |
| 333 | << status; |
Linus Nilsson | af4a321 | 2020-12-15 08:18:25 -0800 | [diff] [blame] | 334 | mSampleReader->unselectTrack(trackIndex); |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 335 | return status; |
| 336 | } |
| 337 | |
Linus Nilsson | fdb3e33 | 2020-09-18 17:11:41 -0700 | [diff] [blame] | 338 | std::scoped_lock lock{mThreadStateMutex}; |
| 339 | mThreadStates[static_cast<const void*>(transcoder.get())] = PENDING; |
| 340 | |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 341 | mTrackTranscoders.emplace_back(std::move(transcoder)); |
| 342 | return AMEDIA_OK; |
| 343 | } |
| 344 | |
Chong Zhang | 308e91f | 2020-06-10 15:27:56 -0700 | [diff] [blame] | 345 | media_status_t MediaTranscoder::configureDestination(int fd) { |
| 346 | if (fd < 0) { |
| 347 | LOG(ERROR) << "Invalid destination fd: " << fd; |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 348 | return AMEDIA_ERROR_INVALID_PARAMETER; |
Chong Zhang | 308e91f | 2020-06-10 15:27:56 -0700 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | if (mSampleWriter != nullptr) { |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 352 | LOG(ERROR) << "Destination is already configured."; |
| 353 | return AMEDIA_ERROR_INVALID_OPERATION; |
| 354 | } |
| 355 | |
Linus Nilsson | c31d249 | 2020-09-23 12:30:00 -0700 | [diff] [blame] | 356 | mSampleWriter = MediaSampleWriter::Create(); |
Chong Zhang | 457c689 | 2021-02-01 15:34:20 -0800 | [diff] [blame] | 357 | const bool initOk = mSampleWriter->init(fd, shared_from_this(), mHeartBeatIntervalUs); |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 358 | |
| 359 | if (!initOk) { |
Chong Zhang | 308e91f | 2020-06-10 15:27:56 -0700 | [diff] [blame] | 360 | LOG(ERROR) << "Unable to initialize sample writer with destination fd: " << fd; |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 361 | mSampleWriter.reset(); |
| 362 | return AMEDIA_ERROR_UNKNOWN; |
| 363 | } |
| 364 | |
Linus Nilsson | fdb3e33 | 2020-09-18 17:11:41 -0700 | [diff] [blame] | 365 | std::scoped_lock lock{mThreadStateMutex}; |
| 366 | mThreadStates[static_cast<const void*>(mSampleWriter.get())] = PENDING; |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 367 | return AMEDIA_OK; |
| 368 | } |
| 369 | |
| 370 | media_status_t MediaTranscoder::start() { |
| 371 | if (mTrackTranscoders.size() < 1) { |
| 372 | LOG(ERROR) << "Unable to start, no tracks are configured."; |
| 373 | return AMEDIA_ERROR_INVALID_OPERATION; |
| 374 | } else if (mSampleWriter == nullptr) { |
| 375 | LOG(ERROR) << "Unable to start, destination is not configured"; |
| 376 | return AMEDIA_ERROR_INVALID_OPERATION; |
| 377 | } |
| 378 | |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 379 | // Start transcoders |
Linus Nilsson | fdb3e33 | 2020-09-18 17:11:41 -0700 | [diff] [blame] | 380 | bool started = true; |
| 381 | { |
| 382 | std::scoped_lock lock{mThreadStateMutex}; |
| 383 | for (auto& transcoder : mTrackTranscoders) { |
| 384 | if (!(started = transcoder->start())) { |
| 385 | break; |
| 386 | } |
| 387 | mThreadStates[static_cast<const void*>(transcoder.get())] = RUNNING; |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 388 | } |
| 389 | } |
Linus Nilsson | fdb3e33 | 2020-09-18 17:11:41 -0700 | [diff] [blame] | 390 | if (!started) { |
| 391 | LOG(ERROR) << "Unable to start track transcoder."; |
| 392 | cancel(); |
| 393 | return AMEDIA_ERROR_UNKNOWN; |
| 394 | } |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 395 | return AMEDIA_OK; |
| 396 | } |
| 397 | |
Linus Nilsson | fdb3e33 | 2020-09-18 17:11:41 -0700 | [diff] [blame] | 398 | media_status_t MediaTranscoder::requestStop(bool stopOnSync) { |
| 399 | std::scoped_lock lock{mThreadStateMutex}; |
| 400 | if (mCancelled) { |
| 401 | LOG(DEBUG) << "MediaTranscoder already cancelled"; |
| 402 | return AMEDIA_ERROR_UNSUPPORTED; |
| 403 | } |
| 404 | |
| 405 | if (!stopOnSync) { |
| 406 | mSampleWriterStopped = true; |
| 407 | mSampleWriter->stop(); |
| 408 | } |
| 409 | |
| 410 | mSampleReader->setEnforceSequentialAccess(false); |
| 411 | for (auto& transcoder : mTrackTranscoders) { |
| 412 | transcoder->stop(stopOnSync); |
| 413 | } |
| 414 | |
| 415 | mCancelled = true; |
| 416 | return AMEDIA_OK; |
| 417 | } |
| 418 | |
| 419 | void MediaTranscoder::waitForThreads() NO_THREAD_SAFETY_ANALYSIS { |
| 420 | std::unique_lock lock{mThreadStateMutex}; |
| 421 | while (!mThreadsDone) { |
| 422 | mThreadsDoneSignal.wait(lock); |
| 423 | } |
| 424 | } |
| 425 | |
Chong Zhang | e4e088f | 2020-10-21 19:10:42 -0700 | [diff] [blame] | 426 | media_status_t MediaTranscoder::pause(std::shared_ptr<ndk::ScopedAParcel>* pausedState) { |
Linus Nilsson | fdb3e33 | 2020-09-18 17:11:41 -0700 | [diff] [blame] | 427 | media_status_t status = requestStop(true /* stopOnSync */); |
| 428 | if (status != AMEDIA_OK) { |
| 429 | return status; |
| 430 | } |
| 431 | |
| 432 | waitForThreads(); |
| 433 | |
Chong Zhang | b55c545 | 2020-06-26 14:32:12 -0700 | [diff] [blame] | 434 | // TODO: write internal states to parcel. |
Chong Zhang | e4e088f | 2020-10-21 19:10:42 -0700 | [diff] [blame] | 435 | *pausedState = std::shared_ptr<::ndk::ScopedAParcel>(new ::ndk::ScopedAParcel()); |
Linus Nilsson | fdb3e33 | 2020-09-18 17:11:41 -0700 | [diff] [blame] | 436 | return AMEDIA_OK; |
| 437 | } |
| 438 | |
| 439 | media_status_t MediaTranscoder::cancel() { |
| 440 | media_status_t status = requestStop(false /* stopOnSync */); |
| 441 | if (status != AMEDIA_OK) { |
| 442 | return status; |
| 443 | } |
| 444 | |
| 445 | waitForThreads(); |
| 446 | |
| 447 | // TODO: Release transcoders? |
| 448 | return AMEDIA_OK; |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 449 | } |
| 450 | |
| 451 | media_status_t MediaTranscoder::resume() { |
Chong Zhang | b55c545 | 2020-06-26 14:32:12 -0700 | [diff] [blame] | 452 | // TODO: restore internal states from parcel. |
| 453 | return start(); |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 454 | } |
| 455 | |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 456 | } // namespace android |