SDL_jackaudio.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2019 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "../../SDL_internal.h"
  19. #if SDL_AUDIO_DRIVER_JACK
  20. #include "SDL_assert.h"
  21. #include "SDL_timer.h"
  22. #include "SDL_audio.h"
  23. #include "../SDL_audio_c.h"
  24. #include "SDL_jackaudio.h"
  25. #include "SDL_loadso.h"
  26. #include "../../thread/SDL_systhread.h"
  27. static jack_client_t * (*JACK_jack_client_open) (const char *, jack_options_t, jack_status_t *, ...);
  28. static int (*JACK_jack_client_close) (jack_client_t *);
  29. static void (*JACK_jack_on_shutdown) (jack_client_t *, JackShutdownCallback, void *);
  30. static int (*JACK_jack_activate) (jack_client_t *);
  31. static int (*JACK_jack_deactivate) (jack_client_t *);
  32. static void * (*JACK_jack_port_get_buffer) (jack_port_t *, jack_nframes_t);
  33. static int (*JACK_jack_port_unregister) (jack_client_t *, jack_port_t *);
  34. static void (*JACK_jack_free) (void *);
  35. static const char ** (*JACK_jack_get_ports) (jack_client_t *, const char *, const char *, unsigned long);
  36. static jack_nframes_t (*JACK_jack_get_sample_rate) (jack_client_t *);
  37. static jack_nframes_t (*JACK_jack_get_buffer_size) (jack_client_t *);
  38. static jack_port_t * (*JACK_jack_port_register) (jack_client_t *, const char *, const char *, unsigned long, unsigned long);
  39. static jack_port_t * (*JACK_jack_port_by_name) (jack_client_t *, const char *);
  40. static const char * (*JACK_jack_port_name) (const jack_port_t *);
  41. static const char * (*JACK_jack_port_type) (const jack_port_t *);
  42. static int (*JACK_jack_connect) (jack_client_t *, const char *, const char *);
  43. static int (*JACK_jack_set_process_callback) (jack_client_t *, JackProcessCallback, void *);
  44. static int load_jack_syms(void);
  45. #ifdef SDL_AUDIO_DRIVER_JACK_DYNAMIC
  46. static const char *jack_library = SDL_AUDIO_DRIVER_JACK_DYNAMIC;
  47. static void *jack_handle = NULL;
  48. /* !!! FIXME: this is copy/pasted in several places now */
  49. static int
  50. load_jack_sym(const char *fn, void **addr)
  51. {
  52. *addr = SDL_LoadFunction(jack_handle, fn);
  53. if (*addr == NULL) {
  54. /* Don't call SDL_SetError(): SDL_LoadFunction already did. */
  55. return 0;
  56. }
  57. return 1;
  58. }
  59. /* cast funcs to char* first, to please GCC's strict aliasing rules. */
  60. #define SDL_JACK_SYM(x) \
  61. if (!load_jack_sym(#x, (void **) (char *) &JACK_##x)) return -1
  62. static void
  63. UnloadJackLibrary(void)
  64. {
  65. if (jack_handle != NULL) {
  66. SDL_UnloadObject(jack_handle);
  67. jack_handle = NULL;
  68. }
  69. }
  70. static int
  71. LoadJackLibrary(void)
  72. {
  73. int retval = 0;
  74. if (jack_handle == NULL) {
  75. jack_handle = SDL_LoadObject(jack_library);
  76. if (jack_handle == NULL) {
  77. retval = -1;
  78. /* Don't call SDL_SetError(): SDL_LoadObject already did. */
  79. } else {
  80. retval = load_jack_syms();
  81. if (retval < 0) {
  82. UnloadJackLibrary();
  83. }
  84. }
  85. }
  86. return retval;
  87. }
  88. #else
  89. #define SDL_JACK_SYM(x) JACK_##x = x
  90. static void
  91. UnloadJackLibrary(void)
  92. {
  93. }
  94. static int
  95. LoadJackLibrary(void)
  96. {
  97. load_jack_syms();
  98. return 0;
  99. }
  100. #endif /* SDL_AUDIO_DRIVER_JACK_DYNAMIC */
  101. static int
  102. load_jack_syms(void)
  103. {
  104. SDL_JACK_SYM(jack_client_open);
  105. SDL_JACK_SYM(jack_client_close);
  106. SDL_JACK_SYM(jack_on_shutdown);
  107. SDL_JACK_SYM(jack_activate);
  108. SDL_JACK_SYM(jack_deactivate);
  109. SDL_JACK_SYM(jack_port_get_buffer);
  110. SDL_JACK_SYM(jack_port_unregister);
  111. SDL_JACK_SYM(jack_free);
  112. SDL_JACK_SYM(jack_get_ports);
  113. SDL_JACK_SYM(jack_get_sample_rate);
  114. SDL_JACK_SYM(jack_get_buffer_size);
  115. SDL_JACK_SYM(jack_port_register);
  116. SDL_JACK_SYM(jack_port_by_name);
  117. SDL_JACK_SYM(jack_port_name);
  118. SDL_JACK_SYM(jack_port_type);
  119. SDL_JACK_SYM(jack_connect);
  120. SDL_JACK_SYM(jack_set_process_callback);
  121. return 0;
  122. }
  123. static void
  124. jackShutdownCallback(void *arg) /* JACK went away; device is lost. */
  125. {
  126. SDL_AudioDevice *this = (SDL_AudioDevice *) arg;
  127. SDL_OpenedAudioDeviceDisconnected(this);
  128. SDL_SemPost(this->hidden->iosem); /* unblock the SDL thread. */
  129. }
  130. // !!! FIXME: implement and register these!
  131. //typedef int(* JackSampleRateCallback)(jack_nframes_t nframes, void *arg)
  132. //typedef int(* JackBufferSizeCallback)(jack_nframes_t nframes, void *arg)
  133. static int
  134. jackProcessPlaybackCallback(jack_nframes_t nframes, void *arg)
  135. {
  136. SDL_AudioDevice *this = (SDL_AudioDevice *) arg;
  137. jack_port_t **ports = this->hidden->sdlports;
  138. const int total_channels = this->spec.channels;
  139. const int total_frames = this->spec.samples;
  140. int channelsi;
  141. if (!SDL_AtomicGet(&this->enabled)) {
  142. /* silence the buffer to avoid repeats and corruption. */
  143. SDL_memset(this->hidden->iobuffer, '\0', this->spec.size);
  144. }
  145. for (channelsi = 0; channelsi < total_channels; channelsi++) {
  146. float *dst = (float *) JACK_jack_port_get_buffer(ports[channelsi], nframes);
  147. if (dst) {
  148. const float *src = ((float *) this->hidden->iobuffer) + channelsi;
  149. int framesi;
  150. for (framesi = 0; framesi < total_frames; framesi++) {
  151. *(dst++) = *src;
  152. src += total_channels;
  153. }
  154. }
  155. }
  156. SDL_SemPost(this->hidden->iosem); /* tell SDL thread we're done; refill the buffer. */
  157. return 0; /* success */
  158. }
  159. /* This function waits until it is possible to write a full sound buffer */
  160. static void
  161. JACK_WaitDevice(_THIS)
  162. {
  163. if (SDL_AtomicGet(&this->enabled)) {
  164. if (SDL_SemWait(this->hidden->iosem) == -1) {
  165. SDL_OpenedAudioDeviceDisconnected(this);
  166. }
  167. }
  168. }
  169. static Uint8 *
  170. JACK_GetDeviceBuf(_THIS)
  171. {
  172. return (Uint8 *) this->hidden->iobuffer;
  173. }
  174. static int
  175. jackProcessCaptureCallback(jack_nframes_t nframes, void *arg)
  176. {
  177. SDL_AudioDevice *this = (SDL_AudioDevice *) arg;
  178. if (SDL_AtomicGet(&this->enabled)) {
  179. jack_port_t **ports = this->hidden->sdlports;
  180. const int total_channels = this->spec.channels;
  181. const int total_frames = this->spec.samples;
  182. int channelsi;
  183. for (channelsi = 0; channelsi < total_channels; channelsi++) {
  184. const float *src = (const float *) JACK_jack_port_get_buffer(ports[channelsi], nframes);
  185. if (src) {
  186. float *dst = ((float *) this->hidden->iobuffer) + channelsi;
  187. int framesi;
  188. for (framesi = 0; framesi < total_frames; framesi++) {
  189. *dst = *(src++);
  190. dst += total_channels;
  191. }
  192. }
  193. }
  194. }
  195. SDL_SemPost(this->hidden->iosem); /* tell SDL thread we're done; new buffer is ready! */
  196. return 0; /* success */
  197. }
  198. static int
  199. JACK_CaptureFromDevice(_THIS, void *buffer, int buflen)
  200. {
  201. SDL_assert(buflen == this->spec.size); /* we always fill a full buffer. */
  202. /* Wait for JACK to fill the iobuffer */
  203. if (SDL_SemWait(this->hidden->iosem) == -1) {
  204. return -1;
  205. }
  206. SDL_memcpy(buffer, this->hidden->iobuffer, buflen);
  207. return buflen;
  208. }
  209. static void
  210. JACK_FlushCapture(_THIS)
  211. {
  212. SDL_SemWait(this->hidden->iosem);
  213. }
  214. static void
  215. JACK_CloseDevice(_THIS)
  216. {
  217. if (this->hidden->client) {
  218. JACK_jack_deactivate(this->hidden->client);
  219. if (this->hidden->sdlports) {
  220. const int channels = this->spec.channels;
  221. int i;
  222. for (i = 0; i < channels; i++) {
  223. JACK_jack_port_unregister(this->hidden->client, this->hidden->sdlports[i]);
  224. }
  225. SDL_free(this->hidden->sdlports);
  226. }
  227. JACK_jack_client_close(this->hidden->client);
  228. }
  229. if (this->hidden->iosem) {
  230. SDL_DestroySemaphore(this->hidden->iosem);
  231. }
  232. SDL_free(this->hidden->iobuffer);
  233. }
  234. static int
  235. JACK_OpenDevice(_THIS, void *handle, const char *devname, int iscapture)
  236. {
  237. /* Note that JACK uses "output" for capture devices (they output audio
  238. data to us) and "input" for playback (we input audio data to them).
  239. Likewise, SDL's playback port will be "output" (we write data out)
  240. and capture will be "input" (we read data in). */
  241. const unsigned long sysportflags = iscapture ? JackPortIsOutput : JackPortIsInput;
  242. const unsigned long sdlportflags = iscapture ? JackPortIsInput : JackPortIsOutput;
  243. const JackProcessCallback callback = iscapture ? jackProcessCaptureCallback : jackProcessPlaybackCallback;
  244. const char *sdlportstr = iscapture ? "input" : "output";
  245. const char **devports = NULL;
  246. int *audio_ports;
  247. jack_client_t *client = NULL;
  248. jack_status_t status;
  249. int channels = 0;
  250. int ports = 0;
  251. int i;
  252. /* Initialize all variables that we clean on shutdown */
  253. this->hidden = (struct SDL_PrivateAudioData *) SDL_calloc(1, sizeof (*this->hidden));
  254. if (this->hidden == NULL) {
  255. return SDL_OutOfMemory();
  256. }
  257. /* !!! FIXME: we _still_ need an API to specify an app name */
  258. client = JACK_jack_client_open("SDL", JackNoStartServer, &status, NULL);
  259. this->hidden->client = client;
  260. if (client == NULL) {
  261. return SDL_SetError("Can't open JACK client");
  262. }
  263. devports = JACK_jack_get_ports(client, NULL, NULL, JackPortIsPhysical | sysportflags);
  264. if (!devports || !devports[0]) {
  265. return SDL_SetError("No physical JACK ports available");
  266. }
  267. while (devports[++ports]) {
  268. /* spin to count devports */
  269. }
  270. /* Filter out non-audio ports */
  271. audio_ports = SDL_calloc(ports, sizeof *audio_ports);
  272. for (i = 0; i < ports; i++) {
  273. const jack_port_t *dport = JACK_jack_port_by_name(client, devports[i]);
  274. const char *type = JACK_jack_port_type(dport);
  275. const int len = SDL_strlen(type);
  276. /* See if type ends with "audio" */
  277. if (len >= 5 && !SDL_memcmp(type+len-5, "audio", 5)) {
  278. audio_ports[channels++] = i;
  279. }
  280. }
  281. if (channels == 0) {
  282. return SDL_SetError("No physical JACK ports available");
  283. }
  284. /* !!! FIXME: docs say about buffer size: "This size may change, clients that depend on it must register a bufsize_callback so they will be notified if it does." */
  285. /* Jack pretty much demands what it wants. */
  286. this->spec.format = AUDIO_F32SYS;
  287. this->spec.freq = JACK_jack_get_sample_rate(client);
  288. this->spec.channels = channels;
  289. this->spec.samples = JACK_jack_get_buffer_size(client);
  290. SDL_CalculateAudioSpec(&this->spec);
  291. this->hidden->iosem = SDL_CreateSemaphore(0);
  292. if (!this->hidden->iosem) {
  293. return -1; /* error was set by SDL_CreateSemaphore */
  294. }
  295. this->hidden->iobuffer = (float *) SDL_calloc(1, this->spec.size);
  296. if (!this->hidden->iobuffer) {
  297. return SDL_OutOfMemory();
  298. }
  299. /* Build SDL's ports, which we will connect to the device ports. */
  300. this->hidden->sdlports = (jack_port_t **) SDL_calloc(channels, sizeof (jack_port_t *));
  301. if (this->hidden->sdlports == NULL) {
  302. return SDL_OutOfMemory();
  303. }
  304. for (i = 0; i < channels; i++) {
  305. char portname[32];
  306. SDL_snprintf(portname, sizeof (portname), "sdl_jack_%s_%d", sdlportstr, i);
  307. this->hidden->sdlports[i] = JACK_jack_port_register(client, portname, JACK_DEFAULT_AUDIO_TYPE, sdlportflags, 0);
  308. if (this->hidden->sdlports[i] == NULL) {
  309. return SDL_SetError("jack_port_register failed");
  310. }
  311. }
  312. if (JACK_jack_set_process_callback(client, callback, this) != 0) {
  313. return SDL_SetError("JACK: Couldn't set process callback");
  314. }
  315. JACK_jack_on_shutdown(client, jackShutdownCallback, this);
  316. if (JACK_jack_activate(client) != 0) {
  317. return SDL_SetError("Failed to activate JACK client");
  318. }
  319. /* once activated, we can connect all the ports. */
  320. for (i = 0; i < channels; i++) {
  321. const char *sdlport = JACK_jack_port_name(this->hidden->sdlports[i]);
  322. const char *srcport = iscapture ? devports[audio_ports[i]] : sdlport;
  323. const char *dstport = iscapture ? sdlport : devports[audio_ports[i]];
  324. if (JACK_jack_connect(client, srcport, dstport) != 0) {
  325. return SDL_SetError("Couldn't connect JACK ports: %s => %s", srcport, dstport);
  326. }
  327. }
  328. /* don't need these anymore. */
  329. JACK_jack_free(devports);
  330. SDL_free(audio_ports);
  331. /* We're ready to rock and roll. :-) */
  332. return 0;
  333. }
  334. static void
  335. JACK_Deinitialize(void)
  336. {
  337. UnloadJackLibrary();
  338. }
  339. static int
  340. JACK_Init(SDL_AudioDriverImpl * impl)
  341. {
  342. if (LoadJackLibrary() < 0) {
  343. return 0;
  344. } else {
  345. /* Make sure a JACK server is running and available. */
  346. jack_status_t status;
  347. jack_client_t *client = JACK_jack_client_open("SDL", JackNoStartServer, &status, NULL);
  348. if (client == NULL) {
  349. UnloadJackLibrary();
  350. return 0;
  351. }
  352. JACK_jack_client_close(client);
  353. }
  354. /* Set the function pointers */
  355. impl->OpenDevice = JACK_OpenDevice;
  356. impl->WaitDevice = JACK_WaitDevice;
  357. impl->GetDeviceBuf = JACK_GetDeviceBuf;
  358. impl->CloseDevice = JACK_CloseDevice;
  359. impl->Deinitialize = JACK_Deinitialize;
  360. impl->CaptureFromDevice = JACK_CaptureFromDevice;
  361. impl->FlushCapture = JACK_FlushCapture;
  362. impl->OnlyHasDefaultOutputDevice = SDL_TRUE;
  363. impl->OnlyHasDefaultCaptureDevice = SDL_TRUE;
  364. impl->HasCaptureSupport = SDL_TRUE;
  365. return 1; /* this audio target is available. */
  366. }
  367. AudioBootStrap JACK_bootstrap = {
  368. "jack", "JACK Audio Connection Kit", JACK_Init, 0
  369. };
  370. #endif /* SDL_AUDIO_DRIVER_JACK */
  371. /* vi: set ts=4 sw=4 expandtab: */