blob: 84108506a4ef124970684673c17d5274e7752bb2 [file] [log] [blame]
Chong Zhang66469272020-06-04 16:51:55 -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 "TranscoderWrapper"
19
20#include <aidl/android/media/TranscodingErrorCode.h>
21#include <aidl/android/media/TranscodingRequestParcel.h>
22#include <media/MediaTranscoder.h>
23#include <media/NdkCommon.h>
24#include <media/TranscoderWrapper.h>
25#include <utils/Log.h>
26
27#include <thread>
28
29namespace android {
30using Status = ::ndk::ScopedAStatus;
31using ::aidl::android::media::TranscodingErrorCode;
32using ::aidl::android::media::TranscodingVideoCodecType;
33using ::aidl::android::media::TranscodingVideoTrackFormat;
34
35static TranscodingErrorCode toTranscodingError(media_status_t status) {
36 switch (status) {
37 case AMEDIA_OK:
38 return TranscodingErrorCode::kNoError;
39 case AMEDIACODEC_ERROR_INSUFFICIENT_RESOURCE: // FALLTHRU
40 case AMEDIACODEC_ERROR_RECLAIMED:
41 return TranscodingErrorCode::kInsufficientResources;
42 case AMEDIA_ERROR_MALFORMED:
43 return TranscodingErrorCode::kMalformed;
44 case AMEDIA_ERROR_UNSUPPORTED:
45 return TranscodingErrorCode::kUnsupported;
46 case AMEDIA_ERROR_INVALID_OBJECT: // FALLTHRU
47 case AMEDIA_ERROR_INVALID_PARAMETER:
48 return TranscodingErrorCode::kInvalidParameter;
49 case AMEDIA_ERROR_INVALID_OPERATION:
50 return TranscodingErrorCode::kInvalidOperation;
51 case AMEDIA_ERROR_IO:
52 return TranscodingErrorCode::kErrorIO;
53 case AMEDIA_ERROR_UNKNOWN: // FALLTHRU
54 default:
55 return TranscodingErrorCode::kUnknown;
56 }
57}
58
Chong Zhangb55c5452020-06-26 14:32:12 -070059static AMediaFormat* getVideoFormat(
60 const char* originalMime,
61 const std::optional<TranscodingVideoTrackFormat>& requestedFormat) {
62 if (requestedFormat == std::nullopt) {
63 return nullptr;
64 }
65
66 AMediaFormat* format = AMediaFormat_new();
67 bool changed = false;
68 if (requestedFormat->codecType == TranscodingVideoCodecType::kHevc &&
69 strcmp(originalMime, AMEDIA_MIMETYPE_VIDEO_HEVC)) {
70 AMediaFormat_setString(format, AMEDIAFORMAT_KEY_MIME, AMEDIA_MIMETYPE_VIDEO_HEVC);
71 changed = true;
72 } else if (requestedFormat->codecType == TranscodingVideoCodecType::kAvc &&
73 strcmp(originalMime, AMEDIA_MIMETYPE_VIDEO_AVC)) {
74 AMediaFormat_setString(format, AMEDIAFORMAT_KEY_MIME, AMEDIA_MIMETYPE_VIDEO_AVC);
75 changed = true;
76 }
77 if (requestedFormat->bitrateBps > 0) {
78 AMediaFormat_setInt32(format, AMEDIAFORMAT_KEY_BIT_RATE, requestedFormat->bitrateBps);
79 changed = true;
80 }
81 // TODO: translate other fields from requestedFormat to the format for MediaTranscoder.
82 // Also need to determine more settings to expose in TranscodingVideoTrackFormat.
83 if (!changed) {
84 AMediaFormat_delete(format);
85 // Use null format for passthru.
86 format = nullptr;
87 }
88 return format;
89}
90
Chong Zhang66469272020-06-04 16:51:55 -070091//static
Chong Zhangf9077512020-09-21 21:02:06 -070092std::string TranscoderWrapper::toString(const Event& event) {
93 std::string typeStr;
94 switch (event.type) {
Chong Zhang66469272020-06-04 16:51:55 -070095 case Event::Start:
Chong Zhangf9077512020-09-21 21:02:06 -070096 typeStr = "Start";
Chong Zhang66469272020-06-04 16:51:55 -070097 break;
Chong Zhangf9077512020-09-21 21:02:06 -070098 case Event::Pause:
99 typeStr = "Pause";
100 break;
101 case Event::Resume:
102 typeStr = "Resume";
103 break;
104 case Event::Stop:
105 typeStr = "Stop";
106 break;
107 case Event::Finish:
108 typeStr = "Finish";
109 break;
110 case Event::Error:
111 typeStr = "Error";
112 break;
113 case Event::Progress:
114 typeStr = "Progress";
115 break;
116 default:
117 return "(unknown)";
Chong Zhang66469272020-06-04 16:51:55 -0700118 }
Chong Zhangf9077512020-09-21 21:02:06 -0700119 std::string result;
Chong Zhangbc062482020-10-14 16:43:53 -0700120 result = "session {" + std::to_string(event.clientId) + "," + std::to_string(event.sessionId) +
Chong Zhangf9077512020-09-21 21:02:06 -0700121 "}: " + typeStr;
122 if (event.type == Event::Error || event.type == Event::Progress) {
123 result += " " + std::to_string(event.arg);
124 }
125 return result;
Chong Zhang66469272020-06-04 16:51:55 -0700126}
127
128class TranscoderWrapper::CallbackImpl : public MediaTranscoder::CallbackInterface {
129public:
130 CallbackImpl(const std::shared_ptr<TranscoderWrapper>& owner, ClientIdType clientId,
Chong Zhangbc062482020-10-14 16:43:53 -0700131 SessionIdType sessionId)
132 : mOwner(owner), mClientId(clientId), mSessionId(sessionId) {}
Chong Zhang66469272020-06-04 16:51:55 -0700133
134 virtual void onFinished(const MediaTranscoder* transcoder __unused) override {
135 auto owner = mOwner.lock();
136 if (owner != nullptr) {
Chong Zhangbc062482020-10-14 16:43:53 -0700137 owner->onFinish(mClientId, mSessionId);
Chong Zhang66469272020-06-04 16:51:55 -0700138 }
139 }
140
141 virtual void onError(const MediaTranscoder* transcoder __unused,
142 media_status_t error) override {
143 auto owner = mOwner.lock();
144 if (owner != nullptr) {
Chong Zhangbc062482020-10-14 16:43:53 -0700145 owner->onError(mClientId, mSessionId, error);
Chong Zhang66469272020-06-04 16:51:55 -0700146 }
147 }
148
149 virtual void onProgressUpdate(const MediaTranscoder* transcoder __unused,
150 int32_t progress) override {
Chong Zhang98b8a372020-07-08 17:27:37 -0700151 auto owner = mOwner.lock();
152 if (owner != nullptr) {
Chong Zhangbc062482020-10-14 16:43:53 -0700153 owner->onProgress(mClientId, mSessionId, progress);
Chong Zhang98b8a372020-07-08 17:27:37 -0700154 }
Chong Zhang66469272020-06-04 16:51:55 -0700155 }
156
157 virtual void onCodecResourceLost(const MediaTranscoder* transcoder __unused,
Chong Zhange4e088f2020-10-21 19:10:42 -0700158 const std::shared_ptr<ndk::ScopedAParcel>& pausedState
Chong Zhang66469272020-06-04 16:51:55 -0700159 __unused) override {
Chong Zhangbc062482020-10-14 16:43:53 -0700160 ALOGV("%s: session {%lld, %d}", __FUNCTION__, (long long)mClientId, mSessionId);
Chong Zhang66469272020-06-04 16:51:55 -0700161 }
162
163private:
164 std::weak_ptr<TranscoderWrapper> mOwner;
165 ClientIdType mClientId;
Chong Zhangbc062482020-10-14 16:43:53 -0700166 SessionIdType mSessionId;
Chong Zhang66469272020-06-04 16:51:55 -0700167};
168
Chong Zhangbc062482020-10-14 16:43:53 -0700169TranscoderWrapper::TranscoderWrapper() : mCurrentClientId(0), mCurrentSessionId(-1) {
Chong Zhang66469272020-06-04 16:51:55 -0700170 std::thread(&TranscoderWrapper::threadLoop, this).detach();
171}
172
173void TranscoderWrapper::setCallback(const std::shared_ptr<TranscoderCallbackInterface>& cb) {
174 mCallback = cb;
175}
176
Chong Zhangf9077512020-09-21 21:02:06 -0700177static bool isResourceError(media_status_t err) {
178 return err == AMEDIACODEC_ERROR_RECLAIMED || err == AMEDIACODEC_ERROR_INSUFFICIENT_RESOURCE;
179}
180
Chong Zhangbc062482020-10-14 16:43:53 -0700181void TranscoderWrapper::reportError(ClientIdType clientId, SessionIdType sessionId,
182 media_status_t err) {
Chong Zhangf9077512020-09-21 21:02:06 -0700183 auto callback = mCallback.lock();
184 if (callback != nullptr) {
185 if (isResourceError(err)) {
186 // Add a placeholder pause state to mPausedStateMap. This is required when resuming.
187 // TODO: remove this when transcoder pause/resume logic is ready. New logic will
188 // no longer use the pause states.
Chong Zhangbc062482020-10-14 16:43:53 -0700189 auto it = mPausedStateMap.find(SessionKeyType(clientId, sessionId));
Chong Zhangf9077512020-09-21 21:02:06 -0700190 if (it == mPausedStateMap.end()) {
Chong Zhangbc062482020-10-14 16:43:53 -0700191 mPausedStateMap.emplace(SessionKeyType(clientId, sessionId),
Chong Zhange4e088f2020-10-21 19:10:42 -0700192 new ndk::ScopedAParcel());
Chong Zhangf9077512020-09-21 21:02:06 -0700193 }
194
Chong Zhangeffd8962020-12-02 14:29:09 -0800195 callback->onResourceLost(clientId, sessionId);
Chong Zhangf9077512020-09-21 21:02:06 -0700196 } else {
Chong Zhangbc062482020-10-14 16:43:53 -0700197 callback->onError(clientId, sessionId, toTranscodingError(err));
Chong Zhangf9077512020-09-21 21:02:06 -0700198 }
199 }
200}
201
Chong Zhangbc062482020-10-14 16:43:53 -0700202void TranscoderWrapper::start(ClientIdType clientId, SessionIdType sessionId,
Chong Zhang66469272020-06-04 16:51:55 -0700203 const TranscodingRequestParcel& request,
Chong Zhangb55c5452020-06-26 14:32:12 -0700204 const std::shared_ptr<ITranscodingClientCallback>& clientCb) {
hkuang34377c32020-09-24 22:28:27 -0700205 queueEvent(Event::Start, clientId, sessionId, [=, &request] {
Chong Zhangbc062482020-10-14 16:43:53 -0700206 media_status_t err = handleStart(clientId, sessionId, request, clientCb);
Chong Zhangf9077512020-09-21 21:02:06 -0700207 if (err != AMEDIA_OK) {
Chong Zhang66469272020-06-04 16:51:55 -0700208 cleanup();
Chong Zhangbc062482020-10-14 16:43:53 -0700209 reportError(clientId, sessionId, err);
Chong Zhangb55c5452020-06-26 14:32:12 -0700210 } else {
Chong Zhangf9077512020-09-21 21:02:06 -0700211 auto callback = mCallback.lock();
Chong Zhangb55c5452020-06-26 14:32:12 -0700212 if (callback != nullptr) {
Chong Zhangbc062482020-10-14 16:43:53 -0700213 callback->onStarted(clientId, sessionId);
Chong Zhangb55c5452020-06-26 14:32:12 -0700214 }
Chong Zhang66469272020-06-04 16:51:55 -0700215 }
216 });
217}
218
Chong Zhangbc062482020-10-14 16:43:53 -0700219void TranscoderWrapper::pause(ClientIdType clientId, SessionIdType sessionId) {
220 queueEvent(Event::Pause, clientId, sessionId, [=] {
221 media_status_t err = handlePause(clientId, sessionId);
Chong Zhangb55c5452020-06-26 14:32:12 -0700222
223 cleanup();
224
Chong Zhangf9077512020-09-21 21:02:06 -0700225 if (err != AMEDIA_OK) {
Chong Zhangbc062482020-10-14 16:43:53 -0700226 reportError(clientId, sessionId, err);
Chong Zhangf9077512020-09-21 21:02:06 -0700227 } else {
228 auto callback = mCallback.lock();
229 if (callback != nullptr) {
Chong Zhangbc062482020-10-14 16:43:53 -0700230 callback->onPaused(clientId, sessionId);
Chong Zhangb55c5452020-06-26 14:32:12 -0700231 }
232 }
233 });
Chong Zhang66469272020-06-04 16:51:55 -0700234}
235
Chong Zhangbc062482020-10-14 16:43:53 -0700236void TranscoderWrapper::resume(ClientIdType clientId, SessionIdType sessionId,
Chong Zhangb55c5452020-06-26 14:32:12 -0700237 const TranscodingRequestParcel& request,
238 const std::shared_ptr<ITranscodingClientCallback>& clientCb) {
hkuang34377c32020-09-24 22:28:27 -0700239 queueEvent(Event::Resume, clientId, sessionId, [=, &request] {
Chong Zhangbc062482020-10-14 16:43:53 -0700240 media_status_t err = handleResume(clientId, sessionId, request, clientCb);
Chong Zhangf9077512020-09-21 21:02:06 -0700241 if (err != AMEDIA_OK) {
Chong Zhangb55c5452020-06-26 14:32:12 -0700242 cleanup();
Chong Zhangbc062482020-10-14 16:43:53 -0700243 reportError(clientId, sessionId, err);
Chong Zhangb55c5452020-06-26 14:32:12 -0700244 } else {
Chong Zhangf9077512020-09-21 21:02:06 -0700245 auto callback = mCallback.lock();
Chong Zhangb55c5452020-06-26 14:32:12 -0700246 if (callback != nullptr) {
Chong Zhangbc062482020-10-14 16:43:53 -0700247 callback->onResumed(clientId, sessionId);
Chong Zhangb55c5452020-06-26 14:32:12 -0700248 }
249 }
250 });
Chong Zhang66469272020-06-04 16:51:55 -0700251}
252
Chong Zhangbc062482020-10-14 16:43:53 -0700253void TranscoderWrapper::stop(ClientIdType clientId, SessionIdType sessionId) {
254 queueEvent(Event::Stop, clientId, sessionId, [=] {
255 if (mTranscoder != nullptr && clientId == mCurrentClientId &&
256 sessionId == mCurrentSessionId) {
257 // Cancelling the currently running session.
Chong Zhangb55c5452020-06-26 14:32:12 -0700258 media_status_t err = mTranscoder->cancel();
259 if (err != AMEDIA_OK) {
Chong Zhangf9077512020-09-21 21:02:06 -0700260 ALOGW("failed to stop transcoder: %d", err);
Chong Zhangb55c5452020-06-26 14:32:12 -0700261 } else {
262 ALOGI("transcoder stopped");
263 }
264 cleanup();
Chong Zhang66469272020-06-04 16:51:55 -0700265 } else {
Chong Zhangbc062482020-10-14 16:43:53 -0700266 // For sessions that's not currently running, release any pausedState for the session.
267 mPausedStateMap.erase(SessionKeyType(clientId, sessionId));
Chong Zhang66469272020-06-04 16:51:55 -0700268 }
Chong Zhangb55c5452020-06-26 14:32:12 -0700269 // No callback needed for stop.
Chong Zhang66469272020-06-04 16:51:55 -0700270 });
271}
272
Chong Zhangbc062482020-10-14 16:43:53 -0700273void TranscoderWrapper::onFinish(ClientIdType clientId, SessionIdType sessionId) {
274 queueEvent(Event::Finish, clientId, sessionId, [=] {
275 if (mTranscoder != nullptr && clientId == mCurrentClientId &&
276 sessionId == mCurrentSessionId) {
Chong Zhangb55c5452020-06-26 14:32:12 -0700277 cleanup();
278 }
Chong Zhang66469272020-06-04 16:51:55 -0700279
280 auto callback = mCallback.lock();
281 if (callback != nullptr) {
Chong Zhangbc062482020-10-14 16:43:53 -0700282 callback->onFinish(clientId, sessionId);
Chong Zhang66469272020-06-04 16:51:55 -0700283 }
284 });
285}
286
Chong Zhangbc062482020-10-14 16:43:53 -0700287void TranscoderWrapper::onError(ClientIdType clientId, SessionIdType sessionId,
288 media_status_t error) {
Chong Zhangf9077512020-09-21 21:02:06 -0700289 queueEvent(
Chong Zhangbc062482020-10-14 16:43:53 -0700290 Event::Error, clientId, sessionId,
Chong Zhangf9077512020-09-21 21:02:06 -0700291 [=] {
292 if (mTranscoder != nullptr && clientId == mCurrentClientId &&
Chong Zhangbc062482020-10-14 16:43:53 -0700293 sessionId == mCurrentSessionId) {
Chong Zhangf9077512020-09-21 21:02:06 -0700294 cleanup();
295 }
Chong Zhangbc062482020-10-14 16:43:53 -0700296 reportError(clientId, sessionId, error);
Chong Zhangf9077512020-09-21 21:02:06 -0700297 },
298 error);
Chong Zhang66469272020-06-04 16:51:55 -0700299}
300
Chong Zhangbc062482020-10-14 16:43:53 -0700301void TranscoderWrapper::onProgress(ClientIdType clientId, SessionIdType sessionId,
302 int32_t progress) {
Chong Zhangf9077512020-09-21 21:02:06 -0700303 queueEvent(
Chong Zhangbc062482020-10-14 16:43:53 -0700304 Event::Progress, clientId, sessionId,
Chong Zhangf9077512020-09-21 21:02:06 -0700305 [=] {
306 auto callback = mCallback.lock();
307 if (callback != nullptr) {
Chong Zhangbc062482020-10-14 16:43:53 -0700308 callback->onProgressUpdate(clientId, sessionId, progress);
Chong Zhangf9077512020-09-21 21:02:06 -0700309 }
310 },
311 progress);
Chong Zhang98b8a372020-07-08 17:27:37 -0700312}
313
Chong Zhangf9077512020-09-21 21:02:06 -0700314media_status_t TranscoderWrapper::setupTranscoder(
Chong Zhangbc062482020-10-14 16:43:53 -0700315 ClientIdType clientId, SessionIdType sessionId, const TranscodingRequestParcel& request,
Chong Zhangb55c5452020-06-26 14:32:12 -0700316 const std::shared_ptr<ITranscodingClientCallback>& clientCb,
Chong Zhange4e088f2020-10-21 19:10:42 -0700317 const std::shared_ptr<ndk::ScopedAParcel>& pausedState) {
Chong Zhang66469272020-06-04 16:51:55 -0700318 if (clientCb == nullptr) {
319 ALOGE("client callback is null");
Chong Zhangf9077512020-09-21 21:02:06 -0700320 return AMEDIA_ERROR_INVALID_PARAMETER;
Chong Zhang66469272020-06-04 16:51:55 -0700321 }
322
323 if (mTranscoder != nullptr) {
324 ALOGE("transcoder already running");
Chong Zhangf9077512020-09-21 21:02:06 -0700325 return AMEDIA_ERROR_INVALID_OPERATION;
Chong Zhang66469272020-06-04 16:51:55 -0700326 }
327
328 Status status;
329 ::ndk::ScopedFileDescriptor srcFd, dstFd;
hkuang34377c32020-09-24 22:28:27 -0700330 int srcFdInt = request.sourceFd.get();
331 if (srcFdInt < 0) {
332 status = clientCb->openFileDescriptor(request.sourceFilePath, "r", &srcFd);
333 if (!status.isOk() || srcFd.get() < 0) {
334 ALOGE("failed to open source");
335 return AMEDIA_ERROR_IO;
336 }
337 srcFdInt = srcFd.get();
Chong Zhang66469272020-06-04 16:51:55 -0700338 }
339
hkuang34377c32020-09-24 22:28:27 -0700340 int dstFdInt = request.destinationFd.get();
341 if (dstFdInt < 0) {
342 // Open dest file with "rw", as the transcoder could potentially reuse part of it
343 // for resume case. We might want the further differentiate and open with "w" only
344 // for start.
345 status = clientCb->openFileDescriptor(request.destinationFilePath, "rw", &dstFd);
346 if (!status.isOk() || dstFd.get() < 0) {
347 ALOGE("failed to open destination");
348 return AMEDIA_ERROR_IO;
349 }
350 dstFdInt = dstFd.get();
Chong Zhang66469272020-06-04 16:51:55 -0700351 }
352
353 mCurrentClientId = clientId;
Chong Zhangbc062482020-10-14 16:43:53 -0700354 mCurrentSessionId = sessionId;
355 mTranscoderCb = std::make_shared<CallbackImpl>(shared_from_this(), clientId, sessionId);
Chong Zhangbbb4eac2020-11-18 11:12:06 -0800356 mTranscoder = MediaTranscoder::create(mTranscoderCb, request.clientPid, request.clientUid,
357 pausedState);
Chong Zhang66469272020-06-04 16:51:55 -0700358 if (mTranscoder == nullptr) {
359 ALOGE("failed to create transcoder");
Chong Zhangf9077512020-09-21 21:02:06 -0700360 return AMEDIA_ERROR_UNKNOWN;
Chong Zhang66469272020-06-04 16:51:55 -0700361 }
362
hkuang34377c32020-09-24 22:28:27 -0700363 media_status_t err = mTranscoder->configureSource(srcFdInt);
Chong Zhang66469272020-06-04 16:51:55 -0700364 if (err != AMEDIA_OK) {
365 ALOGE("failed to configure source: %d", err);
Chong Zhangf9077512020-09-21 21:02:06 -0700366 return err;
Chong Zhang66469272020-06-04 16:51:55 -0700367 }
368
369 std::vector<std::shared_ptr<AMediaFormat>> trackFormats = mTranscoder->getTrackFormats();
370 if (trackFormats.size() == 0) {
371 ALOGE("failed to get track formats!");
Chong Zhangf9077512020-09-21 21:02:06 -0700372 return AMEDIA_ERROR_MALFORMED;
Chong Zhang66469272020-06-04 16:51:55 -0700373 }
374
375 for (int i = 0; i < trackFormats.size(); ++i) {
376 AMediaFormat* format = nullptr;
377 const char* mime = nullptr;
378 AMediaFormat_getString(trackFormats[i].get(), AMEDIAFORMAT_KEY_MIME, &mime);
379
380 if (!strncmp(mime, "video/", 6)) {
381 format = getVideoFormat(mime, request.requestedVideoTrackFormat);
382 }
383
384 err = mTranscoder->configureTrackFormat(i, format);
385 if (format != nullptr) {
386 AMediaFormat_delete(format);
387 }
388 if (err != AMEDIA_OK) {
389 ALOGE("failed to configure track format for track %d: %d", i, err);
Chong Zhangf9077512020-09-21 21:02:06 -0700390 return err;
Chong Zhang66469272020-06-04 16:51:55 -0700391 }
392 }
393
hkuang34377c32020-09-24 22:28:27 -0700394 err = mTranscoder->configureDestination(dstFdInt);
Chong Zhang66469272020-06-04 16:51:55 -0700395 if (err != AMEDIA_OK) {
396 ALOGE("failed to configure dest: %d", err);
Chong Zhangf9077512020-09-21 21:02:06 -0700397 return err;
Chong Zhang66469272020-06-04 16:51:55 -0700398 }
399
Chong Zhangf9077512020-09-21 21:02:06 -0700400 return AMEDIA_OK;
Chong Zhangb55c5452020-06-26 14:32:12 -0700401}
402
Chong Zhangf9077512020-09-21 21:02:06 -0700403media_status_t TranscoderWrapper::handleStart(
Chong Zhangbc062482020-10-14 16:43:53 -0700404 ClientIdType clientId, SessionIdType sessionId, const TranscodingRequestParcel& request,
Chong Zhangb55c5452020-06-26 14:32:12 -0700405 const std::shared_ptr<ITranscodingClientCallback>& clientCb) {
Chong Zhangf9077512020-09-21 21:02:06 -0700406 ALOGI("%s: setting up transcoder for start", __FUNCTION__);
Chong Zhangbc062482020-10-14 16:43:53 -0700407 media_status_t err = setupTranscoder(clientId, sessionId, request, clientCb);
Chong Zhangf9077512020-09-21 21:02:06 -0700408 if (err != AMEDIA_OK) {
Chong Zhangb55c5452020-06-26 14:32:12 -0700409 ALOGI("%s: failed to setup transcoder", __FUNCTION__);
410 return err;
Chong Zhang66469272020-06-04 16:51:55 -0700411 }
412
Chong Zhangf9077512020-09-21 21:02:06 -0700413 err = mTranscoder->start();
414 if (err != AMEDIA_OK) {
Chong Zhangb55c5452020-06-26 14:32:12 -0700415 ALOGE("%s: failed to start transcoder: %d", __FUNCTION__, err);
Chong Zhangf9077512020-09-21 21:02:06 -0700416 return err;
Chong Zhangb55c5452020-06-26 14:32:12 -0700417 }
418
419 ALOGI("%s: transcoder started", __FUNCTION__);
Chong Zhangf9077512020-09-21 21:02:06 -0700420 return AMEDIA_OK;
Chong Zhangb55c5452020-06-26 14:32:12 -0700421}
422
Chong Zhangbc062482020-10-14 16:43:53 -0700423media_status_t TranscoderWrapper::handlePause(ClientIdType clientId, SessionIdType sessionId) {
Chong Zhangb55c5452020-06-26 14:32:12 -0700424 if (mTranscoder == nullptr) {
425 ALOGE("%s: transcoder is not running", __FUNCTION__);
Chong Zhangf9077512020-09-21 21:02:06 -0700426 return AMEDIA_ERROR_INVALID_OPERATION;
Chong Zhangb55c5452020-06-26 14:32:12 -0700427 }
428
Chong Zhangbc062482020-10-14 16:43:53 -0700429 if (clientId != mCurrentClientId || sessionId != mCurrentSessionId) {
430 ALOGW("%s: stopping session {%lld, %d} that's not current session {%lld, %d}", __FUNCTION__,
431 (long long)clientId, sessionId, (long long)mCurrentClientId, mCurrentSessionId);
Chong Zhangb55c5452020-06-26 14:32:12 -0700432 }
433
Chong Zhangf9077512020-09-21 21:02:06 -0700434 ALOGI("%s: pausing transcoder", __FUNCTION__);
435
Chong Zhange4e088f2020-10-21 19:10:42 -0700436 std::shared_ptr<ndk::ScopedAParcel> pauseStates;
Chong Zhangb55c5452020-06-26 14:32:12 -0700437 media_status_t err = mTranscoder->pause(&pauseStates);
438 if (err != AMEDIA_OK) {
439 ALOGE("%s: failed to pause transcoder: %d", __FUNCTION__, err);
Chong Zhangf9077512020-09-21 21:02:06 -0700440 return err;
Chong Zhangb55c5452020-06-26 14:32:12 -0700441 }
Chong Zhangbc062482020-10-14 16:43:53 -0700442 mPausedStateMap[SessionKeyType(clientId, sessionId)] = pauseStates;
Chong Zhangb55c5452020-06-26 14:32:12 -0700443
444 ALOGI("%s: transcoder paused", __FUNCTION__);
Chong Zhangf9077512020-09-21 21:02:06 -0700445 return AMEDIA_OK;
Chong Zhangb55c5452020-06-26 14:32:12 -0700446}
447
Chong Zhangf9077512020-09-21 21:02:06 -0700448media_status_t TranscoderWrapper::handleResume(
Chong Zhangbc062482020-10-14 16:43:53 -0700449 ClientIdType clientId, SessionIdType sessionId, const TranscodingRequestParcel& request,
Chong Zhangb55c5452020-06-26 14:32:12 -0700450 const std::shared_ptr<ITranscodingClientCallback>& clientCb) {
Chong Zhange4e088f2020-10-21 19:10:42 -0700451 std::shared_ptr<ndk::ScopedAParcel> pausedState;
Chong Zhangbc062482020-10-14 16:43:53 -0700452 auto it = mPausedStateMap.find(SessionKeyType(clientId, sessionId));
Chong Zhangb55c5452020-06-26 14:32:12 -0700453 if (it != mPausedStateMap.end()) {
454 pausedState = it->second;
455 mPausedStateMap.erase(it);
456 } else {
457 ALOGE("%s: can't find paused state", __FUNCTION__);
Chong Zhangf9077512020-09-21 21:02:06 -0700458 return AMEDIA_ERROR_INVALID_OPERATION;
Chong Zhangb55c5452020-06-26 14:32:12 -0700459 }
460
Chong Zhangf9077512020-09-21 21:02:06 -0700461 ALOGI("%s: setting up transcoder for resume", __FUNCTION__);
Chong Zhangbc062482020-10-14 16:43:53 -0700462 media_status_t err = setupTranscoder(clientId, sessionId, request, clientCb, pausedState);
Chong Zhangf9077512020-09-21 21:02:06 -0700463 if (err != AMEDIA_OK) {
464 ALOGE("%s: failed to setup transcoder: %d", __FUNCTION__, err);
Chong Zhangb55c5452020-06-26 14:32:12 -0700465 return err;
466 }
467
Chong Zhangf9077512020-09-21 21:02:06 -0700468 err = mTranscoder->resume();
469 if (err != AMEDIA_OK) {
Chong Zhangb55c5452020-06-26 14:32:12 -0700470 ALOGE("%s: failed to resume transcoder: %d", __FUNCTION__, err);
Chong Zhangf9077512020-09-21 21:02:06 -0700471 return err;
Chong Zhangb55c5452020-06-26 14:32:12 -0700472 }
473
474 ALOGI("%s: transcoder resumed", __FUNCTION__);
Chong Zhangf9077512020-09-21 21:02:06 -0700475 return AMEDIA_OK;
Chong Zhang66469272020-06-04 16:51:55 -0700476}
477
478void TranscoderWrapper::cleanup() {
479 mCurrentClientId = 0;
Chong Zhangbc062482020-10-14 16:43:53 -0700480 mCurrentSessionId = -1;
Chong Zhang66469272020-06-04 16:51:55 -0700481 mTranscoderCb = nullptr;
482 mTranscoder = nullptr;
483}
484
Chong Zhangbc062482020-10-14 16:43:53 -0700485void TranscoderWrapper::queueEvent(Event::Type type, ClientIdType clientId, SessionIdType sessionId,
Chong Zhangf9077512020-09-21 21:02:06 -0700486 const std::function<void()> runnable, int32_t arg) {
Chong Zhang66469272020-06-04 16:51:55 -0700487 std::scoped_lock lock{mLock};
488
Chong Zhangbc062482020-10-14 16:43:53 -0700489 mQueue.push_back({type, clientId, sessionId, runnable, arg});
Chong Zhang66469272020-06-04 16:51:55 -0700490 mCondition.notify_one();
491}
492
493void TranscoderWrapper::threadLoop() {
494 std::unique_lock<std::mutex> lock{mLock};
495 // TranscoderWrapper currently lives in the transcoding service, as long as
496 // MediaTranscodingService itself.
497 while (true) {
498 // Wait for the next event.
499 while (mQueue.empty()) {
500 mCondition.wait(lock);
501 }
502
503 Event event = *mQueue.begin();
504 mQueue.pop_front();
505
Chong Zhangf9077512020-09-21 21:02:06 -0700506 ALOGD("%s: %s", __FUNCTION__, toString(event).c_str());
Chong Zhang66469272020-06-04 16:51:55 -0700507
Chong Zhangb55c5452020-06-26 14:32:12 -0700508 lock.unlock();
Chong Zhang66469272020-06-04 16:51:55 -0700509 event.runnable();
Chong Zhangb55c5452020-06-26 14:32:12 -0700510 lock.lock();
Chong Zhang66469272020-06-04 16:51:55 -0700511 }
512}
513
514} // namespace android