Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 1 | /* |
| 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 "VideoTrackTranscoder" |
| 19 | |
| 20 | #include <android-base/logging.h> |
Hangyu Kuang | 0d93f0b | 2020-12-11 18:51:16 +0000 | [diff] [blame] | 21 | #include <android-base/properties.h> |
Linus Nilsson | 9359189 | 2020-08-03 18:56:55 -0700 | [diff] [blame] | 22 | #include <media/NdkCommon.h> |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 23 | #include <media/VideoTrackTranscoder.h> |
Linus Nilsson | b09aac2 | 2020-07-29 11:56:53 -0700 | [diff] [blame] | 24 | #include <utils/AndroidThreads.h> |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 25 | |
Linus Nilsson | 7a127b2 | 2020-10-15 16:23:54 -0700 | [diff] [blame] | 26 | using namespace AMediaFormatUtils; |
| 27 | |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 28 | namespace android { |
| 29 | |
| 30 | // Check that the codec sample flags have the expected NDK meaning. |
| 31 | static_assert(SAMPLE_FLAG_CODEC_CONFIG == AMEDIACODEC_BUFFER_FLAG_CODEC_CONFIG, |
| 32 | "Sample flag mismatch: CODEC_CONFIG"); |
| 33 | static_assert(SAMPLE_FLAG_END_OF_STREAM == AMEDIACODEC_BUFFER_FLAG_END_OF_STREAM, |
| 34 | "Sample flag mismatch: END_OF_STREAM"); |
| 35 | static_assert(SAMPLE_FLAG_PARTIAL_FRAME == AMEDIACODEC_BUFFER_FLAG_PARTIAL_FRAME, |
| 36 | "Sample flag mismatch: PARTIAL_FRAME"); |
| 37 | |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 38 | // Color format defined by surface. (See MediaCodecInfo.CodecCapabilities#COLOR_FormatSurface.) |
| 39 | static constexpr int32_t kColorFormatSurface = 0x7f000789; |
| 40 | // Default key frame interval in seconds. |
| 41 | static constexpr float kDefaultKeyFrameIntervalSeconds = 1.0f; |
Linus Nilsson | 7a127b2 | 2020-10-15 16:23:54 -0700 | [diff] [blame] | 42 | // Default codec operating rate. |
Linus Nilsson | 504394a | 2020-12-16 11:10:10 -0800 | [diff] [blame] | 43 | static int32_t kDefaultCodecOperatingRate720P = base::GetIntProperty( |
hkuang | bcb9ec1 | 2020-12-14 22:16:09 -0800 | [diff] [blame] | 44 | "debug.media.transcoding.codec_max_operating_rate_720P", /*default*/ 480); |
| 45 | static int32_t kDefaultCodecOperatingRate1080P = base::GetIntProperty( |
| 46 | "debug.media.transcoding.codec_max_operating_rate_1080P", /*default*/ 240); |
Linus Nilsson | 7a127b2 | 2020-10-15 16:23:54 -0700 | [diff] [blame] | 47 | // Default codec priority. |
| 48 | static constexpr int32_t kDefaultCodecPriority = 1; |
| 49 | // Default bitrate, in case source estimation fails. |
| 50 | static constexpr int32_t kDefaultBitrateMbps = 10 * 1000 * 1000; |
Linus Nilsson | af4a321 | 2020-12-15 08:18:25 -0800 | [diff] [blame] | 51 | // Default frame rate. |
| 52 | static constexpr int32_t kDefaultFrameRate = 30; |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 53 | |
Linus Nilsson | f464154 | 2021-02-04 15:12:26 -0800 | [diff] [blame] | 54 | // Determines whether a track format describes HDR video content or not. The |
| 55 | // logic is based on isHdr() in libstagefright/Utils.cpp. |
| 56 | static bool isHdr(AMediaFormat* format) { |
| 57 | // If VUI signals HDR content, this internal flag is set by the extractor. |
| 58 | int32_t isHdr; |
| 59 | if (AMediaFormat_getInt32(format, "android._is-hdr", &isHdr)) { |
| 60 | return isHdr; |
| 61 | } |
| 62 | |
| 63 | // If container supplied HDR static info without transfer set, assume HDR. |
| 64 | const char* hdrInfo; |
| 65 | int32_t transfer; |
| 66 | if ((AMediaFormat_getString(format, AMEDIAFORMAT_KEY_HDR_STATIC_INFO, &hdrInfo) || |
| 67 | AMediaFormat_getString(format, "hdr10-plus-info", &hdrInfo)) && |
| 68 | !AMediaFormat_getInt32(format, AMEDIAFORMAT_KEY_COLOR_TRANSFER, &transfer)) { |
| 69 | return true; |
| 70 | } |
| 71 | |
| 72 | // Otherwise, check if an HDR transfer function is set. |
| 73 | if (AMediaFormat_getInt32(format, AMEDIAFORMAT_KEY_COLOR_TRANSFER, &transfer)) { |
| 74 | return transfer == COLOR_TRANSFER_ST2084 || transfer == COLOR_TRANSFER_HLG; |
| 75 | } |
| 76 | |
| 77 | return false; |
| 78 | } |
| 79 | |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 80 | template <typename T> |
| 81 | void VideoTrackTranscoder::BlockingQueue<T>::push(T const& value, bool front) { |
| 82 | { |
Linus Nilsson | 93cf913 | 2020-09-24 12:12:48 -0700 | [diff] [blame] | 83 | std::scoped_lock lock(mMutex); |
| 84 | if (mAborted) { |
| 85 | return; |
| 86 | } |
| 87 | |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 88 | if (front) { |
| 89 | mQueue.push_front(value); |
| 90 | } else { |
| 91 | mQueue.push_back(value); |
| 92 | } |
| 93 | } |
| 94 | mCondition.notify_one(); |
| 95 | } |
| 96 | |
| 97 | template <typename T> |
| 98 | T VideoTrackTranscoder::BlockingQueue<T>::pop() { |
Linus Nilsson | 93cf913 | 2020-09-24 12:12:48 -0700 | [diff] [blame] | 99 | std::unique_lock lock(mMutex); |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 100 | while (mQueue.empty()) { |
| 101 | mCondition.wait(lock); |
| 102 | } |
| 103 | T value = mQueue.front(); |
| 104 | mQueue.pop_front(); |
| 105 | return value; |
| 106 | } |
| 107 | |
Linus Nilsson | 93cf913 | 2020-09-24 12:12:48 -0700 | [diff] [blame] | 108 | // Note: Do not call if another thread might waiting in pop. |
| 109 | template <typename T> |
| 110 | void VideoTrackTranscoder::BlockingQueue<T>::abort() { |
| 111 | std::scoped_lock lock(mMutex); |
| 112 | mAborted = true; |
| 113 | mQueue.clear(); |
| 114 | } |
| 115 | |
Linus Nilsson | e4716f2 | 2020-07-10 16:07:57 -0700 | [diff] [blame] | 116 | // The CodecWrapper class is used to let AMediaCodec instances outlive the transcoder object itself |
| 117 | // by giving the codec a weak pointer to the transcoder. Codecs wrapped in this object are kept |
| 118 | // alive by the transcoder and the codec's outstanding buffers. Once the transcoder stops and all |
| 119 | // output buffers have been released by downstream components the codec will also be released. |
| 120 | class VideoTrackTranscoder::CodecWrapper { |
| 121 | public: |
| 122 | CodecWrapper(AMediaCodec* codec, const std::weak_ptr<VideoTrackTranscoder>& transcoder) |
| 123 | : mCodec(codec), mTranscoder(transcoder), mCodecStarted(false) {} |
| 124 | ~CodecWrapper() { |
| 125 | if (mCodecStarted) { |
| 126 | AMediaCodec_stop(mCodec); |
| 127 | } |
| 128 | AMediaCodec_delete(mCodec); |
| 129 | } |
| 130 | |
| 131 | AMediaCodec* getCodec() { return mCodec; } |
| 132 | std::shared_ptr<VideoTrackTranscoder> getTranscoder() const { return mTranscoder.lock(); }; |
| 133 | void setStarted() { mCodecStarted = true; } |
| 134 | |
| 135 | private: |
| 136 | AMediaCodec* mCodec; |
| 137 | std::weak_ptr<VideoTrackTranscoder> mTranscoder; |
| 138 | bool mCodecStarted; |
| 139 | }; |
| 140 | |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 141 | // Dispatch responses to codec callbacks onto the message queue. |
| 142 | struct AsyncCodecCallbackDispatch { |
| 143 | static void onAsyncInputAvailable(AMediaCodec* codec, void* userdata, int32_t index) { |
Linus Nilsson | e4716f2 | 2020-07-10 16:07:57 -0700 | [diff] [blame] | 144 | VideoTrackTranscoder::CodecWrapper* wrapper = |
| 145 | static_cast<VideoTrackTranscoder::CodecWrapper*>(userdata); |
| 146 | if (auto transcoder = wrapper->getTranscoder()) { |
| 147 | if (codec == transcoder->mDecoder) { |
| 148 | transcoder->mCodecMessageQueue.push( |
| 149 | [transcoder, index] { transcoder->enqueueInputSample(index); }); |
| 150 | } |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 151 | } |
| 152 | } |
| 153 | |
| 154 | static void onAsyncOutputAvailable(AMediaCodec* codec, void* userdata, int32_t index, |
| 155 | AMediaCodecBufferInfo* bufferInfoPtr) { |
Linus Nilsson | e4716f2 | 2020-07-10 16:07:57 -0700 | [diff] [blame] | 156 | VideoTrackTranscoder::CodecWrapper* wrapper = |
| 157 | static_cast<VideoTrackTranscoder::CodecWrapper*>(userdata); |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 158 | AMediaCodecBufferInfo bufferInfo = *bufferInfoPtr; |
Linus Nilsson | e4716f2 | 2020-07-10 16:07:57 -0700 | [diff] [blame] | 159 | if (auto transcoder = wrapper->getTranscoder()) { |
| 160 | transcoder->mCodecMessageQueue.push([transcoder, index, codec, bufferInfo] { |
| 161 | if (codec == transcoder->mDecoder) { |
| 162 | transcoder->transferBuffer(index, bufferInfo); |
| 163 | } else if (codec == transcoder->mEncoder->getCodec()) { |
| 164 | transcoder->dequeueOutputSample(index, bufferInfo); |
| 165 | } |
| 166 | }); |
| 167 | } |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | static void onAsyncFormatChanged(AMediaCodec* codec, void* userdata, AMediaFormat* format) { |
Linus Nilsson | e4716f2 | 2020-07-10 16:07:57 -0700 | [diff] [blame] | 171 | VideoTrackTranscoder::CodecWrapper* wrapper = |
| 172 | static_cast<VideoTrackTranscoder::CodecWrapper*>(userdata); |
| 173 | if (auto transcoder = wrapper->getTranscoder()) { |
| 174 | const char* kCodecName = (codec == transcoder->mDecoder ? "Decoder" : "Encoder"); |
| 175 | LOG(DEBUG) << kCodecName << " format changed: " << AMediaFormat_toString(format); |
| 176 | if (codec == transcoder->mEncoder->getCodec()) { |
| 177 | transcoder->mCodecMessageQueue.push( |
| 178 | [transcoder, format] { transcoder->updateTrackFormat(format); }); |
| 179 | } |
Chong Zhang | a2cc86b | 2020-06-17 16:56:49 -0700 | [diff] [blame] | 180 | } |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | static void onAsyncError(AMediaCodec* codec, void* userdata, media_status_t error, |
| 184 | int32_t actionCode, const char* detail) { |
| 185 | LOG(ERROR) << "Error from codec " << codec << ", userdata " << userdata << ", error " |
| 186 | << error << ", action " << actionCode << ", detail " << detail; |
Linus Nilsson | e4716f2 | 2020-07-10 16:07:57 -0700 | [diff] [blame] | 187 | VideoTrackTranscoder::CodecWrapper* wrapper = |
| 188 | static_cast<VideoTrackTranscoder::CodecWrapper*>(userdata); |
| 189 | if (auto transcoder = wrapper->getTranscoder()) { |
| 190 | transcoder->mCodecMessageQueue.push( |
Linus Nilsson | fdb3e33 | 2020-09-18 17:11:41 -0700 | [diff] [blame] | 191 | [transcoder, error] { transcoder->mStatus = error; }, true); |
Linus Nilsson | e4716f2 | 2020-07-10 16:07:57 -0700 | [diff] [blame] | 192 | } |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 193 | } |
| 194 | }; |
| 195 | |
Linus Nilsson | e4716f2 | 2020-07-10 16:07:57 -0700 | [diff] [blame] | 196 | // static |
| 197 | std::shared_ptr<VideoTrackTranscoder> VideoTrackTranscoder::create( |
Chong Zhang | bbb4eac | 2020-11-18 11:12:06 -0800 | [diff] [blame] | 198 | const std::weak_ptr<MediaTrackTranscoderCallback>& transcoderCallback, pid_t pid, |
| 199 | uid_t uid) { |
| 200 | return std::shared_ptr<VideoTrackTranscoder>( |
| 201 | new VideoTrackTranscoder(transcoderCallback, pid, uid)); |
Linus Nilsson | e4716f2 | 2020-07-10 16:07:57 -0700 | [diff] [blame] | 202 | } |
| 203 | |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 204 | VideoTrackTranscoder::~VideoTrackTranscoder() { |
| 205 | if (mDecoder != nullptr) { |
| 206 | AMediaCodec_delete(mDecoder); |
| 207 | } |
| 208 | |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 209 | if (mSurface != nullptr) { |
| 210 | ANativeWindow_release(mSurface); |
| 211 | } |
| 212 | } |
| 213 | |
hkuang | bcb9ec1 | 2020-12-14 22:16:09 -0800 | [diff] [blame] | 214 | // Search the default operating rate based on resolution. |
| 215 | static int32_t getDefaultOperatingRate(AMediaFormat* encoderFormat) { |
| 216 | int32_t width, height; |
| 217 | if (AMediaFormat_getInt32(encoderFormat, AMEDIAFORMAT_KEY_WIDTH, &width) && (width > 0) && |
| 218 | AMediaFormat_getInt32(encoderFormat, AMEDIAFORMAT_KEY_HEIGHT, &height) && (height > 0)) { |
| 219 | if ((width == 1280 && height == 720) || (width == 720 && height == 1280)) { |
Linus Nilsson | 504394a | 2020-12-16 11:10:10 -0800 | [diff] [blame] | 220 | return kDefaultCodecOperatingRate720P; |
hkuang | bcb9ec1 | 2020-12-14 22:16:09 -0800 | [diff] [blame] | 221 | } else if ((width == 1920 && height == 1080) || (width == 1080 && height == 1920)) { |
| 222 | return kDefaultCodecOperatingRate1080P; |
hkuang | bcb9ec1 | 2020-12-14 22:16:09 -0800 | [diff] [blame] | 223 | } else { |
| 224 | LOG(WARNING) << "Could not find default operating rate: " << width << " " << height; |
Linus Nilsson | 504394a | 2020-12-16 11:10:10 -0800 | [diff] [blame] | 225 | // Don't set operating rate if the correct dimensions are not found. |
hkuang | bcb9ec1 | 2020-12-14 22:16:09 -0800 | [diff] [blame] | 226 | } |
| 227 | } else { |
| 228 | LOG(ERROR) << "Failed to get default operating rate due to missing resolution"; |
| 229 | } |
| 230 | return -1; |
| 231 | } |
| 232 | |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 233 | // Creates and configures the codecs. |
| 234 | media_status_t VideoTrackTranscoder::configureDestinationFormat( |
| 235 | const std::shared_ptr<AMediaFormat>& destinationFormat) { |
| 236 | media_status_t status = AMEDIA_OK; |
| 237 | |
| 238 | if (destinationFormat == nullptr) { |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 239 | LOG(ERROR) << "Destination format is null, use passthrough transcoder"; |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 240 | return AMEDIA_ERROR_INVALID_PARAMETER; |
| 241 | } |
| 242 | |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 243 | AMediaFormat* encoderFormat = AMediaFormat_new(); |
| 244 | if (!encoderFormat || AMediaFormat_copy(encoderFormat, destinationFormat.get()) != AMEDIA_OK) { |
| 245 | LOG(ERROR) << "Unable to copy destination format"; |
| 246 | return AMEDIA_ERROR_INVALID_PARAMETER; |
| 247 | } |
| 248 | |
Linus Nilsson | 800793f | 2020-07-31 16:16:38 -0700 | [diff] [blame] | 249 | int32_t bitrate; |
| 250 | if (!AMediaFormat_getInt32(encoderFormat, AMEDIAFORMAT_KEY_BIT_RATE, &bitrate)) { |
| 251 | status = mMediaSampleReader->getEstimatedBitrateForTrack(mTrackIndex, &bitrate); |
| 252 | if (status != AMEDIA_OK) { |
| 253 | LOG(ERROR) << "Unable to estimate bitrate. Using default " << kDefaultBitrateMbps; |
| 254 | bitrate = kDefaultBitrateMbps; |
| 255 | } |
| 256 | |
| 257 | LOG(INFO) << "Configuring bitrate " << bitrate; |
| 258 | AMediaFormat_setInt32(encoderFormat, AMEDIAFORMAT_KEY_BIT_RATE, bitrate); |
| 259 | } |
| 260 | |
Linus Nilsson | 7a127b2 | 2020-10-15 16:23:54 -0700 | [diff] [blame] | 261 | SetDefaultFormatValueFloat(AMEDIAFORMAT_KEY_I_FRAME_INTERVAL, encoderFormat, |
| 262 | kDefaultKeyFrameIntervalSeconds); |
hkuang | bcb9ec1 | 2020-12-14 22:16:09 -0800 | [diff] [blame] | 263 | |
| 264 | int32_t operatingRate = getDefaultOperatingRate(encoderFormat); |
| 265 | |
| 266 | if (operatingRate != -1) { |
Linus Nilsson | 4aef0de | 2021-01-29 17:58:55 -0800 | [diff] [blame] | 267 | float tmpf; |
| 268 | int32_t tmpi; |
| 269 | if (!AMediaFormat_getFloat(encoderFormat, AMEDIAFORMAT_KEY_OPERATING_RATE, &tmpf) && |
| 270 | !AMediaFormat_getInt32(encoderFormat, AMEDIAFORMAT_KEY_OPERATING_RATE, &tmpi)) { |
| 271 | AMediaFormat_setInt32(encoderFormat, AMEDIAFORMAT_KEY_OPERATING_RATE, operatingRate); |
| 272 | } |
hkuang | bcb9ec1 | 2020-12-14 22:16:09 -0800 | [diff] [blame] | 273 | } |
| 274 | |
Linus Nilsson | 7a127b2 | 2020-10-15 16:23:54 -0700 | [diff] [blame] | 275 | SetDefaultFormatValueInt32(AMEDIAFORMAT_KEY_PRIORITY, encoderFormat, kDefaultCodecPriority); |
Linus Nilsson | af4a321 | 2020-12-15 08:18:25 -0800 | [diff] [blame] | 276 | SetDefaultFormatValueInt32(AMEDIAFORMAT_KEY_FRAME_RATE, encoderFormat, kDefaultFrameRate); |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 277 | AMediaFormat_setInt32(encoderFormat, AMEDIAFORMAT_KEY_COLOR_FORMAT, kColorFormatSurface); |
| 278 | |
Chong Zhang | d6e4aec | 2020-06-22 14:13:07 -0700 | [diff] [blame] | 279 | // Always encode without rotation. The rotation degree will be transferred directly to |
| 280 | // MediaSampleWriter track format, and MediaSampleWriter will call AMediaMuxer_setOrientationHint. |
| 281 | AMediaFormat_setInt32(encoderFormat, AMEDIAFORMAT_KEY_ROTATION, 0); |
| 282 | |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 283 | mDestinationFormat = std::shared_ptr<AMediaFormat>(encoderFormat, &AMediaFormat_delete); |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 284 | |
| 285 | // Create and configure the encoder. |
| 286 | const char* destinationMime = nullptr; |
| 287 | bool ok = AMediaFormat_getString(mDestinationFormat.get(), AMEDIAFORMAT_KEY_MIME, |
| 288 | &destinationMime); |
| 289 | if (!ok) { |
| 290 | LOG(ERROR) << "Destination MIME type is required for transcoding."; |
| 291 | return AMEDIA_ERROR_INVALID_PARAMETER; |
| 292 | } |
| 293 | |
Linus Nilsson | 4aef0de | 2021-01-29 17:58:55 -0800 | [diff] [blame] | 294 | // TODO: replace __ANDROID_API_FUTURE__with 31 when it's official (b/178144708) |
| 295 | #define __TRANSCODING_MIN_API__ __ANDROID_API_FUTURE__ |
Jiyong Park | 26ddfc5 | 2021-01-22 16:26:40 +0900 | [diff] [blame] | 296 | |
| 297 | AMediaCodec* encoder; |
| 298 | if (__builtin_available(android __TRANSCODING_MIN_API__, *)) { |
| 299 | encoder = AMediaCodec_createEncoderByTypeForClient(destinationMime, mPid, mUid); |
| 300 | } else { |
| 301 | encoder = AMediaCodec_createEncoderByType(destinationMime); |
| 302 | } |
Linus Nilsson | c6221db | 2020-03-18 14:46:22 -0700 | [diff] [blame] | 303 | if (encoder == nullptr) { |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 304 | LOG(ERROR) << "Unable to create encoder for type " << destinationMime; |
| 305 | return AMEDIA_ERROR_UNSUPPORTED; |
| 306 | } |
Linus Nilsson | e4716f2 | 2020-07-10 16:07:57 -0700 | [diff] [blame] | 307 | mEncoder = std::make_shared<CodecWrapper>(encoder, shared_from_this()); |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 308 | |
Linus Nilsson | af4a321 | 2020-12-15 08:18:25 -0800 | [diff] [blame] | 309 | LOG(DEBUG) << "Configuring encoder with: " << AMediaFormat_toString(mDestinationFormat.get()); |
Linus Nilsson | e4716f2 | 2020-07-10 16:07:57 -0700 | [diff] [blame] | 310 | status = AMediaCodec_configure(mEncoder->getCodec(), mDestinationFormat.get(), |
| 311 | NULL /* surface */, NULL /* crypto */, |
| 312 | AMEDIACODEC_CONFIGURE_FLAG_ENCODE); |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 313 | if (status != AMEDIA_OK) { |
| 314 | LOG(ERROR) << "Unable to configure video encoder: " << status; |
| 315 | return status; |
| 316 | } |
| 317 | |
Linus Nilsson | e4716f2 | 2020-07-10 16:07:57 -0700 | [diff] [blame] | 318 | status = AMediaCodec_createInputSurface(mEncoder->getCodec(), &mSurface); |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 319 | if (status != AMEDIA_OK) { |
| 320 | LOG(ERROR) << "Unable to create an encoder input surface: %d" << status; |
| 321 | return status; |
| 322 | } |
| 323 | |
| 324 | // Create and configure the decoder. |
| 325 | const char* sourceMime = nullptr; |
| 326 | ok = AMediaFormat_getString(mSourceFormat.get(), AMEDIAFORMAT_KEY_MIME, &sourceMime); |
| 327 | if (!ok) { |
| 328 | LOG(ERROR) << "Source MIME type is required for transcoding."; |
| 329 | return AMEDIA_ERROR_INVALID_PARAMETER; |
| 330 | } |
| 331 | |
Jiyong Park | 26ddfc5 | 2021-01-22 16:26:40 +0900 | [diff] [blame] | 332 | if (__builtin_available(android __TRANSCODING_MIN_API__, *)) { |
| 333 | mDecoder = AMediaCodec_createDecoderByTypeForClient(sourceMime, mPid, mUid); |
| 334 | } else { |
| 335 | mDecoder = AMediaCodec_createDecoderByType(sourceMime); |
| 336 | } |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 337 | if (mDecoder == nullptr) { |
| 338 | LOG(ERROR) << "Unable to create decoder for type " << sourceMime; |
| 339 | return AMEDIA_ERROR_UNSUPPORTED; |
| 340 | } |
| 341 | |
Linus Nilsson | 9359189 | 2020-08-03 18:56:55 -0700 | [diff] [blame] | 342 | auto decoderFormat = std::shared_ptr<AMediaFormat>(AMediaFormat_new(), &AMediaFormat_delete); |
| 343 | if (!decoderFormat || |
| 344 | AMediaFormat_copy(decoderFormat.get(), mSourceFormat.get()) != AMEDIA_OK) { |
| 345 | LOG(ERROR) << "Unable to copy source format"; |
| 346 | return AMEDIA_ERROR_INVALID_PARAMETER; |
| 347 | } |
| 348 | |
Linus Nilsson | f464154 | 2021-02-04 15:12:26 -0800 | [diff] [blame] | 349 | // Request decoder to convert HDR content to SDR. |
| 350 | const bool sourceIsHdr = isHdr(mSourceFormat.get()); |
| 351 | if (sourceIsHdr) { |
| 352 | AMediaFormat_setInt32(decoderFormat.get(), |
| 353 | TBD_AMEDIACODEC_PARAMETER_KEY_COLOR_TRANSFER_REQUEST, |
| 354 | COLOR_TRANSFER_SDR_VIDEO); |
| 355 | } |
| 356 | |
Linus Nilsson | 9359189 | 2020-08-03 18:56:55 -0700 | [diff] [blame] | 357 | // Prevent decoder from overwriting frames that the encoder has not yet consumed. |
| 358 | AMediaFormat_setInt32(decoderFormat.get(), TBD_AMEDIACODEC_PARAMETER_KEY_ALLOW_FRAME_DROP, 0); |
| 359 | |
Linus Nilsson | 16d772b | 2020-09-29 19:21:11 -0700 | [diff] [blame] | 360 | // Copy over configurations that apply to both encoder and decoder. |
Linus Nilsson | 7a127b2 | 2020-10-15 16:23:54 -0700 | [diff] [blame] | 361 | static const EntryCopier kEncoderEntriesToCopy[] = { |
Linus Nilsson | 16d772b | 2020-09-29 19:21:11 -0700 | [diff] [blame] | 362 | ENTRY_COPIER2(AMEDIAFORMAT_KEY_OPERATING_RATE, Float, Int32), |
| 363 | ENTRY_COPIER(AMEDIAFORMAT_KEY_PRIORITY, Int32), |
| 364 | }; |
| 365 | const size_t entryCount = sizeof(kEncoderEntriesToCopy) / sizeof(kEncoderEntriesToCopy[0]); |
Linus Nilsson | 7a127b2 | 2020-10-15 16:23:54 -0700 | [diff] [blame] | 366 | CopyFormatEntries(mDestinationFormat.get(), decoderFormat.get(), kEncoderEntriesToCopy, |
| 367 | entryCount); |
Linus Nilsson | 16d772b | 2020-09-29 19:21:11 -0700 | [diff] [blame] | 368 | |
Linus Nilsson | af4a321 | 2020-12-15 08:18:25 -0800 | [diff] [blame] | 369 | LOG(DEBUG) << "Configuring decoder with: " << AMediaFormat_toString(decoderFormat.get()); |
Linus Nilsson | 9359189 | 2020-08-03 18:56:55 -0700 | [diff] [blame] | 370 | status = AMediaCodec_configure(mDecoder, decoderFormat.get(), mSurface, NULL /* crypto */, |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 371 | 0 /* flags */); |
| 372 | if (status != AMEDIA_OK) { |
| 373 | LOG(ERROR) << "Unable to configure video decoder: " << status; |
| 374 | return status; |
| 375 | } |
| 376 | |
Linus Nilsson | f464154 | 2021-02-04 15:12:26 -0800 | [diff] [blame] | 377 | if (sourceIsHdr) { |
| 378 | bool supported = false; |
| 379 | AMediaFormat* inputFormat = AMediaCodec_getInputFormat(mDecoder); |
| 380 | |
| 381 | if (inputFormat != nullptr) { |
| 382 | int32_t transferFunc; |
| 383 | supported = AMediaFormat_getInt32(inputFormat, |
| 384 | TBD_AMEDIACODEC_PARAMETER_KEY_COLOR_TRANSFER_REQUEST, |
| 385 | &transferFunc) && |
| 386 | transferFunc == COLOR_TRANSFER_SDR_VIDEO; |
| 387 | AMediaFormat_delete(inputFormat); |
| 388 | } |
| 389 | |
| 390 | if (!supported) { |
| 391 | LOG(ERROR) << "HDR to SDR conversion unsupported by the codec"; |
| 392 | return AMEDIA_ERROR_UNSUPPORTED; |
| 393 | } |
| 394 | } |
| 395 | |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 396 | // Configure codecs to run in async mode. |
| 397 | AMediaCodecOnAsyncNotifyCallback asyncCodecCallbacks = { |
| 398 | .onAsyncInputAvailable = AsyncCodecCallbackDispatch::onAsyncInputAvailable, |
| 399 | .onAsyncOutputAvailable = AsyncCodecCallbackDispatch::onAsyncOutputAvailable, |
| 400 | .onAsyncFormatChanged = AsyncCodecCallbackDispatch::onAsyncFormatChanged, |
| 401 | .onAsyncError = AsyncCodecCallbackDispatch::onAsyncError}; |
| 402 | |
Linus Nilsson | e4716f2 | 2020-07-10 16:07:57 -0700 | [diff] [blame] | 403 | // Note: The decoder does not need its own wrapper because its lifetime is tied to the |
| 404 | // transcoder. But the same callbacks are reused for decoder and encoder so we pass the encoder |
| 405 | // wrapper as userdata here but never read the codec from it in the callback. |
| 406 | status = AMediaCodec_setAsyncNotifyCallback(mDecoder, asyncCodecCallbacks, mEncoder.get()); |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 407 | if (status != AMEDIA_OK) { |
| 408 | LOG(ERROR) << "Unable to set decoder to async mode: " << status; |
| 409 | return status; |
| 410 | } |
| 411 | |
Linus Nilsson | e4716f2 | 2020-07-10 16:07:57 -0700 | [diff] [blame] | 412 | status = AMediaCodec_setAsyncNotifyCallback(mEncoder->getCodec(), asyncCodecCallbacks, |
| 413 | mEncoder.get()); |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 414 | if (status != AMEDIA_OK) { |
| 415 | LOG(ERROR) << "Unable to set encoder to async mode: " << status; |
| 416 | return status; |
| 417 | } |
| 418 | |
| 419 | return AMEDIA_OK; |
| 420 | } |
| 421 | |
| 422 | void VideoTrackTranscoder::enqueueInputSample(int32_t bufferIndex) { |
| 423 | media_status_t status = AMEDIA_OK; |
| 424 | |
Linus Nilsson | c6221db | 2020-03-18 14:46:22 -0700 | [diff] [blame] | 425 | if (mEosFromSource) { |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 426 | return; |
| 427 | } |
| 428 | |
| 429 | status = mMediaSampleReader->getSampleInfoForTrack(mTrackIndex, &mSampleInfo); |
| 430 | if (status != AMEDIA_OK && status != AMEDIA_ERROR_END_OF_STREAM) { |
| 431 | LOG(ERROR) << "Error getting next sample info: " << status; |
| 432 | mStatus = status; |
| 433 | return; |
| 434 | } |
| 435 | const bool endOfStream = (status == AMEDIA_ERROR_END_OF_STREAM); |
| 436 | |
| 437 | if (!endOfStream) { |
| 438 | size_t bufferSize = 0; |
| 439 | uint8_t* sourceBuffer = AMediaCodec_getInputBuffer(mDecoder, bufferIndex, &bufferSize); |
| 440 | if (sourceBuffer == nullptr) { |
| 441 | LOG(ERROR) << "Decoder returned a NULL input buffer."; |
| 442 | mStatus = AMEDIA_ERROR_UNKNOWN; |
| 443 | return; |
| 444 | } else if (bufferSize < mSampleInfo.size) { |
| 445 | LOG(ERROR) << "Decoder returned an input buffer that is smaller than the sample."; |
| 446 | mStatus = AMEDIA_ERROR_UNKNOWN; |
| 447 | return; |
| 448 | } |
| 449 | |
| 450 | status = mMediaSampleReader->readSampleDataForTrack(mTrackIndex, sourceBuffer, |
| 451 | mSampleInfo.size); |
| 452 | if (status != AMEDIA_OK) { |
| 453 | LOG(ERROR) << "Unable to read next sample data. Aborting transcode."; |
| 454 | mStatus = status; |
| 455 | return; |
| 456 | } |
Linus Nilsson | 4735241 | 2020-12-16 12:21:26 -0800 | [diff] [blame] | 457 | |
| 458 | if (mSampleInfo.size) { |
| 459 | ++mInputFrameCount; |
| 460 | } |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 461 | } else { |
| 462 | LOG(DEBUG) << "EOS from source."; |
Linus Nilsson | c6221db | 2020-03-18 14:46:22 -0700 | [diff] [blame] | 463 | mEosFromSource = true; |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | status = AMediaCodec_queueInputBuffer(mDecoder, bufferIndex, 0, mSampleInfo.size, |
| 467 | mSampleInfo.presentationTimeUs, mSampleInfo.flags); |
| 468 | if (status != AMEDIA_OK) { |
| 469 | LOG(ERROR) << "Unable to queue input buffer for decode: " << status; |
| 470 | mStatus = status; |
| 471 | return; |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | void VideoTrackTranscoder::transferBuffer(int32_t bufferIndex, AMediaCodecBufferInfo bufferInfo) { |
| 476 | if (bufferIndex >= 0) { |
| 477 | bool needsRender = bufferInfo.size > 0; |
| 478 | AMediaCodec_releaseOutputBuffer(mDecoder, bufferIndex, needsRender); |
| 479 | } |
| 480 | |
| 481 | if (bufferInfo.flags & AMEDIACODEC_BUFFER_FLAG_END_OF_STREAM) { |
| 482 | LOG(DEBUG) << "EOS from decoder."; |
Linus Nilsson | e4716f2 | 2020-07-10 16:07:57 -0700 | [diff] [blame] | 483 | media_status_t status = AMediaCodec_signalEndOfInputStream(mEncoder->getCodec()); |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 484 | if (status != AMEDIA_OK) { |
| 485 | LOG(ERROR) << "SignalEOS on encoder returned error: " << status; |
| 486 | mStatus = status; |
| 487 | } |
| 488 | } |
| 489 | } |
| 490 | |
| 491 | void VideoTrackTranscoder::dequeueOutputSample(int32_t bufferIndex, |
| 492 | AMediaCodecBufferInfo bufferInfo) { |
| 493 | if (bufferIndex >= 0) { |
| 494 | size_t sampleSize = 0; |
Linus Nilsson | e4716f2 | 2020-07-10 16:07:57 -0700 | [diff] [blame] | 495 | uint8_t* buffer = |
| 496 | AMediaCodec_getOutputBuffer(mEncoder->getCodec(), bufferIndex, &sampleSize); |
Linus Nilsson | c6221db | 2020-03-18 14:46:22 -0700 | [diff] [blame] | 497 | |
Linus Nilsson | e4716f2 | 2020-07-10 16:07:57 -0700 | [diff] [blame] | 498 | MediaSample::OnSampleReleasedCallback bufferReleaseCallback = |
| 499 | [encoder = mEncoder](MediaSample* sample) { |
| 500 | AMediaCodec_releaseOutputBuffer(encoder->getCodec(), sample->bufferId, |
| 501 | false /* render */); |
| 502 | }; |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 503 | |
| 504 | std::shared_ptr<MediaSample> sample = MediaSample::createWithReleaseCallback( |
Linus Nilsson | c6221db | 2020-03-18 14:46:22 -0700 | [diff] [blame] | 505 | buffer, bufferInfo.offset, bufferIndex, bufferReleaseCallback); |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 506 | sample->info.size = bufferInfo.size; |
| 507 | sample->info.flags = bufferInfo.flags; |
| 508 | sample->info.presentationTimeUs = bufferInfo.presentationTimeUs; |
| 509 | |
Linus Nilsson | 4735241 | 2020-12-16 12:21:26 -0800 | [diff] [blame] | 510 | if (bufferInfo.size > 0 && (bufferInfo.flags & SAMPLE_FLAG_CODEC_CONFIG) == 0) { |
| 511 | ++mOutputFrameCount; |
| 512 | } |
Linus Nilsson | c31d249 | 2020-09-23 12:30:00 -0700 | [diff] [blame] | 513 | onOutputSampleAvailable(sample); |
Linus Nilsson | fdb3e33 | 2020-09-18 17:11:41 -0700 | [diff] [blame] | 514 | |
| 515 | mLastSampleWasSync = sample->info.flags & SAMPLE_FLAG_SYNC_SAMPLE; |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 516 | } else if (bufferIndex == AMEDIACODEC_INFO_OUTPUT_FORMAT_CHANGED) { |
Linus Nilsson | e4716f2 | 2020-07-10 16:07:57 -0700 | [diff] [blame] | 517 | AMediaFormat* newFormat = AMediaCodec_getOutputFormat(mEncoder->getCodec()); |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 518 | LOG(DEBUG) << "Encoder output format changed: " << AMediaFormat_toString(newFormat); |
| 519 | } |
| 520 | |
| 521 | if (bufferInfo.flags & AMEDIACODEC_BUFFER_FLAG_END_OF_STREAM) { |
| 522 | LOG(DEBUG) << "EOS from encoder."; |
Linus Nilsson | c6221db | 2020-03-18 14:46:22 -0700 | [diff] [blame] | 523 | mEosFromEncoder = true; |
Linus Nilsson | 4735241 | 2020-12-16 12:21:26 -0800 | [diff] [blame] | 524 | |
| 525 | if (mInputFrameCount != mOutputFrameCount) { |
| 526 | LOG(WARNING) << "Input / Output frame count mismatch: " << mInputFrameCount << " vs " |
| 527 | << mOutputFrameCount; |
| 528 | if (mInputFrameCount > 0 && mOutputFrameCount == 0) { |
| 529 | LOG(ERROR) << "Encoder did not produce any output frames."; |
| 530 | mStatus = AMEDIA_ERROR_UNKNOWN; |
| 531 | } |
| 532 | } |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 533 | } |
| 534 | } |
| 535 | |
Chong Zhang | a2cc86b | 2020-06-17 16:56:49 -0700 | [diff] [blame] | 536 | void VideoTrackTranscoder::updateTrackFormat(AMediaFormat* outputFormat) { |
| 537 | if (mActualOutputFormat != nullptr) { |
| 538 | LOG(WARNING) << "Ignoring duplicate format change."; |
| 539 | return; |
| 540 | } |
| 541 | |
| 542 | AMediaFormat* formatCopy = AMediaFormat_new(); |
| 543 | if (!formatCopy || AMediaFormat_copy(formatCopy, outputFormat) != AMEDIA_OK) { |
| 544 | LOG(ERROR) << "Unable to copy outputFormat"; |
| 545 | AMediaFormat_delete(formatCopy); |
| 546 | mStatus = AMEDIA_ERROR_INVALID_PARAMETER; |
| 547 | return; |
| 548 | } |
| 549 | |
| 550 | // Generate the actual track format for muxer based on the encoder output format, |
| 551 | // since many vital information comes in the encoder format (eg. CSD). |
| 552 | // Transfer necessary fields from the user-configured track format (derived from |
| 553 | // source track format and user transcoding request) where needed. |
| 554 | |
| 555 | // Transfer SAR settings: |
| 556 | // If mDestinationFormat has SAR set, it means the original source has SAR specified |
| 557 | // at container level. This is supposed to override any SAR settings in the bitstream, |
| 558 | // thus should always be transferred to the container of the transcoded file. |
| 559 | int32_t sarWidth, sarHeight; |
Chong Zhang | d6e4aec | 2020-06-22 14:13:07 -0700 | [diff] [blame] | 560 | if (AMediaFormat_getInt32(mSourceFormat.get(), AMEDIAFORMAT_KEY_SAR_WIDTH, &sarWidth) && |
Chong Zhang | a2cc86b | 2020-06-17 16:56:49 -0700 | [diff] [blame] | 561 | (sarWidth > 0) && |
Chong Zhang | d6e4aec | 2020-06-22 14:13:07 -0700 | [diff] [blame] | 562 | AMediaFormat_getInt32(mSourceFormat.get(), AMEDIAFORMAT_KEY_SAR_HEIGHT, &sarHeight) && |
Chong Zhang | a2cc86b | 2020-06-17 16:56:49 -0700 | [diff] [blame] | 563 | (sarHeight > 0)) { |
| 564 | AMediaFormat_setInt32(formatCopy, AMEDIAFORMAT_KEY_SAR_WIDTH, sarWidth); |
| 565 | AMediaFormat_setInt32(formatCopy, AMEDIAFORMAT_KEY_SAR_HEIGHT, sarHeight); |
| 566 | } |
| 567 | // Transfer DAR settings. |
| 568 | int32_t displayWidth, displayHeight; |
Chong Zhang | d6e4aec | 2020-06-22 14:13:07 -0700 | [diff] [blame] | 569 | if (AMediaFormat_getInt32(mSourceFormat.get(), AMEDIAFORMAT_KEY_DISPLAY_WIDTH, &displayWidth) && |
Chong Zhang | a2cc86b | 2020-06-17 16:56:49 -0700 | [diff] [blame] | 570 | (displayWidth > 0) && |
Chong Zhang | d6e4aec | 2020-06-22 14:13:07 -0700 | [diff] [blame] | 571 | AMediaFormat_getInt32(mSourceFormat.get(), AMEDIAFORMAT_KEY_DISPLAY_HEIGHT, |
Chong Zhang | a2cc86b | 2020-06-17 16:56:49 -0700 | [diff] [blame] | 572 | &displayHeight) && |
| 573 | (displayHeight > 0)) { |
| 574 | AMediaFormat_setInt32(formatCopy, AMEDIAFORMAT_KEY_DISPLAY_WIDTH, displayWidth); |
| 575 | AMediaFormat_setInt32(formatCopy, AMEDIAFORMAT_KEY_DISPLAY_HEIGHT, displayHeight); |
| 576 | } |
| 577 | |
Chong Zhang | d6e4aec | 2020-06-22 14:13:07 -0700 | [diff] [blame] | 578 | // Transfer rotation settings. |
| 579 | // Note that muxer itself doesn't take rotation from the track format. It requires |
| 580 | // AMediaMuxer_setOrientationHint to set the rotation. Here we pass the rotation to |
| 581 | // MediaSampleWriter using the track format. MediaSampleWriter will then call |
| 582 | // AMediaMuxer_setOrientationHint as needed. |
| 583 | int32_t rotation; |
| 584 | if (AMediaFormat_getInt32(mSourceFormat.get(), AMEDIAFORMAT_KEY_ROTATION, &rotation) && |
| 585 | (rotation != 0)) { |
| 586 | AMediaFormat_setInt32(formatCopy, AMEDIAFORMAT_KEY_ROTATION, rotation); |
| 587 | } |
| 588 | |
Linus Nilsson | 42a971b | 2020-07-01 16:41:11 -0700 | [diff] [blame] | 589 | // Transfer track duration. |
| 590 | // Preserve the source track duration by sending it to MediaSampleWriter. |
| 591 | int64_t durationUs; |
| 592 | if (AMediaFormat_getInt64(mSourceFormat.get(), AMEDIAFORMAT_KEY_DURATION, &durationUs) && |
| 593 | durationUs > 0) { |
| 594 | AMediaFormat_setInt64(formatCopy, AMEDIAFORMAT_KEY_DURATION, durationUs); |
| 595 | } |
| 596 | |
Chong Zhang | a2cc86b | 2020-06-17 16:56:49 -0700 | [diff] [blame] | 597 | // TODO: transfer other fields as required. |
| 598 | |
| 599 | mActualOutputFormat = std::shared_ptr<AMediaFormat>(formatCopy, &AMediaFormat_delete); |
| 600 | |
| 601 | notifyTrackFormatAvailable(); |
| 602 | } |
| 603 | |
Linus Nilsson | fdb3e33 | 2020-09-18 17:11:41 -0700 | [diff] [blame] | 604 | media_status_t VideoTrackTranscoder::runTranscodeLoop(bool* stopped) { |
Linus Nilsson | b09aac2 | 2020-07-29 11:56:53 -0700 | [diff] [blame] | 605 | androidSetThreadPriority(0 /* tid (0 = current) */, ANDROID_PRIORITY_VIDEO); |
| 606 | |
Chong Zhang | b55c545 | 2020-06-26 14:32:12 -0700 | [diff] [blame] | 607 | // Push start decoder and encoder as two messages, so that these are subject to the |
Chong Zhang | bc06248 | 2020-10-14 16:43:53 -0700 | [diff] [blame] | 608 | // stop request as well. If the session is cancelled (or paused) immediately after start, |
Chong Zhang | b55c545 | 2020-06-26 14:32:12 -0700 | [diff] [blame] | 609 | // we don't need to waste time start then stop the codecs. |
| 610 | mCodecMessageQueue.push([this] { |
| 611 | media_status_t status = AMediaCodec_start(mDecoder); |
| 612 | if (status != AMEDIA_OK) { |
| 613 | LOG(ERROR) << "Unable to start video decoder: " << status; |
| 614 | mStatus = status; |
| 615 | } |
| 616 | }); |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 617 | |
Chong Zhang | b55c545 | 2020-06-26 14:32:12 -0700 | [diff] [blame] | 618 | mCodecMessageQueue.push([this] { |
Linus Nilsson | e4716f2 | 2020-07-10 16:07:57 -0700 | [diff] [blame] | 619 | media_status_t status = AMediaCodec_start(mEncoder->getCodec()); |
Chong Zhang | b55c545 | 2020-06-26 14:32:12 -0700 | [diff] [blame] | 620 | if (status != AMEDIA_OK) { |
| 621 | LOG(ERROR) << "Unable to start video encoder: " << status; |
| 622 | mStatus = status; |
| 623 | } |
Linus Nilsson | e4716f2 | 2020-07-10 16:07:57 -0700 | [diff] [blame] | 624 | mEncoder->setStarted(); |
Chong Zhang | b55c545 | 2020-06-26 14:32:12 -0700 | [diff] [blame] | 625 | }); |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 626 | |
| 627 | // Process codec events until EOS is reached, transcoding is stopped or an error occurs. |
Linus Nilsson | fdb3e33 | 2020-09-18 17:11:41 -0700 | [diff] [blame] | 628 | while (mStopRequest != STOP_NOW && !mEosFromEncoder && mStatus == AMEDIA_OK) { |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 629 | std::function<void()> message = mCodecMessageQueue.pop(); |
| 630 | message(); |
Linus Nilsson | fdb3e33 | 2020-09-18 17:11:41 -0700 | [diff] [blame] | 631 | |
| 632 | if (mStopRequest == STOP_ON_SYNC && mLastSampleWasSync) { |
| 633 | break; |
| 634 | } |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 635 | } |
| 636 | |
Linus Nilsson | 93cf913 | 2020-09-24 12:12:48 -0700 | [diff] [blame] | 637 | mCodecMessageQueue.abort(); |
| 638 | AMediaCodec_stop(mDecoder); |
| 639 | |
Linus Nilsson | fdb3e33 | 2020-09-18 17:11:41 -0700 | [diff] [blame] | 640 | // Signal if transcoding was stopped before it finished. |
| 641 | if (mStopRequest != NONE && !mEosFromEncoder && mStatus == AMEDIA_OK) { |
| 642 | *stopped = true; |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 643 | } |
| 644 | |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 645 | return mStatus; |
| 646 | } |
| 647 | |
| 648 | void VideoTrackTranscoder::abortTranscodeLoop() { |
Linus Nilsson | fdb3e33 | 2020-09-18 17:11:41 -0700 | [diff] [blame] | 649 | if (mStopRequest == STOP_NOW) { |
| 650 | // Wake up transcoder thread. |
| 651 | mCodecMessageQueue.push([] {}, true /* front */); |
| 652 | } |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 653 | } |
| 654 | |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 655 | std::shared_ptr<AMediaFormat> VideoTrackTranscoder::getOutputFormat() const { |
Chong Zhang | a2cc86b | 2020-06-17 16:56:49 -0700 | [diff] [blame] | 656 | return mActualOutputFormat; |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 657 | } |
| 658 | |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 659 | } // namespace android |