root/branches/ppalmers-streaming/src/libstreaming/generic/StreamProcessor.h

Revision 706, 9.9 kB (checked in by ppalmers, 16 years ago)

code cleanup

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