blob: da861878a67c3ed6e44677d3590ee84bbe0ff384 [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) {
Chong Zhangbc062482020-10-14 16:43:53 -0700205 queueEvent(Event::Start, clientId, sessionId, [=] {
206 media_status_t err = handleStart(clientId, sessionId, request, clientCb);
Chong Zhang66469272020-06-04 16:51:55 -0700207
Chong Zhangf9077512020-09-21 21:02:06 -0700208 if (err != AMEDIA_OK) {
Chong Zhang66469272020-06-04 16:51:55 -0700209 cleanup();
Chong Zhangbc062482020-10-14 16:43:53 -0700210 reportError(clientId, sessionId, err);
Chong Zhangb55c5452020-06-26 14:32:12 -0700211 } else {
Chong Zhangf9077512020-09-21 21:02:06 -0700212 auto callback = mCallback.lock();
Chong Zhangb55c5452020-06-26 14:32:12 -0700213 if (callback != nullptr) {
Chong Zhangbc062482020-10-14 16:43:53 -0700214 callback->onStarted(clientId, sessionId);
Chong Zhangb55c5452020-06-26 14:32:12 -0700215 }
Chong Zhang66469272020-06-04 16:51:55 -0700216 }
217 });
218}
219
Chong Zhangbc062482020-10-14 16:43:53 -0700220void TranscoderWrapper::pause(ClientIdType clientId, SessionIdType sessionId) {
221 queueEvent(Event::Pause, clientId, sessionId, [=] {
222 media_status_t err = handlePause(clientId, sessionId);
Chong Zhangb55c5452020-06-26 14:32:12 -0700223
224 cleanup();
225
Chong Zhangf9077512020-09-21 21:02:06 -0700226 if (err != AMEDIA_OK) {
Chong Zhangbc062482020-10-14 16:43:53 -0700227 reportError(clientId, sessionId, err);
Chong Zhangf9077512020-09-21 21:02:06 -0700228 } else {
229 auto callback = mCallback.lock();
230 if (callback != nullptr) {
Chong Zhangbc062482020-10-14 16:43:53 -0700231 callback->onPaused(clientId, sessionId);
Chong Zhangb55c5452020-06-26 14:32:12 -0700232 }
233 }
234 });
Chong Zhang66469272020-06-04 16:51:55 -0700235}
236
Chong Zhangbc062482020-10-14 16:43:53 -0700237void TranscoderWrapper::resume(ClientIdType clientId, SessionIdType sessionId,
Chong Zhangb55c5452020-06-26 14:32:12 -0700238 const TranscodingRequestParcel& request,
239 const std::shared_ptr<ITranscodingClientCallback>& clientCb) {
Chong Zhangbc062482020-10-14 16:43:53 -0700240 queueEvent(Event::Resume, clientId, sessionId, [=] {
241 media_status_t err = handleResume(clientId, sessionId, request, clientCb);
Chong Zhangb55c5452020-06-26 14:32:12 -0700242
Chong Zhangf9077512020-09-21 21:02:06 -0700243 if (err != AMEDIA_OK) {
Chong Zhangb55c5452020-06-26 14:32:12 -0700244 cleanup();
Chong Zhangbc062482020-10-14 16:43:53 -0700245 reportError(clientId, sessionId, err);
Chong Zhangb55c5452020-06-26 14:32:12 -0700246 } else {
Chong Zhangf9077512020-09-21 21:02:06 -0700247 auto callback = mCallback.lock();
Chong Zhangb55c5452020-06-26 14:32:12 -0700248 if (callback != nullptr) {
Chong Zhangbc062482020-10-14 16:43:53 -0700249 callback->onResumed(clientId, sessionId);
Chong Zhangb55c5452020-06-26 14:32:12 -0700250 }
251 }
252 });
Chong Zhang66469272020-06-04 16:51:55 -0700253}
254
Chong Zhangbc062482020-10-14 16:43:53 -0700255void TranscoderWrapper::stop(ClientIdType clientId, SessionIdType sessionId) {
256 queueEvent(Event::Stop, clientId, sessionId, [=] {
257 if (mTranscoder != nullptr && clientId == mCurrentClientId &&
258 sessionId == mCurrentSessionId) {
259 // Cancelling the currently running session.
Chong Zhangb55c5452020-06-26 14:32:12 -0700260 media_status_t err = mTranscoder->cancel();
261 if (err != AMEDIA_OK) {
Chong Zhangf9077512020-09-21 21:02:06 -0700262 ALOGW("failed to stop transcoder: %d", err);
Chong Zhangb55c5452020-06-26 14:32:12 -0700263 } else {
264 ALOGI("transcoder stopped");
265 }
266 cleanup();
Chong Zhang66469272020-06-04 16:51:55 -0700267 } else {
Chong Zhangbc062482020-10-14 16:43:53 -0700268 // For sessions that's not currently running, release any pausedState for the session.
269 mPausedStateMap.erase(SessionKeyType(clientId, sessionId));
Chong Zhang66469272020-06-04 16:51:55 -0700270 }
Chong Zhangb55c5452020-06-26 14:32:12 -0700271 // No callback needed for stop.
Chong Zhang66469272020-06-04 16:51:55 -0700272 });
273}
274
Chong Zhangbc062482020-10-14 16:43:53 -0700275void TranscoderWrapper::onFinish(ClientIdType clientId, SessionIdType sessionId) {
276 queueEvent(Event::Finish, clientId, sessionId, [=] {
277 if (mTranscoder != nullptr && clientId == mCurrentClientId &&
278 sessionId == mCurrentSessionId) {
Chong Zhangb55c5452020-06-26 14:32:12 -0700279 cleanup();
280 }
Chong Zhang66469272020-06-04 16:51:55 -0700281
282 auto callback = mCallback.lock();
283 if (callback != nullptr) {
Chong Zhangbc062482020-10-14 16:43:53 -0700284 callback->onFinish(clientId, sessionId);
Chong Zhang66469272020-06-04 16:51:55 -0700285 }
286 });
287}
288
Chong Zhangbc062482020-10-14 16:43:53 -0700289void TranscoderWrapper::onError(ClientIdType clientId, SessionIdType sessionId,
290 media_status_t error) {
Chong Zhangf9077512020-09-21 21:02:06 -0700291 queueEvent(
Chong Zhangbc062482020-10-14 16:43:53 -0700292 Event::Error, clientId, sessionId,
Chong Zhangf9077512020-09-21 21:02:06 -0700293 [=] {
294 if (mTranscoder != nullptr && clientId == mCurrentClientId &&
Chong Zhangbc062482020-10-14 16:43:53 -0700295 sessionId == mCurrentSessionId) {
Chong Zhangf9077512020-09-21 21:02:06 -0700296 cleanup();
297 }
Chong Zhangbc062482020-10-14 16:43:53 -0700298 reportError(clientId, sessionId, error);
Chong Zhangf9077512020-09-21 21:02:06 -0700299 },
300 error);
Chong Zhang66469272020-06-04 16:51:55 -0700301}
302
Chong Zhangbc062482020-10-14 16:43:53 -0700303void TranscoderWrapper::onProgress(ClientIdType clientId, SessionIdType sessionId,
304 int32_t progress) {
Chong Zhangf9077512020-09-21 21:02:06 -0700305 queueEvent(
Chong Zhangbc062482020-10-14 16:43:53 -0700306 Event::Progress, clientId, sessionId,
Chong Zhangf9077512020-09-21 21:02:06 -0700307 [=] {
308 auto callback = mCallback.lock();
309 if (callback != nullptr) {
Chong Zhangbc062482020-10-14 16:43:53 -0700310 callback->onProgressUpdate(clientId, sessionId, progress);
Chong Zhangf9077512020-09-21 21:02:06 -0700311 }
312 },
313 progress);
Chong Zhang98b8a372020-07-08 17:27:37 -0700314}
315
Chong Zhangf9077512020-09-21 21:02:06 -0700316media_status_t TranscoderWrapper::setupTranscoder(
Chong Zhangbc062482020-10-14 16:43:53 -0700317 ClientIdType clientId, SessionIdType sessionId, const TranscodingRequestParcel& request,
Chong Zhangb55c5452020-06-26 14:32:12 -0700318 const std::shared_ptr<ITranscodingClientCallback>& clientCb,
Chong Zhange4e088f2020-10-21 19:10:42 -0700319 const std::shared_ptr<ndk::ScopedAParcel>& pausedState) {
Chong Zhang66469272020-06-04 16:51:55 -0700320 if (clientCb == nullptr) {
321 ALOGE("client callback is null");
Chong Zhangf9077512020-09-21 21:02:06 -0700322 return AMEDIA_ERROR_INVALID_PARAMETER;
Chong Zhang66469272020-06-04 16:51:55 -0700323 }
324
325 if (mTranscoder != nullptr) {
326 ALOGE("transcoder already running");
Chong Zhangf9077512020-09-21 21:02:06 -0700327 return AMEDIA_ERROR_INVALID_OPERATION;
Chong Zhang66469272020-06-04 16:51:55 -0700328 }
329
330 Status status;
331 ::ndk::ScopedFileDescriptor srcFd, dstFd;
332 status = clientCb->openFileDescriptor(request.sourceFilePath, "r", &srcFd);
333 if (!status.isOk() || srcFd.get() < 0) {
334 ALOGE("failed to open source");
Chong Zhangf9077512020-09-21 21:02:06 -0700335 return AMEDIA_ERROR_IO;
Chong Zhang66469272020-06-04 16:51:55 -0700336 }
337
Chong Zhangb55c5452020-06-26 14:32:12 -0700338 // Open dest file with "rw", as the transcoder could potentially reuse part of it
339 // for resume case. We might want the further differentiate and open with "w" only
340 // for start.
341 status = clientCb->openFileDescriptor(request.destinationFilePath, "rw", &dstFd);
Chong Zhang66469272020-06-04 16:51:55 -0700342 if (!status.isOk() || dstFd.get() < 0) {
343 ALOGE("failed to open destination");
Chong Zhangf9077512020-09-21 21:02:06 -0700344 return AMEDIA_ERROR_IO;
Chong Zhang66469272020-06-04 16:51:55 -0700345 }
346
347 mCurrentClientId = clientId;
Chong Zhangbc062482020-10-14 16:43:53 -0700348 mCurrentSessionId = sessionId;
349 mTranscoderCb = std::make_shared<CallbackImpl>(shared_from_this(), clientId, sessionId);
Chong Zhangbbb4eac2020-11-18 11:12:06 -0800350 mTranscoder = MediaTranscoder::create(mTranscoderCb, request.clientPid, request.clientUid,
351 pausedState);
Chong Zhang66469272020-06-04 16:51:55 -0700352 if (mTranscoder == nullptr) {
353 ALOGE("failed to create transcoder");
Chong Zhangf9077512020-09-21 21:02:06 -0700354 return AMEDIA_ERROR_UNKNOWN;
Chong Zhang66469272020-06-04 16:51:55 -0700355 }
356
357 media_status_t err = mTranscoder->configureSource(srcFd.get());
358 if (err != AMEDIA_OK) {
359 ALOGE("failed to configure source: %d", err);
Chong Zhangf9077512020-09-21 21:02:06 -0700360 return err;
Chong Zhang66469272020-06-04 16:51:55 -0700361 }
362
363 std::vector<std::shared_ptr<AMediaFormat>> trackFormats = mTranscoder->getTrackFormats();
364 if (trackFormats.size() == 0) {
365 ALOGE("failed to get track formats!");
Chong Zhangf9077512020-09-21 21:02:06 -0700366 return AMEDIA_ERROR_MALFORMED;
Chong Zhang66469272020-06-04 16:51:55 -0700367 }
368
369 for (int i = 0; i < trackFormats.size(); ++i) {
370 AMediaFormat* format = nullptr;
371 const char* mime = nullptr;
372 AMediaFormat_getString(trackFormats[i].get(), AMEDIAFORMAT_KEY_MIME, &mime);
373
374 if (!strncmp(mime, "video/", 6)) {
375 format = getVideoFormat(mime, request.requestedVideoTrackFormat);
376 }
377
378 err = mTranscoder->configureTrackFormat(i, format);
379 if (format != nullptr) {
380 AMediaFormat_delete(format);
381 }
382 if (err != AMEDIA_OK) {
383 ALOGE("failed to configure track format for track %d: %d", i, err);
Chong Zhangf9077512020-09-21 21:02:06 -0700384 return err;
Chong Zhang66469272020-06-04 16:51:55 -0700385 }
386 }
387
388 err = mTranscoder->configureDestination(dstFd.get());
389 if (err != AMEDIA_OK) {
390 ALOGE("failed to configure dest: %d", err);
Chong Zhangf9077512020-09-21 21:02:06 -0700391 return err;
Chong Zhang66469272020-06-04 16:51:55 -0700392 }
393
Chong Zhangf9077512020-09-21 21:02:06 -0700394 return AMEDIA_OK;
Chong Zhangb55c5452020-06-26 14:32:12 -0700395}
396
Chong Zhangf9077512020-09-21 21:02:06 -0700397media_status_t TranscoderWrapper::handleStart(
Chong Zhangbc062482020-10-14 16:43:53 -0700398 ClientIdType clientId, SessionIdType sessionId, const TranscodingRequestParcel& request,
Chong Zhangb55c5452020-06-26 14:32:12 -0700399 const std::shared_ptr<ITranscodingClientCallback>& clientCb) {
Chong Zhangf9077512020-09-21 21:02:06 -0700400 ALOGI("%s: setting up transcoder for start", __FUNCTION__);
Chong Zhangbc062482020-10-14 16:43:53 -0700401 media_status_t err = setupTranscoder(clientId, sessionId, request, clientCb);
Chong Zhangf9077512020-09-21 21:02:06 -0700402 if (err != AMEDIA_OK) {
Chong Zhangb55c5452020-06-26 14:32:12 -0700403 ALOGI("%s: failed to setup transcoder", __FUNCTION__);
404 return err;
Chong Zhang66469272020-06-04 16:51:55 -0700405 }
406
Chong Zhangf9077512020-09-21 21:02:06 -0700407 err = mTranscoder->start();
408 if (err != AMEDIA_OK) {
Chong Zhangb55c5452020-06-26 14:32:12 -0700409 ALOGE("%s: failed to start transcoder: %d", __FUNCTION__, err);
Chong Zhangf9077512020-09-21 21:02:06 -0700410 return err;
Chong Zhangb55c5452020-06-26 14:32:12 -0700411 }
412
413 ALOGI("%s: transcoder started", __FUNCTION__);
Chong Zhangf9077512020-09-21 21:02:06 -0700414 return AMEDIA_OK;
Chong Zhangb55c5452020-06-26 14:32:12 -0700415}
416
Chong Zhangbc062482020-10-14 16:43:53 -0700417media_status_t TranscoderWrapper::handlePause(ClientIdType clientId, SessionIdType sessionId) {
Chong Zhangb55c5452020-06-26 14:32:12 -0700418 if (mTranscoder == nullptr) {
419 ALOGE("%s: transcoder is not running", __FUNCTION__);
Chong Zhangf9077512020-09-21 21:02:06 -0700420 return AMEDIA_ERROR_INVALID_OPERATION;
Chong Zhangb55c5452020-06-26 14:32:12 -0700421 }
422
Chong Zhangbc062482020-10-14 16:43:53 -0700423 if (clientId != mCurrentClientId || sessionId != mCurrentSessionId) {
424 ALOGW("%s: stopping session {%lld, %d} that's not current session {%lld, %d}", __FUNCTION__,
425 (long long)clientId, sessionId, (long long)mCurrentClientId, mCurrentSessionId);
Chong Zhangb55c5452020-06-26 14:32:12 -0700426 }
427
Chong Zhangf9077512020-09-21 21:02:06 -0700428 ALOGI("%s: pausing transcoder", __FUNCTION__);
429
Chong Zhange4e088f2020-10-21 19:10:42 -0700430 std::shared_ptr<ndk::ScopedAParcel> pauseStates;
Chong Zhangb55c5452020-06-26 14:32:12 -0700431 media_status_t err = mTranscoder->pause(&pauseStates);
432 if (err != AMEDIA_OK) {
433 ALOGE("%s: failed to pause transcoder: %d", __FUNCTION__, err);
Chong Zhangf9077512020-09-21 21:02:06 -0700434 return err;
Chong Zhangb55c5452020-06-26 14:32:12 -0700435 }
Chong Zhangbc062482020-10-14 16:43:53 -0700436 mPausedStateMap[SessionKeyType(clientId, sessionId)] = pauseStates;
Chong Zhangb55c5452020-06-26 14:32:12 -0700437
438 ALOGI("%s: transcoder paused", __FUNCTION__);
Chong Zhangf9077512020-09-21 21:02:06 -0700439 return AMEDIA_OK;
Chong Zhangb55c5452020-06-26 14:32:12 -0700440}
441
Chong Zhangf9077512020-09-21 21:02:06 -0700442media_status_t TranscoderWrapper::handleResume(
Chong Zhangbc062482020-10-14 16:43:53 -0700443 ClientIdType clientId, SessionIdType sessionId, const TranscodingRequestParcel& request,
Chong Zhangb55c5452020-06-26 14:32:12 -0700444 const std::shared_ptr<ITranscodingClientCallback>& clientCb) {
Chong Zhange4e088f2020-10-21 19:10:42 -0700445 std::shared_ptr<ndk::ScopedAParcel> pausedState;
Chong Zhangbc062482020-10-14 16:43:53 -0700446 auto it = mPausedStateMap.find(SessionKeyType(clientId, sessionId));
Chong Zhangb55c5452020-06-26 14:32:12 -0700447 if (it != mPausedStateMap.end()) {
448 pausedState = it->second;
449 mPausedStateMap.erase(it);
450 } else {
451 ALOGE("%s: can't find paused state", __FUNCTION__);
Chong Zhangf9077512020-09-21 21:02:06 -0700452 return AMEDIA_ERROR_INVALID_OPERATION;
Chong Zhangb55c5452020-06-26 14:32:12 -0700453 }
454
Chong Zhangf9077512020-09-21 21:02:06 -0700455 ALOGI("%s: setting up transcoder for resume", __FUNCTION__);
Chong Zhangbc062482020-10-14 16:43:53 -0700456 media_status_t err = setupTranscoder(clientId, sessionId, request, clientCb, pausedState);
Chong Zhangf9077512020-09-21 21:02:06 -0700457 if (err != AMEDIA_OK) {
458 ALOGE("%s: failed to setup transcoder: %d", __FUNCTION__, err);
Chong Zhangb55c5452020-06-26 14:32:12 -0700459 return err;
460 }
461
Chong Zhangf9077512020-09-21 21:02:06 -0700462 err = mTranscoder->resume();
463 if (err != AMEDIA_OK) {
Chong Zhangb55c5452020-06-26 14:32:12 -0700464 ALOGE("%s: failed to resume transcoder: %d", __FUNCTION__, err);
Chong Zhangf9077512020-09-21 21:02:06 -0700465 return err;
Chong Zhangb55c5452020-06-26 14:32:12 -0700466 }
467
468 ALOGI("%s: transcoder resumed", __FUNCTION__);
Chong Zhangf9077512020-09-21 21:02:06 -0700469 return AMEDIA_OK;
Chong Zhang66469272020-06-04 16:51:55 -0700470}
471
472void TranscoderWrapper::cleanup() {
473 mCurrentClientId = 0;
Chong Zhangbc062482020-10-14 16:43:53 -0700474 mCurrentSessionId = -1;
Chong Zhang66469272020-06-04 16:51:55 -0700475 mTranscoderCb = nullptr;
476 mTranscoder = nullptr;
477}
478
Chong Zhangbc062482020-10-14 16:43:53 -0700479void TranscoderWrapper::queueEvent(Event::Type type, ClientIdType clientId, SessionIdType sessionId,
Chong Zhangf9077512020-09-21 21:02:06 -0700480 const std::function<void()> runnable, int32_t arg) {
Chong Zhang66469272020-06-04 16:51:55 -0700481 std::scoped_lock lock{mLock};
482
Chong Zhangbc062482020-10-14 16:43:53 -0700483 mQueue.push_back({type, clientId, sessionId, runnable, arg});
Chong Zhang66469272020-06-04 16:51:55 -0700484 mCondition.notify_one();
485}
486
487void TranscoderWrapper::threadLoop() {
488 std::unique_lock<std::mutex> lock{mLock};
489 // TranscoderWrapper currently lives in the transcoding service, as long as
490 // MediaTranscodingService itself.
491 while (true) {
492 // Wait for the next event.
493 while (mQueue.empty()) {
494 mCondition.wait(lock);
495 }
496
497 Event event = *mQueue.begin();
498 mQueue.pop_front();
499
Chong Zhangf9077512020-09-21 21:02:06 -0700500 ALOGD("%s: %s", __FUNCTION__, toString(event).c_str());
Chong Zhang66469272020-06-04 16:51:55 -0700501
Chong Zhangb55c5452020-06-26 14:32:12 -0700502 lock.unlock();
Chong Zhang66469272020-06-04 16:51:55 -0700503 event.runnable();
Chong Zhangb55c5452020-06-26 14:32:12 -0700504 lock.lock();
Chong Zhang66469272020-06-04 16:51:55 -0700505 }
506}
507
508} // namespace android