1 /*
2   Copyright (C) 2009-2010 Grame
3 
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as published by
6   the Free Software Foundation; either version 2.1 of the License, or
7   (at your option) any later version.
8 
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   GNU Lesser General Public License for more details.
13 
14   You should have received a copy of the GNU Lesser General Public License
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 
18 */
19 
20 module jack.c.net;
21 public import jack.c.systemdeps;
22 public import jack.c.types;
23 
24 extern (C)
25 {
26 
27 enum DEFAULT_MULTICAST_IP = "225.3.19.154";
28 enum DEFAULT_PORT         = 19000;
29 enum DEFAULT_MTU          = 1500;
30 enum MASTER_NAME_SIZE     = 256;
31 
32 enum SOCKET_ERROR = -1;
33 
34 enum JackNetEncoder {
35     JackFloatEncoder = 0,   // samples are transmitted as float
36     JackIntEncoder = 1,     // samples are transmitted as 16 bits integer
37     JackCeltEncoder = 2,    // samples are transmitted using CELT codec (http://www.celt-codec.org/)
38     JackOpusEncoder = 3,    // samples are transmitted using OPUS codec (http://www.opus-codec.org/)
39 };
40 
41 struct jack_slave_t {
42     int audio_input;    // from master or to slave (-1 to take master audio physical inputs)
43     int audio_output;   // to master or from slave (-1 to take master audio physical outputs)
44     int midi_input;     // from master or to slave (-1 to take master MIDI physical inputs)
45     int midi_output;    // to master or from slave (-1 to take master MIDI physical outputs)
46     int mtu;            // network Maximum Transmission Unit
47     int time_out;       // in second, -1 means in infinite
48     int encoder;        // encoder type (one of JackNetEncoder)
49     int kbps;           // KB per second for CELT encoder
50     int latency;        // network latency
51 
52 };
53 
54 struct jack_master_t {
55     int audio_input;                    // master audio physical outputs (-1 to take slave wanted audio inputs)
56     int audio_output;                   // master audio physical inputs (-1 to take slave wanted audio outputs)
57     int midi_input;                     // master MIDI physical outputs (-1 to take slave wanted MIDI inputs)
58     int midi_output;                    // master MIDI physical inputs (-1 to take slave wanted MIDI outputs)
59     jack_nframes_t buffer_size;         // mater buffer size
60     jack_nframes_t sample_rate;         // mater sample rate
61     char[MASTER_NAME_SIZE] master_name; // master machine name
62 };
63 
64 /**
65  *  jack_net_slave_t is an opaque type. You may only access it using the
66  *  API provided.
67  */
68 struct jack_net_slave_t;
69 
70  /**
71  * Open a network connection with the master machine.
72  * @param ip the multicast address of the master
73  * @param port the connection port
74  * @param request a connection request structure
75  * @param result a connection result structure
76  *
77  * @return Opaque net handle if successful or NULL in case of error.
78  */
79 jack_net_slave_t* jack_net_slave_open(const(char)* ip, int port, const(char)* name, jack_slave_t* request, jack_master_t* result);
80 
81 /**
82  * Close the network connection with the master machine.
83  * @param net the network connection to be closed
84  *
85  * @return 0 on success, otherwise a non-zero error code
86  */
87 int jack_net_slave_close(jack_net_slave_t* net);
88 
89 /**
90  * Prototype for Process callback.
91  * @param nframes buffer size
92  * @param audio_input number of audio inputs
93  * @param audio_input_buffer an array of audio input buffers (from master)
94  * @param midi_input number of MIDI inputs
95  * @param midi_input_buffer an array of MIDI input buffers (from master)
96  * @param audio_output number of audio outputs
97  * @param audio_output_buffer an array of audio output buffers (to master)
98  * @param midi_output number of MIDI outputs
99  * @param midi_output_buffer an array of MIDI output buffers (to master)
100  * @param arg pointer to a client supplied structure supplied by jack_set_net_process_callback()
101  *
102  * @return zero on success, non-zero on error
103  */
104 alias JackNetSlaveProcessCallback = int function(jack_nframes_t buffer_size,
105                                                  int audio_input,
106                                                  float** audio_input_buffer,
107                                                  int midi_input,
108                                                  void** midi_input_buffer,
109                                                  int audio_output,
110                                                  float** audio_output_buffer,
111                                                  int midi_output,
112                                                  void** midi_output_buffer,
113                                                  void* data);
114 
115 /**
116  * Set network process callback.
117  * @param net the network connection
118  * @param net_callback the process callback
119  * @param arg pointer to a client supplied structure
120  *
121  * @return 0 on success, otherwise a non-zero error code
122  */
123 int jack_set_net_slave_process_callback(jack_net_slave_t * net, JackNetSlaveProcessCallback net_callback, void *arg);
124 
125 /**
126  * Start processing thread, the net_callback will start to be called.
127  * @param net the network connection
128  *
129  * @return 0 on success, otherwise a non-zero error code
130  */
131 int jack_net_slave_activate(jack_net_slave_t* net);
132 
133 /**
134  * Stop processing thread.
135  * @param net the network connection
136  *
137  * @return 0 on success, otherwise a non-zero error code
138  */
139 int jack_net_slave_deactivate(jack_net_slave_t* net);
140 
141 /**
142  * Prototype for BufferSize callback.
143  * @param nframes buffer size
144  * @param arg pointer to a client supplied structure supplied by jack_set_net_buffer_size_callback()
145  *
146  * @return zero on success, non-zero on error
147  */
148 alias JackNetSlaveBufferSizeCallback = int function(jack_nframes_t nframes, void *arg);
149 
150 /**
151  * Prototype for SampleRate callback.
152  * @param nframes sample rate
153  * @param arg pointer to a client supplied structure supplied by jack_set_net_sample_rate_callback()
154  *
155  * @return zero on success, non-zero on error
156  */
157 alias JackNetSlaveSampleRateCallback = int function(jack_nframes_t nframes, void *arg);
158 
159 /**
160  * Set network buffer size callback.
161  * @param net the network connection
162  * @param bufsize_callback the buffer size callback
163  * @param arg pointer to a client supplied structure
164  *
165  * @return 0 on success, otherwise a non-zero error code
166  */
167 int jack_set_net_slave_buffer_size_callback(jack_net_slave_t *net, JackNetSlaveBufferSizeCallback bufsize_callback, void *arg);
168 
169 /**
170  * Set network sample rate callback.
171  * @param net the network connection
172  * @param samplerate_callback the sample rate callback
173  * @param arg pointer to a client supplied structure
174  *
175  * @return 0 on success, otherwise a non-zero error code
176  */
177 int jack_set_net_slave_sample_rate_callback(jack_net_slave_t *net, JackNetSlaveSampleRateCallback samplerate_callback, void *arg);
178 
179 /**
180  * Prototype for server Shutdown callback (if not set, the client will just restart, waiting for an available master again).
181  * @param arg pointer to a client supplied structure supplied by jack_set_net_shutdown_callback()
182  */
183 alias JackNetSlaveShutdownCallback = void function(void* data);
184 
185 /**
186  * Set network shutdown callback.
187  * @param net the network connection
188  * @param shutdown_callback the shutdown callback
189  * @param arg pointer to a client supplied structure
190  *
191  * @return 0 on success, otherwise a non-zero error code
192  */
193 int jack_set_net_slave_shutdown_callback(jack_net_slave_t *net, JackNetSlaveShutdownCallback shutdown_callback, void *arg);
194 
195 /**
196  *  jack_net_master_t is an opaque type, you may only access it using the API provided.
197  */
198 struct jack_net_master_t;
199 
200  /**
201  * Open a network connection with the slave machine.
202  * @param ip the multicast address of the master
203  * @param port the connection port
204  * @param request a connection request structure
205  * @param result a connection result structure
206  *
207  * @return Opaque net handle if successful or NULL in case of error.
208  */
209 jack_net_master_t* jack_net_master_open(const(char)* ip, int port, const(char)* name, jack_master_t* request, jack_slave_t* result);
210 
211 /**
212  * Close the network connection with the slave machine.
213  * @param net the network connection to be closed
214  *
215  * @return 0 on success, otherwise a non-zero error code
216  */
217 int jack_net_master_close(jack_net_master_t* net);
218 
219 /**
220  * Receive sync and data from the network.
221  * @param net the network connection
222  * @param audio_input number of audio inputs
223  * @param audio_input_buffer an array of audio input buffers
224  * @param midi_input number of MIDI inputs
225  * @param midi_input_buffer an array of MIDI input buffers
226  *
227  * @return zero on success, non-zero on error
228  */
229 int jack_net_master_recv(jack_net_master_t* net, int audio_input, float** audio_input_buffer, int midi_input, void** midi_input_buffer);
230 
231 /**
232  * Send sync and data to the network.
233  * @param net the network connection
234  * @param audio_output number of audio outputs
235  * @param audio_output_buffer an array of audio output buffers
236  * @param midi_output number of MIDI ouputs
237  * @param midi_output_buffer an array of MIDI output buffers
238  *
239  * @return zero on success, non-zero on error
240  */
241 int jack_net_master_send(jack_net_master_t* net, int audio_output, float** audio_output_buffer, int midi_output, void** midi_output_buffer);
242 
243 // Experimental Adapter API
244 
245 /**
246  *  jack_adapter_t is an opaque type, you may only access it using the API provided.
247  */
248 struct jack_adapter_t;
249 
250 /**
251  * Create an adapter.
252  * @param input number of audio inputs
253  * @param output of audio outputs
254  * @param host_buffer_size the host buffer size in frames
255  * @param host_sample_rate the host buffer sample rate
256  * @param adapted_buffer_size the adapted buffer size in frames
257  * @param adapted_sample_rate the adapted buffer sample rate
258  *
259  * @return 0 on success, otherwise a non-zero error code
260  */
261 jack_adapter_t* jack_create_adapter(int input, int output,
262                                     jack_nframes_t host_buffer_size,
263                                     jack_nframes_t host_sample_rate,
264                                     jack_nframes_t adapted_buffer_size,
265                                     jack_nframes_t adapted_sample_rate);
266 
267 /**
268  * Destroy an adapter.
269  * @param adapter the adapter to be destroyed
270  *
271  * @return 0 on success, otherwise a non-zero error code
272  */
273 int jack_destroy_adapter(jack_adapter_t* adapter);
274 
275 /**
276  * Flush internal state of an adapter.
277  * @param adapter the adapter to be flushed
278  *
279  * @return 0 on success, otherwise a non-zero error code
280  */
281 void jack_flush_adapter(jack_adapter_t* adapter);
282 
283 /**
284  * Push input to and pull output from adapter ringbuffer.
285  * @param adapter the adapter
286  * @param input an array of audio input buffers
287  * @param output an array of audio ouput buffers
288  * @param frames number of frames
289  *
290  * @return 0 on success, otherwise a non-zero error code
291  */
292 int jack_adapter_push_and_pull(jack_adapter_t* adapter, float** input, float** output, uint frames);
293 
294 /**
295  * Pull input to and push output from adapter ringbuffer.
296  * @param adapter the adapter
297  * @param input an array of audio input buffers
298  * @param output an array of audio ouput buffers
299  * @param frames number of frames
300  *
301  * @return 0 on success, otherwise a non-zero error code
302  */
303 int jack_adapter_pull_and_push(jack_adapter_t* adapter, float** input, float** output, uint frames);
304 
305 }