blob: a4cbf33f8caf340d4c8ae9ecf4589a539148366d [file] [log] [blame]
Linus Nilssonc6221db2020-03-18 14:46:22 -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 "PassthroughTrackTranscoder"
19
20#include <android-base/logging.h>
21#include <media/PassthroughTrackTranscoder.h>
22
23namespace android {
24
25PassthroughTrackTranscoder::BufferPool::~BufferPool() {
26 for (auto it = mAddressSizeMap.begin(); it != mAddressSizeMap.end(); ++it) {
27 delete[] it->first;
28 }
29}
30
31uint8_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()) {
47 mFreeBufferMap.erase(it);
48 return it->second;
49 }
50
51 // Allocate a new buffer.
52 uint8_t* buffer = new (std::nothrow) uint8_t[minimumBufferSize];
53 if (buffer == nullptr) {
54 LOG(ERROR) << "Unable to allocate new buffer of size: " << minimumBufferSize;
55 return nullptr;
56 }
57
58 // If the maximum buffer count is reached, remove an existing free buffer.
59 if (mAddressSizeMap.size() >= mMaxBufferCount) {
60 auto it = mFreeBufferMap.begin();
61 mFreeBufferMap.erase(it);
62 mAddressSizeMap.erase(it->second);
63 delete[] it->second;
64 }
65
66 // Add the buffer to the tracking set.
67 mAddressSizeMap.emplace(buffer, minimumBufferSize);
68 return buffer;
69}
70
71void PassthroughTrackTranscoder::BufferPool::returnBuffer(uint8_t* buffer) {
72 std::scoped_lock lock(mMutex);
73
74 if (buffer == nullptr || mAddressSizeMap.find(buffer) == mAddressSizeMap.end()) {
75 LOG(WARNING) << "Ignoring untracked buffer " << buffer;
76 return;
77 }
78
79 mFreeBufferMap.emplace(mAddressSizeMap[buffer], buffer);
80 mCondition.notify_one();
81}
82
83void PassthroughTrackTranscoder::BufferPool::abort() {
84 std::scoped_lock lock(mMutex);
85 mAborted = true;
86 mCondition.notify_all();
87}
88
89media_status_t PassthroughTrackTranscoder::configureDestinationFormat(
90 const std::shared_ptr<AMediaFormat>& destinationFormat __unused) {
91 // Called by MediaTrackTranscoder. Passthrough doesn't care about destination so just return ok.
92 return AMEDIA_OK;
93}
94
95media_status_t PassthroughTrackTranscoder::runTranscodeLoop() {
96 MediaSampleInfo info;
97 std::shared_ptr<MediaSample> sample;
98
Chong Zhanga2cc86b2020-06-17 16:56:49 -070099 // Notify the track format as soon as we start. It's same as the source format.
100 notifyTrackFormatAvailable();
101
Linus Nilssonc6221db2020-03-18 14:46:22 -0700102 MediaSample::OnSampleReleasedCallback bufferReleaseCallback =
103 [bufferPool = mBufferPool](MediaSample* sample) {
104 bufferPool->returnBuffer(const_cast<uint8_t*>(sample->buffer));
105 };
106
107 // Move samples until EOS is reached or transcoding is stopped.
108 while (!mStopRequested && !mEosFromSource) {
109 media_status_t status = mMediaSampleReader->getSampleInfoForTrack(mTrackIndex, &info);
110
111 if (status == AMEDIA_OK) {
112 uint8_t* buffer = mBufferPool->getBufferWithSize(info.size);
113 if (buffer == nullptr) {
114 if (mStopRequested) {
115 break;
116 }
117
118 LOG(ERROR) << "Unable to get buffer from pool";
119 return AMEDIA_ERROR_IO; // TODO: Custom error codes?
120 }
121
122 sample = MediaSample::createWithReleaseCallback(
123 buffer, 0 /* offset */, 0 /* bufferId */, bufferReleaseCallback);
124
125 status = mMediaSampleReader->readSampleDataForTrack(mTrackIndex, buffer, info.size);
126 if (status != AMEDIA_OK) {
127 LOG(ERROR) << "Unable to read next sample data. Aborting transcode.";
128 return status;
129 }
130
131 } else if (status == AMEDIA_ERROR_END_OF_STREAM) {
132 sample = std::make_shared<MediaSample>();
133 mEosFromSource = true;
134 } else {
135 LOG(ERROR) << "Unable to get next sample info. Aborting transcode.";
136 return status;
137 }
138
139 sample->info = info;
Linus Nilssoncab39d82020-05-14 16:32:21 -0700140 if (mOutputQueue->enqueue(sample)) {
Linus Nilssonc6221db2020-03-18 14:46:22 -0700141 LOG(ERROR) << "Output queue aborted";
142 return AMEDIA_ERROR_IO;
143 }
144
145 mMediaSampleReader->advanceTrack(mTrackIndex);
146 }
147
148 if (mStopRequested && !mEosFromSource) {
149 return AMEDIA_ERROR_UNKNOWN; // TODO: Custom error codes?
150 }
151 return AMEDIA_OK;
152}
153
154void PassthroughTrackTranscoder::abortTranscodeLoop() {
155 mStopRequested = true;
156 mBufferPool->abort();
157}
158
Linus Nilssoncab39d82020-05-14 16:32:21 -0700159std::shared_ptr<AMediaFormat> PassthroughTrackTranscoder::getOutputFormat() const {
160 return mSourceFormat;
161}
Linus Nilssonc6221db2020-03-18 14:46:22 -0700162} // namespace android