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.

2322 lines
55KB

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