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.

3647 lines
92KB

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