blob: 3d4ff151923d9800e03b7b5c06c1763d02950ea3 [file] [log] [blame]
Linus Nilssoncab39d82020-05-14 16:32:21 -07001/*
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 Zhang308e91f2020-06-10 15:27:56 -070023#include <media/MediaSampleWriter.h>
Linus Nilssoncab39d82020-05-14 16:32:21 -070024#include <media/MediaTranscoder.h>
Linus Nilsson16d772b2020-09-29 19:21:11 -070025#include <media/NdkCommon.h>
Linus Nilssoncab39d82020-05-14 16:32:21 -070026#include <media/PassthroughTrackTranscoder.h>
27#include <media/VideoTrackTranscoder.h>
28#include <unistd.h>
29
30namespace android {
31
Linus Nilssoncab39d82020-05-14 16:32:21 -070032static 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 Nilsson16d772b2020-09-29 19:21:11 -070049 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 Nilssoncab39d82020-05-14 16:32:21 -070065 };
Linus Nilsson16d772b2020-09-29 19:21:11 -070066 const size_t entryCount = sizeof(kSupportedFormatEntries) / sizeof(kSupportedFormatEntries[0]);
Linus Nilssoncab39d82020-05-14 16:32:21 -070067
Linus Nilsson16d772b2020-09-29 19:21:11 -070068 AMediaFormatUtils::CopyFormatEntries(overlay, format, kSupportedFormatEntries, entryCount);
Linus Nilssoncab39d82020-05-14 16:32:21 -070069 return format;
70}
71
Linus Nilssonfdb3e332020-09-18 17:11:41 -070072void 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 Zhangb55c5452020-06-26 14:32:12 -0700105 return;
106 }
107
Linus Nilssonfdb3e332020-09-18 17:11:41 -0700108 // 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 Nilssoncab39d82020-05-14 16:32:21 -0700125 }
126}
127
Chong Zhanga2cc86b2020-06-17 16:56:49 -0700128void MediaTranscoder::onTrackFormatAvailable(const MediaTrackTranscoder* transcoder) {
Linus Nilssonfdb3e332020-09-18 17:11:41 -0700129 LOG(DEBUG) << "TrackTranscoder " << transcoder << " format available.";
Chong Zhanga2cc86b2020-06-17 16:56:49 -0700130
131 std::scoped_lock lock{mTracksAddedMutex};
Linus Nilssonfdb3e332020-09-18 17:11:41 -0700132 const void* sampleWriterPtr = static_cast<const void*>(mSampleWriter.get());
Chong Zhanga2cc86b2020-06-17 16:56:49 -0700133
134 // Ignore duplicate format change.
135 if (mTracksAdded.count(transcoder) > 0) {
136 return;
137 }
138
139 // Add track to the writer.
Linus Nilssonc31d2492020-09-23 12:30:00 -0700140 auto consumer = mSampleWriter->addTrack(transcoder->getOutputFormat());
141 if (consumer == nullptr) {
Chong Zhanga2cc86b2020-06-17 16:56:49 -0700142 LOG(ERROR) << "Unable to add track to sample writer.";
Linus Nilssonfdb3e332020-09-18 17:11:41 -0700143 onThreadFinished(sampleWriterPtr, AMEDIA_ERROR_UNKNOWN, false /* stopped */);
Chong Zhanga2cc86b2020-06-17 16:56:49 -0700144 return;
145 }
146
Linus Nilssonc31d2492020-09-23 12:30:00 -0700147 MediaTrackTranscoder* mutableTranscoder = const_cast<MediaTrackTranscoder*>(transcoder);
148 mutableTranscoder->setSampleConsumer(consumer);
149
Chong Zhanga2cc86b2020-06-17 16:56:49 -0700150 mTracksAdded.insert(transcoder);
Linus Nilssonfdb3e332020-09-18 17:11:41 -0700151 bool errorStarting = false;
Chong Zhanga2cc86b2020-06-17 16:56:49 -0700152 if (mTracksAdded.size() == mTrackTranscoders.size()) {
Linus Nilsson6233fed2020-08-13 15:15:14 -0700153 // 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 Nilssonfdb3e332020-09-18 17:11:41 -0700158
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 Zhanga2cc86b2020-06-17 16:56:49 -0700170 }
171 }
Linus Nilssonfdb3e332020-09-18 17:11:41 -0700172
173 if (errorStarting) {
174 LOG(ERROR) << "Unable to start sample writer.";
175 onThreadFinished(sampleWriterPtr, AMEDIA_ERROR_UNKNOWN, false /* stopped */);
176 }
Chong Zhanga2cc86b2020-06-17 16:56:49 -0700177}
178
Linus Nilssoncab39d82020-05-14 16:32:21 -0700179void MediaTranscoder::onTrackFinished(const MediaTrackTranscoder* transcoder) {
180 LOG(DEBUG) << "TrackTranscoder " << transcoder << " finished";
Linus Nilssonfdb3e332020-09-18 17:11:41 -0700181 onThreadFinished(static_cast<const void*>(transcoder), AMEDIA_OK, false /* stopped */);
182}
183
184void MediaTranscoder::onTrackStopped(const MediaTrackTranscoder* transcoder) {
185 LOG(DEBUG) << "TrackTranscoder " << transcoder << " stopped";
186 onThreadFinished(static_cast<const void*>(transcoder), AMEDIA_OK, true /* stopped */);
Linus Nilssoncab39d82020-05-14 16:32:21 -0700187}
188
189void MediaTranscoder::onTrackError(const MediaTrackTranscoder* transcoder, media_status_t status) {
Linus Nilsson93cf9132020-09-24 12:12:48 -0700190 LOG(ERROR) << "TrackTranscoder " << transcoder << " returned error " << status;
Linus Nilssonfdb3e332020-09-18 17:11:41 -0700191 onThreadFinished(static_cast<const void*>(transcoder), status, false /* stopped */);
Linus Nilssoncab39d82020-05-14 16:32:21 -0700192}
193
Linus Nilssonfdb3e332020-09-18 17:11:41 -0700194void 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
199void MediaTranscoder::onStopped(const MediaSampleWriter* writer) {
200 LOG(DEBUG) << "Sample writer " << writer << " stopped";
201 onThreadFinished(static_cast<const void*>(writer), AMEDIA_OK, true /* stopped */);
Linus Nilssoncab39d82020-05-14 16:32:21 -0700202}
203
Linus Nilssone2cdd1f2020-07-07 17:29:26 -0700204void MediaTranscoder::onProgressUpdate(const MediaSampleWriter* writer __unused, int32_t progress) {
205 // Dispatch progress updated to the client.
206 mCallbacks->onProgressUpdate(this, progress);
207}
208
Chong Zhangbbb4eac2020-11-18 11:12:06 -0800209MediaTranscoder::MediaTranscoder(const std::shared_ptr<CallbackInterface>& callbacks, pid_t pid,
210 uid_t uid)
211 : mCallbacks(callbacks), mPid(pid), mUid(uid) {}
Chong Zhang308e91f2020-06-10 15:27:56 -0700212
Linus Nilssoncab39d82020-05-14 16:32:21 -0700213std::shared_ptr<MediaTranscoder> MediaTranscoder::create(
Chong Zhangbbb4eac2020-11-18 11:12:06 -0800214 const std::shared_ptr<CallbackInterface>& callbacks, pid_t pid, uid_t uid,
Chong Zhange4e088f2020-10-21 19:10:42 -0700215 const std::shared_ptr<ndk::ScopedAParcel>& pausedState) {
Linus Nilssoncab39d82020-05-14 16:32:21 -0700216 if (pausedState != nullptr) {
Chong Zhangb55c5452020-06-26 14:32:12 -0700217 LOG(INFO) << "Initializing from paused state.";
218 }
219 if (callbacks == nullptr) {
Linus Nilssoncab39d82020-05-14 16:32:21 -0700220 LOG(ERROR) << "Callbacks cannot be null";
221 return nullptr;
222 }
223
Chong Zhangbbb4eac2020-11-18 11:12:06 -0800224 return std::shared_ptr<MediaTranscoder>(new MediaTranscoder(callbacks, pid, uid));
Linus Nilssoncab39d82020-05-14 16:32:21 -0700225}
226
Chong Zhang308e91f2020-06-10 15:27:56 -0700227media_status_t MediaTranscoder::configureSource(int fd) {
228 if (fd < 0) {
229 LOG(ERROR) << "Invalid source fd: " << fd;
Linus Nilssoncab39d82020-05-14 16:32:21 -0700230 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 Nilssoncab39d82020-05-14 16:32:21 -0700237
238 if (mSampleReader == nullptr) {
Chong Zhang308e91f2020-06-10 15:27:56 -0700239 LOG(ERROR) << "Unable to parse source fd: " << fd;
Linus Nilssoncab39d82020-05-14 16:32:21 -0700240 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
257std::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
268media_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 Nilssone4716f22020-07-10 16:07:57 -0700278 std::shared_ptr<MediaTrackTranscoder> transcoder;
279 std::shared_ptr<AMediaFormat> format;
Linus Nilssoncab39d82020-05-14 16:32:21 -0700280
281 if (trackFormat == nullptr) {
Linus Nilssone4716f22020-07-10 16:07:57 -0700282 transcoder = std::make_shared<PassthroughTrackTranscoder>(shared_from_this());
Linus Nilssoncab39d82020-05-14 16:32:21 -0700283 } else {
284 const char* srcMime = nullptr;
285 if (!AMediaFormat_getString(mSourceTrackFormats[trackIndex].get(), AMEDIAFORMAT_KEY_MIME,
286 &srcMime)) {
287 LOG(ERROR) << "Source track #" << trackIndex << " has no mime type";
288 return AMEDIA_ERROR_MALFORMED;
289 }
290
291 if (strncmp(srcMime, "video/", 6) != 0) {
292 LOG(ERROR) << "Only video tracks are supported for transcoding. Unable to configure "
293 "track #"
294 << trackIndex << " with mime " << srcMime;
295 return AMEDIA_ERROR_UNSUPPORTED;
296 }
297
298 const char* dstMime = nullptr;
299 if (AMediaFormat_getString(trackFormat, AMEDIAFORMAT_KEY_MIME, &dstMime)) {
300 if (strncmp(dstMime, "video/", 6) != 0) {
301 LOG(ERROR) << "Unable to convert media types for track #" << trackIndex << ", from "
302 << srcMime << " to " << dstMime;
303 return AMEDIA_ERROR_UNSUPPORTED;
304 }
305 }
306
Chong Zhangbbb4eac2020-11-18 11:12:06 -0800307 transcoder = VideoTrackTranscoder::create(shared_from_this(), mPid, mUid);
Linus Nilssoncab39d82020-05-14 16:32:21 -0700308
309 AMediaFormat* mergedFormat =
310 mergeMediaFormats(mSourceTrackFormats[trackIndex].get(), trackFormat);
311 if (mergedFormat == nullptr) {
312 LOG(ERROR) << "Unable to merge source and destination formats";
313 return AMEDIA_ERROR_UNKNOWN;
314 }
315
316 format = std::shared_ptr<AMediaFormat>(mergedFormat, &AMediaFormat_delete);
317 }
318
Linus Nilssonaf4a3212020-12-15 08:18:25 -0800319 media_status_t status = mSampleReader->selectTrack(trackIndex);
320 if (status != AMEDIA_OK) {
321 LOG(ERROR) << "Unable to select track " << trackIndex;
322 return status;
323 }
324
Chong Zhangc8c88cc2020-09-17 12:50:49 -0700325 status = transcoder->configure(mSampleReader, trackIndex, format);
Linus Nilssoncab39d82020-05-14 16:32:21 -0700326 if (status != AMEDIA_OK) {
327 LOG(ERROR) << "Configure track transcoder for track #" << trackIndex << " returned error "
328 << status;
Linus Nilssonaf4a3212020-12-15 08:18:25 -0800329 mSampleReader->unselectTrack(trackIndex);
Linus Nilssoncab39d82020-05-14 16:32:21 -0700330 return status;
331 }
332
Linus Nilssonfdb3e332020-09-18 17:11:41 -0700333 std::scoped_lock lock{mThreadStateMutex};
334 mThreadStates[static_cast<const void*>(transcoder.get())] = PENDING;
335
Linus Nilssoncab39d82020-05-14 16:32:21 -0700336 mTrackTranscoders.emplace_back(std::move(transcoder));
337 return AMEDIA_OK;
338}
339
Chong Zhang308e91f2020-06-10 15:27:56 -0700340media_status_t MediaTranscoder::configureDestination(int fd) {
341 if (fd < 0) {
342 LOG(ERROR) << "Invalid destination fd: " << fd;
Linus Nilssoncab39d82020-05-14 16:32:21 -0700343 return AMEDIA_ERROR_INVALID_PARAMETER;
Chong Zhang308e91f2020-06-10 15:27:56 -0700344 }
345
346 if (mSampleWriter != nullptr) {
Linus Nilssoncab39d82020-05-14 16:32:21 -0700347 LOG(ERROR) << "Destination is already configured.";
348 return AMEDIA_ERROR_INVALID_OPERATION;
349 }
350
Linus Nilssonc31d2492020-09-23 12:30:00 -0700351 mSampleWriter = MediaSampleWriter::Create();
Linus Nilssone2cdd1f2020-07-07 17:29:26 -0700352 const bool initOk = mSampleWriter->init(fd, shared_from_this());
Linus Nilssoncab39d82020-05-14 16:32:21 -0700353
354 if (!initOk) {
Chong Zhang308e91f2020-06-10 15:27:56 -0700355 LOG(ERROR) << "Unable to initialize sample writer with destination fd: " << fd;
Linus Nilssoncab39d82020-05-14 16:32:21 -0700356 mSampleWriter.reset();
357 return AMEDIA_ERROR_UNKNOWN;
358 }
359
Linus Nilssonfdb3e332020-09-18 17:11:41 -0700360 std::scoped_lock lock{mThreadStateMutex};
361 mThreadStates[static_cast<const void*>(mSampleWriter.get())] = PENDING;
Linus Nilssoncab39d82020-05-14 16:32:21 -0700362 return AMEDIA_OK;
363}
364
365media_status_t MediaTranscoder::start() {
366 if (mTrackTranscoders.size() < 1) {
367 LOG(ERROR) << "Unable to start, no tracks are configured.";
368 return AMEDIA_ERROR_INVALID_OPERATION;
369 } else if (mSampleWriter == nullptr) {
370 LOG(ERROR) << "Unable to start, destination is not configured";
371 return AMEDIA_ERROR_INVALID_OPERATION;
372 }
373
Linus Nilssoncab39d82020-05-14 16:32:21 -0700374 // Start transcoders
Linus Nilssonfdb3e332020-09-18 17:11:41 -0700375 bool started = true;
376 {
377 std::scoped_lock lock{mThreadStateMutex};
378 for (auto& transcoder : mTrackTranscoders) {
379 if (!(started = transcoder->start())) {
380 break;
381 }
382 mThreadStates[static_cast<const void*>(transcoder.get())] = RUNNING;
Linus Nilssoncab39d82020-05-14 16:32:21 -0700383 }
384 }
Linus Nilssonfdb3e332020-09-18 17:11:41 -0700385 if (!started) {
386 LOG(ERROR) << "Unable to start track transcoder.";
387 cancel();
388 return AMEDIA_ERROR_UNKNOWN;
389 }
Linus Nilssoncab39d82020-05-14 16:32:21 -0700390 return AMEDIA_OK;
391}
392
Linus Nilssonfdb3e332020-09-18 17:11:41 -0700393media_status_t MediaTranscoder::requestStop(bool stopOnSync) {
394 std::scoped_lock lock{mThreadStateMutex};
395 if (mCancelled) {
396 LOG(DEBUG) << "MediaTranscoder already cancelled";
397 return AMEDIA_ERROR_UNSUPPORTED;
398 }
399
400 if (!stopOnSync) {
401 mSampleWriterStopped = true;
402 mSampleWriter->stop();
403 }
404
405 mSampleReader->setEnforceSequentialAccess(false);
406 for (auto& transcoder : mTrackTranscoders) {
407 transcoder->stop(stopOnSync);
408 }
409
410 mCancelled = true;
411 return AMEDIA_OK;
412}
413
414void MediaTranscoder::waitForThreads() NO_THREAD_SAFETY_ANALYSIS {
415 std::unique_lock lock{mThreadStateMutex};
416 while (!mThreadsDone) {
417 mThreadsDoneSignal.wait(lock);
418 }
419}
420
Chong Zhange4e088f2020-10-21 19:10:42 -0700421media_status_t MediaTranscoder::pause(std::shared_ptr<ndk::ScopedAParcel>* pausedState) {
Linus Nilssonfdb3e332020-09-18 17:11:41 -0700422 media_status_t status = requestStop(true /* stopOnSync */);
423 if (status != AMEDIA_OK) {
424 return status;
425 }
426
427 waitForThreads();
428
Chong Zhangb55c5452020-06-26 14:32:12 -0700429 // TODO: write internal states to parcel.
Chong Zhange4e088f2020-10-21 19:10:42 -0700430 *pausedState = std::shared_ptr<::ndk::ScopedAParcel>(new ::ndk::ScopedAParcel());
Linus Nilssonfdb3e332020-09-18 17:11:41 -0700431 return AMEDIA_OK;
432}
433
434media_status_t MediaTranscoder::cancel() {
435 media_status_t status = requestStop(false /* stopOnSync */);
436 if (status != AMEDIA_OK) {
437 return status;
438 }
439
440 waitForThreads();
441
442 // TODO: Release transcoders?
443 return AMEDIA_OK;
Linus Nilssoncab39d82020-05-14 16:32:21 -0700444}
445
446media_status_t MediaTranscoder::resume() {
Chong Zhangb55c5452020-06-26 14:32:12 -0700447 // TODO: restore internal states from parcel.
448 return start();
Linus Nilssoncab39d82020-05-14 16:32:21 -0700449}
450
Linus Nilssoncab39d82020-05-14 16:32:21 -0700451} // namespace android