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.

3295 lines
80KB

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