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.

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