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.

4117 lines
98KB

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