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 "MediaTrackTranscoder" |
| 19 | |
| 20 | #include <android-base/logging.h> |
| 21 | #include <media/MediaTrackTranscoder.h> |
Chong Zhang | 308e91f | 2020-06-10 15:27:56 -0700 | [diff] [blame] | 22 | #include <media/MediaTrackTranscoderCallback.h> |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 23 | |
| 24 | namespace android { |
| 25 | |
| 26 | media_status_t MediaTrackTranscoder::configure( |
| 27 | const std::shared_ptr<MediaSampleReader>& mediaSampleReader, int trackIndex, |
| 28 | const std::shared_ptr<AMediaFormat>& destinationFormat) { |
| 29 | std::scoped_lock lock{mStateMutex}; |
| 30 | |
| 31 | if (mState != UNINITIALIZED) { |
| 32 | LOG(ERROR) << "Configure can only be called once"; |
| 33 | return AMEDIA_ERROR_UNSUPPORTED; |
| 34 | } |
| 35 | |
| 36 | if (mediaSampleReader == nullptr) { |
| 37 | LOG(ERROR) << "MediaSampleReader is null"; |
| 38 | return AMEDIA_ERROR_INVALID_PARAMETER; |
| 39 | } |
| 40 | if (trackIndex < 0 || trackIndex >= mediaSampleReader->getTrackCount()) { |
| 41 | LOG(ERROR) << "TrackIndex is invalid " << trackIndex; |
| 42 | return AMEDIA_ERROR_INVALID_PARAMETER; |
| 43 | } |
| 44 | |
| 45 | mMediaSampleReader = mediaSampleReader; |
| 46 | mTrackIndex = trackIndex; |
| 47 | |
Linus Nilsson | 800793f | 2020-07-31 16:16:38 -0700 | [diff] [blame] | 48 | mSourceFormat = std::shared_ptr<AMediaFormat>(mMediaSampleReader->getTrackFormat(mTrackIndex), |
| 49 | &AMediaFormat_delete); |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 50 | if (mSourceFormat == nullptr) { |
| 51 | LOG(ERROR) << "Unable to get format for track #" << mTrackIndex; |
| 52 | return AMEDIA_ERROR_MALFORMED; |
| 53 | } |
| 54 | |
| 55 | media_status_t status = configureDestinationFormat(destinationFormat); |
| 56 | if (status != AMEDIA_OK) { |
| 57 | LOG(ERROR) << "configure failed with error " << status; |
| 58 | return status; |
| 59 | } |
| 60 | |
| 61 | mState = CONFIGURED; |
| 62 | return AMEDIA_OK; |
| 63 | } |
| 64 | |
| 65 | bool MediaTrackTranscoder::start() { |
| 66 | std::scoped_lock lock{mStateMutex}; |
| 67 | |
| 68 | if (mState != CONFIGURED) { |
| 69 | LOG(ERROR) << "TrackTranscoder must be configured before started"; |
| 70 | return false; |
| 71 | } |
Linus Nilsson | fdb3e33 | 2020-09-18 17:11:41 -0700 | [diff] [blame] | 72 | mState = STARTED; |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 73 | |
Linus Nilsson | fdb3e33 | 2020-09-18 17:11:41 -0700 | [diff] [blame] | 74 | std::thread([this] { |
| 75 | bool stopped = false; |
| 76 | media_status_t status = runTranscodeLoop(&stopped); |
| 77 | |
| 78 | // Output an EOS sample if the transcoder was stopped. |
| 79 | if (stopped) { |
| 80 | auto sample = std::make_shared<MediaSample>(); |
| 81 | sample->info.flags = SAMPLE_FLAG_END_OF_STREAM; |
| 82 | onOutputSampleAvailable(sample); |
| 83 | } |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 84 | |
| 85 | // Notify the client. |
| 86 | if (auto callbacks = mTranscoderCallback.lock()) { |
Linus Nilsson | fdb3e33 | 2020-09-18 17:11:41 -0700 | [diff] [blame] | 87 | if (stopped) { |
| 88 | callbacks->onTrackStopped(this); |
| 89 | } else if (status == AMEDIA_OK) { |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 90 | callbacks->onTrackFinished(this); |
Linus Nilsson | fdb3e33 | 2020-09-18 17:11:41 -0700 | [diff] [blame] | 91 | } else { |
| 92 | callbacks->onTrackError(this, status); |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 93 | } |
| 94 | } |
Linus Nilsson | fdb3e33 | 2020-09-18 17:11:41 -0700 | [diff] [blame] | 95 | }).detach(); |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 96 | |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 97 | return true; |
| 98 | } |
| 99 | |
Linus Nilsson | fdb3e33 | 2020-09-18 17:11:41 -0700 | [diff] [blame] | 100 | void MediaTrackTranscoder::stop(bool stopOnSyncSample) { |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 101 | std::scoped_lock lock{mStateMutex}; |
| 102 | |
Linus Nilsson | fdb3e33 | 2020-09-18 17:11:41 -0700 | [diff] [blame] | 103 | if (mState == STARTED || (mStopRequest == STOP_ON_SYNC && !stopOnSyncSample)) { |
| 104 | mStopRequest = stopOnSyncSample ? STOP_ON_SYNC : STOP_NOW; |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 105 | abortTranscodeLoop(); |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 106 | mState = STOPPED; |
Linus Nilsson | fdb3e33 | 2020-09-18 17:11:41 -0700 | [diff] [blame] | 107 | } else { |
| 108 | LOG(WARNING) << "TrackTranscoder must be started before stopped"; |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 109 | } |
Linus Nilsson | 0da327a | 2020-01-31 16:22:18 -0800 | [diff] [blame] | 110 | } |
| 111 | |
Chong Zhang | a2cc86b | 2020-06-17 16:56:49 -0700 | [diff] [blame] | 112 | void MediaTrackTranscoder::notifyTrackFormatAvailable() { |
| 113 | if (auto callbacks = mTranscoderCallback.lock()) { |
| 114 | callbacks->onTrackFormatAvailable(this); |
| 115 | } |
| 116 | } |
| 117 | |
Linus Nilsson | c31d249 | 2020-09-23 12:30:00 -0700 | [diff] [blame] | 118 | void MediaTrackTranscoder::onOutputSampleAvailable(const std::shared_ptr<MediaSample>& sample) { |
| 119 | std::scoped_lock lock{mSampleMutex}; |
| 120 | if (mSampleConsumer == nullptr) { |
| 121 | mSampleQueue.enqueue(sample); |
| 122 | } else { |
| 123 | mSampleConsumer(sample); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | void MediaTrackTranscoder::setSampleConsumer( |
| 128 | const MediaSampleWriter::MediaSampleConsumerFunction& sampleConsumer) { |
| 129 | std::scoped_lock lock{mSampleMutex}; |
| 130 | mSampleConsumer = sampleConsumer; |
| 131 | |
| 132 | std::shared_ptr<MediaSample> sample; |
| 133 | while (!mSampleQueue.isEmpty() && !mSampleQueue.dequeue(&sample)) { |
| 134 | mSampleConsumer(sample); |
| 135 | } |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | } // namespace android |