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

Revision 401, 9.3 kB (checked in by pieterpalmers, 16 years ago)

move some code from amdtp SP to SP base 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    
87     virtual bool prepareForEnable(uint64_t time_to_enable_at);
88     virtual bool prepareForDisable();
89    
90     bool enable(uint64_t time_to_enable_at); ///< enable the stream processing
91     bool disable(); ///< disable the stream processing
92     bool isEnabled() {return !m_is_disabled;};
93
94     virtual bool putFrames(unsigned int nbframes, int64_t ts) = 0; ///< transfer the buffer contents from client
95     virtual bool getFrames(unsigned int nbframes) = 0; ///< transfer the buffer contents to the client
96
97     virtual bool reset(); ///< reset the streams & buffers (e.g. after xrun)
98
99     virtual bool prepare(); ///< prepare the streams & buffers (e.g. prefill)
100
101     virtual void dumpInfo();
102
103     virtual bool init();
104
105     virtual void setVerboseLevel(int l);
106
107     virtual bool prepareForStop() {return true;};
108     virtual bool prepareForStart() {return true;};
109
110
111 public:
112     FreebobUtil::TimestampedBuffer *m_data_buffer;
113
114 protected:
115
116     void setManager(StreamProcessorManager *manager) {m_manager=manager;};
117     void clearManager() {m_manager=0;};
118
119     unsigned int m_nb_buffers; ///< cached from manager->getNbBuffers(), the number of periods to buffer
120     unsigned int m_period; ///< cached from manager->getPeriod(), the period size
121
122     unsigned int m_xruns;
123
124     unsigned int m_framerate;
125
126     StreamProcessorManager *m_manager;
127    
128     bool m_running;
129     bool m_disabled;
130     bool m_is_disabled;
131     unsigned int m_cycle_to_enable_at;
132    
133     StreamStatistics m_PacketStat;
134     StreamStatistics m_PeriodStat;
135    
136     StreamStatistics m_WakeupStat;
137    
138
139     DECLARE_DEBUG_MODULE;
140    
141     // frame counter & sync stuff
142     public:
143         /**
144          * @brief Can this StreamProcessor handle a transfer of nframes frames?
145          *
146          * this function indicates if the streamprocessor can handle a transfer of
147          * nframes frames. It is used to detect underruns-to-be.
148          *
149          * @param nframes number of frames
150          * @return true if the StreamProcessor can handle this amount of frames
151          *         false if it can't
152          */
153         virtual bool canClientTransferFrames(unsigned int nframes) = 0;
154        
155         /**
156          * \brief return the time until the next period boundary (in microseconds)
157          *
158          * Return the time until the next period boundary. If this StreamProcessor
159          * is the current synchronization source, this function is called to
160          * determine when a buffer transfer can be made. When this value is
161          * smaller than 0, a period boundary is assumed to be crossed, hence a
162          * transfer can be made.
163          *
164          * \return the time in usecs
165          */
166         int64_t getTimeUntilNextPeriodUsecs();
167         /**
168          * \brief return the time of the next period boundary (in microseconds)
169          *
170          * Returns the time of the next period boundary, in microseconds. The
171          * goal of this function is to determine the exact point of the period
172          * boundary. This is assumed to be the point at which the buffer transfer should
173          * take place, meaning that it can be used as a reference timestamp for transmitting
174          * StreamProcessors
175          *
176          * \return the time in usecs
177          */
178         uint64_t getTimeAtPeriodUsecs();
179
180         /**
181          * \brief return the time of the next period boundary (in internal units)
182          *
183          * The same as getTimeUntilNextPeriodUsecs() but in internal units.
184          *
185          * @return the time in internal units
186          */
187         virtual uint64_t getTimeAtPeriod() = 0;
188
189         uint64_t getTimeNow();
190        
191        
192         /**
193          * Returns the sync delay. This is the time a syncsource
194          * delays a period signal, e.g. to cope with buffering.
195          * @return the sync delay
196          */
197         int getSyncDelay() {return m_sync_delay;};
198         /**
199          * sets the sync delay
200          * @param d sync delay
201          */
202         void setSyncDelay(int d) {m_sync_delay=d;};
203        
204         /**
205          * Returns the minimal sync delay a SP needs
206          * @return minimal sync delay
207          */
208         virtual int getMinimalSyncDelay() = 0;
209
210         bool setSyncSource(StreamProcessor *s);
211         float getTicksPerFrame() {return m_ticks_per_frame;};
212
213         int getLastCycle() {return m_last_cycle;};
214
215         int getBufferFill();
216        
217     protected:
218         StreamProcessor *m_SyncSource;
219
220         float m_ticks_per_frame;
221
222         int m_last_cycle;
223         int m_sync_delay;
224
225 };
226
227 /*!
228 \brief Class providing a generic interface for receive Stream Processors
229
230 */
231 class ReceiveStreamProcessor : public StreamProcessor {
232
233 public:
234         ReceiveStreamProcessor(int port, int framerate);
235
236         virtual ~ReceiveStreamProcessor();
237
238
239         virtual enum EProcessorType getType() {return E_Receive;};
240        
241         virtual enum raw1394_iso_disposition
242                 getPacket(unsigned char *data, unsigned int *length,
243                       unsigned char *tag, unsigned char *sy,
244                       int cycle, unsigned int dropped, unsigned int max_length)
245                       {return RAW1394_ISO_STOP;};
246         virtual bool putFrames(unsigned int nbframes, int64_t ts) {return false;};
247        
248         virtual enum raw1394_iso_disposition putPacket(unsigned char *data, unsigned int length,
249                       unsigned char channel, unsigned char tag, unsigned char sy,
250                           unsigned int cycle, unsigned int dropped) = 0;
251         virtual void setVerboseLevel(int l);
252
253     uint64_t getTimeAtPeriod();
254     bool canClientTransferFrames(unsigned int nframes);
255
256 protected:
257     bool processWriteBlock(char *data, unsigned int nevents, unsigned int offset) {return true;};
258
259     DECLARE_DEBUG_MODULE;
260
261 };
262
263 /*!
264 \brief Class providing a generic interface for receive Stream Processors
265
266 */
267 class TransmitStreamProcessor : public StreamProcessor {
268
269 public:
270         TransmitStreamProcessor(int port, int framerate);
271
272         virtual ~TransmitStreamProcessor();
273
274         virtual enum EProcessorType getType() {return E_Transmit;};
275
276         virtual enum raw1394_iso_disposition
277                 putPacket(unsigned char *data, unsigned int length,
278                       unsigned char channel, unsigned char tag, unsigned char sy,
279                           unsigned int cycle, unsigned int dropped) {return RAW1394_ISO_STOP;};
280         virtual bool getFrames(unsigned int nbframes) {return false;};
281
282         virtual enum raw1394_iso_disposition
283                 getPacket(unsigned char *data, unsigned int *length,
284                       unsigned char *tag, unsigned char *sy,
285                       int cycle, unsigned int dropped, unsigned int max_length) = 0;
286         virtual void setVerboseLevel(int l);
287
288     uint64_t getTimeAtPeriod();
289     bool canClientTransferFrames(unsigned int nframes);
290
291 protected:
292     bool processReadBlock(char *data, unsigned int nevents, unsigned int offset) {return true;};
293
294     DECLARE_DEBUG_MODULE;
295
296
297 };
298
299 }
300
301 #endif /* __FREEBOB_STREAMPROCESSOR__ */
302
303
Note: See TracBrowser for help on using the browser.