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.

4016 lines
100KB

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