• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_BASE_H_
18 
19 #define MEDIA_BUFFER_BASE_H_
20 
21 #include <media/MediaExtractorPluginApi.h>
22 #include <media/stagefright/foundation/AMessage.h>
23 #include <media/NdkMediaFormatPriv.h>
24 
25 namespace android {
26 
27 class MediaBufferBase;
28 class MetaDataBase;
29 
30 class MediaBufferObserver {
31 public:
MediaBufferObserver()32     MediaBufferObserver() {}
~MediaBufferObserver()33     virtual ~MediaBufferObserver() {}
34 
35     virtual void signalBufferReturned(MediaBufferBase *buffer) = 0;
36 
37 private:
38     MediaBufferObserver(const MediaBufferObserver &);
39     MediaBufferObserver &operator=(const MediaBufferObserver &);
40 };
41 
42 class MediaBufferBase {
43 public:
44     static MediaBufferBase *Create(size_t size);
45 
46     // If MediaBufferGroup is set, decrement the local reference count;
47     // if the local reference count drops to 0, return the buffer to the
48     // associated MediaBufferGroup.
49     //
50     // If no MediaBufferGroup is set, the local reference count must be zero
51     // when called, whereupon the MediaBuffer is deleted.
52     virtual void release() = 0;
53 
54     // Increments the local reference count.
55     // Use only when MediaBufferGroup is set.
56     virtual void add_ref() = 0;
57 
58     virtual void *data() const = 0;
59     virtual size_t size() const = 0;
60 
61     virtual size_t range_offset() const = 0;
62     virtual size_t range_length() const = 0;
63 
64     virtual void set_range(size_t offset, size_t length) = 0;
65 
66     virtual MetaDataBase& meta_data() = 0;
67 
68     // Clears meta data and resets the range to the full extent.
69     virtual void reset() = 0;
70 
71     virtual void setObserver(MediaBufferObserver *group) = 0;
72 
73     virtual int refcount() const = 0;
74 
75     virtual int localRefcount() const = 0;
76     virtual int remoteRefcount() const = 0;
77 
~MediaBufferBase()78     virtual ~MediaBufferBase() {
79         delete mWrapper;
80         delete mFormat;
81     };
82 
wrap()83     CMediaBuffer *wrap() {
84         if (mWrapper) {
85             return mWrapper;
86         }
87         mWrapper = new CMediaBuffer;
88         mWrapper->handle = this;
89 
90         mWrapper->release = [](void *handle) -> void {
91             ((MediaBufferBase*)handle)->release();
92         };
93 
94         mWrapper->data = [](void *handle) -> void * {
95             return ((MediaBufferBase*)handle)->data();
96         };
97 
98         mWrapper->size = [](void *handle) -> size_t {
99             return ((MediaBufferBase*)handle)->size();
100         };
101 
102         mWrapper->range_offset = [](void *handle) -> size_t {
103             return ((MediaBufferBase*)handle)->range_offset();
104         };
105 
106         mWrapper->range_length = [](void *handle) -> size_t {
107             return ((MediaBufferBase*)handle)->range_length();
108         };
109 
110         mWrapper->set_range = [](void *handle, size_t offset, size_t length) -> void {
111             return ((MediaBufferBase*)handle)->set_range(offset, length);
112         };
113 
114         mWrapper->meta_data = [](void *handle) -> AMediaFormat* {
115             if (((MediaBufferBase*)handle)->mFormat == nullptr) {
116                 sp<AMessage> msg = new AMessage();
117                 ((MediaBufferBase*)handle)->mFormat = AMediaFormat_fromMsg(&msg);
118             }
119             return ((MediaBufferBase*)handle)->mFormat;
120         };
121 
122         return mWrapper;
123     }
124 protected:
MediaBufferBase()125     MediaBufferBase() {
126         mWrapper = nullptr;
127         mFormat = nullptr;
128     }
129 private:
130     CMediaBuffer *mWrapper;
131     AMediaFormat *mFormat;
132 };
133 
134 }  // namespace android
135 
136 #endif  // MEDIA_BUFFER_BASE_H_
137