root/branches/ppalmers-streaming/src/libstreaming/amdtp/AmdtpStreamProcessor.h

Revision 705, 7.0 kB (checked in by ppalmers, 15 years ago)

restructure the streaming directory

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