blob: 2a8dd41c06768b00b525d246ef45525522f1e5b2 [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)
47 : mInternal(new InternalData()) {
48 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)
Dongwon Kang4d0c0a82018-01-24 17:37:43 -080052 : mInternal(new InternalData()) {
53 mInternal->mGrowthLimit = growthLimit;
Andy Hungcdeb6602016-06-28 17:21:44 -070054
Dongwon Kang4d0c0a82018-01-24 17:37:43 -080055 if (mInternal->mGrowthLimit > 0 && buffers > mInternal->mGrowthLimit) {
Andy Hung7f7dea62016-12-20 15:43:37 -080056 ALOGW("Preallocated buffers %zu > growthLimit %zu, increasing growthLimit",
Dongwon Kang4d0c0a82018-01-24 17:37:43 -080057 buffers, mInternal->mGrowthLimit);
58 mInternal->mGrowthLimit = buffers;
Andy Hung7f7dea62016-12-20 15:43:37 -080059 }
60
Andy Hungcdeb6602016-06-28 17:21:44 -070061 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 Hung9bd3c9b2016-09-07 14:42:55 -070073 if (mem.get() == nullptr || mem->pointer() == nullptr) {
Andy Hungcdeb6602016-06-28 17:21:44 -070074 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 Jiae9a5b962016-02-12 11:38:27 -080096MediaBufferGroup::~MediaBufferGroup() {
Dongwon Kang1889c3e2018-02-01 13:44:57 -080097 for (MediaBufferBase *buffer : mInternal->mBuffers) {
Andy Hung9bd3c9b2016-09-07 14:42:55 -070098 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 Hungf59c0ba2016-06-15 17:59:30 -0700116 buffer->setObserver(nullptr);
Wei Jiae9a5b962016-02-12 11:38:27 -0800117 buffer->release();
118 }
Dongwon Kang4d0c0a82018-01-24 17:37:43 -0800119 delete mInternal;
Wei Jiae9a5b962016-02-12 11:38:27 -0800120}
121
Dongwon Kang1889c3e2018-02-01 13:44:57 -0800122void MediaBufferGroup::add_buffer(MediaBufferBase *buffer) {
Dongwon Kang4d0c0a82018-01-24 17:37:43 -0800123 Mutex::Autolock autoLock(mInternal->mLock);
Wei Jiae9a5b962016-02-12 11:38:27 -0800124
Andy Hung7f7dea62016-12-20 15:43:37 -0800125 // if we're above our growth limit, release buffers if we can
Dongwon Kang4d0c0a82018-01-24 17:37:43 -0800126 for (auto it = mInternal->mBuffers.begin();
127 mInternal->mGrowthLimit > 0
128 && mInternal->mBuffers.size() >= mInternal->mGrowthLimit
129 && it != mInternal->mBuffers.end();) {
Andy Hung7f7dea62016-12-20 15:43:37 -0800130 if ((*it)->refcount() == 0) {
131 (*it)->setObserver(nullptr);
132 (*it)->release();
Dongwon Kang4d0c0a82018-01-24 17:37:43 -0800133 it = mInternal->mBuffers.erase(it);
Andy Hung7f7dea62016-12-20 15:43:37 -0800134 } else {
135 ++it;
136 }
137 }
138
Wei Jiae9a5b962016-02-12 11:38:27 -0800139 buffer->setObserver(this);
Dongwon Kang4d0c0a82018-01-24 17:37:43 -0800140 mInternal->mBuffers.emplace_back(buffer);
Andy Hungf59c0ba2016-06-15 17:59:30 -0700141}
Wei Jiae9a5b962016-02-12 11:38:27 -0800142
Andy Hungcdeb6602016-06-28 17:21:44 -0700143bool MediaBufferGroup::has_buffers() {
Dongwon Kang4d0c0a82018-01-24 17:37:43 -0800144 if (mInternal->mBuffers.size() < mInternal->mGrowthLimit) {
Andy Hungcdeb6602016-06-28 17:21:44 -0700145 return true; // We can add more buffers internally.
146 }
Dongwon Kang1889c3e2018-02-01 13:44:57 -0800147 for (MediaBufferBase *buffer : mInternal->mBuffers) {
Andy Hungcdeb6602016-06-28 17:21:44 -0700148 if (buffer->refcount() == 0) {
149 return true;
150 }
151 }
152 return false;
153}
154
Wei Jiae9a5b962016-02-12 11:38:27 -0800155status_t MediaBufferGroup::acquire_buffer(
Dongwon Kang1889c3e2018-02-01 13:44:57 -0800156 MediaBufferBase **out, bool nonBlocking, size_t requestedSize) {
Dongwon Kang4d0c0a82018-01-24 17:37:43 -0800157 Mutex::Autolock autoLock(mInternal->mLock);
Wei Jiae9a5b962016-02-12 11:38:27 -0800158 for (;;) {
Wei Jiae9a5b962016-02-12 11:38:27 -0800159 size_t smallest = requestedSize;
Dongwon Kang1889c3e2018-02-01 13:44:57 -0800160 MediaBufferBase *buffer = nullptr;
Dongwon Kang4d0c0a82018-01-24 17:37:43 -0800161 auto free = mInternal->mBuffers.end();
162 for (auto it = mInternal->mBuffers.begin(); it != mInternal->mBuffers.end(); ++it) {
Andy Hungf59c0ba2016-06-15 17:59:30 -0700163 if ((*it)->refcount() == 0) {
164 const size_t size = (*it)->size();
165 if (size >= requestedSize) {
166 buffer = *it;
167 break;
168 }
169 if (size < smallest) {
170 smallest = size; // always free the smallest buf
171 free = it;
172 }
Wei Jiae9a5b962016-02-12 11:38:27 -0800173 }
Wei Jiae9a5b962016-02-12 11:38:27 -0800174 }
Andy Hungf59c0ba2016-06-15 17:59:30 -0700175 if (buffer == nullptr
Dongwon Kang4d0c0a82018-01-24 17:37:43 -0800176 && (free != mInternal->mBuffers.end()
177 || mInternal->mBuffers.size() < mInternal->mGrowthLimit)) {
Andy Hungf59c0ba2016-06-15 17:59:30 -0700178 // We alloc before we free so failure leaves group unchanged.
179 const size_t allocateSize = requestedSize < SIZE_MAX / 3 * 2 /* NB: ordering */ ?
180 requestedSize * 3 / 2 : requestedSize;
181 buffer = new MediaBuffer(allocateSize);
182 if (buffer->data() == nullptr) {
183 ALOGE("Allocation failure for size %zu", allocateSize);
184 delete buffer; // Invalid alloc, prefer not to call release.
185 buffer = nullptr;
186 } else {
187 buffer->setObserver(this);
Dongwon Kang4d0c0a82018-01-24 17:37:43 -0800188 if (free != mInternal->mBuffers.end()) {
Andy Hungf59c0ba2016-06-15 17:59:30 -0700189 ALOGV("reallocate buffer, requested size %zu vs available %zu",
190 requestedSize, (*free)->size());
191 (*free)->setObserver(nullptr);
192 (*free)->release();
193 *free = buffer; // in-place replace
194 } else {
195 ALOGV("allocate buffer, requested size %zu", requestedSize);
Dongwon Kang4d0c0a82018-01-24 17:37:43 -0800196 mInternal->mBuffers.emplace_back(buffer);
Andy Hungf59c0ba2016-06-15 17:59:30 -0700197 }
Wei Jiae9a5b962016-02-12 11:38:27 -0800198 }
Wei Jiae9a5b962016-02-12 11:38:27 -0800199 }
Andy Hungf59c0ba2016-06-15 17:59:30 -0700200 if (buffer != nullptr) {
Wei Jiae9a5b962016-02-12 11:38:27 -0800201 buffer->add_ref();
202 buffer->reset();
Wei Jiae9a5b962016-02-12 11:38:27 -0800203 *out = buffer;
Andy Hungf59c0ba2016-06-15 17:59:30 -0700204 return OK;
Wei Jiae9a5b962016-02-12 11:38:27 -0800205 }
Wei Jiae9a5b962016-02-12 11:38:27 -0800206 if (nonBlocking) {
Andy Hungf59c0ba2016-06-15 17:59:30 -0700207 *out = nullptr;
Wei Jiae9a5b962016-02-12 11:38:27 -0800208 return WOULD_BLOCK;
209 }
Andy Hungf59c0ba2016-06-15 17:59:30 -0700210 // All buffers are in use, block until one of them is returned.
Dongwon Kang4d0c0a82018-01-24 17:37:43 -0800211 mInternal->mCondition.wait(mInternal->mLock);
Wei Jiae9a5b962016-02-12 11:38:27 -0800212 }
Andy Hungf59c0ba2016-06-15 17:59:30 -0700213 // Never gets here.
Wei Jiae9a5b962016-02-12 11:38:27 -0800214}
215
Dongwon Kang4d0c0a82018-01-24 17:37:43 -0800216size_t MediaBufferGroup::buffers() const {
217 return mInternal->mBuffers.size();
218}
219
Dongwon Kang1889c3e2018-02-01 13:44:57 -0800220void MediaBufferGroup::signalBufferReturned(MediaBufferBase *) {
Dongwon Kang4d0c0a82018-01-24 17:37:43 -0800221 Mutex::Autolock autoLock(mInternal->mLock);
222 mInternal->mCondition.signal();
Wei Jiae9a5b962016-02-12 11:38:27 -0800223}
224
225} // namespace android