root/trunk/libffado/src/libutil/TimestampedBuffer.h

Revision 807, 9.3 kB (checked in by ppalmers, 14 years ago)

more reliability things

Line 
1 /*
2  * Copyright (C) 2005-2007 by Pieter Palmers
3  *
4  * This file is part of FFADO
5  * FFADO = Free Firewire (pro-)audio drivers for linux
6  *
7  * FFADO is based upon FreeBoB.
8  *
9  * This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  */
23
24 #ifndef __FFADO_TIMESTAMPEDBUFFER__
25 #define __FFADO_TIMESTAMPEDBUFFER__
26
27 #include "../debugmodule/debugmodule.h"
28 #include "libutil/ringbuffer.h"
29
30 //typedef float ffado_timestamp_t;
31 //#define TIMESTAMP_FORMAT_SPEC "%14.3f"
32 typedef double ffado_timestamp_t;
33 #define TIMESTAMP_FORMAT_SPEC "%14.3f"
34 // typedef int64_t ffado_timestamp_t;
35 // #define TIMESTAMP_FORMAT_SPEC "%012lld"
36
37 namespace Util
38 {
39
40 class TimestampedBufferClient;
41
42 /**
43     * \brief Class implementing a frame buffer that is time-aware
44     *
45     * This class implements a buffer that is time-aware. Whenever new frames
46     * are written to the buffer, the timestamp corresponding to the last frame
47     * in the buffer is updated. This allows to calculate the timestamp of any
48     * other frame in the buffer.
49     *
50     * The buffer is a frame buffer, having the following parameters defining
51     * it's behaviour:
52     * - buff_size: buffer size in frames (setBufferSize())
53     * - events_per_frame: the number of events per frame (setEventsPerFrame())
54     * - event_size: the storage size of the events (in bytes) (setEventSize())
55     *
56     * The total size of the buffer (in bytes) is at least
57     * buff_size*events_per_frame*event_size.
58     *
59     * Timestamp tracking is done by requiring that a timestamp is specified every
60     * time frames are added to the buffer. In combination with the buffer fill and
61     * the frame rate (calculated internally), this allows to calculate the timestamp
62     * of any frame in the buffer. In order to initialize the internal data structures,
63     * the setNominalRate() and setUpdatePeriod() functions are provided.
64     *
65     * \note Currently the class only supports fixed size writes of size update_period.
66     *       This can change in the future, implementation ideas are already in place.
67     *
68     * The TimestampedBuffer class is time unit agnostic. It can handle any time unit
69     * as long as it fits in a 64 bit unsigned integer. The buffer supports wrapped
70     * timestamps using (...).
71     *
72     * There are two methods of reading and writing to the buffer.
73     *
74     * The first method uses conventional readFrames() and writeFrames() functions.
75     *
76     * The second method makes use of the TimestampedBufferClient interface. When a
77     * TimestampedBuffer is created, it is required that a TimestampedBufferClient is
78     * registered. This client implements the processReadBlock and processWriteBlock
79     * functions. These are block processing 'callbacks' that allow zero-copy processing
80     * of the buffer contents. In order to initiate block processing, the
81     * blockProcessWriteFrames and blockProcessReadFrames functions are provided by
82     * TimestampedBuffer.
83     *
84     */
85 class TimestampedBuffer
86 {
87     public:
88         TimestampedBuffer ( TimestampedBufferClient * );
89         virtual ~TimestampedBuffer();
90
91         /**
92          * @brief waits for the availability of frames (blocking)
93          * @param nframes number of frames
94          *
95          * @return true if frames are available, false if not (e.g. signal occurred)
96          */
97         bool waitForFrames(unsigned int nframes);
98
99         /**
100          * @brief waits for the availability of frames (blocking)
101          *
102          * waits for one update period of frames
103          *
104          * @return true if frames are available, false if not (e.g. signal occurred)
105          */
106         bool waitForFrames();
107
108         /**
109          * @brief waits for the availability of frames (non-blocking)
110          * @param nframes number of frames
111          *
112          * @return true if frames are available, false if not
113          */
114         bool tryWaitForFrames(unsigned int nframes);
115
116         /**
117          * @brief waits for the availability of frames (non-blocking)
118          *
119          * waits for one update period of frames
120          *
121          * @return true if frames are available, false if not
122          */
123         bool tryWaitForFrames();
124
125         bool writeDummyFrame();
126         bool dropFrames ( unsigned int nbframes );
127
128         bool writeFrames ( unsigned int nbframes, char *data, ffado_timestamp_t ts );
129         bool readFrames ( unsigned int nbframes, char *data );
130
131         bool preloadFrames ( unsigned int nbframes, char *data, bool keep_head_ts );
132
133         bool blockProcessWriteFrames ( unsigned int nbframes, ffado_timestamp_t ts );
134         bool blockProcessReadFrames ( unsigned int nbframes );
135
136         bool init();
137         bool prepare();
138         bool clearBuffer();
139
140         bool isEnabled() {return m_enabled;};
141         void enable() {m_enabled=true;};
142         void disable() {m_enabled=false;};
143
144         bool isTransparent() {return m_transparent;};
145         void setTransparent ( bool v ) {m_transparent=v;};
146
147         bool setEventSize ( unsigned int s );
148         bool setEventsPerFrame ( unsigned int s );
149         bool setBufferSize ( unsigned int s );
150         unsigned int getBufferSize() {return m_buffer_size;};
151
152         unsigned int getBytesPerFrame() {return m_bytes_per_frame;};
153
154         bool setWrapValue ( ffado_timestamp_t w );
155
156         unsigned int getBufferFill();
157         unsigned int getBufferSpace();
158
159         // timestamp stuff
160         int getFrameCounter() {return m_framecounter;};
161
162         void getBufferHeadTimestamp ( ffado_timestamp_t *ts, signed int *fc );
163         void getBufferTailTimestamp ( ffado_timestamp_t *ts, signed int *fc );
164
165         void setBufferTailTimestamp ( ffado_timestamp_t new_timestamp );
166         void setBufferHeadTimestamp ( ffado_timestamp_t new_timestamp );
167
168         // sync related, also drops or add frames when necessary
169         bool syncBufferHeadToTimestamp ( ffado_timestamp_t ts );
170         bool syncBufferTailToTimestamp ( ffado_timestamp_t ts );
171         bool syncCorrectLag ( int64_t ts );
172
173         ffado_timestamp_t getTimestampFromTail ( int nframes );
174         ffado_timestamp_t getTimestampFromHead ( int nframes );
175
176         // buffer offset stuff
177         /// return the tick offset value
178         ffado_timestamp_t getTickOffset() {return m_tick_offset;};
179
180         bool setFrameOffset ( int nframes );
181         bool setTickOffset ( ffado_timestamp_t );
182
183         // dll stuff
184         bool setNominalRate ( float r );
185         float getNominalRate() {return m_nominal_rate;};
186         float getRate();
187
188         bool setUpdatePeriod ( unsigned int t );
189         unsigned int getUpdatePeriod();
190
191         // misc stuff
192         void dumpInfo();
193         void setVerboseLevel ( int l ) {setDebugLevel ( l );};
194
195     private:
196         void decrementFrameCounter ( int nbframes );
197         void incrementFrameCounter ( int nbframes, ffado_timestamp_t new_timestamp );
198         void resetFrameCounter();
199
200     protected:
201
202         ffado_ringbuffer_t * m_event_buffer;
203         char* m_cluster_buffer;
204
205         unsigned int m_event_size; // the size of one event
206         unsigned int m_events_per_frame; // the number of events in a frame
207         unsigned int m_buffer_size; // the number of frames in the buffer
208         unsigned int m_bytes_per_frame;
209         unsigned int m_bytes_per_buffer;
210         bool m_enabled; // you can get frames FIXME: rename!!
211         bool m_transparent; // the buffer should hold the frames put in it. if true, discards all frames
212
213         ffado_timestamp_t m_wrap_at; // value to wrap at
214
215         TimestampedBufferClient *m_Client;
216
217         DECLARE_DEBUG_MODULE;
218
219     private:
220         // the framecounter gives the number of frames in the buffer
221         signed int m_framecounter;
222
223         // the offset that define the timing of the buffer
224         ffado_timestamp_t m_tick_offset;
225
226         // the buffer tail timestamp gives the timestamp of the last frame
227         // that was put into the buffer
228         ffado_timestamp_t   m_buffer_tail_timestamp;
229         ffado_timestamp_t   m_buffer_next_tail_timestamp;
230
231         // this mutex protects the access to the framecounter
232         // and the buffer head timestamp.
233         pthread_mutex_t m_framecounter_lock;
234
235         // tracking DLL variables
236 // JMW: try double for this too
237 //    float m_dll_e2;
238         double m_dll_e2;
239         float m_dll_b;
240         float m_dll_c;
241
242         float m_nominal_rate;
243         float calculateRate();
244         float m_current_rate;
245         unsigned int m_update_period;
246 };
247
248 /**
249     * \brief Interface to be implemented by TimestampedBuffer clients
250     */
251 class TimestampedBufferClient
252 {
253     public:
254         TimestampedBufferClient() {};
255         virtual ~TimestampedBufferClient() {};
256
257         virtual bool processReadBlock ( char *data, unsigned int nevents, unsigned int offset ) =0;
258         virtual bool processWriteBlock ( char *data, unsigned int nevents, unsigned int offset ) =0;
259
260 };
261
262 } // end of namespace Util
263
264 #endif /* __FFADO_TIMESTAMPEDBUFFER__ */
265
266
Note: See TracBrowser for help on using the browser.