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.

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