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.

4138 lines
99KB

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