1 |
/* $Id$ */ |
---|
2 |
|
---|
3 |
/* |
---|
4 |
* FreeBob Streaming API |
---|
5 |
* FreeBob = Firewire (pro-)audio for linux |
---|
6 |
* |
---|
7 |
* http://freebob.sf.net |
---|
8 |
* |
---|
9 |
* Copyright (C) 2005,2006 Pieter Palmers <pieterpalmers@users.sourceforge.net> |
---|
10 |
* |
---|
11 |
* This program is free software {} you can redistribute it and/or modify |
---|
12 |
* it under the terms of the GNU General Public License as published by |
---|
13 |
* the Free Software Foundation {} either version 2 of the License, or |
---|
14 |
* (at your option) any later version. |
---|
15 |
* |
---|
16 |
* This program is distributed in the hope that it will be useful, |
---|
17 |
* but WITHOUT ANY WARRANTY {} without even the implied warranty of |
---|
18 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
19 |
* GNU General Public License for more details. |
---|
20 |
* |
---|
21 |
* You should have received a copy of the GNU General Public License |
---|
22 |
* along with this program {} if not, write to the Free Software |
---|
23 |
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
24 |
* |
---|
25 |
* |
---|
26 |
* |
---|
27 |
*/ |
---|
28 |
#ifndef __FREEBOB_STREAMPROCESSOR__ |
---|
29 |
#define __FREEBOB_STREAMPROCESSOR__ |
---|
30 |
|
---|
31 |
#include "../debugmodule/debugmodule.h" |
---|
32 |
|
---|
33 |
#include "IsoStream.h" |
---|
34 |
#include "PortManager.h" |
---|
35 |
|
---|
36 |
#include <pthread.h> |
---|
37 |
|
---|
38 |
#include "libutil/StreamStatistics.h" |
---|
39 |
|
---|
40 |
#include "libutil/TimestampedBuffer.h" |
---|
41 |
|
---|
42 |
namespace FreebobStreaming { |
---|
43 |
|
---|
44 |
class StreamProcessorManager; |
---|
45 |
|
---|
46 |
/*! |
---|
47 |
\brief Class providing a generic interface for Stream Processors |
---|
48 |
|
---|
49 |
A stream processor multiplexes or demultiplexes an ISO stream into a |
---|
50 |
collection of ports. This class should be subclassed, and the relevant |
---|
51 |
functions should be overloaded. |
---|
52 |
|
---|
53 |
*/ |
---|
54 |
class StreamProcessor : public IsoStream, |
---|
55 |
public PortManager, |
---|
56 |
public FreebobUtil::TimestampedBufferClient { |
---|
57 |
|
---|
58 |
friend class StreamProcessorManager; |
---|
59 |
|
---|
60 |
public: |
---|
61 |
enum EProcessorType { |
---|
62 |
E_Receive, |
---|
63 |
E_Transmit |
---|
64 |
}; |
---|
65 |
|
---|
66 |
StreamProcessor(enum IsoStream::EStreamType type, int port, int framerate); |
---|
67 |
virtual ~StreamProcessor(); |
---|
68 |
|
---|
69 |
virtual enum raw1394_iso_disposition |
---|
70 |
putPacket(unsigned char *data, unsigned int length, |
---|
71 |
unsigned char channel, unsigned char tag, unsigned char sy, |
---|
72 |
unsigned int cycle, unsigned int dropped) = 0; |
---|
73 |
virtual enum raw1394_iso_disposition |
---|
74 |
getPacket(unsigned char *data, unsigned int *length, |
---|
75 |
unsigned char *tag, unsigned char *sy, |
---|
76 |
int cycle, unsigned int dropped, unsigned int max_length) = 0; |
---|
77 |
|
---|
78 |
virtual enum EProcessorType getType() =0; |
---|
79 |
|
---|
80 |
bool xrunOccurred() { return (m_xruns>0);}; |
---|
81 |
|
---|
82 |
// move to private? |
---|
83 |
void resetXrunCounter(); |
---|
84 |
|
---|
85 |
bool isRunning(); ///< returns true if there is some stream data processed |
---|
86 |
|
---|
87 |
virtual bool prepareForEnable(uint64_t time_to_enable_at); |
---|
88 |
virtual bool prepareForDisable(); |
---|
89 |
|
---|
90 |
bool enable(uint64_t time_to_enable_at); ///< enable the stream processing |
---|
91 |
bool disable(); ///< disable the stream processing |
---|
92 |
bool isEnabled() {return !m_is_disabled;}; |
---|
93 |
|
---|
94 |
virtual bool putFrames(unsigned int nbframes, int64_t ts) = 0; ///< transfer the buffer contents from client |
---|
95 |
virtual bool getFrames(unsigned int nbframes) = 0; ///< transfer the buffer contents to the client |
---|
96 |
|
---|
97 |
virtual bool reset(); ///< reset the streams & buffers (e.g. after xrun) |
---|
98 |
|
---|
99 |
virtual bool prepare(); ///< prepare the streams & buffers (e.g. prefill) |
---|
100 |
|
---|
101 |
virtual void dumpInfo(); |
---|
102 |
|
---|
103 |
virtual bool init(); |
---|
104 |
|
---|
105 |
virtual void setVerboseLevel(int l); |
---|
106 |
|
---|
107 |
virtual bool prepareForStop() {return true;}; |
---|
108 |
virtual bool prepareForStart() {return true;}; |
---|
109 |
|
---|
110 |
|
---|
111 |
public: |
---|
112 |
FreebobUtil::TimestampedBuffer *m_data_buffer; |
---|
113 |
|
---|
114 |
protected: |
---|
115 |
|
---|
116 |
void setManager(StreamProcessorManager *manager) {m_manager=manager;}; |
---|
117 |
void clearManager() {m_manager=0;}; |
---|
118 |
|
---|
119 |
unsigned int m_nb_buffers; ///< cached from manager->getNbBuffers(), the number of periods to buffer |
---|
120 |
unsigned int m_period; ///< cached from manager->getPeriod(), the period size |
---|
121 |
|
---|
122 |
unsigned int m_xruns; |
---|
123 |
|
---|
124 |
unsigned int m_framerate; |
---|
125 |
|
---|
126 |
StreamProcessorManager *m_manager; |
---|
127 |
|
---|
128 |
bool m_running; |
---|
129 |
bool m_disabled; |
---|
130 |
bool m_is_disabled; |
---|
131 |
unsigned int m_cycle_to_enable_at; |
---|
132 |
|
---|
133 |
StreamStatistics m_PacketStat; |
---|
134 |
StreamStatistics m_PeriodStat; |
---|
135 |
|
---|
136 |
StreamStatistics m_WakeupStat; |
---|
137 |
|
---|
138 |
|
---|
139 |
DECLARE_DEBUG_MODULE; |
---|
140 |
|
---|
141 |
// frame counter & sync stuff |
---|
142 |
public: |
---|
143 |
/** |
---|
144 |
* @brief Can this StreamProcessor handle a transfer of nframes frames? |
---|
145 |
* |
---|
146 |
* this function indicates if the streamprocessor can handle a transfer of |
---|
147 |
* nframes frames. It is used to detect underruns-to-be. |
---|
148 |
* |
---|
149 |
* @param nframes number of frames |
---|
150 |
* @return true if the StreamProcessor can handle this amount of frames |
---|
151 |
* false if it can't |
---|
152 |
*/ |
---|
153 |
virtual bool canClientTransferFrames(unsigned int nframes) {return true;}; |
---|
154 |
|
---|
155 |
/** |
---|
156 |
* \brief return the time until the next period boundary (in microseconds) |
---|
157 |
* |
---|
158 |
* Return the time until the next period boundary. If this StreamProcessor |
---|
159 |
* is the current synchronization source, this function is called to |
---|
160 |
* determine when a buffer transfer can be made. When this value is |
---|
161 |
* smaller than 0, a period boundary is assumed to be crossed, hence a |
---|
162 |
* transfer can be made. |
---|
163 |
* |
---|
164 |
* \return the time in usecs |
---|
165 |
*/ |
---|
166 |
virtual int64_t getTimeUntilNextPeriodUsecs() = 0; |
---|
167 |
/** |
---|
168 |
* \brief return the time of the next period boundary (in microseconds) |
---|
169 |
* |
---|
170 |
* Returns the time of the next period boundary, in microseconds. The |
---|
171 |
* goal of this function is to determine the exact point of the period |
---|
172 |
* boundary. This is assumed to be the point at which the buffer transfer should |
---|
173 |
* take place, meaning that it can be used as a reference timestamp for transmitting |
---|
174 |
* StreamProcessors |
---|
175 |
* |
---|
176 |
* \return the time in usecs |
---|
177 |
*/ |
---|
178 |
virtual uint64_t getTimeAtPeriodUsecs() = 0; |
---|
179 |
|
---|
180 |
/** |
---|
181 |
* \brief return the time of the next period boundary (in internal units) |
---|
182 |
* |
---|
183 |
* The same as getTimeUntilNextPeriodUsecs() but in internal units. |
---|
184 |
* |
---|
185 |
* @return the time in internal units |
---|
186 |
*/ |
---|
187 |
virtual uint64_t getTimeAtPeriod() = 0; |
---|
188 |
|
---|
189 |
uint64_t getTimeNow(); |
---|
190 |
|
---|
191 |
bool setSyncSource(StreamProcessor *s); |
---|
192 |
float getTicksPerFrame() {return m_ticks_per_frame;}; |
---|
193 |
|
---|
194 |
int getLastCycle() {return m_last_cycle;}; |
---|
195 |
|
---|
196 |
protected: |
---|
197 |
StreamProcessor *m_SyncSource; |
---|
198 |
|
---|
199 |
float m_ticks_per_frame; |
---|
200 |
|
---|
201 |
int m_last_cycle; |
---|
202 |
|
---|
203 |
}; |
---|
204 |
|
---|
205 |
/*! |
---|
206 |
\brief Class providing a generic interface for receive Stream Processors |
---|
207 |
|
---|
208 |
*/ |
---|
209 |
class ReceiveStreamProcessor : public StreamProcessor { |
---|
210 |
|
---|
211 |
public: |
---|
212 |
ReceiveStreamProcessor(int port, int framerate); |
---|
213 |
|
---|
214 |
virtual ~ReceiveStreamProcessor(); |
---|
215 |
|
---|
216 |
|
---|
217 |
virtual enum EProcessorType getType() {return E_Receive;}; |
---|
218 |
|
---|
219 |
virtual enum raw1394_iso_disposition |
---|
220 |
getPacket(unsigned char *data, unsigned int *length, |
---|
221 |
unsigned char *tag, unsigned char *sy, |
---|
222 |
int cycle, unsigned int dropped, unsigned int max_length) |
---|
223 |
{return RAW1394_ISO_STOP;}; |
---|
224 |
virtual bool putFrames(unsigned int nbframes, int64_t ts) {return false;}; |
---|
225 |
|
---|
226 |
virtual enum raw1394_iso_disposition putPacket(unsigned char *data, unsigned int length, |
---|
227 |
unsigned char channel, unsigned char tag, unsigned char sy, |
---|
228 |
unsigned int cycle, unsigned int dropped) = 0; |
---|
229 |
virtual void setVerboseLevel(int l); |
---|
230 |
|
---|
231 |
protected: |
---|
232 |
bool processWriteBlock(char *data, unsigned int nevents, unsigned int offset) {return true;}; |
---|
233 |
|
---|
234 |
DECLARE_DEBUG_MODULE; |
---|
235 |
|
---|
236 |
}; |
---|
237 |
|
---|
238 |
/*! |
---|
239 |
\brief Class providing a generic interface for receive Stream Processors |
---|
240 |
|
---|
241 |
*/ |
---|
242 |
class TransmitStreamProcessor : public StreamProcessor { |
---|
243 |
|
---|
244 |
public: |
---|
245 |
TransmitStreamProcessor(int port, int framerate); |
---|
246 |
|
---|
247 |
virtual ~TransmitStreamProcessor(); |
---|
248 |
|
---|
249 |
virtual enum EProcessorType getType() {return E_Transmit;}; |
---|
250 |
|
---|
251 |
virtual enum raw1394_iso_disposition |
---|
252 |
putPacket(unsigned char *data, unsigned int length, |
---|
253 |
unsigned char channel, unsigned char tag, unsigned char sy, |
---|
254 |
unsigned int cycle, unsigned int dropped) {return RAW1394_ISO_STOP;}; |
---|
255 |
virtual bool getFrames(unsigned int nbframes) {return false;}; |
---|
256 |
|
---|
257 |
virtual enum raw1394_iso_disposition |
---|
258 |
getPacket(unsigned char *data, unsigned int *length, |
---|
259 |
unsigned char *tag, unsigned char *sy, |
---|
260 |
int cycle, unsigned int dropped, unsigned int max_length) = 0; |
---|
261 |
virtual void setVerboseLevel(int l); |
---|
262 |
|
---|
263 |
protected: |
---|
264 |
bool processReadBlock(char *data, unsigned int nevents, unsigned int offset) {return true;}; |
---|
265 |
|
---|
266 |
DECLARE_DEBUG_MODULE; |
---|
267 |
|
---|
268 |
|
---|
269 |
}; |
---|
270 |
|
---|
271 |
} |
---|
272 |
|
---|
273 |
#endif /* __FREEBOB_STREAMPROCESSOR__ */ |
---|
274 |
|
---|
275 |
|
---|