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> |
| 21 | #include <media/VideoTrackTranscoder.h> |
| 22 | |
| 23 | namespace android { |
| 24 | |
| 25 | // Check that the codec sample flags have the expected NDK meaning. |
| 26 | static_assert(SAMPLE_FLAG_CODEC_CONFIG == AMEDIACODEC_BUFFER_FLAG_CODEC_CONFIG, |
| 27 | "Sample flag mismatch: CODEC_CONFIG"); |
| 28 | static_assert(SAMPLE_FLAG_END_OF_STREAM == AMEDIACODEC_BUFFER_FLAG_END_OF_STREAM, |
| 29 | "Sample flag mismatch: END_OF_STREAM"); |
| 30 | static_assert(SAMPLE_FLAG_PARTIAL_FRAME == AMEDIACODEC_BUFFER_FLAG_PARTIAL_FRAME, |
| 31 | "Sample flag mismatch: PARTIAL_FRAME"); |
| 32 | |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 33 | // Color format defined by surface. (See MediaCodecInfo.CodecCapabilities#COLOR_FormatSurface.) |
| 34 | static constexpr int32_t kColorFormatSurface = 0x7f000789; |
| 35 | // Default key frame interval in seconds. |
| 36 | static constexpr float kDefaultKeyFrameIntervalSeconds = 1.0f; |
| 37 | |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 38 | template <typename T> |
| 39 | void VideoTrackTranscoder::BlockingQueue<T>::push(T const& value, bool front) { |
| 40 | { |
| 41 | std::unique_lock<std::mutex> lock(mMutex); |
| 42 | if (front) { |
| 43 | mQueue.push_front(value); |
| 44 | } else { |
| 45 | mQueue.push_back(value); |
| 46 | } |
| 47 | } |
| 48 | mCondition.notify_one(); |
| 49 | } |
| 50 | |
| 51 | template <typename T> |
| 52 | T VideoTrackTranscoder::BlockingQueue<T>::pop() { |
| 53 | std::unique_lock<std::mutex> lock(mMutex); |
| 54 | while (mQueue.empty()) { |
| 55 | mCondition.wait(lock); |
| 56 | } |
| 57 | T value = mQueue.front(); |
| 58 | mQueue.pop_front(); |
| 59 | return value; |
| 60 | } |
| 61 | |
Linus Nilsson | e4716f2 | 2020-07-10 16:07:57 -0700 | [diff] [blame] | 62 | // The CodecWrapper class is used to let AMediaCodec instances outlive the transcoder object itself |
| 63 | // by giving the codec a weak pointer to the transcoder. Codecs wrapped in this object are kept |
| 64 | // alive by the transcoder and the codec's outstanding buffers. Once the transcoder stops and all |
| 65 | // output buffers have been released by downstream components the codec will also be released. |
| 66 | class VideoTrackTranscoder::CodecWrapper { |
| 67 | public: |
| 68 | CodecWrapper(AMediaCodec* codec, const std::weak_ptr<VideoTrackTranscoder>& transcoder) |
| 69 | : mCodec(codec), mTranscoder(transcoder), mCodecStarted(false) {} |
| 70 | ~CodecWrapper() { |
| 71 | if (mCodecStarted) { |
| 72 | AMediaCodec_stop(mCodec); |
| 73 | } |
| 74 | AMediaCodec_delete(mCodec); |
| 75 | } |
| 76 | |
| 77 | AMediaCodec* getCodec() { return mCodec; } |
| 78 | std::shared_ptr<VideoTrackTranscoder> getTranscoder() const { return mTranscoder.lock(); }; |
| 79 | void setStarted() { mCodecStarted = true; } |
| 80 | |
| 81 | private: |
| 82 | AMediaCodec* mCodec; |
| 83 | std::weak_ptr<VideoTrackTranscoder> mTranscoder; |
| 84 | bool mCodecStarted; |
| 85 | }; |
| 86 | |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 87 | // Dispatch responses to codec callbacks onto the message queue. |
| 88 | struct AsyncCodecCallbackDispatch { |
| 89 | static void onAsyncInputAvailable(AMediaCodec* codec, void* userdata, int32_t index) { |
Linus Nilsson | e4716f2 | 2020-07-10 16:07:57 -0700 | [diff] [blame] | 90 | VideoTrackTranscoder::CodecWrapper* wrapper = |
| 91 | static_cast<VideoTrackTranscoder::CodecWrapper*>(userdata); |
| 92 | if (auto transcoder = wrapper->getTranscoder()) { |
| 93 | if (codec == transcoder->mDecoder) { |
| 94 | transcoder->mCodecMessageQueue.push( |
| 95 | [transcoder, index] { transcoder->enqueueInputSample(index); }); |
| 96 | } |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 97 | } |
| 98 | } |
| 99 | |
| 100 | static void onAsyncOutputAvailable(AMediaCodec* codec, void* userdata, int32_t index, |
| 101 | AMediaCodecBufferInfo* bufferInfoPtr) { |
Linus Nilsson | e4716f2 | 2020-07-10 16:07:57 -0700 | [diff] [blame] | 102 | VideoTrackTranscoder::CodecWrapper* wrapper = |
| 103 | static_cast<VideoTrackTranscoder::CodecWrapper*>(userdata); |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 104 | AMediaCodecBufferInfo bufferInfo = *bufferInfoPtr; |
Linus Nilsson | e4716f2 | 2020-07-10 16:07:57 -0700 | [diff] [blame] | 105 | if (auto transcoder = wrapper->getTranscoder()) { |
| 106 | transcoder->mCodecMessageQueue.push([transcoder, index, codec, bufferInfo] { |
| 107 | if (codec == transcoder->mDecoder) { |
| 108 | transcoder->transferBuffer(index, bufferInfo); |
| 109 | } else if (codec == transcoder->mEncoder->getCodec()) { |
| 110 | transcoder->dequeueOutputSample(index, bufferInfo); |
| 111 | } |
| 112 | }); |
| 113 | } |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | static void onAsyncFormatChanged(AMediaCodec* codec, void* userdata, AMediaFormat* format) { |
Linus Nilsson | e4716f2 | 2020-07-10 16:07:57 -0700 | [diff] [blame] | 117 | VideoTrackTranscoder::CodecWrapper* wrapper = |
| 118 | static_cast<VideoTrackTranscoder::CodecWrapper*>(userdata); |
| 119 | if (auto transcoder = wrapper->getTranscoder()) { |
| 120 | const char* kCodecName = (codec == transcoder->mDecoder ? "Decoder" : "Encoder"); |
| 121 | LOG(DEBUG) << kCodecName << " format changed: " << AMediaFormat_toString(format); |
| 122 | if (codec == transcoder->mEncoder->getCodec()) { |
| 123 | transcoder->mCodecMessageQueue.push( |
| 124 | [transcoder, format] { transcoder->updateTrackFormat(format); }); |
| 125 | } |
Chong Zhang | a2cc86b | 2020-06-17 16:56:49 -0700 | [diff] [blame] | 126 | } |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | static void onAsyncError(AMediaCodec* codec, void* userdata, media_status_t error, |
| 130 | int32_t actionCode, const char* detail) { |
| 131 | LOG(ERROR) << "Error from codec " << codec << ", userdata " << userdata << ", error " |
| 132 | << error << ", action " << actionCode << ", detail " << detail; |
Linus Nilsson | e4716f2 | 2020-07-10 16:07:57 -0700 | [diff] [blame] | 133 | VideoTrackTranscoder::CodecWrapper* wrapper = |
| 134 | static_cast<VideoTrackTranscoder::CodecWrapper*>(userdata); |
| 135 | if (auto transcoder = wrapper->getTranscoder()) { |
| 136 | transcoder->mCodecMessageQueue.push( |
| 137 | [transcoder, error] { |
| 138 | transcoder->mStatus = error; |
| 139 | transcoder->mStopRequested = true; |
| 140 | }, |
| 141 | true); |
| 142 | } |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 143 | } |
| 144 | }; |
| 145 | |
Linus Nilsson | e4716f2 | 2020-07-10 16:07:57 -0700 | [diff] [blame] | 146 | // static |
| 147 | std::shared_ptr<VideoTrackTranscoder> VideoTrackTranscoder::create( |
| 148 | const std::weak_ptr<MediaTrackTranscoderCallback>& transcoderCallback) { |
| 149 | return std::shared_ptr<VideoTrackTranscoder>(new VideoTrackTranscoder(transcoderCallback)); |
| 150 | } |
| 151 | |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 152 | VideoTrackTranscoder::~VideoTrackTranscoder() { |
| 153 | if (mDecoder != nullptr) { |
| 154 | AMediaCodec_delete(mDecoder); |
| 155 | } |
| 156 | |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 157 | if (mSurface != nullptr) { |
| 158 | ANativeWindow_release(mSurface); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | // Creates and configures the codecs. |
| 163 | media_status_t VideoTrackTranscoder::configureDestinationFormat( |
| 164 | const std::shared_ptr<AMediaFormat>& destinationFormat) { |
| 165 | media_status_t status = AMEDIA_OK; |
| 166 | |
| 167 | if (destinationFormat == nullptr) { |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 168 | LOG(ERROR) << "Destination format is null, use passthrough transcoder"; |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 169 | return AMEDIA_ERROR_INVALID_PARAMETER; |
| 170 | } |
| 171 | |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 172 | AMediaFormat* encoderFormat = AMediaFormat_new(); |
| 173 | if (!encoderFormat || AMediaFormat_copy(encoderFormat, destinationFormat.get()) != AMEDIA_OK) { |
| 174 | LOG(ERROR) << "Unable to copy destination format"; |
| 175 | return AMEDIA_ERROR_INVALID_PARAMETER; |
| 176 | } |
| 177 | |
| 178 | float tmp; |
| 179 | if (!AMediaFormat_getFloat(encoderFormat, AMEDIAFORMAT_KEY_I_FRAME_INTERVAL, &tmp)) { |
| 180 | AMediaFormat_setFloat(encoderFormat, AMEDIAFORMAT_KEY_I_FRAME_INTERVAL, |
| 181 | kDefaultKeyFrameIntervalSeconds); |
| 182 | } |
| 183 | AMediaFormat_setInt32(encoderFormat, AMEDIAFORMAT_KEY_COLOR_FORMAT, kColorFormatSurface); |
| 184 | |
Chong Zhang | d6e4aec | 2020-06-22 14:13:07 -0700 | [diff] [blame] | 185 | // Always encode without rotation. The rotation degree will be transferred directly to |
| 186 | // MediaSampleWriter track format, and MediaSampleWriter will call AMediaMuxer_setOrientationHint. |
| 187 | AMediaFormat_setInt32(encoderFormat, AMEDIAFORMAT_KEY_ROTATION, 0); |
| 188 | |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 189 | mDestinationFormat = std::shared_ptr<AMediaFormat>(encoderFormat, &AMediaFormat_delete); |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 190 | |
| 191 | // Create and configure the encoder. |
| 192 | const char* destinationMime = nullptr; |
| 193 | bool ok = AMediaFormat_getString(mDestinationFormat.get(), AMEDIAFORMAT_KEY_MIME, |
| 194 | &destinationMime); |
| 195 | if (!ok) { |
| 196 | LOG(ERROR) << "Destination MIME type is required for transcoding."; |
| 197 | return AMEDIA_ERROR_INVALID_PARAMETER; |
| 198 | } |
| 199 | |
Linus Nilsson | c6221db | 2020-03-18 14:46:22 -0700 | [diff] [blame] | 200 | AMediaCodec* encoder = AMediaCodec_createEncoderByType(destinationMime); |
| 201 | if (encoder == nullptr) { |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 202 | LOG(ERROR) << "Unable to create encoder for type " << destinationMime; |
| 203 | return AMEDIA_ERROR_UNSUPPORTED; |
| 204 | } |
Linus Nilsson | e4716f2 | 2020-07-10 16:07:57 -0700 | [diff] [blame] | 205 | mEncoder = std::make_shared<CodecWrapper>(encoder, shared_from_this()); |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 206 | |
Linus Nilsson | e4716f2 | 2020-07-10 16:07:57 -0700 | [diff] [blame] | 207 | status = AMediaCodec_configure(mEncoder->getCodec(), mDestinationFormat.get(), |
| 208 | NULL /* surface */, NULL /* crypto */, |
| 209 | AMEDIACODEC_CONFIGURE_FLAG_ENCODE); |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 210 | if (status != AMEDIA_OK) { |
| 211 | LOG(ERROR) << "Unable to configure video encoder: " << status; |
| 212 | return status; |
| 213 | } |
| 214 | |
Linus Nilsson | e4716f2 | 2020-07-10 16:07:57 -0700 | [diff] [blame] | 215 | status = AMediaCodec_createInputSurface(mEncoder->getCodec(), &mSurface); |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 216 | if (status != AMEDIA_OK) { |
| 217 | LOG(ERROR) << "Unable to create an encoder input surface: %d" << status; |
| 218 | return status; |
| 219 | } |
| 220 | |
| 221 | // Create and configure the decoder. |
| 222 | const char* sourceMime = nullptr; |
| 223 | ok = AMediaFormat_getString(mSourceFormat.get(), AMEDIAFORMAT_KEY_MIME, &sourceMime); |
| 224 | if (!ok) { |
| 225 | LOG(ERROR) << "Source MIME type is required for transcoding."; |
| 226 | return AMEDIA_ERROR_INVALID_PARAMETER; |
| 227 | } |
| 228 | |
| 229 | mDecoder = AMediaCodec_createDecoderByType(sourceMime); |
| 230 | if (mDecoder == nullptr) { |
| 231 | LOG(ERROR) << "Unable to create decoder for type " << sourceMime; |
| 232 | return AMEDIA_ERROR_UNSUPPORTED; |
| 233 | } |
| 234 | |
| 235 | status = AMediaCodec_configure(mDecoder, mSourceFormat.get(), mSurface, NULL /* crypto */, |
| 236 | 0 /* flags */); |
| 237 | if (status != AMEDIA_OK) { |
| 238 | LOG(ERROR) << "Unable to configure video decoder: " << status; |
| 239 | return status; |
| 240 | } |
| 241 | |
| 242 | // Configure codecs to run in async mode. |
| 243 | AMediaCodecOnAsyncNotifyCallback asyncCodecCallbacks = { |
| 244 | .onAsyncInputAvailable = AsyncCodecCallbackDispatch::onAsyncInputAvailable, |
| 245 | .onAsyncOutputAvailable = AsyncCodecCallbackDispatch::onAsyncOutputAvailable, |
| 246 | .onAsyncFormatChanged = AsyncCodecCallbackDispatch::onAsyncFormatChanged, |
| 247 | .onAsyncError = AsyncCodecCallbackDispatch::onAsyncError}; |
| 248 | |
Linus Nilsson | e4716f2 | 2020-07-10 16:07:57 -0700 | [diff] [blame] | 249 | // Note: The decoder does not need its own wrapper because its lifetime is tied to the |
| 250 | // transcoder. But the same callbacks are reused for decoder and encoder so we pass the encoder |
| 251 | // wrapper as userdata here but never read the codec from it in the callback. |
| 252 | status = AMediaCodec_setAsyncNotifyCallback(mDecoder, asyncCodecCallbacks, mEncoder.get()); |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 253 | if (status != AMEDIA_OK) { |
| 254 | LOG(ERROR) << "Unable to set decoder to async mode: " << status; |
| 255 | return status; |
| 256 | } |
| 257 | |
Linus Nilsson | e4716f2 | 2020-07-10 16:07:57 -0700 | [diff] [blame] | 258 | status = AMediaCodec_setAsyncNotifyCallback(mEncoder->getCodec(), asyncCodecCallbacks, |
| 259 | mEncoder.get()); |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 260 | if (status != AMEDIA_OK) { |
| 261 | LOG(ERROR) << "Unable to set encoder to async mode: " << status; |
| 262 | return status; |
| 263 | } |
| 264 | |
| 265 | return AMEDIA_OK; |
| 266 | } |
| 267 | |
| 268 | void VideoTrackTranscoder::enqueueInputSample(int32_t bufferIndex) { |
| 269 | media_status_t status = AMEDIA_OK; |
| 270 | |
Linus Nilsson | c6221db | 2020-03-18 14:46:22 -0700 | [diff] [blame] | 271 | if (mEosFromSource) { |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 272 | return; |
| 273 | } |
| 274 | |
| 275 | status = mMediaSampleReader->getSampleInfoForTrack(mTrackIndex, &mSampleInfo); |
| 276 | if (status != AMEDIA_OK && status != AMEDIA_ERROR_END_OF_STREAM) { |
| 277 | LOG(ERROR) << "Error getting next sample info: " << status; |
| 278 | mStatus = status; |
| 279 | return; |
| 280 | } |
| 281 | const bool endOfStream = (status == AMEDIA_ERROR_END_OF_STREAM); |
| 282 | |
| 283 | if (!endOfStream) { |
| 284 | size_t bufferSize = 0; |
| 285 | uint8_t* sourceBuffer = AMediaCodec_getInputBuffer(mDecoder, bufferIndex, &bufferSize); |
| 286 | if (sourceBuffer == nullptr) { |
| 287 | LOG(ERROR) << "Decoder returned a NULL input buffer."; |
| 288 | mStatus = AMEDIA_ERROR_UNKNOWN; |
| 289 | return; |
| 290 | } else if (bufferSize < mSampleInfo.size) { |
| 291 | LOG(ERROR) << "Decoder returned an input buffer that is smaller than the sample."; |
| 292 | mStatus = AMEDIA_ERROR_UNKNOWN; |
| 293 | return; |
| 294 | } |
| 295 | |
| 296 | status = mMediaSampleReader->readSampleDataForTrack(mTrackIndex, sourceBuffer, |
| 297 | mSampleInfo.size); |
| 298 | if (status != AMEDIA_OK) { |
| 299 | LOG(ERROR) << "Unable to read next sample data. Aborting transcode."; |
| 300 | mStatus = status; |
| 301 | return; |
| 302 | } |
| 303 | |
| 304 | mMediaSampleReader->advanceTrack(mTrackIndex); |
| 305 | } else { |
| 306 | LOG(DEBUG) << "EOS from source."; |
Linus Nilsson | c6221db | 2020-03-18 14:46:22 -0700 | [diff] [blame] | 307 | mEosFromSource = true; |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | status = AMediaCodec_queueInputBuffer(mDecoder, bufferIndex, 0, mSampleInfo.size, |
| 311 | mSampleInfo.presentationTimeUs, mSampleInfo.flags); |
| 312 | if (status != AMEDIA_OK) { |
| 313 | LOG(ERROR) << "Unable to queue input buffer for decode: " << status; |
| 314 | mStatus = status; |
| 315 | return; |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | void VideoTrackTranscoder::transferBuffer(int32_t bufferIndex, AMediaCodecBufferInfo bufferInfo) { |
| 320 | if (bufferIndex >= 0) { |
| 321 | bool needsRender = bufferInfo.size > 0; |
| 322 | AMediaCodec_releaseOutputBuffer(mDecoder, bufferIndex, needsRender); |
| 323 | } |
| 324 | |
| 325 | if (bufferInfo.flags & AMEDIACODEC_BUFFER_FLAG_END_OF_STREAM) { |
| 326 | LOG(DEBUG) << "EOS from decoder."; |
Linus Nilsson | e4716f2 | 2020-07-10 16:07:57 -0700 | [diff] [blame] | 327 | media_status_t status = AMediaCodec_signalEndOfInputStream(mEncoder->getCodec()); |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 328 | if (status != AMEDIA_OK) { |
| 329 | LOG(ERROR) << "SignalEOS on encoder returned error: " << status; |
| 330 | mStatus = status; |
| 331 | } |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | void VideoTrackTranscoder::dequeueOutputSample(int32_t bufferIndex, |
| 336 | AMediaCodecBufferInfo bufferInfo) { |
| 337 | if (bufferIndex >= 0) { |
| 338 | size_t sampleSize = 0; |
Linus Nilsson | e4716f2 | 2020-07-10 16:07:57 -0700 | [diff] [blame] | 339 | uint8_t* buffer = |
| 340 | AMediaCodec_getOutputBuffer(mEncoder->getCodec(), bufferIndex, &sampleSize); |
Linus Nilsson | c6221db | 2020-03-18 14:46:22 -0700 | [diff] [blame] | 341 | |
Linus Nilsson | e4716f2 | 2020-07-10 16:07:57 -0700 | [diff] [blame] | 342 | MediaSample::OnSampleReleasedCallback bufferReleaseCallback = |
| 343 | [encoder = mEncoder](MediaSample* sample) { |
| 344 | AMediaCodec_releaseOutputBuffer(encoder->getCodec(), sample->bufferId, |
| 345 | false /* render */); |
| 346 | }; |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 347 | |
| 348 | std::shared_ptr<MediaSample> sample = MediaSample::createWithReleaseCallback( |
Linus Nilsson | c6221db | 2020-03-18 14:46:22 -0700 | [diff] [blame] | 349 | buffer, bufferInfo.offset, bufferIndex, bufferReleaseCallback); |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 350 | sample->info.size = bufferInfo.size; |
| 351 | sample->info.flags = bufferInfo.flags; |
| 352 | sample->info.presentationTimeUs = bufferInfo.presentationTimeUs; |
| 353 | |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 354 | const bool aborted = mOutputQueue->enqueue(sample); |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 355 | if (aborted) { |
| 356 | LOG(ERROR) << "Output sample queue was aborted. Stopping transcode."; |
| 357 | mStatus = AMEDIA_ERROR_IO; // TODO: Define custom error codes? |
| 358 | return; |
| 359 | } |
| 360 | } else if (bufferIndex == AMEDIACODEC_INFO_OUTPUT_FORMAT_CHANGED) { |
Linus Nilsson | e4716f2 | 2020-07-10 16:07:57 -0700 | [diff] [blame] | 361 | AMediaFormat* newFormat = AMediaCodec_getOutputFormat(mEncoder->getCodec()); |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 362 | LOG(DEBUG) << "Encoder output format changed: " << AMediaFormat_toString(newFormat); |
| 363 | } |
| 364 | |
| 365 | if (bufferInfo.flags & AMEDIACODEC_BUFFER_FLAG_END_OF_STREAM) { |
| 366 | LOG(DEBUG) << "EOS from encoder."; |
Linus Nilsson | c6221db | 2020-03-18 14:46:22 -0700 | [diff] [blame] | 367 | mEosFromEncoder = true; |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 368 | } |
| 369 | } |
| 370 | |
Chong Zhang | a2cc86b | 2020-06-17 16:56:49 -0700 | [diff] [blame] | 371 | void VideoTrackTranscoder::updateTrackFormat(AMediaFormat* outputFormat) { |
| 372 | if (mActualOutputFormat != nullptr) { |
| 373 | LOG(WARNING) << "Ignoring duplicate format change."; |
| 374 | return; |
| 375 | } |
| 376 | |
| 377 | AMediaFormat* formatCopy = AMediaFormat_new(); |
| 378 | if (!formatCopy || AMediaFormat_copy(formatCopy, outputFormat) != AMEDIA_OK) { |
| 379 | LOG(ERROR) << "Unable to copy outputFormat"; |
| 380 | AMediaFormat_delete(formatCopy); |
| 381 | mStatus = AMEDIA_ERROR_INVALID_PARAMETER; |
| 382 | return; |
| 383 | } |
| 384 | |
| 385 | // Generate the actual track format for muxer based on the encoder output format, |
| 386 | // since many vital information comes in the encoder format (eg. CSD). |
| 387 | // Transfer necessary fields from the user-configured track format (derived from |
| 388 | // source track format and user transcoding request) where needed. |
| 389 | |
| 390 | // Transfer SAR settings: |
| 391 | // If mDestinationFormat has SAR set, it means the original source has SAR specified |
| 392 | // at container level. This is supposed to override any SAR settings in the bitstream, |
| 393 | // thus should always be transferred to the container of the transcoded file. |
| 394 | int32_t sarWidth, sarHeight; |
Chong Zhang | d6e4aec | 2020-06-22 14:13:07 -0700 | [diff] [blame] | 395 | if (AMediaFormat_getInt32(mSourceFormat.get(), AMEDIAFORMAT_KEY_SAR_WIDTH, &sarWidth) && |
Chong Zhang | a2cc86b | 2020-06-17 16:56:49 -0700 | [diff] [blame] | 396 | (sarWidth > 0) && |
Chong Zhang | d6e4aec | 2020-06-22 14:13:07 -0700 | [diff] [blame] | 397 | AMediaFormat_getInt32(mSourceFormat.get(), AMEDIAFORMAT_KEY_SAR_HEIGHT, &sarHeight) && |
Chong Zhang | a2cc86b | 2020-06-17 16:56:49 -0700 | [diff] [blame] | 398 | (sarHeight > 0)) { |
| 399 | AMediaFormat_setInt32(formatCopy, AMEDIAFORMAT_KEY_SAR_WIDTH, sarWidth); |
| 400 | AMediaFormat_setInt32(formatCopy, AMEDIAFORMAT_KEY_SAR_HEIGHT, sarHeight); |
| 401 | } |
| 402 | // Transfer DAR settings. |
| 403 | int32_t displayWidth, displayHeight; |
Chong Zhang | d6e4aec | 2020-06-22 14:13:07 -0700 | [diff] [blame] | 404 | if (AMediaFormat_getInt32(mSourceFormat.get(), AMEDIAFORMAT_KEY_DISPLAY_WIDTH, &displayWidth) && |
Chong Zhang | a2cc86b | 2020-06-17 16:56:49 -0700 | [diff] [blame] | 405 | (displayWidth > 0) && |
Chong Zhang | d6e4aec | 2020-06-22 14:13:07 -0700 | [diff] [blame] | 406 | AMediaFormat_getInt32(mSourceFormat.get(), AMEDIAFORMAT_KEY_DISPLAY_HEIGHT, |
Chong Zhang | a2cc86b | 2020-06-17 16:56:49 -0700 | [diff] [blame] | 407 | &displayHeight) && |
| 408 | (displayHeight > 0)) { |
| 409 | AMediaFormat_setInt32(formatCopy, AMEDIAFORMAT_KEY_DISPLAY_WIDTH, displayWidth); |
| 410 | AMediaFormat_setInt32(formatCopy, AMEDIAFORMAT_KEY_DISPLAY_HEIGHT, displayHeight); |
| 411 | } |
| 412 | |
Chong Zhang | d6e4aec | 2020-06-22 14:13:07 -0700 | [diff] [blame] | 413 | // Transfer rotation settings. |
| 414 | // Note that muxer itself doesn't take rotation from the track format. It requires |
| 415 | // AMediaMuxer_setOrientationHint to set the rotation. Here we pass the rotation to |
| 416 | // MediaSampleWriter using the track format. MediaSampleWriter will then call |
| 417 | // AMediaMuxer_setOrientationHint as needed. |
| 418 | int32_t rotation; |
| 419 | if (AMediaFormat_getInt32(mSourceFormat.get(), AMEDIAFORMAT_KEY_ROTATION, &rotation) && |
| 420 | (rotation != 0)) { |
| 421 | AMediaFormat_setInt32(formatCopy, AMEDIAFORMAT_KEY_ROTATION, rotation); |
| 422 | } |
| 423 | |
Linus Nilsson | 42a971b | 2020-07-01 16:41:11 -0700 | [diff] [blame] | 424 | // Transfer track duration. |
| 425 | // Preserve the source track duration by sending it to MediaSampleWriter. |
| 426 | int64_t durationUs; |
| 427 | if (AMediaFormat_getInt64(mSourceFormat.get(), AMEDIAFORMAT_KEY_DURATION, &durationUs) && |
| 428 | durationUs > 0) { |
| 429 | AMediaFormat_setInt64(formatCopy, AMEDIAFORMAT_KEY_DURATION, durationUs); |
| 430 | } |
| 431 | |
Chong Zhang | a2cc86b | 2020-06-17 16:56:49 -0700 | [diff] [blame] | 432 | // TODO: transfer other fields as required. |
| 433 | |
| 434 | mActualOutputFormat = std::shared_ptr<AMediaFormat>(formatCopy, &AMediaFormat_delete); |
| 435 | |
| 436 | notifyTrackFormatAvailable(); |
| 437 | } |
| 438 | |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 439 | media_status_t VideoTrackTranscoder::runTranscodeLoop() { |
Chong Zhang | b55c545 | 2020-06-26 14:32:12 -0700 | [diff] [blame] | 440 | // Push start decoder and encoder as two messages, so that these are subject to the |
| 441 | // stop request as well. If the job is cancelled (or paused) immediately after start, |
| 442 | // we don't need to waste time start then stop the codecs. |
| 443 | mCodecMessageQueue.push([this] { |
| 444 | media_status_t status = AMediaCodec_start(mDecoder); |
| 445 | if (status != AMEDIA_OK) { |
| 446 | LOG(ERROR) << "Unable to start video decoder: " << status; |
| 447 | mStatus = status; |
| 448 | } |
| 449 | }); |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 450 | |
Chong Zhang | b55c545 | 2020-06-26 14:32:12 -0700 | [diff] [blame] | 451 | mCodecMessageQueue.push([this] { |
Linus Nilsson | e4716f2 | 2020-07-10 16:07:57 -0700 | [diff] [blame] | 452 | media_status_t status = AMediaCodec_start(mEncoder->getCodec()); |
Chong Zhang | b55c545 | 2020-06-26 14:32:12 -0700 | [diff] [blame] | 453 | if (status != AMEDIA_OK) { |
| 454 | LOG(ERROR) << "Unable to start video encoder: " << status; |
| 455 | mStatus = status; |
| 456 | } |
Linus Nilsson | e4716f2 | 2020-07-10 16:07:57 -0700 | [diff] [blame] | 457 | mEncoder->setStarted(); |
Chong Zhang | b55c545 | 2020-06-26 14:32:12 -0700 | [diff] [blame] | 458 | }); |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 459 | |
| 460 | // Process codec events until EOS is reached, transcoding is stopped or an error occurs. |
Linus Nilsson | c6221db | 2020-03-18 14:46:22 -0700 | [diff] [blame] | 461 | while (!mStopRequested && !mEosFromEncoder && mStatus == AMEDIA_OK) { |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 462 | std::function<void()> message = mCodecMessageQueue.pop(); |
| 463 | message(); |
| 464 | } |
| 465 | |
| 466 | // Return error if transcoding was stopped before it finished. |
Linus Nilsson | c6221db | 2020-03-18 14:46:22 -0700 | [diff] [blame] | 467 | if (mStopRequested && !mEosFromEncoder && mStatus == AMEDIA_OK) { |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 468 | mStatus = AMEDIA_ERROR_UNKNOWN; // TODO: Define custom error codes? |
| 469 | } |
| 470 | |
| 471 | AMediaCodec_stop(mDecoder); |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 472 | return mStatus; |
| 473 | } |
| 474 | |
| 475 | void VideoTrackTranscoder::abortTranscodeLoop() { |
| 476 | // Push abort message to the front of the codec event queue. |
| 477 | mCodecMessageQueue.push([this] { mStopRequested = true; }, true /* front */); |
| 478 | } |
| 479 | |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 480 | std::shared_ptr<AMediaFormat> VideoTrackTranscoder::getOutputFormat() const { |
Chong Zhang | a2cc86b | 2020-06-17 16:56:49 -0700 | [diff] [blame] | 481 | return mActualOutputFormat; |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 482 | } |
| 483 | |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 484 | } // namespace android |