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.

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