jack2 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

401 lines
15KB

  1. /*
  2. Copyright (C) 2009-2010 Grame
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as published by
  5. the Free Software Foundation; either version 2.1 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. */
  15. #ifndef __net_h__
  16. #define __net_h__
  17. #ifdef __cplusplus
  18. extern "C"
  19. {
  20. #endif
  21. #include <jack/systemdeps.h>
  22. #include <jack/types.h>
  23. #include <jack/weakmacros.h>
  24. #define DEFAULT_MULTICAST_IP "225.3.19.154"
  25. #define DEFAULT_PORT 19000
  26. #define DEFAULT_MTU 1500
  27. #define MASTER_NAME_SIZE 256
  28. // Possible error codes
  29. #define NO_ERROR 0
  30. #define SOCKET_ERROR -1
  31. #define SYNC_PACKET_ERROR -2
  32. #define DATA_PACKET_ERROR -3
  33. #define RESTART_CB_API 1
  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. typedef struct {
  41. int audio_input; // from master or to slave (-1 to take master audio physical inputs)
  42. int audio_output; // to master or from slave (-1 to take master audio physical outputs)
  43. int midi_input; // from master or to slave (-1 to take master MIDI physical inputs)
  44. int midi_output; // to master or from slave (-1 to take master MIDI physical outputs)
  45. int mtu; // network Maximum Transmission Unit
  46. int time_out; // in second, -1 means infinite
  47. int encoder; // encoder type (one of JackNetEncoder)
  48. int kbps; // KB per second for CELT encoder
  49. int latency; // network latency
  50. } jack_slave_t;
  51. typedef struct {
  52. int audio_input; // master audio physical outputs (-1 to take slave wanted audio inputs)
  53. int audio_output; // master audio physical inputs (-1 to take slave wanted audio outputs)
  54. int midi_input; // master MIDI physical outputs (-1 to take slave wanted MIDI inputs)
  55. int midi_output; // master MIDI physical inputs (-1 to take slave wanted MIDI outputs)
  56. jack_nframes_t buffer_size; // master buffer size
  57. jack_nframes_t sample_rate; // master sample rate
  58. char master_name[MASTER_NAME_SIZE]; // master machine name
  59. int time_out; // in second, -1 means infinite
  60. int partial_cycle; // if 'true', partial cycle will be used
  61. } jack_master_t;
  62. /**
  63. * jack_net_slave_t is an opaque type. You may only access it using the
  64. * API provided.
  65. */
  66. typedef struct _jack_net_slave jack_net_slave_t;
  67. /**
  68. * Open a network connection with the master machine.
  69. * @param ip the multicast address of the master
  70. * @param port the connection port
  71. * @param request a connection request structure
  72. * @param result a connection result structure
  73. *
  74. * @return Opaque net handle if successful or NULL in case of error.
  75. */
  76. jack_net_slave_t* jack_net_slave_open(const char* ip, int port, const char* name, jack_slave_t* request, jack_master_t* result);
  77. /**
  78. * Close the network connection with the master machine.
  79. * @param net the network connection to be closed
  80. *
  81. * @return 0 on success, otherwise a non-zero error code
  82. */
  83. int jack_net_slave_close(jack_net_slave_t* net);
  84. /**
  85. * Prototype for Process callback.
  86. * @param nframes buffer size
  87. * @param audio_input number of audio inputs
  88. * @param audio_input_buffer an array of audio input buffers (from master)
  89. * @param midi_input number of MIDI inputs
  90. * @param midi_input_buffer an array of MIDI input buffers (from master)
  91. * @param audio_output number of audio outputs
  92. * @param audio_output_buffer an array of audio output buffers (to master)
  93. * @param midi_output number of MIDI outputs
  94. * @param midi_output_buffer an array of MIDI output buffers (to master)
  95. * @param arg pointer to a client supplied structure supplied by jack_set_net_process_callback()
  96. *
  97. * @return zero on success, non-zero on error
  98. */
  99. typedef int (* JackNetSlaveProcessCallback) (jack_nframes_t buffer_size,
  100. int audio_input,
  101. float** audio_input_buffer,
  102. int midi_input,
  103. void** midi_input_buffer,
  104. int audio_output,
  105. float** audio_output_buffer,
  106. int midi_output,
  107. void** midi_output_buffer,
  108. void* data);
  109. /**
  110. * Set network process callback.
  111. * @param net the network connection
  112. * @param net_callback the process callback
  113. * @param arg pointer to a client supplied structure
  114. *
  115. * @return 0 on success, otherwise a non-zero error code
  116. */
  117. int jack_set_net_slave_process_callback(jack_net_slave_t * net, JackNetSlaveProcessCallback net_callback, void *arg);
  118. /**
  119. * Start processing thread, the net_callback will start to be called.
  120. * @param net the network connection
  121. *
  122. * @return 0 on success, otherwise a non-zero error code
  123. */
  124. int jack_net_slave_activate(jack_net_slave_t* net);
  125. /**
  126. * Stop processing thread.
  127. * @param net the network connection
  128. *
  129. * @return 0 on success, otherwise a non-zero error code
  130. */
  131. int jack_net_slave_deactivate(jack_net_slave_t* net);
  132. /**
  133. * Test if slave is still active.
  134. * @param net the network connection
  135. *
  136. * @return a boolean
  137. */
  138. int jack_net_slave_is_active(jack_net_slave_t* net);
  139. /**
  140. * Prototype for BufferSize callback.
  141. * @param nframes buffer size
  142. * @param arg pointer to a client supplied structure supplied by jack_set_net_buffer_size_callback()
  143. *
  144. * @return zero on success, non-zero on error
  145. */
  146. typedef int (*JackNetSlaveBufferSizeCallback)(jack_nframes_t nframes, void *arg);
  147. /**
  148. * Set network buffer size callback.
  149. * @param net the network connection
  150. * @param bufsize_callback the buffer size callback
  151. * @param arg pointer to a client supplied structure
  152. *
  153. * @return 0 on success, otherwise a non-zero error code
  154. */
  155. int jack_set_net_slave_buffer_size_callback(jack_net_slave_t *net, JackNetSlaveBufferSizeCallback bufsize_callback, void *arg);
  156. /**
  157. * Prototype for SampleRate callback.
  158. * @param nframes sample rate
  159. * @param arg pointer to a client supplied structure supplied by jack_set_net_sample_rate_callback()
  160. *
  161. * @return zero on success, non-zero on error
  162. */
  163. typedef int (*JackNetSlaveSampleRateCallback)(jack_nframes_t nframes, void *arg);
  164. /**
  165. * Set network sample rate callback.
  166. * @param net the network connection
  167. * @param samplerate_callback the sample rate callback
  168. * @param arg pointer to a client supplied structure
  169. *
  170. * @return 0 on success, otherwise a non-zero error code
  171. */
  172. int jack_set_net_slave_sample_rate_callback(jack_net_slave_t *net, JackNetSlaveSampleRateCallback samplerate_callback, void *arg);
  173. /**
  174. * Prototype for server Shutdown callback (if not set, the client will just restart, waiting for an available master again).
  175. * @param arg pointer to a client supplied structure supplied by jack_set_net_shutdown_callback()
  176. */
  177. typedef void (*JackNetSlaveShutdownCallback)(void* arg);
  178. /**
  179. * Set network shutdown callback.
  180. * @param net the network connection
  181. * @param shutdown_callback the shutdown callback
  182. * @param arg pointer to a client supplied structure
  183. *
  184. * @return 0 on success, otherwise a non-zero error code
  185. */
  186. int jack_set_net_slave_shutdown_callback(jack_net_slave_t *net, JackNetSlaveShutdownCallback shutdown_callback, void *arg) JACK_OPTIONAL_WEAK_DEPRECATED_EXPORT;
  187. /**
  188. * Prototype for server Restart callback : this is the new preferable way to be notified when the master has disappeared.
  189. * The client may want to retry connecting a certain number of time (which will be done using the time_out value given in jack_net_slave_open)
  190. * by returning 0. Otherwise returning a non-zero error code will definively close the connection
  191. * (and jack_net_slave_is_active will later on return false).
  192. * If both Shutdown and Restart are supplied, Restart callback will be used.
  193. * @param arg pointer to a client supplied structure supplied by jack_set_net_restart_callback()
  194. *
  195. * @return 0 on success, otherwise a non-zero error code
  196. */
  197. typedef int (*JackNetSlaveRestartCallback)(void* arg);
  198. /**
  199. * Set network restart callback.
  200. * @param net the network connection
  201. * @param restart_callback the shutdown callback
  202. * @param arg pointer to a client supplied structure
  203. *
  204. * @return 0 on success, otherwise a non-zero error code
  205. */
  206. int jack_set_net_slave_restart_callback(jack_net_slave_t *net, JackNetSlaveRestartCallback restart_callback, void *arg) JACK_OPTIONAL_WEAK_EXPORT;
  207. /**
  208. * Prototype for server Error callback.
  209. * @param error_code an error code (see "Possible error codes")
  210. * @param arg pointer to a client supplied structure supplied by jack_set_net_error_callback()
  211. */
  212. typedef void (*JackNetSlaveErrorCallback) (int error_code, void* arg);
  213. /**
  214. * Set error restart callback.
  215. * @param net the network connection
  216. * @param error_callback the error callback
  217. * @param arg pointer to a client supplied structure
  218. *
  219. * @return 0 on success, otherwise a non-zero error code
  220. */
  221. int jack_set_net_slave_error_callback(jack_net_slave_t *net, JackNetSlaveErrorCallback error_callback, void *arg) JACK_OPTIONAL_WEAK_EXPORT;
  222. /**
  223. * jack_net_master_t is an opaque type, you may only access it using the API provided.
  224. */
  225. typedef struct _jack_net_master jack_net_master_t;
  226. /**
  227. * Open a network connection with the slave machine.
  228. * @param ip the multicast address of the master
  229. * @param port the connection port
  230. * @param request a connection request structure
  231. * @param result a connection result structure
  232. *
  233. * @return Opaque net handle if successful or NULL in case of error.
  234. */
  235. jack_net_master_t* jack_net_master_open(const char* ip, int port, const char* name, jack_master_t* request, jack_slave_t* result);
  236. /**
  237. * Close the network connection with the slave machine.
  238. * @param net the network connection to be closed
  239. *
  240. * @return 0 on success, otherwise a non-zero error code
  241. */
  242. int jack_net_master_close(jack_net_master_t* net);
  243. /**
  244. * Receive sync and data from the network (complete buffer).
  245. * @param net the network connection
  246. * @param audio_input number of audio inputs
  247. * @param audio_input_buffer an array of audio input buffers
  248. * @param midi_input number of MIDI inputs
  249. * @param midi_input_buffer an array of MIDI input buffers
  250. *
  251. * @return zero on success, non-zero on error
  252. */
  253. int jack_net_master_recv(jack_net_master_t* net, int audio_input, float** audio_input_buffer, int midi_input, void** midi_input_buffer);
  254. /**
  255. * Receive sync and data from the network (incomplete buffer).
  256. * @param net the network connection
  257. * @param audio_input number of audio inputs
  258. * @param audio_input_buffer an array of audio input buffers
  259. * @param midi_input number of MIDI inputs
  260. * @param midi_input_buffer an array of MIDI input buffers
  261. * @param frames the number of frames to receive.
  262. *
  263. * @return zero on success, non-zero on error
  264. */
  265. int jack_net_master_recv_slice(jack_net_master_t* net, int audio_input, float** audio_input_buffer, int midi_input, void** midi_input_buffer, int frames);
  266. /**
  267. * Send sync and data to the network (complete buffer).
  268. * @param net the network connection
  269. * @param audio_output number of audio outputs
  270. * @param audio_output_buffer an array of audio output buffers
  271. * @param midi_output number of MIDI ouputs
  272. * @param midi_output_buffer an array of MIDI output buffers
  273. *
  274. * @return zero on success, non-zero on error
  275. */
  276. int jack_net_master_send(jack_net_master_t* net, int audio_output, float** audio_output_buffer, int midi_output, void** midi_output_buffer);
  277. /**
  278. * Send sync and data to the network (incomplete buffer).
  279. * @param net the network connection
  280. * @param audio_output number of audio outputs
  281. * @param audio_output_buffer an array of audio output buffers
  282. * @param midi_output number of MIDI ouputs
  283. * @param midi_output_buffer an array of MIDI output buffers
  284. * @param frames the number of frames to send.
  285. *
  286. * @return zero on success, non-zero on error
  287. */
  288. int jack_net_master_send_slice(jack_net_master_t* net, int audio_output, float** audio_output_buffer, int midi_output, void** midi_output_buffer, int frames);
  289. // Experimental Adapter API
  290. /**
  291. * jack_adapter_t is an opaque type, you may only access it using the API provided.
  292. */
  293. typedef struct _jack_adapter jack_adapter_t;
  294. /**
  295. * Create an adapter.
  296. * @param input number of audio inputs
  297. * @param output of audio outputs
  298. * @param host_buffer_size the host buffer size in frames
  299. * @param host_sample_rate the host buffer sample rate
  300. * @param adapted_buffer_size the adapted buffer size in frames
  301. * @param adapted_sample_rate the adapted buffer sample rate
  302. *
  303. * @return 0 on success, otherwise a non-zero error code
  304. */
  305. jack_adapter_t* jack_create_adapter(int input, int output,
  306. jack_nframes_t host_buffer_size,
  307. jack_nframes_t host_sample_rate,
  308. jack_nframes_t adapted_buffer_size,
  309. jack_nframes_t adapted_sample_rate);
  310. /**
  311. * Destroy an adapter.
  312. * @param adapter the adapter to be destroyed
  313. *
  314. * @return 0 on success, otherwise a non-zero error code
  315. */
  316. int jack_destroy_adapter(jack_adapter_t* adapter);
  317. /**
  318. * Flush internal state of an adapter.
  319. * @param adapter the adapter to be flushed
  320. *
  321. * @return 0 on success, otherwise a non-zero error code
  322. */
  323. void jack_flush_adapter(jack_adapter_t* adapter);
  324. /**
  325. * Push input to and pull output from adapter ringbuffer.
  326. * @param adapter the adapter
  327. * @param input an array of audio input buffers
  328. * @param output an array of audio ouput buffers
  329. * @param frames number of frames
  330. *
  331. * @return 0 on success, otherwise a non-zero error code
  332. */
  333. int jack_adapter_push_and_pull(jack_adapter_t* adapter, float** input, float** output, unsigned int frames);
  334. /**
  335. * Pull input to and push output from adapter ringbuffer.
  336. * @param adapter the adapter
  337. * @param input an array of audio input buffers
  338. * @param output an array of audio ouput buffers
  339. * @param frames number of frames
  340. *
  341. * @return 0 on success, otherwise a non-zero error code
  342. */
  343. int jack_adapter_pull_and_push(jack_adapter_t* adapter, float** input, float** output, unsigned int frames);
  344. #ifdef __cplusplus
  345. }
  346. #endif
  347. #endif /* __net_h__ */