• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
19 #include "pvmf_media_data.h"
20 #include "pvmf_media_msg_header.h"
21 #include "pvmf_media_data_impl.h"
22 #include "oscl_mem.h"
23 
24 
getTimestamp()25 OSCL_EXPORT_REF PVMFTimestamp PVMFMediaData::getTimestamp()
26 {
27     return hdr_ptr->timestamp;
28 }
29 
getDuration()30 OSCL_EXPORT_REF uint32 PVMFMediaData::getDuration()
31 {
32     return hdr_ptr->duration;
33 }
34 
getStreamID()35 OSCL_EXPORT_REF uint32 PVMFMediaData::getStreamID()
36 {
37     return hdr_ptr->stream_id;
38 }
39 
getSeqNum()40 OSCL_EXPORT_REF uint32 PVMFMediaData::getSeqNum()
41 {
42     return hdr_ptr->seqnum;
43 }
44 
getFormatID()45 OSCL_EXPORT_REF PVUid32 PVMFMediaData::getFormatID()
46 {
47     return hdr_ptr->format_id;
48 }
49 
queryInterface(const PVUuid & uuid,PVInterface * & iface)50 OSCL_EXPORT_REF bool PVMFMediaData::queryInterface(const PVUuid& uuid, PVInterface*& iface)
51 {
52     OSCL_UNUSED_ARG(uuid);
53     iface = NULL;
54     return false;
55 }
56 
getFormatSpecificInfo(OsclRefCounterMemFrag & memfrag)57 OSCL_EXPORT_REF bool PVMFMediaData::getFormatSpecificInfo(OsclRefCounterMemFrag& memfrag)
58 {
59     memfrag = hdr_ptr->format_spec_info;
60     return true;
61 }
62 
setFormatSpecificInfo(OsclRefCounterMemFrag & memfrag)63 OSCL_EXPORT_REF void PVMFMediaData::setFormatSpecificInfo(OsclRefCounterMemFrag& memfrag)
64 {
65     hdr_ptr->format_spec_info = memfrag;
66 }
67 
setMediaFragFilledLen(uint32 index,uint32 len)68 OSCL_EXPORT_REF bool PVMFMediaData::setMediaFragFilledLen(uint32 index, uint32 len)
69 {
70     return impl_ptr->setMediaFragFilledLen(index, len);
71 }
72 
setTimestamp(const PVMFTimestamp & ts)73 OSCL_EXPORT_REF void PVMFMediaData::setTimestamp(const PVMFTimestamp& ts)
74 {
75     hdr_ptr->timestamp = ts;
76 }
77 
setDuration(const uint32 & duration)78 OSCL_EXPORT_REF void PVMFMediaData::setDuration(const uint32& duration)
79 {
80     hdr_ptr->duration = duration;
81 }
82 
setSeqNum(uint32 seq)83 OSCL_EXPORT_REF void PVMFMediaData::setSeqNum(uint32 seq)
84 {
85     hdr_ptr->seqnum = seq;
86 }
87 
setStreamID(uint32 id)88 OSCL_EXPORT_REF void PVMFMediaData::setStreamID(uint32 id)
89 {
90     hdr_ptr->stream_id = id;
91 }
92 
getMarkerInfo()93 OSCL_EXPORT_REF uint32 PVMFMediaData::getMarkerInfo()
94 {
95     return impl_ptr->getMarkerInfo();
96 }
97 
setMarkerInfo(uint32 aMarker)98 OSCL_EXPORT_REF bool PVMFMediaData::setMarkerInfo(uint32 aMarker)
99 {
100     return impl_ptr->setMarkerInfo(aMarker);
101 }
102 
getErrorsFlag()103 OSCL_EXPORT_REF uint32  PVMFMediaData::getErrorsFlag()
104 {
105     return impl_ptr->getErrorsFlag();
106 }
107 
getNumFragments()108 OSCL_EXPORT_REF uint32 PVMFMediaData::getNumFragments()
109 {
110     return impl_ptr->getNumFragments();
111 }
112 
getMediaFragment(uint32 index,OsclRefCounterMemFrag & memfrag)113 OSCL_EXPORT_REF bool PVMFMediaData::getMediaFragment(uint32 index, OsclRefCounterMemFrag& memfrag)
114 {
115     return impl_ptr->getMediaFragment(index, memfrag);
116 }
117 
getMediaDataImpl(OsclSharedPtr<PVMFMediaDataImpl> & media_data_impl)118 OSCL_EXPORT_REF bool PVMFMediaData::getMediaDataImpl(OsclSharedPtr<PVMFMediaDataImpl>&
119         media_data_impl)
120 {
121     media_data_impl = impl_ptr;
122     return true;
123 }
124 
125 
getFilledSize()126 OSCL_EXPORT_REF uint32 PVMFMediaData::getFilledSize()
127 {
128     return impl_ptr->getFilledSize();
129 }
130 
getCapacity()131 OSCL_EXPORT_REF uint32 PVMFMediaData::getCapacity()
132 {
133     return impl_ptr->getCapacity();
134 }
135 
getMessageHeader()136 OSCL_EXPORT_REF const PVMFMediaMsgHeader* PVMFMediaData::getMessageHeader()
137 {
138     return hdr_ptr;
139 }
140 
141 
142 
143 
144 class MediaDataCleanupSA : public OsclDestructDealloc
145 {
146     public:
~MediaDataCleanupSA()147         virtual ~MediaDataCleanupSA() {};
destruct_and_dealloc(OsclAny * ptr)148         virtual void destruct_and_dealloc(OsclAny* ptr)
149         {
150             uint8* tmp_ptr = (uint8*) ptr;
151             uint aligned_refcnt_size = oscl_mem_aligned_size(sizeof(OsclRefCounterSA<MediaDataCleanupSA>));
152             tmp_ptr += aligned_refcnt_size;
153             PVMFMediaData* mdata_ptr = reinterpret_cast<PVMFMediaData*>(tmp_ptr);
154             mdata_ptr->~PVMFMediaData();
155             OsclMemAllocator alloc;
156             alloc.deallocate(ptr);
157         }
158 };
159 
160 class MediaDataCleanupDA : public OsclDestructDealloc
161 {
162     public:
MediaDataCleanupDA(Oscl_DefAlloc * in_gen_alloc)163         MediaDataCleanupDA(Oscl_DefAlloc* in_gen_alloc) : gen_alloc(in_gen_alloc) {};
~MediaDataCleanupDA()164         virtual ~MediaDataCleanupDA() {};
165 
destruct_and_dealloc(OsclAny * ptr)166         virtual void destruct_and_dealloc(OsclAny* ptr)
167         {
168             uint8* tmp_ptr = (uint8*) ptr;
169             uint aligned_size = oscl_mem_aligned_size(sizeof(OsclRefCounterDA));
170             // skip the refcounter
171             tmp_ptr += aligned_size;
172             // skip the MediaData Cleanup
173             aligned_size = oscl_mem_aligned_size(sizeof(MediaDataCleanupDA));
174             tmp_ptr += aligned_size;
175 
176             PVMFMediaData* mdata_ptr = reinterpret_cast<PVMFMediaData*>(tmp_ptr);
177             mdata_ptr->~PVMFMediaData();
178             gen_alloc->deallocate(ptr);
179         }
180 
181     private:
182         Oscl_DefAlloc* gen_alloc;
183 };
184 
185 
186 OSCL_EXPORT_REF OsclSharedPtr<PVMFMediaData>
createMediaData(OsclSharedPtr<PVMFMediaDataImpl> & in_impl_ptr,Oscl_DefAlloc * gen_alloc)187 PVMFMediaData::createMediaData(OsclSharedPtr<PVMFMediaDataImpl>& in_impl_ptr,
188                                Oscl_DefAlloc* gen_alloc)
189 {
190     OsclSharedPtr<PVMFMediaData> shared_media_data;
191 
192     // allocate enough room
193     uint8* my_ptr;
194     OsclRefCounter* my_refcnt;
195 
196     uint aligned_media_data_size = oscl_mem_aligned_size(sizeof(PVMFMediaData));
197 
198     // must compute the aligned size for PVMFMediaData.
199     if (gen_alloc)
200     {
201         uint aligned_refcnt_size = oscl_mem_aligned_size(sizeof(OsclRefCounterDA));
202         uint aligned_cleanup_size = oscl_mem_aligned_size(sizeof(MediaDataCleanupDA));
203         my_ptr = (uint8*) gen_alloc->ALLOCATE(aligned_refcnt_size +
204                                               aligned_cleanup_size +
205                                               aligned_media_data_size +
206                                               sizeof(PVMFMediaMsgHeader));
207 
208         //not allocators leave, so check for NULL ptr
209         if (my_ptr == NULL) return shared_media_data;
210 
211         MediaDataCleanupDA *my_cleanup = OSCL_PLACEMENT_NEW(my_ptr + aligned_refcnt_size, MediaDataCleanupDA(gen_alloc));
212         my_refcnt = OSCL_PLACEMENT_NEW(my_ptr, OsclRefCounterDA(my_ptr, my_cleanup));
213         my_ptr += aligned_refcnt_size + aligned_cleanup_size;
214 
215     }
216     else
217     {
218         OsclMemAllocator my_alloc;
219         uint aligned_refcnt_size = oscl_mem_aligned_size(sizeof(OsclRefCounterSA<MediaDataCleanupSA>));
220         my_ptr = (uint8*) my_alloc.ALLOCATE(aligned_refcnt_size +
221                                             aligned_media_data_size + sizeof(PVMFMediaMsgHeader));
222         my_refcnt = OSCL_PLACEMENT_NEW(my_ptr, OsclRefCounterSA<MediaDataCleanupSA>(my_ptr));
223         my_ptr += aligned_refcnt_size;
224     }
225 
226     PVMFMediaData *media_data_ptr = OSCL_PLACEMENT_NEW(my_ptr, PVMFMediaData());
227 
228     media_data_ptr->hdr_ptr = OSCL_PLACEMENT_NEW(my_ptr + aligned_media_data_size, PVMFMediaMsgHeader());
229 
230     media_data_ptr->impl_ptr = in_impl_ptr;
231 
232     shared_media_data.Bind(media_data_ptr, my_refcnt);
233 
234     return shared_media_data;
235 }
236 
237 OSCL_EXPORT_REF OsclSharedPtr<PVMFMediaData>
createMediaData(OsclSharedPtr<PVMFMediaDataImpl> & in_impl_ptr,const PVMFMediaMsgHeader * msgHeader,Oscl_DefAlloc * gen_alloc)238 PVMFMediaData::createMediaData(OsclSharedPtr<PVMFMediaDataImpl>& in_impl_ptr,
239                                const PVMFMediaMsgHeader* msgHeader,
240                                Oscl_DefAlloc* gen_alloc)
241 {
242     OsclSharedPtr<PVMFMediaData> shared_media_data;
243 
244     // allocate enough room
245     uint8* my_ptr;
246     OsclRefCounter* my_refcnt;
247 
248     uint aligned_media_data_size = oscl_mem_aligned_size(sizeof(PVMFMediaData));
249 
250     // must compute the aligned size for PVMFMediaData.
251     if (gen_alloc)
252     {
253         uint aligned_refcnt_size = oscl_mem_aligned_size(sizeof(OsclRefCounterDA));
254         uint aligned_cleanup_size = oscl_mem_aligned_size(sizeof(MediaDataCleanupDA));
255         my_ptr = (uint8*) gen_alloc->ALLOCATE(aligned_refcnt_size +
256                                               aligned_cleanup_size +
257                                               aligned_media_data_size +
258                                               sizeof(PVMFMediaMsgHeader));
259 
260         //not allocators leave, so check for NULL ptr
261         if (my_ptr == NULL) return shared_media_data;
262 
263         MediaDataCleanupDA *my_cleanup = OSCL_PLACEMENT_NEW(my_ptr + aligned_refcnt_size, MediaDataCleanupDA(gen_alloc));
264         my_refcnt = OSCL_PLACEMENT_NEW(my_ptr, OsclRefCounterDA(my_ptr, my_cleanup));
265         my_ptr += aligned_refcnt_size + aligned_cleanup_size;
266 
267     }
268     else
269     {
270         OsclMemAllocator my_alloc;
271         uint aligned_refcnt_size = oscl_mem_aligned_size(sizeof(OsclRefCounterSA<MediaDataCleanupSA>));
272         my_ptr = (uint8*) my_alloc.ALLOCATE(aligned_refcnt_size +
273                                             aligned_media_data_size + sizeof(PVMFMediaMsgHeader));
274         my_refcnt = OSCL_PLACEMENT_NEW(my_ptr, OsclRefCounterSA<MediaDataCleanupSA>(my_ptr));
275         my_ptr += aligned_refcnt_size;
276     }
277 
278     PVMFMediaData *media_data_ptr = OSCL_PLACEMENT_NEW(my_ptr, PVMFMediaData());
279 
280     media_data_ptr->hdr_ptr = OSCL_PLACEMENT_NEW(my_ptr + aligned_media_data_size, PVMFMediaMsgHeader());
281 
282     media_data_ptr->hdr_ptr->timestamp = msgHeader->timestamp;
283     media_data_ptr->hdr_ptr->duration = msgHeader->duration;
284     media_data_ptr->hdr_ptr->stream_id = msgHeader->stream_id;
285     media_data_ptr->hdr_ptr->seqnum = msgHeader->seqnum;
286     media_data_ptr->hdr_ptr->format_id = msgHeader->format_id;
287     media_data_ptr->hdr_ptr->format_spec_info = msgHeader->format_spec_info;
288 
289     media_data_ptr->impl_ptr = in_impl_ptr;
290 
291     shared_media_data.Bind(media_data_ptr, my_refcnt);
292 
293     return shared_media_data;
294 }
295 
296 
~PVMFMediaData()297 OSCL_EXPORT_REF PVMFMediaData::~PVMFMediaData()
298 {
299 
300     // call the destructors
301     hdr_ptr->~PVMFMediaMsgHeader();
302 
303     // the destructor of the impl_ptr will be called by default.
304 
305 }
306 
convertToPVMFMediaMsg(PVMFSharedMediaMsgPtr & dest,PVMFSharedMediaDataPtr & source)307 OSCL_EXPORT_REF void convertToPVMFMediaMsg(PVMFSharedMediaMsgPtr& dest, PVMFSharedMediaDataPtr& source)
308 {
309     OsclRefCounter* refcnt = source.GetRefCounter();
310     /* explicitly increment refcnt since Bind doesn't do it when refcnt and pointer are
311      * added separately
312      */
313     refcnt->addRef();
314     dest.Bind(source.GetRep(), refcnt);
315 }
316 
convertToPVMFMediaData(PVMFSharedMediaDataPtr & dest,PVMFSharedMediaMsgPtr & source)317 OSCL_EXPORT_REF void convertToPVMFMediaData(PVMFSharedMediaDataPtr& dest, PVMFSharedMediaMsgPtr& source)
318 {
319     OsclRefCounter* refcnt = source.GetRefCounter();
320     /* explicitly increment refcnt since Bind doesn't do it when refcnt and pointer are
321      * added separately
322      */
323     refcnt->addRef();
324     dest.Bind(reinterpret_cast<PVMFMediaData*>(source.GetRep()), refcnt);
325 }
326 
327 
328