RingBuffer.h 4.68 KB
Newer Older
jcaceres's avatar
jcaceres committed
1
2
//*****************************************************************
/*
jcaceres's avatar
jcaceres committed
3
  JackTrip: A System for High-Quality Audio Network Performance
jcaceres's avatar
jcaceres committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
  over the Internet

  Copyright (c) 2008 Juan-Pablo Caceres, Chris Chafe.
  SoundWIRE group at CCRMA, Stanford University.
  
  Permission is hereby granted, free of charge, to any person
  obtaining a copy of this software and associated documentation
  files (the "Software"), to deal in the Software without
  restriction, including without limitation the rights to use,
  copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the
  Software is furnished to do so, subject to the following
  conditions:
  
  The above copyright notice and this permission notice shall be
  included in all copies or substantial portions of the Software.
  
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  OTHER DEALINGS IN THE SOFTWARE.
*/
//*****************************************************************

/**
 * \file RingBuffer.h
 * \author Juan-Pablo Caceres
 * \date July 2008
 */

#ifndef __RINGBUFFER_H__
#define __RINGBUFFER_H__

#include <QWaitCondition>
#include <QMutex>
#include <QMutexLocker>

45
#include "types_jacktrip.h"
jcaceres's avatar
jcaceres committed
46

jcaceres's avatar
jcaceres committed
47

48
/** \brief Provides a ring-buffer (or circular-buffer) that can be written to and read from
jcaceres's avatar
jcaceres committed
49
 * asynchronously (blocking) or synchronously (non-blocking).
50
51
52
 *
 * The RingBuffer is an array of \b NumSlots slots of memory
 * each of which is of size \b SlotSize bytes (8-bits). Slots can be read and 
jcaceres's avatar
jcaceres committed
53
 * written asynchronously/synchronously by multiple threads.
54
 */
jcaceres's avatar
jcaceres committed
55
56
57
58
class RingBuffer
{
public:

59
  /** \brief The class constructor
jcaceres's avatar
jcaceres committed
60
   * \param SlotSize Size of one slot in bytes
61
62
63
64
65
66
67
68
   * \param NumSlots Number of slots
   */
  RingBuffer(int SlotSize, int NumSlots);

  /** \brief The class destructor
   */
  virtual ~RingBuffer();
  
jcaceres's avatar
jcaceres committed
69
70
  /** \brief Insert a slot into the RingBuffer from ptrToSlot. This method will block until
   * there's space in the buffer.
71
   *
jcaceres's avatar
jcaceres committed
72
73
74
75
76
   * The caller is responsible to make sure sizeof(WriteSlot) = SlotSize. This
   * method should be use when the caller can block against its output, like 
   * sending/receiving UDP packets. It shouldn't be used by audio. For that, use the
   * insertSlotNonBlocking.
   * \param ptrToSlot Pointer to slot to insert into the RingBuffer
jcaceres's avatar
jcaceres committed
77
   */
jcaceres's avatar
jcaceres committed
78
  void insertSlotBlocking(const int8_t* ptrToSlot);
jcaceres's avatar
jcaceres committed
79
  
jcaceres's avatar
jcaceres committed
80
81
82
83
84
85
86
87
  /** \brief Read a slot from the RingBuffer into ptrToReadSlot. This method will block until
   * there's space in the buffer.
   *
   * The caller is responsible to make sure sizeof(ptrToReadSlot) = SlotSize. This
   * method should be use when the caller can block against its input, like 
   * sending/receiving UDP packets. It shouldn't be used by audio. For that, use the
   * readSlotNonBlocking.   
   * \param ptrToReadSlot Pointer to read slot from the RingBuffer
jcaceres's avatar
jcaceres committed
88
   */
jcaceres's avatar
jcaceres committed
89
90
91
92
  void readSlotBlocking(int8_t* ptrToReadSlot);
  
  /** \brief Same as insertSlotBlocking but non-blocking (asynchronous)
   * \param ptrToSlot Pointer to slot to insert into the RingBuffer
jcaceres's avatar
more    
jcaceres committed
93
   */
jcaceres's avatar
jcaceres committed
94
95
96
97
98
99
  void insertSlotNonBlocking(const int8_t* ptrToSlot);
  
  /** \brief Same as readSlotBlocking but non-blocking (asynchronous)
   * \param ptrToReadSlot Pointer to read slot from the RingBuffer
   */
  void readSlotNonBlocking(int8_t* ptrToReadSlot);
jcaceres's avatar
jcaceres committed
100

jcaceres's avatar
jcaceres committed
101
  
jcaceres's avatar
jcaceres committed
102
private:
jcaceres's avatar
more    
jcaceres committed
103

104
  /// \brief Resets the ring buffer for reads under-runs non-blocking
jcaceres's avatar
more    
jcaceres committed
105
  void underrunReset();
106
  /// \brief Resets the ring buffer for writes over-flows non-blocking
jcaceres's avatar
more    
jcaceres committed
107
  void overflowReset();
jcaceres's avatar
jcaceres committed
108
109
  /// \brief Helper method to debug, prints member variables to terminal
  void debugDump() const;
jcaceres's avatar
more    
jcaceres committed
110

111
112
  const int mSlotSize; ///< The size of one slot in byes
  const int mNumSlots; ///< Number of Slots
113
114
115
116
117
118
  const int mTotalSize; ///< Total size of the mRingBuffer = mSlotSize*mNumSlotss
  int mReadPosition; ///< Read Positions in the RingBuffer (Tail)
  int mWritePosition; ///< Write Position in the RingBuffer (Head)
  int mFullSlots; ///< Number of used (full) slots, in slot-size
  int8_t* mRingBuffer; ///< 8-bit array of data (1-byte)
  int8_t* mLastReadSlot; ///< Last slot read  
jcaceres's avatar
jcaceres committed
119

120
121
122
123
  // Thread Synchronization Private Members
  QMutex mMutex; ///< Mutex to protect read and write operations
  QWaitCondition mBufferIsNotFull; ///< Buffer not full condition to monitor threads
  QWaitCondition mBufferIsNotEmpty; ///< Buffer not empty condition to monitor threads
jcaceres's avatar
jcaceres committed
124
125
126
};

#endif