1 |
/* $Id$ */ |
---|
2 |
|
---|
3 |
/* |
---|
4 |
* FreeBob Streaming API |
---|
5 |
* FreeBob = Firewire (pro-)audio for linux |
---|
6 |
* |
---|
7 |
* http://freebob.sf.net |
---|
8 |
* |
---|
9 |
* Copyright (C) 2005,2006,2007 Pieter Palmers <pieterpalmers@users.sourceforge.net> |
---|
10 |
* |
---|
11 |
* This program is free software {} you can redistribute it and/or modify |
---|
12 |
* it under the terms of the GNU General Public License as published by |
---|
13 |
* the Free Software Foundation {} either version 2 of the License, or |
---|
14 |
* (at your option) any later version. |
---|
15 |
* |
---|
16 |
* This program is distributed in the hope that it will be useful, |
---|
17 |
* but WITHOUT ANY WARRANTY {} without even the implied warranty of |
---|
18 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
19 |
* GNU General Public License for more details. |
---|
20 |
* |
---|
21 |
* You should have received a copy of the GNU General Public License |
---|
22 |
* along with this program {} if not, write to the Free Software |
---|
23 |
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
24 |
* |
---|
25 |
* |
---|
26 |
* |
---|
27 |
*/ |
---|
28 |
#ifndef __FREEBOB_TIMESTAMPEDBUFFER__ |
---|
29 |
#define __FREEBOB_TIMESTAMPEDBUFFER__ |
---|
30 |
|
---|
31 |
#include "../debugmodule/debugmodule.h" |
---|
32 |
#include "libutil/ringbuffer.h" |
---|
33 |
|
---|
34 |
namespace FreebobUtil { |
---|
35 |
|
---|
36 |
class TimestampedBufferClient; |
---|
37 |
|
---|
38 |
/** |
---|
39 |
* \brief Class implementing a frame buffer that is time-aware |
---|
40 |
* |
---|
41 |
* This class implements a buffer that is time-aware. Whenever new frames |
---|
42 |
* are written to the buffer, the timestamp corresponding to the last frame |
---|
43 |
* in the buffer is updated. This allows to calculate the timestamp of any |
---|
44 |
* other frame in the buffer. |
---|
45 |
* |
---|
46 |
* The buffer is a frame buffer, having the following parameters defining |
---|
47 |
* it's behaviour: |
---|
48 |
* - buff_size: buffer size in frames (setBufferSize()) |
---|
49 |
* - events_per_frame: the number of events per frame (setEventsPerFrame()) |
---|
50 |
* - event_size: the storage size of the events (in bytes) (setEventSize()) |
---|
51 |
* |
---|
52 |
* The total size of the buffer (in bytes) is at least |
---|
53 |
* buff_size*events_per_frame*event_size. |
---|
54 |
* |
---|
55 |
* Timestamp tracking is done by requiring that a timestamp is specified every |
---|
56 |
* time frames are added to the buffer. In combination with the buffer fill and |
---|
57 |
* the frame rate (calculated internally), this allows to calculate the timestamp |
---|
58 |
* of any frame in the buffer. In order to initialize the internal data structures, |
---|
59 |
* the setNominalRate() and setUpdatePeriod() functions are provided. |
---|
60 |
* |
---|
61 |
* \note Currently the class only supports fixed size writes of size update_period. |
---|
62 |
* This can change in the future, implementation ideas are already in place. |
---|
63 |
* |
---|
64 |
* The TimestampedBuffer class is time unit agnostic. It can handle any time unit |
---|
65 |
* as long as it fits in a 64 bit unsigned integer. The buffer supports wrapped |
---|
66 |
* timestamps using (...). |
---|
67 |
* |
---|
68 |
* There are two methods of reading and writing to the buffer. |
---|
69 |
* |
---|
70 |
* The first method uses conventional readFrames() and writeFrames() functions. |
---|
71 |
* |
---|
72 |
* The second method makes use of the TimestampedBufferClient interface. When a |
---|
73 |
* TimestampedBuffer is created, it is required that a TimestampedBufferClient is |
---|
74 |
* registered. This client implements the processReadBlock and processWriteBlock |
---|
75 |
* functions. These are block processing 'callbacks' that allow zero-copy processing |
---|
76 |
* of the buffer contents. In order to initiate block processing, the |
---|
77 |
* blockProcessWriteFrames and blockProcessReadFrames functions are provided by |
---|
78 |
* TimestampedBuffer. |
---|
79 |
* |
---|
80 |
*/ |
---|
81 |
class TimestampedBuffer { |
---|
82 |
|
---|
83 |
public: |
---|
84 |
|
---|
85 |
|
---|
86 |
TimestampedBuffer(TimestampedBufferClient *); |
---|
87 |
virtual ~TimestampedBuffer(); |
---|
88 |
|
---|
89 |
bool writeFrames(unsigned int nbframes, char *data, uint64_t ts); |
---|
90 |
bool readFrames(unsigned int nbframes, char *data); |
---|
91 |
|
---|
92 |
bool blockProcessWriteFrames(unsigned int nbframes, int64_t ts); |
---|
93 |
bool blockProcessReadFrames(unsigned int nbframes); |
---|
94 |
|
---|
95 |
bool init(); |
---|
96 |
bool prepare(); |
---|
97 |
bool reset(); |
---|
98 |
|
---|
99 |
bool setEventSize(unsigned int s); |
---|
100 |
bool setEventsPerFrame(unsigned int s); |
---|
101 |
bool setBufferSize(unsigned int s); |
---|
102 |
|
---|
103 |
bool setWrapValue(uint64_t w); |
---|
104 |
|
---|
105 |
unsigned int getBufferFill(); |
---|
106 |
|
---|
107 |
// timestamp stuff |
---|
108 |
int getFrameCounter() {return m_framecounter;}; |
---|
109 |
|
---|
110 |
void getBufferHeadTimestamp(uint64_t *ts, uint64_t *fc); |
---|
111 |
void getBufferTailTimestamp(uint64_t *ts, uint64_t *fc); |
---|
112 |
|
---|
113 |
void setBufferTailTimestamp(uint64_t new_timestamp); |
---|
114 |
|
---|
115 |
uint64_t getTimestampFromTail(int nframes); |
---|
116 |
uint64_t getTimestampFromHead(int nframes); |
---|
117 |
|
---|
118 |
// buffer offset stuff |
---|
119 |
/// return the tick offset value |
---|
120 |
signed int getTickOffset() {return m_tick_offset;}; |
---|
121 |
|
---|
122 |
bool setFrameOffset(int nframes); |
---|
123 |
bool setTickOffset(int nframes); |
---|
124 |
|
---|
125 |
// dll stuff |
---|
126 |
bool setNominalRate(float r); |
---|
127 |
float getRate(); |
---|
128 |
|
---|
129 |
bool setUpdatePeriod(unsigned int t); |
---|
130 |
|
---|
131 |
// misc stuff |
---|
132 |
void dumpInfo(); |
---|
133 |
void setVerboseLevel(int l) {setDebugLevel(l);}; |
---|
134 |
|
---|
135 |
private: |
---|
136 |
void decrementFrameCounter(int nbframes); |
---|
137 |
void incrementFrameCounter(int nbframes, uint64_t new_timestamp); |
---|
138 |
void resetFrameCounter(); |
---|
139 |
|
---|
140 |
protected: |
---|
141 |
|
---|
142 |
freebob_ringbuffer_t * m_event_buffer; |
---|
143 |
char* m_cluster_buffer; |
---|
144 |
|
---|
145 |
unsigned int m_event_size; // the size of one event |
---|
146 |
unsigned int m_events_per_frame; // the number of events in a frame |
---|
147 |
unsigned int m_buffer_size; // the number of frames in the buffer |
---|
148 |
unsigned int m_bytes_per_frame; |
---|
149 |
unsigned int m_bytes_per_buffer; |
---|
150 |
|
---|
151 |
uint64_t m_wrap_at; // value to wrap at |
---|
152 |
|
---|
153 |
TimestampedBufferClient *m_Client; |
---|
154 |
|
---|
155 |
DECLARE_DEBUG_MODULE; |
---|
156 |
|
---|
157 |
private: |
---|
158 |
// the framecounter gives the number of frames in the buffer |
---|
159 |
signed int m_framecounter; |
---|
160 |
|
---|
161 |
// the offset that define the timing of the buffer |
---|
162 |
signed int m_tick_offset; |
---|
163 |
|
---|
164 |
// the buffer tail timestamp gives the timestamp of the last frame |
---|
165 |
// that was put into the buffer |
---|
166 |
uint64_t m_buffer_tail_timestamp; |
---|
167 |
uint64_t m_buffer_next_tail_timestamp; |
---|
168 |
|
---|
169 |
// this mutex protects the access to the framecounter |
---|
170 |
// and the buffer head timestamp. |
---|
171 |
pthread_mutex_t m_framecounter_lock; |
---|
172 |
|
---|
173 |
// tracking DLL variables |
---|
174 |
double m_dll_e2; |
---|
175 |
double m_dll_b; |
---|
176 |
double m_dll_c; |
---|
177 |
|
---|
178 |
float m_nominal_rate; |
---|
179 |
unsigned int m_update_period; |
---|
180 |
}; |
---|
181 |
|
---|
182 |
/** |
---|
183 |
* \brief Interface to be implemented by TimestampedBuffer clients |
---|
184 |
*/ |
---|
185 |
class TimestampedBufferClient { |
---|
186 |
public: |
---|
187 |
TimestampedBufferClient() {}; |
---|
188 |
virtual ~TimestampedBufferClient() {}; |
---|
189 |
|
---|
190 |
virtual bool processReadBlock(char *data, unsigned int nevents, unsigned int offset)=0; |
---|
191 |
virtual bool processWriteBlock(char *data, unsigned int nevents, unsigned int offset)=0; |
---|
192 |
|
---|
193 |
}; |
---|
194 |
|
---|
195 |
} // end of namespace FreebobUtil |
---|
196 |
|
---|
197 |
#endif /* __FREEBOB_TIMESTAMPEDBUFFER__ */ |
---|
198 |
|
---|
199 |
|
---|