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

Revision 384, 6.8 kB (checked in by pieterpalmers, 17 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) 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_AMDTPSTREAMPROCESSOR__
29 #define __FREEBOB_AMDTPSTREAMPROCESSOR__
30
31 /**
32  * This class implements IEC61883-6 / AM824 / AMDTP based streaming
33  */
34
35 #include "../debugmodule/debugmodule.h"
36 #include "StreamProcessor.h"
37 #include "cip.h"
38 #include <libiec61883/iec61883.h>
39 #include "ringbuffer.h"
40 #include <pthread.h>
41
42 #define AMDTP_MAX_PACKET_SIZE 2048
43
44 #define IEC61883_STREAM_TYPE_MIDI   0x0D
45 #define IEC61883_STREAM_TYPE_SPDIF  0x00
46 #define IEC61883_STREAM_TYPE_MBLA   0x06
47
48 #define IEC61883_AM824_LABEL_MASK                       0xFF000000
49 #define IEC61883_AM824_GET_LABEL(x)             (((x) & 0xFF000000) >> 24)
50 #define IEC61883_AM824_SET_LABEL(x,y)           ((x) | ((y)<<24))
51
52 #define IEC61883_AM824_LABEL_MIDI_NO_DATA       0x80
53 #define IEC61883_AM824_LABEL_MIDI_1X            0x81
54 #define IEC61883_AM824_LABEL_MIDI_2X            0x82
55 #define IEC61883_AM824_LABEL_MIDI_3X            0x83
56
57 namespace FreebobStreaming {
58
59 class Port;
60 class AmdtpAudioPort;
61 class AmdtpMidiPort;
62 class AmdtpReceiveStreamProcessor;
63
64 /*!
65 \brief The Base Class for an AMDTP transmit stream processor
66
67  This class implements a TransmitStreamProcessor that multiplexes Ports
68  into AMDTP streams.
69  
70 */
71 class AmdtpTransmitStreamProcessor
72         : public TransmitStreamProcessor
73 {
74
75 public:
76     // FIXME: debug
77     friend class AmdtpReceiveStreamProcessor;
78    
79     AmdtpTransmitStreamProcessor(int port, int framerate, int dimension);
80
81     virtual ~AmdtpTransmitStreamProcessor();
82
83     enum raw1394_iso_disposition
84             getPacket(unsigned char *data, unsigned int *length,
85                     unsigned char *tag, unsigned char *sy,
86                     int cycle, unsigned int dropped, unsigned int max_length);
87
88     bool init();
89     bool reset();
90     bool prepare();
91    
92     bool prepareForStop();
93     bool prepareForStart();
94    
95     bool prepareForEnable();
96    
97     bool canClientTransferFrames(unsigned int nbframes);
98     bool putFrames(unsigned int nbframes, int64_t ts); ///< transfer the buffer contents from the client
99
100     // We have 1 period of samples = m_period
101     // this period takes m_period/m_framerate seconds of time
102     // during this time, 8000 packets are sent
103 //      unsigned int getPacketsPerPeriod() {return (m_period*8000)/m_framerate;};
104    
105     // however, if we only count the number of used packets
106     // it is m_period / m_syt_interval
107     unsigned int getPacketsPerPeriod() {return (m_period)/m_syt_interval;};
108    
109     unsigned int getMaxPacketSize() {return 4 * (2 + m_syt_interval * m_dimension);};
110    
111     int64_t getTimeUntilNextPeriodUsecs();
112
113     uint64_t getTimeAtPeriodUsecs();
114     uint64_t getTimeAtPeriod();
115    
116     void setVerboseLevel(int l);
117    
118 protected:
119
120     struct iec61883_cip m_cip_status;
121
122     freebob_ringbuffer_t * m_event_buffer;
123     char* m_cluster_buffer;
124     int m_dimension;
125     unsigned int m_syt_interval;
126
127     int m_fdf;
128    
129     bool prefill();
130    
131     bool transferSilence(unsigned int size);
132
133     int transmitBlock(char *data, unsigned int nevents,
134                         unsigned int offset);
135                        
136     bool encodePacketPorts(quadlet_t *data, unsigned int nevents, unsigned int dbc);
137     int encodePortToMBLAEvents(AmdtpAudioPort *, quadlet_t *data,
138                                 unsigned int offset, unsigned int nevents);
139    
140     int transmitSilenceBlock(char *data, unsigned int nevents,
141                         unsigned int offset);
142     int encodeSilencePortToMBLAEvents(AmdtpAudioPort *, quadlet_t *data,
143                                 unsigned int offset, unsigned int nevents);
144
145     unsigned long m_last_timestamp;
146
147     unsigned int m_dbc;
148    
149     unsigned int m_ringbuffer_size_frames;
150
151     DECLARE_DEBUG_MODULE;
152
153 };
154 /*!
155 \brief The Base Class for an AMDTP receive stream processor
156
157  This class implements a ReceiveStreamProcessor that demultiplexes
158  AMDTP streams into Ports.
159  
160 */
161 class AmdtpReceiveStreamProcessor
162         : public ReceiveStreamProcessor
163 {
164
165 public:
166     // FIXME: debug
167     friend class AmdtpTransmitStreamProcessor;
168
169         AmdtpReceiveStreamProcessor(int port, int framerate, int dimension);
170
171         virtual ~AmdtpReceiveStreamProcessor();
172
173         enum raw1394_iso_disposition putPacket(unsigned char *data, unsigned int length,
174                       unsigned char channel, unsigned char tag, unsigned char sy,
175                           unsigned int cycle, unsigned int dropped);
176
177
178         bool init();
179         bool reset();
180         bool prepare();
181        
182     bool prepareForStop();
183     bool prepareForStart();
184        
185     bool canClientTransferFrames(unsigned int nbframes);
186     bool getFrames(unsigned int nbframes, int64_t ts); ///< transfer the buffer contents to the client
187
188     // We have 1 period of samples = m_period
189     // this period takes m_period/m_framerate seconds of time
190     // during this time, 8000 packets are sent
191 //      unsigned int getPacketsPerPeriod() {return (m_period*8000)/m_framerate;};
192    
193     // however, if we only count the number of used packets
194     // it is m_period / m_syt_interval
195         unsigned int getPacketsPerPeriod() {return (m_period)/m_syt_interval;};
196        
197         unsigned int getMaxPacketSize() {return 4 * (2 + m_syt_interval * m_dimension);};
198
199     void dumpInfo();
200    
201     int64_t getTimeUntilNextPeriodUsecs();
202
203     uint64_t getTimeAtPeriodUsecs();
204     uint64_t getTimeAtPeriod();
205        
206     void setVerboseLevel(int l);
207            
208 protected:
209
210     int receiveBlock(char *data, unsigned int nevents, unsigned int offset);
211     bool decodePacketPorts(quadlet_t *data, unsigned int nevents, unsigned int dbc);
212    
213     int decodeMBLAEventsToPort(AmdtpAudioPort *, quadlet_t *data, unsigned int offset, unsigned int nevents);
214
215     freebob_ringbuffer_t * m_event_buffer;
216     char* m_cluster_buffer;
217     int m_dimension;
218     unsigned int m_syt_interval;
219    
220     uint64_t m_last_timestamp; /// last timestamp (in ticks)
221     uint64_t m_last_timestamp2; /// last timestamp (in ticks)
222     uint64_t m_last_timestamp_at_period_ticks;
223    
224     DECLARE_DEBUG_MODULE;
225
226 };
227
228
229 } // end of namespace FreebobStreaming
230
231 #endif /* __FREEBOB_AMDTPSTREAMPROCESSOR__ */
232
233
Note: See TracBrowser for help on using the browser.