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.

2579 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->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);
  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. client->control->graph_order_arg = NULL;
  1060. if (req->type == ClientDynamic) {
  1061. if (jack_load_client (engine, client, req->object_path)) {
  1062. jack_error ("cannot dynamically load client from \"%s\"", req->object_path);
  1063. jack_client_delete (engine, client);
  1064. return 0;
  1065. }
  1066. }
  1067. return client;
  1068. }
  1069. static void
  1070. jack_port_clear_connections (jack_engine_t *engine, jack_port_internal_t *port)
  1071. {
  1072. GSList *node, *next;
  1073. for (node = port->connections; node; ) {
  1074. next = g_slist_next (node);
  1075. jack_port_disconnect_internal (engine,
  1076. ((jack_connection_internal_t *) node->data)->source,
  1077. ((jack_connection_internal_t *) node->data)->destination,
  1078. FALSE);
  1079. node = next;
  1080. }
  1081. g_slist_free (port->connections);
  1082. port->connections = 0;
  1083. }
  1084. static void
  1085. jack_remove_client (jack_engine_t *engine, jack_client_internal_t *client)
  1086. {
  1087. GSList *node;
  1088. int i;
  1089. /* caller must hold the graph_lock */
  1090. /* these stop the process() loop from paying this client any attention,
  1091. as well as stopping jack_deliver_event() from bothering to try to
  1092. talk to the client.
  1093. */
  1094. client->control->dead = TRUE;
  1095. client->control->active = FALSE;
  1096. if (client == engine->timebase_client) {
  1097. engine->timebase_client = 0;
  1098. engine->control->time.frame = 0;
  1099. }
  1100. /* FIXME: called again in jack_client_delete(),
  1101. still needed here? (kaiv)
  1102. */
  1103. jack_client_disconnect (engine, client);
  1104. /* try to force the server thread to return from poll */
  1105. close (client->event_fd);
  1106. close (client->request_fd);
  1107. for (node = engine->clients; node; node = g_slist_next (node)) {
  1108. if (((jack_client_internal_t *) node->data)->control->id == client->control->id) {
  1109. engine->clients = g_slist_remove_link (engine->clients, node);
  1110. g_slist_free_1 (node);
  1111. break;
  1112. }
  1113. }
  1114. jack_client_do_deactivate (engine, client);
  1115. /* rearrange the pollfd array so that things work right the
  1116. next time we go into poll(2).
  1117. */
  1118. for (i = 0; i < engine->pfd_max; i++) {
  1119. if (engine->pfd[i].fd == client->request_fd) {
  1120. if (i+1 < engine->pfd_max) {
  1121. memmove (&engine->pfd[i], &engine->pfd[i+1], sizeof (struct pollfd) * (engine->pfd_max - i));
  1122. }
  1123. engine->pfd_max--;
  1124. }
  1125. }
  1126. jack_client_delete (engine, client);
  1127. }
  1128. static void
  1129. jack_client_delete (jack_engine_t *engine, jack_client_internal_t *client)
  1130. {
  1131. jack_client_disconnect (engine, client);
  1132. if (jack_client_is_inprocess (client)) {
  1133. jack_client_unload (client);
  1134. free ((char *) client->control);
  1135. } else {
  1136. shmdt ((void *) client->control);
  1137. }
  1138. free (client);
  1139. }
  1140. jack_client_internal_t *
  1141. jack_client_by_name (jack_engine_t *engine, const char *name)
  1142. {
  1143. jack_client_internal_t *client = NULL;
  1144. GSList *node;
  1145. pthread_mutex_lock (&engine->graph_lock);
  1146. for (node = engine->clients; node; node = g_slist_next (node)) {
  1147. if (strcmp ((const char *) ((jack_client_internal_t *) node->data)->control->name, name) == 0) {
  1148. client = (jack_client_internal_t *) node->data;
  1149. break;
  1150. }
  1151. }
  1152. pthread_mutex_unlock (&engine->graph_lock);
  1153. return client;
  1154. }
  1155. jack_client_internal_t *
  1156. jack_client_internal_by_id (jack_engine_t *engine, jack_client_id_t id)
  1157. {
  1158. jack_client_internal_t *client = NULL;
  1159. GSList *node;
  1160. /* call tree ***MUST HOLD*** engine->graph_lock */
  1161. for (node = engine->clients; node; node = g_slist_next (node)) {
  1162. if (((jack_client_internal_t *) node->data)->control->id == id) {
  1163. client = (jack_client_internal_t *) node->data;
  1164. break;
  1165. }
  1166. }
  1167. return client;
  1168. }
  1169. static int
  1170. jack_deliver_event (jack_engine_t *engine, jack_client_internal_t *client, jack_event_t *event)
  1171. {
  1172. char status;
  1173. int client_err = 0;
  1174. /* caller must hold the graph_lock */
  1175. if (client->control->dead) {
  1176. return 0;
  1177. }
  1178. if (jack_client_is_inprocess (client)) {
  1179. switch (event->type) {
  1180. case PortConnected:
  1181. case PortDisconnected:
  1182. jack_client_handle_port_connection (client->control->private_internal_client, event);
  1183. break;
  1184. case BufferSizeChange:
  1185. if (client->control->bufsize) {
  1186. client->control->bufsize (event->x.n, client->control->bufsize_arg);
  1187. }
  1188. break;
  1189. case SampleRateChange:
  1190. if (client->control->srate) {
  1191. client->control->srate (event->x.n, client->control->bufsize_arg);
  1192. }
  1193. break;
  1194. default:
  1195. /* internal clients don't need to know */
  1196. break;
  1197. }
  1198. } else {
  1199. if (write (client->event_fd, event, sizeof (*event)) != sizeof (*event)) {
  1200. jack_error ("cannot send event to client [%s] (%s)", client->control->name, strerror (errno));
  1201. client_err++;
  1202. }
  1203. if (!client_err && (read (client->event_fd, &status, sizeof (status)) != sizeof (status))) {
  1204. jack_error ("cannot read event response from client [%s] (%s)", client->control->name, strerror (errno));
  1205. client_err++;
  1206. }
  1207. if (client_err || status != 0) {
  1208. /* FIXME: caller is not expecting the client to be
  1209. removed while delivering an event! (kaiv)
  1210. */
  1211. jack_error("error while delivering an event");
  1212. /* jack_remove_client (engine, client); */
  1213. }
  1214. }
  1215. return 0;
  1216. }
  1217. int
  1218. jack_client_set_order (jack_engine_t *engine, jack_client_internal_t *client)
  1219. {
  1220. jack_event_t event;
  1221. event.type = GraphReordered;
  1222. event.x.n = client->execution_order;
  1223. return jack_deliver_event (engine, client, &event);
  1224. }
  1225. int
  1226. jack_rechain_graph (jack_engine_t *engine)
  1227. {
  1228. GSList *node, *next;
  1229. unsigned long n;
  1230. int err = 0;
  1231. jack_client_internal_t *client, *subgraph_client, *next_client;
  1232. jack_clear_fifos (engine);
  1233. subgraph_client = 0;
  1234. if (engine->verbose) {
  1235. fprintf(stderr, "-- jack_rechain_graph():\n");
  1236. }
  1237. for (n = 0, node = engine->clients, next = NULL; node; node = next) {
  1238. next = g_slist_next (node);
  1239. if (((jack_client_internal_t *) node->data)->control->active) {
  1240. client = (jack_client_internal_t *) node->data;
  1241. /* find the next active client. its ok for this to be NULL */
  1242. while (next) {
  1243. if (((jack_client_internal_t *) next->data)->control->active) {
  1244. break;
  1245. }
  1246. next = g_slist_next (next);
  1247. };
  1248. if (next == NULL) {
  1249. next_client = NULL;
  1250. } else {
  1251. next_client = (jack_client_internal_t *) next->data;
  1252. }
  1253. client->execution_order = n;
  1254. client->next_client = next_client;
  1255. if (jack_client_is_inprocess (client)) {
  1256. /* break the chain for the current subgraph. the server
  1257. will wait for chain on the nth FIFO, and will
  1258. then execute this in-process client.
  1259. */
  1260. if (subgraph_client) {
  1261. subgraph_client->subgraph_wait_fd = jack_get_fifo_fd (engine, n);
  1262. if (engine->verbose) {
  1263. fprintf(stderr, "client %s: wait_fd=%d, execution_order=%lu.\n",
  1264. subgraph_client->control->name, subgraph_client->subgraph_wait_fd, n);
  1265. }
  1266. n++;
  1267. }
  1268. if (engine->verbose) {
  1269. fprintf(stderr, "client %s: inprocess client, execution_order=%lu.\n",
  1270. client->control->name, n);
  1271. }
  1272. if (client->control->graph_order) {
  1273. client->control->graph_order (client->control->graph_order_arg);
  1274. }
  1275. subgraph_client = 0;
  1276. } else {
  1277. if (subgraph_client == NULL) {
  1278. /* start a new subgraph. the engine will start the chain
  1279. by writing to the nth FIFO.
  1280. */
  1281. subgraph_client = client;
  1282. subgraph_client->subgraph_start_fd = jack_get_fifo_fd (engine, n);
  1283. if (engine->verbose) {
  1284. fprintf(stderr, "client %s: start_fd=%d, execution_order=%lu.\n",
  1285. subgraph_client->control->name, subgraph_client->subgraph_start_fd, n);
  1286. }
  1287. }
  1288. else {
  1289. if (engine->verbose) {
  1290. fprintf(stderr, "client %s: in subgraph after %s, execution_order=%lu.\n",
  1291. client->control->name, subgraph_client->control->name, n);
  1292. }
  1293. }
  1294. /* make sure fifo for 'n + 1' exists
  1295. * before issuing client reorder
  1296. */
  1297. (void) jack_get_fifo_fd(engine, n + 1);
  1298. jack_client_set_order (engine, client);
  1299. n++;
  1300. }
  1301. }
  1302. }
  1303. if (subgraph_client) {
  1304. subgraph_client->subgraph_wait_fd = jack_get_fifo_fd (engine, n);
  1305. if (engine->verbose) {
  1306. fprintf(stderr, "client %s: wait_fd=%d, execution_order=%lu (last client).\n",
  1307. subgraph_client->control->name, subgraph_client->subgraph_wait_fd, n);
  1308. }
  1309. }
  1310. return err;
  1311. }
  1312. static void
  1313. jack_trace_terminal (jack_client_internal_t *c1, jack_client_internal_t *rbase)
  1314. {
  1315. jack_client_internal_t *c2;
  1316. /* make a copy of the existing list of routes that feed c1. this provides
  1317. us with an atomic snapshot of c1's "fed-by" state, which will be
  1318. modified as we progress ...
  1319. */
  1320. GSList *existing;
  1321. GSList *node;
  1322. if (c1->fed_by == NULL) {
  1323. return;
  1324. }
  1325. existing = g_slist_copy (c1->fed_by);
  1326. /* for each route that feeds c1, recurse, marking it as feeding
  1327. rbase as well.
  1328. */
  1329. for (node = existing; node; node = g_slist_next (node)) {
  1330. c2 = (jack_client_internal_t *) node->data;
  1331. /* c2 is a route that feeds c1 which somehow feeds base. mark
  1332. base as being fed by c2, but don't do it more than
  1333. once.
  1334. */
  1335. if (c2 != rbase && c2 != c1) {
  1336. if (g_slist_find (rbase->fed_by, c2) == NULL) {
  1337. rbase->fed_by = g_slist_prepend (rbase->fed_by, c2);
  1338. }
  1339. /* FIXME: if c2->fed_by is not up-to-date, we may end up
  1340. recursing infinitely (kaiv)
  1341. */
  1342. if (g_slist_find (c2->fed_by, c1) == NULL) {
  1343. /* now recurse, so that we can mark base as being fed by
  1344. all routes that feed c2
  1345. */
  1346. jack_trace_terminal (c2, rbase);
  1347. }
  1348. }
  1349. }
  1350. g_slist_free (existing);
  1351. }
  1352. static int
  1353. jack_client_sort (jack_client_internal_t *a, jack_client_internal_t *b)
  1354. {
  1355. if (g_slist_find (a->fed_by, b)) {
  1356. if (g_slist_find (b->fed_by, a)) {
  1357. /* feedback loop: if `a' is the driver
  1358. client, let that execute first.
  1359. */
  1360. if (a->control->type == ClientDriver) {
  1361. /* b comes after a */
  1362. return -1;
  1363. }
  1364. }
  1365. /* a comes after b */
  1366. return 1;
  1367. } else if (g_slist_find (b->fed_by, a)) {
  1368. if (g_slist_find (a->fed_by, b)) {
  1369. /* feedback loop: if `b' is the driver
  1370. client, let that execute first.
  1371. */
  1372. if (b->control->type == ClientDriver) {
  1373. /* b comes before a */
  1374. return 1;
  1375. }
  1376. }
  1377. /* b comes after a */
  1378. return -1;
  1379. } else {
  1380. /* we don't care */
  1381. return 0;
  1382. }
  1383. }
  1384. static int
  1385. jack_client_feeds (jack_client_internal_t *might, jack_client_internal_t *target)
  1386. {
  1387. GSList *pnode, *cnode;
  1388. /* Check every port of `might' for an outbound connection to `target'
  1389. */
  1390. for (pnode = might->ports; pnode; pnode = g_slist_next (pnode)) {
  1391. jack_port_internal_t *port;
  1392. port = (jack_port_internal_t *) pnode->data;
  1393. for (cnode = port->connections; cnode; cnode = g_slist_next (cnode)) {
  1394. jack_connection_internal_t *c;
  1395. c = (jack_connection_internal_t *) cnode->data;
  1396. if (c->source->shared->client_id == might->control->id &&
  1397. c->destination->shared->client_id == target->control->id) {
  1398. return 1;
  1399. }
  1400. }
  1401. }
  1402. return 0;
  1403. }
  1404. /**
  1405. * Sorts the network of clients using the following
  1406. * algorithm:
  1407. *
  1408. * 1) figure out who is connected to whom:
  1409. *
  1410. * foreach client1
  1411. * foreach input port
  1412. * foreach client2
  1413. * foreach output port
  1414. * if client1->input port connected to client2->output port
  1415. * mark client1 fed by client 2
  1416. *
  1417. * 2) trace the connections as terminal arcs in the graph so that
  1418. * if client A feeds client B who feeds client C, mark client C
  1419. * as fed by client A as well as client B, and so forth.
  1420. *
  1421. * 3) now sort according to whether or not client1->fed_by (client2) is true.
  1422. * if the condition is true, client2 must execute before client1
  1423. *
  1424. */
  1425. static void
  1426. jack_sort_graph (jack_engine_t *engine)
  1427. {
  1428. GSList *node, *onode;
  1429. jack_client_internal_t *client;
  1430. jack_client_internal_t *oclient;
  1431. for (node = engine->clients; node; node = g_slist_next (node)) {
  1432. client = (jack_client_internal_t *) node->data;
  1433. g_slist_free (client->fed_by);
  1434. client->fed_by = 0;
  1435. for (onode = engine->clients; onode; onode = g_slist_next (onode)) {
  1436. oclient = (jack_client_internal_t *) onode->data;
  1437. if (jack_client_feeds (oclient, client)) {
  1438. client->fed_by = g_slist_prepend (client->fed_by, oclient);
  1439. }
  1440. }
  1441. }
  1442. for (node = engine->clients; node; node = g_slist_next (node)) {
  1443. jack_trace_terminal ((jack_client_internal_t *) node->data,
  1444. (jack_client_internal_t *) node->data);
  1445. }
  1446. engine->clients = g_slist_sort (engine->clients, (GCompareFunc) jack_client_sort);
  1447. jack_rechain_graph (engine);
  1448. }
  1449. /**
  1450. * Dumps current engine configuration to stderr.
  1451. */
  1452. void jack_dump_configuration(jack_engine_t *engine, int take_lock)
  1453. {
  1454. GSList *clientnode, *portnode, *connectionnode;
  1455. jack_client_internal_t *client;
  1456. jack_client_control_t *ctl;
  1457. jack_port_internal_t *port;
  1458. jack_connection_internal_t* connection;
  1459. int n, m, o;
  1460. fprintf(stderr, "engine.c: <-- dump begins -->\n");
  1461. if (take_lock) {
  1462. pthread_mutex_lock (&engine->graph_lock);
  1463. }
  1464. for (n = 0, clientnode = engine->clients; clientnode; clientnode = g_slist_next (clientnode)) {
  1465. client = (jack_client_internal_t *) clientnode->data;
  1466. ctl = client->control;
  1467. fprintf (stderr, "client #%d: %s (type: %d, process? %s, fed by %d clients)\n",
  1468. ++n,
  1469. ctl->name,
  1470. ctl->type,
  1471. ctl->process ? "yes" : "no",
  1472. g_slist_length(client->fed_by));
  1473. for(m = 0, portnode = client->ports; portnode; portnode = g_slist_next (portnode)) {
  1474. port = (jack_port_internal_t *) portnode->data;
  1475. fprintf(stderr, "\t port #%d: %s\n", ++m, port->shared->name);
  1476. for(o = 0, connectionnode = port->connections;
  1477. connectionnode;
  1478. connectionnode = g_slist_next (connectionnode)) {
  1479. connection = (jack_connection_internal_t *) connectionnode->data;
  1480. fprintf(stderr, "\t\t connection #%d: %s %s\n",
  1481. ++o,
  1482. (port->shared->flags & JackPortIsInput) ? "<-" : "->",
  1483. (port->shared->flags & JackPortIsInput) ?
  1484. connection->source->shared->name :
  1485. connection->destination->shared->name);
  1486. }
  1487. }
  1488. }
  1489. if (take_lock) {
  1490. pthread_mutex_unlock (&engine->graph_lock);
  1491. }
  1492. fprintf(stderr, "engine.c: <-- dump ends -->\n");
  1493. }
  1494. static int
  1495. jack_port_do_connect (jack_engine_t *engine,
  1496. const char *source_port,
  1497. const char *destination_port)
  1498. {
  1499. jack_connection_internal_t *connection;
  1500. jack_port_internal_t *srcport, *dstport;
  1501. jack_port_id_t src_id, dst_id;
  1502. if ((srcport = jack_get_port_by_name (engine, source_port)) == NULL) {
  1503. jack_error ("unknown source port in attempted connection [%s]", source_port);
  1504. return -1;
  1505. }
  1506. if ((dstport = jack_get_port_by_name (engine, destination_port)) == NULL) {
  1507. jack_error ("unknown destination port in attempted connection [%s]", destination_port);
  1508. return -1;
  1509. }
  1510. if ((dstport->shared->flags & JackPortIsInput) == 0) {
  1511. jack_error ("destination port in attempted connection of %s and %s is not an input port",
  1512. source_port, destination_port);
  1513. return -1;
  1514. }
  1515. if ((srcport->shared->flags & JackPortIsOutput) == 0) {
  1516. jack_error ("source port in attempted connection of %s and %s is not an output port",
  1517. source_port, destination_port);
  1518. return -1;
  1519. }
  1520. if (srcport->shared->locked) {
  1521. jack_error ("source port %s is locked against connection changes", source_port);
  1522. return -1;
  1523. }
  1524. if (dstport->shared->locked) {
  1525. jack_error ("destination port %s is locked against connection changes", destination_port);
  1526. return -1;
  1527. }
  1528. if (strcmp (srcport->shared->type_info.type_name,
  1529. dstport->shared->type_info.type_name) != 0) {
  1530. jack_error ("ports used in attemped connection are not of the same data type");
  1531. return -1;
  1532. }
  1533. connection = (jack_connection_internal_t *) malloc (sizeof (jack_connection_internal_t));
  1534. connection->source = srcport;
  1535. connection->destination = dstport;
  1536. src_id = srcport->shared->id;
  1537. dst_id = dstport->shared->id;
  1538. pthread_mutex_lock (&engine->graph_lock);
  1539. if (dstport->connections && dstport->shared->type_info.mixdown == NULL) {
  1540. jack_error ("cannot make multiple connections to a port of type [%s]", dstport->shared->type_info.type_name);
  1541. free (connection);
  1542. return -1;
  1543. } else {
  1544. if (engine->verbose) {
  1545. fprintf (stderr, "connect %s and %s\n",
  1546. srcport->shared->name,
  1547. dstport->shared->name);
  1548. }
  1549. dstport->connections = g_slist_prepend (dstport->connections, connection);
  1550. srcport->connections = g_slist_prepend (srcport->connections, connection);
  1551. jack_sort_graph (engine);
  1552. jack_send_connection_notification (engine, srcport->shared->client_id, src_id, dst_id, TRUE);
  1553. jack_send_connection_notification (engine, dstport->shared->client_id, dst_id, src_id, TRUE);
  1554. }
  1555. pthread_mutex_unlock (&engine->graph_lock);
  1556. return 0;
  1557. }
  1558. int
  1559. jack_port_disconnect_internal (jack_engine_t *engine,
  1560. jack_port_internal_t *srcport,
  1561. jack_port_internal_t *dstport,
  1562. int sort_graph)
  1563. {
  1564. GSList *node;
  1565. jack_connection_internal_t *connect;
  1566. int ret = -1;
  1567. jack_port_id_t src_id, dst_id;
  1568. /* call tree **** MUST HOLD **** engine->graph_lock. */
  1569. for (node = srcport->connections; node; node = g_slist_next (node)) {
  1570. connect = (jack_connection_internal_t *) node->data;
  1571. if (connect->source == srcport && connect->destination == dstport) {
  1572. if (engine->verbose) {
  1573. fprintf (stderr, "DIS-connect %s and %s\n",
  1574. srcport->shared->name,
  1575. dstport->shared->name);
  1576. }
  1577. srcport->connections = g_slist_remove (srcport->connections, connect);
  1578. dstport->connections = g_slist_remove (dstport->connections, connect);
  1579. src_id = srcport->shared->id;
  1580. dst_id = dstport->shared->id;
  1581. /* this is a bit harsh, but it basically says that if we actually
  1582. do a disconnect, and its the last one, then make sure that
  1583. any input monitoring is turned off on the srcport. this isn't
  1584. ideal for all situations, but it works better for most of them.
  1585. */
  1586. if (srcport->connections == NULL) {
  1587. srcport->shared->monitor_requests = 0;
  1588. }
  1589. jack_send_connection_notification (engine, srcport->shared->client_id, src_id, dst_id, FALSE);
  1590. jack_send_connection_notification (engine, dstport->shared->client_id, dst_id, src_id, FALSE);
  1591. free (connect);
  1592. ret = 0;
  1593. break;
  1594. }
  1595. }
  1596. if (sort_graph) {
  1597. jack_sort_graph (engine);
  1598. }
  1599. return ret;
  1600. }
  1601. static int
  1602. jack_port_do_disconnect_all (jack_engine_t *engine,
  1603. jack_port_id_t port_id)
  1604. {
  1605. if (port_id >= engine->control->port_max) {
  1606. jack_error ("illegal port ID in attempted disconnection [%u]", port_id);
  1607. return -1;
  1608. }
  1609. if (engine->verbose) {
  1610. fprintf (stderr, "clear connections for %s\n", engine->internal_ports[port_id].shared->name);
  1611. }
  1612. pthread_mutex_lock (&engine->graph_lock);
  1613. jack_port_clear_connections (engine, &engine->internal_ports[port_id]);
  1614. jack_sort_graph (engine);
  1615. pthread_mutex_unlock (&engine->graph_lock);
  1616. return 0;
  1617. }
  1618. static int
  1619. jack_port_do_disconnect (jack_engine_t *engine,
  1620. const char *source_port,
  1621. const char *destination_port)
  1622. {
  1623. jack_port_internal_t *srcport, *dstport;
  1624. int ret = -1;
  1625. if ((srcport = jack_get_port_by_name (engine, source_port)) == NULL) {
  1626. jack_error ("unknown source port in attempted disconnection [%s]", source_port);
  1627. return -1;
  1628. }
  1629. if ((dstport = jack_get_port_by_name (engine, destination_port)) == NULL) {
  1630. jack_error ("unknown destination port in attempted connection [%s]", destination_port);
  1631. return -1;
  1632. }
  1633. pthread_mutex_lock (&engine->graph_lock);
  1634. ret = jack_port_disconnect_internal (engine, srcport, dstport, TRUE);
  1635. pthread_mutex_unlock (&engine->graph_lock);
  1636. return ret;
  1637. }
  1638. static nframes_t
  1639. jack_get_port_total_latency (jack_engine_t *engine, jack_port_internal_t *port)
  1640. {
  1641. GSList *node;
  1642. nframes_t latency;
  1643. nframes_t max_latency = 0;
  1644. /* call tree must 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. if (connection->source->shared->flags & JackPortIsTerminal) {
  1655. this_latency = connection->source->shared->latency;
  1656. } else {
  1657. this_latency = jack_get_port_total_latency (engine, connection->source);
  1658. }
  1659. } else {
  1660. /* "port" is the source, so get the latency of the destination */
  1661. if (connection->destination->shared->flags & JackPortIsTerminal) {
  1662. this_latency = connection->destination->shared->latency;
  1663. } else {
  1664. this_latency = jack_get_port_total_latency (engine, connection->destination);
  1665. }
  1666. }
  1667. if (this_latency > max_latency) {
  1668. max_latency = this_latency;
  1669. }
  1670. }
  1671. return latency + max_latency;
  1672. }
  1673. static int
  1674. jack_get_total_latency (jack_engine_t *engine, const char *portname, nframes_t *latency)
  1675. {
  1676. jack_port_internal_t *port;
  1677. pthread_mutex_lock (&engine->graph_lock);
  1678. if ((port = jack_get_port_by_name (engine, portname)) == NULL) {
  1679. pthread_mutex_unlock (&engine->graph_lock);
  1680. return -1;
  1681. }
  1682. *latency = jack_get_port_total_latency (engine, port);
  1683. pthread_mutex_unlock (&engine->graph_lock);
  1684. return 0;
  1685. }
  1686. static int
  1687. jack_get_fifo_fd (jack_engine_t *engine, int which_fifo)
  1688. {
  1689. /* caller must hold graph_lock */
  1690. char path[PATH_MAX+1];
  1691. struct stat statbuf;
  1692. sprintf (path, "%s-%d", engine->fifo_prefix, which_fifo);
  1693. if (stat (path, &statbuf)) {
  1694. if (errno == ENOENT) {
  1695. if (mknod (path, 0666|S_IFIFO, 0) < 0) {
  1696. jack_error ("cannot create inter-client FIFO [%s] (%s)\n", path, strerror (errno));
  1697. return -1;
  1698. }
  1699. } else {
  1700. jack_error ("cannot check on FIFO %d\n", which_fifo);
  1701. return -1;
  1702. }
  1703. } else {
  1704. if (!S_ISFIFO(statbuf.st_mode)) {
  1705. jack_error ("FIFO %d (%s) already exists, but is not a FIFO!\n", which_fifo, path);
  1706. return -1;
  1707. }
  1708. }
  1709. if (which_fifo >= engine->fifo_size) {
  1710. int i;
  1711. engine->fifo = (int *) realloc (engine->fifo, sizeof (int) * engine->fifo_size + 16);
  1712. for (i = engine->fifo_size; i < engine->fifo_size + 16; i++) {
  1713. engine->fifo[i] = -1;
  1714. }
  1715. engine->fifo_size += 16;
  1716. }
  1717. if (engine->fifo[which_fifo] < 0) {
  1718. if ((engine->fifo[which_fifo] = open (path, O_RDWR|O_CREAT|O_NONBLOCK, 0666)) < 0) {
  1719. jack_error ("cannot open fifo [%s] (%s)", path, strerror (errno));
  1720. return -1;
  1721. }
  1722. }
  1723. return engine->fifo[which_fifo];
  1724. }
  1725. static void
  1726. jack_clear_fifos (jack_engine_t *engine)
  1727. {
  1728. /* caller must hold graph_lock */
  1729. int i;
  1730. char buf[16];
  1731. /* this just drains the existing FIFO's of any data left in them
  1732. by aborted clients, etc. there is only ever going to be
  1733. 0, 1 or 2 bytes in them, but we'll allow for up to 16.
  1734. */
  1735. for (i = 0; i < engine->fifo_size; i++) {
  1736. if (engine->fifo[i] >= 0) {
  1737. int nread = read (engine->fifo[i], buf, sizeof (buf));
  1738. if (nread < 0 && errno != EAGAIN) {
  1739. jack_error ("clear fifo[%d] error: %s", i, strerror (errno));
  1740. }
  1741. }
  1742. }
  1743. }
  1744. int
  1745. jack_use_driver (jack_engine_t *engine, jack_driver_t *driver)
  1746. {
  1747. if (engine->driver) {
  1748. engine->driver->detach (engine->driver, engine);
  1749. engine->driver = 0;
  1750. }
  1751. if (driver) {
  1752. if (driver->attach (driver, engine)) {
  1753. return -1;
  1754. }
  1755. }
  1756. engine->driver = driver;
  1757. return 0;
  1758. }
  1759. /* PORT RELATED FUNCTIONS */
  1760. jack_port_id_t
  1761. jack_get_free_port (jack_engine_t *engine)
  1762. {
  1763. jack_port_id_t i;
  1764. pthread_mutex_lock (&engine->port_lock);
  1765. for (i = 0; i < engine->port_max; i++) {
  1766. if (engine->control->ports[i].in_use == 0) {
  1767. engine->control->ports[i].in_use = 1;
  1768. break;
  1769. }
  1770. }
  1771. pthread_mutex_unlock (&engine->port_lock);
  1772. if (i == engine->port_max) {
  1773. return NoPort;
  1774. }
  1775. return i;
  1776. }
  1777. static void
  1778. jack_port_release (jack_engine_t *engine, jack_port_internal_t *port)
  1779. {
  1780. pthread_mutex_lock (&engine->port_lock);
  1781. port->shared->in_use = 0;
  1782. if (port->buffer_info) {
  1783. pthread_mutex_lock (&engine->buffer_lock);
  1784. engine->port_buffer_freelist = g_slist_prepend (engine->port_buffer_freelist, port->buffer_info);
  1785. pthread_mutex_unlock (&engine->buffer_lock);
  1786. }
  1787. pthread_mutex_unlock (&engine->port_lock);
  1788. }
  1789. jack_port_internal_t *
  1790. jack_get_port_internal_by_name (jack_engine_t *engine, const char *name)
  1791. {
  1792. jack_port_id_t id;
  1793. pthread_mutex_lock (&engine->port_lock);
  1794. for (id = 0; id < engine->port_max; id++) {
  1795. if (strcmp (engine->control->ports[id].name, name) == 0) {
  1796. break;
  1797. }
  1798. }
  1799. pthread_mutex_unlock (&engine->port_lock);
  1800. if (id != engine->port_max) {
  1801. return &engine->internal_ports[id];
  1802. } else {
  1803. return NULL;
  1804. }
  1805. }
  1806. int
  1807. jack_port_do_register (jack_engine_t *engine, jack_request_t *req)
  1808. {
  1809. jack_port_id_t port_id;
  1810. jack_port_shared_t *shared;
  1811. jack_port_internal_t *port;
  1812. jack_client_internal_t *client;
  1813. pthread_mutex_lock (&engine->graph_lock);
  1814. if ((client = jack_client_internal_by_id (engine, req->x.port_info.client_id)) == NULL) {
  1815. jack_error ("unknown client id in port registration request");
  1816. return -1;
  1817. }
  1818. pthread_mutex_unlock (&engine->graph_lock);
  1819. if ((port_id = jack_get_free_port (engine)) == NoPort) {
  1820. jack_error ("no ports available!");
  1821. return -1;
  1822. }
  1823. shared = &engine->control->ports[port_id];
  1824. strcpy (shared->name, req->x.port_info.name);
  1825. shared->client_id = req->x.port_info.client_id;
  1826. shared->flags = req->x.port_info.flags;
  1827. shared->buffer_size = req->x.port_info.buffer_size;
  1828. shared->latency = 0;
  1829. shared->monitor_requests = 0;
  1830. shared->locked = 0;
  1831. port = &engine->internal_ports[port_id];
  1832. port->shared = shared;
  1833. port->connections = 0;
  1834. if (jack_port_assign_buffer (engine, port)) {
  1835. jack_error ("cannot assign buffer for port");
  1836. return -1;
  1837. }
  1838. pthread_mutex_lock (&engine->graph_lock);
  1839. client->ports = g_slist_prepend (client->ports, port);
  1840. jack_port_registration_notify (engine, port_id, TRUE);
  1841. pthread_mutex_unlock (&engine->graph_lock);
  1842. if (engine->verbose) {
  1843. fprintf (stderr, "registered port %s, offset = %u\n", shared->name, shared->offset);
  1844. }
  1845. req->x.port_info.port_id = port_id;
  1846. return 0;
  1847. }
  1848. int
  1849. jack_port_do_unregister (jack_engine_t *engine, jack_request_t *req)
  1850. {
  1851. jack_client_internal_t *client;
  1852. jack_port_shared_t *shared;
  1853. jack_port_internal_t *port;
  1854. if (req->x.port_info.port_id < 0 || req->x.port_info.port_id > engine->port_max) {
  1855. jack_error ("invalid port ID %d in unregister request", req->x.port_info.port_id);
  1856. return -1;
  1857. }
  1858. shared = &engine->control->ports[req->x.port_info.port_id];
  1859. if (shared->client_id != req->x.port_info.client_id) {
  1860. jack_error ("Client %d is not allowed to remove port %s", req->x.port_info.client_id, shared->name);
  1861. return -1;
  1862. }
  1863. pthread_mutex_lock (&engine->graph_lock);
  1864. if ((client = jack_client_internal_by_id (engine, shared->client_id)) == NULL) {
  1865. jack_error ("unknown client id in port registration request");
  1866. pthread_mutex_unlock (&engine->graph_lock);
  1867. return -1;
  1868. }
  1869. port = &engine->internal_ports[req->x.port_info.port_id];
  1870. jack_port_clear_connections (engine, port);
  1871. jack_port_release (engine, &engine->internal_ports[req->x.port_info.port_id]);
  1872. client->ports = g_slist_remove (client->ports, port);
  1873. jack_port_registration_notify (engine, req->x.port_info.port_id, FALSE);
  1874. pthread_mutex_unlock (&engine->graph_lock);
  1875. return 0;
  1876. }
  1877. void
  1878. jack_port_registration_notify (jack_engine_t *engine, jack_port_id_t port_id, int yn)
  1879. {
  1880. jack_event_t event;
  1881. jack_client_internal_t *client;
  1882. GSList *node;
  1883. event.type = (yn ? PortRegistered : PortUnregistered);
  1884. event.x.port_id = port_id;
  1885. for (node = engine->clients; node; node = g_slist_next (node)) {
  1886. client = (jack_client_internal_t *) node->data;
  1887. if (!client->control->active) {
  1888. continue;
  1889. }
  1890. if (client->control->port_register) {
  1891. if (jack_deliver_event (engine, client, &event)) {
  1892. jack_error ("cannot send port registration notification to %s (%s)",
  1893. client->control->name, strerror (errno));
  1894. }
  1895. }
  1896. }
  1897. }
  1898. int
  1899. jack_port_assign_buffer (jack_engine_t *engine, jack_port_internal_t *port)
  1900. {
  1901. GSList *node;
  1902. jack_port_segment_info_t *psi = 0;
  1903. jack_port_buffer_info_t *bi;
  1904. port->shared->shm_key = -1;
  1905. if (port->shared->flags & JackPortIsInput) {
  1906. port->shared->offset = 0;
  1907. return 0;
  1908. }
  1909. pthread_mutex_lock (&engine->buffer_lock);
  1910. if (engine->port_buffer_freelist == NULL) {
  1911. jack_error ("no more buffers available!");
  1912. goto out;
  1913. }
  1914. bi = (jack_port_buffer_info_t *) engine->port_buffer_freelist->data;
  1915. for (node = engine->port_segments; node; node = g_slist_next (node)) {
  1916. psi = (jack_port_segment_info_t *) node->data;
  1917. if (bi->shm_key == psi->shm_key) {
  1918. port->shared->shm_key = psi->shm_key;
  1919. port->shared->offset = bi->offset;
  1920. port->buffer_info = bi;
  1921. break;
  1922. }
  1923. }
  1924. if (port->shared->shm_key >= 0) {
  1925. engine->port_buffer_freelist = g_slist_remove (engine->port_buffer_freelist, bi);
  1926. } else {
  1927. jack_error ("port segment info for 0x%x:%d not found!", bi->shm_key, bi->offset);
  1928. }
  1929. out:
  1930. pthread_mutex_unlock (&engine->buffer_lock);
  1931. if (port->shared->shm_key < 0) {
  1932. return -1;
  1933. } else {
  1934. return 0;
  1935. }
  1936. }
  1937. static jack_port_internal_t *
  1938. jack_get_port_by_name (jack_engine_t *engine, const char *name)
  1939. {
  1940. jack_port_id_t id;
  1941. /* Note the potential race on "in_use". Other design
  1942. elements prevent this from being a problem.
  1943. */
  1944. for (id = 0; id < engine->port_max; id++) {
  1945. if (engine->control->ports[id].in_use && strcmp (engine->control->ports[id].name, name) == 0) {
  1946. return &engine->internal_ports[id];
  1947. }
  1948. }
  1949. return NULL;
  1950. }
  1951. static int
  1952. jack_send_connection_notification (jack_engine_t *engine, jack_client_id_t client_id,
  1953. jack_port_id_t self_id, jack_port_id_t other_id, int connected)
  1954. {
  1955. jack_client_internal_t *client;
  1956. jack_event_t event;
  1957. if ((client = jack_client_internal_by_id (engine, client_id)) == NULL) {
  1958. jack_error ("no such client %d during connection notification", client_id);
  1959. return -1;
  1960. }
  1961. event.type = (connected ? PortConnected : PortDisconnected);
  1962. event.x.self_id = self_id;
  1963. event.y.other_id = other_id;
  1964. if (jack_deliver_event (engine, client, &event)) {
  1965. jack_error ("cannot send port connection notification to client %s (%s)",
  1966. client->control->name, strerror (errno));
  1967. return -1;
  1968. }
  1969. return 0;
  1970. }
  1971. void
  1972. jack_set_asio_mode (jack_engine_t *engine, int yn)
  1973. {
  1974. engine->asio_mode = yn;
  1975. }