blob: 4bd41052e0c6fd272043c7e24d25b1cdbd3aec49 [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;
Chong Zhang457c6892021-02-01 15:34:20 -0800116 case Event::HeartBeat:
117 typeStr = "HeartBeat";
118 break;
119 case Event::Abandon:
120 typeStr = "Abandon";
121 break;
Chong Zhangf9077512020-09-21 21:02:06 -0700122 default:
123 return "(unknown)";
Chong Zhang66469272020-06-04 16:51:55 -0700124 }
Chong Zhangf9077512020-09-21 21:02:06 -0700125 std::string result;
Chong Zhangbc062482020-10-14 16:43:53 -0700126 result = "session {" + std::to_string(event.clientId) + "," + std::to_string(event.sessionId) +
Chong Zhangf9077512020-09-21 21:02:06 -0700127 "}: " + typeStr;
128 if (event.type == Event::Error || event.type == Event::Progress) {
129 result += " " + std::to_string(event.arg);
130 }
131 return result;
Chong Zhang66469272020-06-04 16:51:55 -0700132}
133
134class TranscoderWrapper::CallbackImpl : public MediaTranscoder::CallbackInterface {
135public:
136 CallbackImpl(const std::shared_ptr<TranscoderWrapper>& owner, ClientIdType clientId,
Chong Zhangbc062482020-10-14 16:43:53 -0700137 SessionIdType sessionId)
138 : mOwner(owner), mClientId(clientId), mSessionId(sessionId) {}
Chong Zhang66469272020-06-04 16:51:55 -0700139
140 virtual void onFinished(const MediaTranscoder* transcoder __unused) override {
141 auto owner = mOwner.lock();
142 if (owner != nullptr) {
Chong Zhangbc062482020-10-14 16:43:53 -0700143 owner->onFinish(mClientId, mSessionId);
Chong Zhang66469272020-06-04 16:51:55 -0700144 }
145 }
146
147 virtual void onError(const MediaTranscoder* transcoder __unused,
148 media_status_t error) override {
149 auto owner = mOwner.lock();
150 if (owner != nullptr) {
Chong Zhangbc062482020-10-14 16:43:53 -0700151 owner->onError(mClientId, mSessionId, error);
Chong Zhang66469272020-06-04 16:51:55 -0700152 }
153 }
154
155 virtual void onProgressUpdate(const MediaTranscoder* transcoder __unused,
156 int32_t progress) override {
Chong Zhang98b8a372020-07-08 17:27:37 -0700157 auto owner = mOwner.lock();
158 if (owner != nullptr) {
Chong Zhangbc062482020-10-14 16:43:53 -0700159 owner->onProgress(mClientId, mSessionId, progress);
Chong Zhang98b8a372020-07-08 17:27:37 -0700160 }
Chong Zhang66469272020-06-04 16:51:55 -0700161 }
162
Chong Zhang457c6892021-02-01 15:34:20 -0800163 virtual void onHeartBeat(const MediaTranscoder* transcoder __unused) override {
164 auto owner = mOwner.lock();
165 if (owner != nullptr) {
166 owner->onHeartBeat(mClientId, mSessionId);
167 }
168 }
169
Chong Zhang66469272020-06-04 16:51:55 -0700170 virtual void onCodecResourceLost(const MediaTranscoder* transcoder __unused,
Chong Zhange4e088f2020-10-21 19:10:42 -0700171 const std::shared_ptr<ndk::ScopedAParcel>& pausedState
Chong Zhang66469272020-06-04 16:51:55 -0700172 __unused) override {
Chong Zhangbc062482020-10-14 16:43:53 -0700173 ALOGV("%s: session {%lld, %d}", __FUNCTION__, (long long)mClientId, mSessionId);
Chong Zhang66469272020-06-04 16:51:55 -0700174 }
175
176private:
177 std::weak_ptr<TranscoderWrapper> mOwner;
178 ClientIdType mClientId;
Chong Zhangbc062482020-10-14 16:43:53 -0700179 SessionIdType mSessionId;
Chong Zhang66469272020-06-04 16:51:55 -0700180};
181
Chong Zhang457c6892021-02-01 15:34:20 -0800182TranscoderWrapper::TranscoderWrapper(const std::shared_ptr<TranscoderCallbackInterface>& cb,
183 int64_t heartBeatIntervalUs)
184 : mCallback(cb),
185 mHeartBeatIntervalUs(heartBeatIntervalUs),
186 mCurrentClientId(0),
187 mCurrentSessionId(-1),
188 mLooperReady(false) {
189 ALOGV("TranscoderWrapper CTOR: %p", this);
Chong Zhang66469272020-06-04 16:51:55 -0700190}
191
Chong Zhang457c6892021-02-01 15:34:20 -0800192TranscoderWrapper::~TranscoderWrapper() {
193 ALOGV("TranscoderWrapper DTOR: %p", this);
Chong Zhang66469272020-06-04 16:51:55 -0700194}
195
Chong Zhangf9077512020-09-21 21:02:06 -0700196static bool isResourceError(media_status_t err) {
197 return err == AMEDIACODEC_ERROR_RECLAIMED || err == AMEDIACODEC_ERROR_INSUFFICIENT_RESOURCE;
198}
199
Chong Zhangbc062482020-10-14 16:43:53 -0700200void TranscoderWrapper::reportError(ClientIdType clientId, SessionIdType sessionId,
201 media_status_t err) {
Chong Zhangf9077512020-09-21 21:02:06 -0700202 auto callback = mCallback.lock();
203 if (callback != nullptr) {
204 if (isResourceError(err)) {
205 // Add a placeholder pause state to mPausedStateMap. This is required when resuming.
206 // TODO: remove this when transcoder pause/resume logic is ready. New logic will
207 // no longer use the pause states.
Chong Zhangbc062482020-10-14 16:43:53 -0700208 auto it = mPausedStateMap.find(SessionKeyType(clientId, sessionId));
Chong Zhangf9077512020-09-21 21:02:06 -0700209 if (it == mPausedStateMap.end()) {
Chong Zhangbc062482020-10-14 16:43:53 -0700210 mPausedStateMap.emplace(SessionKeyType(clientId, sessionId),
Chong Zhange4e088f2020-10-21 19:10:42 -0700211 new ndk::ScopedAParcel());
Chong Zhangf9077512020-09-21 21:02:06 -0700212 }
213
Chong Zhangeffd8962020-12-02 14:29:09 -0800214 callback->onResourceLost(clientId, sessionId);
Chong Zhangf9077512020-09-21 21:02:06 -0700215 } else {
Chong Zhangbc062482020-10-14 16:43:53 -0700216 callback->onError(clientId, sessionId, toTranscodingError(err));
Chong Zhangf9077512020-09-21 21:02:06 -0700217 }
218 }
219}
220
Chong Zhangbc062482020-10-14 16:43:53 -0700221void TranscoderWrapper::start(ClientIdType clientId, SessionIdType sessionId,
Chong Zhang66469272020-06-04 16:51:55 -0700222 const TranscodingRequestParcel& request,
Chong Zhangb55c5452020-06-26 14:32:12 -0700223 const std::shared_ptr<ITranscodingClientCallback>& clientCb) {
hkuang34377c32020-09-24 22:28:27 -0700224 queueEvent(Event::Start, clientId, sessionId, [=, &request] {
Chong Zhangbc062482020-10-14 16:43:53 -0700225 media_status_t err = handleStart(clientId, sessionId, request, clientCb);
Chong Zhangf9077512020-09-21 21:02:06 -0700226 if (err != AMEDIA_OK) {
Chong Zhang66469272020-06-04 16:51:55 -0700227 cleanup();
Chong Zhangbc062482020-10-14 16:43:53 -0700228 reportError(clientId, sessionId, err);
Chong Zhangb55c5452020-06-26 14:32:12 -0700229 } else {
Chong Zhangf9077512020-09-21 21:02:06 -0700230 auto callback = mCallback.lock();
Chong Zhangb55c5452020-06-26 14:32:12 -0700231 if (callback != nullptr) {
Chong Zhangbc062482020-10-14 16:43:53 -0700232 callback->onStarted(clientId, sessionId);
Chong Zhangb55c5452020-06-26 14:32:12 -0700233 }
Chong Zhang66469272020-06-04 16:51:55 -0700234 }
235 });
236}
237
Chong Zhangbc062482020-10-14 16:43:53 -0700238void TranscoderWrapper::pause(ClientIdType clientId, SessionIdType sessionId) {
239 queueEvent(Event::Pause, clientId, sessionId, [=] {
240 media_status_t err = handlePause(clientId, sessionId);
Chong Zhangb55c5452020-06-26 14:32:12 -0700241
242 cleanup();
243
Chong Zhangf9077512020-09-21 21:02:06 -0700244 if (err != AMEDIA_OK) {
Chong Zhangbc062482020-10-14 16:43:53 -0700245 reportError(clientId, sessionId, err);
Chong Zhangf9077512020-09-21 21:02:06 -0700246 } else {
247 auto callback = mCallback.lock();
248 if (callback != nullptr) {
Chong Zhangbc062482020-10-14 16:43:53 -0700249 callback->onPaused(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::resume(ClientIdType clientId, SessionIdType sessionId,
Chong Zhangb55c5452020-06-26 14:32:12 -0700256 const TranscodingRequestParcel& request,
257 const std::shared_ptr<ITranscodingClientCallback>& clientCb) {
hkuang34377c32020-09-24 22:28:27 -0700258 queueEvent(Event::Resume, clientId, sessionId, [=, &request] {
Chong Zhangbc062482020-10-14 16:43:53 -0700259 media_status_t err = handleResume(clientId, sessionId, request, clientCb);
Chong Zhangf9077512020-09-21 21:02:06 -0700260 if (err != AMEDIA_OK) {
Chong Zhangb55c5452020-06-26 14:32:12 -0700261 cleanup();
Chong Zhangbc062482020-10-14 16:43:53 -0700262 reportError(clientId, sessionId, err);
Chong Zhangb55c5452020-06-26 14:32:12 -0700263 } else {
Chong Zhangf9077512020-09-21 21:02:06 -0700264 auto callback = mCallback.lock();
Chong Zhangb55c5452020-06-26 14:32:12 -0700265 if (callback != nullptr) {
Chong Zhangbc062482020-10-14 16:43:53 -0700266 callback->onResumed(clientId, sessionId);
Chong Zhangb55c5452020-06-26 14:32:12 -0700267 }
268 }
269 });
Chong Zhang66469272020-06-04 16:51:55 -0700270}
271
Chong Zhang457c6892021-02-01 15:34:20 -0800272void TranscoderWrapper::stop(ClientIdType clientId, SessionIdType sessionId, bool abandon) {
Chong Zhangbc062482020-10-14 16:43:53 -0700273 queueEvent(Event::Stop, clientId, sessionId, [=] {
274 if (mTranscoder != nullptr && clientId == mCurrentClientId &&
275 sessionId == mCurrentSessionId) {
276 // Cancelling the currently running session.
Chong Zhangb55c5452020-06-26 14:32:12 -0700277 media_status_t err = mTranscoder->cancel();
278 if (err != AMEDIA_OK) {
Chong Zhangf9077512020-09-21 21:02:06 -0700279 ALOGW("failed to stop transcoder: %d", err);
Chong Zhangb55c5452020-06-26 14:32:12 -0700280 } else {
281 ALOGI("transcoder stopped");
282 }
283 cleanup();
Chong Zhang66469272020-06-04 16:51:55 -0700284 } else {
Chong Zhangbc062482020-10-14 16:43:53 -0700285 // For sessions that's not currently running, release any pausedState for the session.
286 mPausedStateMap.erase(SessionKeyType(clientId, sessionId));
Chong Zhang66469272020-06-04 16:51:55 -0700287 }
Chong Zhangb55c5452020-06-26 14:32:12 -0700288 // No callback needed for stop.
Chong Zhang66469272020-06-04 16:51:55 -0700289 });
Chong Zhang457c6892021-02-01 15:34:20 -0800290
291 if (abandon) {
292 queueEvent(Event::Abandon, 0, 0, nullptr);
293 }
Chong Zhang66469272020-06-04 16:51:55 -0700294}
295
Chong Zhangbc062482020-10-14 16:43:53 -0700296void TranscoderWrapper::onFinish(ClientIdType clientId, SessionIdType sessionId) {
297 queueEvent(Event::Finish, clientId, sessionId, [=] {
298 if (mTranscoder != nullptr && clientId == mCurrentClientId &&
299 sessionId == mCurrentSessionId) {
Chong Zhangb55c5452020-06-26 14:32:12 -0700300 cleanup();
301 }
Chong Zhang66469272020-06-04 16:51:55 -0700302
303 auto callback = mCallback.lock();
304 if (callback != nullptr) {
Chong Zhangbc062482020-10-14 16:43:53 -0700305 callback->onFinish(clientId, sessionId);
Chong Zhang66469272020-06-04 16:51:55 -0700306 }
307 });
308}
309
Chong Zhangbc062482020-10-14 16:43:53 -0700310void TranscoderWrapper::onError(ClientIdType clientId, SessionIdType sessionId,
311 media_status_t error) {
Chong Zhangf9077512020-09-21 21:02:06 -0700312 queueEvent(
Chong Zhangbc062482020-10-14 16:43:53 -0700313 Event::Error, clientId, sessionId,
Chong Zhangf9077512020-09-21 21:02:06 -0700314 [=] {
315 if (mTranscoder != nullptr && clientId == mCurrentClientId &&
Chong Zhangbc062482020-10-14 16:43:53 -0700316 sessionId == mCurrentSessionId) {
Chong Zhangf9077512020-09-21 21:02:06 -0700317 cleanup();
318 }
Chong Zhangbc062482020-10-14 16:43:53 -0700319 reportError(clientId, sessionId, error);
Chong Zhangf9077512020-09-21 21:02:06 -0700320 },
321 error);
Chong Zhang66469272020-06-04 16:51:55 -0700322}
323
Chong Zhangbc062482020-10-14 16:43:53 -0700324void TranscoderWrapper::onProgress(ClientIdType clientId, SessionIdType sessionId,
325 int32_t progress) {
Chong Zhangf9077512020-09-21 21:02:06 -0700326 queueEvent(
Chong Zhangbc062482020-10-14 16:43:53 -0700327 Event::Progress, clientId, sessionId,
Chong Zhangf9077512020-09-21 21:02:06 -0700328 [=] {
329 auto callback = mCallback.lock();
330 if (callback != nullptr) {
Chong Zhangbc062482020-10-14 16:43:53 -0700331 callback->onProgressUpdate(clientId, sessionId, progress);
Chong Zhangf9077512020-09-21 21:02:06 -0700332 }
333 },
334 progress);
Chong Zhang98b8a372020-07-08 17:27:37 -0700335}
336
Chong Zhang457c6892021-02-01 15:34:20 -0800337void TranscoderWrapper::onHeartBeat(ClientIdType clientId, SessionIdType sessionId) {
338 queueEvent(Event::HeartBeat, clientId, sessionId, [=] {
339 auto callback = mCallback.lock();
340 if (callback != nullptr) {
341 callback->onHeartBeat(clientId, sessionId);
342 }
343 });
344}
345
Chong Zhangf9077512020-09-21 21:02:06 -0700346media_status_t TranscoderWrapper::setupTranscoder(
Chong Zhangbc062482020-10-14 16:43:53 -0700347 ClientIdType clientId, SessionIdType sessionId, const TranscodingRequestParcel& request,
Chong Zhangb55c5452020-06-26 14:32:12 -0700348 const std::shared_ptr<ITranscodingClientCallback>& clientCb,
Chong Zhange4e088f2020-10-21 19:10:42 -0700349 const std::shared_ptr<ndk::ScopedAParcel>& pausedState) {
Chong Zhang66469272020-06-04 16:51:55 -0700350 if (clientCb == nullptr) {
351 ALOGE("client callback is null");
Chong Zhangf9077512020-09-21 21:02:06 -0700352 return AMEDIA_ERROR_INVALID_PARAMETER;
Chong Zhang66469272020-06-04 16:51:55 -0700353 }
354
355 if (mTranscoder != nullptr) {
356 ALOGE("transcoder already running");
Chong Zhangf9077512020-09-21 21:02:06 -0700357 return AMEDIA_ERROR_INVALID_OPERATION;
Chong Zhang66469272020-06-04 16:51:55 -0700358 }
359
360 Status status;
361 ::ndk::ScopedFileDescriptor srcFd, dstFd;
hkuang34377c32020-09-24 22:28:27 -0700362 int srcFdInt = request.sourceFd.get();
363 if (srcFdInt < 0) {
364 status = clientCb->openFileDescriptor(request.sourceFilePath, "r", &srcFd);
365 if (!status.isOk() || srcFd.get() < 0) {
366 ALOGE("failed to open source");
367 return AMEDIA_ERROR_IO;
368 }
369 srcFdInt = srcFd.get();
Chong Zhang66469272020-06-04 16:51:55 -0700370 }
371
hkuang34377c32020-09-24 22:28:27 -0700372 int dstFdInt = request.destinationFd.get();
373 if (dstFdInt < 0) {
374 // Open dest file with "rw", as the transcoder could potentially reuse part of it
375 // for resume case. We might want the further differentiate and open with "w" only
376 // for start.
377 status = clientCb->openFileDescriptor(request.destinationFilePath, "rw", &dstFd);
378 if (!status.isOk() || dstFd.get() < 0) {
379 ALOGE("failed to open destination");
380 return AMEDIA_ERROR_IO;
381 }
382 dstFdInt = dstFd.get();
Chong Zhang66469272020-06-04 16:51:55 -0700383 }
384
385 mCurrentClientId = clientId;
Chong Zhangbc062482020-10-14 16:43:53 -0700386 mCurrentSessionId = sessionId;
387 mTranscoderCb = std::make_shared<CallbackImpl>(shared_from_this(), clientId, sessionId);
Chong Zhang457c6892021-02-01 15:34:20 -0800388 mTranscoder = MediaTranscoder::create(mTranscoderCb, mHeartBeatIntervalUs, request.clientPid,
389 request.clientUid, pausedState);
Chong Zhang66469272020-06-04 16:51:55 -0700390 if (mTranscoder == nullptr) {
391 ALOGE("failed to create transcoder");
Chong Zhangf9077512020-09-21 21:02:06 -0700392 return AMEDIA_ERROR_UNKNOWN;
Chong Zhang66469272020-06-04 16:51:55 -0700393 }
394
hkuang34377c32020-09-24 22:28:27 -0700395 media_status_t err = mTranscoder->configureSource(srcFdInt);
Chong Zhang66469272020-06-04 16:51:55 -0700396 if (err != AMEDIA_OK) {
397 ALOGE("failed to configure source: %d", err);
Chong Zhangf9077512020-09-21 21:02:06 -0700398 return err;
Chong Zhang66469272020-06-04 16:51:55 -0700399 }
400
401 std::vector<std::shared_ptr<AMediaFormat>> trackFormats = mTranscoder->getTrackFormats();
402 if (trackFormats.size() == 0) {
403 ALOGE("failed to get track formats!");
Chong Zhangf9077512020-09-21 21:02:06 -0700404 return AMEDIA_ERROR_MALFORMED;
Chong Zhang66469272020-06-04 16:51:55 -0700405 }
406
407 for (int i = 0; i < trackFormats.size(); ++i) {
408 AMediaFormat* format = nullptr;
409 const char* mime = nullptr;
410 AMediaFormat_getString(trackFormats[i].get(), AMEDIAFORMAT_KEY_MIME, &mime);
411
412 if (!strncmp(mime, "video/", 6)) {
413 format = getVideoFormat(mime, request.requestedVideoTrackFormat);
414 }
415
416 err = mTranscoder->configureTrackFormat(i, format);
417 if (format != nullptr) {
418 AMediaFormat_delete(format);
419 }
420 if (err != AMEDIA_OK) {
421 ALOGE("failed to configure track format for track %d: %d", i, err);
Chong Zhangf9077512020-09-21 21:02:06 -0700422 return err;
Chong Zhang66469272020-06-04 16:51:55 -0700423 }
424 }
425
hkuang34377c32020-09-24 22:28:27 -0700426 err = mTranscoder->configureDestination(dstFdInt);
Chong Zhang66469272020-06-04 16:51:55 -0700427 if (err != AMEDIA_OK) {
428 ALOGE("failed to configure dest: %d", err);
Chong Zhangf9077512020-09-21 21:02:06 -0700429 return err;
Chong Zhang66469272020-06-04 16:51:55 -0700430 }
431
Chong Zhangf9077512020-09-21 21:02:06 -0700432 return AMEDIA_OK;
Chong Zhangb55c5452020-06-26 14:32:12 -0700433}
434
Chong Zhangf9077512020-09-21 21:02:06 -0700435media_status_t TranscoderWrapper::handleStart(
Chong Zhangbc062482020-10-14 16:43:53 -0700436 ClientIdType clientId, SessionIdType sessionId, const TranscodingRequestParcel& request,
Chong Zhangb55c5452020-06-26 14:32:12 -0700437 const std::shared_ptr<ITranscodingClientCallback>& clientCb) {
Chong Zhangf9077512020-09-21 21:02:06 -0700438 ALOGI("%s: setting up transcoder for start", __FUNCTION__);
Chong Zhangbc062482020-10-14 16:43:53 -0700439 media_status_t err = setupTranscoder(clientId, sessionId, request, clientCb);
Chong Zhangf9077512020-09-21 21:02:06 -0700440 if (err != AMEDIA_OK) {
Chong Zhangb55c5452020-06-26 14:32:12 -0700441 ALOGI("%s: failed to setup transcoder", __FUNCTION__);
442 return err;
Chong Zhang66469272020-06-04 16:51:55 -0700443 }
444
Chong Zhangf9077512020-09-21 21:02:06 -0700445 err = mTranscoder->start();
446 if (err != AMEDIA_OK) {
Chong Zhangb55c5452020-06-26 14:32:12 -0700447 ALOGE("%s: failed to start transcoder: %d", __FUNCTION__, err);
Chong Zhangf9077512020-09-21 21:02:06 -0700448 return err;
Chong Zhangb55c5452020-06-26 14:32:12 -0700449 }
450
451 ALOGI("%s: transcoder started", __FUNCTION__);
Chong Zhangf9077512020-09-21 21:02:06 -0700452 return AMEDIA_OK;
Chong Zhangb55c5452020-06-26 14:32:12 -0700453}
454
Chong Zhangbc062482020-10-14 16:43:53 -0700455media_status_t TranscoderWrapper::handlePause(ClientIdType clientId, SessionIdType sessionId) {
Chong Zhangb55c5452020-06-26 14:32:12 -0700456 if (mTranscoder == nullptr) {
457 ALOGE("%s: transcoder is not running", __FUNCTION__);
Chong Zhangf9077512020-09-21 21:02:06 -0700458 return AMEDIA_ERROR_INVALID_OPERATION;
Chong Zhangb55c5452020-06-26 14:32:12 -0700459 }
460
Chong Zhangbc062482020-10-14 16:43:53 -0700461 if (clientId != mCurrentClientId || sessionId != mCurrentSessionId) {
462 ALOGW("%s: stopping session {%lld, %d} that's not current session {%lld, %d}", __FUNCTION__,
463 (long long)clientId, sessionId, (long long)mCurrentClientId, mCurrentSessionId);
Chong Zhangb55c5452020-06-26 14:32:12 -0700464 }
465
Chong Zhangf9077512020-09-21 21:02:06 -0700466 ALOGI("%s: pausing transcoder", __FUNCTION__);
467
Chong Zhange4e088f2020-10-21 19:10:42 -0700468 std::shared_ptr<ndk::ScopedAParcel> pauseStates;
Chong Zhangb55c5452020-06-26 14:32:12 -0700469 media_status_t err = mTranscoder->pause(&pauseStates);
470 if (err != AMEDIA_OK) {
471 ALOGE("%s: failed to pause transcoder: %d", __FUNCTION__, err);
Chong Zhangf9077512020-09-21 21:02:06 -0700472 return err;
Chong Zhangb55c5452020-06-26 14:32:12 -0700473 }
Chong Zhangbc062482020-10-14 16:43:53 -0700474 mPausedStateMap[SessionKeyType(clientId, sessionId)] = pauseStates;
Chong Zhangb55c5452020-06-26 14:32:12 -0700475
476 ALOGI("%s: transcoder paused", __FUNCTION__);
Chong Zhangf9077512020-09-21 21:02:06 -0700477 return AMEDIA_OK;
Chong Zhangb55c5452020-06-26 14:32:12 -0700478}
479
Chong Zhangf9077512020-09-21 21:02:06 -0700480media_status_t TranscoderWrapper::handleResume(
Chong Zhangbc062482020-10-14 16:43:53 -0700481 ClientIdType clientId, SessionIdType sessionId, const TranscodingRequestParcel& request,
Chong Zhangb55c5452020-06-26 14:32:12 -0700482 const std::shared_ptr<ITranscodingClientCallback>& clientCb) {
Chong Zhange4e088f2020-10-21 19:10:42 -0700483 std::shared_ptr<ndk::ScopedAParcel> pausedState;
Chong Zhangbc062482020-10-14 16:43:53 -0700484 auto it = mPausedStateMap.find(SessionKeyType(clientId, sessionId));
Chong Zhangb55c5452020-06-26 14:32:12 -0700485 if (it != mPausedStateMap.end()) {
486 pausedState = it->second;
487 mPausedStateMap.erase(it);
488 } else {
489 ALOGE("%s: can't find paused state", __FUNCTION__);
Chong Zhangf9077512020-09-21 21:02:06 -0700490 return AMEDIA_ERROR_INVALID_OPERATION;
Chong Zhangb55c5452020-06-26 14:32:12 -0700491 }
492
Chong Zhangf9077512020-09-21 21:02:06 -0700493 ALOGI("%s: setting up transcoder for resume", __FUNCTION__);
Chong Zhangbc062482020-10-14 16:43:53 -0700494 media_status_t err = setupTranscoder(clientId, sessionId, request, clientCb, pausedState);
Chong Zhangf9077512020-09-21 21:02:06 -0700495 if (err != AMEDIA_OK) {
496 ALOGE("%s: failed to setup transcoder: %d", __FUNCTION__, err);
Chong Zhangb55c5452020-06-26 14:32:12 -0700497 return err;
498 }
499
Chong Zhangf9077512020-09-21 21:02:06 -0700500 err = mTranscoder->resume();
501 if (err != AMEDIA_OK) {
Chong Zhangb55c5452020-06-26 14:32:12 -0700502 ALOGE("%s: failed to resume transcoder: %d", __FUNCTION__, err);
Chong Zhangf9077512020-09-21 21:02:06 -0700503 return err;
Chong Zhangb55c5452020-06-26 14:32:12 -0700504 }
505
506 ALOGI("%s: transcoder resumed", __FUNCTION__);
Chong Zhangf9077512020-09-21 21:02:06 -0700507 return AMEDIA_OK;
Chong Zhang66469272020-06-04 16:51:55 -0700508}
509
510void TranscoderWrapper::cleanup() {
511 mCurrentClientId = 0;
Chong Zhangbc062482020-10-14 16:43:53 -0700512 mCurrentSessionId = -1;
Chong Zhang66469272020-06-04 16:51:55 -0700513 mTranscoderCb = nullptr;
514 mTranscoder = nullptr;
515}
516
Chong Zhangbc062482020-10-14 16:43:53 -0700517void TranscoderWrapper::queueEvent(Event::Type type, ClientIdType clientId, SessionIdType sessionId,
Chong Zhangf9077512020-09-21 21:02:06 -0700518 const std::function<void()> runnable, int32_t arg) {
Chong Zhang66469272020-06-04 16:51:55 -0700519 std::scoped_lock lock{mLock};
520
Chong Zhang457c6892021-02-01 15:34:20 -0800521 if (!mLooperReady) {
522 // A shared_ptr to ourselves is given to the thread's stack, so that the TranscoderWrapper
523 // object doesn't go away until the thread exits. When a watchdog timeout happens, this
524 // allows the session controller to release its reference to the TranscoderWrapper object
525 // without blocking on the thread exits.
526 std::thread([owner = shared_from_this()]() { owner->threadLoop(); }).detach();
527 mLooperReady = true;
528 }
529
Chong Zhangbc062482020-10-14 16:43:53 -0700530 mQueue.push_back({type, clientId, sessionId, runnable, arg});
Chong Zhang66469272020-06-04 16:51:55 -0700531 mCondition.notify_one();
532}
533
534void TranscoderWrapper::threadLoop() {
535 std::unique_lock<std::mutex> lock{mLock};
536 // TranscoderWrapper currently lives in the transcoding service, as long as
537 // MediaTranscodingService itself.
538 while (true) {
539 // Wait for the next event.
540 while (mQueue.empty()) {
541 mCondition.wait(lock);
542 }
543
544 Event event = *mQueue.begin();
545 mQueue.pop_front();
546
Chong Zhangf9077512020-09-21 21:02:06 -0700547 ALOGD("%s: %s", __FUNCTION__, toString(event).c_str());
Chong Zhang66469272020-06-04 16:51:55 -0700548
Chong Zhang457c6892021-02-01 15:34:20 -0800549 if (event.type == Event::Abandon) {
550 break;
551 }
552
Chong Zhangb55c5452020-06-26 14:32:12 -0700553 lock.unlock();
Chong Zhang66469272020-06-04 16:51:55 -0700554 event.runnable();
Chong Zhangb55c5452020-06-26 14:32:12 -0700555 lock.lock();
Chong Zhang66469272020-06-04 16:51:55 -0700556 }
557}
558
559} // namespace android