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

Revision 386, 9.1 kB (checked in by pieterpalmers, 17 years ago)

- moved files around to the place they belong
- fixed all compile warnings

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 namespace FreebobStreaming {
41
42 class StreamProcessorManager;
43
44 /*!
45 \brief Class providing a generic interface for Stream Processors
46
47  A stream processor multiplexes or demultiplexes an ISO stream into a
48  collection of ports. This class should be subclassed, and the relevant
49  functions should be overloaded.
50  
51 */
52 class StreamProcessor : public IsoStream,
53                         public PortManager {
54
55     friend class StreamProcessorManager;
56
57 public:
58     enum EProcessorType {
59             E_Receive,
60             E_Transmit
61     };
62
63     StreamProcessor(enum IsoStream::EStreamType type, int port, int framerate);
64     virtual ~StreamProcessor();
65
66     virtual enum raw1394_iso_disposition
67             putPacket(unsigned char *data, unsigned int length,
68                     unsigned char channel, unsigned char tag, unsigned char sy,
69                         unsigned int cycle, unsigned int dropped) = 0;
70     virtual enum raw1394_iso_disposition
71             getPacket(unsigned char *data, unsigned int *length,
72                     unsigned char *tag, unsigned char *sy,
73                     int cycle, unsigned int dropped, unsigned int max_length) = 0;
74
75     virtual enum EProcessorType getType() =0;
76
77     bool xrunOccurred() { return (m_xruns>0);};
78    
79     // move to private?
80     void resetXrunCounter();
81
82     bool isRunning(); ///< returns true if there is some stream data processed
83     bool enable(); ///< enable the stream processing
84     bool disable(); ///< disable the stream processing
85     bool isEnabled() {return !m_is_disabled;};
86
87     virtual bool putFrames(unsigned int nbframes, int64_t ts); ///< transfer the buffer contents from client
88     virtual bool getFrames(unsigned int nbframes, int64_t ts); ///< transfer the buffer contents to the client
89
90     virtual bool reset(); ///< reset the streams & buffers (e.g. after xrun)
91
92     virtual bool prepare(); ///< prepare the streams & buffers (e.g. prefill)
93
94     virtual void dumpInfo();
95
96     virtual bool init();
97
98     virtual void setVerboseLevel(int l);
99
100     virtual bool prepareForStop() {return true;};
101     virtual bool prepareForStart() {return true;};
102    
103     virtual bool prepareForEnable();
104     virtual bool prepareForDisable();
105
106 protected:
107        
108
109     void setManager(StreamProcessorManager *manager) {m_manager=manager;};
110     void clearManager() {m_manager=0;};
111
112     unsigned int m_nb_buffers; ///< cached from manager->getNbBuffers(), the number of periods to buffer
113     unsigned int m_period; ///< cached from manager->getPeriod(), the period size
114
115     unsigned int m_xruns;
116
117     unsigned int m_framerate;
118
119     StreamProcessorManager *m_manager;
120    
121     bool m_running;
122     bool m_disabled;
123     bool m_is_disabled;
124    
125     StreamStatistics m_PacketStat;
126     StreamStatistics m_PeriodStat;
127    
128     StreamStatistics m_WakeupStat;
129    
130
131     DECLARE_DEBUG_MODULE;
132    
133     // frame counter & sync stuff
134     public:
135         /**
136          * @brief Can this StreamProcessor handle a nframes of frames?
137          *
138          * this function indicates if the streamprocessor can handle nframes
139          * of frames. It is used to detect underruns-to-be.
140          *
141          * @param nframes number of frames
142          * @return true if the StreamProcessor can handle this amount of frames
143          *         false if it can't
144          */
145         virtual bool canClientTransferFrames(unsigned int nframes) {return true;};
146        
147         int getFrameCounter() {return m_framecounter;};
148    
149         void decrementFrameCounter(int nbframes, uint64_t new_timestamp);
150         void incrementFrameCounter(int nbframes, uint64_t new_timestamp);
151         void resetFrameCounter();
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         void getBufferHeadTimestamp(uint64_t *ts, uint64_t *fc);
188         void getBufferTailTimestamp(uint64_t *ts, uint64_t *fc);
189        
190         void setBufferTailTimestamp(uint64_t new_timestamp);
191         void setBufferHeadTimestamp(uint64_t new_timestamp);
192         void setBufferTimestamps(uint64_t new_head, uint64_t new_tail);
193        
194         bool setSyncSource(StreamProcessor *s);
195         float getTicksPerFrame() {return m_ticks_per_frame;};
196    
197     private:
198         // the framecounter gives the number of frames in the buffer
199         signed int m_framecounter;
200        
201         // the buffer tail timestamp gives the timestamp of the last frame
202         // that was put into the buffer
203         uint64_t   m_buffer_tail_timestamp;
204        
205         // the buffer head timestamp gives the timestamp of the first frame
206         // that was put into the buffer
207         uint64_t   m_buffer_head_timestamp;
208        
209     protected:
210         StreamProcessor *m_SyncSource;
211        
212         float m_ticks_per_frame;
213
214     private:
215         // this mutex protects the access to the framecounter
216         // and the buffer head timestamp.
217         pthread_mutex_t m_framecounter_lock;
218
219 };
220
221 /*!
222 \brief Class providing a generic interface for receive Stream Processors
223
224 */
225 class ReceiveStreamProcessor : public StreamProcessor {
226
227 public:
228         ReceiveStreamProcessor(int port, int framerate);
229
230         virtual ~ReceiveStreamProcessor();
231
232
233         virtual enum EProcessorType getType() {return E_Receive;};
234        
235         virtual enum raw1394_iso_disposition
236                 getPacket(unsigned char *data, unsigned int *length,
237                       unsigned char *tag, unsigned char *sy,
238                       int cycle, unsigned int dropped, unsigned int max_length)
239                       {return RAW1394_ISO_STOP;};
240                      
241         virtual enum raw1394_iso_disposition putPacket(unsigned char *data, unsigned int length,
242                       unsigned char channel, unsigned char tag, unsigned char sy,
243                           unsigned int cycle, unsigned int dropped) = 0;
244         virtual void setVerboseLevel(int l);
245
246 protected:
247
248      DECLARE_DEBUG_MODULE;
249
250 };
251
252 /*!
253 \brief Class providing a generic interface for receive Stream Processors
254
255 */
256 class TransmitStreamProcessor : public StreamProcessor {
257
258 public:
259         TransmitStreamProcessor(int port, int framerate);
260
261         virtual ~TransmitStreamProcessor();
262
263         virtual enum EProcessorType getType() {return E_Transmit;};
264
265         virtual enum raw1394_iso_disposition
266                 putPacket(unsigned char *data, unsigned int length,
267                       unsigned char channel, unsigned char tag, unsigned char sy,
268                           unsigned int cycle, unsigned int dropped) {return RAW1394_ISO_STOP;};
269                          
270         virtual enum raw1394_iso_disposition
271                 getPacket(unsigned char *data, unsigned int *length,
272                       unsigned char *tag, unsigned char *sy,
273                       int cycle, unsigned int dropped, unsigned int max_length) = 0;
274         virtual void setVerboseLevel(int l);
275
276 protected:
277
278      DECLARE_DEBUG_MODULE;
279
280
281 };
282
283 }
284
285 #endif /* __FREEBOB_STREAMPROCESSOR__ */
286
287
Note: See TracBrowser for help on using the browser.