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.

3696 lines
89KB

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