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.

2623 lines
63KB

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