EMIPLIB

mipstreambuffer.h

Go to the documentation of this file.
00001 /*
00002     
00003   This file is a part of EMIPLIB, the EDM Media over IP Library.
00004   
00005   Copyright (C) 2006-2011  Hasselt University - Expertise Centre for
00006                       Digital Media (EDM) (http://www.edm.uhasselt.be)
00007 
00008   This library is free software; you can redistribute it and/or
00009   modify it under the terms of the GNU Lesser General Public
00010   License as published by the Free Software Foundation; either
00011   version 2.1 of the License, or (at your option) any later version.
00012 
00013   This library is distributed in the hope that it will be useful,
00014   but WITHOUT ANY WARRANTY; without even the implied warranty of
00015   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016   Lesser General Public License for more details.
00017 
00018   You should have received a copy of the GNU Lesser General Public
00019   License along with this library; if not, write to the Free Software
00020   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  
00021   USA
00022 
00023 */
00024 
00029 #ifndef MIPSTREAMBUFFER_H
00030 
00031 #define MIPSTREAMBUFFER_H
00032 
00033 #include "mipconfig.h"
00034 #include "miptypes.h"
00035 #include <jthread/jmutex.h>
00036 #include <list>
00037 #include <vector>
00038 
00043 class EMIPLIB_IMPORTEXPORT MIPStreamBuffer
00044 {
00045 public:
00053         MIPStreamBuffer(int blockSize, int preAllocBlocks = 0);
00054         ~MIPStreamBuffer();
00055 
00057         void clear();
00058 
00060         void write(const void *pData, int amount);
00061 
00063         int read(void *pData, int amount);
00064 
00066         int getAmountBuffered() const;
00067 private:
00068         class BufferBlock
00069         {
00070         public:
00071                 BufferBlock(uint8_t *pData, int bytesFilled)                    { m_pData = pData; m_offset = 0; m_endPos = bytesFilled; }
00072                 ~BufferBlock()                                                  { delete [] m_pData; }
00073                 uint8_t *getData() const                                        { return m_pData; }
00074                 void setOffset(int offset)                                      { m_offset = offset; }
00075                 int getOffset() const                                           { return m_offset; }
00076                 void setEndPos(int endPos)                                      { m_endPos = endPos; }
00077                 int getEndPos() const                                           { return m_endPos; }
00078         private:
00079                 uint8_t *m_pData;
00080                 int m_offset;
00081                 int m_endPos;
00082         };
00083 
00084         void releaseBlock(BufferBlock *pBlock);
00085         BufferBlock *acquireBlock();
00086 
00087         std::list<BufferBlock *> m_blocks;
00088         std::vector<BufferBlock *> m_preAllocatedBlocks;
00089         int m_blockSize;
00090         int m_bytesAvailable;
00091         int m_numPreAlloc;
00092         int m_numPreAllocAvailable;
00093         mutable jthread::JMutex m_mutex;
00094 };
00095 
00096 #endif // MIPSTREAMBUFFER_H
00097