00001 //***************************************************************** 00002 /* 00003 JackTrip: A System for High-Quality Audio Network Performance 00004 over the Internet 00005 00006 Copyright (c) 2008 Juan-Pablo Caceres, Chris Chafe. 00007 SoundWIRE group at CCRMA, Stanford University. 00008 00009 Permission is hereby granted, free of charge, to any person 00010 obtaining a copy of this software and associated documentation 00011 files (the "Software"), to deal in the Software without 00012 restriction, including without limitation the rights to use, 00013 copy, modify, merge, publish, distribute, sublicense, and/or sell 00014 copies of the Software, and to permit persons to whom the 00015 Software is furnished to do so, subject to the following 00016 conditions: 00017 00018 The above copyright notice and this permission notice shall be 00019 included in all copies or substantial portions of the Software. 00020 00021 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 00022 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 00023 OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 00024 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 00025 HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 00026 WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 00027 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 00028 OTHER DEALINGS IN THE SOFTWARE. 00029 */ 00030 //***************************************************************** 00031 00038 #ifndef __RINGBUFFER_H__ 00039 #define __RINGBUFFER_H__ 00040 00041 #include <QWaitCondition> 00042 #include <QMutex> 00043 #include <QMutexLocker> 00044 00045 #include "jacktrip_types.h" 00046 00047 00055 class RingBuffer 00056 { 00057 public: 00058 00063 RingBuffer(int SlotSize, int NumSlots); 00064 00067 virtual ~RingBuffer(); 00068 00078 void insertSlotBlocking(const int8_t* ptrToSlot); 00079 00089 void readSlotBlocking(int8_t* ptrToReadSlot); 00090 00094 void insertSlotNonBlocking(const int8_t* ptrToSlot); 00095 00099 void readSlotNonBlocking(int8_t* ptrToReadSlot); 00100 00101 00102 protected: 00103 00108 virtual void setUnderrunReadSlot(int8_t* ptrToReadSlot); 00109 00116 virtual void setMemoryInReadSlotWithLastReadSlot(int8_t* ptrToReadSlot); 00117 00118 private: 00119 00121 void underrunReset(); 00123 void overflowReset(); 00125 void debugDump() const; 00126 00127 const int mSlotSize; 00128 const int mNumSlots; 00129 const int mTotalSize; 00130 int mReadPosition; 00131 int mWritePosition; 00132 int mFullSlots; 00133 int8_t* mRingBuffer; 00134 int8_t* mLastReadSlot; 00135 00136 // Thread Synchronization Private Members 00137 QMutex mMutex; 00138 QWaitCondition mBufferIsNotFull; 00139 QWaitCondition mBufferIsNotEmpty; 00140 }; 00141 00142 #endif