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.

4494 lines
113KB

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