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 /* ------------------------------------------------------------------- */
20 /* MPEG-4 Util: MediaBufferManager Class */
21 /* ------------------------------------------------------------------- */
22 /*********************************************************************************/
23 /*
24 This MediaBufferManager Class is responsible for maintaining list of used
25 and free MediaBuffers. This is promarily used by the Mpeg4File class when
26 it is retrieving media data from disk. It needs buffers to store data and
27 to pass to the consumers of the media data - namesly PVServer and PVPlayer.
28 */
29
30
31 #define IMPLEMENT_MediaBufferManager
32
33 #include "mediabuffermanager.h"
34
35 typedef Oscl_Vector<MediaBuffer*, OsclMemAllocator> mediaBufferVecType;
36
37 // Constructor
MediaBufferManager(int32 bufSize,uint32 numBufs)38 MediaBufferManager::MediaBufferManager(int32 bufSize, uint32 numBufs)
39 {
40 _bufferSize = bufSize;
41
42 // Create 'numBufs' initial buffers each of size 'bufSize'
43
44 PV_MP4_FF_NEW(fp->auditCB, mediaBufferVecType, (), _pfreeBufVec);
45 PV_MP4_FF_NEW(fp->auditCB, mediaBufferVecType, (), _pusedBufVec);
46
47 // Allocate free buffers
48 for (uint32 i = 0; i < numBufs; i++)
49 {
50 MediaBuffer *mb = NULL;
51 PV_MP4_FF_NEW(fp->auditCB, MediaBuffer, (bufSize), mb);
52 mb->setOwner(this);
53 (*_pfreeBufVec).push_back(mb);
54 }
55 }
56
~MediaBufferManager()57 MediaBufferManager::~MediaBufferManager()
58 {
59 uint32 i;
60
61 // Clean up the vectors of buffers
62 for (i = 0; i < _pfreeBufVec->size(); i++)
63 {
64 PV_MP4_FF_DELETE(NULL, MediaBuffer, (*_pfreeBufVec)[i]);
65 }
66 PV_MP4_FF_TEMPLATED_DELETE(NULL, mediaBufferVecType, Oscl_Vector, _pfreeBufVec);
67
68 for (i = 0; i < _pusedBufVec->size(); i++)
69 {
70 PV_MP4_FF_DELETE(NULL, MediaBuffer, (*_pusedBufVec)[i]);
71 }
72 PV_MP4_FF_TEMPLATED_DELETE(NULL, mediaBufferVecType, Oscl_Vector, _pusedBufVec);
73 }
74
75 // Called by buffer when it is freed
76 void
freeBuf(MediaBuffer * buf)77 MediaBufferManager::freeBuf(MediaBuffer *buf)
78 {
79 // Need to find buffer in _pusedBufVec
80
81 int32 i = 0;
82 MediaBuffer **x;
83 for (x = _pusedBufVec->begin(); x < _pusedBufVec->end(); x++)
84 {
85 MediaBuffer *used = (MediaBuffer*)(*_pusedBufVec)[i];
86 if (used == buf)
87 {
88 break; // Found buffer in used vec
89 }
90 i++;
91 }
92
93 (*_pfreeBufVec).push_back(buf); // buffer to free vec
94 _pusedBufVec->erase(x); // Delete buffer from used vec
95 }
96
97
98 MediaBuffer*
getBuf()99 MediaBufferManager::getBuf()
100 {
101 if (_pfreeBufVec->size() != 0)
102 {
103 MediaBuffer *mb = (MediaBuffer *)(*_pfreeBufVec)[_pfreeBufVec->size()-1]; // get last buf
104 _pfreeBufVec->pop_back();
105 (*_pusedBufVec).push_back(mb);
106 return mb;
107 }
108 else // No free buffers - need to create one
109 {
110 MediaBuffer *mb = newBuf();
111 (*_pusedBufVec).push_back(mb);
112 return mb;
113 }
114 }
115
116 // By default create a 32KB buffer - if want another size buffer, need to set
117 // the bufSize member for this class
118 MediaBuffer*
newBuf()119 MediaBufferManager::newBuf()
120 {
121 MediaBuffer *mb = NULL;
122 PV_MP4_FF_NEW(fp->auditCB, MediaBuffer, (_bufferSize), mb);
123 return mb;
124 }
125
126