Wei Jia | e9a5b96 | 2016-02-12 11:38:27 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 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_TAG "MediaBufferGroup" |
| 18 | #include <utils/Log.h> |
| 19 | |
Dongwon Kang | 4d0c0a8 | 2018-01-24 17:37:43 -0800 | [diff] [blame] | 20 | #include <list> |
| 21 | |
| 22 | #include <binder/MemoryDealer.h> |
Wei Jia | e9a5b96 | 2016-02-12 11:38:27 -0800 | [diff] [blame] | 23 | #include <media/stagefright/foundation/ADebug.h> |
| 24 | #include <media/stagefright/MediaBuffer.h> |
| 25 | #include <media/stagefright/MediaBufferGroup.h> |
Dongwon Kang | 4d0c0a8 | 2018-01-24 17:37:43 -0800 | [diff] [blame] | 26 | #include <utils/threads.h> |
Wei Jia | e9a5b96 | 2016-02-12 11:38:27 -0800 | [diff] [blame] | 27 | |
| 28 | namespace android { |
| 29 | |
Andy Hung | cdeb660 | 2016-06-28 17:21:44 -0700 | [diff] [blame] | 30 | // std::min is not constexpr in C++11 |
| 31 | template<typename T> |
| 32 | constexpr T MIN(const T &a, const T &b) { return a <= b ? a : b; } |
| 33 | |
| 34 | // MediaBufferGroup may create shared memory buffers at a |
| 35 | // smaller threshold than an isolated new MediaBuffer. |
| 36 | static const size_t kSharedMemoryThreshold = MIN( |
| 37 | (size_t)MediaBuffer::kSharedMemThreshold, (size_t)(4 * 1024)); |
| 38 | |
Dongwon Kang | 4d0c0a8 | 2018-01-24 17:37:43 -0800 | [diff] [blame] | 39 | struct MediaBufferGroup::InternalData { |
| 40 | Mutex mLock; |
| 41 | Condition mCondition; |
| 42 | size_t mGrowthLimit; // Do not automatically grow group larger than this. |
Dongwon Kang | 1889c3e | 2018-02-01 13:44:57 -0800 | [diff] [blame] | 43 | std::list<MediaBufferBase *> mBuffers; |
Dongwon Kang | 4d0c0a8 | 2018-01-24 17:37:43 -0800 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | MediaBufferGroup::MediaBufferGroup(size_t growthLimit) |
| 47 | : mInternal(new InternalData()) { |
| 48 | mInternal->mGrowthLimit = growthLimit; |
Wei Jia | e9a5b96 | 2016-02-12 11:38:27 -0800 | [diff] [blame] | 49 | } |
| 50 | |
Andy Hung | cdeb660 | 2016-06-28 17:21:44 -0700 | [diff] [blame] | 51 | MediaBufferGroup::MediaBufferGroup(size_t buffers, size_t buffer_size, size_t growthLimit) |
Dongwon Kang | 4d0c0a8 | 2018-01-24 17:37:43 -0800 | [diff] [blame] | 52 | : mInternal(new InternalData()) { |
| 53 | mInternal->mGrowthLimit = growthLimit; |
Andy Hung | cdeb660 | 2016-06-28 17:21:44 -0700 | [diff] [blame] | 54 | |
Dongwon Kang | 4d0c0a8 | 2018-01-24 17:37:43 -0800 | [diff] [blame] | 55 | if (mInternal->mGrowthLimit > 0 && buffers > mInternal->mGrowthLimit) { |
Andy Hung | 7f7dea6 | 2016-12-20 15:43:37 -0800 | [diff] [blame] | 56 | ALOGW("Preallocated buffers %zu > growthLimit %zu, increasing growthLimit", |
Dongwon Kang | 4d0c0a8 | 2018-01-24 17:37:43 -0800 | [diff] [blame] | 57 | buffers, mInternal->mGrowthLimit); |
| 58 | mInternal->mGrowthLimit = buffers; |
Andy Hung | 7f7dea6 | 2016-12-20 15:43:37 -0800 | [diff] [blame] | 59 | } |
| 60 | |
Andy Hung | cdeb660 | 2016-06-28 17:21:44 -0700 | [diff] [blame] | 61 | if (buffer_size >= kSharedMemoryThreshold) { |
| 62 | ALOGD("creating MemoryDealer"); |
| 63 | // Using a single MemoryDealer is efficient for a group of shared memory objects. |
| 64 | // This loop guarantees that we use shared memory (no fallback to malloc). |
| 65 | |
| 66 | size_t alignment = MemoryDealer::getAllocationAlignment(); |
| 67 | size_t augmented_size = buffer_size + sizeof(MediaBuffer::SharedControl); |
| 68 | size_t total = (augmented_size + alignment - 1) / alignment * alignment * buffers; |
| 69 | sp<MemoryDealer> memoryDealer = new MemoryDealer(total, "MediaBufferGroup"); |
| 70 | |
| 71 | for (size_t i = 0; i < buffers; ++i) { |
| 72 | sp<IMemory> mem = memoryDealer->allocate(augmented_size); |
Andy Hung | 9bd3c9b | 2016-09-07 14:42:55 -0700 | [diff] [blame] | 73 | if (mem.get() == nullptr || mem->pointer() == nullptr) { |
Andy Hung | cdeb660 | 2016-06-28 17:21:44 -0700 | [diff] [blame] | 74 | ALOGW("Only allocated %zu shared buffers of size %zu", i, buffer_size); |
| 75 | break; |
| 76 | } |
| 77 | MediaBuffer *buffer = new MediaBuffer(mem); |
| 78 | buffer->getSharedControl()->clear(); |
| 79 | add_buffer(buffer); |
| 80 | } |
| 81 | return; |
| 82 | } |
| 83 | |
| 84 | // Non-shared memory allocation. |
| 85 | for (size_t i = 0; i < buffers; ++i) { |
| 86 | MediaBuffer *buffer = new MediaBuffer(buffer_size); |
| 87 | if (buffer->data() == nullptr) { |
| 88 | delete buffer; // don't call release, it's not properly formed |
| 89 | ALOGW("Only allocated %zu malloc buffers of size %zu", i, buffer_size); |
| 90 | break; |
| 91 | } |
| 92 | add_buffer(buffer); |
| 93 | } |
| 94 | } |
| 95 | |
Wei Jia | e9a5b96 | 2016-02-12 11:38:27 -0800 | [diff] [blame] | 96 | MediaBufferGroup::~MediaBufferGroup() { |
Dongwon Kang | 1889c3e | 2018-02-01 13:44:57 -0800 | [diff] [blame] | 97 | for (MediaBufferBase *buffer : mInternal->mBuffers) { |
Andy Hung | 9bd3c9b | 2016-09-07 14:42:55 -0700 | [diff] [blame] | 98 | if (buffer->refcount() != 0) { |
| 99 | const int localRefcount = buffer->localRefcount(); |
| 100 | const int remoteRefcount = buffer->remoteRefcount(); |
| 101 | |
| 102 | // Fatal if we have a local refcount. |
| 103 | LOG_ALWAYS_FATAL_IF(localRefcount != 0, |
| 104 | "buffer(%p) localRefcount %d != 0, remoteRefcount %d", |
| 105 | buffer, localRefcount, remoteRefcount); |
| 106 | |
| 107 | // Log an error if we have a remaining remote refcount, |
| 108 | // as the remote process may have died or may have inappropriate behavior. |
| 109 | // The shared memory associated with the MediaBuffer will |
| 110 | // automatically be reclaimed when there are no remaining fds |
| 111 | // associated with it. |
| 112 | ALOGE("buffer(%p) has residual remoteRefcount %d", |
| 113 | buffer, remoteRefcount); |
| 114 | } |
| 115 | // gracefully delete. |
Andy Hung | f59c0ba | 2016-06-15 17:59:30 -0700 | [diff] [blame] | 116 | buffer->setObserver(nullptr); |
Wei Jia | e9a5b96 | 2016-02-12 11:38:27 -0800 | [diff] [blame] | 117 | buffer->release(); |
| 118 | } |
Dongwon Kang | 4d0c0a8 | 2018-01-24 17:37:43 -0800 | [diff] [blame] | 119 | delete mInternal; |
Wei Jia | e9a5b96 | 2016-02-12 11:38:27 -0800 | [diff] [blame] | 120 | } |
| 121 | |
Dongwon Kang | 1889c3e | 2018-02-01 13:44:57 -0800 | [diff] [blame] | 122 | void MediaBufferGroup::add_buffer(MediaBufferBase *buffer) { |
Dongwon Kang | 4d0c0a8 | 2018-01-24 17:37:43 -0800 | [diff] [blame] | 123 | Mutex::Autolock autoLock(mInternal->mLock); |
Wei Jia | e9a5b96 | 2016-02-12 11:38:27 -0800 | [diff] [blame] | 124 | |
Andy Hung | 7f7dea6 | 2016-12-20 15:43:37 -0800 | [diff] [blame] | 125 | // if we're above our growth limit, release buffers if we can |
Dongwon Kang | 4d0c0a8 | 2018-01-24 17:37:43 -0800 | [diff] [blame] | 126 | for (auto it = mInternal->mBuffers.begin(); |
| 127 | mInternal->mGrowthLimit > 0 |
| 128 | && mInternal->mBuffers.size() >= mInternal->mGrowthLimit |
| 129 | && it != mInternal->mBuffers.end();) { |
Andy Hung | 7f7dea6 | 2016-12-20 15:43:37 -0800 | [diff] [blame] | 130 | if ((*it)->refcount() == 0) { |
| 131 | (*it)->setObserver(nullptr); |
| 132 | (*it)->release(); |
Dongwon Kang | 4d0c0a8 | 2018-01-24 17:37:43 -0800 | [diff] [blame] | 133 | it = mInternal->mBuffers.erase(it); |
Andy Hung | 7f7dea6 | 2016-12-20 15:43:37 -0800 | [diff] [blame] | 134 | } else { |
| 135 | ++it; |
| 136 | } |
| 137 | } |
| 138 | |
Wei Jia | e9a5b96 | 2016-02-12 11:38:27 -0800 | [diff] [blame] | 139 | buffer->setObserver(this); |
Dongwon Kang | 4d0c0a8 | 2018-01-24 17:37:43 -0800 | [diff] [blame] | 140 | mInternal->mBuffers.emplace_back(buffer); |
Andy Hung | f59c0ba | 2016-06-15 17:59:30 -0700 | [diff] [blame] | 141 | } |
Wei Jia | e9a5b96 | 2016-02-12 11:38:27 -0800 | [diff] [blame] | 142 | |
Andy Hung | cdeb660 | 2016-06-28 17:21:44 -0700 | [diff] [blame] | 143 | bool MediaBufferGroup::has_buffers() { |
Dongwon Kang | 4d0c0a8 | 2018-01-24 17:37:43 -0800 | [diff] [blame] | 144 | if (mInternal->mBuffers.size() < mInternal->mGrowthLimit) { |
Andy Hung | cdeb660 | 2016-06-28 17:21:44 -0700 | [diff] [blame] | 145 | return true; // We can add more buffers internally. |
| 146 | } |
Dongwon Kang | 1889c3e | 2018-02-01 13:44:57 -0800 | [diff] [blame] | 147 | for (MediaBufferBase *buffer : mInternal->mBuffers) { |
Andy Hung | cdeb660 | 2016-06-28 17:21:44 -0700 | [diff] [blame] | 148 | if (buffer->refcount() == 0) { |
| 149 | return true; |
| 150 | } |
| 151 | } |
| 152 | return false; |
| 153 | } |
| 154 | |
Wei Jia | e9a5b96 | 2016-02-12 11:38:27 -0800 | [diff] [blame] | 155 | status_t MediaBufferGroup::acquire_buffer( |
Dongwon Kang | 1889c3e | 2018-02-01 13:44:57 -0800 | [diff] [blame] | 156 | MediaBufferBase **out, bool nonBlocking, size_t requestedSize) { |
Dongwon Kang | 4d0c0a8 | 2018-01-24 17:37:43 -0800 | [diff] [blame] | 157 | Mutex::Autolock autoLock(mInternal->mLock); |
Wei Jia | e9a5b96 | 2016-02-12 11:38:27 -0800 | [diff] [blame] | 158 | for (;;) { |
Wei Jia | e9a5b96 | 2016-02-12 11:38:27 -0800 | [diff] [blame] | 159 | size_t smallest = requestedSize; |
Marco Nelissen | efa1251 | 2018-09-17 12:18:03 -0700 | [diff] [blame^] | 160 | size_t biggest = requestedSize; |
Dongwon Kang | 1889c3e | 2018-02-01 13:44:57 -0800 | [diff] [blame] | 161 | MediaBufferBase *buffer = nullptr; |
Dongwon Kang | 4d0c0a8 | 2018-01-24 17:37:43 -0800 | [diff] [blame] | 162 | auto free = mInternal->mBuffers.end(); |
| 163 | for (auto it = mInternal->mBuffers.begin(); it != mInternal->mBuffers.end(); ++it) { |
Marco Nelissen | efa1251 | 2018-09-17 12:18:03 -0700 | [diff] [blame^] | 164 | const size_t size = (*it)->size(); |
| 165 | if (size > biggest) { |
| 166 | biggest = size; |
| 167 | } |
Andy Hung | f59c0ba | 2016-06-15 17:59:30 -0700 | [diff] [blame] | 168 | if ((*it)->refcount() == 0) { |
Andy Hung | f59c0ba | 2016-06-15 17:59:30 -0700 | [diff] [blame] | 169 | if (size >= requestedSize) { |
| 170 | buffer = *it; |
| 171 | break; |
| 172 | } |
| 173 | if (size < smallest) { |
| 174 | smallest = size; // always free the smallest buf |
| 175 | free = it; |
| 176 | } |
Wei Jia | e9a5b96 | 2016-02-12 11:38:27 -0800 | [diff] [blame] | 177 | } |
Wei Jia | e9a5b96 | 2016-02-12 11:38:27 -0800 | [diff] [blame] | 178 | } |
Andy Hung | f59c0ba | 2016-06-15 17:59:30 -0700 | [diff] [blame] | 179 | if (buffer == nullptr |
Dongwon Kang | 4d0c0a8 | 2018-01-24 17:37:43 -0800 | [diff] [blame] | 180 | && (free != mInternal->mBuffers.end() |
| 181 | || mInternal->mBuffers.size() < mInternal->mGrowthLimit)) { |
Andy Hung | f59c0ba | 2016-06-15 17:59:30 -0700 | [diff] [blame] | 182 | // We alloc before we free so failure leaves group unchanged. |
Marco Nelissen | efa1251 | 2018-09-17 12:18:03 -0700 | [diff] [blame^] | 183 | const size_t allocateSize = requestedSize == 0 ? biggest : |
| 184 | requestedSize < SIZE_MAX / 3 * 2 /* NB: ordering */ ? |
Andy Hung | f59c0ba | 2016-06-15 17:59:30 -0700 | [diff] [blame] | 185 | requestedSize * 3 / 2 : requestedSize; |
| 186 | buffer = new MediaBuffer(allocateSize); |
| 187 | if (buffer->data() == nullptr) { |
| 188 | ALOGE("Allocation failure for size %zu", allocateSize); |
| 189 | delete buffer; // Invalid alloc, prefer not to call release. |
| 190 | buffer = nullptr; |
| 191 | } else { |
| 192 | buffer->setObserver(this); |
Dongwon Kang | 4d0c0a8 | 2018-01-24 17:37:43 -0800 | [diff] [blame] | 193 | if (free != mInternal->mBuffers.end()) { |
Andy Hung | f59c0ba | 2016-06-15 17:59:30 -0700 | [diff] [blame] | 194 | ALOGV("reallocate buffer, requested size %zu vs available %zu", |
| 195 | requestedSize, (*free)->size()); |
| 196 | (*free)->setObserver(nullptr); |
| 197 | (*free)->release(); |
| 198 | *free = buffer; // in-place replace |
| 199 | } else { |
| 200 | ALOGV("allocate buffer, requested size %zu", requestedSize); |
Dongwon Kang | 4d0c0a8 | 2018-01-24 17:37:43 -0800 | [diff] [blame] | 201 | mInternal->mBuffers.emplace_back(buffer); |
Andy Hung | f59c0ba | 2016-06-15 17:59:30 -0700 | [diff] [blame] | 202 | } |
Wei Jia | e9a5b96 | 2016-02-12 11:38:27 -0800 | [diff] [blame] | 203 | } |
Wei Jia | e9a5b96 | 2016-02-12 11:38:27 -0800 | [diff] [blame] | 204 | } |
Andy Hung | f59c0ba | 2016-06-15 17:59:30 -0700 | [diff] [blame] | 205 | if (buffer != nullptr) { |
Wei Jia | e9a5b96 | 2016-02-12 11:38:27 -0800 | [diff] [blame] | 206 | buffer->add_ref(); |
| 207 | buffer->reset(); |
Wei Jia | e9a5b96 | 2016-02-12 11:38:27 -0800 | [diff] [blame] | 208 | *out = buffer; |
Andy Hung | f59c0ba | 2016-06-15 17:59:30 -0700 | [diff] [blame] | 209 | return OK; |
Wei Jia | e9a5b96 | 2016-02-12 11:38:27 -0800 | [diff] [blame] | 210 | } |
Wei Jia | e9a5b96 | 2016-02-12 11:38:27 -0800 | [diff] [blame] | 211 | if (nonBlocking) { |
Andy Hung | f59c0ba | 2016-06-15 17:59:30 -0700 | [diff] [blame] | 212 | *out = nullptr; |
Wei Jia | e9a5b96 | 2016-02-12 11:38:27 -0800 | [diff] [blame] | 213 | return WOULD_BLOCK; |
| 214 | } |
Andy Hung | f59c0ba | 2016-06-15 17:59:30 -0700 | [diff] [blame] | 215 | // All buffers are in use, block until one of them is returned. |
Dongwon Kang | 4d0c0a8 | 2018-01-24 17:37:43 -0800 | [diff] [blame] | 216 | mInternal->mCondition.wait(mInternal->mLock); |
Wei Jia | e9a5b96 | 2016-02-12 11:38:27 -0800 | [diff] [blame] | 217 | } |
Andy Hung | f59c0ba | 2016-06-15 17:59:30 -0700 | [diff] [blame] | 218 | // Never gets here. |
Wei Jia | e9a5b96 | 2016-02-12 11:38:27 -0800 | [diff] [blame] | 219 | } |
| 220 | |
Dongwon Kang | 4d0c0a8 | 2018-01-24 17:37:43 -0800 | [diff] [blame] | 221 | size_t MediaBufferGroup::buffers() const { |
| 222 | return mInternal->mBuffers.size(); |
| 223 | } |
| 224 | |
Dongwon Kang | 1889c3e | 2018-02-01 13:44:57 -0800 | [diff] [blame] | 225 | void MediaBufferGroup::signalBufferReturned(MediaBufferBase *) { |
Dongwon Kang | 4d0c0a8 | 2018-01-24 17:37:43 -0800 | [diff] [blame] | 226 | Mutex::Autolock autoLock(mInternal->mLock); |
| 227 | mInternal->mCondition.signal(); |
Wei Jia | e9a5b96 | 2016-02-12 11:38:27 -0800 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | } // namespace android |