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 | bbb4eac | 2020-11-18 11:12:06 -0800 | [diff] [blame] | 209 | MediaTranscoder::MediaTranscoder(const std::shared_ptr<CallbackInterface>& callbacks, pid_t pid, |
| 210 | uid_t uid) |
| 211 | : mCallbacks(callbacks), mPid(pid), mUid(uid) {} |
Chong Zhang | 308e91f | 2020-06-10 15:27:56 -0700 | [diff] [blame] | 212 | |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 213 | std::shared_ptr<MediaTranscoder> MediaTranscoder::create( |
Chong Zhang | bbb4eac | 2020-11-18 11:12:06 -0800 | [diff] [blame] | 214 | const std::shared_ptr<CallbackInterface>& callbacks, pid_t pid, uid_t uid, |
Chong Zhang | e4e088f | 2020-10-21 19:10:42 -0700 | [diff] [blame] | 215 | const std::shared_ptr<ndk::ScopedAParcel>& pausedState) { |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 216 | if (pausedState != nullptr) { |
Chong Zhang | b55c545 | 2020-06-26 14:32:12 -0700 | [diff] [blame] | 217 | LOG(INFO) << "Initializing from paused state."; |
| 218 | } |
| 219 | if (callbacks == nullptr) { |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 220 | LOG(ERROR) << "Callbacks cannot be null"; |
| 221 | return nullptr; |
| 222 | } |
| 223 | |
Chong Zhang | bbb4eac | 2020-11-18 11:12:06 -0800 | [diff] [blame] | 224 | return std::shared_ptr<MediaTranscoder>(new MediaTranscoder(callbacks, pid, uid)); |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 225 | } |
| 226 | |
Chong Zhang | 308e91f | 2020-06-10 15:27:56 -0700 | [diff] [blame] | 227 | media_status_t MediaTranscoder::configureSource(int fd) { |
| 228 | if (fd < 0) { |
| 229 | LOG(ERROR) << "Invalid source fd: " << fd; |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 230 | return AMEDIA_ERROR_INVALID_PARAMETER; |
| 231 | } |
| 232 | |
| 233 | const size_t fileSize = lseek(fd, 0, SEEK_END); |
| 234 | lseek(fd, 0, SEEK_SET); |
| 235 | |
| 236 | mSampleReader = MediaSampleReaderNDK::createFromFd(fd, 0 /* offset */, fileSize); |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 237 | |
| 238 | if (mSampleReader == nullptr) { |
Chong Zhang | 308e91f | 2020-06-10 15:27:56 -0700 | [diff] [blame] | 239 | LOG(ERROR) << "Unable to parse source fd: " << fd; |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 240 | return AMEDIA_ERROR_UNSUPPORTED; |
| 241 | } |
| 242 | |
| 243 | const size_t trackCount = mSampleReader->getTrackCount(); |
| 244 | for (size_t trackIndex = 0; trackIndex < trackCount; ++trackIndex) { |
| 245 | AMediaFormat* trackFormat = mSampleReader->getTrackFormat(static_cast<int>(trackIndex)); |
| 246 | if (trackFormat == nullptr) { |
| 247 | LOG(ERROR) << "Track #" << trackIndex << " has no format"; |
| 248 | return AMEDIA_ERROR_MALFORMED; |
| 249 | } |
| 250 | |
| 251 | mSourceTrackFormats.emplace_back(trackFormat, &AMediaFormat_delete); |
| 252 | } |
| 253 | |
| 254 | return AMEDIA_OK; |
| 255 | } |
| 256 | |
| 257 | std::vector<std::shared_ptr<AMediaFormat>> MediaTranscoder::getTrackFormats() const { |
| 258 | // Return a deep copy of the formats to avoid the caller modifying our internal formats. |
| 259 | std::vector<std::shared_ptr<AMediaFormat>> trackFormats; |
| 260 | for (const std::shared_ptr<AMediaFormat>& sourceFormat : mSourceTrackFormats) { |
| 261 | AMediaFormat* copy = AMediaFormat_new(); |
| 262 | AMediaFormat_copy(copy, sourceFormat.get()); |
| 263 | trackFormats.emplace_back(copy, &AMediaFormat_delete); |
| 264 | } |
| 265 | return trackFormats; |
| 266 | } |
| 267 | |
| 268 | media_status_t MediaTranscoder::configureTrackFormat(size_t trackIndex, AMediaFormat* trackFormat) { |
| 269 | if (mSampleReader == nullptr) { |
| 270 | LOG(ERROR) << "Source must be configured before tracks"; |
| 271 | return AMEDIA_ERROR_INVALID_OPERATION; |
| 272 | } else if (trackIndex >= mSourceTrackFormats.size()) { |
| 273 | LOG(ERROR) << "Track index " << trackIndex |
| 274 | << " is out of bounds. Track count: " << mSourceTrackFormats.size(); |
| 275 | return AMEDIA_ERROR_INVALID_PARAMETER; |
| 276 | } |
| 277 | |
Linus Nilsson | 6233fed | 2020-08-13 15:15:14 -0700 | [diff] [blame] | 278 | media_status_t status = mSampleReader->selectTrack(trackIndex); |
| 279 | if (status != AMEDIA_OK) { |
| 280 | LOG(ERROR) << "Unable to select track " << trackIndex; |
| 281 | return status; |
| 282 | } |
| 283 | |
Linus Nilsson | e4716f2 | 2020-07-10 16:07:57 -0700 | [diff] [blame] | 284 | std::shared_ptr<MediaTrackTranscoder> transcoder; |
| 285 | std::shared_ptr<AMediaFormat> format; |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 286 | |
| 287 | if (trackFormat == nullptr) { |
Linus Nilsson | e4716f2 | 2020-07-10 16:07:57 -0700 | [diff] [blame] | 288 | transcoder = std::make_shared<PassthroughTrackTranscoder>(shared_from_this()); |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 289 | } else { |
| 290 | const char* srcMime = nullptr; |
| 291 | if (!AMediaFormat_getString(mSourceTrackFormats[trackIndex].get(), AMEDIAFORMAT_KEY_MIME, |
| 292 | &srcMime)) { |
| 293 | LOG(ERROR) << "Source track #" << trackIndex << " has no mime type"; |
| 294 | return AMEDIA_ERROR_MALFORMED; |
| 295 | } |
| 296 | |
| 297 | if (strncmp(srcMime, "video/", 6) != 0) { |
| 298 | LOG(ERROR) << "Only video tracks are supported for transcoding. Unable to configure " |
| 299 | "track #" |
| 300 | << trackIndex << " with mime " << srcMime; |
| 301 | return AMEDIA_ERROR_UNSUPPORTED; |
| 302 | } |
| 303 | |
| 304 | const char* dstMime = nullptr; |
| 305 | if (AMediaFormat_getString(trackFormat, AMEDIAFORMAT_KEY_MIME, &dstMime)) { |
| 306 | if (strncmp(dstMime, "video/", 6) != 0) { |
| 307 | LOG(ERROR) << "Unable to convert media types for track #" << trackIndex << ", from " |
| 308 | << srcMime << " to " << dstMime; |
| 309 | return AMEDIA_ERROR_UNSUPPORTED; |
| 310 | } |
| 311 | } |
| 312 | |
Chong Zhang | bbb4eac | 2020-11-18 11:12:06 -0800 | [diff] [blame] | 313 | transcoder = VideoTrackTranscoder::create(shared_from_this(), mPid, mUid); |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 314 | |
| 315 | AMediaFormat* mergedFormat = |
| 316 | mergeMediaFormats(mSourceTrackFormats[trackIndex].get(), trackFormat); |
| 317 | if (mergedFormat == nullptr) { |
| 318 | LOG(ERROR) << "Unable to merge source and destination formats"; |
| 319 | return AMEDIA_ERROR_UNKNOWN; |
| 320 | } |
| 321 | |
| 322 | format = std::shared_ptr<AMediaFormat>(mergedFormat, &AMediaFormat_delete); |
| 323 | } |
| 324 | |
Chong Zhang | c8c88cc | 2020-09-17 12:50:49 -0700 | [diff] [blame] | 325 | status = transcoder->configure(mSampleReader, trackIndex, format); |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 326 | if (status != AMEDIA_OK) { |
| 327 | LOG(ERROR) << "Configure track transcoder for track #" << trackIndex << " returned error " |
| 328 | << status; |
| 329 | return status; |
| 330 | } |
| 331 | |
Linus Nilsson | fdb3e33 | 2020-09-18 17:11:41 -0700 | [diff] [blame] | 332 | std::scoped_lock lock{mThreadStateMutex}; |
| 333 | mThreadStates[static_cast<const void*>(transcoder.get())] = PENDING; |
| 334 | |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 335 | mTrackTranscoders.emplace_back(std::move(transcoder)); |
| 336 | return AMEDIA_OK; |
| 337 | } |
| 338 | |
Chong Zhang | 308e91f | 2020-06-10 15:27:56 -0700 | [diff] [blame] | 339 | media_status_t MediaTranscoder::configureDestination(int fd) { |
| 340 | if (fd < 0) { |
| 341 | LOG(ERROR) << "Invalid destination fd: " << fd; |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 342 | return AMEDIA_ERROR_INVALID_PARAMETER; |
Chong Zhang | 308e91f | 2020-06-10 15:27:56 -0700 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | if (mSampleWriter != nullptr) { |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 346 | LOG(ERROR) << "Destination is already configured."; |
| 347 | return AMEDIA_ERROR_INVALID_OPERATION; |
| 348 | } |
| 349 | |
Linus Nilsson | c31d249 | 2020-09-23 12:30:00 -0700 | [diff] [blame] | 350 | mSampleWriter = MediaSampleWriter::Create(); |
Linus Nilsson | e2cdd1f | 2020-07-07 17:29:26 -0700 | [diff] [blame] | 351 | const bool initOk = mSampleWriter->init(fd, shared_from_this()); |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 352 | |
| 353 | if (!initOk) { |
Chong Zhang | 308e91f | 2020-06-10 15:27:56 -0700 | [diff] [blame] | 354 | LOG(ERROR) << "Unable to initialize sample writer with destination fd: " << fd; |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 355 | mSampleWriter.reset(); |
| 356 | return AMEDIA_ERROR_UNKNOWN; |
| 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*>(mSampleWriter.get())] = PENDING; |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 361 | return AMEDIA_OK; |
| 362 | } |
| 363 | |
| 364 | media_status_t MediaTranscoder::start() { |
| 365 | if (mTrackTranscoders.size() < 1) { |
| 366 | LOG(ERROR) << "Unable to start, no tracks are configured."; |
| 367 | return AMEDIA_ERROR_INVALID_OPERATION; |
| 368 | } else if (mSampleWriter == nullptr) { |
| 369 | LOG(ERROR) << "Unable to start, destination is not configured"; |
| 370 | return AMEDIA_ERROR_INVALID_OPERATION; |
| 371 | } |
| 372 | |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 373 | // Start transcoders |
Linus Nilsson | fdb3e33 | 2020-09-18 17:11:41 -0700 | [diff] [blame] | 374 | bool started = true; |
| 375 | { |
| 376 | std::scoped_lock lock{mThreadStateMutex}; |
| 377 | for (auto& transcoder : mTrackTranscoders) { |
| 378 | if (!(started = transcoder->start())) { |
| 379 | break; |
| 380 | } |
| 381 | mThreadStates[static_cast<const void*>(transcoder.get())] = RUNNING; |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 382 | } |
| 383 | } |
Linus Nilsson | fdb3e33 | 2020-09-18 17:11:41 -0700 | [diff] [blame] | 384 | if (!started) { |
| 385 | LOG(ERROR) << "Unable to start track transcoder."; |
| 386 | cancel(); |
| 387 | return AMEDIA_ERROR_UNKNOWN; |
| 388 | } |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 389 | return AMEDIA_OK; |
| 390 | } |
| 391 | |
Linus Nilsson | fdb3e33 | 2020-09-18 17:11:41 -0700 | [diff] [blame] | 392 | media_status_t MediaTranscoder::requestStop(bool stopOnSync) { |
| 393 | std::scoped_lock lock{mThreadStateMutex}; |
| 394 | if (mCancelled) { |
| 395 | LOG(DEBUG) << "MediaTranscoder already cancelled"; |
| 396 | return AMEDIA_ERROR_UNSUPPORTED; |
| 397 | } |
| 398 | |
| 399 | if (!stopOnSync) { |
| 400 | mSampleWriterStopped = true; |
| 401 | mSampleWriter->stop(); |
| 402 | } |
| 403 | |
| 404 | mSampleReader->setEnforceSequentialAccess(false); |
| 405 | for (auto& transcoder : mTrackTranscoders) { |
| 406 | transcoder->stop(stopOnSync); |
| 407 | } |
| 408 | |
| 409 | mCancelled = true; |
| 410 | return AMEDIA_OK; |
| 411 | } |
| 412 | |
| 413 | void MediaTranscoder::waitForThreads() NO_THREAD_SAFETY_ANALYSIS { |
| 414 | std::unique_lock lock{mThreadStateMutex}; |
| 415 | while (!mThreadsDone) { |
| 416 | mThreadsDoneSignal.wait(lock); |
| 417 | } |
| 418 | } |
| 419 | |
Chong Zhang | e4e088f | 2020-10-21 19:10:42 -0700 | [diff] [blame] | 420 | media_status_t MediaTranscoder::pause(std::shared_ptr<ndk::ScopedAParcel>* pausedState) { |
Linus Nilsson | fdb3e33 | 2020-09-18 17:11:41 -0700 | [diff] [blame] | 421 | media_status_t status = requestStop(true /* stopOnSync */); |
| 422 | if (status != AMEDIA_OK) { |
| 423 | return status; |
| 424 | } |
| 425 | |
| 426 | waitForThreads(); |
| 427 | |
Chong Zhang | b55c545 | 2020-06-26 14:32:12 -0700 | [diff] [blame] | 428 | // TODO: write internal states to parcel. |
Chong Zhang | e4e088f | 2020-10-21 19:10:42 -0700 | [diff] [blame] | 429 | *pausedState = std::shared_ptr<::ndk::ScopedAParcel>(new ::ndk::ScopedAParcel()); |
Linus Nilsson | fdb3e33 | 2020-09-18 17:11:41 -0700 | [diff] [blame] | 430 | return AMEDIA_OK; |
| 431 | } |
| 432 | |
| 433 | media_status_t MediaTranscoder::cancel() { |
| 434 | media_status_t status = requestStop(false /* stopOnSync */); |
| 435 | if (status != AMEDIA_OK) { |
| 436 | return status; |
| 437 | } |
| 438 | |
| 439 | waitForThreads(); |
| 440 | |
| 441 | // TODO: Release transcoders? |
| 442 | return AMEDIA_OK; |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | media_status_t MediaTranscoder::resume() { |
Chong Zhang | b55c545 | 2020-06-26 14:32:12 -0700 | [diff] [blame] | 446 | // TODO: restore internal states from parcel. |
| 447 | return start(); |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 448 | } |
| 449 | |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 450 | } // namespace android |