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.

2812 lines
68KB

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