root/trunk/libffado/src/libstreaming/StreamProcessor.h

Revision 494, 9.3 kB (checked in by ppalmers, 15 years ago)

- switch over to a generic ffado_timestamp_t for the timestamped buffer (currently float)
- implemented some experimental stream phase sync method

- various small things

NOTE: not a very stable commit

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 library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License version 2.1, as published by the Free Software Foundation;
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
21  * MA 02110-1301 USA
22  */
23
24 #ifndef __FFADO_STREAMPROCESSOR__
25 #define __FFADO_STREAMPROCESSOR__
26
27 #include "../debugmodule/debugmodule.h"
28
29 #include "IsoStream.h"
30 #include "PortManager.h"
31
32 #include <pthread.h>
33
34 #include "libutil/StreamStatistics.h"
35 #include "libutil/TimestampedBuffer.h"
36 #include "libutil/OptionContainer.h"
37
38 namespace Streaming {
39
40 class StreamProcessorManager;
41
42 /*!
43 \brief Class providing a generic interface for Stream Processors
44
45  A stream processor multiplexes or demultiplexes an ISO stream into a
46  collection of ports. This class should be subclassed, and the relevant
47  functions should be overloaded.
48
49 */
50 class StreamProcessor : public IsoStream,
51                         public PortManager,
52                         public Util::TimestampedBufferClient,
53                         public Util::OptionContainer {
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
84     virtual bool prepareForEnable(uint64_t time_to_enable_at);
85     virtual bool prepareForDisable();
86
87     bool enable(uint64_t time_to_enable_at); ///< enable the stream processing
88     bool disable(); ///< disable the stream processing
89     bool isEnabled() {return !m_is_disabled;};
90
91     virtual bool putFrames(unsigned int nbframes, int64_t ts) = 0; ///< transfer the buffer contents from client
92     virtual bool getFrames(unsigned int nbframes, int64_t ts) = 0; ///< transfer the buffer contents to the client
93
94     virtual bool reset(); ///< reset the streams & buffers (e.g. after xrun)
95
96     virtual bool prepare(); ///< prepare the streams & buffers (e.g. prefill)
97
98     virtual void dumpInfo();
99
100     virtual bool init();
101
102     virtual void setVerboseLevel(int l);
103
104     virtual bool prepareForStop() {return true;};
105     virtual bool prepareForStart() {return true;};
106
107
108 public:
109     Util::TimestampedBuffer *m_data_buffer;
110
111 protected: // SPM related
112     void setManager(StreamProcessorManager *manager) {m_manager=manager;};
113     void clearManager() {m_manager=0;};
114
115 protected:
116     unsigned int m_nb_buffers; ///< cached from manager->getNbBuffers(), the number of periods to buffer
117     unsigned int m_period; ///< cached from manager->getPeriod(), the period size
118
119     unsigned int m_xruns;
120
121     unsigned int m_framerate;
122
123     StreamProcessorManager *m_manager;
124
125     bool m_running;
126     bool m_disabled;
127     bool m_is_disabled;
128     unsigned int m_cycle_to_enable_at;
129
130     StreamStatistics m_PacketStat;
131     StreamStatistics m_PeriodStat;
132
133     StreamStatistics m_WakeupStat;
134
135
136     DECLARE_DEBUG_MODULE;
137
138     // frame counter & sync stuff
139     public:
140         /**
141          * @brief Can this StreamProcessor handle a transfer of nframes frames?
142          *
143          * this function indicates if the streamprocessor can handle a transfer of
144          * nframes frames. It is used to detect underruns-to-be.
145          *
146          * @param nframes number of frames
147          * @return true if the StreamProcessor can handle this amount of frames
148          *         false if it can't
149          */
150         virtual bool canClientTransferFrames(unsigned int nframes) = 0;
151
152         /**
153          * \brief return the time until the next period boundary should be signaled (in microseconds)
154          *
155          * Return the time until the next period boundary signal. If this StreamProcessor
156          * is the current synchronization source, this function is called to
157          * determine when a buffer transfer can be made. When this value is
158          * smaller than 0, a period boundary is assumed to be crossed, hence a
159          * transfer can be made.
160          *
161          * \return the time in usecs
162          */
163         int64_t getTimeUntilNextPeriodSignalUsecs();
164         /**
165          * \brief return the time of the next period boundary (in microseconds)
166          *
167          * Returns the time of the next period boundary, in microseconds. The
168          * goal of this function is to determine the exact point of the period
169          * boundary. This is assumed to be the point at which the buffer transfer should
170          * take place, meaning that it can be used as a reference timestamp for transmitting
171          * StreamProcessors
172          *
173          * \return the time in usecs
174          */
175         uint64_t getTimeAtPeriodUsecs();
176
177         /**
178          * \brief return the time of the next period boundary (in internal units)
179          *
180          * The same as getTimeAtPeriodUsecs() but in internal units.
181          *
182          * @return the time in internal units
183          */
184         virtual uint64_t getTimeAtPeriod() = 0;
185
186         uint64_t getTimeNow();
187
188
189         /**
190          * Returns the sync delay. This is the time a syncsource
191          * delays a period signal, e.g. to cope with buffering.
192          * @return the sync delay
193          */
194         int getSyncDelay() {return m_sync_delay;};
195         /**
196          * sets the sync delay
197          * @param d sync delay
198          */
199         void setSyncDelay(int d) {m_sync_delay=d;};
200
201         /**
202          * Returns the minimal sync delay a SP needs
203          * @return minimal sync delay
204          */
205         virtual int getMinimalSyncDelay() = 0;
206
207         bool setSyncSource(StreamProcessor *s);
208         float getTicksPerFrame();
209
210         int getLastCycle() {return m_last_cycle;};
211
212         int getBufferFill();
213
214     protected:
215         StreamProcessor *m_SyncSource;
216
217         float m_ticks_per_frame;
218
219         int m_last_cycle;
220         int m_sync_delay;
221
222 };
223
224 /*!
225 \brief Class providing a generic interface for receive Stream Processors
226
227 */
228 class ReceiveStreamProcessor : public StreamProcessor {
229
230 public:
231     ReceiveStreamProcessor(int port, int framerate);
232
233     virtual ~ReceiveStreamProcessor();
234
235
236     virtual enum EProcessorType getType() {return E_Receive;};
237
238     virtual enum raw1394_iso_disposition
239         getPacket(unsigned char *data, unsigned int *length,
240                   unsigned char *tag, unsigned char *sy,
241                   int cycle, unsigned int dropped, unsigned int max_length)
242                   {return RAW1394_ISO_STOP;};
243         virtual bool putFrames(unsigned int nbframes, int64_t ts) {return false;};
244
245         virtual enum raw1394_iso_disposition putPacket(unsigned char *data, unsigned int length,
246                   unsigned char channel, unsigned char tag, unsigned char sy,
247                   unsigned int cycle, unsigned int dropped) = 0;
248      virtual void setVerboseLevel(int l);
249
250     uint64_t getTimeAtPeriod();
251     bool canClientTransferFrames(unsigned int nframes);
252
253 protected:
254     bool processWriteBlock(char *data, unsigned int nevents, unsigned int offset) {return true;};
255
256     DECLARE_DEBUG_MODULE;
257
258 };
259
260 /*!
261 \brief Class providing a generic interface for receive Stream Processors
262
263 */
264 class TransmitStreamProcessor : public StreamProcessor {
265
266 public:
267     TransmitStreamProcessor(int port, int framerate);
268
269     virtual ~TransmitStreamProcessor();
270
271     virtual enum EProcessorType getType() {return E_Transmit;};
272
273     virtual enum raw1394_iso_disposition
274         putPacket(unsigned char *data, unsigned int length,
275                   unsigned char channel, unsigned char tag, unsigned char sy,
276                   unsigned int cycle, unsigned int dropped) {return RAW1394_ISO_STOP;};
277         virtual bool getFrames(unsigned int nbframes, int64_t ts) {return false;};
278
279     virtual enum raw1394_iso_disposition
280         getPacket(unsigned char *data, unsigned int *length,
281                   unsigned char *tag, unsigned char *sy,
282                   int cycle, unsigned int dropped, unsigned int max_length) = 0;
283      virtual void setVerboseLevel(int l);
284
285     uint64_t getTimeAtPeriod();
286     bool canClientTransferFrames(unsigned int nframes);
287
288 protected:
289     bool processReadBlock(char *data, unsigned int nevents, unsigned int offset) {return true;};
290
291     DECLARE_DEBUG_MODULE;
292
293
294 };
295
296 }
297
298 #endif /* __FFADO_STREAMPROCESSOR__ */
299
300
Note: See TracBrowser for help on using the browser.