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.

4334 lines
109KB

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