root/branches/streaming-rework/src/libstreaming/IsoHandler.h

Revision 384, 7.6 kB (checked in by pieterpalmers, 16 years ago)

- temporary commit as backup measure
- rewrote synchronisation code
- receive streaming based on SYT works
- transmit streaming synced to received stream sort of works, still

have to iron out some issues.

NOTE: all devices but the bebob's are disabled in this code,

because they still have to be ported to the new sync
mechanism.

Line 
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) 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_ISOHANDLER__
29 #define __FREEBOB_ISOHANDLER__
30
31 #include "../debugmodule/debugmodule.h"
32
33 #include "libutil/TimeSource.h"
34
35 #include <libraw1394/raw1394.h>
36
37
38 enum raw1394_iso_disposition ;
39 namespace FreebobStreaming
40 {
41
42 class IsoStream;
43 /*!
44 \brief The Base Class for ISO Handlers
45
46  These classes perform the actual ISO communication through libraw1394.
47  They are different from IsoStreams because one handler can provide multiple
48  streams with packets in case of ISO multichannel receive.
49
50 */
51
52 class IsoHandler : public FreebobUtil::TimeSource
53 {
54     protected:
55    
56     public:
57        
58         enum EHandlerType {
59                 EHT_Receive,
60                 EHT_Transmit
61         };
62
63         IsoHandler(int port);
64
65         IsoHandler(int port, unsigned int buf_packets, unsigned int max_packet_size, int irq);
66
67         virtual ~IsoHandler();
68
69         virtual bool init();
70         virtual bool prepare();
71         virtual bool start(int cycle);
72         virtual bool stop();
73        
74         int iterate() { if(m_handle) return raw1394_loop_iterate(m_handle); else return -1; };
75        
76         void setVerboseLevel(int l);
77
78         // no setter functions, because those would require a re-init
79         unsigned int getMaxPacketSize() { return m_max_packet_size;};
80         unsigned int getBuffersize() { return m_buf_packets;};
81         int getWakeupInterval() { return m_irq_interval;};
82
83         int getPacketCount() {return m_packetcount;};
84         void resetPacketCount() {m_packetcount=0;};
85
86         int getDroppedCount() {return m_dropped;};
87         void resetDroppedCount() {m_dropped=0;};
88
89         virtual enum EHandlerType getType() = 0;
90
91         int getFileDescriptor() { return raw1394_get_fd(m_handle);};
92
93         void dumpInfo();
94
95         bool inUse() {return (m_Client != 0) ;};
96         virtual bool isStreamRegistered(IsoStream *s) {return (m_Client == s);};
97
98         virtual bool registerStream(IsoStream *);
99         virtual bool unregisterStream(IsoStream *);
100
101         int getLocalNodeId() {return raw1394_get_local_id( m_handle );};
102         int getPort() {return m_port;};
103
104         /// get the most recent cycle timer value (in ticks)
105         unsigned int getCycleTimerTicks();
106         /// get the most recent cycle timer value (as is)
107         unsigned int getCycleTimer();
108         /// Maps a value of the active TimeSource to a Cycle Timer value.
109         unsigned int mapToCycleTimer(freebob_microsecs_t now);
110         /// Maps a Cycle Timer value to the active TimeSource's unit.
111         freebob_microsecs_t mapToTimeSource(unsigned int cc);
112         /// update the cycle timer cache
113         bool updateCycleTimer();
114         float getTicksPerUsec() {return m_ticks_per_usec;};
115
116         // register a master timing source
117         bool setSyncMaster(FreebobUtil::TimeSource *t);
118    
119     protected:
120         raw1394handle_t m_handle;
121         raw1394handle_t m_handle_util;
122         int             m_port;
123         unsigned int    m_buf_packets;
124         unsigned int    m_max_packet_size;
125         int             m_irq_interval;
126        
127         uint64_t        m_cycletimer_ticks;
128         uint64_t m_lastmeas_usecs;
129         float               m_ticks_per_usec;
130         float               m_ticks_per_usec_dll_err2;
131        
132         int m_packetcount;
133         int m_dropped;
134
135         IsoStream *m_Client;
136
137         FreebobUtil::TimeSource *m_TimeSource;
138
139         virtual int handleBusReset(unsigned int generation);
140
141
142         DECLARE_DEBUG_MODULE;
143
144     private:
145         static int busreset_handler(raw1394handle_t handle, unsigned int generation);
146
147         void initCycleTimer();
148
149     // the state machine
150     private:
151         enum EHandlerStates {
152             E_Created,
153             E_Initialized,
154             E_Prepared,
155             E_Running,
156             E_Error
157         };
158        
159         enum EHandlerStates m_State;
160
161     // implement the TimeSource interface
162     public:
163         freebob_microsecs_t getCurrentTime();
164         freebob_microsecs_t getCurrentTimeAsUsecs();
165         inline freebob_microsecs_t unWrapTime(freebob_microsecs_t t);
166         inline freebob_microsecs_t wrapTime(freebob_microsecs_t t);
167        
168     private:
169         // to cope with wraparound
170         unsigned int m_TimeSource_LastSecs;
171         unsigned int m_TimeSource_NbCycleWraps;
172
173 };
174
175 /*!
176 \brief ISO receive handler class (not multichannel)
177 */
178
179 class IsoRecvHandler : public IsoHandler
180 {
181
182     public:
183         IsoRecvHandler(int port);
184         IsoRecvHandler(int port, unsigned int buf_packets, unsigned int max_packet_size, int irq);
185         virtual ~IsoRecvHandler();
186
187         bool init();
188    
189         enum EHandlerType getType() { return EHT_Receive;};
190
191         bool start(int cycle);
192
193         bool prepare();
194
195     protected:
196         int handleBusReset(unsigned int generation);
197
198     private:
199         static enum raw1394_iso_disposition
200         iso_receive_handler(raw1394handle_t handle, unsigned char *data,
201                             unsigned int length, unsigned char channel,
202                             unsigned char tag, unsigned char sy, unsigned int cycle,
203                             unsigned int dropped);
204
205         enum raw1394_iso_disposition 
206                 putPacket(unsigned char *data, unsigned int length,
207                           unsigned char channel, unsigned char tag, unsigned char sy,
208                           unsigned int cycle, unsigned int dropped);
209
210 };
211
212 /*!
213 \brief ISO transmit handler class
214 */
215
216 class IsoXmitHandler  : public IsoHandler
217 {
218     public:
219         IsoXmitHandler(int port);
220         IsoXmitHandler(int port, unsigned int buf_packets,
221                         unsigned int max_packet_size, int irq);
222         IsoXmitHandler(int port, unsigned int buf_packets,
223                         unsigned int max_packet_size, int irq,
224                         enum raw1394_iso_speed speed);
225         virtual ~IsoXmitHandler();
226
227         bool init();
228        
229         enum EHandlerType getType() { return EHT_Transmit;};
230
231         unsigned int getPreBuffers() {return m_prebuffers;};
232         void setPreBuffers(unsigned int n) {m_prebuffers=n;};
233
234         bool start(int cycle);
235
236         bool prepare();
237
238     protected:
239         int handleBusReset(unsigned int generation);
240
241     private:
242         static enum raw1394_iso_disposition iso_transmit_handler(raw1394handle_t handle,
243                         unsigned char *data, unsigned int *length,
244                         unsigned char *tag, unsigned char *sy,
245                         int cycle, unsigned int dropped);
246         enum raw1394_iso_disposition 
247                 getPacket(unsigned char *data, unsigned int *length,
248                         unsigned char *tag, unsigned char *sy,
249                         int cycle, unsigned int dropped);
250
251         enum raw1394_iso_speed m_speed;
252        
253         unsigned int m_prebuffers;
254
255 };
256
257 }
258
259 #endif /* __FREEBOB_ISOHANDLER__  */
260
261
262
Note: See TracBrowser for help on using the browser.