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.

2356 lines
57KB

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