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.

4085 lines
97KB

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