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.

4546 lines
115KB

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