blob: 84ff9a602dc3424f091598168e6a28ffd70cda1b [file] [log] [blame]
Wei Jiae9a5b962016-02-12 11:38:27 -08001/*
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 Kang4d0c0a82018-01-24 17:37:43 -080020#include <list>
21
22#include <binder/MemoryDealer.h>
Wei Jiae9a5b962016-02-12 11:38:27 -080023#include <media/stagefright/foundation/ADebug.h>
24#include <media/stagefright/MediaBuffer.h>
25#include <media/stagefright/MediaBufferGroup.h>
Dongwon Kang4d0c0a82018-01-24 17:37:43 -080026#include <utils/threads.h>
Wei Jiae9a5b962016-02-12 11:38:27 -080027
28namespace android {
29
Andy Hungcdeb6602016-06-28 17:21:44 -070030// std::min is not constexpr in C++11
31template<typename T>
32constexpr 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.
36static const size_t kSharedMemoryThreshold = MIN(
37 (size_t)MediaBuffer::kSharedMemThreshold, (size_t)(4 * 1024));
38
Dongwon Kang4d0c0a82018-01-24 17:37:43 -080039struct MediaBufferGroup::InternalData {
40 Mutex mLock;
41 Condition mCondition;
42 size_t mGrowthLimit; // Do not automatically grow group larger than this.
Dongwon Kang1889c3e2018-02-01 13:44:57 -080043 std::list<MediaBufferBase *> mBuffers;
Dongwon Kang4d0c0a82018-01-24 17:37:43 -080044};
45
46MediaBufferGroup::MediaBufferGroup(size_t growthLimit)
Marco Nelissen0e043b62018-11-14 11:26:05 -080047 : mWrapper(nullptr), mInternal(new InternalData()) {
Dongwon Kang4d0c0a82018-01-24 17:37:43 -080048 mInternal->mGrowthLimit = growthLimit;
Wei Jiae9a5b962016-02-12 11:38:27 -080049}
50
Andy Hungcdeb6602016-06-28 17:21:44 -070051MediaBufferGroup::MediaBufferGroup(size_t buffers, size_t buffer_size, size_t growthLimit)
Marco Nelissen0e043b62018-11-14 11:26:05 -080052 : mWrapper(nullptr), mInternal(new InternalData()) {
53 init(buffers, buffer_size, growthLimit);
54}
55
56void MediaBufferGroup::init(size_t buffers, size_t buffer_size, size_t growthLimit) {
Dongwon Kang4d0c0a82018-01-24 17:37:43 -080057 mInternal->mGrowthLimit = growthLimit;
Andy Hungcdeb6602016-06-28 17:21:44 -070058
Dongwon Kang4d0c0a82018-01-24 17:37:43 -080059 if (mInternal->mGrowthLimit > 0 && buffers > mInternal->mGrowthLimit) {
Andy Hung7f7dea62016-12-20 15:43:37 -080060 ALOGW("Preallocated buffers %zu > growthLimit %zu, increasing growthLimit",
Dongwon Kang4d0c0a82018-01-24 17:37:43 -080061 buffers, mInternal->mGrowthLimit);
62 mInternal->mGrowthLimit = buffers;
Andy Hung7f7dea62016-12-20 15:43:37 -080063 }
64
Marco Nelissenff0508b2018-12-14 15:17:58 -080065#ifndef NO_IMEMORY
Andy Hungcdeb6602016-06-28 17:21:44 -070066 if (buffer_size >= kSharedMemoryThreshold) {
67 ALOGD("creating MemoryDealer");
68 // Using a single MemoryDealer is efficient for a group of shared memory objects.
69 // This loop guarantees that we use shared memory (no fallback to malloc).
70
71 size_t alignment = MemoryDealer::getAllocationAlignment();
72 size_t augmented_size = buffer_size + sizeof(MediaBuffer::SharedControl);
73 size_t total = (augmented_size + alignment - 1) / alignment * alignment * buffers;
74 sp<MemoryDealer> memoryDealer = new MemoryDealer(total, "MediaBufferGroup");
75
76 for (size_t i = 0; i < buffers; ++i) {
77 sp<IMemory> mem = memoryDealer->allocate(augmented_size);
Andy Hung9bd3c9b2016-09-07 14:42:55 -070078 if (mem.get() == nullptr || mem->pointer() == nullptr) {
Andy Hungcdeb6602016-06-28 17:21:44 -070079 ALOGW("Only allocated %zu shared buffers of size %zu", i, buffer_size);
80 break;
81 }
82 MediaBuffer *buffer = new MediaBuffer(mem);
83 buffer->getSharedControl()->clear();
84 add_buffer(buffer);
85 }
86 return;
87 }
Marco Nelissenff0508b2018-12-14 15:17:58 -080088#else
89 (void)kSharedMemoryThreshold;
90#endif
Andy Hungcdeb6602016-06-28 17:21:44 -070091
92 // Non-shared memory allocation.
93 for (size_t i = 0; i < buffers; ++i) {
94 MediaBuffer *buffer = new MediaBuffer(buffer_size);
95 if (buffer->data() == nullptr) {
96 delete buffer; // don't call release, it's not properly formed
97 ALOGW("Only allocated %zu malloc buffers of size %zu", i, buffer_size);
98 break;
99 }
100 add_buffer(buffer);
101 }
102}
103
Wei Jiae9a5b962016-02-12 11:38:27 -0800104MediaBufferGroup::~MediaBufferGroup() {
Dongwon Kang1889c3e2018-02-01 13:44:57 -0800105 for (MediaBufferBase *buffer : mInternal->mBuffers) {
Andy Hung9bd3c9b2016-09-07 14:42:55 -0700106 if (buffer->refcount() != 0) {
107 const int localRefcount = buffer->localRefcount();
108 const int remoteRefcount = buffer->remoteRefcount();
109
110 // Fatal if we have a local refcount.
111 LOG_ALWAYS_FATAL_IF(localRefcount != 0,
112 "buffer(%p) localRefcount %d != 0, remoteRefcount %d",
113 buffer, localRefcount, remoteRefcount);
114
115 // Log an error if we have a remaining remote refcount,
116 // as the remote process may have died or may have inappropriate behavior.
117 // The shared memory associated with the MediaBuffer will
118 // automatically be reclaimed when there are no remaining fds
119 // associated with it.
120 ALOGE("buffer(%p) has residual remoteRefcount %d",
121 buffer, remoteRefcount);
122 }
123 // gracefully delete.
Andy Hungf59c0ba2016-06-15 17:59:30 -0700124 buffer->setObserver(nullptr);
Wei Jiae9a5b962016-02-12 11:38:27 -0800125 buffer->release();
126 }
Dongwon Kang4d0c0a82018-01-24 17:37:43 -0800127 delete mInternal;
Marco Nelissenbe9768e2018-12-19 13:10:35 -0800128 delete mWrapper;
Wei Jiae9a5b962016-02-12 11:38:27 -0800129}
130
Dongwon Kang1889c3e2018-02-01 13:44:57 -0800131void MediaBufferGroup::add_buffer(MediaBufferBase *buffer) {
Dongwon Kang4d0c0a82018-01-24 17:37:43 -0800132 Mutex::Autolock autoLock(mInternal->mLock);
Wei Jiae9a5b962016-02-12 11:38:27 -0800133
Andy Hung7f7dea62016-12-20 15:43:37 -0800134 // if we're above our growth limit, release buffers if we can
Dongwon Kang4d0c0a82018-01-24 17:37:43 -0800135 for (auto it = mInternal->mBuffers.begin();
136 mInternal->mGrowthLimit > 0
137 && mInternal->mBuffers.size() >= mInternal->mGrowthLimit
138 && it != mInternal->mBuffers.end();) {
Andy Hung7f7dea62016-12-20 15:43:37 -0800139 if ((*it)->refcount() == 0) {
140 (*it)->setObserver(nullptr);
141 (*it)->release();
Dongwon Kang4d0c0a82018-01-24 17:37:43 -0800142 it = mInternal->mBuffers.erase(it);
Andy Hung7f7dea62016-12-20 15:43:37 -0800143 } else {
144 ++it;
145 }
146 }
147
Wei Jiae9a5b962016-02-12 11:38:27 -0800148 buffer->setObserver(this);
Dongwon Kang4d0c0a82018-01-24 17:37:43 -0800149 mInternal->mBuffers.emplace_back(buffer);
Andy Hungf59c0ba2016-06-15 17:59:30 -0700150}
Wei Jiae9a5b962016-02-12 11:38:27 -0800151
Andy Hungcdeb6602016-06-28 17:21:44 -0700152bool MediaBufferGroup::has_buffers() {
Dongwon Kang4d0c0a82018-01-24 17:37:43 -0800153 if (mInternal->mBuffers.size() < mInternal->mGrowthLimit) {
Andy Hungcdeb6602016-06-28 17:21:44 -0700154 return true; // We can add more buffers internally.
155 }
Dongwon Kang1889c3e2018-02-01 13:44:57 -0800156 for (MediaBufferBase *buffer : mInternal->mBuffers) {
Andy Hungcdeb6602016-06-28 17:21:44 -0700157 if (buffer->refcount() == 0) {
158 return true;
159 }
160 }
161 return false;
162}
163
Wei Jiae9a5b962016-02-12 11:38:27 -0800164status_t MediaBufferGroup::acquire_buffer(
Dongwon Kang1889c3e2018-02-01 13:44:57 -0800165 MediaBufferBase **out, bool nonBlocking, size_t requestedSize) {
Dongwon Kang4d0c0a82018-01-24 17:37:43 -0800166 Mutex::Autolock autoLock(mInternal->mLock);
Wei Jiae9a5b962016-02-12 11:38:27 -0800167 for (;;) {
Wei Jiae9a5b962016-02-12 11:38:27 -0800168 size_t smallest = requestedSize;
Marco Nelissenefa12512018-09-17 12:18:03 -0700169 size_t biggest = requestedSize;
Dongwon Kang1889c3e2018-02-01 13:44:57 -0800170 MediaBufferBase *buffer = nullptr;
Dongwon Kang4d0c0a82018-01-24 17:37:43 -0800171 auto free = mInternal->mBuffers.end();
172 for (auto it = mInternal->mBuffers.begin(); it != mInternal->mBuffers.end(); ++it) {
Marco Nelissenefa12512018-09-17 12:18:03 -0700173 const size_t size = (*it)->size();
174 if (size > biggest) {
175 biggest = size;
176 }
Andy Hungf59c0ba2016-06-15 17:59:30 -0700177 if ((*it)->refcount() == 0) {
Andy Hungf59c0ba2016-06-15 17:59:30 -0700178 if (size >= requestedSize) {
179 buffer = *it;
180 break;
181 }
182 if (size < smallest) {
183 smallest = size; // always free the smallest buf
184 free = it;
185 }
Wei Jiae9a5b962016-02-12 11:38:27 -0800186 }
Wei Jiae9a5b962016-02-12 11:38:27 -0800187 }
Andy Hungf59c0ba2016-06-15 17:59:30 -0700188 if (buffer == nullptr
Dongwon Kang4d0c0a82018-01-24 17:37:43 -0800189 && (free != mInternal->mBuffers.end()
190 || mInternal->mBuffers.size() < mInternal->mGrowthLimit)) {
Andy Hungf59c0ba2016-06-15 17:59:30 -0700191 // We alloc before we free so failure leaves group unchanged.
Marco Nelissenefa12512018-09-17 12:18:03 -0700192 const size_t allocateSize = requestedSize == 0 ? biggest :
193 requestedSize < SIZE_MAX / 3 * 2 /* NB: ordering */ ?
Andy Hungf59c0ba2016-06-15 17:59:30 -0700194 requestedSize * 3 / 2 : requestedSize;
195 buffer = new MediaBuffer(allocateSize);
196 if (buffer->data() == nullptr) {
197 ALOGE("Allocation failure for size %zu", allocateSize);
198 delete buffer; // Invalid alloc, prefer not to call release.
199 buffer = nullptr;
200 } else {
201 buffer->setObserver(this);
Dongwon Kang4d0c0a82018-01-24 17:37:43 -0800202 if (free != mInternal->mBuffers.end()) {
Andy Hungf59c0ba2016-06-15 17:59:30 -0700203 ALOGV("reallocate buffer, requested size %zu vs available %zu",
204 requestedSize, (*free)->size());
205 (*free)->setObserver(nullptr);
206 (*free)->release();
207 *free = buffer; // in-place replace
208 } else {
209 ALOGV("allocate buffer, requested size %zu", requestedSize);
Dongwon Kang4d0c0a82018-01-24 17:37:43 -0800210 mInternal->mBuffers.emplace_back(buffer);
Andy Hungf59c0ba2016-06-15 17:59:30 -0700211 }
Wei Jiae9a5b962016-02-12 11:38:27 -0800212 }
Wei Jiae9a5b962016-02-12 11:38:27 -0800213 }
Andy Hungf59c0ba2016-06-15 17:59:30 -0700214 if (buffer != nullptr) {
Wei Jiae9a5b962016-02-12 11:38:27 -0800215 buffer->add_ref();
216 buffer->reset();
Wei Jiae9a5b962016-02-12 11:38:27 -0800217 *out = buffer;
Andy Hungf59c0ba2016-06-15 17:59:30 -0700218 return OK;
Wei Jiae9a5b962016-02-12 11:38:27 -0800219 }
Wei Jiae9a5b962016-02-12 11:38:27 -0800220 if (nonBlocking) {
Andy Hungf59c0ba2016-06-15 17:59:30 -0700221 *out = nullptr;
Wei Jiae9a5b962016-02-12 11:38:27 -0800222 return WOULD_BLOCK;
223 }
Andy Hungf59c0ba2016-06-15 17:59:30 -0700224 // All buffers are in use, block until one of them is returned.
Dongwon Kang4d0c0a82018-01-24 17:37:43 -0800225 mInternal->mCondition.wait(mInternal->mLock);
Wei Jiae9a5b962016-02-12 11:38:27 -0800226 }
Andy Hungf59c0ba2016-06-15 17:59:30 -0700227 // Never gets here.
Wei Jiae9a5b962016-02-12 11:38:27 -0800228}
229
Dongwon Kang4d0c0a82018-01-24 17:37:43 -0800230size_t MediaBufferGroup::buffers() const {
231 return mInternal->mBuffers.size();
232}
233
Dongwon Kang1889c3e2018-02-01 13:44:57 -0800234void MediaBufferGroup::signalBufferReturned(MediaBufferBase *) {
Dongwon Kang4d0c0a82018-01-24 17:37:43 -0800235 Mutex::Autolock autoLock(mInternal->mLock);
236 mInternal->mCondition.signal();
Wei Jiae9a5b962016-02-12 11:38:27 -0800237}
238
239} // namespace android