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

Revision 721, 14.7 kB (checked in by ppalmers, 13 years ago)

another update

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 \brief Class providing a generic interface for Stream Processors
43
44  A stream processor multiplexes or demultiplexes an ISO stream into a
45  collection of ports. This class should be subclassed, and the relevant
46  functions should be overloaded.
47
48 */
49 class StreamProcessor : public IsoStream,
50                         public PortManager,
51                         public Util::TimestampedBufferClient,
52                         public Util::OptionContainer
53 {
54
55     friend class StreamProcessorManager; // FIXME: get rid of this
56
57 public:
58     ///> the streamprocessor type
59     enum eProcessorType {
60         ePT_Receive,
61         ePT_Transmit
62     };
63     ///> returns the type of the streamprocessor
64     virtual enum eProcessorType getType() { return m_processor_type; };
65 private:
66     // this can only be set by the constructor
67     enum eProcessorType m_processor_type;
68     // pretty printing
69     const char *ePTToString(enum eProcessorType);
70 protected:
71     ///> the state the streamprocessor is in
72     enum eProcessorState {
73         ePS_Invalid,
74         ePS_Created,
75         // ePS_WaitingToStop, FIXME: this will be needed for the MOTU's
76         ePS_Stopped,
77         ePS_WaitingForStream,
78         ePS_DryRunning,
79         ePS_WaitingForStreamEnable,
80         ePS_Running,
81         ePS_WaitingForStreamDisable,
82     };
83
84     ///> set the SP state to a specific value
85     void setState(enum eProcessorState);
86     ///> get the SP state
87     enum eProcessorState getState() {return m_state;};
88 private:
89     enum eProcessorState m_state;
90     // state switching
91     enum eProcessorState m_next_state;
92     unsigned int m_cycle_to_switch_state;
93     bool updateState();
94     // pretty printing
95     const char *ePSToString(enum eProcessorState);
96
97     bool doStop();
98     bool doWaitForRunningStream();
99     bool doDryRunning();
100     bool doWaitForStreamEnable();
101     bool doRunning();
102     bool doWaitForStreamDisable();
103
104     bool scheduleStateTransition(enum eProcessorState state, uint64_t time_instant);
105     bool waitForState(enum eProcessorState state, unsigned int timeout);
106
107 public: //--- state stuff
108     bool isRunning()
109             {return m_state == ePS_Running;};
110     bool isDryRunning()
111             {return m_state == ePS_DryRunning;};
112     bool isStopped()
113             {return m_state == ePS_Stopped;};
114
115     // these schedule and wait for the state transition
116     bool startDryRunning(int64_t time_to_start_at);
117     bool startRunning(int64_t time_to_start_at);
118     bool stopDryRunning(int64_t time_to_stop_at);
119     bool stopRunning(int64_t time_to_stop_at);
120
121     // these only schedule the transition
122     bool scheduleStartDryRunning(int64_t time_to_start_at);
123     bool scheduleStartRunning(int64_t time_to_start_at);
124     bool scheduleStopDryRunning(int64_t time_to_stop_at);
125     bool scheduleStopRunning(int64_t time_to_stop_at);
126
127     // the main difference between init and prepare is that when prepare is called,
128     // the SP is registered to a manager (FIXME: can't it be called by the manager?)
129     bool init();
130     bool prepare();
131
132 public: // constructor/destructor
133     StreamProcessor(enum eProcessorType type, int port);
134     virtual ~StreamProcessor();
135
136 public: // the public receive/transmit functions
137     // the transmit interface accepts frames and provides packets
138     // implement these for a transmit SP
139     // leave default for a receive SP
140
141     // the receive interface accepts packets and provides frames
142     // these are implemented by the parent SP
143     enum raw1394_iso_disposition
144         putPacket(unsigned char *data, unsigned int length,
145                   unsigned char channel, unsigned char tag, unsigned char sy,
146                   unsigned int cycle, unsigned int dropped);
147
148     enum raw1394_iso_disposition
149     getPacket(unsigned char *data, unsigned int *length,
150                 unsigned char *tag, unsigned char *sy,
151                 int cycle, unsigned int dropped, unsigned int max_length);
152
153     bool getFrames(unsigned int nbframes, int64_t ts); ///< transfer the buffer contents to the client
154     bool putFrames(unsigned int nbframes, int64_t ts); ///< transfer the client contents to the buffer
155
156 protected: // the helper receive/transmit functions
157     // to be implemented by the children
158     // the following methods are to be implemented by receive SP subclasses
159     virtual bool processPacketHeader(unsigned char *data, unsigned int length,
160                                      unsigned char channel, unsigned char tag,
161                                      unsigned char sy, unsigned int cycle,
162                                      unsigned int dropped)
163         {debugWarning("call not allowed\n"); return false;};
164     virtual bool processPacketData(unsigned char *data, unsigned int length,
165                                    unsigned char channel, unsigned char tag,
166                                    unsigned char sy, unsigned int cycle,
167                                    unsigned int dropped)
168         {debugWarning("call not allowed\n"); return false;};
169     virtual bool processReadBlock(char *data, unsigned int nevents, unsigned int offset)
170         {debugWarning("call not allowed\n"); return false;};
171     virtual bool provideSilenceBlock(unsigned int nevents, unsigned int offset)
172         {debugWarning("call not allowed\n"); return false;};
173
174     // the following methods are to be implemented by transmit SP subclasses
175     virtual bool generatePacketHeader(unsigned char *data, unsigned int *length,
176                                       unsigned char *tag, unsigned char *sy,
177                                       int cycle, unsigned int dropped,
178                                       unsigned int max_length)
179         {debugWarning("call not allowed\n"); return false;};
180     virtual bool generatePacketData(unsigned char *data, unsigned int *length,
181                                     unsigned char *tag, unsigned char *sy,
182                                     int cycle, unsigned int dropped,
183                                     unsigned int max_length)
184         {debugWarning("call not allowed\n"); return false;};
185     virtual bool generateSilentPacketHeader(unsigned char *data, unsigned int *length,
186                                             unsigned char *tag, unsigned char *sy,
187                                             int cycle, unsigned int dropped,
188                                             unsigned int max_length)
189         {debugWarning("call not allowed\n"); return false;};
190     virtual bool generateSilentPacketData(unsigned char *data, unsigned int *length,
191                                           unsigned char *tag, unsigned char *sy,
192                                           int cycle, unsigned int dropped,
193                                           unsigned int max_length)
194         {debugWarning("call not allowed\n"); return false;};
195     virtual bool processWriteBlock(char *data, unsigned int nevents, unsigned int offset)
196         {debugWarning("call not allowed\n"); return false;};
197     virtual bool transmitSilenceBlock(char *data, unsigned int nevents, unsigned int offset)
198         {debugWarning("call not allowed\n"); return false;};
199
200 private:
201     bool getFramesDry(unsigned int nbframes, int64_t ts);
202     bool getFramesWet(unsigned int nbframes, int64_t ts);
203     bool putFramesDry(unsigned int nbframes, int64_t ts);
204     bool putFramesWet(unsigned int nbframes, int64_t ts);
205
206     bool transferSilence(unsigned int size);
207
208     // move to private?
209     bool xrunOccurred() { return (m_xruns>0); }; // FIXME: m_xruns not updated
210
211 protected: // FIXME: move to private
212     uint64_t m_dropped; /// FIXME:debug
213     uint64_t m_last_dropped; /// FIXME:debug
214     int m_last_good_cycle; /// FIXME:debug
215     uint64_t m_last_timestamp; /// last timestamp (in ticks)
216     uint64_t m_last_timestamp2; /// last timestamp (in ticks)
217     uint64_t m_last_timestamp_at_period_ticks;
218
219 //--- data buffering and accounting
220 public:
221     void getBufferHeadTimestamp ( ffado_timestamp_t *ts, signed int *fc )
222         {m_data_buffer->getBufferHeadTimestamp(ts, fc);};
223     void getBufferTailTimestamp ( ffado_timestamp_t *ts, signed int *fc )
224         {m_data_buffer->getBufferTailTimestamp(ts, fc);};
225
226     void setBufferTailTimestamp ( ffado_timestamp_t new_timestamp )
227         {m_data_buffer->setBufferTailTimestamp(new_timestamp);};
228     void setBufferHeadTimestamp ( ffado_timestamp_t new_timestamp )
229         {m_data_buffer->setBufferHeadTimestamp(new_timestamp);};
230 protected:
231     Util::TimestampedBuffer *m_data_buffer;
232
233 protected:
234     unsigned int m_xruns;
235
236     StreamProcessorManager *m_manager;
237
238     // frame counter & sync stuff
239     public:
240         /**
241          * @brief Can this StreamProcessor handle a transfer of nframes frames?
242          *
243          * this function indicates if the streamprocessor can handle a transfer of
244          * nframes frames. It is used to detect underruns-to-be.
245          *
246          * @param nframes number of frames
247          * @return true if the StreamProcessor can handle this amount of frames
248          *         false if it can't
249          */
250         bool canClientTransferFrames(unsigned int nframes);
251
252         /**
253          * @brief drop nframes from the internal buffer
254          *
255          * this function drops nframes from the internal buffers, without any
256          * specification on what frames are dropped. Timestamps are not updated.
257          *
258          * @param nframes number of frames
259          * @return true if the operation was successful
260          */
261         bool dropFrames(unsigned int nframes);
262
263         /**
264          * \brief return the time until the next period boundary should be signaled (in microseconds)
265          *
266          * Return the time until the next period boundary signal. If this StreamProcessor
267          * is the current synchronization source, this function is called to
268          * determine when a buffer transfer can be made. When this value is
269          * smaller than 0, a period boundary is assumed to be crossed, hence a
270          * transfer can be made.
271          *
272          * \return the time in usecs
273          */
274         int64_t getTimeUntilNextPeriodSignalUsecs();
275         /**
276          * \brief return the time of the next period boundary (in microseconds)
277          *
278          * Returns the time of the next period boundary, in microseconds. The
279          * goal of this function is to determine the exact point of the period
280          * boundary. This is assumed to be the point at which the buffer transfer should
281          * take place, meaning that it can be used as a reference timestamp for transmitting
282          * StreamProcessors
283          *
284          * \return the time in usecs
285          */
286         uint64_t getTimeAtPeriodUsecs();
287
288         /**
289          * \brief return the time of the next period boundary (in internal units)
290          *
291          * The same as getTimeAtPeriodUsecs() but in internal units.
292          *
293          * @return the time in internal units
294          */
295         uint64_t getTimeAtPeriod();
296
297         uint64_t getTimeNow(); // FIXME: should disappear
298
299
300         /**
301          * Returns the sync delay. This is the time a syncsource
302          * delays a period signal, e.g. to cope with buffering.
303          * @return the sync delay
304          */
305         int getSyncDelay() {return m_sync_delay;};
306         /**
307          * sets the sync delay
308          * @param d sync delay
309          */
310         void setSyncDelay(int d) {m_sync_delay = d;};
311
312         /**
313          * @brief get the maximal frame latency
314          *
315          * The maximum frame latency is the maximum time that will elapse
316          * between the frame being received by the 1394 stack, and the moment this
317          * frame is presented to the StreamProcessor.
318          *
319          * For transmit SP's this is the maximum time that a frame is requested by
320          * the handler ahead of the time the frame is intended to be transmitted.
321          *
322          * This is useful to figure out how longer than the actual reception time
323          * we have to wait before trying to read the frame from the SP.
324          *
325          * @return maximal frame latency
326          */
327         int getMaxFrameLatency();
328
329         float getTicksPerFrame();
330
331         int getLastCycle() {return m_last_cycle;};
332
333         int getBufferFill();
334
335         // Child implementation interface
336         /**
337         * @brief prepare the child SP
338         * @return true if successful, false otherwise
339         * @pre the m_manager pointer points to a valid manager
340         * @post getEventsPerFrame() returns the correct value
341         * @post getEventSize() returns the correct value
342         * @post getUpdatePeriod() returns the correct value
343         * @post processPacketHeader(...) can be called
344         * @post processPacketData(...) can be called
345         */
346         virtual bool prepareChild() = 0;
347         /**
348          * @brief get the number of events contained in one frame
349          * @return the number of events contained in one frame
350          */
351         virtual unsigned int getEventsPerFrame() = 0;
352
353         /**
354          * @brief get the size of one frame in bytes
355          * @return the size of one frame in bytes
356          */
357         virtual unsigned int getEventSize() = 0;
358
359         /**
360          * @brief get the nominal number of frames in a packet
361          * @return the nominal number of frames in a packet
362          */
363         virtual unsigned int getNominalFramesPerPacket() = 0;
364
365     protected:
366         float m_ticks_per_frame;
367         int m_last_cycle;
368         int m_sync_delay;
369
370 protected: // SPM related
371     void setManager(StreamProcessorManager *manager) {m_manager=manager;};
372     void clearManager() {m_manager=NULL;};
373
374 public:
375     // debug stuff
376     virtual void dumpInfo();
377     virtual void setVerboseLevel(int l);
378     StreamStatistics m_PacketStat;
379     StreamStatistics m_PeriodStat;
380     StreamStatistics m_WakeupStat;
381     DECLARE_DEBUG_MODULE;
382 };
383
384 }
385
386 #endif /* __FFADO_STREAMPROCESSOR__ */
387
388
Note: See TracBrowser for help on using the browser.