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 20 #include <media/stagefright/foundation/ADebug.h> 21 #include <media/stagefright/MediaBuffer.h> 22 #include <media/stagefright/MediaBufferGroup.h> 23 24 namespace android { 25 MediaBufferGroup()26MediaBufferGroup::MediaBufferGroup() 27 : mFirstBuffer(NULL), 28 mLastBuffer(NULL) { 29 } 30 ~MediaBufferGroup()31MediaBufferGroup::~MediaBufferGroup() { 32 MediaBuffer *next; 33 for (MediaBuffer *buffer = mFirstBuffer; buffer != NULL; 34 buffer = next) { 35 next = buffer->nextBuffer(); 36 37 CHECK_EQ(buffer->refcount(), 0); 38 39 buffer->setObserver(NULL); 40 buffer->release(); 41 } 42 } 43 add_buffer(MediaBuffer * buffer)44void MediaBufferGroup::add_buffer(MediaBuffer *buffer) { 45 Mutex::Autolock autoLock(mLock); 46 47 buffer->setObserver(this); 48 49 if (mLastBuffer) { 50 mLastBuffer->setNextBuffer(buffer); 51 } else { 52 mFirstBuffer = buffer; 53 } 54 55 mLastBuffer = buffer; 56 } 57 acquire_buffer(MediaBuffer ** out)58status_t MediaBufferGroup::acquire_buffer(MediaBuffer **out) { 59 Mutex::Autolock autoLock(mLock); 60 61 for (;;) { 62 for (MediaBuffer *buffer = mFirstBuffer; 63 buffer != NULL; buffer = buffer->nextBuffer()) { 64 if (buffer->refcount() == 0) { 65 buffer->add_ref(); 66 buffer->reset(); 67 68 *out = buffer; 69 goto exit; 70 } 71 } 72 73 // All buffers are in use. Block until one of them is returned to us. 74 mCondition.wait(mLock); 75 } 76 77 exit: 78 return OK; 79 } 80 signalBufferReturned(MediaBuffer *)81void MediaBufferGroup::signalBufferReturned(MediaBuffer *) { 82 Mutex::Autolock autoLock(mLock); 83 mCondition.signal(); 84 } 85 86 } // namespace android 87