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

Revision 385, 9.1 kB (checked in by pieterpalmers, 16 years ago)

- fixed issues with SYT timestamp processing
- SYT based sync works if syncing to the received stream

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