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.

4213 lines
101KB

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