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.

2560 lines
62KB

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