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.

2307 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. GSList *portnode;
  550. jack_port_internal_t *port;
  551. if (client == engine->timebase_client) {
  552. engine->timebase_client = 0;
  553. engine->control->frame_time = 0;
  554. }
  555. for (portnode = client->ports; portnode; portnode = g_slist_next (portnode)) {
  556. port = (jack_port_internal_t *) portnode->data;
  557. jack_port_clear_connections (engine, port);
  558. }
  559. ret = jack_client_do_deactivate (engine, node->data);
  560. break;
  561. }
  562. }
  563. pthread_mutex_unlock (&engine->graph_lock);
  564. return ret;
  565. }
  566. static int
  567. jack_set_timebase (jack_engine_t *engine, jack_client_id_t client)
  568. {
  569. int ret = -1;
  570. pthread_mutex_lock (&engine->graph_lock);
  571. if ((engine->timebase_client = jack_client_internal_by_id (engine, client)) != 0) {
  572. engine->control->frame_time = engine->timebase_client->control->frame_time;
  573. ret = 0;
  574. }
  575. pthread_mutex_unlock (&engine->graph_lock);
  576. return ret;
  577. }
  578. static int
  579. handle_client_jack_error (jack_engine_t *engine, int fd)
  580. {
  581. jack_client_internal_t *client = 0;
  582. GSList *node;
  583. pthread_mutex_lock (&engine->graph_lock);
  584. for (node = engine->clients; node; node = g_slist_next (node)) {
  585. if (((jack_client_internal_t *) node->data)->request_fd == fd) {
  586. client = (jack_client_internal_t *) node->data;
  587. break;
  588. }
  589. }
  590. pthread_mutex_unlock (&engine->graph_lock);
  591. if (client == 0) {
  592. jack_error ("i/o error on unknown client fd %d", fd);
  593. return -1;
  594. }
  595. jack_remove_client (engine, client);
  596. return 0;
  597. }
  598. static int
  599. jack_client_port_monitor (jack_engine_t *engine, jack_port_id_t port_id, int onoff)
  600. {
  601. jack_port_shared_t *port;
  602. jack_client_internal_t *client = NULL;
  603. jack_event_t event;
  604. if (port_id < 0 || port_id >= engine->port_max) {
  605. jack_error ("illegal port ID in port monitor request");
  606. return -1;
  607. }
  608. port = &engine->control->ports[port_id];
  609. if (!(port->flags & JackPortCanMonitor)) {
  610. jack_error ("port monitor request made on a port (%s) that doesn't support monitoring",
  611. port->name);
  612. return -1;
  613. }
  614. pthread_mutex_lock (&engine->graph_lock);
  615. if ((client = jack_client_internal_by_id (engine, port->client_id)) == NULL) {
  616. jack_error ("unknown client owns port %d!!", port_id);
  617. pthread_mutex_unlock (&engine->graph_lock);
  618. return -1;
  619. }
  620. pthread_mutex_unlock (&engine->graph_lock);
  621. event.type = (onoff ? PortMonitor : PortUnMonitor);
  622. event.x.port_id = port_id;
  623. return jack_deliver_event (engine, client, &event);
  624. }
  625. static int
  626. handle_client_io (jack_engine_t *engine, int fd)
  627. {
  628. jack_request_t req;
  629. jack_client_internal_t *client = 0;
  630. int reply_fd;
  631. GSList *node;
  632. pthread_mutex_lock (&engine->graph_lock);
  633. for (node = engine->clients; node; node = g_slist_next (node)) {
  634. if (((jack_client_internal_t *) node->data)->request_fd == fd) {
  635. client = (jack_client_internal_t *) node->data;
  636. break;
  637. }
  638. }
  639. pthread_mutex_unlock (&engine->graph_lock);
  640. if (client == 0) {
  641. jack_error ("client input on unknown fd %d!", fd);
  642. return -1;
  643. }
  644. if (read (client->request_fd, &req, sizeof (req)) < sizeof (req)) {
  645. jack_error ("cannot read request from client");
  646. jack_remove_client (engine, client);
  647. return -1;
  648. }
  649. reply_fd = client->request_fd;
  650. switch (req.type) {
  651. case RegisterPort:
  652. req.status = jack_port_do_register (engine, &req);
  653. break;
  654. case UnRegisterPort:
  655. req.status = jack_port_do_unregister (engine, &req);
  656. break;
  657. case ConnectPorts:
  658. req.status = jack_port_do_connect (engine, req.x.connect.source_port, req.x.connect.destination_port);
  659. break;
  660. case DisconnectPorts:
  661. req.status = jack_port_do_disconnect (engine, req.x.connect.source_port, req.x.connect.destination_port);
  662. break;
  663. case DropClient:
  664. req.status = jack_client_drop (engine, req.x.client_id);
  665. reply_fd = -1;
  666. break;
  667. case ActivateClient:
  668. req.status = jack_client_activate (engine, req.x.client_id);
  669. break;
  670. case DeactivateClient:
  671. req.status = jack_client_deactivate (engine, req.x.client_id, TRUE);
  672. break;
  673. case SetTimeBaseClient:
  674. req.status = jack_set_timebase (engine, req.x.client_id);
  675. break;
  676. case RequestPortMonitor:
  677. req.status = jack_client_port_monitor (engine, req.x.port_info.port_id, TRUE);
  678. break;
  679. case RequestPortUnMonitor:
  680. req.status = jack_client_port_monitor (engine, req.x.port_info.port_id, FALSE);
  681. break;
  682. }
  683. if (reply_fd >= 0) {
  684. if (write (reply_fd, &req, sizeof (req)) < sizeof (req)) {
  685. jack_error ("cannot write request result to client");
  686. return -1;
  687. }
  688. }
  689. return 0;
  690. }
  691. static void *
  692. jack_server_thread (void *arg)
  693. {
  694. jack_engine_t *engine = (jack_engine_t *) arg;
  695. struct sockaddr_un client_addr;
  696. socklen_t client_addrlen;
  697. struct pollfd *pfd;
  698. int client_socket;
  699. int done = 0;
  700. int i;
  701. int max;
  702. pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  703. engine->pfd[0].fd = engine->fds[0];
  704. engine->pfd[0].events = POLLIN|POLLERR;
  705. engine->pfd[1].fd = engine->fds[1];
  706. engine->pfd[1].events = POLLIN|POLLERR;
  707. engine->pfd_max = 2;
  708. while (!done) {
  709. /* XXX race here with new external clients
  710. causing engine->pfd to be reallocated.
  711. I don't know how to solve this
  712. short of copying the entire
  713. contents of the pfd struct. Ick.
  714. */
  715. max = engine->pfd_max;
  716. pfd = engine->pfd;
  717. if (poll (pfd, max, 10000) < 0) {
  718. if (errno == EINTR) {
  719. continue;
  720. }
  721. jack_error ("poll failed (%s)", strerror (errno));
  722. break;
  723. }
  724. /* check the master server socket */
  725. if (pfd[0].revents & POLLERR) {
  726. jack_error ("error on server socket");
  727. break;
  728. }
  729. if (pfd[0].revents & POLLIN) {
  730. memset (&client_addr, 0, sizeof (client_addr));
  731. client_addrlen = sizeof (client_addr);
  732. if ((client_socket = accept (engine->fds[0], (struct sockaddr *) &client_addr, &client_addrlen)) < 0) {
  733. jack_error ("cannot accept new connection (%s)", strerror (errno));
  734. } else if (handle_new_client (engine, client_socket) < 0) {
  735. jack_error ("cannot complete new client connection process");
  736. close (client_socket);
  737. }
  738. }
  739. /* check the ACK server socket */
  740. if (pfd[1].revents & POLLERR) {
  741. jack_error ("error on server ACK socket");
  742. break;
  743. }
  744. if (pfd[1].revents & POLLIN) {
  745. memset (&client_addr, 0, sizeof (client_addr));
  746. client_addrlen = sizeof (client_addr);
  747. if ((client_socket = accept (engine->fds[1], (struct sockaddr *) &client_addr, &client_addrlen)) < 0) {
  748. jack_error ("cannot accept new ACK connection (%s)", strerror (errno));
  749. } else if (handle_client_ack_connection (engine, client_socket)) {
  750. jack_error ("cannot complete client ACK connection process");
  751. close (client_socket);
  752. }
  753. }
  754. /* check each client socket */
  755. for (i = 2; i < max; i++) {
  756. if (pfd[i].fd < 0) {
  757. continue;
  758. }
  759. if (pfd[i].revents & ~POLLIN) {
  760. handle_client_jack_error (engine, pfd[i].fd);
  761. } else if (pfd[i].revents & POLLIN) {
  762. if (handle_client_io (engine, pfd[i].fd)) {
  763. jack_error ("bad hci\n");
  764. }
  765. }
  766. }
  767. }
  768. return 0;
  769. }
  770. static void
  771. jack_start_server (jack_engine_t *engine)
  772. {
  773. pthread_create (&engine->server_thread, 0, &jack_server_thread, engine);
  774. pthread_detach (engine->server_thread);
  775. }
  776. jack_engine_t *
  777. jack_engine_new (int realtime, int rtpriority)
  778. {
  779. jack_engine_t *engine;
  780. size_t control_size;
  781. void *addr;
  782. int i;
  783. engine = (jack_engine_t *) malloc (sizeof (jack_engine_t));
  784. engine->driver = 0;
  785. engine->process = jack_process;
  786. engine->set_sample_rate = jack_set_sample_rate;
  787. engine->set_buffer_size = jack_set_buffer_size;
  788. engine->next_client_id = 1;
  789. engine->timebase_client = 0;
  790. engine->port_max = 128;
  791. engine->rtpriority = rtpriority;
  792. engine->silent_buffer = 0;
  793. engine->getthehelloutathere = FALSE;
  794. pthread_mutex_init (&engine->graph_lock, 0);
  795. pthread_mutex_init (&engine->buffer_lock, 0);
  796. pthread_mutex_init (&engine->port_lock, 0);
  797. engine->clients = 0;
  798. engine->port_segments = 0;
  799. engine->port_buffer_freelist = 0;
  800. engine->pfd_size = 16;
  801. engine->pfd_max = 0;
  802. engine->pfd = (struct pollfd *) malloc (sizeof (struct pollfd) * engine->pfd_size);
  803. engine->fifo_size = 16;
  804. engine->fifo = (int *) malloc (sizeof (int) * engine->fifo_size);
  805. for (i = 0; i < engine->fifo_size; i++) {
  806. engine->fifo[i] = -1;
  807. }
  808. /* Build a linked list of known port types. We use a list so that
  809. we can easily manage other data types without messing with
  810. reallocation of arrays, etc.
  811. */
  812. engine->port_types = NULL;
  813. for (i = 0; builtin_port_types[i].type_name; i++) {
  814. engine->port_types = g_slist_append (engine->port_types, &builtin_port_types[i]);
  815. }
  816. engine->external_client_cnt = 0;
  817. srandom (time ((time_t *) 0));
  818. engine->control_key = random();
  819. control_size = sizeof (jack_control_t) + (sizeof (jack_port_shared_t) * engine->port_max);
  820. if ((engine->control_shm_id = shmget (engine->control_key, control_size, IPC_CREAT|0644)) < 0) {
  821. jack_error ("cannot create engine control shared memory segment (%s)", strerror (errno));
  822. return 0;
  823. }
  824. if ((addr = shmat (engine->control_shm_id, 0, 0)) == (void *) -1) {
  825. jack_error ("cannot attach control shared memory segment (%s)", strerror (errno));
  826. shmctl (engine->control_shm_id, IPC_RMID, 0);
  827. return 0;
  828. }
  829. on_exit (shm_destroy, (void *) engine->control_shm_id);
  830. engine->control = (jack_control_t *) addr;
  831. /* Mark all ports as available */
  832. for (i = 0; i < engine->port_max; i++) {
  833. engine->control->ports[i].in_use = 0;
  834. engine->control->ports[i].id = i;
  835. }
  836. /* allocate internal port structures so that we can keep
  837. track of port connections.
  838. */
  839. engine->internal_ports = (jack_port_internal_t *) malloc (sizeof (jack_port_internal_t) * engine->port_max);
  840. for (i = 0; i < engine->port_max; i++) {
  841. engine->internal_ports[i].connections = 0;
  842. }
  843. if (make_sockets (engine->fds) < 0) {
  844. jack_error ("cannot create server sockets");
  845. return 0;
  846. }
  847. engine->control->port_max = engine->port_max;
  848. engine->control->real_time = realtime;
  849. engine->control->client_priority = engine->rtpriority - 1;
  850. engine->control->sample_rate = 0;
  851. engine->control->buffer_size = 0;
  852. engine->control->frame_time = 0;
  853. sprintf (engine->fifo_prefix, "/tmp/jack_fifo_%d", getpid());
  854. jack_create_fifo (engine, 0);
  855. jack_start_server (engine);
  856. return engine;
  857. }
  858. static int
  859. jack_become_real_time (pthread_t thread, int priority)
  860. {
  861. struct sched_param rtparam;
  862. int x;
  863. memset (&rtparam, 0, sizeof (rtparam));
  864. rtparam.sched_priority = priority;
  865. if ((x = pthread_setschedparam (thread, SCHED_FIFO, &rtparam)) != 0) {
  866. jack_error ("cannot set thread to real-time priority (FIFO/%d) (%d: %s)", rtparam.sched_priority, x, strerror (errno));
  867. }
  868. if (mlockall (MCL_CURRENT | MCL_FUTURE) != 0) {
  869. jack_error ("cannot lock down memory for RT thread (%s)", strerror (errno));
  870. }
  871. return 0;
  872. }
  873. void
  874. cancel_cleanup1 (void *arg)
  875. {
  876. jack_engine_t *engine = (jack_engine_t *) arg;
  877. printf ("audio thread cancelled or finished\n");
  878. engine->driver->audio_stop (engine->driver);
  879. }
  880. void
  881. cancel_cleanup2 (int status, void *arg)
  882. {
  883. jack_engine_t *engine = (jack_engine_t *) arg;
  884. engine->driver->audio_stop (engine->driver);
  885. engine->driver->finish (engine->driver);
  886. }
  887. static void *
  888. jack_audio_thread (void *arg)
  889. {
  890. jack_engine_t *engine = (jack_engine_t *) arg;
  891. jack_driver_t *driver = engine->driver;
  892. // unsigned long start, end;
  893. if (engine->control->real_time) {
  894. jack_become_real_time (pthread_self(), engine->rtpriority);
  895. }
  896. pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  897. on_exit (cancel_cleanup2, engine);
  898. if (driver->audio_start (driver)) {
  899. jack_error ("cannot start driver");
  900. pthread_exit (0);
  901. }
  902. while (1) {
  903. // start = end;
  904. if (driver->wait (driver)) {
  905. break;
  906. }
  907. // rdtscl (end);
  908. // printf ("driver cycle time: %.6f usecs\n", ((float) (end - start)) / 450.00f);
  909. }
  910. pthread_exit (0);
  911. }
  912. int
  913. jack_run (jack_engine_t *engine)
  914. {
  915. if (engine->driver == 0) {
  916. jack_error ("engine driver not set; cannot start");
  917. return -1;
  918. }
  919. return pthread_create (&engine->audio_thread, 0, jack_audio_thread, engine);
  920. }
  921. int
  922. jack_wait (jack_engine_t *engine)
  923. {
  924. void *ret = 0;
  925. int err;
  926. if ((err = pthread_join (engine->audio_thread, &ret)) != 0) {
  927. switch (err) {
  928. case EINVAL:
  929. jack_error ("cannot join with audio thread (thread detached, or another thread is waiting)");
  930. break;
  931. case ESRCH:
  932. jack_error ("cannot join with audio thread (thread no longer exists)");
  933. break;
  934. case EDEADLK:
  935. jack_error ("programming error: jack_wait() called by audio thread");
  936. break;
  937. default:
  938. jack_error ("cannot join with audio thread (%s)", strerror (errno));
  939. }
  940. }
  941. return (int) ret;
  942. }
  943. int
  944. jack_engine_delete (jack_engine_t *engine)
  945. {
  946. pthread_cancel (engine->audio_thread);
  947. return 0;
  948. }
  949. static jack_client_internal_t *
  950. jack_client_internal_new (jack_engine_t *engine, int fd, jack_client_connect_request_t *req)
  951. {
  952. jack_client_internal_t *client;
  953. key_t shm_key = 0;
  954. int shm_id = 0;
  955. void *addr = 0;
  956. switch (req->type) {
  957. case ClientDynamic:
  958. case ClientDriver:
  959. break;
  960. case ClientOutOfProcess:
  961. shm_key = random();
  962. if ((shm_id = shmget (shm_key, sizeof (jack_client_control_t), IPC_CREAT|0666)) < 0) {
  963. jack_error ("cannot create client control block");
  964. return 0;
  965. }
  966. if ((addr = shmat (shm_id, 0, 0)) == (void *) -1) {
  967. jack_error ("cannot attach new client control block");
  968. shmctl (shm_id, IPC_RMID, 0);
  969. return 0;
  970. }
  971. break;
  972. }
  973. client = (jack_client_internal_t *) malloc (sizeof (jack_client_internal_t));
  974. client->request_fd = fd;
  975. client->event_fd = -1;
  976. client->ports = 0;
  977. client->fed_by = 0;
  978. client->rank = UINT_MAX;
  979. client->next_client = NULL;
  980. client->handle = NULL;
  981. if (req->type != ClientOutOfProcess) {
  982. client->control = (jack_client_control_t *) malloc (sizeof (jack_client_control_t));
  983. } else {
  984. client->shm_id = shm_id;
  985. client->shm_key = shm_key;
  986. client->control = (jack_client_control_t *) addr;
  987. }
  988. client->control->type = req->type;
  989. client->control->active = FALSE;
  990. client->control->dead = FALSE;
  991. client->control->id = engine->next_client_id++;
  992. strcpy ((char *) client->control->name, req->name);
  993. client->control->process = NULL;
  994. client->control->process_arg = NULL;
  995. client->control->bufsize = NULL;
  996. client->control->bufsize_arg = NULL;
  997. client->control->srate = NULL;
  998. client->control->srate_arg = NULL;
  999. client->control->port_register = NULL;
  1000. client->control->port_register_arg = NULL;
  1001. client->control->port_monitor = NULL;
  1002. client->control->port_monitor_arg = NULL;
  1003. if (req->type == ClientDynamic) {
  1004. if (jack_load_client (engine, client, req->object_path)) {
  1005. jack_error ("cannot dynamically load client from \"%s\"", req->object_path);
  1006. jack_client_delete (engine, client);
  1007. return 0;
  1008. }
  1009. }
  1010. return client;
  1011. }
  1012. static void
  1013. jack_port_clear_connections (jack_engine_t *engine, jack_port_internal_t *port)
  1014. {
  1015. GSList *node, *next;
  1016. for (node = port->connections; node; ) {
  1017. next = g_slist_next (node);
  1018. jack_port_disconnect_internal (engine,
  1019. ((jack_connection_internal_t *) node->data)->source,
  1020. ((jack_connection_internal_t *) node->data)->destination,
  1021. FALSE);
  1022. node = next;
  1023. }
  1024. g_slist_free (port->connections);
  1025. port->connections = 0;
  1026. }
  1027. static void
  1028. jack_remove_client (jack_engine_t *engine, jack_client_internal_t *client)
  1029. {
  1030. GSList *node;
  1031. int i;
  1032. printf ("removing client %s\n", client->control->name);
  1033. pthread_mutex_lock (&engine->graph_lock);
  1034. client->control->dead = TRUE;
  1035. if (client == engine->timebase_client) {
  1036. engine->timebase_client = 0;
  1037. engine->control->frame_time = 0;
  1038. }
  1039. jack_client_disconnect (engine, client);
  1040. for (node = engine->clients; node; node = g_slist_next (node)) {
  1041. if (((jack_client_internal_t *) node->data)->control->id == client->control->id) {
  1042. engine->clients = g_slist_remove_link (engine->clients, node);
  1043. g_slist_free_1 (node);
  1044. break;
  1045. }
  1046. }
  1047. jack_client_do_deactivate (engine, client);
  1048. /* rearrange the pollfd array so that things work right the
  1049. next time we go into poll(2).
  1050. */
  1051. for (i = 0; i < engine->pfd_max; i++) {
  1052. if (engine->pfd[i].fd == client->request_fd) {
  1053. if (i+1 < engine->pfd_max) {
  1054. memmove (&engine->pfd[i], &engine->pfd[i+1], sizeof (struct pollfd) * (engine->pfd_max - i));
  1055. }
  1056. engine->pfd_max--;
  1057. }
  1058. }
  1059. close (client->event_fd);
  1060. close (client->request_fd);
  1061. jack_client_delete (engine, client);
  1062. pthread_mutex_unlock (&engine->graph_lock);
  1063. }
  1064. static void
  1065. jack_client_delete (jack_engine_t *engine, jack_client_internal_t *client)
  1066. {
  1067. jack_client_disconnect (engine, client);
  1068. if (jack_client_is_inprocess (client)) {
  1069. jack_client_unload (client);
  1070. free ((char *) client->control);
  1071. } else {
  1072. shmdt ((void *) client->control);
  1073. }
  1074. free (client);
  1075. }
  1076. jack_client_internal_t *
  1077. jack_client_by_name (jack_engine_t *engine, const char *name)
  1078. {
  1079. jack_client_internal_t *client = NULL;
  1080. GSList *node;
  1081. pthread_mutex_lock (&engine->graph_lock);
  1082. for (node = engine->clients; node; node = g_slist_next (node)) {
  1083. if (strcmp ((const char *) ((jack_client_internal_t *) node->data)->control->name, name) == 0) {
  1084. client = (jack_client_internal_t *) node->data;
  1085. break;
  1086. }
  1087. }
  1088. pthread_mutex_unlock (&engine->graph_lock);
  1089. return client;
  1090. }
  1091. jack_client_internal_t *
  1092. jack_client_internal_by_id (jack_engine_t *engine, jack_client_id_t id)
  1093. {
  1094. jack_client_internal_t *client = NULL;
  1095. GSList *node;
  1096. /* call tree ***MUST HOLD*** engine->graph_lock */
  1097. for (node = engine->clients; node; node = g_slist_next (node)) {
  1098. if (((jack_client_internal_t *) node->data)->control->id == id) {
  1099. client = (jack_client_internal_t *) node->data;
  1100. break;
  1101. }
  1102. }
  1103. return client;
  1104. }
  1105. static int
  1106. jack_deliver_event (jack_engine_t *engine, jack_client_internal_t *client, jack_event_t *event)
  1107. {
  1108. char status;
  1109. if (client->control->dead) {
  1110. return 0;
  1111. }
  1112. if (jack_client_is_inprocess (client)) {
  1113. switch (event->type) {
  1114. case PortConnected:
  1115. case PortDisconnected:
  1116. jack_client_handle_port_connection (client->control->private_internal_client, event);
  1117. break;
  1118. case GraphReordered:
  1119. jack_error ("reorder event delivered to internal client!");
  1120. break;
  1121. case BufferSizeChange:
  1122. if (client->control->bufsize) {
  1123. client->control->bufsize (event->x.n, client->control->bufsize_arg);
  1124. }
  1125. break;
  1126. case SampleRateChange:
  1127. if (client->control->srate) {
  1128. client->control->srate (event->x.n, client->control->bufsize_arg);
  1129. }
  1130. break;
  1131. case PortMonitor:
  1132. if (client->control->port_monitor) {
  1133. client->control->port_monitor (event->x.port_id, TRUE, client->control->port_monitor_arg);
  1134. }
  1135. break;
  1136. case PortUnMonitor:
  1137. if (client->control->port_monitor) {
  1138. client->control->port_monitor (event->x.port_id, FALSE, client->control->port_monitor_arg);
  1139. }
  1140. break;
  1141. default:
  1142. /* internal clients don't need to know */
  1143. break;
  1144. }
  1145. } else {
  1146. if (write (client->event_fd, event, sizeof (*event)) != sizeof (*event)) {
  1147. jack_error ("cannot send event to client [%s] (%s)", client->control->name, strerror (errno));
  1148. return -1;
  1149. }
  1150. if (read (client->event_fd, &status, sizeof (status)) != sizeof (status)) {
  1151. jack_error ("cannot read event response from client [%s] (%s)", client->control->name, strerror (errno));
  1152. return -1;
  1153. }
  1154. }
  1155. return 0;
  1156. }
  1157. int
  1158. jack_client_set_order (jack_engine_t *engine, jack_client_internal_t *client)
  1159. {
  1160. jack_event_t event;
  1161. event.type = GraphReordered;
  1162. event.x.n = client->rank;
  1163. return jack_deliver_event (engine, client, &event);
  1164. }
  1165. int
  1166. jack_rechain_graph (jack_engine_t *engine, int take_lock)
  1167. {
  1168. GSList *node, *next;
  1169. unsigned long n;
  1170. int err = 0;
  1171. int set;
  1172. jack_client_internal_t *client, *subgraph_client, *next_client;
  1173. if (take_lock) {
  1174. pthread_mutex_lock (&engine->graph_lock);
  1175. }
  1176. /* We're going to try to avoid reconnecting clients that
  1177. don't need to be reconnected. This is slightly tricky,
  1178. but worth it for performance reasons.
  1179. */
  1180. subgraph_client = 0;
  1181. if ((node = engine->clients) == 0) {
  1182. goto done;
  1183. }
  1184. client = (jack_client_internal_t *) node->data;
  1185. if ((next = g_slist_next (node)) == NULL) {
  1186. next_client = 0;
  1187. } else {
  1188. next_client = (jack_client_internal_t *) next->data;
  1189. }
  1190. n = 0;
  1191. do {
  1192. if (client->rank != n || client->next_client != next_client) {
  1193. client->rank = n;
  1194. client->next_client = next_client;
  1195. set = TRUE;
  1196. } else {
  1197. set = FALSE;
  1198. }
  1199. if (jack_client_is_inprocess (client)) {
  1200. /* break the chain for the current subgraph. the server
  1201. will wait for chain on the nth FIFO, and will
  1202. then execute this in-process client.
  1203. */
  1204. if (subgraph_client) {
  1205. subgraph_client->subgraph_wait_fd = jack_get_fifo_fd (engine, n);
  1206. }
  1207. subgraph_client = 0;
  1208. } else {
  1209. if (subgraph_client == 0) {
  1210. /* start a new subgraph. the engine will start the chain
  1211. by writing to the nth FIFO.
  1212. */
  1213. subgraph_client = client;
  1214. subgraph_client->subgraph_start_fd = jack_get_fifo_fd (engine, n);
  1215. }
  1216. if (set) {
  1217. jack_client_set_order (engine, client);
  1218. }
  1219. n++;
  1220. }
  1221. if (next == 0) {
  1222. break;
  1223. }
  1224. node = next;
  1225. client = (jack_client_internal_t *) node->data;
  1226. if ((next = g_slist_next (node)) == 0) {
  1227. next_client = 0;
  1228. } else {
  1229. next_client = (jack_client_internal_t *) next->data;
  1230. }
  1231. } while (1);
  1232. if (subgraph_client) {
  1233. subgraph_client->subgraph_wait_fd = jack_get_fifo_fd (engine, n);
  1234. }
  1235. done:
  1236. if (take_lock) {
  1237. pthread_mutex_unlock (&engine->graph_lock);
  1238. }
  1239. return err;
  1240. }
  1241. static void
  1242. jack_trace_terminal (jack_client_internal_t *c1, jack_client_internal_t *rbase)
  1243. {
  1244. jack_client_internal_t *c2;
  1245. /* make a copy of the existing list of routes that feed c1 */
  1246. GSList *existing;
  1247. GSList *node;
  1248. if (c1->fed_by == 0) {
  1249. return;
  1250. }
  1251. existing = g_slist_copy (c1->fed_by);
  1252. /* for each route that feeds c1, recurse, marking it as feeding
  1253. rbase as well.
  1254. */
  1255. for (node = existing; node; node = g_slist_next (node)) {
  1256. c2 = (jack_client_internal_t *) node->data;
  1257. /* c2 is a route that feeds c1 which somehow feeds base. mark
  1258. base as being fed by c2
  1259. */
  1260. rbase->fed_by = g_slist_prepend (rbase->fed_by, c2);
  1261. if (c2 != rbase && c2 != c1) {
  1262. /* now recurse, so that we can mark base as being fed by
  1263. all routes that feed c2
  1264. */
  1265. jack_trace_terminal (c2, rbase);
  1266. }
  1267. }
  1268. }
  1269. static int
  1270. jack_client_sort (jack_client_internal_t *a, jack_client_internal_t *b)
  1271. {
  1272. /* the driver client always comes after everything else */
  1273. if (a->control->type == ClientDriver) {
  1274. return 1;
  1275. }
  1276. if (b->control->type == ClientDriver) {
  1277. return -1;
  1278. }
  1279. if (g_slist_find (a->fed_by, b)) {
  1280. /* a comes after b */
  1281. return 1;
  1282. } else if (g_slist_find (b->fed_by, a)) {
  1283. /* b comes after a */
  1284. return -1;
  1285. } else {
  1286. /* we don't care */
  1287. return 0;
  1288. }
  1289. }
  1290. static int
  1291. jack_client_feeds (jack_client_internal_t *might, jack_client_internal_t *target)
  1292. {
  1293. GSList *pnode, *cnode;
  1294. /* Check every port of `might' for an outbound connection to `target'
  1295. */
  1296. for (pnode = might->ports; pnode; pnode = g_slist_next (pnode)) {
  1297. jack_port_internal_t *port;
  1298. port = (jack_port_internal_t *) pnode->data;
  1299. for (cnode = port->connections; cnode; cnode = g_slist_next (cnode)) {
  1300. jack_connection_internal_t *c;
  1301. c = (jack_connection_internal_t *) cnode->data;
  1302. if (c->source->shared->client_id == might->control->id &&
  1303. c->destination->shared->client_id == target->control->id) {
  1304. return 1;
  1305. }
  1306. }
  1307. }
  1308. return 0;
  1309. }
  1310. static void
  1311. jack_sort_graph (jack_engine_t *engine, int take_lock)
  1312. {
  1313. GSList *node, *onode;
  1314. jack_client_internal_t *client;
  1315. jack_client_internal_t *oclient;
  1316. if (take_lock) {
  1317. pthread_mutex_lock (&engine->graph_lock);
  1318. }
  1319. for (node = engine->clients; node; node = g_slist_next (node)) {
  1320. client = (jack_client_internal_t *) node->data;
  1321. g_slist_free (client->fed_by);
  1322. client->fed_by = 0;
  1323. for (onode = engine->clients; onode; onode = g_slist_next (onode)) {
  1324. oclient = (jack_client_internal_t *) onode->data;
  1325. if (jack_client_feeds (oclient, client)) {
  1326. client->fed_by = g_slist_prepend (client->fed_by, oclient);
  1327. }
  1328. }
  1329. }
  1330. for (node = engine->clients; node; node = g_slist_next (node)) {
  1331. jack_trace_terminal ((jack_client_internal_t *) node->data,
  1332. (jack_client_internal_t *) node->data);
  1333. }
  1334. engine->clients = g_slist_sort (engine->clients, (GCompareFunc) jack_client_sort);
  1335. jack_rechain_graph (engine, FALSE);
  1336. if (take_lock) {
  1337. pthread_mutex_unlock (&engine->graph_lock);
  1338. }
  1339. }
  1340. static int
  1341. jack_port_do_connect (jack_engine_t *engine,
  1342. const char *source_port,
  1343. const char *destination_port)
  1344. {
  1345. jack_connection_internal_t *connection;
  1346. jack_port_internal_t *srcport, *dstport;
  1347. jack_port_id_t src_id, dst_id;
  1348. fprintf (stderr, "trying to connect %s and %s\n", source_port, destination_port);
  1349. if ((srcport = jack_get_port_by_name (engine, source_port)) == 0) {
  1350. jack_error ("unknown source port in attempted connection [%s]", source_port);
  1351. return -1;
  1352. }
  1353. if ((dstport = jack_get_port_by_name (engine, destination_port)) == 0) {
  1354. jack_error ("unknown destination port in attempted connection [%s]", destination_port);
  1355. return -1;
  1356. }
  1357. if ((dstport->shared->flags & JackPortIsInput) == 0) {
  1358. jack_error ("destination port in attempted connection is not an input port");
  1359. return -1;
  1360. }
  1361. if ((srcport->shared->flags & JackPortIsOutput) == 0) {
  1362. jack_error ("source port in attempted connection is not an output port");
  1363. return -1;
  1364. }
  1365. if (strcmp (srcport->shared->type_info.type_name,
  1366. dstport->shared->type_info.type_name) != 0) {
  1367. jack_error ("ports used in attemped connection are not of the same data type");
  1368. return -1;
  1369. }
  1370. connection = (jack_connection_internal_t *) malloc (sizeof (jack_connection_internal_t));
  1371. connection->source = srcport;
  1372. connection->destination = dstport;
  1373. src_id = srcport->shared->id;
  1374. dst_id = dstport->shared->id;
  1375. pthread_mutex_lock (&engine->graph_lock);
  1376. if (dstport->connections && dstport->shared->type_info.mixdown == NULL) {
  1377. jack_error ("cannot make multiple connections to a port of type [%s]", dstport->shared->type_info.type_name);
  1378. free (connection);
  1379. return -1;
  1380. } else {
  1381. dstport->connections = g_slist_prepend (dstport->connections, connection);
  1382. srcport->connections = g_slist_prepend (srcport->connections, connection);
  1383. jack_sort_graph (engine, FALSE);
  1384. jack_send_connection_notification (engine, srcport->shared->client_id, src_id, dst_id, TRUE);
  1385. jack_send_connection_notification (engine, dstport->shared->client_id, dst_id, src_id, TRUE);
  1386. }
  1387. pthread_mutex_unlock (&engine->graph_lock);
  1388. return 0;
  1389. }
  1390. int
  1391. jack_port_disconnect_internal (jack_engine_t *engine,
  1392. jack_port_internal_t *srcport,
  1393. jack_port_internal_t *dstport,
  1394. int sort_graph)
  1395. {
  1396. GSList *node;
  1397. jack_connection_internal_t *connect;
  1398. int ret = -1;
  1399. jack_port_id_t src_id, dst_id;
  1400. /* call tree **** MUST HOLD **** engine->graph_lock. */
  1401. printf ("disconnecting %s and %s\n", srcport->shared->name, dstport->shared->name);
  1402. for (node = srcport->connections; node; node = g_slist_next (node)) {
  1403. connect = (jack_connection_internal_t *) node->data;
  1404. if (connect->source == srcport && connect->destination == dstport) {
  1405. srcport->connections = g_slist_remove (srcport->connections, connect);
  1406. dstport->connections = g_slist_remove (dstport->connections, connect);
  1407. src_id = srcport->shared->id;
  1408. dst_id = dstport->shared->id;
  1409. jack_send_connection_notification (engine, srcport->shared->client_id, src_id, dst_id, FALSE);
  1410. jack_send_connection_notification (engine, dstport->shared->client_id, dst_id, src_id, FALSE);
  1411. free (connect);
  1412. ret = 0;
  1413. break;
  1414. }
  1415. }
  1416. if (sort_graph) {
  1417. jack_sort_graph (engine, FALSE);
  1418. }
  1419. if (ret == -1) {
  1420. printf ("disconnect failed\n");
  1421. }
  1422. return ret;
  1423. }
  1424. static int
  1425. jack_port_do_disconnect (jack_engine_t *engine,
  1426. const char *source_port,
  1427. const char *destination_port)
  1428. {
  1429. jack_port_internal_t *srcport, *dstport;
  1430. int ret = -1;
  1431. if ((srcport = jack_get_port_by_name (engine, source_port)) == 0) {
  1432. jack_error ("unknown source port in attempted connection [%s]", source_port);
  1433. return -1;
  1434. }
  1435. if ((dstport = jack_get_port_by_name (engine, destination_port)) == 0) {
  1436. jack_error ("unknown destination port in attempted connection [%s]", destination_port);
  1437. return -1;
  1438. }
  1439. pthread_mutex_lock (&engine->graph_lock);
  1440. ret = jack_port_disconnect_internal (engine, srcport, dstport, TRUE);
  1441. pthread_mutex_unlock (&engine->graph_lock);
  1442. return ret;
  1443. }
  1444. static int
  1445. jack_create_fifo (jack_engine_t *engine, int which_fifo)
  1446. {
  1447. char path[FIFO_NAME_SIZE+1];
  1448. sprintf (path, "%s-%d", engine->fifo_prefix, which_fifo);
  1449. if (mknod (path, 0666|S_IFIFO, 0) < 0) {
  1450. if (errno != EEXIST) {
  1451. jack_error ("cannot create inter-client FIFO [%s] (%s)", path, strerror (errno));
  1452. return -1;
  1453. }
  1454. } else {
  1455. on_exit (unlink_path, strdup (path));
  1456. }
  1457. jack_get_fifo_fd (engine, which_fifo);
  1458. return 0;
  1459. }
  1460. static int
  1461. jack_get_fifo_fd (jack_engine_t *engine, int which_fifo)
  1462. {
  1463. char path[FIFO_NAME_SIZE+1];
  1464. sprintf (path, "%s-%d", engine->fifo_prefix, which_fifo);
  1465. if (which_fifo >= engine->fifo_size) {
  1466. int i;
  1467. engine->fifo = (int *) realloc (engine->fifo, sizeof (int) * engine->fifo_size + 16);
  1468. for (i = engine->fifo_size; i < engine->fifo_size + 16; i++) {
  1469. engine->fifo[i] = -1;
  1470. }
  1471. engine->fifo_size += 16;
  1472. }
  1473. if (engine->fifo[which_fifo] < 0) {
  1474. if ((engine->fifo[which_fifo] = open (path, O_RDWR|O_CREAT, 0666)) < 0) {
  1475. jack_error ("cannot open fifo [%s] (%s)", path, strerror (errno));
  1476. return -1;
  1477. }
  1478. }
  1479. return engine->fifo[which_fifo];
  1480. }
  1481. int
  1482. jack_use_driver (jack_engine_t *engine, jack_driver_t *driver)
  1483. {
  1484. if (engine->driver) {
  1485. engine->driver->detach (engine->driver, engine);
  1486. engine->driver = 0;
  1487. }
  1488. if (driver) {
  1489. if (driver->attach (driver, engine)) {
  1490. return -1;
  1491. }
  1492. }
  1493. engine->driver = driver;
  1494. return 0;
  1495. }
  1496. /* PORT RELATED FUNCTIONS */
  1497. jack_port_id_t
  1498. jack_get_free_port (jack_engine_t *engine)
  1499. {
  1500. jack_port_id_t i;
  1501. pthread_mutex_lock (&engine->port_lock);
  1502. for (i = 0; i < engine->port_max; i++) {
  1503. if (engine->control->ports[i].in_use == 0) {
  1504. engine->control->ports[i].in_use = 1;
  1505. break;
  1506. }
  1507. }
  1508. pthread_mutex_unlock (&engine->port_lock);
  1509. if (i == engine->port_max) {
  1510. return NoPort;
  1511. }
  1512. return i;
  1513. }
  1514. static void
  1515. jack_port_release (jack_engine_t *engine, jack_port_internal_t *port)
  1516. {
  1517. /* XXX add the buffer used by the port back the (correct) freelist */
  1518. pthread_mutex_lock (&engine->port_lock);
  1519. port->shared->in_use = 0;
  1520. pthread_mutex_unlock (&engine->port_lock);
  1521. }
  1522. jack_port_internal_t *
  1523. jack_get_port_internal_by_name (jack_engine_t *engine, const char *name)
  1524. {
  1525. jack_port_id_t id;
  1526. pthread_mutex_lock (&engine->port_lock);
  1527. for (id = 0; id < engine->port_max; id++) {
  1528. if (strcmp (engine->control->ports[id].name, name) == 0) {
  1529. break;
  1530. }
  1531. }
  1532. pthread_mutex_unlock (&engine->port_lock);
  1533. if (id != engine->port_max) {
  1534. return &engine->internal_ports[id];
  1535. } else {
  1536. return NULL;
  1537. }
  1538. }
  1539. int
  1540. jack_port_do_register (jack_engine_t *engine, jack_request_t *req)
  1541. {
  1542. GSList *node;
  1543. jack_port_id_t port_id;
  1544. jack_port_shared_t *shared;
  1545. jack_port_internal_t *port;
  1546. jack_client_internal_t *client;
  1547. jack_port_type_info_t *type_info;
  1548. pthread_mutex_lock (&engine->graph_lock);
  1549. if ((client = jack_client_internal_by_id (engine, req->x.port_info.client_id)) == 0) {
  1550. jack_error ("unknown client id in port registration request");
  1551. return -1;
  1552. }
  1553. pthread_mutex_unlock (&engine->graph_lock);
  1554. if ((port_id = jack_get_free_port (engine)) == NoPort) {
  1555. jack_error ("no ports available!");
  1556. return -1;
  1557. }
  1558. shared = &engine->control->ports[port_id];
  1559. strcpy (shared->name, req->x.port_info.name);
  1560. shared->client_id = req->x.port_info.client_id;
  1561. shared->flags = req->x.port_info.flags;
  1562. shared->locked = 0;
  1563. shared->buffer_size = req->x.port_info.buffer_size;
  1564. port = &engine->internal_ports[port_id];
  1565. port->shared = shared;
  1566. port->connections = 0;
  1567. type_info = NULL;
  1568. for (node = engine->port_types; node; node = g_slist_next (node)) {
  1569. if (strcmp (req->x.port_info.type, ((jack_port_type_info_t *) node->data)->type_name) == 0) {
  1570. type_info = (jack_port_type_info_t *) node->data;
  1571. break;
  1572. }
  1573. }
  1574. if (type_info == NULL) {
  1575. /* not a builtin type, so allocate a new type_info structure,
  1576. and fill it appropriately.
  1577. */
  1578. type_info = (jack_port_type_info_t *) malloc (sizeof (jack_port_type_info_t));
  1579. type_info->type_name = strdup (req->x.port_info.type);
  1580. type_info->mixdown = NULL; /* we have no idea how to mix this */
  1581. type_info->buffer_scale_factor = -1; /* use specified port buffer size */
  1582. engine->port_types = g_slist_prepend (engine->port_types, type_info);
  1583. }
  1584. memcpy (&port->shared->type_info, type_info, sizeof (jack_port_type_info_t));
  1585. if (jack_port_assign_buffer (engine, port)) {
  1586. jack_error ("cannot assign buffer for port");
  1587. return -1;
  1588. }
  1589. pthread_mutex_lock (&engine->graph_lock);
  1590. client->ports = g_slist_prepend (client->ports, port);
  1591. jack_port_registration_notify (engine, port_id, TRUE);
  1592. pthread_mutex_unlock (&engine->graph_lock);
  1593. req->x.port_info.port_id = port_id;
  1594. return 0;
  1595. }
  1596. int
  1597. jack_port_do_unregister (jack_engine_t *engine, jack_request_t *req)
  1598. {
  1599. jack_client_internal_t *client;
  1600. jack_port_shared_t *shared;
  1601. jack_port_internal_t *port;
  1602. if (req->x.port_info.port_id < 0 || req->x.port_info.port_id > engine->port_max) {
  1603. jack_error ("invalid port ID %d in unregister request\n", req->x.port_info.port_id);
  1604. return -1;
  1605. }
  1606. shared = &engine->control->ports[req->x.port_info.port_id];
  1607. pthread_mutex_lock (&engine->graph_lock);
  1608. if ((client = jack_client_internal_by_id (engine, shared->client_id)) == NULL) {
  1609. jack_error ("unknown client id in port registration request");
  1610. return -1;
  1611. }
  1612. pthread_mutex_unlock (&engine->graph_lock);
  1613. port = &engine->internal_ports[req->x.port_info.port_id];
  1614. jack_port_release (engine, &engine->internal_ports[req->x.port_info.port_id]);
  1615. pthread_mutex_lock (&engine->graph_lock);
  1616. client->ports = g_slist_remove (client->ports, port);
  1617. jack_port_registration_notify (engine, req->x.port_info.port_id, FALSE);
  1618. pthread_mutex_unlock (&engine->graph_lock);
  1619. return 0;
  1620. }
  1621. void
  1622. jack_port_registration_notify (jack_engine_t *engine, jack_port_id_t port_id, int yn)
  1623. {
  1624. jack_event_t event;
  1625. jack_client_internal_t *client;
  1626. GSList *node;
  1627. event.type = (yn ? PortRegistered : PortUnregistered);
  1628. event.x.port_id = port_id;
  1629. for (node = engine->clients; node; node = g_slist_next (node)) {
  1630. client = (jack_client_internal_t *) node->data;
  1631. if (!client->control->active) {
  1632. continue;
  1633. }
  1634. if (client->control->port_register) {
  1635. if (jack_deliver_event (engine, client, &event)) {
  1636. jack_error ("cannot send port registration notification to %s (%s)",
  1637. client->control->name, strerror (errno));
  1638. }
  1639. }
  1640. }
  1641. }
  1642. int
  1643. jack_port_assign_buffer (jack_engine_t *engine, jack_port_internal_t *port)
  1644. {
  1645. GSList *node;
  1646. jack_port_segment_info_t *psi;
  1647. jack_port_buffer_info_t *bi;
  1648. port->shared->shm_key = -1;
  1649. if (port->shared->flags & JackPortIsInput) {
  1650. return 0;
  1651. }
  1652. pthread_mutex_lock (&engine->buffer_lock);
  1653. if (engine->port_buffer_freelist == NULL) {
  1654. jack_error ("no more buffers available!");
  1655. goto out;
  1656. }
  1657. bi = (jack_port_buffer_info_t *) engine->port_buffer_freelist->data;
  1658. for (node = engine->port_segments; node; node = g_slist_next (node)) {
  1659. psi = (jack_port_segment_info_t *) node->data;
  1660. if (bi->shm_key == psi->shm_key) {
  1661. port->shared->shm_key = psi->shm_key;
  1662. port->shared->offset = bi->offset;
  1663. break;
  1664. }
  1665. }
  1666. if (port->shared->shm_key >= 0) {
  1667. engine->port_buffer_freelist = g_slist_remove (engine->port_buffer_freelist, bi);
  1668. } else {
  1669. jack_error ("port segment info for 0x%x:%d not found!", bi->shm_key, bi->offset);
  1670. }
  1671. out:
  1672. pthread_mutex_unlock (&engine->buffer_lock);
  1673. if (port->shared->shm_key < 0) {
  1674. return -1;
  1675. } else {
  1676. return 0;
  1677. }
  1678. }
  1679. static jack_port_internal_t *
  1680. jack_get_port_by_name (jack_engine_t *engine, const char *name)
  1681. {
  1682. jack_port_id_t id;
  1683. /* Note the potential race on "in_use". Other design
  1684. elements prevent this from being a problem.
  1685. */
  1686. for (id = 0; id < engine->port_max; id++) {
  1687. if (engine->control->ports[id].in_use && strcmp (engine->control->ports[id].name, name) == 0) {
  1688. return &engine->internal_ports[id];
  1689. }
  1690. }
  1691. return NULL;
  1692. }
  1693. static int
  1694. jack_send_connection_notification (jack_engine_t *engine, jack_client_id_t client_id,
  1695. jack_port_id_t self_id, jack_port_id_t other_id, int connected)
  1696. {
  1697. jack_client_internal_t *client;
  1698. jack_event_t event;
  1699. if ((client = jack_client_internal_by_id (engine, client_id)) == 0) {
  1700. jack_error ("no such client %d during connection notification", client_id);
  1701. return -1;
  1702. }
  1703. event.type = (connected ? PortConnected : PortDisconnected);
  1704. event.x.self_id = self_id;
  1705. event.y.other_id = other_id;
  1706. if (jack_deliver_event (engine, client, &event)) {
  1707. jack_error ("cannot send port connection notification to client %s (%s)",
  1708. client->control->name, strerror (errno));
  1709. return -1;
  1710. }
  1711. return 0;
  1712. }
  1713. static void
  1714. jack_audio_port_mixdown (jack_port_t *port, nframes_t nframes)
  1715. {
  1716. GSList *node;
  1717. jack_port_t *input;
  1718. nframes_t n;
  1719. sample_t *buffer;
  1720. sample_t *dst, *src;
  1721. /* by the time we've called this, we've already established
  1722. the existence of more than 1 connection to this input port.
  1723. */
  1724. node = port->connections;
  1725. input = (jack_port_t *) node->data;
  1726. buffer = jack_port_buffer (port);
  1727. memcpy (buffer, jack_port_buffer (input), sizeof (sample_t) * nframes);
  1728. for (node = g_slist_next (node); node; node = g_slist_next (node)) {
  1729. input = (jack_port_t *) node->data;
  1730. n = nframes;
  1731. dst = buffer;
  1732. src = jack_port_buffer (input);
  1733. while (n--) {
  1734. *dst++ += *src++;
  1735. }
  1736. }
  1737. }