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.

3652 lines
93KB

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