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.

429 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 or OPUS codec
  49. int latency; // network latency in number of buffers
  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 buffers 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. *
  70. * @param ip the multicast address of the master
  71. * @param port the connection port
  72. * @param request a connection request structure
  73. * @param result a connection result structure
  74. *
  75. * @return Opaque net handle if successful or NULL in case of error.
  76. */
  77. jack_net_slave_t* jack_net_slave_open(const char* ip, int port, const char* name, jack_slave_t* request, jack_master_t* result);
  78. /**
  79. * Close the network connection with the master machine.
  80. *
  81. * @param net the network connection to be closed
  82. *
  83. * @return 0 on success, otherwise a non-zero error code
  84. */
  85. int jack_net_slave_close(jack_net_slave_t* net);
  86. /**
  87. * Prototype for Process callback.
  88. *
  89. * @param nframes buffer size
  90. * @param audio_input number of audio inputs
  91. * @param audio_input_buffer an array of audio input buffers (from master)
  92. * @param midi_input number of MIDI inputs
  93. * @param midi_input_buffer an array of MIDI input buffers (from master)
  94. * @param audio_output number of audio outputs
  95. * @param audio_output_buffer an array of audio output buffers (to master)
  96. * @param midi_output number of MIDI outputs
  97. * @param midi_output_buffer an array of MIDI output buffers (to master)
  98. * @param arg pointer to a client supplied structure supplied by jack_set_net_process_callback()
  99. *
  100. * @return zero on success, non-zero on error
  101. */
  102. typedef int (* JackNetSlaveProcessCallback) (jack_nframes_t buffer_size,
  103. int audio_input,
  104. float** audio_input_buffer,
  105. int midi_input,
  106. void** midi_input_buffer,
  107. int audio_output,
  108. float** audio_output_buffer,
  109. int midi_output,
  110. void** midi_output_buffer,
  111. void* data);
  112. /**
  113. * Set network process callback.
  114. *
  115. * @param net the network connection
  116. * @param net_callback the process callback
  117. * @param arg pointer to a client supplied structure
  118. *
  119. * @return 0 on success, otherwise a non-zero error code
  120. */
  121. int jack_set_net_slave_process_callback(jack_net_slave_t * net, JackNetSlaveProcessCallback net_callback, void *arg);
  122. /**
  123. * Start processing thread, the net_callback will start to be called.
  124. *
  125. * @param net the network connection
  126. *
  127. * @return 0 on success, otherwise a non-zero error code
  128. */
  129. int jack_net_slave_activate(jack_net_slave_t* net);
  130. /**
  131. * Stop processing thread.
  132. *
  133. * @param net the network connection
  134. *
  135. * @return 0 on success, otherwise a non-zero error code
  136. */
  137. int jack_net_slave_deactivate(jack_net_slave_t* net);
  138. /**
  139. * Test if slave is still active.
  140. *
  141. * @param net the network connection
  142. *
  143. * @return a boolean
  144. */
  145. int jack_net_slave_is_active(jack_net_slave_t* net);
  146. /**
  147. * Prototype for BufferSize callback.
  148. *
  149. * @param nframes buffer size
  150. * @param arg pointer to a client supplied structure supplied by jack_set_net_buffer_size_callback()
  151. *
  152. * @return zero on success, non-zero on error
  153. */
  154. typedef int (*JackNetSlaveBufferSizeCallback)(jack_nframes_t nframes, void *arg);
  155. /**
  156. * Set network buffer size callback.
  157. *
  158. * @param net the network connection
  159. * @param bufsize_callback the buffer size callback
  160. * @param arg pointer to a client supplied structure
  161. *
  162. * @return 0 on success, otherwise a non-zero error code
  163. */
  164. int jack_set_net_slave_buffer_size_callback(jack_net_slave_t *net, JackNetSlaveBufferSizeCallback bufsize_callback, void *arg);
  165. /**
  166. * Prototype for SampleRate callback.
  167. *
  168. * @param nframes sample rate
  169. * @param arg pointer to a client supplied structure supplied by jack_set_net_sample_rate_callback()
  170. *
  171. * @return zero on success, non-zero on error
  172. */
  173. typedef int (*JackNetSlaveSampleRateCallback)(jack_nframes_t nframes, void *arg);
  174. /**
  175. * Set network sample rate callback.
  176. *
  177. * @param net the network connection
  178. * @param samplerate_callback the sample rate callback
  179. * @param arg pointer to a client supplied structure
  180. *
  181. * @return 0 on success, otherwise a non-zero error code
  182. */
  183. int jack_set_net_slave_sample_rate_callback(jack_net_slave_t *net, JackNetSlaveSampleRateCallback samplerate_callback, void *arg);
  184. /**
  185. * Prototype for server Shutdown callback (if not set, the client will just restart, waiting for an available master again).
  186. *
  187. * @param arg pointer to a client supplied structure supplied by jack_set_net_shutdown_callback()
  188. */
  189. typedef void (*JackNetSlaveShutdownCallback)(void* arg);
  190. /**
  191. * Set network shutdown callback.
  192. *
  193. * @param net the network connection
  194. * @param shutdown_callback the shutdown callback
  195. * @param arg pointer to a client supplied structure
  196. *
  197. * @return 0 on success, otherwise a non-zero error code
  198. */
  199. int jack_set_net_slave_shutdown_callback(jack_net_slave_t *net, JackNetSlaveShutdownCallback shutdown_callback, void *arg) JACK_OPTIONAL_WEAK_DEPRECATED_EXPORT;
  200. /**
  201. * Prototype for server Restart callback : this is the new preferable way to be notified when the master has disappeared.
  202. * 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)
  203. * by returning 0. Otherwise returning a non-zero error code will definively close the connection
  204. * (and jack_net_slave_is_active will later on return false).
  205. * If both Shutdown and Restart are supplied, Restart callback will be used.
  206. *
  207. * @param arg pointer to a client supplied structure supplied by jack_set_net_restart_callback()
  208. *
  209. * @return 0 on success, otherwise a non-zero error code
  210. */
  211. typedef int (*JackNetSlaveRestartCallback)(void* arg);
  212. /**
  213. * Set network restart callback.
  214. *
  215. * @param net the network connection
  216. * @param restart_callback the shutdown 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_restart_callback(jack_net_slave_t *net, JackNetSlaveRestartCallback restart_callback, void *arg) JACK_OPTIONAL_WEAK_EXPORT;
  222. /**
  223. * Prototype for server Error callback.
  224. *
  225. * @param error_code an error code (see "Possible error codes")
  226. * @param arg pointer to a client supplied structure supplied by jack_set_net_error_callback()
  227. */
  228. typedef void (*JackNetSlaveErrorCallback) (int error_code, void* arg);
  229. /**
  230. * Set error restart callback.
  231. *
  232. * @param net the network connection
  233. * @param error_callback the error callback
  234. * @param arg pointer to a client supplied structure
  235. *
  236. * @return 0 on success, otherwise a non-zero error code
  237. */
  238. int jack_set_net_slave_error_callback(jack_net_slave_t *net, JackNetSlaveErrorCallback error_callback, void *arg) JACK_OPTIONAL_WEAK_EXPORT;
  239. /**
  240. * jack_net_master_t is an opaque type, you may only access it using the API provided.
  241. */
  242. typedef struct _jack_net_master jack_net_master_t;
  243. /**
  244. * Open a network connection with the slave machine.
  245. *
  246. * @param ip the multicast address of the master
  247. * @param port the connection port
  248. * @param request a connection request structure
  249. * @param result a connection result structure
  250. *
  251. * @return Opaque net handle if successful or NULL in case of error.
  252. */
  253. jack_net_master_t* jack_net_master_open(const char* ip, int port, const char* name, jack_master_t* request, jack_slave_t* result);
  254. /**
  255. * Close the network connection with the slave machine.
  256. *
  257. * @param net the network connection to be closed
  258. *
  259. * @return 0 on success, otherwise a non-zero error code
  260. */
  261. int jack_net_master_close(jack_net_master_t* net);
  262. /**
  263. * Receive sync and data from the network (complete buffer).
  264. *
  265. * @param net the network connection
  266. * @param audio_input number of audio inputs
  267. * @param audio_input_buffer an array of audio input buffers
  268. * @param midi_input number of MIDI inputs
  269. * @param midi_input_buffer an array of MIDI input buffers
  270. *
  271. * @return zero on success, non-zero on error
  272. */
  273. int jack_net_master_recv(jack_net_master_t* net, int audio_input, float** audio_input_buffer, int midi_input, void** midi_input_buffer);
  274. /**
  275. * Receive sync and data from the network (incomplete buffer).
  276. *
  277. * @param net the network connection
  278. * @param audio_input number of audio inputs
  279. * @param audio_input_buffer an array of audio input buffers
  280. * @param midi_input number of MIDI inputs
  281. * @param midi_input_buffer an array of MIDI input buffers
  282. * @param frames the number of frames to receive.
  283. *
  284. * @return zero on success, non-zero on error
  285. */
  286. 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);
  287. /**
  288. * Send sync and data to the network (complete buffer).
  289. *
  290. * @param net the network connection
  291. * @param audio_output number of audio outputs
  292. * @param audio_output_buffer an array of audio output buffers
  293. * @param midi_output number of MIDI ouputs
  294. * @param midi_output_buffer an array of MIDI output buffers
  295. *
  296. * @return zero on success, non-zero on error
  297. */
  298. int jack_net_master_send(jack_net_master_t* net, int audio_output, float** audio_output_buffer, int midi_output, void** midi_output_buffer);
  299. /**
  300. * Send sync and data to the network (incomplete buffer).
  301. *
  302. * @param net the network connection
  303. * @param audio_output number of audio outputs
  304. * @param audio_output_buffer an array of audio output buffers
  305. * @param midi_output number of MIDI ouputs
  306. * @param midi_output_buffer an array of MIDI output buffers
  307. * @param frames the number of frames to send.
  308. *
  309. * @return zero on success, non-zero on error
  310. */
  311. 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);
  312. // Experimental Adapter API
  313. /**
  314. * jack_adapter_t is an opaque type, you may only access it using the API provided.
  315. */
  316. typedef struct _jack_adapter jack_adapter_t;
  317. /**
  318. * Create an adapter.
  319. *
  320. * @param input number of audio inputs
  321. * @param output of audio outputs
  322. * @param host_buffer_size the host buffer size in frames
  323. * @param host_sample_rate the host buffer sample rate
  324. * @param adapted_buffer_size the adapted buffer size in frames
  325. * @param adapted_sample_rate the adapted buffer sample rate
  326. *
  327. * @return 0 on success, otherwise a non-zero error code
  328. */
  329. jack_adapter_t* jack_create_adapter(int input, int output,
  330. jack_nframes_t host_buffer_size,
  331. jack_nframes_t host_sample_rate,
  332. jack_nframes_t adapted_buffer_size,
  333. jack_nframes_t adapted_sample_rate);
  334. /**
  335. * Destroy an adapter.
  336. *
  337. * @param adapter the adapter to be destroyed
  338. *
  339. * @return 0 on success, otherwise a non-zero error code
  340. */
  341. int jack_destroy_adapter(jack_adapter_t* adapter);
  342. /**
  343. * Flush internal state of an adapter.
  344. *
  345. * @param adapter the adapter to be flushed
  346. *
  347. * @return 0 on success, otherwise a non-zero error code
  348. */
  349. void jack_flush_adapter(jack_adapter_t* adapter);
  350. /**
  351. * Push input to and pull output from adapter ringbuffer.
  352. *
  353. * @param adapter the adapter
  354. * @param input an array of audio input buffers
  355. * @param output an array of audio ouput buffers
  356. * @param frames number of frames
  357. *
  358. * @return 0 on success, otherwise a non-zero error code
  359. */
  360. int jack_adapter_push_and_pull(jack_adapter_t* adapter, float** input, float** output, unsigned int frames);
  361. /**
  362. * Pull input to and push output from adapter ringbuffer.
  363. *
  364. * @param adapter the adapter
  365. * @param input an array of audio input buffers
  366. * @param output an array of audio ouput buffers
  367. * @param frames number of frames
  368. *
  369. * @return 0 on success, otherwise a non-zero error code
  370. */
  371. int jack_adapter_pull_and_push(jack_adapter_t* adapter, float** input, float** output, unsigned int frames);
  372. #ifdef __cplusplus
  373. }
  374. #endif
  375. #endif /* __net_h__ */