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.

2841 lines
69KB

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