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.

3673 lines
92KB

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