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
114KB

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