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.

4198 lines
100KB

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