1 /* ------------------------------------------------------------------
2 * Copyright (C) 1998-2009 PacketVideo
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
13 * express or implied.
14 * See the License for the specific language governing permissions
15 * and limitations under the License.
16 * -------------------------------------------------------------------
17 */
18 #include "pvmf_simple_media_buffer.h"
19
20 #include "oscl_mem.h"
21
22 // constants
23 const uint PVMF_SIMPLE_MEDIA_BUF_DEFAULT_SIZE = 200;
24
25
appendMediaFragment(OsclRefCounterMemFrag & memfrag)26 OSCL_EXPORT_REF bool PVMFSimpleMediaBuffer::appendMediaFragment(OsclRefCounterMemFrag& memfrag)
27 {
28 buffer.ptr = memfrag.getMemFragPtr();
29 buffer.len = memfrag.getMemFragSize();
30 return true;
31 }
32
clearMediaFragments()33 OSCL_EXPORT_REF bool PVMFSimpleMediaBuffer::clearMediaFragments()
34 {
35 buffer.ptr = NULL;
36 buffer.len = 0;
37 return true;
38 }
39
~PVMFSimpleMediaBuffer()40 OSCL_EXPORT_REF PVMFSimpleMediaBuffer::~PVMFSimpleMediaBuffer()
41 {
42 refcnt->removeRef();
43 }
44
getMarkerInfo()45 OSCL_EXPORT_REF uint32 PVMFSimpleMediaBuffer::getMarkerInfo()
46 {
47 return marker_info;
48 }
49
isRandomAccessPoint()50 OSCL_EXPORT_REF bool PVMFSimpleMediaBuffer::isRandomAccessPoint()
51 {
52 return random_access_point;
53 }
54
getErrorsFlag()55 OSCL_EXPORT_REF uint32 PVMFSimpleMediaBuffer::getErrorsFlag()
56 {
57 return errors_flag;
58 }
59
getNumFragments()60 OSCL_EXPORT_REF uint32 PVMFSimpleMediaBuffer::getNumFragments()
61 {
62 return 1;
63 }
64
getMediaFragment(uint32 index,OsclRefCounterMemFrag & memfrag)65 OSCL_EXPORT_REF bool PVMFSimpleMediaBuffer::getMediaFragment(uint32 index, OsclRefCounterMemFrag& memfrag)
66 {
67 if (index != 0)
68 {
69 return false;
70 }
71
72 memfrag = OsclRefCounterMemFrag(buffer, refcnt, capacity);
73
74 // explicitly addref
75 refcnt->addRef();
76
77 return true;
78
79 }
80
getMediaFragmentSize(uint32 index,uint32 & size)81 OSCL_EXPORT_REF bool PVMFSimpleMediaBuffer::getMediaFragmentSize(uint32 index, uint32& size)
82 {
83 size = 0;
84 if (index != 0)
85 {
86 return false;
87 }
88 size = buffer.len;
89 return true;
90 }
91
getFilledSize()92 OSCL_EXPORT_REF uint32 PVMFSimpleMediaBuffer::getFilledSize()
93 {
94 return buffer.len;
95 }
96
getCapacity()97 OSCL_EXPORT_REF uint32 PVMFSimpleMediaBuffer::getCapacity()
98 {
99 return capacity;
100 }
101
setCapacity(uint32 aCapacity)102 OSCL_EXPORT_REF void PVMFSimpleMediaBuffer::setCapacity(uint32 aCapacity)
103 {
104 capacity = aCapacity;
105 return;
106 }
107
setMediaFragFilledLen(uint32 index,uint32 len)108 OSCL_EXPORT_REF bool PVMFSimpleMediaBuffer::setMediaFragFilledLen(uint32 index, uint32 len)
109 {
110 if (index != 0)
111 {
112 return false;
113 }
114
115 if (len > capacity)
116 {
117 return false;
118 }
119
120 buffer.len = len;
121 return true;
122 }
123
setMarkerInfo(uint32 in_marker)124 OSCL_EXPORT_REF bool PVMFSimpleMediaBuffer::setMarkerInfo(uint32 in_marker)
125 {
126 marker_info = in_marker;
127 return true;
128 }
129
setRandomAccessPoint(bool flag)130 OSCL_EXPORT_REF bool PVMFSimpleMediaBuffer::setRandomAccessPoint(bool flag)
131 {
132 random_access_point = flag;
133 return true;
134 }
135
setErrorsFlag(uint32 flag)136 OSCL_EXPORT_REF bool PVMFSimpleMediaBuffer::setErrorsFlag(uint32 flag)
137 {
138 errors_flag = flag;
139 return true;
140 }
141
142
PVMFSimpleMediaBuffer(void * ptr,uint32 in_capacity,OsclRefCounter * my_refcnt)143 OSCL_EXPORT_REF PVMFSimpleMediaBuffer::PVMFSimpleMediaBuffer(void * ptr,
144 uint32 in_capacity,
145 OsclRefCounter *my_refcnt) :
146 marker_info(0), random_access_point(false), errors_flag(0)
147 {
148 buffer.ptr = ptr;
149 buffer.len = 0;
150 capacity = in_capacity;
151 refcnt = my_refcnt;
152 }
153
154 class SimpleMediaBufferCombinedCleanupSA : public OsclDestructDealloc
155 {
156 public:
~SimpleMediaBufferCombinedCleanupSA()157 virtual ~SimpleMediaBufferCombinedCleanupSA() {};
destruct_and_dealloc(OsclAny * ptr)158 virtual void destruct_and_dealloc(OsclAny* ptr)
159 {
160 // no need to call destructors in this case just dealloc
161 OsclMemAllocator alloc;
162 alloc.deallocate(ptr);
163 }
164 };
165
166 class SimpleMediaBufferCombinedCleanupDA : public OsclDestructDealloc
167 {
168 public:
SimpleMediaBufferCombinedCleanupDA(Oscl_DefAlloc * in_gen_alloc)169 SimpleMediaBufferCombinedCleanupDA(Oscl_DefAlloc* in_gen_alloc) :
170 gen_alloc(in_gen_alloc) {};
~SimpleMediaBufferCombinedCleanupDA()171 virtual ~SimpleMediaBufferCombinedCleanupDA() {};
172
destruct_and_dealloc(OsclAny * ptr)173 virtual void destruct_and_dealloc(OsclAny* ptr)
174 {
175 // no need to call destructors in this case just dealloc
176 gen_alloc->deallocate(ptr);
177 }
178
179 private:
180 Oscl_DefAlloc* gen_alloc;
181 };
182
183
184
allocate(uint32 requested_size)185 OSCL_EXPORT_REF OsclSharedPtr<PVMFMediaDataImpl> PVMFSimpleMediaBufferCombinedAlloc::allocate(uint32 requested_size)
186 {
187 OsclSharedPtr<PVMFMediaDataImpl> shared_media_data;
188 if (requested_size == 0)
189 {
190 requested_size = PVMF_SIMPLE_MEDIA_BUF_DEFAULT_SIZE;
191 }
192
193 uint aligned_refcnt_size;
194 uint aligned_class_size = oscl_mem_aligned_size(sizeof(PVMFSimpleMediaBuffer));
195 OsclRefCounter* my_refcnt;
196 uint8* my_ptr;
197
198 uint aligned_cleanup_size = oscl_mem_aligned_size(sizeof(SimpleMediaBufferCombinedCleanupDA));
199 aligned_refcnt_size = oscl_mem_aligned_size(sizeof(OsclRefCounterDA));
200 my_ptr = (uint8*) gen_alloc->ALLOCATE(aligned_refcnt_size + aligned_cleanup_size +
201 aligned_class_size + requested_size);
202
203 if (my_ptr == NULL)
204 {
205 //we assume that gen_alloc (Oscl_DefAlloc implementation) provided to this
206 //class does not throw an exception during the normal course of operation
207 //viz. running out of memory in a memory pool temporarily
208 return shared_media_data;
209 }
210 SimpleMediaBufferCombinedCleanupDA* cleanup_ptr = OSCL_PLACEMENT_NEW(my_ptr + aligned_refcnt_size, SimpleMediaBufferCombinedCleanupDA(gen_alloc));
211
212 // create the recounter after the cleanup object
213 my_refcnt = OSCL_PLACEMENT_NEW(my_ptr, OsclRefCounterDA(my_ptr, cleanup_ptr));
214 my_ptr += aligned_refcnt_size + aligned_cleanup_size;
215
216
217
218 void *ptr;
219 ptr = my_ptr + aligned_class_size;
220 PVMFMediaDataImpl* media_data_ptr = OSCL_PLACEMENT_NEW(my_ptr, PVMFSimpleMediaBuffer(ptr, requested_size, my_refcnt));
221 shared_media_data.Bind(media_data_ptr, my_refcnt);
222 return shared_media_data;
223 }
224
225