Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members

RingBuffer.h

Go to the documentation of this file.
00001 /* audiodevs: Abstraction layer for audio hardware & samples 00002 Copyright (C) 2003-2004 Nemosoft Unv. 00003 00004 This program is free software; you can redistribute it and/or modify 00005 it under the terms of the GNU General Public License as published by 00006 the Free Software Foundation; either version 2 of the License, or 00007 (at your option) any later version. 00008 00009 This program is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 GNU General Public License for more details. 00013 00014 You should have received a copy of the GNU General Public License 00015 along with this program; if not, write to the Free Software 00016 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00017 00018 For questions, remarks, patches, etc. for this program, the author can be 00019 reached at camstream@smcc.demon.nl. 00020 */ 00021 00022 #ifndef RINGBUFFER_HPP 00023 #define RINGBUFFER_HPP 00024 00025 #ifdef HAVE_CONFIG_H 00026 #include "config.h" 00027 #endif 00028 00029 #include <qlist.h> 00030 #include <qobject.h> 00031 #include <qthread.h> 00032 00033 #include "AudioSample.h" 00034 00035 // Forward declarations 00036 class CRingBufferWriter; 00037 class CRingBufferReader; 00038 00039 class CRingBuffer: public QObject 00040 { 00041 friend class CRingBufferWriter; 00042 friend class CRingBufferReader; 00043 Q_OBJECT 00044 private: 00045 QMutex Head; 00046 QMutex Lists; 00047 00048 QList<CRingBufferWriter> Writers; 00049 QList<CRingBufferReader> Readers; 00050 00051 unsigned int BufferSpace, BufferHead, MaxLength; 00052 void *Buffer; 00053 00054 /* Statistical data */ 00055 long BytesWritten; 00056 int Overflows; 00057 00058 /* Called by Writers */ 00059 void AttachWriter(CRingBufferWriter *s); 00060 void DetachWriter(CRingBufferWriter *s); 00061 int AddToBuffer(void *data, int len, bool must_fit); 00062 int SpaceLeft(); 00063 int SpaceUsed(); 00064 00065 /* Called by Readers */ 00066 void AttachReader(CRingBufferReader *r); 00067 void DetachReader(CRingBufferReader *r); 00068 00069 /* Called by both Readers and Writers */ 00070 void Flush(); 00071 00072 public: 00073 CRingBuffer(unsigned int buffer_space); 00074 ~CRingBuffer(); 00075 00076 unsigned int GetBufferLength(); 00077 00078 signals: 00079 void ReaderAttached(); 00080 void ReaderDetached(); 00081 void WriterAttached(); 00082 void WriterDetached(); 00083 00084 void BufferFlushed(); 00085 }; 00086 00087 00088 class CRingBufferWriter 00089 { 00090 private: 00091 CRingBuffer *pRing; 00092 00093 CRingBufferWriter(const CRingBufferWriter &) {}; // copies are impossible 00094 00095 public: 00096 CRingBufferWriter(CRingBuffer *ring); 00097 ~CRingBufferWriter(); 00098 00099 int WriteToBuffer(void *, int len, bool must_fit = false) const; 00100 int SpaceLeft() const; 00101 int SpaceUsed() const; 00102 00103 void Flush() const; 00104 }; 00105 00106 00107 class CRingBufferReader: public QObject 00108 { 00109 friend class CRingBuffer; 00110 Q_OBJECT 00111 private: 00112 CRingBuffer *pRing; 00113 QMutex Lock; 00114 QWaitCondition DataReady; 00115 unsigned int BufferTail, MyBufferLength; 00116 unsigned int LowWaterMark, HighWaterMark; 00117 00118 CRingBufferReader(const CRingBufferReader &r) {}; // copies are impossible 00119 00120 public: 00121 CRingBufferReader(CRingBuffer *ring); 00122 ~CRingBufferReader(); 00123 00124 int SpaceUsed() const; 00125 int ReadFromBufferTail(void *, unsigned int len); 00126 int ReadFromBufferHead(void *, unsigned int len, bool clear = false, unsigned long time = ULONG_MAX); 00127 00128 virtual bool event(QEvent *e); // overloaded 00129 00130 void SetLowWaterMark(unsigned int); 00131 unsigned int GetLowWaterMark() const; 00132 void SetHighWaterMark(unsigned int); 00133 unsigned int GetHighWaterMark() const; 00134 00135 signals: 00136 void DataArrived(int bytes); 00137 void BufferFlushed(); 00138 }; 00139 00140 #endif

Generated on Wed Dec 13 23:38:46 2006 for CamStream by doxygen 1.3.7