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.

4872 lines
128KB

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