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.

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