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

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

first working version of the reworked streaming code

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