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.

4182 lines
99KB

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