Linus Nilsson | c6221db | 2020-03-18 14:46:22 -0700 | [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 "PassthroughTrackTranscoder" |
| 19 | |
| 20 | #include <android-base/logging.h> |
| 21 | #include <media/PassthroughTrackTranscoder.h> |
| 22 | |
| 23 | namespace android { |
| 24 | |
| 25 | PassthroughTrackTranscoder::BufferPool::~BufferPool() { |
| 26 | for (auto it = mAddressSizeMap.begin(); it != mAddressSizeMap.end(); ++it) { |
| 27 | delete[] it->first; |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | uint8_t* PassthroughTrackTranscoder::BufferPool::getBufferWithSize(size_t minimumBufferSize) |
| 32 | NO_THREAD_SAFETY_ANALYSIS { |
| 33 | std::unique_lock lock(mMutex); |
| 34 | |
| 35 | // Wait if maximum number of buffers are allocated but none are free. |
| 36 | while (mAddressSizeMap.size() >= mMaxBufferCount && mFreeBufferMap.empty() && !mAborted) { |
| 37 | mCondition.wait(lock); |
| 38 | } |
| 39 | |
| 40 | if (mAborted) { |
| 41 | return nullptr; |
| 42 | } |
| 43 | |
| 44 | // Check if the free list contains a large enough buffer. |
| 45 | auto it = mFreeBufferMap.lower_bound(minimumBufferSize); |
| 46 | if (it != mFreeBufferMap.end()) { |
Linus Nilsson | 16a5521 | 2020-07-14 15:59:58 -0700 | [diff] [blame] | 47 | uint8_t* buffer = it->second; |
Linus Nilsson | c6221db | 2020-03-18 14:46:22 -0700 | [diff] [blame] | 48 | mFreeBufferMap.erase(it); |
Linus Nilsson | 16a5521 | 2020-07-14 15:59:58 -0700 | [diff] [blame] | 49 | return buffer; |
| 50 | } |
| 51 | |
| 52 | // If the maximum buffer count is reached, remove an existing free buffer. |
| 53 | if (mAddressSizeMap.size() >= mMaxBufferCount) { |
| 54 | auto it = mFreeBufferMap.begin(); |
| 55 | mAddressSizeMap.erase(it->second); |
| 56 | delete[] it->second; |
| 57 | mFreeBufferMap.erase(it); |
Linus Nilsson | c6221db | 2020-03-18 14:46:22 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | // Allocate a new buffer. |
| 61 | uint8_t* buffer = new (std::nothrow) uint8_t[minimumBufferSize]; |
| 62 | if (buffer == nullptr) { |
| 63 | LOG(ERROR) << "Unable to allocate new buffer of size: " << minimumBufferSize; |
| 64 | return nullptr; |
| 65 | } |
| 66 | |
Linus Nilsson | c6221db | 2020-03-18 14:46:22 -0700 | [diff] [blame] | 67 | // Add the buffer to the tracking set. |
| 68 | mAddressSizeMap.emplace(buffer, minimumBufferSize); |
| 69 | return buffer; |
| 70 | } |
| 71 | |
| 72 | void PassthroughTrackTranscoder::BufferPool::returnBuffer(uint8_t* buffer) { |
| 73 | std::scoped_lock lock(mMutex); |
| 74 | |
| 75 | if (buffer == nullptr || mAddressSizeMap.find(buffer) == mAddressSizeMap.end()) { |
| 76 | LOG(WARNING) << "Ignoring untracked buffer " << buffer; |
| 77 | return; |
| 78 | } |
| 79 | |
| 80 | mFreeBufferMap.emplace(mAddressSizeMap[buffer], buffer); |
| 81 | mCondition.notify_one(); |
| 82 | } |
| 83 | |
| 84 | void PassthroughTrackTranscoder::BufferPool::abort() { |
| 85 | std::scoped_lock lock(mMutex); |
| 86 | mAborted = true; |
| 87 | mCondition.notify_all(); |
| 88 | } |
| 89 | |
| 90 | media_status_t PassthroughTrackTranscoder::configureDestinationFormat( |
| 91 | const std::shared_ptr<AMediaFormat>& destinationFormat __unused) { |
| 92 | // Called by MediaTrackTranscoder. Passthrough doesn't care about destination so just return ok. |
| 93 | return AMEDIA_OK; |
| 94 | } |
| 95 | |
Linus Nilsson | fdb3e33 | 2020-09-18 17:11:41 -0700 | [diff] [blame] | 96 | media_status_t PassthroughTrackTranscoder::runTranscodeLoop(bool* stopped) { |
Linus Nilsson | c6221db | 2020-03-18 14:46:22 -0700 | [diff] [blame] | 97 | MediaSampleInfo info; |
| 98 | std::shared_ptr<MediaSample> sample; |
Linus Nilsson | fdb3e33 | 2020-09-18 17:11:41 -0700 | [diff] [blame] | 99 | bool eosReached = false; |
Linus Nilsson | c6221db | 2020-03-18 14:46:22 -0700 | [diff] [blame] | 100 | |
Chong Zhang | a2cc86b | 2020-06-17 16:56:49 -0700 | [diff] [blame] | 101 | // Notify the track format as soon as we start. It's same as the source format. |
| 102 | notifyTrackFormatAvailable(); |
| 103 | |
Linus Nilsson | c6221db | 2020-03-18 14:46:22 -0700 | [diff] [blame] | 104 | MediaSample::OnSampleReleasedCallback bufferReleaseCallback = |
| 105 | [bufferPool = mBufferPool](MediaSample* sample) { |
| 106 | bufferPool->returnBuffer(const_cast<uint8_t*>(sample->buffer)); |
| 107 | }; |
| 108 | |
| 109 | // Move samples until EOS is reached or transcoding is stopped. |
Linus Nilsson | fdb3e33 | 2020-09-18 17:11:41 -0700 | [diff] [blame] | 110 | while (mStopRequest != STOP_NOW && !eosReached) { |
Linus Nilsson | c6221db | 2020-03-18 14:46:22 -0700 | [diff] [blame] | 111 | media_status_t status = mMediaSampleReader->getSampleInfoForTrack(mTrackIndex, &info); |
| 112 | |
| 113 | if (status == AMEDIA_OK) { |
| 114 | uint8_t* buffer = mBufferPool->getBufferWithSize(info.size); |
| 115 | if (buffer == nullptr) { |
Linus Nilsson | fdb3e33 | 2020-09-18 17:11:41 -0700 | [diff] [blame] | 116 | if (mStopRequest == STOP_NOW) { |
Linus Nilsson | c6221db | 2020-03-18 14:46:22 -0700 | [diff] [blame] | 117 | break; |
| 118 | } |
| 119 | |
| 120 | LOG(ERROR) << "Unable to get buffer from pool"; |
Linus Nilsson | fdb3e33 | 2020-09-18 17:11:41 -0700 | [diff] [blame] | 121 | return AMEDIA_ERROR_UNKNOWN; |
Linus Nilsson | c6221db | 2020-03-18 14:46:22 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | sample = MediaSample::createWithReleaseCallback( |
| 125 | buffer, 0 /* offset */, 0 /* bufferId */, bufferReleaseCallback); |
| 126 | |
| 127 | status = mMediaSampleReader->readSampleDataForTrack(mTrackIndex, buffer, info.size); |
| 128 | if (status != AMEDIA_OK) { |
| 129 | LOG(ERROR) << "Unable to read next sample data. Aborting transcode."; |
| 130 | return status; |
| 131 | } |
| 132 | |
| 133 | } else if (status == AMEDIA_ERROR_END_OF_STREAM) { |
| 134 | sample = std::make_shared<MediaSample>(); |
Linus Nilsson | fdb3e33 | 2020-09-18 17:11:41 -0700 | [diff] [blame] | 135 | eosReached = true; |
Linus Nilsson | c6221db | 2020-03-18 14:46:22 -0700 | [diff] [blame] | 136 | } else { |
| 137 | LOG(ERROR) << "Unable to get next sample info. Aborting transcode."; |
| 138 | return status; |
| 139 | } |
| 140 | |
| 141 | sample->info = info; |
Linus Nilsson | c31d249 | 2020-09-23 12:30:00 -0700 | [diff] [blame] | 142 | onOutputSampleAvailable(sample); |
Linus Nilsson | fdb3e33 | 2020-09-18 17:11:41 -0700 | [diff] [blame] | 143 | |
| 144 | if (mStopRequest == STOP_ON_SYNC && info.flags & SAMPLE_FLAG_SYNC_SAMPLE) { |
| 145 | break; |
| 146 | } |
Linus Nilsson | c6221db | 2020-03-18 14:46:22 -0700 | [diff] [blame] | 147 | } |
| 148 | |
Linus Nilsson | fdb3e33 | 2020-09-18 17:11:41 -0700 | [diff] [blame] | 149 | if (mStopRequest != NONE && !eosReached) { |
| 150 | *stopped = true; |
Linus Nilsson | c6221db | 2020-03-18 14:46:22 -0700 | [diff] [blame] | 151 | } |
| 152 | return AMEDIA_OK; |
| 153 | } |
| 154 | |
| 155 | void PassthroughTrackTranscoder::abortTranscodeLoop() { |
Linus Nilsson | fdb3e33 | 2020-09-18 17:11:41 -0700 | [diff] [blame] | 156 | if (mStopRequest == STOP_NOW) { |
| 157 | mBufferPool->abort(); |
| 158 | } |
Linus Nilsson | c6221db | 2020-03-18 14:46:22 -0700 | [diff] [blame] | 159 | } |
| 160 | |
Linus Nilsson | cab39d8 | 2020-05-14 16:32:21 -0700 | [diff] [blame] | 161 | std::shared_ptr<AMediaFormat> PassthroughTrackTranscoder::getOutputFormat() const { |
| 162 | return mSourceFormat; |
| 163 | } |
Linus Nilsson | c6221db | 2020-03-18 14:46:22 -0700 | [diff] [blame] | 164 | } // namespace android |