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.

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