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