jack1 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.

2237 lines
53KB

  1. /*
  2. Copyright (C) 2001 Paul Davis
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 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 General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  14. $Id$
  15. */
  16. #include <unistd.h>
  17. #include <sys/socket.h>
  18. #include <sys/poll.h>
  19. #include <sys/un.h>
  20. #include <sys/stat.h>
  21. #include <errno.h>
  22. #include <fcntl.h>
  23. #include <sys/shm.h>
  24. #include <stdio.h>
  25. #include <stdarg.h>
  26. #include <sys/ipc.h>
  27. #include <signal.h>
  28. #include <sys/types.h>
  29. #include <string.h>
  30. #include <limits.h>
  31. #include <sys/mman.h>
  32. #include <asm/msr.h>
  33. #include <jack/internal.h>
  34. #include <jack/engine.h>
  35. #include <jack/driver.h>
  36. typedef struct {
  37. jack_port_internal_t *source;
  38. jack_port_internal_t *destination;
  39. } jack_connection_internal_t;
  40. typedef struct _jack_client_internal {
  41. jack_client_control_t *control;
  42. int request_fd;
  43. int event_fd;
  44. int subgraph_start_fd;
  45. int subgraph_wait_fd;
  46. GSList *ports; /* protected by engine->graph_lock */
  47. int shm_id;
  48. int shm_key;
  49. unsigned long rank;
  50. struct _jack_client_internal *next_client; /* not a linked list! */
  51. dlhandle handle;
  52. } jack_client_internal_t;
  53. static int jack_port_assign_buffer (jack_engine_t *, jack_port_internal_t *);
  54. static jack_port_internal_t *jack_get_port_by_name (jack_engine_t *, const char *name);
  55. static void jack_client_delete (jack_engine_t *, jack_client_internal_t *);
  56. static void jack_remove_client (jack_engine_t *engine, jack_client_internal_t *client);
  57. static jack_client_internal_t *jack_client_internal_new (jack_engine_t *engine, int fd, jack_client_connect_request_t *);
  58. static jack_client_internal_t *jack_client_internal_by_id (jack_engine_t *engine, jack_client_id_t id);
  59. static void jack_sort_graph (jack_engine_t *engine, int take_lock);
  60. static int jack_rechain_graph (jack_engine_t *engine, int take_lock);
  61. static int jack_get_fifo_fd (jack_engine_t *engine, int which_fifo);
  62. static int jack_create_fifo (jack_engine_t *engine, int which_fifo);
  63. static int jack_port_do_connect (jack_engine_t *engine, const char *source_port, const char *destination_port);
  64. static int jack_port_do_disconnect (jack_engine_t *engine, const char *source_port, const char *destination_port);
  65. static int jack_port_do_unregister (jack_engine_t *engine, jack_request_t *);
  66. static int jack_port_do_register (jack_engine_t *engine, jack_request_t *);
  67. static void jack_port_release (jack_engine_t *engine, jack_port_internal_t *);
  68. static void jack_port_clear_connections (jack_engine_t *engine, jack_port_internal_t *port);
  69. static int jack_port_disconnect_internal (jack_engine_t *engine, jack_port_internal_t *src,
  70. jack_port_internal_t *dst, int sort_graph);
  71. static void jack_port_registration_notify (jack_engine_t *, jack_port_id_t, int);
  72. static int jack_send_connection_notification (jack_engine_t *, jack_client_id_t, jack_port_id_t, jack_port_id_t, int);
  73. static int jack_deliver_event (jack_engine_t *, jack_client_internal_t *, jack_event_t *);
  74. static void jack_audio_port_mixdown (jack_port_t *port, nframes_t nframes);
  75. /* This is a disgusting kludge to work around issues with shmat.
  76. A more robust solution is needed.
  77. */
  78. static char *top_end_of_unmapped_memory = (char *) (1048576 * 1536); /* 1.5GB */
  79. static char *low_end_of_unmapped_memory = (char *) (1048576 * 1024); /* 1GB */
  80. static char *
  81. fixed_shmat (int shmid, char *shmaddr, int shmflg, size_t size)
  82. {
  83. char *addr;
  84. char *attempt;
  85. if (shmaddr != 0) {
  86. return shmat (shmid, shmaddr, shmflg);
  87. }
  88. attempt = (char *) (top_end_of_unmapped_memory - size);
  89. while (attempt > low_end_of_unmapped_memory) {
  90. if ((addr = (char *) shmat (shmid, attempt, shmflg|SHM_RND)) != (char *) -1) {
  91. top_end_of_unmapped_memory = addr;
  92. return addr;
  93. }
  94. attempt -= size;
  95. }
  96. return (char *) -1;
  97. }
  98. jack_port_type_info_t builtin_port_types[] = {
  99. { JACK_DEFAULT_AUDIO_TYPE, jack_audio_port_mixdown, 1 },
  100. { 0, NULL }
  101. };
  102. static inline int
  103. jack_client_is_inprocess (jack_client_internal_t *client)
  104. {
  105. return (client->control->type == ClientDynamic) || (client->control->type == ClientDriver);
  106. }
  107. static
  108. void shm_destroy (int status, void *arg)
  109. {
  110. int shm_id = (int) arg;
  111. shmctl (shm_id, IPC_RMID, 0);
  112. }
  113. static
  114. void unlink_path (int status, void *arg)
  115. {
  116. char *path = (char *) arg;
  117. unlink (path);
  118. free (arg);
  119. }
  120. static int
  121. make_sockets (int fd[2])
  122. {
  123. struct sockaddr_un addr;
  124. int i;
  125. /* First, the master server socket */
  126. if ((fd[0] = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) {
  127. jack_error ("cannot create server socket (%s)", strerror (errno));
  128. return -1;
  129. }
  130. addr.sun_family = AF_UNIX;
  131. for (i = 0; i < 999; i++) {
  132. snprintf (addr.sun_path, sizeof (addr.sun_path) - 1, "/tmp/jack_%d", i);
  133. if (access (addr.sun_path, F_OK) != 0) {
  134. break;
  135. }
  136. }
  137. if (i == 999) {
  138. jack_error ("all possible server socket names in use!!!");
  139. close (fd[0]);
  140. return -1;
  141. }
  142. on_exit (unlink_path, (void *) strdup (addr.sun_path));
  143. if (bind (fd[0], (struct sockaddr *) &addr, sizeof (addr)) < 0) {
  144. jack_error ("cannot bind server to socket (%s)", strerror (errno));
  145. close (fd[0]);
  146. return -1;
  147. }
  148. if (listen (fd[0], 1) < 0) {
  149. jack_error ("cannot enable listen on server socket (%s)", strerror (errno));
  150. close (fd[0]);
  151. return -1;
  152. }
  153. /* Now the client/server event ack server socket */
  154. if ((fd[1] = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) {
  155. jack_error ("cannot create event ACK socket (%s)", strerror (errno));
  156. close (fd[0]);
  157. return -1;
  158. }
  159. addr.sun_family = AF_UNIX;
  160. for (i = 0; i < 999; i++) {
  161. snprintf (addr.sun_path, sizeof (addr.sun_path) - 1, "/tmp/jack_ack_%d", i);
  162. if (access (addr.sun_path, F_OK) != 0) {
  163. break;
  164. }
  165. }
  166. if (i == 999) {
  167. jack_error ("all possible server ACK socket names in use!!!");
  168. close (fd[0]);
  169. close (fd[1]);
  170. return -1;
  171. }
  172. on_exit (unlink_path, (void *) strdup (addr.sun_path));
  173. if (bind (fd[1], (struct sockaddr *) &addr, sizeof (addr)) < 0) {
  174. jack_error ("cannot bind server to socket (%s)", strerror (errno));
  175. close (fd[0]);
  176. close (fd[1]);
  177. return -1;
  178. }
  179. if (listen (fd[1], 1) < 0) {
  180. jack_error ("cannot enable listen on server socket (%s)", strerror (errno));
  181. close (fd[0]);
  182. close (fd[1]);
  183. return -1;
  184. }
  185. return 0;
  186. }
  187. static void
  188. jack_cleanup_clients (jack_engine_t *engine)
  189. {
  190. jack_client_control_t *ctl;
  191. jack_client_internal_t *client;
  192. GSList *node;
  193. GSList *remove = 0;
  194. static int x = 0;
  195. x++;
  196. pthread_mutex_lock (&engine->graph_lock);
  197. for (node = engine->clients; node; node = g_slist_next (node)) {
  198. client = (jack_client_internal_t *) node->data;
  199. ctl = client->control;
  200. if (ctl->state > JACK_CLIENT_STATE_NOT_TRIGGERED) {
  201. remove = g_slist_prepend (remove, node->data);
  202. printf ("%d: removing failed client %s\n", x, client->control->name);
  203. }
  204. }
  205. pthread_mutex_unlock (&engine->graph_lock);
  206. if (remove) {
  207. for (node = remove; node; node = g_slist_next (node)) {
  208. jack_remove_client (engine, (jack_client_internal_t *) node->data);
  209. }
  210. g_slist_free (remove);
  211. }
  212. }
  213. static int
  214. jack_add_port_segment (jack_engine_t *engine, unsigned long nports)
  215. {
  216. jack_port_segment_info_t *si;
  217. key_t key;
  218. int id;
  219. char *addr;
  220. int offset;
  221. size_t size;
  222. size_t step;
  223. key = random();
  224. size = nports * sizeof (sample_t) * engine->control->buffer_size;
  225. if ((id = shmget (key, size, IPC_CREAT|0666)) < 0) {
  226. jack_error ("cannot create new port segment of %d bytes, key = 0x%x (%s)", size, key, strerror (errno));
  227. return -1;
  228. }
  229. if ((addr = fixed_shmat (id, 0, 0, size)) == (char *) -1) {
  230. jack_error ("cannot attach new port segment (%s)", strerror (errno));
  231. shmctl (id, IPC_RMID, 0);
  232. return -1;
  233. }
  234. on_exit (shm_destroy, (void *) id);
  235. si = (jack_port_segment_info_t *) malloc (sizeof (jack_port_segment_info_t));
  236. si->shm_key = key;
  237. si->address = addr;
  238. engine->port_segments = g_slist_prepend (engine->port_segments, si);
  239. engine->port_segment_key = key; /* XXX fix me */
  240. engine->port_segment_address = addr; /* XXX fix me */
  241. pthread_mutex_lock (&engine->buffer_lock);
  242. offset = 0;
  243. step = engine->control->buffer_size * sizeof (sample_t);
  244. while (offset < size) {
  245. jack_port_buffer_info_t *bi;
  246. bi = (jack_port_buffer_info_t *) malloc (sizeof (jack_port_buffer_info_t));
  247. bi->shm_key = key;
  248. bi->offset = offset;
  249. /* we append because we want the list to be in memory-address order */
  250. engine->port_buffer_freelist = g_slist_append (engine->port_buffer_freelist, bi);
  251. offset += step;
  252. }
  253. /* convert the first chunk of the segment into a zero-filled area */
  254. if (engine->silent_buffer == 0) {
  255. engine->silent_buffer = (jack_port_buffer_info_t *) engine->port_buffer_freelist->data;
  256. engine->port_buffer_freelist = g_slist_remove_link (engine->port_buffer_freelist, engine->port_buffer_freelist);
  257. memset (engine->port_segment_address + engine->silent_buffer->offset, 0,
  258. sizeof (sample_t) * engine->control->buffer_size);
  259. }
  260. pthread_mutex_unlock (&engine->buffer_lock);
  261. /* XXX notify all clients of new segment */
  262. return 0;
  263. }
  264. static int
  265. jack_set_buffer_size (jack_engine_t *engine, nframes_t nframes)
  266. {
  267. /* XXX this is not really right, since it only works for
  268. audio ports.
  269. */
  270. engine->control->buffer_size = nframes;
  271. jack_add_port_segment (engine, engine->control->port_max);
  272. return 0;
  273. }
  274. static int
  275. jack_set_sample_rate (jack_engine_t *engine, nframes_t nframes)
  276. {
  277. engine->control->sample_rate = nframes;
  278. return 0;
  279. }
  280. static int
  281. jack_process (jack_engine_t *engine, nframes_t nframes)
  282. {
  283. int err = 0;
  284. jack_client_internal_t *client;
  285. jack_client_control_t *ctl;
  286. GSList *node;
  287. struct pollfd pollfd[1];
  288. char c;
  289. // unsigned long then, now;
  290. // rdtscl (then);
  291. if (pthread_mutex_trylock (&engine->graph_lock) != 0) {
  292. return 0;
  293. }
  294. for (node = engine->clients; node; node = g_slist_next (node)) {
  295. ctl = ((jack_client_internal_t *) node->data)->control;
  296. ctl->state = JACK_CLIENT_STATE_NOT_TRIGGERED;
  297. ctl->nframes = nframes;
  298. }
  299. if (engine->timebase_client) {
  300. engine->control->frame_time = engine->timebase_client->control->frame_time;
  301. }
  302. for (node = engine->clients; err == 0 && node; ) {
  303. client = (jack_client_internal_t *) node->data;
  304. if (!client->control->active) {
  305. node = g_slist_next (node);
  306. continue;
  307. }
  308. ctl = client->control;
  309. if (jack_client_is_inprocess (client)) {
  310. /* in-process client ("plugin") */
  311. if (ctl->process (nframes, ctl->process_arg) == 0) {
  312. ctl->state = JACK_CLIENT_STATE_FINISHED;
  313. } else {
  314. jack_error ("in-process client %s failed", client->control->name);
  315. ctl->state = JACK_CLIENT_STATE_TRIGGERED;
  316. err++;
  317. break;
  318. }
  319. node = g_slist_next (node);
  320. } else {
  321. /* out of process subgraph */
  322. if (write (client->subgraph_start_fd, &c, sizeof (c)) != sizeof (c)) {
  323. jack_error ("cannot initiate graph processing (%s)", strerror (errno));
  324. err++;
  325. break;
  326. }
  327. /* now wait for the result. use poll instead of read so that we
  328. can timeout effectively.
  329. */
  330. pollfd[0].fd = client->subgraph_wait_fd;
  331. pollfd[0].events = POLLIN|POLLERR|POLLHUP|POLLNVAL;
  332. if (poll (pollfd, 1, engine->driver->period_interval) < 0) {
  333. jack_error ("engine cannot poll for graph completion (%s)", strerror (errno));
  334. err++;
  335. break;
  336. }
  337. if (pollfd[0].revents == 0) {
  338. jack_error ("subgraph starting at %s timed out (state = %d)",
  339. client->control->name, client->control->state);
  340. err++;
  341. break;
  342. } else if (pollfd[0].revents & ~POLLIN) {
  343. jack_error ("error/hangup on graph wait fd");
  344. err++;
  345. break;
  346. } else {
  347. if (read (client->subgraph_wait_fd, &c, sizeof (c)) != sizeof (c)) {
  348. jack_error ("cannot clean up byte from graph wait fd (%s)", strerror (errno));
  349. err++;
  350. break;
  351. }
  352. }
  353. /* Move to next in-process client (or end of client list) */
  354. while (node) {
  355. if (jack_client_is_inprocess (((jack_client_internal_t *) node->data))) {
  356. break;
  357. }
  358. node = g_slist_next (node);
  359. }
  360. }
  361. }
  362. pthread_mutex_unlock (&engine->graph_lock);
  363. if (err) {
  364. jack_cleanup_clients (engine);
  365. }
  366. // rdtscl (now);
  367. // printf ("engine cycle time: %.6f usecs\n", ((float) (now - then)) / 450.00f);
  368. return 0;
  369. }
  370. static int
  371. jack_load_client (jack_engine_t *engine, jack_client_internal_t *client, const char *path_to_so)
  372. {
  373. const char *errstr;
  374. dlhandle handle;
  375. handle = dlopen (path_to_so, RTLD_NOW|RTLD_GLOBAL);
  376. if (handle == 0) {
  377. if ((errstr = dlerror ()) != 0) {
  378. jack_error ("can't load \"%s\": %s", path_to_so, errstr);
  379. } else {
  380. jack_error ("bizarre error loading driver shared object %s", path_to_so);
  381. }
  382. return -1;
  383. }
  384. client->handle = handle;
  385. #if 0
  386. initialize = dlsym (handle, "client_initialize");
  387. if ((errstr = dlerror ()) != 0) {
  388. jack_error ("no initialize function in shared object %s\n", path_to_so);
  389. dlclose (handle);
  390. return -1;
  391. }
  392. finish = dlsym (handle, "client_finish");
  393. if ((errstr = dlerror ()) != 0) {
  394. jack_error ("no finish function in in shared driver object %s", path_to_so);
  395. dlclose (handle);
  396. return -1;
  397. }
  398. #endif
  399. return 0;
  400. }
  401. static void
  402. jack_client_unload (jack_client_internal_t *client)
  403. {
  404. if (client->handle) {
  405. // client->finish (client);
  406. dlclose (client->handle);
  407. }
  408. }
  409. static int
  410. handle_new_client (jack_engine_t *engine, int client_fd)
  411. {
  412. jack_client_internal_t *client;
  413. jack_client_connect_request_t req;
  414. jack_client_connect_result_t res;
  415. if (read (client_fd, &req, sizeof (req)) != sizeof (req)) {
  416. jack_error ("cannot read connection request from client");
  417. return -1;
  418. }
  419. res.status = 0;
  420. if ((client = jack_client_internal_new (engine, client_fd, &req)) == 0) {
  421. jack_error ("cannot create new client object");
  422. return -1;
  423. }
  424. printf ("new client: %s, type %d @ %p\n", client->control->name, req.type, client->control);
  425. res.status = 0;
  426. res.client_key = client->shm_key;
  427. res.control_key = engine->control_key;
  428. res.port_segment_key = engine->port_segment_key;
  429. res.port_segment_address = engine->port_segment_address;
  430. res.realtime = engine->control->real_time;
  431. res.realtime_priority = engine->rtpriority - 1;
  432. if (jack_client_is_inprocess (client)) {
  433. res.client_control = client->control;
  434. res.engine_control = engine->control;
  435. } else {
  436. strcpy (res.fifo_prefix, engine->fifo_prefix);
  437. }
  438. res.status = 0;
  439. if (write (client->request_fd, &res, sizeof (res)) != sizeof (res)) {
  440. jack_error ("cannot write connection response to client");
  441. jack_client_delete (engine, client);
  442. return -1;
  443. }
  444. if (res.status) {
  445. return res.status;
  446. }
  447. pthread_mutex_lock (&engine->graph_lock);
  448. engine->clients = g_slist_prepend (engine->clients, client);
  449. pthread_mutex_unlock (&engine->graph_lock);
  450. if (client->control->type != ClientDynamic) {
  451. if (engine->pfd_max >= engine->pfd_size) {
  452. engine->pfd = (struct pollfd *) realloc (engine->pfd, sizeof (struct pollfd) * engine->pfd_size + 16);
  453. engine->pfd_size += 16;
  454. }
  455. engine->pfd[engine->pfd_max].fd = client->request_fd;
  456. engine->pfd[engine->pfd_max].events = POLLIN|POLLPRI|POLLERR|POLLHUP|POLLNVAL;
  457. engine->pfd_max++;
  458. }
  459. return 0;
  460. }
  461. static int
  462. handle_client_ack_connection (jack_engine_t *engine, int client_fd)
  463. {
  464. jack_client_internal_t *client;
  465. jack_client_connect_ack_request_t req;
  466. jack_client_connect_ack_result_t res;
  467. if (read (client_fd, &req, sizeof (req)) != sizeof (req)) {
  468. jack_error ("cannot read ACK connection request from client");
  469. return -1;
  470. }
  471. if ((client = jack_client_internal_by_id (engine, req.client_id)) == NULL) {
  472. jack_error ("unknown client ID in ACK connection request");
  473. return -1;
  474. }
  475. fprintf (stderr, "client %s is on event fd %d\n", client->control->name, client_fd);
  476. client->event_fd = client_fd;
  477. res.status = 0;
  478. if (write (client->event_fd, &res, sizeof (res)) != sizeof (res)) {
  479. jack_error ("cannot write ACK connection response to client");
  480. return -1;
  481. }
  482. return 0;
  483. }
  484. static int
  485. jack_client_drop (jack_engine_t *engine, jack_client_id_t id)
  486. {
  487. jack_client_internal_t *client;
  488. if ((client = jack_client_internal_by_id (engine, id)) == 0) {
  489. jack_error ("unknown client ID in DropClient request");
  490. return -1;
  491. }
  492. jack_remove_client (engine, client);
  493. return 0;
  494. }
  495. #if 0
  496. static int
  497. jack_client_has_connections (jack_client_internal_t *client)
  498. {
  499. GSList *node;
  500. for (node = client->ports; node; node = g_slist_next (node)) {
  501. if (((jack_port_internal_t *) node->data)->connections) {
  502. return TRUE;
  503. }
  504. }
  505. return FALSE;
  506. }
  507. #endif
  508. static int
  509. jack_client_activate (jack_engine_t *engine, jack_client_id_t id)
  510. {
  511. jack_client_internal_t *client;
  512. GSList *node;
  513. int ret = -1;
  514. pthread_mutex_lock (&engine->graph_lock);
  515. for (node = engine->clients; node; node = g_slist_next (node)) {
  516. if (((jack_client_internal_t *) node->data)->control->id == id) {
  517. client = (jack_client_internal_t *) node->data;
  518. if (!jack_client_is_inprocess (client)) {
  519. jack_create_fifo (engine, ++engine->external_client_cnt);
  520. }
  521. client->control->active = TRUE;
  522. jack_rechain_graph (engine, FALSE);
  523. ret = 0;
  524. break;
  525. }
  526. }
  527. pthread_mutex_unlock (&engine->graph_lock);
  528. return ret;
  529. }
  530. static int
  531. jack_client_do_deactivate (jack_engine_t *engine, jack_client_internal_t *client)
  532. {
  533. /* called must hold engine->graph_lock and must have checked for and/or
  534. cleared all connections held by client.
  535. */
  536. client->control->active = FALSE;
  537. if (!jack_client_is_inprocess (client)) {
  538. engine->external_client_cnt--;
  539. }
  540. jack_sort_graph (engine, FALSE);
  541. return 0;
  542. }
  543. static void
  544. jack_client_disconnect (jack_engine_t *engine, jack_client_internal_t *client)
  545. {
  546. GSList *node;
  547. jack_port_internal_t *port;
  548. /* call tree **** MUST HOLD *** engine->graph_lock */
  549. for (node = client->ports; node; node = g_slist_next (node)) {
  550. port = (jack_port_internal_t *) node->data;
  551. jack_port_clear_connections (engine, port);
  552. jack_port_release (engine, port);
  553. }
  554. g_slist_free (client->ports);
  555. client->ports = 0;
  556. }
  557. static int
  558. jack_client_deactivate (jack_engine_t *engine, jack_client_id_t id, int to_wait)
  559. {
  560. GSList *node;
  561. int ret = -1;
  562. pthread_mutex_lock (&engine->graph_lock);
  563. printf ("trying ... deactivating client id %d\n", id);
  564. for (node = engine->clients; node; node = g_slist_next (node)) {
  565. jack_client_internal_t *client = (jack_client_internal_t *) node->data;
  566. if (client->control->id == id) {
  567. printf ("deactivating client id %d\n", id);
  568. if (client == engine->timebase_client) {
  569. engine->timebase_client = 0;
  570. engine->control->frame_time = 0;
  571. }
  572. jack_client_disconnect (engine, client);
  573. ret = jack_client_do_deactivate (engine, node->data);
  574. break;
  575. }
  576. }
  577. pthread_mutex_unlock (&engine->graph_lock);
  578. return ret;
  579. }
  580. static int
  581. jack_set_timebase (jack_engine_t *engine, jack_client_id_t client)
  582. {
  583. int ret = -1;
  584. pthread_mutex_lock (&engine->graph_lock);
  585. if ((engine->timebase_client = jack_client_internal_by_id (engine, client)) != 0) {
  586. engine->control->frame_time = engine->timebase_client->control->frame_time;
  587. ret = 0;
  588. }
  589. pthread_mutex_unlock (&engine->graph_lock);
  590. return ret;
  591. }
  592. static int
  593. handle_client_jack_error (jack_engine_t *engine, int fd)
  594. {
  595. jack_client_internal_t *client = 0;
  596. GSList *node;
  597. pthread_mutex_lock (&engine->graph_lock);
  598. for (node = engine->clients; node; node = g_slist_next (node)) {
  599. if (((jack_client_internal_t *) node->data)->request_fd == fd) {
  600. client = (jack_client_internal_t *) node->data;
  601. break;
  602. }
  603. }
  604. pthread_mutex_unlock (&engine->graph_lock);
  605. if (client == 0) {
  606. jack_error ("i/o error on unknown client fd %d", fd);
  607. return -1;
  608. }
  609. jack_remove_client (engine, client);
  610. return 0;
  611. }
  612. static int
  613. jack_client_port_monitor (jack_engine_t *engine, jack_port_id_t port_id, int onoff)
  614. {
  615. jack_port_shared_t *port;
  616. jack_client_internal_t *client = NULL;
  617. jack_event_t event;
  618. if (port_id < 0 || port_id >= engine->port_max) {
  619. jack_error ("illegal port ID in port monitor request");
  620. return -1;
  621. }
  622. port = &engine->control->ports[port_id];
  623. if (!(port->flags & JackPortCanMonitor)) {
  624. jack_error ("port monitor request made on a port (%s) that doesn't support monitoring",
  625. port->name);
  626. return -1;
  627. }
  628. pthread_mutex_lock (&engine->graph_lock);
  629. if ((client = jack_client_internal_by_id (engine, port->client_id)) == NULL) {
  630. jack_error ("unknown client owns port %d!!", port_id);
  631. pthread_mutex_unlock (&engine->graph_lock);
  632. return -1;
  633. }
  634. pthread_mutex_unlock (&engine->graph_lock);
  635. event.type = (onoff ? PortMonitor : PortUnMonitor);
  636. event.x.port_id = port_id;
  637. return jack_deliver_event (engine, client, &event);
  638. }
  639. static int
  640. handle_client_io (jack_engine_t *engine, int fd)
  641. {
  642. jack_request_t req;
  643. jack_client_internal_t *client = 0;
  644. int reply_fd;
  645. GSList *node;
  646. pthread_mutex_lock (&engine->graph_lock);
  647. for (node = engine->clients; node; node = g_slist_next (node)) {
  648. if (((jack_client_internal_t *) node->data)->request_fd == fd) {
  649. client = (jack_client_internal_t *) node->data;
  650. break;
  651. }
  652. }
  653. pthread_mutex_unlock (&engine->graph_lock);
  654. if (client == 0) {
  655. jack_error ("client input on unknown fd %d!", fd);
  656. return -1;
  657. }
  658. if (read (client->request_fd, &req, sizeof (req)) < sizeof (req)) {
  659. jack_error ("cannot read request from client");
  660. jack_remove_client (engine, client);
  661. return -1;
  662. }
  663. reply_fd = client->request_fd;
  664. switch (req.type) {
  665. case RegisterPort:
  666. req.status = jack_port_do_register (engine, &req);
  667. break;
  668. case UnRegisterPort:
  669. req.status = jack_port_do_unregister (engine, &req);
  670. break;
  671. case ConnectPorts:
  672. req.status = jack_port_do_connect (engine, req.x.connect.source_port, req.x.connect.destination_port);
  673. break;
  674. case DisconnectPorts:
  675. req.status = jack_port_do_disconnect (engine, req.x.connect.source_port, req.x.connect.destination_port);
  676. break;
  677. case DropClient:
  678. req.status = jack_client_drop (engine, req.x.client_id);
  679. reply_fd = -1;
  680. break;
  681. case ActivateClient:
  682. req.status = jack_client_activate (engine, req.x.client_id);
  683. break;
  684. case DeactivateClient:
  685. req.status = jack_client_deactivate (engine, req.x.client_id, TRUE);
  686. break;
  687. case SetTimeBaseClient:
  688. req.status = jack_set_timebase (engine, req.x.client_id);
  689. break;
  690. case RequestPortMonitor:
  691. req.status = jack_client_port_monitor (engine, req.x.port_info.port_id, TRUE);
  692. break;
  693. case RequestPortUnMonitor:
  694. req.status = jack_client_port_monitor (engine, req.x.port_info.port_id, FALSE);
  695. break;
  696. }
  697. if (reply_fd >= 0) {
  698. if (write (reply_fd, &req, sizeof (req)) < sizeof (req)) {
  699. jack_error ("cannot write request result to client");
  700. return -1;
  701. }
  702. }
  703. return 0;
  704. }
  705. static void *
  706. jack_server_thread (void *arg)
  707. {
  708. jack_engine_t *engine = (jack_engine_t *) arg;
  709. struct sockaddr_un client_addr;
  710. socklen_t client_addrlen;
  711. struct pollfd *pfd;
  712. int client_socket;
  713. int done = 0;
  714. int i;
  715. int max;
  716. pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  717. engine->pfd[0].fd = engine->fds[0];
  718. engine->pfd[0].events = POLLIN|POLLERR;
  719. engine->pfd[1].fd = engine->fds[1];
  720. engine->pfd[1].events = POLLIN|POLLERR;
  721. engine->pfd_max = 2;
  722. while (!done) {
  723. /* XXX race here with new external clients
  724. causing engine->pfd to be reallocated.
  725. I don't know how to solve this
  726. short of copying the entire
  727. contents of the pfd struct. Ick.
  728. */
  729. max = engine->pfd_max;
  730. pfd = engine->pfd;
  731. if (poll (pfd, max, 10000) < 0) {
  732. if (errno == EINTR) {
  733. continue;
  734. }
  735. jack_error ("poll failed (%s)", strerror (errno));
  736. break;
  737. }
  738. /* check the master server socket */
  739. if (pfd[0].revents & POLLERR) {
  740. jack_error ("error on server socket");
  741. break;
  742. }
  743. if (pfd[0].revents & POLLIN) {
  744. memset (&client_addr, 0, sizeof (client_addr));
  745. client_addrlen = sizeof (client_addr);
  746. if ((client_socket = accept (engine->fds[0], (struct sockaddr *) &client_addr, &client_addrlen)) < 0) {
  747. jack_error ("cannot accept new connection (%s)", strerror (errno));
  748. } else if (handle_new_client (engine, client_socket) < 0) {
  749. jack_error ("cannot complete new client connection process");
  750. close (client_socket);
  751. }
  752. }
  753. /* check the ACK server socket */
  754. if (pfd[1].revents & POLLERR) {
  755. jack_error ("error on server ACK socket");
  756. break;
  757. }
  758. if (pfd[1].revents & POLLIN) {
  759. memset (&client_addr, 0, sizeof (client_addr));
  760. client_addrlen = sizeof (client_addr);
  761. if ((client_socket = accept (engine->fds[1], (struct sockaddr *) &client_addr, &client_addrlen)) < 0) {
  762. jack_error ("cannot accept new ACK connection (%s)", strerror (errno));
  763. } else if (handle_client_ack_connection (engine, client_socket)) {
  764. jack_error ("cannot complete client ACK connection process");
  765. close (client_socket);
  766. }
  767. }
  768. /* check each client socket */
  769. for (i = 2; i < max; i++) {
  770. if (pfd[i].fd < 0) {
  771. continue;
  772. }
  773. if (pfd[i].revents & ~POLLIN) {
  774. handle_client_jack_error (engine, pfd[i].fd);
  775. } else if (pfd[i].revents & POLLIN) {
  776. if (handle_client_io (engine, pfd[i].fd)) {
  777. jack_error ("bad hci\n");
  778. }
  779. }
  780. }
  781. }
  782. return 0;
  783. }
  784. static void
  785. jack_start_server (jack_engine_t *engine)
  786. {
  787. pthread_create (&engine->server_thread, 0, &jack_server_thread, engine);
  788. pthread_detach (engine->server_thread);
  789. }
  790. jack_engine_t *
  791. jack_engine_new (int realtime, int rtpriority)
  792. {
  793. jack_engine_t *engine;
  794. size_t control_size;
  795. void *addr;
  796. int i;
  797. engine = (jack_engine_t *) malloc (sizeof (jack_engine_t));
  798. engine->driver = 0;
  799. engine->process = jack_process;
  800. engine->set_sample_rate = jack_set_sample_rate;
  801. engine->set_buffer_size = jack_set_buffer_size;
  802. engine->next_client_id = 1;
  803. engine->timebase_client = 0;
  804. engine->port_max = 128;
  805. engine->rtpriority = rtpriority;
  806. engine->silent_buffer = 0;
  807. engine->getthehelloutathere = FALSE;
  808. pthread_mutex_init (&engine->graph_lock, 0);
  809. pthread_mutex_init (&engine->buffer_lock, 0);
  810. pthread_mutex_init (&engine->port_lock, 0);
  811. engine->clients = 0;
  812. engine->port_segments = 0;
  813. engine->port_buffer_freelist = 0;
  814. engine->pfd_size = 16;
  815. engine->pfd_max = 0;
  816. engine->pfd = (struct pollfd *) malloc (sizeof (struct pollfd) * engine->pfd_size);
  817. engine->fifo_size = 16;
  818. engine->fifo = (int *) malloc (sizeof (int) * engine->fifo_size);
  819. for (i = 0; i < engine->fifo_size; i++) {
  820. engine->fifo[i] = -1;
  821. }
  822. /* Build a linked list of known port types. We use a list so that
  823. we can easily manage other data types without messing with
  824. reallocation of arrays, etc.
  825. */
  826. engine->port_types = NULL;
  827. for (i = 0; builtin_port_types[i].type_name; i++) {
  828. engine->port_types = g_slist_append (engine->port_types, &builtin_port_types[i]);
  829. }
  830. engine->external_client_cnt = 0;
  831. srandom (time ((time_t *) 0));
  832. engine->control_key = random();
  833. control_size = sizeof (jack_control_t) + (sizeof (jack_port_shared_t) * engine->port_max);
  834. if ((engine->control_shm_id = shmget (engine->control_key, control_size, IPC_CREAT|0644)) < 0) {
  835. jack_error ("cannot create engine control shared memory segment (%s)", strerror (errno));
  836. return 0;
  837. }
  838. if ((addr = fixed_shmat (engine->control_shm_id, 0, 0, control_size)) == (void *) -1) {
  839. jack_error ("cannot attach control shared memory segment (%s)", strerror (errno));
  840. shmctl (engine->control_shm_id, IPC_RMID, 0);
  841. return 0;
  842. }
  843. on_exit (shm_destroy, (void *) engine->control_shm_id);
  844. engine->control = (jack_control_t *) addr;
  845. /* Mark all ports as available */
  846. for (i = 0; i < engine->port_max; i++) {
  847. engine->control->ports[i].in_use = 0;
  848. engine->control->ports[i].id = i;
  849. }
  850. /* allocate internal port structures so that we can keep
  851. track of port connections.
  852. */
  853. engine->internal_ports = (jack_port_internal_t *) malloc (sizeof (jack_port_internal_t) * engine->port_max);
  854. for (i = 0; i < engine->port_max; i++) {
  855. engine->internal_ports[i].connections = 0;
  856. }
  857. if (make_sockets (engine->fds) < 0) {
  858. jack_error ("cannot create server sockets");
  859. return 0;
  860. }
  861. engine->control->port_max = engine->port_max;
  862. engine->control->real_time = realtime;
  863. engine->control->client_priority = engine->rtpriority - 1;
  864. engine->control->sample_rate = 0;
  865. engine->control->buffer_size = 0;
  866. engine->control->frame_time = 0;
  867. sprintf (engine->fifo_prefix, "/tmp/jack_fifo_%d", getpid());
  868. jack_create_fifo (engine, 0);
  869. jack_start_server (engine);
  870. return engine;
  871. }
  872. static int
  873. jack_become_real_time (pthread_t thread, int priority)
  874. {
  875. struct sched_param rtparam;
  876. int x;
  877. memset (&rtparam, 0, sizeof (rtparam));
  878. rtparam.sched_priority = priority;
  879. if ((x = pthread_setschedparam (thread, SCHED_FIFO, &rtparam)) != 0) {
  880. jack_error ("cannot set thread to real-time priority (FIFO/%d) (%d: %s)", rtparam.sched_priority, x, strerror (errno));
  881. }
  882. if (mlockall (MCL_CURRENT | MCL_FUTURE) != 0) {
  883. jack_error ("cannot lock down memory for RT thread (%s)", strerror (errno));
  884. }
  885. return 0;
  886. }
  887. void
  888. cancel_cleanup1 (void *arg)
  889. {
  890. jack_engine_t *engine = (jack_engine_t *) arg;
  891. printf ("audio thread cancelled or finished\n");
  892. engine->driver->audio_stop (engine->driver);
  893. }
  894. void
  895. cancel_cleanup2 (int status, void *arg)
  896. {
  897. jack_engine_t *engine = (jack_engine_t *) arg;
  898. engine->driver->audio_stop (engine->driver);
  899. engine->driver->finish (engine->driver);
  900. }
  901. static void *
  902. jack_audio_thread (void *arg)
  903. {
  904. jack_engine_t *engine = (jack_engine_t *) arg;
  905. jack_driver_t *driver = engine->driver;
  906. unsigned long start, end;
  907. if (engine->control->real_time) {
  908. jack_become_real_time (pthread_self(), engine->rtpriority);
  909. }
  910. pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  911. on_exit (cancel_cleanup2, engine);
  912. if (driver->audio_start (driver)) {
  913. jack_error ("cannot start driver");
  914. pthread_exit (0);
  915. }
  916. while (1) {
  917. start = end;
  918. if (driver->wait (driver)) {
  919. break;
  920. }
  921. rdtscl (end);
  922. // printf ("driver cycle time: %.6f usecs\n", ((float) (end - start)) / 450.00f);
  923. }
  924. pthread_exit (0);
  925. }
  926. int
  927. jack_run (jack_engine_t *engine)
  928. {
  929. if (engine->driver == 0) {
  930. jack_error ("engine driver not set; cannot start");
  931. return -1;
  932. }
  933. return pthread_create (&engine->audio_thread, 0, jack_audio_thread, engine);
  934. }
  935. int
  936. jack_wait (jack_engine_t *engine)
  937. {
  938. void *ret = 0;
  939. int err;
  940. if ((err = pthread_join (engine->audio_thread, &ret)) != 0) {
  941. switch (err) {
  942. case EINVAL:
  943. jack_error ("cannot join with audio thread (thread detached, or another thread is waiting)");
  944. break;
  945. case ESRCH:
  946. jack_error ("cannot join with audio thread (thread no longer exists)");
  947. break;
  948. case EDEADLK:
  949. jack_error ("programming error: jack_wait() called by audio thread");
  950. break;
  951. default:
  952. jack_error ("cannot join with audio thread (%s)", strerror (errno));
  953. }
  954. }
  955. return (int) ret;
  956. }
  957. int
  958. jack_engine_delete (jack_engine_t *engine)
  959. {
  960. pthread_cancel (engine->audio_thread);
  961. return 0;
  962. }
  963. static jack_client_internal_t *
  964. jack_client_internal_new (jack_engine_t *engine, int fd, jack_client_connect_request_t *req)
  965. {
  966. jack_client_internal_t *client;
  967. key_t shm_key = 0;
  968. int shm_id = 0;
  969. void *addr = 0;
  970. switch (req->type) {
  971. case ClientDynamic:
  972. case ClientDriver:
  973. break;
  974. case ClientOutOfProcess:
  975. shm_key = random();
  976. if ((shm_id = shmget (shm_key, sizeof (jack_client_control_t), IPC_CREAT|0666)) < 0) {
  977. jack_error ("cannot create client control block");
  978. return 0;
  979. }
  980. if ((addr = fixed_shmat (shm_id, 0, 0, sizeof (jack_client_control_t))) == (void *) -1) {
  981. jack_error ("cannot attach new client control block");
  982. shmctl (shm_id, IPC_RMID, 0);
  983. return 0;
  984. }
  985. break;
  986. }
  987. client = (jack_client_internal_t *) malloc (sizeof (jack_client_internal_t));
  988. client->request_fd = fd;
  989. client->event_fd = -1;
  990. client->ports = 0;
  991. client->rank = UINT_MAX;
  992. client->next_client = NULL;
  993. client->handle = NULL;
  994. if (req->type != ClientOutOfProcess) {
  995. client->control = (jack_client_control_t *) malloc (sizeof (jack_client_control_t));
  996. } else {
  997. client->shm_id = shm_id;
  998. client->shm_key = shm_key;
  999. client->control = (jack_client_control_t *) addr;
  1000. }
  1001. client->control->type = req->type;
  1002. client->control->active = FALSE;
  1003. client->control->dead = FALSE;
  1004. client->control->id = engine->next_client_id++;
  1005. strcpy ((char *) client->control->name, req->name);
  1006. client->control->process = NULL;
  1007. client->control->process_arg = NULL;
  1008. client->control->bufsize = NULL;
  1009. client->control->bufsize_arg = NULL;
  1010. client->control->srate = NULL;
  1011. client->control->srate_arg = NULL;
  1012. client->control->port_register = NULL;
  1013. client->control->port_register_arg = NULL;
  1014. client->control->port_monitor = NULL;
  1015. client->control->port_monitor_arg = NULL;
  1016. if (req->type == ClientDynamic) {
  1017. if (jack_load_client (engine, client, req->object_path)) {
  1018. jack_error ("cannot dynamically load client from \"%s\"", req->object_path);
  1019. jack_client_delete (engine, client);
  1020. return 0;
  1021. }
  1022. }
  1023. return client;
  1024. }
  1025. static void
  1026. jack_port_clear_connections (jack_engine_t *engine, jack_port_internal_t *port)
  1027. {
  1028. GSList *node, *next;
  1029. for (node = port->connections; node; ) {
  1030. next = g_slist_next (node);
  1031. jack_port_disconnect_internal (engine,
  1032. ((jack_connection_internal_t *) node->data)->source,
  1033. ((jack_connection_internal_t *) node->data)->destination,
  1034. FALSE);
  1035. node = next;
  1036. }
  1037. g_slist_free (port->connections);
  1038. port->connections = 0;
  1039. }
  1040. static void
  1041. jack_remove_client (jack_engine_t *engine, jack_client_internal_t *client)
  1042. {
  1043. GSList *node;
  1044. int i;
  1045. printf ("removing client %s\n", client->control->name);
  1046. pthread_mutex_lock (&engine->graph_lock);
  1047. client->control->dead = TRUE;
  1048. if (client == engine->timebase_client) {
  1049. engine->timebase_client = 0;
  1050. engine->control->frame_time = 0;
  1051. }
  1052. jack_client_disconnect (engine, client);
  1053. jack_client_do_deactivate (engine, client);
  1054. for (node = engine->clients; node; node = g_slist_next (node)) {
  1055. if (((jack_client_internal_t *) node->data)->control->id == client->control->id) {
  1056. engine->clients = g_slist_remove_link (engine->clients, node);
  1057. g_slist_free_1 (node);
  1058. break;
  1059. }
  1060. }
  1061. /* rearrange the pollfd array so that things work right the
  1062. next time we go into poll(2).
  1063. */
  1064. for (i = 0; i < engine->pfd_max; i++) {
  1065. if (engine->pfd[i].fd == client->request_fd) {
  1066. if (i+1 < engine->pfd_max) {
  1067. memcpy (&engine->pfd[i], &engine->pfd[i+1], sizeof (struct pollfd) * (engine->pfd_max - i));
  1068. }
  1069. engine->pfd_max--;
  1070. }
  1071. }
  1072. close (client->event_fd);
  1073. close (client->request_fd);
  1074. jack_client_delete (engine, client);
  1075. pthread_mutex_unlock (&engine->graph_lock);
  1076. }
  1077. static void
  1078. jack_client_delete (jack_engine_t *engine, jack_client_internal_t *client)
  1079. {
  1080. jack_client_disconnect (engine, client);
  1081. if (jack_client_is_inprocess (client)) {
  1082. jack_client_unload (client);
  1083. free ((char *) client->control);
  1084. } else {
  1085. shmdt ((void *) client->control);
  1086. }
  1087. free (client);
  1088. }
  1089. jack_client_internal_t *
  1090. jack_client_by_name (jack_engine_t *engine, const char *name)
  1091. {
  1092. jack_client_internal_t *client = NULL;
  1093. GSList *node;
  1094. pthread_mutex_lock (&engine->graph_lock);
  1095. for (node = engine->clients; node; node = g_slist_next (node)) {
  1096. if (strcmp ((const char *) ((jack_client_internal_t *) node->data)->control->name, name) == 0) {
  1097. client = (jack_client_internal_t *) node->data;
  1098. break;
  1099. }
  1100. }
  1101. pthread_mutex_unlock (&engine->graph_lock);
  1102. return client;
  1103. }
  1104. jack_client_internal_t *
  1105. jack_client_internal_by_id (jack_engine_t *engine, jack_client_id_t id)
  1106. {
  1107. jack_client_internal_t *client = NULL;
  1108. GSList *node;
  1109. /* call tree ***MUST HOLD*** engine->graph_lock */
  1110. for (node = engine->clients; node; node = g_slist_next (node)) {
  1111. if (((jack_client_internal_t *) node->data)->control->id == id) {
  1112. client = (jack_client_internal_t *) node->data;
  1113. break;
  1114. }
  1115. }
  1116. return client;
  1117. }
  1118. static int
  1119. jack_deliver_event (jack_engine_t *engine, jack_client_internal_t *client, jack_event_t *event)
  1120. {
  1121. char status;
  1122. if (client->control->dead) {
  1123. return 0;
  1124. }
  1125. if (jack_client_is_inprocess (client)) {
  1126. switch (event->type) {
  1127. case PortConnected:
  1128. case PortDisconnected:
  1129. jack_client_handle_port_connection (client->control->private_internal_client, event);
  1130. break;
  1131. case GraphReordered:
  1132. jack_error ("reorder event delivered to internal client!");
  1133. break;
  1134. case BufferSizeChange:
  1135. if (client->control->bufsize) {
  1136. client->control->bufsize (event->x.n, client->control->bufsize_arg);
  1137. }
  1138. break;
  1139. case SampleRateChange:
  1140. if (client->control->srate) {
  1141. client->control->srate (event->x.n, client->control->bufsize_arg);
  1142. }
  1143. break;
  1144. case PortMonitor:
  1145. if (client->control->port_monitor) {
  1146. client->control->port_monitor (event->x.port_id, TRUE, client->control->port_monitor_arg);
  1147. }
  1148. break;
  1149. case PortUnMonitor:
  1150. if (client->control->port_monitor) {
  1151. client->control->port_monitor (event->x.port_id, FALSE, client->control->port_monitor_arg);
  1152. }
  1153. break;
  1154. default:
  1155. /* internal clients don't need to know */
  1156. break;
  1157. }
  1158. } else {
  1159. if (write (client->event_fd, event, sizeof (*event)) != sizeof (*event)) {
  1160. jack_error ("cannot send event to client [%s] (%s)", client->control->name, strerror (errno));
  1161. return -1;
  1162. }
  1163. if (read (client->event_fd, &status, sizeof (status)) != sizeof (status)) {
  1164. jack_error ("cannot read event response from client [%s] (%s)", client->control->name, strerror (errno));
  1165. return -1;
  1166. }
  1167. }
  1168. return 0;
  1169. }
  1170. int
  1171. jack_client_set_order (jack_engine_t *engine, jack_client_internal_t *client)
  1172. {
  1173. jack_event_t event;
  1174. event.type = GraphReordered;
  1175. event.x.n = client->rank;
  1176. return jack_deliver_event (engine, client, &event);
  1177. }
  1178. int
  1179. jack_rechain_graph (jack_engine_t *engine, int take_lock)
  1180. {
  1181. GSList *node, *next;
  1182. unsigned long n;
  1183. int err = 0;
  1184. int set;
  1185. jack_client_internal_t *client, *subgraph_client, *next_client;
  1186. if (take_lock) {
  1187. pthread_mutex_lock (&engine->graph_lock);
  1188. }
  1189. /* We're going to try to avoid reconnecting clients that
  1190. don't need to be reconnected. This is slightly tricky,
  1191. but worth it for performance reasons.
  1192. */
  1193. subgraph_client = 0;
  1194. if ((node = engine->clients) == 0) {
  1195. goto done;
  1196. }
  1197. client = (jack_client_internal_t *) node->data;
  1198. if ((next = g_slist_next (node)) == NULL) {
  1199. next_client = 0;
  1200. } else {
  1201. next_client = (jack_client_internal_t *) next->data;
  1202. }
  1203. n = 0;
  1204. do {
  1205. if (client->rank != n || client->next_client != next_client) {
  1206. client->rank = n;
  1207. client->next_client = next_client;
  1208. set = TRUE;
  1209. } else {
  1210. set = FALSE;
  1211. }
  1212. if (jack_client_is_inprocess (client)) {
  1213. if (subgraph_client) {
  1214. subgraph_client->subgraph_wait_fd = jack_get_fifo_fd (engine, n);
  1215. }
  1216. subgraph_client = 0;
  1217. } else {
  1218. if (subgraph_client == 0) {
  1219. subgraph_client = client;
  1220. subgraph_client->subgraph_start_fd = jack_get_fifo_fd (engine, n);
  1221. }
  1222. if (set) {
  1223. jack_client_set_order (engine, client);
  1224. }
  1225. n++;
  1226. }
  1227. if (next == 0) {
  1228. break;
  1229. }
  1230. node = next;
  1231. client = (jack_client_internal_t *) node->data;
  1232. if ((next = g_slist_next (node)) == 0) {
  1233. next_client = 0;
  1234. } else {
  1235. next_client = (jack_client_internal_t *) next->data;
  1236. }
  1237. } while (1);
  1238. if (subgraph_client) {
  1239. subgraph_client->subgraph_wait_fd = jack_get_fifo_fd (engine, n);
  1240. }
  1241. done:
  1242. if (take_lock) {
  1243. pthread_mutex_unlock (&engine->graph_lock);
  1244. }
  1245. return err;
  1246. }
  1247. int
  1248. jack_client_connected (jack_client_internal_t *a, jack_client_internal_t *b)
  1249. {
  1250. GSList *pnode, *cnode;
  1251. int ret = 0;
  1252. /* Check every port on the first client for a connection to
  1253. the second client.
  1254. Return -1 if a should execute before b
  1255. Return 1 if a should execute after b
  1256. Return 0 if they are not connected (ie. it doesn't matter)
  1257. If there is a feedback loop between a and b, the result
  1258. is undefined (the first connected port will be used to
  1259. determine the result). This is "OK", since there is no
  1260. correct execution order in that case.
  1261. */
  1262. for (pnode = a->ports; pnode; pnode = g_slist_next (pnode)) {
  1263. jack_port_internal_t *port;
  1264. port = (jack_port_internal_t *) pnode->data;
  1265. for (cnode = port->connections; cnode; cnode = g_slist_next (cnode)) {
  1266. jack_connection_internal_t *c;
  1267. c = (jack_connection_internal_t *) cnode->data;
  1268. if (c->source->shared->client_id == b->control->id) {
  1269. /* b is the source, so a should be
  1270. executed *after* b.
  1271. */
  1272. ret = 1;
  1273. break;
  1274. } else if (c->source->shared->client_id == a->control->id) {
  1275. /* a is the source, so a should be
  1276. executed *before* b
  1277. */
  1278. ret = -1;
  1279. break;
  1280. }
  1281. }
  1282. if (ret) {
  1283. break;
  1284. }
  1285. }
  1286. return ret;
  1287. }
  1288. static void
  1289. jack_sort_graph (jack_engine_t *engine, int take_lock)
  1290. {
  1291. if (take_lock) {
  1292. pthread_mutex_lock (&engine->graph_lock);
  1293. }
  1294. engine->clients = g_slist_sort (engine->clients, (GCompareFunc) jack_client_connected);
  1295. jack_rechain_graph (engine, FALSE);
  1296. if (take_lock) {
  1297. pthread_mutex_unlock (&engine->graph_lock);
  1298. }
  1299. }
  1300. static int
  1301. jack_port_do_connect (jack_engine_t *engine,
  1302. const char *source_port,
  1303. const char *destination_port)
  1304. {
  1305. jack_connection_internal_t *connection;
  1306. jack_port_internal_t *srcport, *dstport;
  1307. jack_port_id_t src_id, dst_id;
  1308. fprintf (stderr, "trying to connect %s and %s\n", source_port, destination_port);
  1309. if ((srcport = jack_get_port_by_name (engine, source_port)) == 0) {
  1310. jack_error ("unknown source port in attempted connection [%s]", source_port);
  1311. return -1;
  1312. }
  1313. if ((dstport = jack_get_port_by_name (engine, destination_port)) == 0) {
  1314. jack_error ("unknown destination port in attempted connection [%s]", destination_port);
  1315. return -1;
  1316. }
  1317. if ((dstport->shared->flags & JackPortIsInput) == 0) {
  1318. jack_error ("destination port in attempted connection is not an input port");
  1319. return -1;
  1320. }
  1321. if ((srcport->shared->flags & JackPortIsOutput) == 0) {
  1322. jack_error ("source port in attempted connection is not an output port");
  1323. return -1;
  1324. }
  1325. if (strcmp (srcport->shared->type_info.type_name,
  1326. dstport->shared->type_info.type_name) != 0) {
  1327. jack_error ("ports used in attemped connection are not of the same data type");
  1328. return -1;
  1329. }
  1330. connection = (jack_connection_internal_t *) malloc (sizeof (jack_connection_internal_t));
  1331. connection->source = srcport;
  1332. connection->destination = dstport;
  1333. src_id = srcport->shared->id;
  1334. dst_id = dstport->shared->id;
  1335. pthread_mutex_lock (&engine->graph_lock);
  1336. if (dstport->shared->type_info.mixdown == NULL && dstport->connections) {
  1337. jack_error ("cannot make multiple connections to a port of type [%s]", dstport->shared->type_info.type_name);
  1338. free (connection);
  1339. } else {
  1340. dstport->connections = g_slist_prepend (dstport->connections, connection);
  1341. srcport->connections = g_slist_prepend (srcport->connections, connection);
  1342. jack_sort_graph (engine, FALSE);
  1343. jack_send_connection_notification (engine, srcport->shared->client_id, src_id, dst_id, TRUE);
  1344. jack_send_connection_notification (engine, dstport->shared->client_id, dst_id, src_id, TRUE);
  1345. }
  1346. pthread_mutex_unlock (&engine->graph_lock);
  1347. return 0;
  1348. }
  1349. int
  1350. jack_port_disconnect_internal (jack_engine_t *engine,
  1351. jack_port_internal_t *srcport,
  1352. jack_port_internal_t *dstport,
  1353. int sort_graph)
  1354. {
  1355. GSList *node;
  1356. jack_connection_internal_t *connect;
  1357. int ret = -1;
  1358. jack_port_id_t src_id, dst_id;
  1359. /* call tree **** MUST HOLD **** engine->graph_lock. */
  1360. printf ("disconnecting %s and %s\n", srcport->shared->name, dstport->shared->name);
  1361. for (node = srcport->connections; node; node = g_slist_next (node)) {
  1362. connect = (jack_connection_internal_t *) node->data;
  1363. if (connect->source == srcport && connect->destination == dstport) {
  1364. srcport->connections = g_slist_remove (srcport->connections, connect);
  1365. dstport->connections = g_slist_remove (dstport->connections, connect);
  1366. src_id = srcport->shared->id;
  1367. dst_id = dstport->shared->id;
  1368. jack_send_connection_notification (engine, srcport->shared->client_id, src_id, dst_id, FALSE);
  1369. jack_send_connection_notification (engine, dstport->shared->client_id, dst_id, src_id, FALSE);
  1370. free (connect);
  1371. ret = 0;
  1372. break;
  1373. }
  1374. }
  1375. if (sort_graph) {
  1376. jack_sort_graph (engine, FALSE);
  1377. }
  1378. return ret;
  1379. }
  1380. static int
  1381. jack_port_do_disconnect (jack_engine_t *engine,
  1382. const char *source_port,
  1383. const char *destination_port)
  1384. {
  1385. jack_port_internal_t *srcport, *dstport;
  1386. int ret = -1;
  1387. if ((srcport = jack_get_port_by_name (engine, source_port)) == 0) {
  1388. jack_error ("unknown source port in attempted connection [%s]", source_port);
  1389. return -1;
  1390. }
  1391. if ((dstport = jack_get_port_by_name (engine, destination_port)) == 0) {
  1392. jack_error ("unknown destination port in attempted connection [%s]", destination_port);
  1393. return -1;
  1394. }
  1395. pthread_mutex_lock (&engine->graph_lock);
  1396. ret = jack_port_disconnect_internal (engine, srcport, dstport, TRUE);
  1397. pthread_mutex_unlock (&engine->graph_lock);
  1398. return ret;
  1399. }
  1400. static int
  1401. jack_create_fifo (jack_engine_t *engine, int which_fifo)
  1402. {
  1403. char path[FIFO_NAME_SIZE+1];
  1404. sprintf (path, "%s-%d", engine->fifo_prefix, which_fifo);
  1405. if (mknod (path, 0666|S_IFIFO, 0) < 0) {
  1406. if (errno != EEXIST) {
  1407. jack_error ("cannot create inter-client FIFO [%s] (%s)", path, strerror (errno));
  1408. return -1;
  1409. }
  1410. } else {
  1411. on_exit (unlink_path, strdup (path));
  1412. }
  1413. jack_get_fifo_fd (engine, which_fifo);
  1414. return 0;
  1415. }
  1416. static int
  1417. jack_get_fifo_fd (jack_engine_t *engine, int which_fifo)
  1418. {
  1419. char path[FIFO_NAME_SIZE+1];
  1420. sprintf (path, "%s-%d", engine->fifo_prefix, which_fifo);
  1421. if (which_fifo >= engine->fifo_size) {
  1422. int i;
  1423. engine->fifo = (int *) realloc (engine->fifo, sizeof (int) * engine->fifo_size + 16);
  1424. for (i = engine->fifo_size; i < engine->fifo_size + 16; i++) {
  1425. engine->fifo[i] = -1;
  1426. }
  1427. engine->fifo_size += 16;
  1428. }
  1429. if (engine->fifo[which_fifo] < 0) {
  1430. if ((engine->fifo[which_fifo] = open (path, O_RDWR|O_CREAT, 0666)) < 0) {
  1431. jack_error ("cannot open fifo [%s] (%s)", path, strerror (errno));
  1432. return -1;
  1433. }
  1434. }
  1435. return engine->fifo[which_fifo];
  1436. }
  1437. int
  1438. jack_use_driver (jack_engine_t *engine, jack_driver_t *driver)
  1439. {
  1440. if (driver) {
  1441. driver->detach (driver, engine);
  1442. engine->driver = 0;
  1443. }
  1444. if (driver->attach (driver, engine)) {
  1445. return -1;
  1446. }
  1447. engine->driver = driver;
  1448. return 0;
  1449. }
  1450. /* PORT RELATED FUNCTIONS */
  1451. jack_port_id_t
  1452. jack_get_free_port (jack_engine_t *engine)
  1453. {
  1454. jack_port_id_t i;
  1455. pthread_mutex_lock (&engine->port_lock);
  1456. for (i = 0; i < engine->port_max; i++) {
  1457. if (engine->control->ports[i].in_use == 0) {
  1458. engine->control->ports[i].in_use = 1;
  1459. break;
  1460. }
  1461. }
  1462. pthread_mutex_unlock (&engine->port_lock);
  1463. if (i == engine->port_max) {
  1464. return NoPort;
  1465. }
  1466. return i;
  1467. }
  1468. static void
  1469. jack_port_release (jack_engine_t *engine, jack_port_internal_t *port)
  1470. {
  1471. /* XXX add the buffer used by the port back the (correct) freelist */
  1472. pthread_mutex_lock (&engine->port_lock);
  1473. port->shared->in_use = 0;
  1474. pthread_mutex_unlock (&engine->port_lock);
  1475. }
  1476. jack_port_internal_t *
  1477. jack_get_port_internal_by_name (jack_engine_t *engine, const char *name)
  1478. {
  1479. jack_port_id_t id;
  1480. pthread_mutex_lock (&engine->port_lock);
  1481. for (id = 0; id < engine->port_max; id++) {
  1482. if (strcmp (engine->control->ports[id].name, name) == 0) {
  1483. break;
  1484. }
  1485. }
  1486. pthread_mutex_unlock (&engine->port_lock);
  1487. if (id != engine->port_max) {
  1488. return &engine->internal_ports[id];
  1489. } else {
  1490. return NULL;
  1491. }
  1492. }
  1493. int
  1494. jack_port_do_register (jack_engine_t *engine, jack_request_t *req)
  1495. {
  1496. GSList *node;
  1497. jack_port_id_t port_id;
  1498. jack_port_shared_t *shared;
  1499. jack_port_internal_t *port;
  1500. jack_client_internal_t *client;
  1501. jack_port_type_info_t *type_info;
  1502. pthread_mutex_lock (&engine->graph_lock);
  1503. if ((client = jack_client_internal_by_id (engine, req->x.port_info.client_id)) == 0) {
  1504. jack_error ("unknown client id in port registration request");
  1505. return -1;
  1506. }
  1507. pthread_mutex_unlock (&engine->graph_lock);
  1508. if ((port_id = jack_get_free_port (engine)) == NoPort) {
  1509. jack_error ("no ports available!");
  1510. return -1;
  1511. }
  1512. shared = &engine->control->ports[port_id];
  1513. strcpy (shared->name, req->x.port_info.name);
  1514. shared->client_id = req->x.port_info.client_id;
  1515. shared->flags = req->x.port_info.flags;
  1516. shared->locked = 0;
  1517. shared->buffer_size = req->x.port_info.buffer_size;
  1518. port = &engine->internal_ports[port_id];
  1519. port->shared = shared;
  1520. port->connections = 0;
  1521. type_info = NULL;
  1522. for (node = engine->port_types; node; node = g_slist_next (node)) {
  1523. if (strcmp (req->x.port_info.type, ((jack_port_type_info_t *) node->data)->type_name) == 0) {
  1524. type_info = (jack_port_type_info_t *) node->data;
  1525. break;
  1526. }
  1527. }
  1528. if (type_info == NULL) {
  1529. /* not a builtin type, so allocate a new type_info structure,
  1530. and fill it appropriately.
  1531. */
  1532. type_info = (jack_port_type_info_t *) malloc (sizeof (jack_port_type_info_t));
  1533. type_info->type_name = strdup (req->x.port_info.type);
  1534. type_info->mixdown = NULL; /* we have no idea how to mix this */
  1535. type_info->buffer_scale_factor = -1; /* use specified port buffer size */
  1536. engine->port_types = g_slist_prepend (engine->port_types, type_info);
  1537. }
  1538. memcpy (&port->shared->type_info, type_info, sizeof (jack_port_type_info_t));
  1539. if (jack_port_assign_buffer (engine, port)) {
  1540. jack_error ("cannot assign buffer for port");
  1541. return -1;
  1542. }
  1543. pthread_mutex_lock (&engine->graph_lock);
  1544. client->ports = g_slist_prepend (client->ports, port);
  1545. jack_port_registration_notify (engine, port_id, TRUE);
  1546. pthread_mutex_unlock (&engine->graph_lock);
  1547. req->x.port_info.port_id = port_id;
  1548. return 0;
  1549. }
  1550. int
  1551. jack_port_do_unregister (jack_engine_t *engine, jack_request_t *req)
  1552. {
  1553. jack_client_internal_t *client;
  1554. jack_port_shared_t *shared;
  1555. jack_port_internal_t *port;
  1556. if (req->x.port_info.port_id < 0 || req->x.port_info.port_id > engine->port_max) {
  1557. jack_error ("invalid port ID %d in unregister request\n", req->x.port_info.port_id);
  1558. return -1;
  1559. }
  1560. shared = &engine->control->ports[req->x.port_info.port_id];
  1561. pthread_mutex_lock (&engine->graph_lock);
  1562. if ((client = jack_client_internal_by_id (engine, shared->client_id)) == NULL) {
  1563. jack_error ("unknown client id in port registration request");
  1564. return -1;
  1565. }
  1566. pthread_mutex_unlock (&engine->graph_lock);
  1567. port = &engine->internal_ports[req->x.port_info.port_id];
  1568. jack_port_release (engine, &engine->internal_ports[req->x.port_info.port_id]);
  1569. pthread_mutex_lock (&engine->graph_lock);
  1570. client->ports = g_slist_remove (client->ports, port);
  1571. jack_port_registration_notify (engine, req->x.port_info.port_id, FALSE);
  1572. pthread_mutex_unlock (&engine->graph_lock);
  1573. return 0;
  1574. }
  1575. void
  1576. jack_port_registration_notify (jack_engine_t *engine, jack_port_id_t port_id, int yn)
  1577. {
  1578. jack_event_t event;
  1579. jack_client_internal_t *client;
  1580. GSList *node;
  1581. event.type = (yn ? PortRegistered : PortUnregistered);
  1582. event.x.port_id = port_id;
  1583. for (node = engine->clients; node; node = g_slist_next (node)) {
  1584. client = (jack_client_internal_t *) node->data;
  1585. if (!client->control->active) {
  1586. continue;
  1587. }
  1588. if (client->control->port_register) {
  1589. if (jack_deliver_event (engine, client, &event)) {
  1590. jack_error ("cannot send port registration notification to %s (%s)",
  1591. client->control->name, strerror (errno));
  1592. }
  1593. }
  1594. }
  1595. }
  1596. int
  1597. jack_port_assign_buffer (jack_engine_t *engine, jack_port_internal_t *port)
  1598. {
  1599. GSList *node;
  1600. jack_port_segment_info_t *psi;
  1601. jack_port_buffer_info_t *bi;
  1602. port->shared->buffer = NULL;
  1603. if (port->shared->flags & JackPortIsInput) {
  1604. return 0;
  1605. }
  1606. pthread_mutex_lock (&engine->buffer_lock);
  1607. if (engine->port_buffer_freelist == NULL) {
  1608. jack_error ("no more buffers available!");
  1609. goto out;
  1610. }
  1611. bi = (jack_port_buffer_info_t *) engine->port_buffer_freelist->data;
  1612. for (node = engine->port_segments; node; node = g_slist_next (node)) {
  1613. psi = (jack_port_segment_info_t *) node->data;
  1614. if (bi->shm_key == psi->shm_key) {
  1615. port->shared->buffer = psi->address + bi->offset;
  1616. break;
  1617. }
  1618. }
  1619. if (port->shared->buffer) {
  1620. engine->port_buffer_freelist = g_slist_remove (engine->port_buffer_freelist, bi);
  1621. } else {
  1622. jack_error ("port segment info for 0x%x:%d not found!", bi->shm_key, bi->offset);
  1623. }
  1624. out:
  1625. pthread_mutex_unlock (&engine->buffer_lock);
  1626. if (port->shared->buffer == NULL) {
  1627. return -1;
  1628. } else {
  1629. return 0;
  1630. }
  1631. }
  1632. static jack_port_internal_t *
  1633. jack_get_port_by_name (jack_engine_t *engine, const char *name)
  1634. {
  1635. jack_port_id_t id;
  1636. /* Note the potential race on "in_use". Other design
  1637. elements prevent this from being a problem.
  1638. */
  1639. for (id = 0; id < engine->port_max; id++) {
  1640. if (engine->control->ports[id].in_use && strcmp (engine->control->ports[id].name, name) == 0) {
  1641. return &engine->internal_ports[id];
  1642. }
  1643. }
  1644. return NULL;
  1645. }
  1646. static int
  1647. jack_send_connection_notification (jack_engine_t *engine, jack_client_id_t client_id,
  1648. jack_port_id_t self_id, jack_port_id_t other_id, int connected)
  1649. {
  1650. jack_client_internal_t *client;
  1651. jack_event_t event;
  1652. if ((client = jack_client_internal_by_id (engine, client_id)) == 0) {
  1653. jack_error ("no such client %d during connection notification", client_id);
  1654. return -1;
  1655. }
  1656. fprintf (stderr, "sending connection to client %s\n", client->control->name);
  1657. event.type = (connected ? PortConnected : PortDisconnected);
  1658. event.x.self_id = self_id;
  1659. event.y.other_id = other_id;
  1660. if (jack_deliver_event (engine, client, &event)) {
  1661. jack_error ("cannot send port connection notification to client %s (%s)",
  1662. client->control->name, strerror (errno));
  1663. return -1;
  1664. }
  1665. return 0;
  1666. }
  1667. static void
  1668. jack_audio_port_mixdown (jack_port_t *port, nframes_t nframes)
  1669. {
  1670. GSList *node;
  1671. jack_port_shared_t *input;
  1672. nframes_t n;
  1673. sample_t *dst, *src;
  1674. /* by the time we've called this, we've already established
  1675. the existence of more than 1 connection to this input port.
  1676. */
  1677. node = port->connections;
  1678. input = (jack_port_shared_t *) node->data;
  1679. memcpy (port->shared->buffer, input->buffer, sizeof (sample_t) * nframes);
  1680. for (node = g_slist_next (node); node; node = g_slist_next (node)) {
  1681. input = (jack_port_shared_t *) node->data;
  1682. n = nframes;
  1683. dst = port->shared->buffer;
  1684. src = input->buffer;
  1685. while (--n) {
  1686. *dst++ += *src++;
  1687. }
  1688. }
  1689. }