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