root/trunk/libffado/src/libstreaming/AmdtpStreamProcessor.h

Revision 494, 6.9 kB (checked in by ppalmers, 15 years ago)

- switch over to a generic ffado_timestamp_t for the timestamped buffer (currently float)
- implemented some experimental stream phase sync method

- various small things

NOTE: not a very stable commit

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