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.

4142 lines
103KB

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