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.

351 lines
13KB

  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. #define SOCKET_ERROR -1
  29. #define RESTART_CB_API 1
  30. enum JackNetEncoder {
  31. JackFloatEncoder = 0, // samples are transmitted as float
  32. JackIntEncoder = 1, // samples are transmitted as 16 bits integer
  33. JackCeltEncoder = 2, // samples are transmitted using CELT codec (http://www.celt-codec.org/)
  34. JackOpusEncoder = 3, // samples are transmitted using OPUS codec (http://www.opus-codec.org/)
  35. };
  36. typedef struct {
  37. int audio_input; // from master or to slave (-1 to take master audio physical inputs)
  38. int audio_output; // to master or from slave (-1 to take master audio physical outputs)
  39. int midi_input; // from master or to slave (-1 to take master MIDI physical inputs)
  40. int midi_output; // to master or from slave (-1 to take master MIDI physical outputs)
  41. int mtu; // network Maximum Transmission Unit
  42. int time_out; // in second, -1 means infinite
  43. int encoder; // encoder type (one of JackNetEncoder)
  44. int kbps; // KB per second for CELT encoder
  45. int latency; // network latency
  46. } jack_slave_t;
  47. typedef struct {
  48. int audio_input; // master audio physical outputs (-1 to take slave wanted audio inputs)
  49. int audio_output; // master audio physical inputs (-1 to take slave wanted audio outputs)
  50. int midi_input; // master MIDI physical outputs (-1 to take slave wanted MIDI inputs)
  51. int midi_output; // master MIDI physical inputs (-1 to take slave wanted MIDI outputs)
  52. jack_nframes_t buffer_size; // master buffer size
  53. jack_nframes_t sample_rate; // master sample rate
  54. char master_name[MASTER_NAME_SIZE]; // master machine name
  55. int time_out; // in second, -1 means infinite
  56. } jack_master_t;
  57. /**
  58. * jack_net_slave_t is an opaque type. You may only access it using the
  59. * API provided.
  60. */
  61. typedef struct _jack_net_slave jack_net_slave_t;
  62. /**
  63. * Open a network connection with the master machine.
  64. * @param ip the multicast address of the master
  65. * @param port the connection port
  66. * @param request a connection request structure
  67. * @param result a connection result structure
  68. *
  69. * @return Opaque net handle if successful or NULL in case of error.
  70. */
  71. jack_net_slave_t* jack_net_slave_open(const char* ip, int port, const char* name, jack_slave_t* request, jack_master_t* result);
  72. /**
  73. * Close the network connection with the master machine.
  74. * @param net the network connection to be closed
  75. *
  76. * @return 0 on success, otherwise a non-zero error code
  77. */
  78. int jack_net_slave_close(jack_net_slave_t* net);
  79. /**
  80. * Prototype for Process callback.
  81. * @param nframes buffer size
  82. * @param audio_input number of audio inputs
  83. * @param audio_input_buffer an array of audio input buffers (from master)
  84. * @param midi_input number of MIDI inputs
  85. * @param midi_input_buffer an array of MIDI input buffers (from master)
  86. * @param audio_output number of audio outputs
  87. * @param audio_output_buffer an array of audio output buffers (to master)
  88. * @param midi_output number of MIDI outputs
  89. * @param midi_output_buffer an array of MIDI output buffers (to master)
  90. * @param arg pointer to a client supplied structure supplied by jack_set_net_process_callback()
  91. *
  92. * @return zero on success, non-zero on error
  93. */
  94. typedef int (* JackNetSlaveProcessCallback) (jack_nframes_t buffer_size,
  95. int audio_input,
  96. float** audio_input_buffer,
  97. int midi_input,
  98. void** midi_input_buffer,
  99. int audio_output,
  100. float** audio_output_buffer,
  101. int midi_output,
  102. void** midi_output_buffer,
  103. void* data);
  104. /**
  105. * Set network process callback.
  106. * @param net the network connection
  107. * @param net_callback the process callback
  108. * @param arg pointer to a client supplied structure
  109. *
  110. * @return 0 on success, otherwise a non-zero error code
  111. */
  112. int jack_set_net_slave_process_callback(jack_net_slave_t * net, JackNetSlaveProcessCallback net_callback, void *arg);
  113. /**
  114. * Start processing thread, the net_callback will start to be called.
  115. * @param net the network connection
  116. *
  117. * @return 0 on success, otherwise a non-zero error code
  118. */
  119. int jack_net_slave_activate(jack_net_slave_t* net);
  120. /**
  121. * Stop processing thread.
  122. * @param net the network connection
  123. *
  124. * @return 0 on success, otherwise a non-zero error code
  125. */
  126. int jack_net_slave_deactivate(jack_net_slave_t* net);
  127. /**
  128. * Test if slave is still active.
  129. * @param net the network connection
  130. *
  131. * @return a boolean
  132. */
  133. int jack_net_slave_is_active(jack_net_slave_t* net);
  134. /**
  135. * Prototype for BufferSize callback.
  136. * @param nframes buffer size
  137. * @param arg pointer to a client supplied structure supplied by jack_set_net_buffer_size_callback()
  138. *
  139. * @return zero on success, non-zero on error
  140. */
  141. typedef int (*JackNetSlaveBufferSizeCallback)(jack_nframes_t nframes, void *arg);
  142. /**
  143. * Prototype for SampleRate callback.
  144. * @param nframes sample rate
  145. * @param arg pointer to a client supplied structure supplied by jack_set_net_sample_rate_callback()
  146. *
  147. * @return zero on success, non-zero on error
  148. */
  149. typedef int (*JackNetSlaveSampleRateCallback)(jack_nframes_t nframes, void *arg);
  150. /**
  151. * Set network buffer size callback.
  152. * @param net the network connection
  153. * @param bufsize_callback the buffer size callback
  154. * @param arg pointer to a client supplied structure
  155. *
  156. * @return 0 on success, otherwise a non-zero error code
  157. */
  158. int jack_set_net_slave_buffer_size_callback(jack_net_slave_t *net, JackNetSlaveBufferSizeCallback bufsize_callback, void *arg);
  159. /**
  160. * Set network sample rate callback.
  161. * @param net the network connection
  162. * @param samplerate_callback the sample rate 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_sample_rate_callback(jack_net_slave_t *net, JackNetSlaveSampleRateCallback samplerate_callback, void *arg);
  168. /**
  169. * Prototype for server Shutdown callback (if not set, the client will just restart, waiting for an available master again).
  170. * @param arg pointer to a client supplied structure supplied by jack_set_net_shutdown_callback()
  171. */
  172. typedef void (*JackNetSlaveShutdownCallback)(void* data);
  173. /**
  174. * Set network shutdown callback.
  175. * @param net the network connection
  176. * @param shutdown_callback the shutdown callback
  177. * @param arg pointer to a client supplied structure
  178. *
  179. * @return 0 on success, otherwise a non-zero error code
  180. */
  181. int jack_set_net_slave_shutdown_callback(jack_net_slave_t *net, JackNetSlaveShutdownCallback shutdown_callback, void *arg) JACK_OPTIONAL_WEAK_DEPRECATED_EXPORT;
  182. /**
  183. * Prototype for server Restart callback : this is the new preferable way to be notified when the master has disappeared.
  184. * 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)
  185. * by returning 0. Otherwise returning a non-zero error code will definively close the connection.
  186. * If both Shutdown and Restart are supplied, Restart callback will be used.
  187. * @param arg pointer to a client supplied structure supplied by jack_set_net_restart_callback()
  188. *
  189. * @return 0 on success, otherwise a non-zero error code
  190. */
  191. typedef int (*JackNetSlaveRestartCallback)(void* data);
  192. /**
  193. * Set network restart callback.
  194. * @param net the network connection
  195. * @param restart_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_restart_callback(jack_net_slave_t *net, JackNetSlaveRestartCallback restart_callback, void *arg) JACK_OPTIONAL_WEAK_EXPORT;
  201. /**
  202. * jack_net_master_t is an opaque type, you may only access it using the API provided.
  203. */
  204. typedef struct _jack_net_master jack_net_master_t;
  205. /**
  206. * Open a network connection with the slave machine.
  207. * @param ip the multicast address of the master
  208. * @param port the connection port
  209. * @param request a connection request structure
  210. * @param result a connection result structure
  211. *
  212. * @return Opaque net handle if successful or NULL in case of error.
  213. */
  214. jack_net_master_t* jack_net_master_open(const char* ip, int port, const char* name, jack_master_t* request, jack_slave_t* result);
  215. /**
  216. * Close the network connection with the slave machine.
  217. * @param net the network connection to be closed
  218. *
  219. * @return 0 on success, otherwise a non-zero error code
  220. */
  221. int jack_net_master_close(jack_net_master_t* net);
  222. /**
  223. * Receive sync and data from the network.
  224. * @param net the network connection
  225. * @param audio_input number of audio inputs
  226. * @param audio_input_buffer an array of audio input buffers
  227. * @param midi_input number of MIDI inputs
  228. * @param midi_input_buffer an array of MIDI input buffers
  229. *
  230. * @return zero on success, non-zero on error
  231. */
  232. int jack_net_master_recv(jack_net_master_t* net, int audio_input, float** audio_input_buffer, int midi_input, void** midi_input_buffer);
  233. /**
  234. * Send sync and data to the network.
  235. * @param net the network connection
  236. * @param audio_output number of audio outputs
  237. * @param audio_output_buffer an array of audio output buffers
  238. * @param midi_output number of MIDI ouputs
  239. * @param midi_output_buffer an array of MIDI output buffers
  240. *
  241. * @return zero on success, non-zero on error
  242. */
  243. int jack_net_master_send(jack_net_master_t* net, int audio_output, float** audio_output_buffer, int midi_output, void** midi_output_buffer);
  244. // Experimental Adapter API
  245. /**
  246. * jack_adapter_t is an opaque type, you may only access it using the API provided.
  247. */
  248. typedef struct _jack_adapter jack_adapter_t;
  249. /**
  250. * Create an adapter.
  251. * @param input number of audio inputs
  252. * @param output of audio outputs
  253. * @param host_buffer_size the host buffer size in frames
  254. * @param host_sample_rate the host buffer sample rate
  255. * @param adapted_buffer_size the adapted buffer size in frames
  256. * @param adapted_sample_rate the adapted buffer sample rate
  257. *
  258. * @return 0 on success, otherwise a non-zero error code
  259. */
  260. jack_adapter_t* jack_create_adapter(int input, int output,
  261. jack_nframes_t host_buffer_size,
  262. jack_nframes_t host_sample_rate,
  263. jack_nframes_t adapted_buffer_size,
  264. jack_nframes_t adapted_sample_rate);
  265. /**
  266. * Destroy an adapter.
  267. * @param adapter the adapter to be destroyed
  268. *
  269. * @return 0 on success, otherwise a non-zero error code
  270. */
  271. int jack_destroy_adapter(jack_adapter_t* adapter);
  272. /**
  273. * Flush internal state of an adapter.
  274. * @param adapter the adapter to be flushed
  275. *
  276. * @return 0 on success, otherwise a non-zero error code
  277. */
  278. void jack_flush_adapter(jack_adapter_t* adapter);
  279. /**
  280. * Push input to and pull output from adapter ringbuffer.
  281. * @param adapter the adapter
  282. * @param input an array of audio input buffers
  283. * @param output an array of audio ouput buffers
  284. * @param frames number of frames
  285. *
  286. * @return 0 on success, otherwise a non-zero error code
  287. */
  288. int jack_adapter_push_and_pull(jack_adapter_t* adapter, float** input, float** output, unsigned int frames);
  289. /**
  290. * Pull input to and push output from adapter ringbuffer.
  291. * @param adapter the adapter
  292. * @param input an array of audio input buffers
  293. * @param output an array of audio ouput buffers
  294. * @param frames number of frames
  295. *
  296. * @return 0 on success, otherwise a non-zero error code
  297. */
  298. int jack_adapter_pull_and_push(jack_adapter_t* adapter, float** input, float** output, unsigned int frames);
  299. #ifdef __cplusplus
  300. }
  301. #endif
  302. #endif /* __net_h__ */