blob: 61e767ca18d99427a753b2c031d4356db6db8a70 [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 Zhangb55c5452020-06-26 14:32:12 -0700158 const std::shared_ptr<const Parcel>& 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 Zhangf9077512020-09-21 21:02:06 -0700192 std::shared_ptr<const Parcel>());
193 }
194
195 callback->onResourceLost();
196 } 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,
319 const std::shared_ptr<const Parcel>& 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 Zhangb55c5452020-06-26 14:32:12 -0700350 mTranscoder = MediaTranscoder::create(mTranscoderCb, pausedState);
Chong Zhang66469272020-06-04 16:51:55 -0700351 if (mTranscoder == nullptr) {
352 ALOGE("failed to create transcoder");
Chong Zhangf9077512020-09-21 21:02:06 -0700353 return AMEDIA_ERROR_UNKNOWN;
Chong Zhang66469272020-06-04 16:51:55 -0700354 }
355
356 media_status_t err = mTranscoder->configureSource(srcFd.get());
357 if (err != AMEDIA_OK) {
358 ALOGE("failed to configure source: %d", err);
Chong Zhangf9077512020-09-21 21:02:06 -0700359 return err;
Chong Zhang66469272020-06-04 16:51:55 -0700360 }
361
362 std::vector<std::shared_ptr<AMediaFormat>> trackFormats = mTranscoder->getTrackFormats();
363 if (trackFormats.size() == 0) {
364 ALOGE("failed to get track formats!");
Chong Zhangf9077512020-09-21 21:02:06 -0700365 return AMEDIA_ERROR_MALFORMED;
Chong Zhang66469272020-06-04 16:51:55 -0700366 }
367
368 for (int i = 0; i < trackFormats.size(); ++i) {
369 AMediaFormat* format = nullptr;
370 const char* mime = nullptr;
371 AMediaFormat_getString(trackFormats[i].get(), AMEDIAFORMAT_KEY_MIME, &mime);
372
373 if (!strncmp(mime, "video/", 6)) {
374 format = getVideoFormat(mime, request.requestedVideoTrackFormat);
375 }
376
377 err = mTranscoder->configureTrackFormat(i, format);
378 if (format != nullptr) {
379 AMediaFormat_delete(format);
380 }
381 if (err != AMEDIA_OK) {
382 ALOGE("failed to configure track format for track %d: %d", i, err);
Chong Zhangf9077512020-09-21 21:02:06 -0700383 return err;
Chong Zhang66469272020-06-04 16:51:55 -0700384 }
385 }
386
387 err = mTranscoder->configureDestination(dstFd.get());
388 if (err != AMEDIA_OK) {
389 ALOGE("failed to configure dest: %d", err);
Chong Zhangf9077512020-09-21 21:02:06 -0700390 return err;
Chong Zhang66469272020-06-04 16:51:55 -0700391 }
392
Chong Zhangf9077512020-09-21 21:02:06 -0700393 return AMEDIA_OK;
Chong Zhangb55c5452020-06-26 14:32:12 -0700394}
395
Chong Zhangf9077512020-09-21 21:02:06 -0700396media_status_t TranscoderWrapper::handleStart(
Chong Zhangbc062482020-10-14 16:43:53 -0700397 ClientIdType clientId, SessionIdType sessionId, const TranscodingRequestParcel& request,
Chong Zhangb55c5452020-06-26 14:32:12 -0700398 const std::shared_ptr<ITranscodingClientCallback>& clientCb) {
Chong Zhangf9077512020-09-21 21:02:06 -0700399 ALOGI("%s: setting up transcoder for start", __FUNCTION__);
Chong Zhangbc062482020-10-14 16:43:53 -0700400 media_status_t err = setupTranscoder(clientId, sessionId, request, clientCb);
Chong Zhangf9077512020-09-21 21:02:06 -0700401 if (err != AMEDIA_OK) {
Chong Zhangb55c5452020-06-26 14:32:12 -0700402 ALOGI("%s: failed to setup transcoder", __FUNCTION__);
403 return err;
Chong Zhang66469272020-06-04 16:51:55 -0700404 }
405
Chong Zhangf9077512020-09-21 21:02:06 -0700406 err = mTranscoder->start();
407 if (err != AMEDIA_OK) {
Chong Zhangb55c5452020-06-26 14:32:12 -0700408 ALOGE("%s: failed to start transcoder: %d", __FUNCTION__, err);
Chong Zhangf9077512020-09-21 21:02:06 -0700409 return err;
Chong Zhangb55c5452020-06-26 14:32:12 -0700410 }
411
412 ALOGI("%s: transcoder started", __FUNCTION__);
Chong Zhangf9077512020-09-21 21:02:06 -0700413 return AMEDIA_OK;
Chong Zhangb55c5452020-06-26 14:32:12 -0700414}
415
Chong Zhangbc062482020-10-14 16:43:53 -0700416media_status_t TranscoderWrapper::handlePause(ClientIdType clientId, SessionIdType sessionId) {
Chong Zhangb55c5452020-06-26 14:32:12 -0700417 if (mTranscoder == nullptr) {
418 ALOGE("%s: transcoder is not running", __FUNCTION__);
Chong Zhangf9077512020-09-21 21:02:06 -0700419 return AMEDIA_ERROR_INVALID_OPERATION;
Chong Zhangb55c5452020-06-26 14:32:12 -0700420 }
421
Chong Zhangbc062482020-10-14 16:43:53 -0700422 if (clientId != mCurrentClientId || sessionId != mCurrentSessionId) {
423 ALOGW("%s: stopping session {%lld, %d} that's not current session {%lld, %d}", __FUNCTION__,
424 (long long)clientId, sessionId, (long long)mCurrentClientId, mCurrentSessionId);
Chong Zhangb55c5452020-06-26 14:32:12 -0700425 }
426
Chong Zhangf9077512020-09-21 21:02:06 -0700427 ALOGI("%s: pausing transcoder", __FUNCTION__);
428
Chong Zhangb55c5452020-06-26 14:32:12 -0700429 std::shared_ptr<const Parcel> pauseStates;
430 media_status_t err = mTranscoder->pause(&pauseStates);
431 if (err != AMEDIA_OK) {
432 ALOGE("%s: failed to pause transcoder: %d", __FUNCTION__, err);
Chong Zhangf9077512020-09-21 21:02:06 -0700433 return err;
Chong Zhangb55c5452020-06-26 14:32:12 -0700434 }
Chong Zhangbc062482020-10-14 16:43:53 -0700435 mPausedStateMap[SessionKeyType(clientId, sessionId)] = pauseStates;
Chong Zhangb55c5452020-06-26 14:32:12 -0700436
437 ALOGI("%s: transcoder paused", __FUNCTION__);
Chong Zhangf9077512020-09-21 21:02:06 -0700438 return AMEDIA_OK;
Chong Zhangb55c5452020-06-26 14:32:12 -0700439}
440
Chong Zhangf9077512020-09-21 21:02:06 -0700441media_status_t TranscoderWrapper::handleResume(
Chong Zhangbc062482020-10-14 16:43:53 -0700442 ClientIdType clientId, SessionIdType sessionId, const TranscodingRequestParcel& request,
Chong Zhangb55c5452020-06-26 14:32:12 -0700443 const std::shared_ptr<ITranscodingClientCallback>& clientCb) {
444 std::shared_ptr<const Parcel> pausedState;
Chong Zhangbc062482020-10-14 16:43:53 -0700445 auto it = mPausedStateMap.find(SessionKeyType(clientId, sessionId));
Chong Zhangb55c5452020-06-26 14:32:12 -0700446 if (it != mPausedStateMap.end()) {
447 pausedState = it->second;
448 mPausedStateMap.erase(it);
449 } else {
450 ALOGE("%s: can't find paused state", __FUNCTION__);
Chong Zhangf9077512020-09-21 21:02:06 -0700451 return AMEDIA_ERROR_INVALID_OPERATION;
Chong Zhangb55c5452020-06-26 14:32:12 -0700452 }
453
Chong Zhangf9077512020-09-21 21:02:06 -0700454 ALOGI("%s: setting up transcoder for resume", __FUNCTION__);
Chong Zhangbc062482020-10-14 16:43:53 -0700455 media_status_t err = setupTranscoder(clientId, sessionId, request, clientCb, pausedState);
Chong Zhangf9077512020-09-21 21:02:06 -0700456 if (err != AMEDIA_OK) {
457 ALOGE("%s: failed to setup transcoder: %d", __FUNCTION__, err);
Chong Zhangb55c5452020-06-26 14:32:12 -0700458 return err;
459 }
460
Chong Zhangf9077512020-09-21 21:02:06 -0700461 err = mTranscoder->resume();
462 if (err != AMEDIA_OK) {
Chong Zhangb55c5452020-06-26 14:32:12 -0700463 ALOGE("%s: failed to resume transcoder: %d", __FUNCTION__, err);
Chong Zhangf9077512020-09-21 21:02:06 -0700464 return err;
Chong Zhangb55c5452020-06-26 14:32:12 -0700465 }
466
467 ALOGI("%s: transcoder resumed", __FUNCTION__);
Chong Zhangf9077512020-09-21 21:02:06 -0700468 return AMEDIA_OK;
Chong Zhang66469272020-06-04 16:51:55 -0700469}
470
471void TranscoderWrapper::cleanup() {
472 mCurrentClientId = 0;
Chong Zhangbc062482020-10-14 16:43:53 -0700473 mCurrentSessionId = -1;
Chong Zhang66469272020-06-04 16:51:55 -0700474 mTranscoderCb = nullptr;
475 mTranscoder = nullptr;
476}
477
Chong Zhangbc062482020-10-14 16:43:53 -0700478void TranscoderWrapper::queueEvent(Event::Type type, ClientIdType clientId, SessionIdType sessionId,
Chong Zhangf9077512020-09-21 21:02:06 -0700479 const std::function<void()> runnable, int32_t arg) {
Chong Zhang66469272020-06-04 16:51:55 -0700480 std::scoped_lock lock{mLock};
481
Chong Zhangbc062482020-10-14 16:43:53 -0700482 mQueue.push_back({type, clientId, sessionId, runnable, arg});
Chong Zhang66469272020-06-04 16:51:55 -0700483 mCondition.notify_one();
484}
485
486void TranscoderWrapper::threadLoop() {
487 std::unique_lock<std::mutex> lock{mLock};
488 // TranscoderWrapper currently lives in the transcoding service, as long as
489 // MediaTranscodingService itself.
490 while (true) {
491 // Wait for the next event.
492 while (mQueue.empty()) {
493 mCondition.wait(lock);
494 }
495
496 Event event = *mQueue.begin();
497 mQueue.pop_front();
498
Chong Zhangf9077512020-09-21 21:02:06 -0700499 ALOGD("%s: %s", __FUNCTION__, toString(event).c_str());
Chong Zhang66469272020-06-04 16:51:55 -0700500
Chong Zhangb55c5452020-06-26 14:32:12 -0700501 lock.unlock();
Chong Zhang66469272020-06-04 16:51:55 -0700502 event.runnable();
Chong Zhangb55c5452020-06-26 14:32:12 -0700503 lock.lock();
Chong Zhang66469272020-06-04 16:51:55 -0700504 }
505}
506
507} // namespace android