root/branches/streaming-rework/src/libstreaming/StreamProcessor.h

Revision 392, 8.5 kB (checked in by pieterpalmers, 17 years ago)

- document TimestampedBuffer? class
- partially ported timestamp handling to TimestampedBuffer?
- introduced test for TimestampedBuffer? class

Line 
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 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_STREAMPROCESSOR__
29 #define __FREEBOB_STREAMPROCESSOR__
30
31 #include "../debugmodule/debugmodule.h"
32
33 #include "IsoStream.h"
34 #include "PortManager.h"
35
36 #include <pthread.h>
37
38 #include "libutil/StreamStatistics.h"
39
40 #include "libutil/TimestampedBuffer.h"
41
42 namespace FreebobStreaming {
43
44 class StreamProcessorManager;
45
46 /*!
47 \brief Class providing a generic interface for Stream Processors
48
49  A stream processor multiplexes or demultiplexes an ISO stream into a
50  collection of ports. This class should be subclassed, and the relevant
51  functions should be overloaded.
52  
53 */
54 class StreamProcessor : public IsoStream,
55                         public PortManager,
56                         public FreebobUtil::TimestampedBufferClient {
57
58     friend class StreamProcessorManager;
59
60 public:
61     enum EProcessorType {
62             E_Receive,
63             E_Transmit
64     };
65
66     StreamProcessor(enum IsoStream::EStreamType type, int port, int framerate);
67     virtual ~StreamProcessor();
68
69     virtual enum raw1394_iso_disposition
70             putPacket(unsigned char *data, unsigned int length,
71                     unsigned char channel, unsigned char tag, unsigned char sy,
72                         unsigned int cycle, unsigned int dropped) = 0;
73     virtual enum raw1394_iso_disposition
74             getPacket(unsigned char *data, unsigned int *length,
75                     unsigned char *tag, unsigned char *sy,
76                     int cycle, unsigned int dropped, unsigned int max_length) = 0;
77
78     virtual enum EProcessorType getType() =0;
79
80     bool xrunOccurred() { return (m_xruns>0);};
81    
82     // move to private?
83     void resetXrunCounter();
84
85     bool isRunning(); ///< returns true if there is some stream data processed
86     bool enable(uint64_t time_to_enable_at); ///< enable the stream processing
87     bool disable(); ///< disable the stream processing
88     bool isEnabled() {return !m_is_disabled;};
89
90     virtual bool putFrames(unsigned int nbframes, int64_t ts) = 0; ///< transfer the buffer contents from client
91     virtual bool getFrames(unsigned int nbframes) = 0; ///< transfer the buffer contents to the client
92
93     virtual bool reset(); ///< reset the streams & buffers (e.g. after xrun)
94
95     virtual bool prepare(); ///< prepare the streams & buffers (e.g. prefill)
96
97     virtual void dumpInfo();
98
99     virtual bool init();
100
101     virtual void setVerboseLevel(int l);
102
103     virtual bool prepareForStop() {return true;};
104     virtual bool prepareForStart() {return true;};
105    
106     virtual bool prepareForEnable();
107     virtual bool prepareForDisable();
108
109 public:
110     FreebobUtil::TimestampedBuffer *m_data_buffer;
111
112 protected:
113
114     void setManager(StreamProcessorManager *manager) {m_manager=manager;};
115     void clearManager() {m_manager=0;};
116
117     unsigned int m_nb_buffers; ///< cached from manager->getNbBuffers(), the number of periods to buffer
118     unsigned int m_period; ///< cached from manager->getPeriod(), the period size
119
120     unsigned int m_xruns;
121
122     unsigned int m_framerate;
123
124     StreamProcessorManager *m_manager;
125    
126     bool m_running;
127     bool m_disabled;
128     bool m_is_disabled;
129     unsigned int m_cycle_to_enable_at;
130    
131     StreamStatistics m_PacketStat;
132     StreamStatistics m_PeriodStat;
133    
134     StreamStatistics m_WakeupStat;
135    
136
137     DECLARE_DEBUG_MODULE;
138    
139     // frame counter & sync stuff
140     public:
141         /**
142          * @brief Can this StreamProcessor handle a nframes of frames?
143          *
144          * this function indicates if the streamprocessor can handle nframes
145          * of frames. It is used to detect underruns-to-be.
146          *
147          * @param nframes number of frames
148          * @return true if the StreamProcessor can handle this amount of frames
149          *         false if it can't
150          */
151         virtual bool canClientTransferFrames(unsigned int nframes) {return true;};
152        
153         /**
154          * \brief return the time until the next period boundary (in microseconds)
155          *
156          * Return the time until the next period boundary. If this StreamProcessor
157          * is the current synchronization source, this function is called to
158          * determine when a buffer transfer can be made. When this value is
159          * smaller than 0, a period boundary is assumed to be crossed, hence a
160          * transfer can be made.
161          *
162          * \return the time in usecs
163          */
164         virtual int64_t getTimeUntilNextPeriodUsecs() = 0;
165         /**
166          * \brief return the time of the next period boundary (in microseconds)
167          *
168          * Returns the time of the next period boundary, in microseconds. The
169          * goal of this function is to determine the exact point of the period
170          * boundary. This is assumed to be the point at which the buffer transfer should
171          * take place, meaning that it can be used as a reference timestamp for transmitting
172          * StreamProcessors
173          *
174          * \return the time in usecs
175          */
176         virtual uint64_t getTimeAtPeriodUsecs() = 0;
177
178         /**
179          * \brief return the time of the next period boundary (in internal units)
180          *
181          * The same as getTimeUntilNextPeriodUsecs() but in internal units.
182          *
183          * @return the time in internal units
184          */
185         virtual uint64_t getTimeAtPeriod() = 0;
186
187         uint64_t getTimeNow();
188
189         bool setSyncSource(StreamProcessor *s);
190         float getTicksPerFrame() {return m_ticks_per_frame;};
191
192         int getLastCycle() {return m_last_cycle;};
193
194     protected:
195         StreamProcessor *m_SyncSource;
196
197         float m_ticks_per_frame;
198
199         int m_last_cycle;
200
201 };
202
203 /*!
204 \brief Class providing a generic interface for receive Stream Processors
205
206 */
207 class ReceiveStreamProcessor : public StreamProcessor {
208
209 public:
210         ReceiveStreamProcessor(int port, int framerate);
211
212         virtual ~ReceiveStreamProcessor();
213
214
215         virtual enum EProcessorType getType() {return E_Receive;};
216        
217         virtual enum raw1394_iso_disposition
218                 getPacket(unsigned char *data, unsigned int *length,
219                       unsigned char *tag, unsigned char *sy,
220                       int cycle, unsigned int dropped, unsigned int max_length)
221                       {return RAW1394_ISO_STOP;};
222         virtual bool putFrames(unsigned int nbframes, int64_t ts) {return false;};
223        
224         virtual enum raw1394_iso_disposition putPacket(unsigned char *data, unsigned int length,
225                       unsigned char channel, unsigned char tag, unsigned char sy,
226                           unsigned int cycle, unsigned int dropped) = 0;
227         virtual void setVerboseLevel(int l);
228
229 protected:
230     bool processWriteBlock(char *data, unsigned int nevents, unsigned int offset) {return true;};
231
232     DECLARE_DEBUG_MODULE;
233
234 };
235
236 /*!
237 \brief Class providing a generic interface for receive Stream Processors
238
239 */
240 class TransmitStreamProcessor : public StreamProcessor {
241
242 public:
243         TransmitStreamProcessor(int port, int framerate);
244
245         virtual ~TransmitStreamProcessor();
246
247         virtual enum EProcessorType getType() {return E_Transmit;};
248
249         virtual enum raw1394_iso_disposition
250                 putPacket(unsigned char *data, unsigned int length,
251                       unsigned char channel, unsigned char tag, unsigned char sy,
252                           unsigned int cycle, unsigned int dropped) {return RAW1394_ISO_STOP;};
253         virtual bool getFrames(unsigned int nbframes) {return false;};
254
255         virtual enum raw1394_iso_disposition
256                 getPacket(unsigned char *data, unsigned int *length,
257                       unsigned char *tag, unsigned char *sy,
258                       int cycle, unsigned int dropped, unsigned int max_length) = 0;
259         virtual void setVerboseLevel(int l);
260
261 protected:
262     bool processReadBlock(char *data, unsigned int nevents, unsigned int offset) {return true;};
263
264     DECLARE_DEBUG_MODULE;
265
266
267 };
268
269 }
270
271 #endif /* __FREEBOB_STREAMPROCESSOR__ */
272
273
Note: See TracBrowser for help on using the browser.