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