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