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.

4119 lines
102KB

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