sipxmedialib  Version 3.3
MpBuf.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2006-2012 SIPez LLC. All rights reserved.
3 //
4 //
5 // $$
7 
8 #ifndef _INCLUDED_MPBUF_H // [
9 #define _INCLUDED_MPBUF_H
10 
17 // SYSTEM INCLUDES
18 #include <stdlib.h>
19 #include <assert.h>
20 
21 // APPLICATION INCLUDES
22 #include "mp/MpBufPool.h"
23 
24 // DEFINES
25 // MACROS
26 // Uncomment MPBUF_DEBUG define to enable deep MpBuf debug with a lot of
27 // messages and more asserts.
28 //#define MPBUF_DEBUG
29 
30 // EXTERNAL FUNCTIONS
31 // EXTERNAL VARIABLES
32 // FORWARD DECLARATIONS
33 class MpFlowGraphBase;
34 
35 // CONSTANTS
36 // STRUCTS
37 
39 
45 typedef enum {
61 
62 // TYPEDEFS
63 
65 
74 struct MpBuf {
75  friend class MpBufPtr;
76  friend class MpBufPool;
77 
78 /* //////////////////////////// PUBLIC //////////////////////////////////// */
79 public:
80 /* ============================ CREATORS ================================== */
82 
83 
84 
86 
87 /* ============================ MANIPULATORS ============================== */
89 
90 
92  void attach();
93 
95  void detach();
96 
98 
99 /* ============================ ACCESSORS ================================= */
101 
102 
103  void setFlowGraph(MpFlowGraphBase* flowgraph)
104  {
105  mpFlowGraph = flowgraph;
106  };
107 
109  MP_BUFFERS_TREE getType() const {return mType;};
110 
112  MpBufPool *getBufferPool() const {return mpPool;};
113 
115 
116 /* ============================ INQUIRY =================================== */
118 
119 
121 
122 /* //////////////////////////// PROTECTED ///////////////////////////////// */
123 protected:
124 
129  void (*mpDestroy)(MpBuf*);
130  void (*mpInitClone)(MpBuf*);
132 
136  static void sInitClone(MpBuf *pBuffer);
137 
138 /* //////////////////////////// PRIVATE /////////////////////////////////// */
139 private:
140 
142  MpBuf(const MpBuf &);
147  MpBuf &operator=(const MpBuf &);
152 };
153 
155 
160 class MpBufPtr {
161  friend class MpBufPool;
162 
163 /* //////////////////////////// PUBLIC //////////////////////////////////// */
164 public:
165 
166 /* ============================ CREATORS ================================== */
168 
169 
172  : mpBuffer(NULL)
173  {};
174 
176 
179  MpBufPtr(MpBuf *buffer)
180  : mpBuffer(buffer)
181  {
182  if (mpBuffer != NULL) {
183  mpBuffer->mType = MP_BUF;
184  mpBuffer->mpDestroy = NULL;
185  mpBuffer->mpInitClone = MpBuf::sInitClone;
186  mpBuffer->attach();
187  }
188 #ifdef _DEBUG
189  else {
190  osPrintf("mpBuffer == NULL!\n");
191  }
192 #endif
193  };
194 
197  {
198  if (mpBuffer != NULL)
199  mpBuffer->detach();
200  };
201 
203  MpBufPtr(const MpBufPtr &buffer)
204  : mpBuffer(buffer.mpBuffer)
205  {
206  if (mpBuffer != NULL)
207  mpBuffer->attach();
208  }
209 
210  MpBufPtr clone() const
211  {
212  MpBufPtr clone;
213 
214  // Return invalid pointer as a copy of invalid pointer.
215  if (!isValid())
216  return clone;
217 
218  // Get fresh buffer
219  clone.mpBuffer = mpBuffer->getBufferPool()->getBuffer();
220  if (!clone.isValid())
221  return clone;
222 
223  // Copy raw buffer's content to new location
224  memcpy(clone.mpBuffer, mpBuffer, mpBuffer->getBufferPool()->getBlockSize());
225 
226  // Init clone
227  clone->mpInitClone(clone.mpBuffer);
228 
229  return clone;
230  }
231 
233 
234 /* ============================ MANIPULATORS ============================== */
236 
237 
239 
242  MpBufPtr &operator=(const MpBufPtr &bufferPtr)
243  {
244  // Check for a=a case;
245  if (&bufferPtr == this) {
246  return *this;
247  }
248 
249  if (mpBuffer != NULL)
250  mpBuffer->detach();
251  mpBuffer = bufferPtr.mpBuffer;
252  if (mpBuffer != NULL)
253  mpBuffer->attach();
254 
255  return *this;
256  }
257 
259 
262  bool operator==(const MpBufPtr &pBuffer)
263  {
264  return (mpBuffer == pBuffer.mpBuffer);
265  }
266 
268 
271  bool operator!=(const MpBufPtr &pBuffer)
272  {
273  return (mpBuffer != pBuffer.mpBuffer);
274  }
275 
277 
280  void release()
281  {
282  if (mpBuffer != NULL)
283  mpBuffer->detach();
284  mpBuffer = NULL;
285  }
286 
288 
293  void swap(MpBufPtr &pBuffer)
294  {
295  MpBuf *temp = pBuffer.mpBuffer;
296  pBuffer.mpBuffer = mpBuffer;
297  mpBuffer = temp;
298  }
299 
301 
307  {
308  // We already writable?
309  if (isWritable())
310  return true;
311 
312  // Cannot make buffer writable...
313  if (mpBuffer == NULL)
314  return false;
315 
316  // Create the clone and own it.
317  MpBufPtr pBuf = clone();
318  swap(pBuf);
319 
320  return true;
321  }
322 
324 
325 /* ============================ ACCESSORS ================================= */
327 
328 
330  int getBufferNumber() const
331  {
332  if (mpBuffer == NULL)
333  return -1;
334  else
335  return mpBuffer->mpPool->getBufferNumber(mpBuffer);
336  };
337 
338  void setFlowGraph(MpFlowGraphBase* flowgraph)
339  {
340  if(mpBuffer != NULL)
341  {
342  mpBuffer->setFlowGraph(flowgraph);
343  }
344  };
345 
347  MpBuf *operator->() {assert(mpBuffer!=NULL); return mpBuffer;};
348 
350  const MpBuf *operator->() const {assert(mpBuffer!=NULL); return mpBuffer;};
351 
353  {
354  if(mpBuffer) return(mpBuffer->getType());
355  else return((MP_BUFFERS_TREE)-1);
356  };
358 
359 /* ============================ INQUIRY =================================== */
361 
362 
365  bool isValid() const {return mpBuffer != NULL;};
366 
368 
371  bool isWritable() {return (mpBuffer != NULL) && (mpBuffer->mRefCounter == 1);};
372 
374 
375 /* //////////////////////////// PROTECTED ///////////////////////////////// */
376 protected:
377  friend struct MpDataBuf;
378 
380 
381 /* //////////////////////////// PRIVATE /////////////////////////////////// */
382 private:
383 
384 };
385 
387 #define MPBUF_DEFAULT_CONSTRUCTOR(classname) \
388  classname##Ptr() {};
389 
390 #define MPBUF_FROM_BASE_CONSTRUCTOR_INIT(classname, buffer_type) \
391  classname *pBuffer = (classname*)mpBuffer; \
392  assert(pBuffer->mpPool->getBlockSize() >= sizeof(classname)); \
393  pBuffer->mType = buffer_type; \
394  pBuffer->init(); \
395 
396 #define MPBUF_FROM_BASE_CONSTRUCTOR(classname, buffer_type, base_classname) \
398  classname##Ptr(MpBuf *buffer) \
399  : base_classname##Ptr(buffer) \
400  { \
401  if (mpBuffer != NULL) { \
402  MPBUF_FROM_BASE_CONSTRUCTOR_INIT(classname, buffer_type) \
403  } \
404  };
405 
407 #define MPBUF_TYPECHECKED_COPY(classname, buffer_type, base_classname) \
408  classname##Ptr(const MpBufPtr &buffer) \
409  : base_classname##Ptr(buffer) \
410  { \
411  assert( (!buffer.isValid()) \
412  || ( buffer->getType() >= buffer_type \
413  && buffer->getType() < buffer_type##_END)); \
414  };
415 
417 #define MPBUF_MEMBER_ACCESS_OPERATOR(classname) \
418  classname *operator->() \
419  {assert(mpBuffer!=NULL); return (classname*)mpBuffer;};
420 
422 #define MPBUF_CONST_MEMBER_ACCESS_OPERATOR(classname) \
423  const classname *operator->() const \
424  {assert(mpBuffer!=NULL); return (classname*)mpBuffer;};
425 
426 #endif // _INCLUDED_MPBUF_H ]
427 
MpBufPtr(MpBuf *buffer)
This constructor owns MpBuf object.
Definition: MpBuf.h:179
Begin of the MpArrayBuf type.
Definition: MpBuf.h:47
MpBuf & operator=(const MpBuf &)
Disable assignment operator.
End of the MpAudioBuf type.
Definition: MpBuf.h:51
unsigned getBlockSize() const
Return size of the one block in the pool (in bytes).
Definition: MpBufPool.h:69
MpBuf * mpBuffer
Pointer to real buffer.
Definition: MpBuf.h:379
End of the MpDataBuf type.
Definition: MpBuf.h:58
void setFlowGraph(MpFlowGraphBase *flowgraph)
Definition: MpBuf.h:338
MP_BUFFERS_TREE getType() const
Definition: MpBuf.h:352
int getBufferNumber() const
Return number of the buffer in the pool. Use this for debug output.
Definition: MpBuf.h:330
MpFlowGraphBase * mpFlowGraph
Debug pointer to flowgraph in which this buf is used.
Definition: MpBuf.h:128
MpBufPtr clone() const
Definition: MpBuf.h:210
MpBufPtr & operator=(const MpBufPtr &bufferPtr)
Smart assignment.
Definition: MpBuf.h:242
Flow graph for coordinating the execution of media processing resources.
Definition: MpFlowGraphBase.h:91
static void sInitClone(MpBuf *pBuffer)
Function that initialize buffer after cloning. It adjusts reference counters.
Definition: MpBuf.cpp:64
~MpBufPtr()
Destructor. It decrements buffer&#39;s reference counter.
Definition: MpBuf.h:196
MpBuf * operator->()
Return pointer to MpBuf.
Definition: MpBuf.h:347
bool operator!=(const MpBufPtr &pBuffer)
Compare two smart pointers.
Definition: MpBuf.h:271
void swap(MpBufPtr &pBuffer)
Swap to buffers.
Definition: MpBuf.h:293
MpBufPtr(const MpBufPtr &buffer)
Copy buffer pointer and increment its reference counter.
Definition: MpBuf.h:203
void(* mpInitClone)(MpBuf *)
Definition: MpBuf.h:131
void setFlowGraph(MpFlowGraphBase *flowgraph)
Definition: MpBuf.h:103
MpBuf * getBuffer()
Get free block from pool.
Definition: MpBufPool.cpp:150
const MpBuf * operator->() const
Return readonly pointer to MpBuf.
Definition: MpBuf.h:350
Begin of the MpDataBuf type.
Definition: MpBuf.h:49
MpBufPool * getBufferPool() const
Get parent pool of this buffer.
Definition: MpBuf.h:112
void detach()
Decrements reference counter and free buffer if needed.
Definition: MpBuf.cpp:34
bool requestWrite()
Check if buffer is writable and create copy if no.
Definition: MpBuf.h:306
End of the MpArrayBuf type.
Definition: MpBuf.h:48
int mRefCounter
Reference counter for use with MpBufPtr.
Definition: MpBuf.h:126
void attach()
Increments reference counter.
Definition: MpBuf.cpp:23
MpBufPool * mpPool
Parent memory pool.
Definition: MpBuf.h:127
MP_BUFFERS_TREE getType() const
Get buffer type.
Definition: MpBuf.h:109
bool operator==(const MpBufPtr &pBuffer)
Compare two smart pointers.
Definition: MpBuf.h:262
Smart pointer to MpBuf.
Definition: MpBuf.h:160
End of the MpBuf type.
Definition: MpBuf.h:59
End of the MpUdpBuf type.
Definition: MpBuf.h:57
MP_BUFFERS_TREE mType
Buffer class type. Used for type safety.
Definition: MpBuf.h:125
void release()
Release buffer we are pointing to.
Definition: MpBuf.h:280
void(* mpDestroy)(MpBuf *)
Definition: MpBuf.h:129
Base class for all media buffers.
Definition: MpBuf.h:74
bool isValid() const
Can this pointer be dereferenced? Use this function instead of NULL comparison.
Definition: MpBuf.h:365
Begin of the MpBuf type.
Definition: MpBuf.h:46
End of the MpVideoBuf type.
Definition: MpBuf.h:53
Begin of the MpAudioBuf type.
Definition: MpBuf.h:50
Begin of the MpUdpBuf type.
Definition: MpBuf.h:54
Begin of the MpVideoBuf type.
Definition: MpBuf.h:52
End of the MpRtpBuf type.
Definition: MpBuf.h:56
MP_BUFFERS_TREE
Enum used for runtime type checks.
Definition: MpBuf.h:45
MpBuf(const MpBuf &)
Disable copy (and other) constructor.
Pool of buffers.
Definition: MpBufPool.h:32
Begin of the MpRtpBuf type.
Definition: MpBuf.h:55
MpBufPtr()
Default constructor - construct invalid pointer.
Definition: MpBuf.h:171
Stores data in the external buffer.
Definition: MpDataBuf.h:42