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.

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