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.

4131 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. engine->control->client_priority = (realtime
  1499. ? engine->rtpriority - 1
  1500. : 0);
  1501. engine->control->do_mlock = do_mlock;
  1502. engine->control->do_munlock = do_unlock;
  1503. engine->control->cpu_load = 0;
  1504. engine->control->xrun_delayed_usecs = 0;
  1505. engine->control->max_delayed_usecs = 0;
  1506. jack_set_clock_source (clock_source);
  1507. engine->control->clock_source = clock_source;
  1508. VERBOSE (engine, "clock source = %s", jack_clock_source_name (clock_source));
  1509. engine->control->frame_timer.frames = frame_time_offset;
  1510. engine->control->frame_timer.reset_pending = 0;
  1511. engine->control->frame_timer.current_wakeup = 0;
  1512. engine->control->frame_timer.next_wakeup = 0;
  1513. engine->control->frame_timer.initialized = 0;
  1514. engine->control->frame_timer.filter_coefficient = 0.01;
  1515. engine->control->frame_timer.second_order_integrator = 0;
  1516. engine->first_wakeup = 1;
  1517. engine->control->buffer_size = 0;
  1518. jack_transport_init (engine);
  1519. jack_set_sample_rate (engine, 0);
  1520. engine->control->internal = 0;
  1521. engine->control->has_capabilities = 0;
  1522. #ifdef JACK_USE_MACH_THREADS
  1523. /* specific resources for server/client real-time thread
  1524. * communication */
  1525. engine->servertask = mach_task_self();
  1526. if (task_get_bootstrap_port(engine->servertask, &engine->bp)){
  1527. jack_error("Jackd: Can't find bootstrap mach port");
  1528. return NULL;
  1529. }
  1530. engine->portnum = 0;
  1531. #endif /* JACK_USE_MACH_THREADS */
  1532. #ifdef USE_CAPABILITIES
  1533. if (uid == 0 || euid == 0) {
  1534. VERBOSE (engine, "running with uid=%d and euid=%d, "
  1535. "will not try to use capabilites",
  1536. uid, euid);
  1537. } else {
  1538. /* only try to use capabilities if not running as root */
  1539. engine->control->has_capabilities = check_capabilities (engine);
  1540. if (engine->control->has_capabilities == 0) {
  1541. VERBOSE (engine, "required capabilities not "
  1542. "available");
  1543. }
  1544. if (engine->verbose) {
  1545. size_t size;
  1546. cap_t cap = cap_init();
  1547. capgetp(0, cap);
  1548. VERBOSE (engine, "capabilities: %s",
  1549. cap_to_text(cap, &size));
  1550. }
  1551. }
  1552. #endif /* USE_CAPABILITIES */
  1553. engine->control->engine_ok = 1;
  1554. snprintf (engine->fifo_prefix, sizeof (engine->fifo_prefix),
  1555. "%s/jack-ack-fifo-%d",
  1556. jack_server_dir (engine->server_name, server_dir), getpid ());
  1557. (void) jack_get_fifo_fd (engine, 0);
  1558. jack_client_create_thread (NULL, &engine->server_thread, 0, FALSE,
  1559. &jack_server_thread, engine);
  1560. return engine;
  1561. }
  1562. static void
  1563. jack_engine_delay (jack_engine_t *engine, float delayed_usecs)
  1564. {
  1565. jack_event_t event;
  1566. engine->control->frame_timer.reset_pending = 1;
  1567. engine->control->xrun_delayed_usecs = delayed_usecs;
  1568. if (delayed_usecs > engine->control->max_delayed_usecs)
  1569. engine->control->max_delayed_usecs = delayed_usecs;
  1570. event.type = XRun;
  1571. jack_deliver_event_to_all (engine, &event);
  1572. }
  1573. static inline void
  1574. jack_inc_frame_time (jack_engine_t *engine, jack_nframes_t nframes)
  1575. {
  1576. jack_frame_timer_t *timer = &engine->control->frame_timer;
  1577. jack_time_t now = engine->driver->last_wait_ust; // effective time
  1578. float delta;
  1579. // really need a memory barrier here
  1580. timer->guard1++;
  1581. delta = (int64_t) now - (int64_t) timer->next_wakeup;
  1582. timer->current_wakeup = timer->next_wakeup;
  1583. timer->frames += nframes;
  1584. timer->second_order_integrator += 0.5f *
  1585. timer->filter_coefficient * delta;
  1586. timer->next_wakeup = timer->current_wakeup +
  1587. engine->driver->period_usecs +
  1588. (int64_t) floorf ((timer->filter_coefficient *
  1589. (delta + timer->second_order_integrator)));
  1590. timer->initialized = 1;
  1591. // might need a memory barrier here
  1592. timer->guard2++;
  1593. }
  1594. static void*
  1595. jack_engine_freewheel (void *arg)
  1596. {
  1597. jack_engine_t* engine = (jack_engine_t *) arg;
  1598. VERBOSE (engine, "freewheel thread starting ...");
  1599. /* we should not be running SCHED_FIFO, so we don't
  1600. have to do anything about scheduling.
  1601. */
  1602. while (engine->freewheeling) {
  1603. jack_lock_graph (engine);
  1604. if (jack_engine_process (engine,
  1605. engine->control->buffer_size)) {
  1606. jack_error ("process cycle within freewheel failed");
  1607. jack_unlock_graph (engine);
  1608. break;
  1609. }
  1610. jack_engine_post_process (engine);
  1611. jack_unlock_graph (engine);
  1612. }
  1613. VERBOSE (engine, "freewheel came to an end, naturally");
  1614. return 0;
  1615. }
  1616. static int
  1617. jack_start_freewheeling (jack_engine_t* engine)
  1618. {
  1619. jack_event_t event;
  1620. if (engine->freewheeling) {
  1621. return 0;
  1622. }
  1623. if (engine->driver == NULL) {
  1624. jack_error ("cannot start freewheeling without a driver!");
  1625. return -1;
  1626. }
  1627. /* stop driver before telling anyone about it so
  1628. there are no more process() calls being handled.
  1629. */
  1630. if (engine->driver->stop (engine->driver)) {
  1631. jack_error ("could not stop driver for freewheeling");
  1632. return -1;
  1633. }
  1634. engine->freewheeling = 1;
  1635. event.type = StartFreewheel;
  1636. jack_deliver_event_to_all (engine, &event);
  1637. if (jack_client_create_thread (NULL, &engine->freewheel_thread, 0, FALSE,
  1638. jack_engine_freewheel, engine)) {
  1639. jack_error ("could not start create freewheel thread");
  1640. return -1;
  1641. }
  1642. return 0;
  1643. }
  1644. static int
  1645. jack_stop_freewheeling (jack_engine_t* engine)
  1646. {
  1647. jack_event_t event;
  1648. void *ftstatus;
  1649. if (!engine->freewheeling) {
  1650. return 0;
  1651. }
  1652. if (engine->driver == NULL) {
  1653. jack_error ("cannot start freewheeling without a driver!");
  1654. return -1;
  1655. }
  1656. if (!engine->freewheeling) {
  1657. VERBOSE (engine, "stop freewheel when not freewheeling");
  1658. return 0;
  1659. }
  1660. /* tell the freewheel thread to stop, and wait for it
  1661. to exit.
  1662. */
  1663. engine->freewheeling = 0;
  1664. VERBOSE (engine, "freewheeling stopped, waiting for thread");
  1665. pthread_join (engine->freewheel_thread, &ftstatus);
  1666. VERBOSE (engine, "freewheel thread has returned");
  1667. /* tell everyone we've stopped */
  1668. event.type = StopFreewheel;
  1669. jack_deliver_event_to_all (engine, &event);
  1670. /* restart the driver */
  1671. if (engine->driver->start (engine->driver)) {
  1672. jack_error ("could not restart driver after freewheeling");
  1673. return -1;
  1674. }
  1675. return 0;
  1676. }
  1677. static int
  1678. jack_run_one_cycle (jack_engine_t *engine, jack_nframes_t nframes,
  1679. float delayed_usecs)
  1680. {
  1681. jack_driver_t* driver = engine->driver;
  1682. int ret = -1;
  1683. static int consecutive_excessive_delays = 0;
  1684. #define WORK_SCALE 1.0f
  1685. if (engine->control->real_time &&
  1686. engine->spare_usecs &&
  1687. ((WORK_SCALE * engine->spare_usecs) <= delayed_usecs)) {
  1688. MESSAGE("delay of %.3f usecs exceeds estimated spare"
  1689. " time of %.3f; restart ...\n",
  1690. delayed_usecs, WORK_SCALE * engine->spare_usecs);
  1691. if (++consecutive_excessive_delays > 10) {
  1692. jack_error ("too many consecutive interrupt delays "
  1693. "... engine pausing");
  1694. return -1; /* will exit the thread loop */
  1695. }
  1696. jack_engine_delay (engine, delayed_usecs);
  1697. return 0;
  1698. } else {
  1699. consecutive_excessive_delays = 0;
  1700. }
  1701. DEBUG ("trying to acquire read lock");
  1702. if (jack_try_rdlock_graph (engine)) {
  1703. /* engine can't run. just throw away an entire cycle */
  1704. VERBOSE (engine, "null cycle");
  1705. driver->null_cycle (driver, nframes);
  1706. return 0;
  1707. }
  1708. if (engine->problems) {
  1709. /* engine can't run. just throw away an entire cycle */
  1710. VERBOSE (engine, "problem-driven null cycle");
  1711. jack_unlock_graph (engine);
  1712. driver->null_cycle (driver, nframes);
  1713. return 0;
  1714. }
  1715. if (!engine->freewheeling) {
  1716. DEBUG("waiting for driver read\n");
  1717. if (driver->read (driver, nframes)) {
  1718. goto unlock;
  1719. }
  1720. }
  1721. DEBUG("run process\n");
  1722. if (jack_engine_process (engine, nframes) == 0) {
  1723. if (!engine->freewheeling) {
  1724. if (driver->write (driver, nframes)) {
  1725. goto unlock;
  1726. }
  1727. }
  1728. } else {
  1729. JSList *node;
  1730. DEBUG ("engine process cycle failed");
  1731. /* we are already late, or something else went wrong,
  1732. so it can't hurt to check the existence of all
  1733. clients.
  1734. */
  1735. for (node = engine->clients; node;
  1736. node = jack_slist_next (node)) {
  1737. jack_client_internal_t *client =
  1738. (jack_client_internal_t *) node->data;
  1739. if (client->control->type == ClientExternal) {
  1740. if (kill (client->control->pid, 0)) {
  1741. VERBOSE(engine,
  1742. "client %s has died/exited",
  1743. client->control->name);
  1744. client->error++;
  1745. }
  1746. if(client->control->last_status != 0) {
  1747. VERBOSE(engine,
  1748. "client %s has nonzero process callback status (%d)\n",
  1749. client->control->name, client->control->last_status);
  1750. client->error++;
  1751. }
  1752. }
  1753. DEBUG ("client %s errors = %d", client->control->name,
  1754. client->error);
  1755. }
  1756. }
  1757. jack_engine_post_process (engine);
  1758. if (delayed_usecs > engine->control->max_delayed_usecs)
  1759. engine->control->max_delayed_usecs = delayed_usecs;
  1760. ret = 0;
  1761. unlock:
  1762. jack_unlock_graph (engine);
  1763. DEBUG("cycle finished, status = %d", ret);
  1764. return ret;
  1765. }
  1766. static void
  1767. jack_engine_driver_exit (jack_engine_t* engine)
  1768. {
  1769. jack_driver_t* driver = engine->driver;
  1770. VERBOSE (engine, "stopping driver");
  1771. driver->stop (driver);
  1772. VERBOSE (engine, "detaching driver");
  1773. driver->detach (driver, engine);
  1774. /* tell anyone waiting that the driver exited. */
  1775. kill (engine->wait_pid, SIGUSR2);
  1776. engine->driver = NULL;
  1777. }
  1778. static int
  1779. jack_run_cycle (jack_engine_t *engine, jack_nframes_t nframes,
  1780. float delayed_usecs)
  1781. {
  1782. jack_nframes_t left;
  1783. jack_nframes_t b_size = engine->control->buffer_size;
  1784. jack_frame_timer_t* timer = &engine->control->frame_timer;
  1785. int no_increment = 0;
  1786. if (engine->first_wakeup) {
  1787. /* the first wakeup */
  1788. timer->next_wakeup =
  1789. engine->driver->last_wait_ust +
  1790. engine->driver->period_usecs;
  1791. engine->first_wakeup = 0;
  1792. /* if we got an xrun/delayed wakeup on the first cycle,
  1793. reset the pending flag (we have no predicted wakeups
  1794. to use), but avoid incrementing the frame timer.
  1795. */
  1796. if (timer->reset_pending) {
  1797. timer->reset_pending = 0;
  1798. no_increment = 1;
  1799. }
  1800. }
  1801. if (timer->reset_pending) {
  1802. /* post xrun-handling */
  1803. /* don't bother to increment the frame counter, because we missed 1 or more
  1804. deadlines in the backend anyway.
  1805. */
  1806. timer->current_wakeup = engine->driver->last_wait_ust;
  1807. timer->next_wakeup = engine->driver->last_wait_ust +
  1808. engine->driver->period_usecs;
  1809. timer->reset_pending = 0;
  1810. } else {
  1811. /* normal condition */
  1812. if (!no_increment) {
  1813. jack_inc_frame_time (engine, nframes);
  1814. }
  1815. }
  1816. if (engine->verbose) {
  1817. if (nframes != b_size) {
  1818. VERBOSE (engine,
  1819. "late driver wakeup: nframes to process = %"
  1820. PRIu32 ".", nframes);
  1821. }
  1822. }
  1823. /* run as many cycles as it takes to consume nframes */
  1824. for (left = nframes; left >= b_size; left -= b_size) {
  1825. if (jack_run_one_cycle (engine, b_size, delayed_usecs)) {
  1826. jack_error ("cycle execution failure, exiting");
  1827. return EIO;
  1828. }
  1829. }
  1830. return 0;
  1831. }
  1832. void
  1833. jack_engine_delete (jack_engine_t *engine)
  1834. {
  1835. int i;
  1836. if (engine == NULL)
  1837. return;
  1838. VERBOSE (engine, "starting server engine shutdown");
  1839. engine->control->engine_ok = 0; /* tell clients we're going away */
  1840. /* this will wake the server thread and cause it to exit */
  1841. close (engine->cleanup_fifo[0]);
  1842. close (engine->cleanup_fifo[1]);
  1843. /* shutdown master socket to prevent new clients arriving */
  1844. shutdown (engine->fds[0], SHUT_RDWR);
  1845. // close (engine->fds[0]);
  1846. /* now really tell them we're going away */
  1847. for (i = 0; i < engine->pfd_max; ++i) {
  1848. shutdown (engine->pfd[i].fd, SHUT_RDWR);
  1849. }
  1850. if (engine->driver) {
  1851. jack_driver_t* driver = engine->driver;
  1852. VERBOSE (engine, "stopping driver");
  1853. driver->stop (driver);
  1854. // VERBOSE (engine, "detaching driver");
  1855. // driver->detach (driver, engine);
  1856. VERBOSE (engine, "unloading driver");
  1857. jack_driver_unload (driver);
  1858. engine->driver = NULL;
  1859. }
  1860. VERBOSE (engine, "freeing shared port segments");
  1861. for (i = 0; i < engine->control->n_port_types; ++i) {
  1862. jack_release_shm (&engine->port_segment[i]);
  1863. jack_destroy_shm (&engine->port_segment[i]);
  1864. }
  1865. /* stop the other engine threads */
  1866. VERBOSE (engine, "stopping server thread");
  1867. #if JACK_USE_MACH_THREADS
  1868. // MacOSX pthread_cancel still not implemented correctly in Darwin
  1869. mach_port_t machThread = pthread_mach_thread_np (engine->server_thread);
  1870. thread_terminate (machThread);
  1871. #else
  1872. pthread_cancel (engine->server_thread);
  1873. pthread_join (engine->server_thread, NULL);
  1874. #endif
  1875. #ifndef JACK_USE_MACH_THREADS
  1876. /* Cancel the watchdog thread and wait for it to terminate.
  1877. *
  1878. * The watchdog thread is not used on MacOSX since CoreAudio
  1879. * drivers already contain a similar mechanism.
  1880. */
  1881. if (engine->control->real_time && engine->watchdog_thread) {
  1882. VERBOSE (engine, "stopping watchdog thread");
  1883. pthread_cancel (engine->watchdog_thread);
  1884. pthread_join (engine->watchdog_thread, NULL);
  1885. }
  1886. #endif
  1887. VERBOSE (engine, "last xrun delay: %.3f usecs",
  1888. engine->control->xrun_delayed_usecs);
  1889. VERBOSE (engine, "max delay reported by backend: %.3f usecs",
  1890. engine->control->max_delayed_usecs);
  1891. /* free engine control shm segment */
  1892. engine->control = NULL;
  1893. VERBOSE (engine, "freeing engine shared memory");
  1894. jack_release_shm (&engine->control_shm);
  1895. jack_destroy_shm (&engine->control_shm);
  1896. VERBOSE (engine, "max usecs: %.3f, engine deleted", engine->max_usecs);
  1897. free (engine);
  1898. jack_messagebuffer_exit();
  1899. }
  1900. void
  1901. jack_port_clear_connections (jack_engine_t *engine,
  1902. jack_port_internal_t *port)
  1903. {
  1904. JSList *node, *next;
  1905. for (node = port->connections; node; ) {
  1906. next = jack_slist_next (node);
  1907. jack_port_disconnect_internal (
  1908. engine, ((jack_connection_internal_t *)
  1909. node->data)->source,
  1910. ((jack_connection_internal_t *)
  1911. node->data)->destination);
  1912. node = next;
  1913. }
  1914. jack_slist_free (port->connections);
  1915. port->connections = 0;
  1916. }
  1917. static void
  1918. jack_deliver_event_to_all (jack_engine_t *engine, jack_event_t *event)
  1919. {
  1920. JSList *node;
  1921. jack_rdlock_graph (engine);
  1922. for (node = engine->clients; node; node = jack_slist_next (node)) {
  1923. jack_deliver_event (engine,
  1924. (jack_client_internal_t *) node->data,
  1925. event);
  1926. }
  1927. jack_unlock_graph (engine);
  1928. }
  1929. static void
  1930. 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)
  1931. {
  1932. JSList *node;
  1933. jack_event_t event;
  1934. event.type = (connected ? PortConnected : PortDisconnected);
  1935. event.x.self_id = a;
  1936. event.y.other_id = b;
  1937. /* GRAPH MUST BE LOCKED : see callers of jack_send_connection_notification()
  1938. */
  1939. jack_client_internal_t* src_client = jack_client_internal_by_id (engine, src);
  1940. jack_client_internal_t* dst_client = jack_client_internal_by_id (engine, dst);
  1941. for (node = engine->clients; node; node = jack_slist_next (node)) {
  1942. jack_client_internal_t* client = (jack_client_internal_t*) node->data;
  1943. if (src_client != client && dst_client != client && client->control->port_connect_cbset != FALSE) {
  1944. /* one of the ports belong to this client or it has a port connect callback */
  1945. jack_deliver_event (engine, client, &event);
  1946. }
  1947. }
  1948. }
  1949. static int
  1950. jack_deliver_event (jack_engine_t *engine, jack_client_internal_t *client,
  1951. jack_event_t *event)
  1952. {
  1953. char status;
  1954. /* caller must hold the graph lock */
  1955. DEBUG ("delivering event (type %d)", event->type);
  1956. /* we are not RT-constrained here, so use kill(2) to beef up
  1957. our check on a client's continued well-being
  1958. */
  1959. if (client->control->dead || client->error >= JACK_ERROR_WITH_SOCKETS
  1960. || (client->control->type == ClientExternal && kill (client->control->pid, 0))) {
  1961. DEBUG ("client %s is dead - no event sent",
  1962. client->control->name);
  1963. return 0;
  1964. }
  1965. DEBUG ("client %s is still alive", client->control->name);
  1966. if (jack_client_is_internal (client)) {
  1967. switch (event->type) {
  1968. case PortConnected:
  1969. case PortDisconnected:
  1970. jack_client_handle_port_connection
  1971. (client->private_client, event);
  1972. break;
  1973. case BufferSizeChange:
  1974. jack_client_invalidate_port_buffers
  1975. (client->private_client);
  1976. if (client->control->bufsize_cbset) {
  1977. client->private_client->bufsize
  1978. (event->x.n,
  1979. client->private_client->bufsize_arg);
  1980. }
  1981. break;
  1982. case SampleRateChange:
  1983. if (client->control->srate_cbset) {
  1984. client->private_client->srate
  1985. (event->x.n,
  1986. client->private_client->srate_arg);
  1987. }
  1988. break;
  1989. case GraphReordered:
  1990. if (client->control->graph_order_cbset) {
  1991. client->private_client->graph_order
  1992. (client->private_client->graph_order_arg);
  1993. }
  1994. break;
  1995. case XRun:
  1996. if (client->control->xrun_cbset) {
  1997. client->private_client->xrun
  1998. (client->private_client->xrun_arg);
  1999. }
  2000. break;
  2001. default:
  2002. /* internal clients don't need to know */
  2003. break;
  2004. }
  2005. } else {
  2006. if (client->control->active) {
  2007. /* there's a thread waiting for events, so
  2008. * it's worth telling the client */
  2009. DEBUG ("engine writing on event fd");
  2010. if (write (client->event_fd, event, sizeof (*event))
  2011. != sizeof (*event)) {
  2012. jack_error ("cannot send event to client [%s]"
  2013. " (%s)", client->control->name,
  2014. strerror (errno));
  2015. client->error += JACK_ERROR_WITH_SOCKETS;
  2016. jack_lock_problems (engine);
  2017. engine->problems++;
  2018. jack_unlock_problems (engine);
  2019. }
  2020. if (client->error) {
  2021. status = 1;
  2022. } else {
  2023. // then we check whether there really is an error.... :)
  2024. struct pollfd pfd[1];
  2025. pfd[0].fd = client->event_fd;
  2026. pfd[0].events = POLLERR|POLLIN|POLLHUP|POLLNVAL;
  2027. jack_time_t poll_timeout = JACKD_CLIENT_EVENT_TIMEOUT;
  2028. int poll_ret;
  2029. jack_time_t then = jack_get_microseconds ();
  2030. jack_time_t now;
  2031. #ifdef __linux
  2032. again:
  2033. #endif
  2034. VERBOSE(engine,"client event poll on %d for %s starts at %lld",
  2035. client->event_fd, client->control->name, then);
  2036. if ((poll_ret = poll (pfd, 1, poll_timeout)) < 0) {
  2037. DEBUG ("client event poll not ok! (-1) poll returned an error");
  2038. jack_error ("poll on subgraph processing failed (%s)", strerror (errno));
  2039. status = -1;
  2040. } else {
  2041. DEBUG ("\n\n\n\n\n back from client event poll, revents = 0x%x\n\n\n", pfd[0].revents);
  2042. now = jack_get_microseconds();
  2043. VERBOSE(engine,"back from client event poll after %lld usecs", now - then);
  2044. if (pfd[0].revents & ~POLLIN) {
  2045. /* some kind of OOB socket event */
  2046. DEBUG ("client event poll not ok! (-2), revents = %d\n", pfd[0].revents);
  2047. jack_error ("subgraph starting at %s lost client", client->control->name);
  2048. status = -2;
  2049. } else if (pfd[0].revents & POLLIN) {
  2050. /* client responded normally */
  2051. DEBUG ("client event poll ok!");
  2052. status = 0;
  2053. } else if (poll_ret == 0) {
  2054. /* no events, no errors, we woke up because poll()
  2055. decided that time was up ...
  2056. */
  2057. #ifdef __linux
  2058. if (linux_poll_bug_encountered (engine, then, &poll_timeout)) {
  2059. goto again;
  2060. }
  2061. if (poll_timeout < 200) {
  2062. VERBOSE (engine, "FALSE WAKEUP skipped, remaining = %lld usec", poll_timeout);
  2063. status = 0;
  2064. } else {
  2065. #endif
  2066. DEBUG ("client event poll not ok! (1 = poll timed out, revents = 0x%04x, poll_ret = %d)", pfd[0].revents, poll_ret);
  2067. VERBOSE (engine,"client %s did not respond to event type %d in time"
  2068. "(fd=%d, revents = 0x%04x, timeout was %lld)",
  2069. client->control->name, event->type,
  2070. client->event_fd,
  2071. pfd[0].revents,
  2072. poll_timeout);
  2073. status = 1;
  2074. #ifdef __linux
  2075. }
  2076. #endif
  2077. }
  2078. }
  2079. }
  2080. if (status == 0) {
  2081. if (read (client->event_fd, &status, sizeof (status)) != sizeof (status)) {
  2082. jack_error ("cannot read event response from "
  2083. "client [%s] (%s)",
  2084. client->control->name,
  2085. strerror (errno));
  2086. client->error += JACK_ERROR_WITH_SOCKETS;
  2087. jack_lock_problems (engine);
  2088. engine->problems++;
  2089. jack_unlock_problems (engine);
  2090. }
  2091. } else {
  2092. jack_error ("bad status (%d) for client %s "
  2093. "handling event (type = %d)",
  2094. status,
  2095. client->control->name,
  2096. event->type);
  2097. client->error += JACK_ERROR_WITH_SOCKETS;
  2098. jack_lock_problems (engine);
  2099. engine->problems++;
  2100. jack_unlock_problems (engine);
  2101. }
  2102. }
  2103. }
  2104. DEBUG ("event delivered");
  2105. return 0;
  2106. }
  2107. int
  2108. jack_rechain_graph (jack_engine_t *engine)
  2109. {
  2110. JSList *node, *next;
  2111. unsigned long n;
  2112. int err = 0;
  2113. jack_client_internal_t *client, *subgraph_client, *next_client;
  2114. jack_event_t event;
  2115. int upstream_is_jackd;
  2116. jack_clear_fifos (engine);
  2117. subgraph_client = 0;
  2118. VERBOSE(engine, "++ jack_rechain_graph():");
  2119. event.type = GraphReordered;
  2120. for (n = 0, node = engine->clients, next = NULL; node; node = next) {
  2121. next = jack_slist_next (node);
  2122. VERBOSE(engine, "+++ client is now %s active ? %d",
  2123. ((jack_client_internal_t *) node->data)->control->name,
  2124. ((jack_client_internal_t *) node->data)->control->active);
  2125. if (((jack_client_internal_t *) node->data)->control->active) {
  2126. client = (jack_client_internal_t *) node->data;
  2127. /* find the next active client. its ok for
  2128. * this to be NULL */
  2129. while (next) {
  2130. if (((jack_client_internal_t *)
  2131. next->data)->control->active) {
  2132. break;
  2133. }
  2134. next = jack_slist_next (next);
  2135. };
  2136. if (next == NULL) {
  2137. next_client = NULL;
  2138. } else {
  2139. next_client = (jack_client_internal_t *)
  2140. next->data;
  2141. }
  2142. client->execution_order = n;
  2143. client->next_client = next_client;
  2144. if (jack_client_is_internal (client)) {
  2145. /* break the chain for the current
  2146. * subgraph. the server will wait for
  2147. * chain on the nth FIFO, and will
  2148. * then execute this internal
  2149. * client. */
  2150. if (subgraph_client) {
  2151. subgraph_client->subgraph_wait_fd =
  2152. jack_get_fifo_fd (engine, n);
  2153. VERBOSE (engine, "client %s: wait_fd="
  2154. "%d, execution_order="
  2155. "%lu.",
  2156. subgraph_client->
  2157. control->name,
  2158. subgraph_client->
  2159. subgraph_wait_fd, n);
  2160. n++;
  2161. }
  2162. VERBOSE (engine, "client %s: internal "
  2163. "client, execution_order="
  2164. "%lu.",
  2165. client->control->name, n);
  2166. /* this does the right thing for
  2167. * internal clients too
  2168. */
  2169. jack_deliver_event (engine, client, &event);
  2170. subgraph_client = 0;
  2171. } else {
  2172. if (subgraph_client == NULL) {
  2173. /* start a new subgraph. the
  2174. * engine will start the chain
  2175. * by writing to the nth
  2176. * FIFO.
  2177. */
  2178. subgraph_client = client;
  2179. subgraph_client->subgraph_start_fd =
  2180. jack_get_fifo_fd (engine, n);
  2181. VERBOSE (engine, "client %s: "
  2182. "start_fd=%d, execution"
  2183. "_order=%lu.",
  2184. subgraph_client->
  2185. control->name,
  2186. subgraph_client->
  2187. subgraph_start_fd, n);
  2188. /* this external client after
  2189. this will have jackd as its
  2190. upstream connection.
  2191. */
  2192. upstream_is_jackd = 1;
  2193. }
  2194. else {
  2195. VERBOSE (engine, "client %s: in"
  2196. " subgraph after %s, "
  2197. "execution_order="
  2198. "%lu.",
  2199. client->control->name,
  2200. subgraph_client->
  2201. control->name, n);
  2202. subgraph_client->subgraph_wait_fd = -1;
  2203. /* this external client after
  2204. this will have another
  2205. client as its upstream
  2206. connection.
  2207. */
  2208. upstream_is_jackd = 0;
  2209. }
  2210. /* make sure fifo for 'n + 1' exists
  2211. * before issuing client reorder
  2212. */
  2213. (void) jack_get_fifo_fd(
  2214. engine, client->execution_order + 1);
  2215. event.x.n = client->execution_order;
  2216. event.y.n = upstream_is_jackd;
  2217. jack_deliver_event (engine, client, &event);
  2218. n++;
  2219. }
  2220. }
  2221. }
  2222. if (subgraph_client) {
  2223. subgraph_client->subgraph_wait_fd =
  2224. jack_get_fifo_fd (engine, n);
  2225. VERBOSE (engine, "client %s: wait_fd=%d, "
  2226. "execution_order=%lu (last client).",
  2227. subgraph_client->control->name,
  2228. subgraph_client->subgraph_wait_fd, n);
  2229. }
  2230. VERBOSE (engine, "-- jack_rechain_graph()");
  2231. return err;
  2232. }
  2233. static jack_nframes_t
  2234. jack_get_port_total_latency (jack_engine_t *engine,
  2235. jack_port_internal_t *port, int hop_count,
  2236. int toward_port)
  2237. {
  2238. JSList *node;
  2239. jack_nframes_t latency;
  2240. jack_nframes_t max_latency = 0;
  2241. #ifdef DEBUG_TOTAL_LATENCY_COMPUTATION
  2242. char prefix[32];
  2243. int i;
  2244. for (i = 0; i < hop_count; ++i) {
  2245. prefix[i] = '\t';
  2246. }
  2247. prefix[i] = '\0';
  2248. #endif
  2249. /* call tree must hold engine->client_lock. */
  2250. latency = port->shared->latency;
  2251. /* we don't prevent cyclic graphs, so we have to do something
  2252. to bottom out in the event that they are created.
  2253. */
  2254. if (hop_count > 8) {
  2255. return latency;
  2256. }
  2257. #ifdef DEBUG_TOTAL_LATENCY_COMPUTATION
  2258. jack_info ("%sFor port %s (%s)", prefix, port->shared->name, (toward_port ? "toward" : "away"));
  2259. #endif
  2260. for (node = port->connections; node; node = jack_slist_next (node)) {
  2261. jack_nframes_t this_latency;
  2262. jack_connection_internal_t *connection;
  2263. connection = (jack_connection_internal_t *) node->data;
  2264. if ((toward_port &&
  2265. (connection->source->shared == port->shared)) ||
  2266. (!toward_port &&
  2267. (connection->destination->shared == port->shared))) {
  2268. #ifdef DEBUG_TOTAL_LATENCY_COMPUTATION
  2269. jack_info ("%s\tskip connection %s->%s",
  2270. prefix,
  2271. connection->source->shared->name,
  2272. connection->destination->shared->name);
  2273. #endif
  2274. continue;
  2275. }
  2276. #ifdef DEBUG_TOTAL_LATENCY_COMPUTATION
  2277. jack_info ("%s\tconnection %s->%s ... ",
  2278. prefix,
  2279. connection->source->shared->name,
  2280. connection->destination->shared->name);
  2281. #endif
  2282. /* if we're a destination in the connection, recurse
  2283. on the source to get its total latency
  2284. */
  2285. if (connection->destination == port) {
  2286. if (connection->source->shared->flags
  2287. & JackPortIsTerminal) {
  2288. this_latency = connection->source->
  2289. shared->latency;
  2290. } else {
  2291. this_latency =
  2292. jack_get_port_total_latency (
  2293. engine, connection->source,
  2294. hop_count + 1,
  2295. toward_port);
  2296. }
  2297. } else {
  2298. /* "port" is the source, so get the latency of
  2299. * the destination */
  2300. if (connection->destination->shared->flags
  2301. & JackPortIsTerminal) {
  2302. this_latency = connection->destination->
  2303. shared->latency;
  2304. } else {
  2305. this_latency =
  2306. jack_get_port_total_latency (
  2307. engine,
  2308. connection->destination,
  2309. hop_count + 1,
  2310. toward_port);
  2311. }
  2312. }
  2313. if (this_latency > max_latency) {
  2314. max_latency = this_latency;
  2315. }
  2316. }
  2317. #ifdef DEBUG_TOTAL_LATENCY_COMPUTATION
  2318. jack_info ("%s\treturn %lu + %lu = %lu", prefix, latency, max_latency, latency + max_latency);
  2319. #endif
  2320. return latency + max_latency;
  2321. }
  2322. static void
  2323. jack_compute_port_total_latency (jack_engine_t* engine, jack_port_shared_t* port)
  2324. {
  2325. if (port->in_use) {
  2326. port->total_latency =
  2327. jack_get_port_total_latency (
  2328. engine, &engine->internal_ports[port->id],
  2329. 0, !(port->flags & JackPortIsOutput));
  2330. }
  2331. }
  2332. static void
  2333. jack_compute_all_port_total_latencies (jack_engine_t *engine)
  2334. {
  2335. jack_port_shared_t *shared = engine->control->ports;
  2336. unsigned int i;
  2337. int toward_port;
  2338. for (i = 0; i < engine->control->port_max; i++) {
  2339. if (shared[i].in_use) {
  2340. if (shared[i].flags & JackPortIsOutput) {
  2341. toward_port = FALSE;
  2342. } else {
  2343. toward_port = TRUE;
  2344. }
  2345. shared[i].total_latency =
  2346. jack_get_port_total_latency (
  2347. engine, &engine->internal_ports[i],
  2348. 0, toward_port);
  2349. }
  2350. }
  2351. }
  2352. /* How the sort works:
  2353. *
  2354. * Each client has a "sortfeeds" list of clients indicating which clients
  2355. * it should be considered as feeding for the purposes of sorting the
  2356. * graph. This list differs from the clients it /actually/ feeds in the
  2357. * following ways:
  2358. *
  2359. * 1. Connections from a client to itself are disregarded
  2360. *
  2361. * 2. Connections to a driver client are disregarded
  2362. *
  2363. * 3. If a connection from A to B is a feedback connection (ie there was
  2364. * already a path from B to A when the connection was made) then instead
  2365. * of B appearing on A's sortfeeds list, A will appear on B's sortfeeds
  2366. * list.
  2367. *
  2368. * If client A is on client B's sortfeeds list, client A must come after
  2369. * client B in the execution order. The above 3 rules ensure that the
  2370. * sortfeeds relation is always acyclic so that all ordering constraints
  2371. * can actually be met.
  2372. *
  2373. * Each client also has a "truefeeds" list which is the same as sortfeeds
  2374. * except that feedback connections appear normally instead of reversed.
  2375. * This is used to detect whether the graph has become acyclic.
  2376. *
  2377. */
  2378. void
  2379. jack_sort_graph (jack_engine_t *engine)
  2380. {
  2381. /* called, obviously, must hold engine->client_lock */
  2382. VERBOSE (engine, "++ jack_sort_graph");
  2383. engine->clients = jack_slist_sort (engine->clients,
  2384. (JCompareFunc) jack_client_sort);
  2385. jack_compute_all_port_total_latencies (engine);
  2386. jack_rechain_graph (engine);
  2387. VERBOSE (engine, "-- jack_sort_graph");
  2388. }
  2389. static int
  2390. jack_client_sort (jack_client_internal_t *a, jack_client_internal_t *b)
  2391. {
  2392. /* drivers are forced to the front, ie considered as sources
  2393. rather than sinks for purposes of the sort */
  2394. if (jack_client_feeds_transitive (a, b) ||
  2395. (a->control->type == ClientDriver &&
  2396. b->control->type != ClientDriver)) {
  2397. return -1;
  2398. } else if (jack_client_feeds_transitive (b, a) ||
  2399. (b->control->type == ClientDriver &&
  2400. a->control->type != ClientDriver)) {
  2401. return 1;
  2402. } else {
  2403. return 0;
  2404. }
  2405. }
  2406. /* transitive closure of the relation expressed by the sortfeeds lists. */
  2407. static int
  2408. jack_client_feeds_transitive (jack_client_internal_t *source,
  2409. jack_client_internal_t *dest )
  2410. {
  2411. jack_client_internal_t *med;
  2412. JSList *node;
  2413. if (jack_slist_find (source->sortfeeds, dest)) {
  2414. return 1;
  2415. }
  2416. for (node = source->sortfeeds; node; node = jack_slist_next (node)) {
  2417. med = (jack_client_internal_t *) node->data;
  2418. if (jack_client_feeds_transitive (med, dest)) {
  2419. return 1;
  2420. }
  2421. }
  2422. return 0;
  2423. }
  2424. /**
  2425. * Checks whether the graph has become acyclic and if so modifies client
  2426. * sortfeeds lists to turn leftover feedback connections into normal ones.
  2427. * This lowers latency, but at the expense of some data corruption.
  2428. */
  2429. static void
  2430. jack_check_acyclic (jack_engine_t *engine)
  2431. {
  2432. JSList *srcnode, *dstnode, *portnode, *connnode;
  2433. jack_client_internal_t *src, *dst;
  2434. jack_port_internal_t *port;
  2435. jack_connection_internal_t *conn;
  2436. int stuck;
  2437. int unsortedclients = 0;
  2438. VERBOSE (engine, "checking for graph become acyclic");
  2439. for (srcnode = engine->clients; srcnode;
  2440. srcnode = jack_slist_next (srcnode)) {
  2441. src = (jack_client_internal_t *) srcnode->data;
  2442. src->tfedcount = src->fedcount;
  2443. unsortedclients++;
  2444. }
  2445. stuck = FALSE;
  2446. /* find out whether a normal sort would have been possible */
  2447. while (unsortedclients && !stuck) {
  2448. stuck = TRUE;
  2449. for (srcnode = engine->clients; srcnode;
  2450. srcnode = jack_slist_next (srcnode)) {
  2451. src = (jack_client_internal_t *) srcnode->data;
  2452. if (!src->tfedcount) {
  2453. stuck = FALSE;
  2454. unsortedclients--;
  2455. src->tfedcount = -1;
  2456. for (dstnode = src->truefeeds; dstnode;
  2457. dstnode = jack_slist_next (dstnode)) {
  2458. dst = (jack_client_internal_t *)
  2459. dstnode->data;
  2460. dst->tfedcount--;
  2461. }
  2462. }
  2463. }
  2464. }
  2465. if (stuck) {
  2466. VERBOSE (engine, "graph is still cyclic" );
  2467. } else {
  2468. VERBOSE (engine, "graph has become acyclic");
  2469. /* turn feedback connections around in sortfeeds */
  2470. for (srcnode = engine->clients; srcnode;
  2471. srcnode = jack_slist_next (srcnode)) {
  2472. src = (jack_client_internal_t *) srcnode->data;
  2473. for (portnode = src->ports; portnode;
  2474. portnode = jack_slist_next (portnode)) {
  2475. port = (jack_port_internal_t *) portnode->data;
  2476. for (connnode = port->connections; connnode;
  2477. connnode = jack_slist_next (connnode)) {
  2478. conn = (jack_connection_internal_t*)
  2479. connnode->data;
  2480. if (conn->dir == -1 )
  2481. /*&&
  2482. conn->srcclient == src) */{
  2483. VERBOSE (engine,
  2484. "reversing connection from "
  2485. "%s to %s",
  2486. conn->srcclient->control->name,
  2487. conn->dstclient->control->name);
  2488. conn->dir = 1;
  2489. conn->dstclient->sortfeeds =
  2490. jack_slist_remove
  2491. (conn->dstclient->sortfeeds,
  2492. conn->srcclient);
  2493. conn->srcclient->sortfeeds =
  2494. jack_slist_prepend
  2495. (conn->srcclient->sortfeeds,
  2496. conn->dstclient );
  2497. }
  2498. }
  2499. }
  2500. }
  2501. engine->feedbackcount = 0;
  2502. }
  2503. }
  2504. /**
  2505. * Dumps current engine configuration.
  2506. */
  2507. void jack_dump_configuration(jack_engine_t *engine, int take_lock)
  2508. {
  2509. JSList *clientnode, *portnode, *connectionnode;
  2510. jack_client_internal_t *client;
  2511. jack_client_control_t *ctl;
  2512. jack_port_internal_t *port;
  2513. jack_connection_internal_t* connection;
  2514. int n, m, o;
  2515. jack_info ("engine.c: <-- dump begins -->");
  2516. if (take_lock) {
  2517. jack_rdlock_graph (engine);
  2518. }
  2519. for (n = 0, clientnode = engine->clients; clientnode;
  2520. clientnode = jack_slist_next (clientnode)) {
  2521. client = (jack_client_internal_t *) clientnode->data;
  2522. ctl = client->control;
  2523. jack_info ("client #%d: %s (type: %d, process? %s,"
  2524. " start=%d wait=%d",
  2525. ++n,
  2526. ctl->name,
  2527. ctl->type,
  2528. ctl->process_cbset ? "yes" : "no",
  2529. client->subgraph_start_fd,
  2530. client->subgraph_wait_fd);
  2531. for(m = 0, portnode = client->ports; portnode;
  2532. portnode = jack_slist_next (portnode)) {
  2533. port = (jack_port_internal_t *) portnode->data;
  2534. jack_info("\t port #%d: %s", ++m,
  2535. port->shared->name);
  2536. for(o = 0, connectionnode = port->connections;
  2537. connectionnode;
  2538. connectionnode =
  2539. jack_slist_next (connectionnode)) {
  2540. connection = (jack_connection_internal_t *)
  2541. connectionnode->data;
  2542. jack_info("\t\t connection #%d: %s %s",
  2543. ++o,
  2544. (port->shared->flags
  2545. & JackPortIsInput)? "<-": "->",
  2546. (port->shared->flags & JackPortIsInput)?
  2547. connection->source->shared->name:
  2548. connection->destination->shared->name);
  2549. }
  2550. }
  2551. }
  2552. if (take_lock) {
  2553. jack_unlock_graph (engine);
  2554. }
  2555. jack_info("engine.c: <-- dump ends -->");
  2556. }
  2557. static int
  2558. jack_port_do_connect (jack_engine_t *engine,
  2559. const char *source_port,
  2560. const char *destination_port)
  2561. {
  2562. jack_connection_internal_t *connection;
  2563. jack_port_internal_t *srcport, *dstport;
  2564. jack_port_id_t src_id, dst_id;
  2565. jack_client_internal_t *srcclient, *dstclient;
  2566. JSList *it;
  2567. if ((srcport = jack_get_port_by_name (engine, source_port)) == NULL) {
  2568. jack_error ("unknown source port in attempted connection [%s]",
  2569. source_port);
  2570. return -1;
  2571. }
  2572. if ((dstport = jack_get_port_by_name (engine, destination_port))
  2573. == NULL) {
  2574. jack_error ("unknown destination port in attempted connection"
  2575. " [%s]", destination_port);
  2576. return -1;
  2577. }
  2578. if ((dstport->shared->flags & JackPortIsInput) == 0) {
  2579. jack_error ("destination port in attempted connection of"
  2580. " %s and %s is not an input port",
  2581. source_port, destination_port);
  2582. return -1;
  2583. }
  2584. if ((srcport->shared->flags & JackPortIsOutput) == 0) {
  2585. jack_error ("source port in attempted connection of %s and"
  2586. " %s is not an output port",
  2587. source_port, destination_port);
  2588. return -1;
  2589. }
  2590. if (srcport->shared->ptype_id != dstport->shared->ptype_id) {
  2591. jack_error ("ports used in attemped connection are not of "
  2592. "the same data type");
  2593. return -1;
  2594. }
  2595. if ((srcclient = jack_client_internal_by_id (engine,
  2596. srcport->shared->client_id))
  2597. == 0) {
  2598. jack_error ("unknown client set as owner of port - "
  2599. "cannot connect");
  2600. return -1;
  2601. }
  2602. if (!srcclient->control->active) {
  2603. jack_error ("cannot connect ports owned by inactive clients;"
  2604. " \"%s\" is not active", srcclient->control->name);
  2605. return -1;
  2606. }
  2607. if ((dstclient = jack_client_internal_by_id (engine,
  2608. dstport->shared->client_id))
  2609. == 0) {
  2610. jack_error ("unknown client set as owner of port - cannot "
  2611. "connect");
  2612. return -1;
  2613. }
  2614. if (!dstclient->control->active) {
  2615. jack_error ("cannot connect ports owned by inactive clients;"
  2616. " \"%s\" is not active", dstclient->control->name);
  2617. return -1;
  2618. }
  2619. for (it = srcport->connections; it; it = it->next) {
  2620. if (((jack_connection_internal_t *)it->data)->destination
  2621. == dstport) {
  2622. return EEXIST;
  2623. }
  2624. }
  2625. connection = (jack_connection_internal_t *)
  2626. malloc (sizeof (jack_connection_internal_t));
  2627. connection->source = srcport;
  2628. connection->destination = dstport;
  2629. connection->srcclient = srcclient;
  2630. connection->dstclient = dstclient;
  2631. src_id = srcport->shared->id;
  2632. dst_id = dstport->shared->id;
  2633. jack_lock_graph (engine);
  2634. if (dstport->connections && !dstport->shared->has_mixdown) {
  2635. jack_port_type_info_t *port_type =
  2636. jack_port_type_info (engine, dstport);
  2637. jack_error ("cannot make multiple connections to a port of"
  2638. " type [%s]", port_type->type_name);
  2639. free (connection);
  2640. jack_unlock_graph (engine);
  2641. return -1;
  2642. } else {
  2643. if (dstclient->control->type == ClientDriver)
  2644. {
  2645. /* Ignore output connections to drivers for purposes
  2646. of sorting. Drivers are executed first in the sort
  2647. order anyway, and we don't want to treat graphs
  2648. such as driver -> client -> driver as containing
  2649. feedback */
  2650. VERBOSE (engine,
  2651. "connect %s and %s (output)",
  2652. srcport->shared->name,
  2653. dstport->shared->name);
  2654. connection->dir = 1;
  2655. }
  2656. else if (srcclient != dstclient) {
  2657. srcclient->truefeeds = jack_slist_prepend
  2658. (srcclient->truefeeds, dstclient);
  2659. dstclient->fedcount++;
  2660. if (jack_client_feeds_transitive (dstclient,
  2661. srcclient ) ||
  2662. (dstclient->control->type == ClientDriver &&
  2663. srcclient->control->type != ClientDriver)) {
  2664. /* dest is running before source so
  2665. this is a feedback connection */
  2666. VERBOSE (engine,
  2667. "connect %s and %s (feedback)",
  2668. srcport->shared->name,
  2669. dstport->shared->name);
  2670. dstclient->sortfeeds = jack_slist_prepend
  2671. (dstclient->sortfeeds, srcclient);
  2672. connection->dir = -1;
  2673. engine->feedbackcount++;
  2674. VERBOSE (engine,
  2675. "feedback count up to %d",
  2676. engine->feedbackcount);
  2677. } else {
  2678. /* this is not a feedback connection */
  2679. VERBOSE (engine,
  2680. "connect %s and %s (forward)",
  2681. srcport->shared->name,
  2682. dstport->shared->name);
  2683. srcclient->sortfeeds = jack_slist_prepend
  2684. (srcclient->sortfeeds, dstclient);
  2685. connection->dir = 1;
  2686. }
  2687. }
  2688. else
  2689. {
  2690. /* this is a connection to self */
  2691. VERBOSE (engine,
  2692. "connect %s and %s (self)",
  2693. srcport->shared->name,
  2694. dstport->shared->name);
  2695. connection->dir = 0;
  2696. }
  2697. dstport->connections =
  2698. jack_slist_prepend (dstport->connections, connection);
  2699. srcport->connections =
  2700. jack_slist_prepend (srcport->connections, connection);
  2701. DEBUG ("actually sorted the graph...");
  2702. jack_send_connection_notification (engine,
  2703. srcport->shared->client_id,
  2704. src_id, dst_id, TRUE);
  2705. jack_send_connection_notification (engine,
  2706. dstport->shared->client_id,
  2707. dst_id, src_id, TRUE);
  2708. /* send a port connection notification just once to everyone who cares excluding clients involved in the connection */
  2709. jack_notify_all_port_interested_clients (engine, srcport->shared->client_id, dstport->shared->client_id, src_id, dst_id, 1);
  2710. jack_sort_graph (engine);
  2711. }
  2712. jack_unlock_graph (engine);
  2713. return 0;
  2714. }
  2715. int
  2716. jack_port_disconnect_internal (jack_engine_t *engine,
  2717. jack_port_internal_t *srcport,
  2718. jack_port_internal_t *dstport )
  2719. {
  2720. JSList *node;
  2721. jack_connection_internal_t *connect;
  2722. int ret = -1;
  2723. jack_port_id_t src_id, dst_id;
  2724. int check_acyclic = engine->feedbackcount;
  2725. /* call tree **** MUST HOLD **** engine->client_lock. */
  2726. for (node = srcport->connections; node;
  2727. node = jack_slist_next (node)) {
  2728. connect = (jack_connection_internal_t *) node->data;
  2729. if (connect->source == srcport &&
  2730. connect->destination == dstport) {
  2731. VERBOSE (engine, "DIS-connect %s and %s",
  2732. srcport->shared->name,
  2733. dstport->shared->name);
  2734. srcport->connections =
  2735. jack_slist_remove (srcport->connections,
  2736. connect);
  2737. dstport->connections =
  2738. jack_slist_remove (dstport->connections,
  2739. connect);
  2740. src_id = srcport->shared->id;
  2741. dst_id = dstport->shared->id;
  2742. /* this is a bit harsh, but it basically says
  2743. that if we actually do a disconnect, and
  2744. its the last one, then make sure that any
  2745. input monitoring is turned off on the
  2746. srcport. this isn't ideal for all
  2747. situations, but it works better for most of
  2748. them.
  2749. */
  2750. if (srcport->connections == NULL) {
  2751. srcport->shared->monitor_requests = 0;
  2752. }
  2753. jack_send_connection_notification (
  2754. engine, srcport->shared->client_id, src_id,
  2755. dst_id, FALSE);
  2756. jack_send_connection_notification (
  2757. engine, dstport->shared->client_id, dst_id,
  2758. src_id, FALSE);
  2759. /* send a port connection notification just once to everyone who cares excluding clients involved in the connection */
  2760. jack_notify_all_port_interested_clients (engine, srcport->shared->client_id, dstport->shared->client_id, src_id, dst_id, 0);
  2761. if (connect->dir) {
  2762. jack_client_internal_t *src;
  2763. jack_client_internal_t *dst;
  2764. src = jack_client_internal_by_id
  2765. (engine, srcport->shared->client_id);
  2766. dst = jack_client_internal_by_id
  2767. (engine, dstport->shared->client_id);
  2768. src->truefeeds = jack_slist_remove
  2769. (src->truefeeds, dst);
  2770. dst->fedcount--;
  2771. if (connect->dir == 1) {
  2772. /* normal connection: remove dest from
  2773. source's sortfeeds list */
  2774. src->sortfeeds = jack_slist_remove
  2775. (src->sortfeeds, dst);
  2776. } else {
  2777. /* feedback connection: remove source
  2778. from dest's sortfeeds list */
  2779. dst->sortfeeds = jack_slist_remove
  2780. (dst->sortfeeds, src);
  2781. engine->feedbackcount--;
  2782. VERBOSE (engine,
  2783. "feedback count down to %d",
  2784. engine->feedbackcount);
  2785. }
  2786. } /* else self-connection: do nothing */
  2787. free (connect);
  2788. ret = 0;
  2789. break;
  2790. }
  2791. }
  2792. if (check_acyclic) {
  2793. jack_check_acyclic (engine);
  2794. }
  2795. jack_sort_graph (engine);
  2796. return ret;
  2797. }
  2798. static int
  2799. jack_port_do_disconnect_all (jack_engine_t *engine,
  2800. jack_port_id_t port_id)
  2801. {
  2802. if (port_id >= engine->control->port_max) {
  2803. jack_error ("illegal port ID in attempted disconnection [%"
  2804. PRIu32 "]", port_id);
  2805. return -1;
  2806. }
  2807. VERBOSE (engine, "clear connections for %s",
  2808. engine->internal_ports[port_id].shared->name);
  2809. jack_lock_graph (engine);
  2810. jack_port_clear_connections (engine, &engine->internal_ports[port_id]);
  2811. jack_sort_graph (engine);
  2812. jack_unlock_graph (engine);
  2813. return 0;
  2814. }
  2815. static int
  2816. jack_port_do_disconnect (jack_engine_t *engine,
  2817. const char *source_port,
  2818. const char *destination_port)
  2819. {
  2820. jack_port_internal_t *srcport, *dstport;
  2821. int ret = -1;
  2822. if ((srcport = jack_get_port_by_name (engine, source_port)) == NULL) {
  2823. jack_error ("unknown source port in attempted disconnection"
  2824. " [%s]", source_port);
  2825. return -1;
  2826. }
  2827. if ((dstport = jack_get_port_by_name (engine, destination_port))
  2828. == NULL) {
  2829. jack_error ("unknown destination port in attempted"
  2830. " disconnection [%s]", destination_port);
  2831. return -1;
  2832. }
  2833. jack_lock_graph (engine);
  2834. ret = jack_port_disconnect_internal (engine, srcport, dstport);
  2835. jack_unlock_graph (engine);
  2836. return ret;
  2837. }
  2838. int
  2839. jack_get_fifo_fd (jack_engine_t *engine, unsigned int which_fifo)
  2840. {
  2841. /* caller must hold client_lock */
  2842. char path[PATH_MAX+1];
  2843. struct stat statbuf;
  2844. snprintf (path, sizeof (path), "%s-%d", engine->fifo_prefix,
  2845. which_fifo);
  2846. DEBUG ("%s", path);
  2847. if (stat (path, &statbuf)) {
  2848. if (errno == ENOENT) {
  2849. if (mkfifo(path, 0666) < 0){
  2850. jack_error ("cannot create inter-client FIFO"
  2851. " [%s] (%s)\n", path,
  2852. strerror (errno));
  2853. return -1;
  2854. }
  2855. } else {
  2856. jack_error ("cannot check on FIFO %d\n", which_fifo);
  2857. return -1;
  2858. }
  2859. } else {
  2860. if (!S_ISFIFO(statbuf.st_mode)) {
  2861. jack_error ("FIFO %d (%s) already exists, but is not"
  2862. " a FIFO!\n", which_fifo, path);
  2863. return -1;
  2864. }
  2865. }
  2866. if (which_fifo >= engine->fifo_size) {
  2867. unsigned int i;
  2868. engine->fifo = (int *)
  2869. realloc (engine->fifo,
  2870. sizeof (int) * (engine->fifo_size + 16));
  2871. for (i = engine->fifo_size; i < engine->fifo_size + 16; i++) {
  2872. engine->fifo[i] = -1;
  2873. }
  2874. engine->fifo_size += 16;
  2875. }
  2876. if (engine->fifo[which_fifo] < 0) {
  2877. if ((engine->fifo[which_fifo] =
  2878. open (path, O_RDWR|O_CREAT|O_NONBLOCK, 0666)) < 0) {
  2879. jack_error ("cannot open fifo [%s] (%s)", path,
  2880. strerror (errno));
  2881. return -1;
  2882. }
  2883. DEBUG ("opened engine->fifo[%d] == %d (%s)",
  2884. which_fifo, engine->fifo[which_fifo], path);
  2885. }
  2886. return engine->fifo[which_fifo];
  2887. }
  2888. static void
  2889. jack_clear_fifos (jack_engine_t *engine)
  2890. {
  2891. /* caller must hold client_lock */
  2892. unsigned int i;
  2893. char buf[16];
  2894. /* this just drains the existing FIFO's of any data left in
  2895. them by aborted clients, etc. there is only ever going to
  2896. be 0, 1 or 2 bytes in them, but we'll allow for up to 16.
  2897. */
  2898. for (i = 0; i < engine->fifo_size; i++) {
  2899. if (engine->fifo[i] >= 0) {
  2900. int nread = read (engine->fifo[i], buf, sizeof (buf));
  2901. if (nread < 0 && errno != EAGAIN) {
  2902. jack_error ("clear fifo[%d] error: %s",
  2903. i, strerror (errno));
  2904. }
  2905. }
  2906. }
  2907. }
  2908. static int
  2909. jack_use_driver (jack_engine_t *engine, jack_driver_t *driver)
  2910. {
  2911. if (engine->driver) {
  2912. engine->driver->detach (engine->driver, engine);
  2913. engine->driver = 0;
  2914. }
  2915. if (driver) {
  2916. engine->driver = driver;
  2917. if (driver->attach (driver, engine)) {
  2918. engine->driver = 0;
  2919. return -1;
  2920. }
  2921. engine->rolling_interval =
  2922. jack_rolling_interval (driver->period_usecs);
  2923. }
  2924. return 0;
  2925. }
  2926. /* PORT RELATED FUNCTIONS */
  2927. static jack_port_id_t
  2928. jack_get_free_port (jack_engine_t *engine)
  2929. {
  2930. jack_port_id_t i;
  2931. pthread_mutex_lock (&engine->port_lock);
  2932. for (i = 0; i < engine->port_max; i++) {
  2933. if (engine->control->ports[i].in_use == 0) {
  2934. engine->control->ports[i].in_use = 1;
  2935. break;
  2936. }
  2937. }
  2938. pthread_mutex_unlock (&engine->port_lock);
  2939. if (i == engine->port_max) {
  2940. return (jack_port_id_t) -1;
  2941. }
  2942. return i;
  2943. }
  2944. void
  2945. jack_port_release (jack_engine_t *engine, jack_port_internal_t *port)
  2946. {
  2947. pthread_mutex_lock (&engine->port_lock);
  2948. port->shared->in_use = 0;
  2949. port->shared->alias1[0] = '\0';
  2950. port->shared->alias2[0] = '\0';
  2951. if (port->buffer_info) {
  2952. jack_port_buffer_list_t *blist =
  2953. jack_port_buffer_list (engine, port);
  2954. pthread_mutex_lock (&blist->lock);
  2955. blist->freelist =
  2956. jack_slist_prepend (blist->freelist,
  2957. port->buffer_info);
  2958. port->buffer_info = NULL;
  2959. pthread_mutex_unlock (&blist->lock);
  2960. }
  2961. pthread_mutex_unlock (&engine->port_lock);
  2962. }
  2963. jack_port_internal_t *
  2964. jack_get_port_internal_by_name (jack_engine_t *engine, const char *name)
  2965. {
  2966. jack_port_id_t id;
  2967. pthread_mutex_lock (&engine->port_lock);
  2968. for (id = 0; id < engine->port_max; id++) {
  2969. if (jack_port_name_equals (&engine->control->ports[id], name)) {
  2970. break;
  2971. }
  2972. }
  2973. pthread_mutex_unlock (&engine->port_lock);
  2974. if (id != engine->port_max) {
  2975. return &engine->internal_ports[id];
  2976. } else {
  2977. return NULL;
  2978. }
  2979. }
  2980. int
  2981. jack_port_do_register (jack_engine_t *engine, jack_request_t *req, int internal)
  2982. {
  2983. jack_port_id_t port_id;
  2984. jack_port_shared_t *shared;
  2985. jack_port_internal_t *port;
  2986. jack_client_internal_t *client;
  2987. unsigned long i;
  2988. char *backend_client_name;
  2989. size_t len;
  2990. for (i = 0; i < engine->control->n_port_types; ++i) {
  2991. if (strcmp (req->x.port_info.type,
  2992. engine->control->port_types[i].type_name) == 0) {
  2993. break;
  2994. }
  2995. }
  2996. if (i == engine->control->n_port_types) {
  2997. jack_error ("cannot register a port of type \"%s\"",
  2998. req->x.port_info.type);
  2999. return -1;
  3000. }
  3001. jack_lock_graph (engine);
  3002. if ((client = jack_client_internal_by_id (engine,
  3003. req->x.port_info.client_id))
  3004. == NULL) {
  3005. jack_error ("unknown client id in port registration request");
  3006. jack_unlock_graph (engine);
  3007. return -1;
  3008. }
  3009. if ((port = jack_get_port_by_name(engine, req->x.port_info.name)) != NULL) {
  3010. jack_error ("duplicate port name in port registration request");
  3011. jack_unlock_graph (engine);
  3012. return -1;
  3013. }
  3014. if ((port_id = jack_get_free_port (engine)) == (jack_port_id_t) -1) {
  3015. jack_error ("no ports available!");
  3016. jack_unlock_graph (engine);
  3017. return -1;
  3018. }
  3019. shared = &engine->control->ports[port_id];
  3020. if (!internal || !engine->driver)
  3021. goto fallback;
  3022. backend_client_name = (char *) engine->driver->internal_client->control->name;
  3023. len = strlen (backend_client_name);
  3024. if (strncmp (req->x.port_info.name, backend_client_name, len) != 0)
  3025. goto fallback;
  3026. /* use backend's original as an alias, use predefined names */
  3027. if (strcmp(req->x.port_info.type, JACK_DEFAULT_AUDIO_TYPE) == 0) {
  3028. if ((req->x.port_info.flags & (JackPortIsPhysical|JackPortIsInput)) == (JackPortIsPhysical|JackPortIsInput)) {
  3029. snprintf (shared->name, sizeof (shared->name), JACK_BACKEND_ALIAS ":playback_%d", ++engine->audio_out_cnt);
  3030. strcpy (shared->alias1, req->x.port_info.name);
  3031. goto next;
  3032. }
  3033. else if ((req->x.port_info.flags & (JackPortIsPhysical|JackPortIsOutput)) == (JackPortIsPhysical|JackPortIsOutput)) {
  3034. snprintf (shared->name, sizeof (shared->name), JACK_BACKEND_ALIAS ":capture_%d", ++engine->audio_in_cnt);
  3035. strcpy (shared->alias1, req->x.port_info.name);
  3036. goto next;
  3037. }
  3038. }
  3039. #if 0 // do not do this for MIDI
  3040. else if (strcmp(req->x.port_info.type, JACK_DEFAULT_MIDI_TYPE) == 0) {
  3041. if ((req->x.port_info.flags & (JackPortIsPhysical|JackPortIsInput)) == (JackPortIsPhysical|JackPortIsInput)) {
  3042. snprintf (shared->name, sizeof (shared->name), JACK_BACKEND_ALIAS ":midi_playback_%d", ++engine->midi_out_cnt);
  3043. strcpy (shared->alias1, req->x.port_info.name);
  3044. goto next;
  3045. }
  3046. else if ((req->x.port_info.flags & (JackPortIsPhysical|JackPortIsOutput)) == (JackPortIsPhysical|JackPortIsOutput)) {
  3047. snprintf (shared->name, sizeof (shared->name), JACK_BACKEND_ALIAS ":midi_capture_%d", ++engine->midi_in_cnt);
  3048. strcpy (shared->alias1, req->x.port_info.name);
  3049. goto next;
  3050. }
  3051. }
  3052. #endif
  3053. fallback:
  3054. strcpy (shared->name, req->x.port_info.name);
  3055. next:
  3056. shared->ptype_id = engine->control->port_types[i].ptype_id;
  3057. shared->client_id = req->x.port_info.client_id;
  3058. shared->flags = req->x.port_info.flags;
  3059. shared->latency = 0;
  3060. shared->monitor_requests = 0;
  3061. port = &engine->internal_ports[port_id];
  3062. port->shared = shared;
  3063. port->connections = 0;
  3064. port->buffer_info = NULL;
  3065. if (jack_port_assign_buffer (engine, port)) {
  3066. jack_error ("cannot assign buffer for port");
  3067. jack_port_release (engine, &engine->internal_ports[port_id]);
  3068. jack_unlock_graph (engine);
  3069. return -1;
  3070. }
  3071. client->ports = jack_slist_prepend (client->ports, port);
  3072. jack_port_registration_notify (engine, port_id, TRUE);
  3073. jack_unlock_graph (engine);
  3074. VERBOSE (engine, "registered port %s, offset = %u",
  3075. shared->name, (unsigned int)shared->offset);
  3076. req->x.port_info.port_id = port_id;
  3077. return 0;
  3078. }
  3079. int
  3080. jack_port_do_unregister (jack_engine_t *engine, jack_request_t *req)
  3081. {
  3082. jack_client_internal_t *client;
  3083. jack_port_shared_t *shared;
  3084. jack_port_internal_t *port;
  3085. if (req->x.port_info.port_id < 0 ||
  3086. req->x.port_info.port_id > engine->port_max) {
  3087. jack_error ("invalid port ID %" PRIu32
  3088. " in unregister request",
  3089. req->x.port_info.port_id);
  3090. return -1;
  3091. }
  3092. shared = &engine->control->ports[req->x.port_info.port_id];
  3093. if (shared->client_id != req->x.port_info.client_id) {
  3094. jack_error ("Client %" PRIu32
  3095. " is not allowed to remove port %s",
  3096. req->x.port_info.client_id, shared->name);
  3097. return -1;
  3098. }
  3099. jack_lock_graph (engine);
  3100. if ((client = jack_client_internal_by_id (engine, shared->client_id))
  3101. == NULL) {
  3102. jack_error ("unknown client id in port registration request");
  3103. jack_unlock_graph (engine);
  3104. return -1;
  3105. }
  3106. port = &engine->internal_ports[req->x.port_info.port_id];
  3107. jack_port_clear_connections (engine, port);
  3108. jack_port_release (engine,
  3109. &engine->internal_ports[req->x.port_info.port_id]);
  3110. client->ports = jack_slist_remove (client->ports, port);
  3111. jack_port_registration_notify (engine, req->x.port_info.port_id,
  3112. FALSE);
  3113. jack_unlock_graph (engine);
  3114. return 0;
  3115. }
  3116. int
  3117. jack_do_get_port_connections (jack_engine_t *engine, jack_request_t *req,
  3118. int reply_fd)
  3119. {
  3120. jack_port_internal_t *port;
  3121. JSList *node;
  3122. unsigned int i;
  3123. int ret = -1;
  3124. int internal = FALSE;
  3125. jack_rdlock_graph (engine);
  3126. port = &engine->internal_ports[req->x.port_info.port_id];
  3127. DEBUG ("Getting connections for port '%s'.", port->shared->name);
  3128. req->x.port_connections.nports = jack_slist_length (port->connections);
  3129. req->status = 0;
  3130. /* figure out if this is an internal or external client */
  3131. for (node = engine->clients; node; node = jack_slist_next (node)) {
  3132. if (((jack_client_internal_t *) node->data)->request_fd
  3133. == reply_fd) {
  3134. internal = jack_client_is_internal(
  3135. (jack_client_internal_t *) node->data);
  3136. break;
  3137. }
  3138. }
  3139. if (!internal) {
  3140. if (write (reply_fd, req, sizeof (*req))
  3141. < (ssize_t) sizeof (req)) {
  3142. jack_error ("cannot write GetPortConnections result "
  3143. "to client via fd = %d (%s)",
  3144. reply_fd, strerror (errno));
  3145. goto out;
  3146. }
  3147. } else {
  3148. req->x.port_connections.ports = (const char**)
  3149. malloc (sizeof (char *)
  3150. * req->x.port_connections.nports);
  3151. }
  3152. if (req->type == GetPortConnections) {
  3153. for (i = 0, node = port->connections; node;
  3154. node = jack_slist_next (node), ++i) {
  3155. jack_port_id_t port_id;
  3156. if (((jack_connection_internal_t *) node->data)->source
  3157. == port) {
  3158. port_id = ((jack_connection_internal_t *)
  3159. node->data)->destination->shared->id;
  3160. } else {
  3161. port_id = ((jack_connection_internal_t *)
  3162. node->data)->source->shared->id;
  3163. }
  3164. if (internal) {
  3165. /* internal client asking for
  3166. * names. store in malloc'ed space,
  3167. * client frees
  3168. */
  3169. char **ports = (char **) req->x.port_connections.ports;
  3170. ports[i] =
  3171. engine->control->ports[port_id].name;
  3172. } else {
  3173. /* external client asking for
  3174. * names. we write the port id's to
  3175. * the reply fd.
  3176. */
  3177. if (write (reply_fd, &port_id,
  3178. sizeof (port_id))
  3179. < (ssize_t) sizeof (port_id)) {
  3180. jack_error ("cannot write port id "
  3181. "to client");
  3182. goto out;
  3183. }
  3184. }
  3185. }
  3186. }
  3187. ret = 0;
  3188. out:
  3189. req->status = ret;
  3190. jack_unlock_graph (engine);
  3191. return ret;
  3192. }
  3193. void
  3194. jack_port_registration_notify (jack_engine_t *engine,
  3195. jack_port_id_t port_id, int yn)
  3196. {
  3197. jack_event_t event;
  3198. jack_client_internal_t *client;
  3199. JSList *node;
  3200. event.type = (yn ? PortRegistered : PortUnregistered);
  3201. event.x.port_id = port_id;
  3202. for (node = engine->clients; node; node = jack_slist_next (node)) {
  3203. client = (jack_client_internal_t *) node->data;
  3204. if (!client->control->active) {
  3205. continue;
  3206. }
  3207. if (client->control->port_register_cbset) {
  3208. if (jack_deliver_event (engine, client, &event)) {
  3209. jack_error ("cannot send port registration"
  3210. " notification to %s (%s)",
  3211. client->control->name,
  3212. strerror (errno));
  3213. }
  3214. }
  3215. }
  3216. }
  3217. void
  3218. jack_client_registration_notify (jack_engine_t *engine,
  3219. const char* name, int yn)
  3220. {
  3221. jack_event_t event;
  3222. jack_client_internal_t *client;
  3223. JSList *node;
  3224. event.type = (yn ? ClientRegistered : ClientUnregistered);
  3225. snprintf (event.x.name, sizeof (event.x.name), "%s", name);
  3226. for (node = engine->clients; node; node = jack_slist_next (node)) {
  3227. client = (jack_client_internal_t *) node->data;
  3228. if (!client->control->active) {
  3229. continue;
  3230. }
  3231. if (strcmp ((char*) client->control->name, (char*) name) == 0) {
  3232. /* do not notify client of its own registration */
  3233. continue;
  3234. }
  3235. if (client->control->client_register_cbset) {
  3236. if (jack_deliver_event (engine, client, &event)) {
  3237. jack_error ("cannot send client registration"
  3238. " notification to %s (%s)",
  3239. client->control->name,
  3240. strerror (errno));
  3241. }
  3242. }
  3243. }
  3244. }
  3245. int
  3246. jack_port_assign_buffer (jack_engine_t *engine, jack_port_internal_t *port)
  3247. {
  3248. jack_port_buffer_list_t *blist =
  3249. jack_port_buffer_list (engine, port);
  3250. jack_port_buffer_info_t *bi;
  3251. if (port->shared->flags & JackPortIsInput) {
  3252. port->shared->offset = 0;
  3253. return 0;
  3254. }
  3255. pthread_mutex_lock (&blist->lock);
  3256. if (blist->freelist == NULL) {
  3257. jack_port_type_info_t *port_type =
  3258. jack_port_type_info (engine, port);
  3259. jack_error ("all %s port buffers in use!",
  3260. port_type->type_name);
  3261. pthread_mutex_unlock (&blist->lock);
  3262. return -1;
  3263. }
  3264. bi = (jack_port_buffer_info_t *) blist->freelist->data;
  3265. blist->freelist = jack_slist_remove (blist->freelist, bi);
  3266. port->shared->offset = bi->offset;
  3267. port->buffer_info = bi;
  3268. pthread_mutex_unlock (&blist->lock);
  3269. return 0;
  3270. }
  3271. static jack_port_internal_t *
  3272. jack_get_port_by_name (jack_engine_t *engine, const char *name)
  3273. {
  3274. jack_port_id_t id;
  3275. /* Note the potential race on "in_use". Other design
  3276. elements prevent this from being a problem.
  3277. */
  3278. for (id = 0; id < engine->port_max; id++) {
  3279. if (engine->control->ports[id].in_use &&
  3280. jack_port_name_equals (&engine->control->ports[id], name)) {
  3281. return &engine->internal_ports[id];
  3282. }
  3283. }
  3284. return NULL;
  3285. }
  3286. static int
  3287. jack_send_connection_notification (jack_engine_t *engine,
  3288. jack_client_id_t client_id,
  3289. jack_port_id_t self_id,
  3290. jack_port_id_t other_id, int connected)
  3291. {
  3292. jack_client_internal_t *client;
  3293. jack_event_t event;
  3294. if ((client = jack_client_internal_by_id (engine, client_id)) == NULL) {
  3295. jack_error ("no such client %" PRIu32
  3296. " during connection notification", client_id);
  3297. return -1;
  3298. }
  3299. if (client->control->active) {
  3300. event.type = (connected ? PortConnected : PortDisconnected);
  3301. event.x.self_id = self_id;
  3302. event.y.other_id = other_id;
  3303. if (jack_deliver_event (engine, client, &event)) {
  3304. jack_error ("cannot send port connection notification"
  3305. " to client %s (%s)",
  3306. client->control->name, strerror (errno));
  3307. return -1;
  3308. }
  3309. }
  3310. return 0;
  3311. }