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> |
Chong Zhang | b55c545 | 2020-06-26 14:32:12 -0700 | [diff] [blame] | 21 | #include <binder/Parcel.h> |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 22 | #include <fcntl.h> |
| 23 | #include <media/MediaSampleReaderNDK.h> |
Chong Zhang | 308e91f | 2020-06-10 15:27:56 -0700 | [diff] [blame] | 24 | #include <media/MediaSampleWriter.h> |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 25 | #include <media/MediaTranscoder.h> |
| 26 | #include <media/PassthroughTrackTranscoder.h> |
| 27 | #include <media/VideoTrackTranscoder.h> |
| 28 | #include <unistd.h> |
| 29 | |
| 30 | namespace android { |
| 31 | |
| 32 | #define DEFINE_FORMAT_VALUE_COPY_FUNC(_type, _typeName) \ |
| 33 | static void copy##_typeName(const char* key, AMediaFormat* to, AMediaFormat* from) { \ |
| 34 | _type value; \ |
| 35 | if (AMediaFormat_get##_typeName(from, key, &value)) { \ |
| 36 | AMediaFormat_set##_typeName(to, key, value); \ |
| 37 | } \ |
| 38 | } |
| 39 | |
| 40 | DEFINE_FORMAT_VALUE_COPY_FUNC(const char*, String); |
| 41 | DEFINE_FORMAT_VALUE_COPY_FUNC(int64_t, Int64); |
| 42 | DEFINE_FORMAT_VALUE_COPY_FUNC(int32_t, Int32); |
| 43 | |
| 44 | static AMediaFormat* mergeMediaFormats(AMediaFormat* base, AMediaFormat* overlay) { |
| 45 | if (base == nullptr || overlay == nullptr) { |
| 46 | LOG(ERROR) << "Cannot merge null formats"; |
| 47 | return nullptr; |
| 48 | } |
| 49 | |
| 50 | AMediaFormat* format = AMediaFormat_new(); |
| 51 | if (AMediaFormat_copy(format, base) != AMEDIA_OK) { |
| 52 | AMediaFormat_delete(format); |
| 53 | return nullptr; |
| 54 | } |
| 55 | |
| 56 | // Note: AMediaFormat does not expose a function for appending values from another format or for |
| 57 | // iterating over all values and keys in a format. Instead we define a static list of known keys |
| 58 | // along with their value types and copy the ones that are present. A better solution would be |
| 59 | // to either implement required functions in NDK or to parse the overlay format's string |
| 60 | // representation and copy all existing keys. |
| 61 | static const struct { |
| 62 | const char* key; |
| 63 | void (*copyValue)(const char* key, AMediaFormat* to, AMediaFormat* from); |
| 64 | } kSupportedConfigs[] = { |
| 65 | {AMEDIAFORMAT_KEY_MIME, copyString}, |
| 66 | {AMEDIAFORMAT_KEY_DURATION, copyInt64}, |
| 67 | {AMEDIAFORMAT_KEY_WIDTH, copyInt32}, |
| 68 | {AMEDIAFORMAT_KEY_HEIGHT, copyInt32}, |
| 69 | {AMEDIAFORMAT_KEY_BIT_RATE, copyInt32}, |
| 70 | {AMEDIAFORMAT_KEY_PROFILE, copyInt32}, |
| 71 | {AMEDIAFORMAT_KEY_LEVEL, copyInt32}, |
| 72 | {AMEDIAFORMAT_KEY_COLOR_FORMAT, copyInt32}, |
| 73 | {AMEDIAFORMAT_KEY_COLOR_RANGE, copyInt32}, |
| 74 | {AMEDIAFORMAT_KEY_COLOR_STANDARD, copyInt32}, |
| 75 | {AMEDIAFORMAT_KEY_COLOR_TRANSFER, copyInt32}, |
| 76 | {AMEDIAFORMAT_KEY_FRAME_RATE, copyInt32}, |
| 77 | {AMEDIAFORMAT_KEY_I_FRAME_INTERVAL, copyInt32}, |
| 78 | }; |
| 79 | |
| 80 | for (int i = 0; i < (sizeof(kSupportedConfigs) / sizeof(kSupportedConfigs[0])); ++i) { |
| 81 | kSupportedConfigs[i].copyValue(kSupportedConfigs[i].key, format, overlay); |
| 82 | } |
| 83 | |
| 84 | return format; |
| 85 | } |
| 86 | |
| 87 | void MediaTranscoder::sendCallback(media_status_t status) { |
Chong Zhang | b55c545 | 2020-06-26 14:32:12 -0700 | [diff] [blame] | 88 | // If the transcoder is already cancelled explicitly, don't send any error callbacks. |
| 89 | // Tracks and sample writer will report errors for abort. However, currently we can't |
| 90 | // tell it apart from real errors. Ideally we still want to report real errors back |
| 91 | // to client, as there is a small chance that explicit abort and the real error come |
| 92 | // at around the same time, we should report that if abort has a specific error code. |
| 93 | // On the other hand, if the transcoder actually finished (status is AMEDIA_OK) at around |
| 94 | // the same time of the abort, we should still report the finish back to the client. |
| 95 | if (mCancelled && status != AMEDIA_OK) { |
| 96 | return; |
| 97 | } |
| 98 | |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 99 | bool expected = false; |
| 100 | if (mCallbackSent.compare_exchange_strong(expected, true)) { |
| 101 | if (status == AMEDIA_OK) { |
| 102 | mCallbacks->onFinished(this); |
| 103 | } else { |
| 104 | mCallbacks->onError(this, status); |
| 105 | } |
| 106 | |
| 107 | // Transcoding is done and the callback to the client has been sent, so tear down the |
Chong Zhang | 308e91f | 2020-06-10 15:27:56 -0700 | [diff] [blame] | 108 | // pipeline but do it asynchronously to avoid deadlocks. If an error occurred, client |
| 109 | // should clean up the file. |
Chong Zhang | a2cc86b | 2020-06-17 16:56:49 -0700 | [diff] [blame] | 110 | std::thread asyncCancelThread{[self = shared_from_this()] { self->cancel(); }}; |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 111 | asyncCancelThread.detach(); |
| 112 | } |
| 113 | } |
| 114 | |
Chong Zhang | a2cc86b | 2020-06-17 16:56:49 -0700 | [diff] [blame] | 115 | void MediaTranscoder::onTrackFormatAvailable(const MediaTrackTranscoder* transcoder) { |
| 116 | LOG(INFO) << "TrackTranscoder " << transcoder << " format available."; |
| 117 | |
| 118 | std::scoped_lock lock{mTracksAddedMutex}; |
| 119 | |
| 120 | // Ignore duplicate format change. |
| 121 | if (mTracksAdded.count(transcoder) > 0) { |
| 122 | return; |
| 123 | } |
| 124 | |
| 125 | // Add track to the writer. |
| 126 | const bool ok = |
| 127 | mSampleWriter->addTrack(transcoder->getOutputQueue(), transcoder->getOutputFormat()); |
| 128 | if (!ok) { |
| 129 | LOG(ERROR) << "Unable to add track to sample writer."; |
| 130 | sendCallback(AMEDIA_ERROR_UNKNOWN); |
| 131 | return; |
| 132 | } |
| 133 | |
| 134 | mTracksAdded.insert(transcoder); |
| 135 | if (mTracksAdded.size() == mTrackTranscoders.size()) { |
Linus Nilsson | 6233fed | 2020-08-13 15:15:14 -0700 | [diff] [blame] | 136 | // Enable sequential access mode on the sample reader to achieve optimal read performance. |
| 137 | // This has to wait until all tracks have delivered their output formats and the sample |
| 138 | // writer is started. Otherwise the tracks will not get their output sample queues drained |
| 139 | // and the transcoder could hang due to one track running out of buffers and blocking the |
| 140 | // other tracks from reading source samples before they could output their formats. |
| 141 | mSampleReader->setEnforceSequentialAccess(true); |
Chong Zhang | a2cc86b | 2020-06-17 16:56:49 -0700 | [diff] [blame] | 142 | LOG(INFO) << "Starting sample writer."; |
| 143 | bool started = mSampleWriter->start(); |
| 144 | if (!started) { |
| 145 | LOG(ERROR) << "Unable to start sample writer."; |
| 146 | sendCallback(AMEDIA_ERROR_UNKNOWN); |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 151 | void MediaTranscoder::onTrackFinished(const MediaTrackTranscoder* transcoder) { |
| 152 | LOG(DEBUG) << "TrackTranscoder " << transcoder << " finished"; |
| 153 | } |
| 154 | |
| 155 | void MediaTranscoder::onTrackError(const MediaTrackTranscoder* transcoder, media_status_t status) { |
| 156 | LOG(DEBUG) << "TrackTranscoder " << transcoder << " returned error " << status; |
| 157 | sendCallback(status); |
| 158 | } |
| 159 | |
Linus Nilsson | e2cdd1f | 2020-07-07 17:29:26 -0700 | [diff] [blame] | 160 | void MediaTranscoder::onFinished(const MediaSampleWriter* writer __unused, media_status_t status) { |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 161 | LOG((status != AMEDIA_OK) ? ERROR : DEBUG) << "Sample writer finished with status " << status; |
| 162 | sendCallback(status); |
| 163 | } |
| 164 | |
Linus Nilsson | e2cdd1f | 2020-07-07 17:29:26 -0700 | [diff] [blame] | 165 | void MediaTranscoder::onProgressUpdate(const MediaSampleWriter* writer __unused, int32_t progress) { |
| 166 | // Dispatch progress updated to the client. |
| 167 | mCallbacks->onProgressUpdate(this, progress); |
| 168 | } |
| 169 | |
Chong Zhang | 308e91f | 2020-06-10 15:27:56 -0700 | [diff] [blame] | 170 | MediaTranscoder::MediaTranscoder(const std::shared_ptr<CallbackInterface>& callbacks) |
Chong Zhang | a2cc86b | 2020-06-17 16:56:49 -0700 | [diff] [blame] | 171 | : mCallbacks(callbacks) {} |
Chong Zhang | 308e91f | 2020-06-10 15:27:56 -0700 | [diff] [blame] | 172 | |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 173 | std::shared_ptr<MediaTranscoder> MediaTranscoder::create( |
| 174 | const std::shared_ptr<CallbackInterface>& callbacks, |
Chong Zhang | b55c545 | 2020-06-26 14:32:12 -0700 | [diff] [blame] | 175 | const std::shared_ptr<const Parcel>& pausedState) { |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 176 | if (pausedState != nullptr) { |
Chong Zhang | b55c545 | 2020-06-26 14:32:12 -0700 | [diff] [blame] | 177 | LOG(INFO) << "Initializing from paused state."; |
| 178 | } |
| 179 | if (callbacks == nullptr) { |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 180 | LOG(ERROR) << "Callbacks cannot be null"; |
| 181 | return nullptr; |
| 182 | } |
| 183 | |
| 184 | return std::shared_ptr<MediaTranscoder>(new MediaTranscoder(callbacks)); |
| 185 | } |
| 186 | |
Chong Zhang | 308e91f | 2020-06-10 15:27:56 -0700 | [diff] [blame] | 187 | media_status_t MediaTranscoder::configureSource(int fd) { |
| 188 | if (fd < 0) { |
| 189 | LOG(ERROR) << "Invalid source fd: " << fd; |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 190 | return AMEDIA_ERROR_INVALID_PARAMETER; |
| 191 | } |
| 192 | |
| 193 | const size_t fileSize = lseek(fd, 0, SEEK_END); |
| 194 | lseek(fd, 0, SEEK_SET); |
| 195 | |
| 196 | mSampleReader = MediaSampleReaderNDK::createFromFd(fd, 0 /* offset */, fileSize); |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 197 | |
| 198 | if (mSampleReader == nullptr) { |
Chong Zhang | 308e91f | 2020-06-10 15:27:56 -0700 | [diff] [blame] | 199 | LOG(ERROR) << "Unable to parse source fd: " << fd; |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 200 | return AMEDIA_ERROR_UNSUPPORTED; |
| 201 | } |
| 202 | |
| 203 | const size_t trackCount = mSampleReader->getTrackCount(); |
| 204 | for (size_t trackIndex = 0; trackIndex < trackCount; ++trackIndex) { |
| 205 | AMediaFormat* trackFormat = mSampleReader->getTrackFormat(static_cast<int>(trackIndex)); |
| 206 | if (trackFormat == nullptr) { |
| 207 | LOG(ERROR) << "Track #" << trackIndex << " has no format"; |
| 208 | return AMEDIA_ERROR_MALFORMED; |
| 209 | } |
| 210 | |
| 211 | mSourceTrackFormats.emplace_back(trackFormat, &AMediaFormat_delete); |
| 212 | } |
| 213 | |
| 214 | return AMEDIA_OK; |
| 215 | } |
| 216 | |
| 217 | std::vector<std::shared_ptr<AMediaFormat>> MediaTranscoder::getTrackFormats() const { |
| 218 | // Return a deep copy of the formats to avoid the caller modifying our internal formats. |
| 219 | std::vector<std::shared_ptr<AMediaFormat>> trackFormats; |
| 220 | for (const std::shared_ptr<AMediaFormat>& sourceFormat : mSourceTrackFormats) { |
| 221 | AMediaFormat* copy = AMediaFormat_new(); |
| 222 | AMediaFormat_copy(copy, sourceFormat.get()); |
| 223 | trackFormats.emplace_back(copy, &AMediaFormat_delete); |
| 224 | } |
| 225 | return trackFormats; |
| 226 | } |
| 227 | |
| 228 | media_status_t MediaTranscoder::configureTrackFormat(size_t trackIndex, AMediaFormat* trackFormat) { |
| 229 | if (mSampleReader == nullptr) { |
| 230 | LOG(ERROR) << "Source must be configured before tracks"; |
| 231 | return AMEDIA_ERROR_INVALID_OPERATION; |
| 232 | } else if (trackIndex >= mSourceTrackFormats.size()) { |
| 233 | LOG(ERROR) << "Track index " << trackIndex |
| 234 | << " is out of bounds. Track count: " << mSourceTrackFormats.size(); |
| 235 | return AMEDIA_ERROR_INVALID_PARAMETER; |
| 236 | } |
| 237 | |
Linus Nilsson | 6233fed | 2020-08-13 15:15:14 -0700 | [diff] [blame] | 238 | media_status_t status = mSampleReader->selectTrack(trackIndex); |
| 239 | if (status != AMEDIA_OK) { |
| 240 | LOG(ERROR) << "Unable to select track " << trackIndex; |
| 241 | return status; |
| 242 | } |
| 243 | |
Linus Nilsson | e4716f2 | 2020-07-10 16:07:57 -0700 | [diff] [blame] | 244 | std::shared_ptr<MediaTrackTranscoder> transcoder; |
| 245 | std::shared_ptr<AMediaFormat> format; |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 246 | |
| 247 | if (trackFormat == nullptr) { |
Linus Nilsson | e4716f2 | 2020-07-10 16:07:57 -0700 | [diff] [blame] | 248 | transcoder = std::make_shared<PassthroughTrackTranscoder>(shared_from_this()); |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 249 | } else { |
| 250 | const char* srcMime = nullptr; |
| 251 | if (!AMediaFormat_getString(mSourceTrackFormats[trackIndex].get(), AMEDIAFORMAT_KEY_MIME, |
| 252 | &srcMime)) { |
| 253 | LOG(ERROR) << "Source track #" << trackIndex << " has no mime type"; |
| 254 | return AMEDIA_ERROR_MALFORMED; |
| 255 | } |
| 256 | |
| 257 | if (strncmp(srcMime, "video/", 6) != 0) { |
| 258 | LOG(ERROR) << "Only video tracks are supported for transcoding. Unable to configure " |
| 259 | "track #" |
| 260 | << trackIndex << " with mime " << srcMime; |
| 261 | return AMEDIA_ERROR_UNSUPPORTED; |
| 262 | } |
| 263 | |
| 264 | const char* dstMime = nullptr; |
| 265 | if (AMediaFormat_getString(trackFormat, AMEDIAFORMAT_KEY_MIME, &dstMime)) { |
| 266 | if (strncmp(dstMime, "video/", 6) != 0) { |
| 267 | LOG(ERROR) << "Unable to convert media types for track #" << trackIndex << ", from " |
| 268 | << srcMime << " to " << dstMime; |
| 269 | return AMEDIA_ERROR_UNSUPPORTED; |
| 270 | } |
| 271 | } |
| 272 | |
Linus Nilsson | e4716f2 | 2020-07-10 16:07:57 -0700 | [diff] [blame] | 273 | transcoder = VideoTrackTranscoder::create(shared_from_this()); |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 274 | |
| 275 | AMediaFormat* mergedFormat = |
| 276 | mergeMediaFormats(mSourceTrackFormats[trackIndex].get(), trackFormat); |
| 277 | if (mergedFormat == nullptr) { |
| 278 | LOG(ERROR) << "Unable to merge source and destination formats"; |
| 279 | return AMEDIA_ERROR_UNKNOWN; |
| 280 | } |
| 281 | |
| 282 | format = std::shared_ptr<AMediaFormat>(mergedFormat, &AMediaFormat_delete); |
| 283 | } |
| 284 | |
Linus Nilsson | 6233fed | 2020-08-13 15:15:14 -0700 | [diff] [blame] | 285 | transcoder->configure(mSampleReader, trackIndex, format); |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 286 | if (status != AMEDIA_OK) { |
| 287 | LOG(ERROR) << "Configure track transcoder for track #" << trackIndex << " returned error " |
| 288 | << status; |
| 289 | return status; |
| 290 | } |
| 291 | |
| 292 | mTrackTranscoders.emplace_back(std::move(transcoder)); |
| 293 | return AMEDIA_OK; |
| 294 | } |
| 295 | |
Chong Zhang | 308e91f | 2020-06-10 15:27:56 -0700 | [diff] [blame] | 296 | media_status_t MediaTranscoder::configureDestination(int fd) { |
| 297 | if (fd < 0) { |
| 298 | LOG(ERROR) << "Invalid destination fd: " << fd; |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 299 | return AMEDIA_ERROR_INVALID_PARAMETER; |
Chong Zhang | 308e91f | 2020-06-10 15:27:56 -0700 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | if (mSampleWriter != nullptr) { |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 303 | LOG(ERROR) << "Destination is already configured."; |
| 304 | return AMEDIA_ERROR_INVALID_OPERATION; |
| 305 | } |
| 306 | |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 307 | mSampleWriter = std::make_unique<MediaSampleWriter>(); |
Linus Nilsson | e2cdd1f | 2020-07-07 17:29:26 -0700 | [diff] [blame] | 308 | const bool initOk = mSampleWriter->init(fd, shared_from_this()); |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 309 | |
| 310 | if (!initOk) { |
Chong Zhang | 308e91f | 2020-06-10 15:27:56 -0700 | [diff] [blame] | 311 | LOG(ERROR) << "Unable to initialize sample writer with destination fd: " << fd; |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 312 | mSampleWriter.reset(); |
| 313 | return AMEDIA_ERROR_UNKNOWN; |
| 314 | } |
| 315 | |
| 316 | return AMEDIA_OK; |
| 317 | } |
| 318 | |
| 319 | media_status_t MediaTranscoder::start() { |
| 320 | if (mTrackTranscoders.size() < 1) { |
| 321 | LOG(ERROR) << "Unable to start, no tracks are configured."; |
| 322 | return AMEDIA_ERROR_INVALID_OPERATION; |
| 323 | } else if (mSampleWriter == nullptr) { |
| 324 | LOG(ERROR) << "Unable to start, destination is not configured"; |
| 325 | return AMEDIA_ERROR_INVALID_OPERATION; |
| 326 | } |
| 327 | |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 328 | // Start transcoders |
| 329 | for (auto& transcoder : mTrackTranscoders) { |
Chong Zhang | a2cc86b | 2020-06-17 16:56:49 -0700 | [diff] [blame] | 330 | bool started = transcoder->start(); |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 331 | if (!started) { |
| 332 | LOG(ERROR) << "Unable to start track transcoder."; |
Chong Zhang | 308e91f | 2020-06-10 15:27:56 -0700 | [diff] [blame] | 333 | cancel(); |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 334 | return AMEDIA_ERROR_UNKNOWN; |
| 335 | } |
| 336 | } |
| 337 | return AMEDIA_OK; |
| 338 | } |
| 339 | |
Chong Zhang | b55c545 | 2020-06-26 14:32:12 -0700 | [diff] [blame] | 340 | media_status_t MediaTranscoder::pause(std::shared_ptr<const Parcel>* pausedState) { |
| 341 | // TODO: write internal states to parcel. |
| 342 | *pausedState = std::make_shared<Parcel>(); |
| 343 | return cancel(); |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | media_status_t MediaTranscoder::resume() { |
Chong Zhang | b55c545 | 2020-06-26 14:32:12 -0700 | [diff] [blame] | 347 | // TODO: restore internal states from parcel. |
| 348 | return start(); |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 349 | } |
| 350 | |
Chong Zhang | 308e91f | 2020-06-10 15:27:56 -0700 | [diff] [blame] | 351 | media_status_t MediaTranscoder::cancel() { |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 352 | bool expected = false; |
| 353 | if (!mCancelled.compare_exchange_strong(expected, true)) { |
| 354 | // Already cancelled. |
| 355 | return AMEDIA_OK; |
| 356 | } |
| 357 | |
| 358 | mSampleWriter->stop(); |
Linus Nilsson | 6233fed | 2020-08-13 15:15:14 -0700 | [diff] [blame] | 359 | mSampleReader->setEnforceSequentialAccess(false); |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 360 | for (auto& transcoder : mTrackTranscoders) { |
| 361 | transcoder->stop(); |
| 362 | } |
| 363 | |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 364 | return AMEDIA_OK; |
| 365 | } |
| 366 | |
| 367 | } // namespace android |