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 #ifndef MEDIA_BUFFER_H_ 18 19 #define MEDIA_BUFFER_H_ 20 21 #include <pthread.h> 22 23 #include <utils/Errors.h> 24 #include <utils/RefBase.h> 25 26 namespace android { 27 28 struct ABuffer; 29 class GraphicBuffer; 30 class MediaBuffer; 31 class MediaBufferObserver; 32 class MetaData; 33 34 class MediaBufferObserver { 35 public: MediaBufferObserver()36 MediaBufferObserver() {} ~MediaBufferObserver()37 virtual ~MediaBufferObserver() {} 38 39 virtual void signalBufferReturned(MediaBuffer *buffer) = 0; 40 41 private: 42 MediaBufferObserver(const MediaBufferObserver &); 43 MediaBufferObserver &operator=(const MediaBufferObserver &); 44 }; 45 46 class MediaBuffer { 47 public: 48 // The underlying data remains the responsibility of the caller! 49 MediaBuffer(void *data, size_t size); 50 51 MediaBuffer(size_t size); 52 53 MediaBuffer(const sp<GraphicBuffer>& graphicBuffer); 54 55 MediaBuffer(const sp<ABuffer> &buffer); 56 57 // Decrements the reference count and returns the buffer to its 58 // associated MediaBufferGroup if the reference count drops to 0. 59 void release(); 60 61 // Increments the reference count. 62 void add_ref(); 63 64 void *data() const; 65 size_t size() const; 66 67 size_t range_offset() const; 68 size_t range_length() const; 69 70 void set_range(size_t offset, size_t length); 71 72 sp<GraphicBuffer> graphicBuffer() const; 73 74 sp<MetaData> meta_data(); 75 76 // Clears meta data and resets the range to the full extent. 77 void reset(); 78 79 void setObserver(MediaBufferObserver *group); 80 81 // Returns a clone of this MediaBuffer increasing its reference count. 82 // The clone references the same data but has its own range and 83 // MetaData. 84 MediaBuffer *clone(); 85 86 int refcount() const; 87 88 protected: 89 virtual ~MediaBuffer(); 90 91 private: 92 friend class MediaBufferGroup; 93 friend class OMXDecoder; 94 95 // For use by OMXDecoder, reference count must be 1, drop reference 96 // count to 0 without signalling the observer. 97 void claim(); 98 99 MediaBufferObserver *mObserver; 100 MediaBuffer *mNextBuffer; 101 int mRefCount; 102 103 void *mData; 104 size_t mSize, mRangeOffset, mRangeLength; 105 sp<GraphicBuffer> mGraphicBuffer; 106 sp<ABuffer> mBuffer; 107 108 bool mOwnsData; 109 110 sp<MetaData> mMetaData; 111 112 MediaBuffer *mOriginal; 113 114 void setNextBuffer(MediaBuffer *buffer); 115 MediaBuffer *nextBuffer(); 116 117 MediaBuffer(const MediaBuffer &); 118 MediaBuffer &operator=(const MediaBuffer &); 119 }; 120 121 } // namespace android 122 123 #endif // MEDIA_BUFFER_H_ 124