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