JACK tools
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.

861 lines
23KB

  1. /* -*- Mode: C ; c-basic-offset: 2 -*- */
  2. /*
  3. * ALSA SEQ < - > JACK MIDI bridge
  4. *
  5. * Copyright (c) 2006,2007 Dmitry S. Baikov <c0ff@konstruktiv.org>
  6. * Copyright (c) 2007,2008,2009 Nedko Arnaudov <nedko@arnaudov.name>
  7. * Copyright (c) 2009,2010,2013 Paul Davis <paul@linuxaudiosystems.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; version 2 of the License.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include <stdbool.h>
  23. #include <stdarg.h>
  24. #include <string.h>
  25. #include <time.h>
  26. #include <alsa/asoundlib.h>
  27. #include <jack/jack.h>
  28. #include <jack/midiport.h>
  29. #include <jack/ringbuffer.h>
  30. #include "list.h"
  31. #include "a2j.h"
  32. #include "port_hash.h"
  33. #include "port.h"
  34. #include "port_thread.h"
  35. #ifdef A2J_DEBUG
  36. bool a2j_do_debug = false;
  37. void
  38. _a2j_debug (const char* fmt, ...)
  39. {
  40. va_list ap;
  41. va_start (ap, fmt);
  42. vfprintf (stderr, fmt, ap);
  43. fputc ('\n', stdout);
  44. }
  45. #endif
  46. void
  47. a2j_error (const char* fmt, ...)
  48. {
  49. va_list ap;
  50. va_start (ap, fmt);
  51. vfprintf (stdout, fmt, ap);
  52. fputc ('\n', stdout);
  53. }
  54. static bool
  55. a2j_stream_init(struct a2j * self, int which)
  56. {
  57. struct a2j_stream *str = &self->stream[which];
  58. str->new_ports = jack_ringbuffer_create (MAX_PORTS * sizeof(struct a2j_port *));
  59. if (str->new_ports == NULL) {
  60. return false;
  61. }
  62. snd_midi_event_new (MAX_EVENT_SIZE, &str->codec);
  63. INIT_LIST_HEAD (&str->list);
  64. return true;
  65. }
  66. static void
  67. a2j_stream_detach (struct a2j_stream * stream_ptr)
  68. {
  69. struct a2j_port * port_ptr;
  70. struct list_head * node_ptr;
  71. while (!list_empty (&stream_ptr->list)) {
  72. node_ptr = stream_ptr->list.next;
  73. list_del (node_ptr);
  74. port_ptr = list_entry (node_ptr, struct a2j_port, siblings);
  75. a2j_debug ("port deleted: %s", port_ptr->name);
  76. a2j_port_free (port_ptr);
  77. }
  78. }
  79. static
  80. void
  81. a2j_stream_close (struct a2j * self, int which)
  82. {
  83. struct a2j_stream *str = &self->stream[which];
  84. if (str->codec)
  85. snd_midi_event_free (str->codec);
  86. if (str->new_ports)
  87. jack_ringbuffer_free (str->new_ports);
  88. }
  89. static void
  90. stop_threads (struct a2j* self)
  91. {
  92. if (self->running) {
  93. void* thread_status;
  94. self->running = false; /* tell alsa io thread to stop, whenever they wake up */
  95. /* do something that we need to do anyway and will wake the io thread, then join */
  96. snd_seq_disconnect_from (self->seq, self->port_id, SND_SEQ_CLIENT_SYSTEM, SND_SEQ_PORT_SYSTEM_ANNOUNCE);
  97. a2j_debug ("wait for ALSA input thread\n");
  98. pthread_join (self->alsa_input_thread, &thread_status);
  99. a2j_debug ("input thread done\n");
  100. /* wake output thread and join */
  101. sem_post(&self->output_semaphore);
  102. pthread_join(self->alsa_output_thread, &thread_status);
  103. a2j_debug ("output thread done\n");
  104. }
  105. }
  106. /*
  107. * =================== Input/output port handling =========================
  108. */
  109. void a2j_add_ports (struct a2j_stream * str)
  110. {
  111. struct a2j_port * port_ptr;
  112. while (jack_ringbuffer_read (str->new_ports, (char *)&port_ptr, sizeof(port_ptr))) {
  113. a2j_debug("jack: inserted port %s", port_ptr->name);
  114. a2j_port_insert (str->port_hash, port_ptr);
  115. }
  116. }
  117. static
  118. void
  119. a2j_port_event (struct a2j * self, snd_seq_event_t * ev)
  120. {
  121. const snd_seq_addr_t addr = ev->data.addr;
  122. if (addr.client == self->client_id)
  123. return;
  124. if (ev->type == SND_SEQ_EVENT_PORT_START || ev->type == SND_SEQ_EVENT_PORT_CHANGE) {
  125. if (jack_ringbuffer_write_space(self->port_add) >= sizeof(addr)) {
  126. a2j_debug("port_event: add/change %d:%d", addr.client, addr.port);
  127. jack_ringbuffer_write(self->port_add, (char*)&addr, sizeof(addr));
  128. } else {
  129. a2j_error("dropping port_event: add/change %d:%d", addr.client, addr.port);
  130. }
  131. } else if (ev->type == SND_SEQ_EVENT_PORT_EXIT) {
  132. a2j_debug("port_event: del %d:%d", addr.client, addr.port);
  133. a2j_port_setdead(self->stream[A2J_PORT_CAPTURE].port_hash, addr);
  134. a2j_port_setdead(self->stream[A2J_PORT_PLAYBACK].port_hash, addr);
  135. }
  136. }
  137. /* --- INBOUND FROM ALSA TO JACK ---- */
  138. static void
  139. a2j_input_event (struct a2j * self, snd_seq_event_t * alsa_event)
  140. {
  141. jack_midi_data_t data[MAX_EVENT_SIZE];
  142. struct a2j_stream *str = &self->stream[A2J_PORT_CAPTURE];
  143. long size;
  144. struct a2j_port *port;
  145. jack_nframes_t now;
  146. now = jack_frame_time (self->jack_client);
  147. if ((port = a2j_port_get(str->port_hash, alsa_event->source)) == NULL) {
  148. return;
  149. }
  150. /*
  151. * RPNs, NRPNs, Bank Change, etc. need special handling
  152. * but seems, ALSA does it for us already.
  153. */
  154. snd_midi_event_reset_decode(str->codec);
  155. if ((size = snd_midi_event_decode(str->codec, data, sizeof(data), alsa_event))<0) {
  156. return;
  157. }
  158. // fixup NoteOn with vel 0
  159. if ((data[0] & 0xF0) == 0x90 && data[2] == 0x00) {
  160. data[0] = 0x80 + (data[0] & 0x0F);
  161. data[2] = 0x40;
  162. }
  163. a2j_debug("input: %d bytes at event_frame=%u", (int)size, now);
  164. if (jack_ringbuffer_write_space(port->inbound_events) >= (sizeof(struct a2j_alsa_midi_event) + size)) {
  165. struct a2j_alsa_midi_event ev;
  166. char *ev_charp = (char*) &ev;
  167. size_t limit;
  168. size_t to_write = sizeof(ev);
  169. jack_ringbuffer_data_t vec[2];
  170. jack_ringbuffer_get_write_vector( port->inbound_events, vec );
  171. ev.time = now;
  172. ev.size = size;
  173. limit = (to_write > vec[0].len ? vec[0].len : to_write);
  174. if (limit) {
  175. memcpy( vec[0].buf, ev_charp, limit );
  176. to_write -= limit;
  177. ev_charp += limit;
  178. vec[0].buf += limit;
  179. vec[0].len -= limit;
  180. }
  181. if (to_write) {
  182. memcpy( vec[1].buf, ev_charp, to_write );
  183. vec[1].buf += to_write;
  184. vec[1].len -= to_write;
  185. }
  186. to_write = size;
  187. ev_charp = (char *)data;
  188. limit = (to_write > vec[0].len ? vec[0].len : to_write);
  189. if (limit) {
  190. memcpy (vec[0].buf, ev_charp, limit);
  191. }
  192. to_write -= limit;
  193. ev_charp += limit;
  194. if (to_write) {
  195. memcpy (vec[1].buf, ev_charp, to_write);
  196. }
  197. jack_ringbuffer_write_advance( port->inbound_events, sizeof(ev) + size );
  198. } else {
  199. a2j_error ("MIDI data lost (incoming event buffer full): %ld bytes lost", size);
  200. }
  201. }
  202. static int
  203. a2j_process_incoming (struct a2j* self, struct a2j_port* port, jack_nframes_t nframes)
  204. {
  205. jack_nframes_t one_period;
  206. struct a2j_alsa_midi_event ev;
  207. char *ev_buf;
  208. /* grab data queued by the ALSA input thread and write it into the JACK
  209. port buffer. it will delivered during the JACK period that this
  210. function is called from.
  211. */
  212. /* first clear the JACK port buffer in preparation for new data
  213. */
  214. a2j_debug ("PORT: %s process input", jack_port_name (port->jack_port));
  215. jack_midi_clear_buffer (port->jack_buf);
  216. one_period = jack_get_buffer_size (self->jack_client);
  217. while (jack_ringbuffer_peek (port->inbound_events, (char*)&ev, sizeof(ev) ) == sizeof(ev) ) {
  218. jack_midi_data_t* buf;
  219. jack_nframes_t offset;
  220. if (ev.time >= self->cycle_start) {
  221. break;
  222. }
  223. //jack_ringbuffer_read_advance (port->inbound_events, sizeof (ev));
  224. ev_buf = (char *) alloca( sizeof(ev) + ev.size );
  225. if (jack_ringbuffer_peek (port->inbound_events, ev_buf, sizeof(ev) + ev.size ) != sizeof(ev) + ev.size)
  226. break;
  227. offset = self->cycle_start - ev.time;
  228. if (offset > one_period) {
  229. /* from a previous cycle, somehow. cram it in at the front */
  230. offset = 0;
  231. } else {
  232. /* offset from start of the current cycle */
  233. offset = one_period - offset;
  234. }
  235. a2j_debug ("event at %d offset %d", ev.time, offset);
  236. /* make sure there is space for it */
  237. buf = jack_midi_event_reserve (port->jack_buf, offset, ev.size);
  238. if (buf) {
  239. /* grab the event */
  240. memcpy( buf, ev_buf + sizeof(ev), ev.size );
  241. } else {
  242. /* throw it away (no space) */
  243. a2j_error ("threw away MIDI event - not reserved at time %d", ev.time);
  244. }
  245. jack_ringbuffer_read_advance (port->inbound_events, sizeof(ev) + ev.size);
  246. a2j_debug("input on %s: sucked %d bytes from inbound at %d", jack_port_name (port->jack_port), ev.size, ev.time);
  247. }
  248. return 0;
  249. }
  250. void*
  251. alsa_input_thread (void* arg)
  252. {
  253. struct a2j * self = arg;
  254. int npfd;
  255. struct pollfd * pfd;
  256. snd_seq_addr_t addr;
  257. snd_seq_client_info_t * client_info;
  258. snd_seq_port_info_t * port_info;
  259. bool initial;
  260. snd_seq_event_t * event;
  261. int ret;
  262. npfd = snd_seq_poll_descriptors_count(self->seq, POLLIN);
  263. pfd = (struct pollfd *)alloca(npfd * sizeof(struct pollfd));
  264. snd_seq_poll_descriptors(self->seq, pfd, npfd, POLLIN);
  265. initial = true;
  266. while (self->running) {
  267. if ((ret = poll(pfd, npfd, 1000)) > 0) {
  268. while (snd_seq_event_input (self->seq, &event) > 0) {
  269. if (initial) {
  270. snd_seq_client_info_alloca(&client_info);
  271. snd_seq_port_info_alloca(&port_info);
  272. snd_seq_client_info_set_client(client_info, -1);
  273. while (snd_seq_query_next_client(self->seq, client_info) >= 0) {
  274. addr.client = snd_seq_client_info_get_client(client_info);
  275. if (addr.client == SND_SEQ_CLIENT_SYSTEM || addr.client == self->client_id) {
  276. continue;
  277. }
  278. snd_seq_port_info_set_client(port_info, addr.client);
  279. snd_seq_port_info_set_port(port_info, -1);
  280. while (snd_seq_query_next_port(self->seq, port_info) >= 0) {
  281. addr.port = snd_seq_port_info_get_port(port_info);
  282. a2j_update_port(self, addr, port_info);
  283. }
  284. }
  285. initial = false;
  286. }
  287. if (event->source.client == SND_SEQ_CLIENT_SYSTEM) {
  288. a2j_port_event(self, event);
  289. } else {
  290. a2j_input_event(self, event);
  291. }
  292. snd_seq_free_event (event);
  293. }
  294. }
  295. }
  296. return (void*) 0;
  297. }
  298. /* --- OUTBOUND FROM JACK TO ALSA ---- */
  299. int
  300. a2j_process_outgoing (
  301. struct a2j * self,
  302. struct a2j_port * port)
  303. {
  304. /* collect data from JACK port buffer and queue it for delivery by ALSA output thread */
  305. int nevents;
  306. jack_ringbuffer_data_t vec[2];
  307. int i;
  308. int written = 0;
  309. size_t limit;
  310. struct a2j_delivery_event* dev;
  311. size_t gap = 0;
  312. jack_ringbuffer_get_write_vector (self->outbound_events, vec);
  313. dev = (struct a2j_delivery_event*) vec[0].buf;
  314. limit = vec[0].len / sizeof (struct a2j_delivery_event);
  315. nevents = jack_midi_get_event_count (port->jack_buf);
  316. for (i = 0; (i < nevents) && (written < limit); ++i) {
  317. jack_midi_event_get (&dev->jack_event, port->jack_buf, i);
  318. if (dev->jack_event.size <= MAX_JACKMIDI_EV_SIZE)
  319. {
  320. dev->time = dev->jack_event.time;
  321. dev->port = port;
  322. memcpy( dev->midistring, dev->jack_event.buffer, dev->jack_event.size );
  323. written++;
  324. ++dev;
  325. }
  326. }
  327. /* anything left? use the second part of the vector, as much as possible */
  328. if (i < nevents)
  329. {
  330. if (vec[0].len)
  331. {
  332. gap = vec[0].len - written * sizeof(struct a2j_delivery_event);
  333. }
  334. dev = (struct a2j_delivery_event*) vec[1].buf;
  335. limit += (vec[1].len / sizeof (struct a2j_delivery_event));
  336. while ((i < nevents) && (written < limit))
  337. {
  338. jack_midi_event_get(&dev->jack_event, port->jack_buf, i);
  339. if (dev->jack_event.size <= MAX_JACKMIDI_EV_SIZE)
  340. {
  341. dev->time = dev->jack_event.time;
  342. dev->port = port;
  343. memcpy(dev->midistring, dev->jack_event.buffer, dev->jack_event.size);
  344. written++;
  345. ++dev;
  346. }
  347. ++i;
  348. }
  349. }
  350. a2j_debug( "done pushing events: %d ... gap: %d ", (int)written, (int)gap );
  351. /* clear JACK port buffer; advance ring buffer ptr */
  352. jack_ringbuffer_write_advance (self->outbound_events, written * sizeof (struct a2j_delivery_event) + gap);
  353. return nevents;
  354. }
  355. static int
  356. time_sorter (struct a2j_delivery_event * a, struct a2j_delivery_event * b)
  357. {
  358. if (a->time < b->time) {
  359. return -1;
  360. } else if (a->time > b->time) {
  361. return 1;
  362. }
  363. return 0;
  364. }
  365. static void*
  366. alsa_output_thread(void * arg)
  367. {
  368. struct a2j * self = (struct a2j*) arg;
  369. struct a2j_stream *str = &self->stream[A2J_PORT_PLAYBACK];
  370. int i;
  371. struct list_head evlist;
  372. struct list_head * node_ptr;
  373. jack_ringbuffer_data_t vec[2];
  374. snd_seq_event_t alsa_event;
  375. struct a2j_delivery_event* ev;
  376. float sr;
  377. jack_nframes_t now;
  378. int err;
  379. int limit;
  380. while (self->running) {
  381. /* first, make a list of all events in the outbound_events FIFO */
  382. INIT_LIST_HEAD(&evlist);
  383. jack_ringbuffer_get_read_vector (self->outbound_events, vec);
  384. a2j_debug ("output thread: got %d+%d events",
  385. (vec[0].len / sizeof (struct a2j_delivery_event)),
  386. (vec[1].len / sizeof (struct a2j_delivery_event)));
  387. ev = (struct a2j_delivery_event*) vec[0].buf;
  388. limit = vec[0].len / sizeof (struct a2j_delivery_event);
  389. for (i = 0; i < limit; ++i) {
  390. list_add_tail(&ev->siblings, &evlist);
  391. ev++;
  392. }
  393. ev = (struct a2j_delivery_event*) vec[1].buf;
  394. limit = vec[1].len / sizeof (struct a2j_delivery_event);
  395. for (i = 0; i < limit; ++i) {
  396. list_add_tail(&ev->siblings, &evlist);
  397. ev++;
  398. }
  399. if (vec[0].len < sizeof(struct a2j_delivery_event) && (vec[1].len == 0)) {
  400. /* no events: wait for some */
  401. a2j_debug ("output thread: wait for events");
  402. sem_wait (&self->output_semaphore);
  403. a2j_debug ("output thread: AWAKE ... loop back for events");
  404. continue;
  405. }
  406. /* now sort this list by time */
  407. list_sort(&evlist, struct a2j_delivery_event, siblings, time_sorter);
  408. /* now deliver */
  409. sr = jack_get_sample_rate (self->jack_client);
  410. list_for_each(node_ptr, &evlist)
  411. {
  412. ev = list_entry(node_ptr, struct a2j_delivery_event, siblings);
  413. snd_seq_ev_clear(&alsa_event);
  414. snd_midi_event_reset_encode(str->codec);
  415. if (!snd_midi_event_encode(str->codec, (const unsigned char *)ev->midistring, ev->jack_event.size, &alsa_event))
  416. {
  417. continue; // invalid event
  418. }
  419. snd_seq_ev_set_source(&alsa_event, self->port_id);
  420. snd_seq_ev_set_dest(&alsa_event, ev->port->remote.client, ev->port->remote.port);
  421. snd_seq_ev_set_direct (&alsa_event);
  422. now = jack_frame_time (self->jack_client);
  423. ev->time += self->cycle_start;
  424. a2j_debug ("@ %d, next event @ %d", now, ev->time);
  425. /* do we need to wait a while before delivering? */
  426. if (ev->time > now) {
  427. struct timespec nanoseconds;
  428. jack_nframes_t sleep_frames = ev->time - now;
  429. float seconds = sleep_frames / sr;
  430. /* if the gap is long enough, sleep */
  431. if (seconds > 0.001) {
  432. nanoseconds.tv_sec = (time_t) seconds;
  433. nanoseconds.tv_nsec = (long) NSEC_PER_SEC * (seconds - nanoseconds.tv_sec);
  434. a2j_debug ("output thread sleeps for %.2f msec", ((double) nanoseconds.tv_nsec / NSEC_PER_SEC) * 1000.0);
  435. if (nanosleep (&nanoseconds, NULL) < 0) {
  436. fprintf (stderr, "BAD SLEEP\n");
  437. /* do something ? */
  438. }
  439. }
  440. }
  441. /* its time to deliver */
  442. err = snd_seq_event_output(self->seq, &alsa_event);
  443. snd_seq_drain_output (self->seq);
  444. now = jack_frame_time (self->jack_client);
  445. a2j_debug("alsa_out: written %d bytes to %s at %d, DELTA = %d", ev->jack_event.size, ev->port->name, now,
  446. (int32_t) (now - ev->time));
  447. }
  448. /* free up space in the FIFO */
  449. jack_ringbuffer_read_advance (self->outbound_events, vec[0].len + vec[1].len);
  450. /* and head back for more */
  451. }
  452. return (void*) 0;
  453. }
  454. /** CORE JACK PROCESSING */
  455. /* ALSA */
  456. static void
  457. a2j_jack_process_internal (struct a2j * self, int dir, jack_nframes_t nframes)
  458. {
  459. struct a2j_stream * stream_ptr;
  460. int i;
  461. struct a2j_port ** port_ptr_ptr;
  462. struct a2j_port * port_ptr;
  463. int nevents = 0;
  464. stream_ptr = &self->stream[dir];
  465. a2j_add_ports(stream_ptr);
  466. // process ports
  467. for (i = 0 ; i < PORT_HASH_SIZE ; i++)
  468. {
  469. port_ptr_ptr = &stream_ptr->port_hash[i];
  470. while (*port_ptr_ptr != NULL)
  471. {
  472. port_ptr = *port_ptr_ptr;
  473. if (!port_ptr->is_dead) {
  474. port_ptr->jack_buf = jack_port_get_buffer(port_ptr->jack_port, nframes);
  475. if (dir == A2J_PORT_CAPTURE) {
  476. a2j_process_incoming (self, port_ptr, nframes);
  477. } else {
  478. nevents += a2j_process_outgoing (self, port_ptr);
  479. }
  480. } else if (jack_ringbuffer_write_space (self->port_del) >= sizeof(port_ptr)) {
  481. a2j_debug("jack: removed port %s", port_ptr->name);
  482. *port_ptr_ptr = port_ptr->next;
  483. jack_ringbuffer_write(self->port_del, (char*)&port_ptr, sizeof(port_ptr));
  484. continue;
  485. }
  486. port_ptr_ptr = &port_ptr->next;
  487. }
  488. }
  489. if (dir == A2J_PORT_PLAYBACK && nevents > 0) {
  490. int sv;
  491. /* if we queued up anything for output, tell the output thread in
  492. case its waiting for us.
  493. */
  494. sem_getvalue (&self->output_semaphore, &sv);
  495. sem_post (&self->output_semaphore);
  496. }
  497. }
  498. static int
  499. a2j_process(jack_nframes_t nframes, void * arg)
  500. {
  501. struct a2j* self = (struct a2j *) arg;
  502. if (self->freewheeling) {
  503. return 0;
  504. }
  505. self->cycle_start = jack_last_frame_time (self->jack_client);
  506. a2j_jack_process_internal (self, A2J_PORT_CAPTURE, nframes);
  507. a2j_jack_process_internal (self, A2J_PORT_PLAYBACK, nframes);
  508. return 0;
  509. }
  510. /* --- */
  511. static
  512. void
  513. a2j_freewheel(int starting, void * arg)
  514. {
  515. struct a2j* self = (struct a2j*) arg;
  516. self->freewheeling = starting;
  517. }
  518. static
  519. void
  520. a2j_shutdown (void * arg)
  521. {
  522. struct a2j* self = (struct a2j*) self;
  523. a2j_debug ("JACK server shutdown notification received.");
  524. stop_threads (self);
  525. }
  526. int
  527. connect_to_alsa (struct a2j* self)
  528. {
  529. int error;
  530. void* thread_status;
  531. self->port_add = jack_ringbuffer_create (2 * MAX_PORTS * sizeof(snd_seq_addr_t));
  532. if (self->port_add == NULL) {
  533. goto free_self;
  534. }
  535. self->port_del = jack_ringbuffer_create(2 * MAX_PORTS * sizeof(struct a2j_port *));
  536. if (self->port_del == NULL) {
  537. goto free_ringbuffer_add;
  538. }
  539. self->outbound_events = jack_ringbuffer_create (MAX_EVENT_SIZE * 16 * sizeof(struct a2j_delivery_event));
  540. if (self->outbound_events == NULL) {
  541. goto free_ringbuffer_del;
  542. }
  543. if (!a2j_stream_init (self, A2J_PORT_CAPTURE)) {
  544. goto free_ringbuffer_outbound;
  545. }
  546. if (!a2j_stream_init (self, A2J_PORT_PLAYBACK)) {
  547. goto close_capture_stream;
  548. }
  549. if ((error = snd_seq_open(&self->seq, "hw", SND_SEQ_OPEN_DUPLEX, 0)) < 0) {
  550. a2j_error("failed to open alsa seq");
  551. goto close_playback_stream;
  552. }
  553. if ((error = snd_seq_set_client_name(self->seq, "jackmidi")) < 0) {
  554. a2j_error("snd_seq_set_client_name() failed");
  555. goto close_seq_client;
  556. }
  557. if ((self->port_id = snd_seq_create_simple_port(
  558. self->seq,
  559. "port",
  560. SND_SEQ_PORT_CAP_READ|SND_SEQ_PORT_CAP_WRITE
  561. #ifndef DEBUG
  562. |SND_SEQ_PORT_CAP_NO_EXPORT
  563. #endif
  564. ,SND_SEQ_PORT_TYPE_APPLICATION)) < 0) {
  565. a2j_error("snd_seq_create_simple_port() failed");
  566. goto close_seq_client;
  567. }
  568. if ((self->client_id = snd_seq_client_id(self->seq)) < 0) {
  569. a2j_error("snd_seq_client_id() failed");
  570. goto close_seq_client;
  571. }
  572. if ((self->queue = snd_seq_alloc_queue(self->seq)) < 0) {
  573. a2j_error("snd_seq_alloc_queue() failed");
  574. goto close_seq_client;
  575. }
  576. snd_seq_start_queue (self->seq, self->queue, 0);
  577. if ((error = snd_seq_nonblock(self->seq, 1)) < 0) {
  578. a2j_error("snd_seq_nonblock() failed");
  579. goto close_seq_client;
  580. }
  581. snd_seq_drop_input (self->seq);
  582. a2j_add_ports(&self->stream[A2J_PORT_CAPTURE]);
  583. a2j_add_ports(&self->stream[A2J_PORT_PLAYBACK]);
  584. if (sem_init(&self->output_semaphore, 0, 0) < 0) {
  585. a2j_error("can't create IO semaphore");
  586. goto close_jack_client;
  587. }
  588. self->running = true;
  589. if (pthread_create(&self->alsa_input_thread, NULL, alsa_input_thread, self) < 0) {
  590. a2j_error("cannot start ALSA input thread");
  591. goto sem_destroy;
  592. }
  593. /* wake the poll loop in the alsa input thread so initial ports are fetched */
  594. if ((error = snd_seq_connect_from (self->seq, self->port_id, SND_SEQ_CLIENT_SYSTEM, SND_SEQ_PORT_SYSTEM_ANNOUNCE)) < 0) {
  595. a2j_error("snd_seq_connect_from() failed");
  596. goto join_input_thread;
  597. }
  598. if (pthread_create(&self->alsa_output_thread, NULL, alsa_output_thread, self) < 0) {
  599. a2j_error("cannot start ALSA input thread");
  600. goto sem_destroy;
  601. }
  602. return 0;
  603. /* error handling */
  604. self->running = false; /* tell alsa threads to stop */
  605. self->finishing = false;
  606. snd_seq_disconnect_from(self->seq, self->port_id, SND_SEQ_CLIENT_SYSTEM, SND_SEQ_PORT_SYSTEM_ANNOUNCE);
  607. join_input_thread:
  608. pthread_join (self->alsa_input_thread, &thread_status);
  609. sem_destroy:
  610. sem_destroy (&self->output_semaphore);
  611. close_jack_client:
  612. if ((error = jack_client_close(self->jack_client)) < 0) {
  613. a2j_error("Cannot close jack client");
  614. }
  615. close_seq_client:
  616. snd_seq_close(self->seq);
  617. close_playback_stream:
  618. a2j_stream_close(self, A2J_PORT_PLAYBACK);
  619. close_capture_stream:
  620. a2j_stream_close(self, A2J_PORT_CAPTURE);
  621. free_ringbuffer_outbound:
  622. jack_ringbuffer_free(self->outbound_events);
  623. free_ringbuffer_del:
  624. jack_ringbuffer_free(self->port_del);
  625. free_ringbuffer_add:
  626. jack_ringbuffer_free(self->port_add);
  627. free_self:
  628. free(self);
  629. return -1;
  630. }
  631. /* JACK internal client API: 2 entry points
  632. */
  633. int
  634. jack_initialize (jack_client_t *client, const char* load_init)
  635. {
  636. struct a2j* self = calloc(1, sizeof(struct a2j));
  637. if (!self) {
  638. return -1;
  639. }
  640. self->jack_client = client;
  641. if (load_init) {
  642. char* args = strdup (load_init);
  643. char* token;
  644. char* ptr = args;
  645. char* savep;
  646. while (1) {
  647. if ((token = strtok_r (ptr, ", ", &savep)) == NULL) {
  648. break;
  649. }
  650. #ifdef A2J_DEBUG
  651. if (strncasecmp (token, "debug", 2) == 0) {
  652. a2j_do_debug = true;
  653. }
  654. #endif
  655. ptr = NULL;
  656. }
  657. free (args);
  658. }
  659. if (connect_to_alsa (self)) {
  660. free (self);
  661. return -1;
  662. }
  663. jack_set_process_callback (client, a2j_process, self);
  664. jack_set_freewheel_callback (client, a2j_freewheel, self);
  665. jack_on_shutdown (client, a2j_shutdown, self);
  666. jack_activate (client);
  667. return 0;
  668. }
  669. void
  670. jack_finish (void *arg)
  671. {
  672. struct a2j* self = (struct a2j*) arg;
  673. self->finishing = true;
  674. stop_threads (self);
  675. sem_destroy(&self->output_semaphore);
  676. jack_ringbuffer_reset (self->port_add);
  677. a2j_stream_detach (&self->stream[A2J_PORT_CAPTURE]);
  678. a2j_stream_detach (&self->stream[A2J_PORT_PLAYBACK]);
  679. snd_seq_close(self->seq);
  680. self->seq = NULL;
  681. a2j_stream_close (self, A2J_PORT_CAPTURE);
  682. a2j_stream_close (self, A2J_PORT_PLAYBACK);
  683. jack_ringbuffer_free(self->outbound_events);
  684. jack_ringbuffer_free(self->port_add);
  685. jack_ringbuffer_free(self->port_del);
  686. free (self);
  687. }