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.

3674 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->freewheeling && engine->watchdog_check == 0) {
  740. jack_error ("jackd watchdog: timeout - killing jackd");
  741. /* Kill the current client (guilt by association). */
  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 (NULL, &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, pid_t cap_pid)
  937. {
  938. int ret = -1;
  939. /* before sending this request the client has
  940. already checked that the engine has
  941. realtime capabilities, that it is running
  942. realtime and that the pid is defined
  943. */
  944. if ((ret = give_capabilities (engine, cap_pid)) != 0) {
  945. jack_error ("could not give capabilities to "
  946. "process %d\n",
  947. cap_pid);
  948. } else {
  949. VERBOSE (engine, "gave capabilities to"
  950. " process %d\n",
  951. cap_pid);
  952. }
  953. return ret;
  954. }
  955. #endif /* USE_CAPABILITIES */
  956. /* perform internal or external client request
  957. *
  958. * reply_fd is NULL for internal requests
  959. */
  960. static void
  961. do_request (jack_engine_t *engine, jack_request_t *req, int *reply_fd)
  962. {
  963. /* The request_lock serializes internal requests (from any
  964. * server thread) with external requests (always from the
  965. * server thread). */
  966. pthread_mutex_lock (&engine->request_lock);
  967. DEBUG ("got a request of type %d", req->type);
  968. switch (req->type) {
  969. case RegisterPort:
  970. req->status = jack_port_do_register (engine, req);
  971. break;
  972. case UnRegisterPort:
  973. req->status = jack_port_do_unregister (engine, req);
  974. break;
  975. case ConnectPorts:
  976. req->status = jack_port_do_connect
  977. (engine, req->x.connect.source_port,
  978. req->x.connect.destination_port);
  979. break;
  980. case DisconnectPort:
  981. req->status = jack_port_do_disconnect_all
  982. (engine, req->x.port_info.port_id);
  983. break;
  984. case DisconnectPorts:
  985. req->status = jack_port_do_disconnect
  986. (engine, req->x.connect.source_port,
  987. req->x.connect.destination_port);
  988. break;
  989. case ActivateClient:
  990. req->status = jack_client_activate (engine, req->x.client_id);
  991. break;
  992. case DeactivateClient:
  993. req->status = jack_client_deactivate (engine, req->x.client_id);
  994. break;
  995. case SetTimeBaseClient:
  996. req->status = jack_timebase_set (engine,
  997. req->x.timebase.client_id,
  998. req->x.timebase.conditional);
  999. break;
  1000. case ResetTimeBaseClient:
  1001. req->status = jack_timebase_reset (engine, req->x.client_id);
  1002. break;
  1003. case SetSyncClient:
  1004. req->status =
  1005. jack_transport_client_set_sync (engine,
  1006. req->x.client_id);
  1007. break;
  1008. case ResetSyncClient:
  1009. req->status =
  1010. jack_transport_client_reset_sync (engine,
  1011. req->x.client_id);
  1012. break;
  1013. case SetSyncTimeout:
  1014. req->status = jack_transport_set_sync_timeout (engine,
  1015. req->x.timeout);
  1016. break;
  1017. #ifdef USE_CAPABILITIES
  1018. case SetClientCapabilities:
  1019. req->status = jack_set_client_capabilities (engine,
  1020. req->x.cap_pid);
  1021. break;
  1022. #endif /* USE_CAPABILITIES */
  1023. case GetPortConnections:
  1024. case GetPortNConnections:
  1025. //JOQ bug: reply_fd may be NULL if internal request
  1026. if ((req->status =
  1027. jack_do_get_port_connections (engine, req, *reply_fd))
  1028. == 0) {
  1029. /* we have already replied, don't do it again */
  1030. *reply_fd = -1;
  1031. }
  1032. break;
  1033. case FreeWheel:
  1034. req->status = jack_start_freewheeling (engine);
  1035. break;
  1036. case StopFreeWheel:
  1037. req->status = jack_stop_freewheeling (engine);
  1038. break;
  1039. case SetBufferSize:
  1040. req->status = jack_set_buffer_size_request (engine,
  1041. req->x.nframes);
  1042. break;
  1043. case IntClientHandle:
  1044. jack_intclient_handle_request (engine, req);
  1045. break;
  1046. case IntClientLoad:
  1047. jack_intclient_load_request (engine, req);
  1048. break;
  1049. case IntClientName:
  1050. jack_intclient_name_request (engine, req);
  1051. break;
  1052. case IntClientUnload:
  1053. jack_intclient_unload_request (engine, req);
  1054. break;
  1055. default:
  1056. /* some requests are handled entirely on the client
  1057. * side, by adjusting the shared memory area(s) */
  1058. break;
  1059. }
  1060. pthread_mutex_unlock (&engine->request_lock);
  1061. DEBUG ("status of request: %d", req->status);
  1062. }
  1063. int
  1064. internal_client_request (void* ptr, jack_request_t *request)
  1065. {
  1066. do_request ((jack_engine_t*) ptr, request, NULL);
  1067. return request->status;
  1068. }
  1069. static int
  1070. handle_external_client_request (jack_engine_t *engine, int fd)
  1071. {
  1072. jack_request_t req;
  1073. jack_client_internal_t *client = 0;
  1074. int reply_fd;
  1075. JSList *node;
  1076. ssize_t r;
  1077. DEBUG ("HIT: before lock");
  1078. jack_lock_graph (engine);
  1079. DEBUG ("HIT: before for");
  1080. for (node = engine->clients; node; node = jack_slist_next (node)) {
  1081. if (((jack_client_internal_t *) node->data)->request_fd == fd) {
  1082. DEBUG ("HIT: in for");
  1083. client = (jack_client_internal_t *) node->data;
  1084. break;
  1085. }
  1086. }
  1087. DEBUG ("HIT: after for");
  1088. jack_unlock_graph (engine);
  1089. if (client == NULL) {
  1090. jack_error ("client input on unknown fd %d!", fd);
  1091. return -1;
  1092. }
  1093. if ((r = read (client->request_fd, &req, sizeof (req)))
  1094. < (ssize_t) sizeof (req)) {
  1095. jack_error ("cannot read request from client (%d/%d/%s)",
  1096. r, sizeof(req), strerror (errno));
  1097. client->error++;
  1098. return -1;
  1099. }
  1100. reply_fd = client->request_fd;
  1101. do_request (engine, &req, &reply_fd);
  1102. if (reply_fd >= 0) {
  1103. DEBUG ("replying to client");
  1104. if (write (reply_fd, &req, sizeof (req))
  1105. < (ssize_t) sizeof (req)) {
  1106. jack_error ("cannot write request result to client");
  1107. return -1;
  1108. }
  1109. } else {
  1110. DEBUG ("*not* replying to client");
  1111. }
  1112. return 0;
  1113. }
  1114. static int
  1115. handle_client_ack_connection (jack_engine_t *engine, int client_fd)
  1116. {
  1117. jack_client_internal_t *client;
  1118. jack_client_connect_ack_request_t req;
  1119. jack_client_connect_ack_result_t res;
  1120. if (read (client_fd, &req, sizeof (req)) != sizeof (req)) {
  1121. jack_error ("cannot read ACK connection request from client");
  1122. return -1;
  1123. }
  1124. if ((client = jack_client_internal_by_id (engine, req.client_id))
  1125. == NULL) {
  1126. jack_error ("unknown client ID in ACK connection request");
  1127. return -1;
  1128. }
  1129. client->event_fd = client_fd;
  1130. res.status = 0;
  1131. if (write (client->event_fd, &res, sizeof (res)) != sizeof (res)) {
  1132. jack_error ("cannot write ACK connection response to client");
  1133. return -1;
  1134. }
  1135. return 0;
  1136. }
  1137. static void *
  1138. jack_server_thread (void *arg)
  1139. {
  1140. jack_engine_t *engine = (jack_engine_t *) arg;
  1141. struct sockaddr_un client_addr;
  1142. socklen_t client_addrlen;
  1143. struct pollfd *pfd;
  1144. int client_socket;
  1145. int done = 0;
  1146. int i;
  1147. int max;
  1148. engine->pfd[0].fd = engine->fds[0];
  1149. engine->pfd[0].events = POLLIN|POLLERR;
  1150. engine->pfd[1].fd = engine->fds[1];
  1151. engine->pfd[1].events = POLLIN|POLLERR;
  1152. engine->pfd_max = 2;
  1153. pfd = engine->pfd;
  1154. max = engine->pfd_max;
  1155. while (!done) {
  1156. DEBUG ("start while");
  1157. if (poll (pfd, max, 10000) < 0) {
  1158. if (errno == EINTR) {
  1159. continue;
  1160. }
  1161. jack_error ("poll failed (%s)", strerror (errno));
  1162. break;
  1163. }
  1164. DEBUG("server thread back from poll");
  1165. /* Stephane Letz: letz@grame.fr : has to be added
  1166. * otherwise pthread_cancel() does not work on MacOSX */
  1167. pthread_testcancel();
  1168. /* check each client socket before handling other request*/
  1169. for (i = 2; i < max; i++) {
  1170. if (pfd[i].fd < 0) {
  1171. continue;
  1172. }
  1173. if (pfd[i].revents & ~POLLIN) {
  1174. jack_client_disconnect (engine, pfd[i].fd);
  1175. } else if (pfd[i].revents & POLLIN) {
  1176. if (handle_external_client_request
  1177. (engine, pfd[i].fd)) {
  1178. jack_error ("could not handle external"
  1179. " client request");
  1180. #ifdef JACK_USE_MACH_THREADS
  1181. /* poll is implemented using
  1182. select (see the macosx/fakepoll
  1183. code). When the socket is closed
  1184. select does not return any error,
  1185. POLLIN is true and the next read
  1186. will return 0 bytes. This
  1187. behaviour is diffrent from the
  1188. Linux poll behaviour. Thus we use
  1189. this condition as a socket error
  1190. and remove the client.
  1191. */
  1192. jack_client_disconnect(engine, pfd[i].fd);
  1193. #endif /* JACK_USE_MACH_THREADS */
  1194. }
  1195. }
  1196. }
  1197. /* check the master server socket */
  1198. if (pfd[0].revents & POLLERR) {
  1199. jack_error ("error on server socket");
  1200. break;
  1201. }
  1202. if (engine->control->engine_ok && pfd[0].revents & POLLIN) {
  1203. DEBUG ("pfd[0].revents & POLLIN");
  1204. memset (&client_addr, 0, sizeof (client_addr));
  1205. client_addrlen = sizeof (client_addr);
  1206. if ((client_socket =
  1207. accept (engine->fds[0],
  1208. (struct sockaddr *) &client_addr,
  1209. &client_addrlen)) < 0) {
  1210. jack_error ("cannot accept new connection (%s)",
  1211. strerror (errno));
  1212. } else if (jack_client_create (engine, client_socket)
  1213. < 0) {
  1214. jack_error ("cannot complete client "
  1215. "connection process");
  1216. close (client_socket);
  1217. }
  1218. }
  1219. /* Possibly, jack_client_create() may have
  1220. * realloced engine->pfd. We depend on that being
  1221. * done within this thread. That is currently true,
  1222. * since external clients are only created here.
  1223. */
  1224. pfd = engine->pfd;
  1225. max = engine->pfd_max;
  1226. /* check the ACK server socket */
  1227. if (pfd[1].revents & POLLERR) {
  1228. jack_error ("error on server ACK socket");
  1229. break;
  1230. }
  1231. if (engine->control->engine_ok && pfd[1].revents & POLLIN) {
  1232. DEBUG ("pfd[1].revents & POLLIN");
  1233. memset (&client_addr, 0, sizeof (client_addr));
  1234. client_addrlen = sizeof (client_addr);
  1235. if ((client_socket =
  1236. accept (engine->fds[1],
  1237. (struct sockaddr *) &client_addr,
  1238. &client_addrlen)) < 0) {
  1239. jack_error ("cannot accept new ACK connection"
  1240. " (%s)", strerror (errno));
  1241. } else if (handle_client_ack_connection
  1242. (engine, client_socket)) {
  1243. jack_error ("cannot complete client ACK "
  1244. "connection process");
  1245. close (client_socket);
  1246. }
  1247. }
  1248. }
  1249. return 0;
  1250. }
  1251. jack_engine_t *
  1252. jack_engine_new (int realtime, int rtpriority, int do_mlock, int do_unlock,
  1253. const char *server_name, int temporary, int verbose,
  1254. int client_timeout, unsigned int port_max, pid_t wait_pid,
  1255. JSList *drivers)
  1256. {
  1257. jack_engine_t *engine;
  1258. unsigned int i;
  1259. #ifdef USE_CAPABILITIES
  1260. uid_t uid = getuid ();
  1261. uid_t euid = geteuid ();
  1262. #endif /* USE_CAPABILITIES */
  1263. /* start a thread to display messages from realtime threads */
  1264. jack_messagebuffer_init();
  1265. jack_init_time ();
  1266. engine = (jack_engine_t *) malloc (sizeof (jack_engine_t));
  1267. engine->drivers = drivers;
  1268. engine->driver = NULL;
  1269. engine->driver_desc = NULL;
  1270. engine->driver_params = NULL;
  1271. engine->set_sample_rate = jack_set_sample_rate;
  1272. engine->set_buffer_size = jack_driver_buffer_size;
  1273. engine->run_cycle = jack_run_cycle;
  1274. engine->delay = jack_engine_delay;
  1275. engine->driver_exit = jack_engine_driver_exit;
  1276. engine->transport_cycle_start = jack_transport_cycle_start;
  1277. engine->client_timeout_msecs = client_timeout;
  1278. engine->next_client_id = 1; /* 0 is a NULL client ID */
  1279. engine->port_max = port_max;
  1280. engine->rtpriority = rtpriority;
  1281. engine->silent_buffer = 0;
  1282. engine->verbose = verbose;
  1283. engine->server_name = server_name;
  1284. engine->temporary = temporary;
  1285. engine->freewheeling = 0;
  1286. engine->feedbackcount = 0;
  1287. engine->wait_pid = wait_pid;
  1288. jack_engine_reset_rolling_usecs (engine);
  1289. engine->max_usecs = 0.0f;
  1290. pthread_mutex_init (&engine->client_lock, 0);
  1291. pthread_mutex_init (&engine->port_lock, 0);
  1292. pthread_mutex_init (&engine->request_lock, 0);
  1293. engine->clients = 0;
  1294. engine->pfd_size = 16;
  1295. engine->pfd_max = 0;
  1296. engine->pfd = (struct pollfd *) malloc (sizeof (struct pollfd)
  1297. * engine->pfd_size);
  1298. engine->fifo_size = 16;
  1299. engine->fifo = (int *) malloc (sizeof (int) * engine->fifo_size);
  1300. for (i = 0; i < engine->fifo_size; i++) {
  1301. engine->fifo[i] = -1;
  1302. }
  1303. engine->external_client_cnt = 0;
  1304. srandom (time ((time_t *) 0));
  1305. if (jack_shmalloc ("jack-engine",
  1306. sizeof (jack_control_t)
  1307. + ((sizeof (jack_port_shared_t) * engine->port_max)),
  1308. &engine->control_shm)) {
  1309. jack_error ("cannot create engine control shared memory "
  1310. "segment (%s)", strerror (errno));
  1311. return NULL;
  1312. }
  1313. if (jack_attach_shm (&engine->control_shm)) {
  1314. jack_error ("cannot attach to engine control shared memory"
  1315. " (%s)", strerror (errno));
  1316. jack_destroy_shm (&engine->control_shm);
  1317. return NULL;
  1318. }
  1319. engine->control = (jack_control_t *)
  1320. jack_shm_addr (&engine->control_shm);
  1321. /* Setup port type information from builtins. buffer space is
  1322. * allocated when the driver calls jack_driver_buffer_size().
  1323. */
  1324. for (i = 0; jack_builtin_port_types[i].type_name[0]; ++i) {
  1325. memcpy (&engine->control->port_types[i],
  1326. &jack_builtin_port_types[i],
  1327. sizeof (jack_port_type_info_t));
  1328. VERBOSE (engine, "registered builtin port type %s\n",
  1329. engine->control->port_types[i].type_name);
  1330. /* the port type id is index into port_types array */
  1331. engine->control->port_types[i].ptype_id = i;
  1332. /* be sure to initialize mutex correctly */
  1333. pthread_mutex_init (&engine->port_buffers[i].lock, NULL);
  1334. /* set buffer list info correctly */
  1335. engine->port_buffers[i].freelist = NULL;
  1336. engine->port_buffers[i].info = NULL;
  1337. /* mark each port segment as not allocated */
  1338. engine->port_segment[i].index = -1;
  1339. engine->port_segment[i].attached_at = 0;
  1340. }
  1341. engine->control->n_port_types = i;
  1342. /* Mark all ports as available */
  1343. for (i = 0; i < engine->port_max; i++) {
  1344. engine->control->ports[i].in_use = 0;
  1345. engine->control->ports[i].id = i;
  1346. }
  1347. /* allocate internal port structures so that we can keep track
  1348. * of port connections.
  1349. */
  1350. engine->internal_ports = (jack_port_internal_t *)
  1351. malloc (sizeof (jack_port_internal_t) * engine->port_max);
  1352. for (i = 0; i < engine->port_max; i++) {
  1353. engine->internal_ports[i].connections = 0;
  1354. }
  1355. if (make_sockets (engine->server_name, engine->fds) < 0) {
  1356. jack_error ("cannot create server sockets");
  1357. return NULL;
  1358. }
  1359. engine->control->port_max = engine->port_max;
  1360. engine->control->real_time = realtime;
  1361. engine->control->client_priority = (realtime
  1362. ? engine->rtpriority - 1
  1363. : 0);
  1364. engine->control->do_mlock = do_mlock;
  1365. engine->control->do_munlock = do_unlock;
  1366. engine->control->cpu_load = 0;
  1367. engine->control->xrun_delayed_usecs = 0;
  1368. engine->control->max_delayed_usecs = 0;
  1369. engine->control->frame_timer.frames = 0;
  1370. engine->control->frame_timer.reset_pending = 0;
  1371. engine->control->frame_timer.current_wakeup = 0;
  1372. engine->control->frame_timer.next_wakeup = 0;
  1373. engine->control->frame_timer.initialized = 0;
  1374. engine->control->frame_timer.filter_coefficient = 0.01;
  1375. engine->control->frame_timer.second_order_integrator = 0;
  1376. engine->first_wakeup = 1;
  1377. engine->control->buffer_size = 0;
  1378. jack_transport_init (engine);
  1379. jack_set_sample_rate (engine, 0);
  1380. engine->control->internal = 0;
  1381. engine->control->has_capabilities = 0;
  1382. #ifdef JACK_USE_MACH_THREADS
  1383. /* specific resources for server/client real-time thread
  1384. * communication */
  1385. engine->servertask = mach_task_self();
  1386. if (task_get_bootstrap_port(engine->servertask, &engine->bp)){
  1387. jack_error("Jackd: Can't find bootstrap mach port");
  1388. return NULL;
  1389. }
  1390. engine->portnum = 0;
  1391. #endif /* JACK_USE_MACH_THREADS */
  1392. #ifdef USE_CAPABILITIES
  1393. if (uid == 0 || euid == 0) {
  1394. VERBOSE (engine, "running with uid=%d and euid=%d, "
  1395. "will not try to use capabilites\n",
  1396. uid, euid);
  1397. } else {
  1398. /* only try to use capabilities if not running as root */
  1399. engine->control->has_capabilities = check_capabilities (engine);
  1400. if (engine->control->has_capabilities == 0) {
  1401. VERBOSE (engine, "required capabilities not "
  1402. "available\n");
  1403. }
  1404. if (engine->verbose) {
  1405. size_t size;
  1406. cap_t cap = cap_init();
  1407. capgetp(0, cap);
  1408. VERBOSE (engine, "capabilities: %s\n",
  1409. cap_to_text(cap, &size));
  1410. }
  1411. }
  1412. #endif /* USE_CAPABILITIES */
  1413. #ifdef USE_MLOCK
  1414. if (realtime && do_mlock && (mlockall (MCL_CURRENT | MCL_FUTURE) != 0)) {
  1415. jack_error ("cannot lock down memory for jackd (%s)",
  1416. strerror (errno));
  1417. #ifdef ENSURE_MLOCK
  1418. return NULL;
  1419. #endif /* ENSURE_MLOCK */
  1420. }
  1421. #endif /* USE_MLOCK */
  1422. engine->control->engine_ok = 1;
  1423. snprintf (engine->fifo_prefix, sizeof (engine->fifo_prefix),
  1424. "%s/jack-ack-fifo-%d",
  1425. jack_server_dir (engine->server_name), getpid ());
  1426. (void) jack_get_fifo_fd (engine, 0);
  1427. jack_create_thread (NULL, &engine->server_thread, 0, FALSE,
  1428. &jack_server_thread, engine);
  1429. return engine;
  1430. }
  1431. static void
  1432. jack_engine_delay (jack_engine_t *engine, float delayed_usecs)
  1433. {
  1434. JSList *node;
  1435. jack_event_t event;
  1436. engine->control->frame_timer.reset_pending = 1;
  1437. engine->control->xrun_delayed_usecs = delayed_usecs;
  1438. if (delayed_usecs > engine->control->max_delayed_usecs)
  1439. engine->control->max_delayed_usecs = delayed_usecs;
  1440. event.type = XRun;
  1441. jack_lock_graph (engine);
  1442. for (node = engine->clients; node; node = jack_slist_next (node)) {
  1443. jack_deliver_event (engine,
  1444. (jack_client_internal_t *) node->data,
  1445. &event);
  1446. }
  1447. jack_unlock_graph (engine);
  1448. }
  1449. static inline void
  1450. jack_inc_frame_time (jack_engine_t *engine, jack_nframes_t nframes)
  1451. {
  1452. jack_frame_timer_t *timer = &engine->control->frame_timer;
  1453. jack_time_t now = engine->driver->last_wait_ust; // effective time
  1454. float delta;
  1455. // really need a memory barrier here
  1456. timer->guard1++;
  1457. delta = (int64_t) now - (int64_t) timer->next_wakeup;
  1458. timer->current_wakeup = timer->next_wakeup;
  1459. timer->frames += nframes;
  1460. timer->second_order_integrator += 0.5f *
  1461. timer->filter_coefficient * delta;
  1462. timer->next_wakeup = timer->current_wakeup +
  1463. engine->driver->period_usecs +
  1464. (int64_t) floorf ((timer->filter_coefficient *
  1465. (delta + timer->second_order_integrator)));
  1466. timer->initialized = 1;
  1467. // might need a memory barrier here
  1468. timer->guard2++;
  1469. }
  1470. static void*
  1471. jack_engine_freewheel (void *arg)
  1472. {
  1473. jack_engine_t* engine = (jack_engine_t *) arg;
  1474. VERBOSE (engine, "freewheel thread starting ...\n");
  1475. /* we should not be running SCHED_FIFO, so we don't
  1476. have to do anything about scheduling.
  1477. */
  1478. while (engine->freewheeling) {
  1479. jack_lock_graph (engine);
  1480. if (jack_engine_process (engine,
  1481. engine->control->buffer_size)) {
  1482. jack_error ("process cycle within freewheel failed");
  1483. jack_unlock_graph (engine);
  1484. break;
  1485. }
  1486. jack_unlock_graph (engine);
  1487. }
  1488. VERBOSE (engine, "freewheel came to an end, naturally\n");
  1489. return 0;
  1490. }
  1491. static int
  1492. jack_start_freewheeling (jack_engine_t* engine)
  1493. {
  1494. jack_event_t event;
  1495. if (engine->freewheeling) {
  1496. return 0;
  1497. }
  1498. if (engine->driver == NULL) {
  1499. jack_error ("cannot start freewheeling without a driver!");
  1500. return -1;
  1501. }
  1502. /* stop driver before telling anyone about it so
  1503. there are no more process() calls being handled.
  1504. */
  1505. if (engine->driver->stop (engine->driver)) {
  1506. jack_error ("could not stop driver for freewheeling");
  1507. return -1;
  1508. }
  1509. engine->freewheeling = 1;
  1510. event.type = StartFreewheel;
  1511. jack_deliver_event_to_all (engine, &event);
  1512. if (jack_create_thread (NULL, &engine->freewheel_thread, 0, FALSE,
  1513. jack_engine_freewheel, engine)) {
  1514. jack_error ("could not start create freewheel thread");
  1515. return -1;
  1516. }
  1517. return 0;
  1518. }
  1519. static int
  1520. jack_stop_freewheeling (jack_engine_t* engine)
  1521. {
  1522. jack_event_t event;
  1523. void *ftstatus;
  1524. if (!engine->freewheeling) {
  1525. return 0;
  1526. }
  1527. if (engine->driver == NULL) {
  1528. jack_error ("cannot start freewheeling without a driver!");
  1529. return -1;
  1530. }
  1531. if (!engine->freewheeling) {
  1532. VERBOSE (engine, "stop freewheel when not freewheeling\n");
  1533. return 0;
  1534. }
  1535. /* tell the freewheel thread to stop, and wait for it
  1536. to exit.
  1537. */
  1538. engine->freewheeling = 0;
  1539. VERBOSE (engine, "freewheeling stopped, waiting for thread\n");
  1540. pthread_join (engine->freewheel_thread, &ftstatus);
  1541. VERBOSE (engine, "freewheel thread has returned\n");
  1542. /* tell everyone we've stopped */
  1543. event.type = StopFreewheel;
  1544. jack_deliver_event_to_all (engine, &event);
  1545. /* restart the driver */
  1546. if (engine->driver->start (engine->driver)) {
  1547. jack_error ("could not restart driver after freewheeling");
  1548. return -1;
  1549. }
  1550. return 0;
  1551. }
  1552. static int
  1553. jack_run_one_cycle (jack_engine_t *engine, jack_nframes_t nframes,
  1554. float delayed_usecs)
  1555. {
  1556. jack_driver_t* driver = engine->driver;
  1557. int ret = -1;
  1558. static int consecutive_excessive_delays = 0;
  1559. #define WORK_SCALE 1.0f
  1560. if (engine->control->real_time &&
  1561. engine->spare_usecs &&
  1562. ((WORK_SCALE * engine->spare_usecs) <= delayed_usecs)) {
  1563. MESSAGE("delay of %.3f usecs exceeds estimated spare"
  1564. " time of %.3f; restart ...\n",
  1565. delayed_usecs, WORK_SCALE * engine->spare_usecs);
  1566. if (++consecutive_excessive_delays > 10) {
  1567. jack_error ("too many consecutive interrupt delays "
  1568. "... engine pausing");
  1569. return -1; /* will exit the thread loop */
  1570. }
  1571. jack_engine_delay (engine, delayed_usecs);
  1572. return 0;
  1573. } else {
  1574. consecutive_excessive_delays = 0;
  1575. }
  1576. if (jack_try_lock_graph (engine)) {
  1577. /* engine can't run. just throw away an entire cycle */
  1578. driver->null_cycle (driver, nframes);
  1579. return 0;
  1580. }
  1581. if (!engine->freewheeling) {
  1582. DEBUG("waiting for driver read\n");
  1583. if (driver->read (driver, nframes)) {
  1584. goto unlock;
  1585. }
  1586. }
  1587. DEBUG("run process\n");
  1588. if (jack_engine_process (engine, nframes) == 0) {
  1589. if (!engine->freewheeling) {
  1590. if (driver->write (driver, nframes)) {
  1591. goto unlock;
  1592. }
  1593. }
  1594. } else {
  1595. JSList *node;
  1596. DEBUG ("engine process cycle failed");
  1597. /* we are already late, or something else went wrong,
  1598. so it can't hurt to check the existence of all
  1599. clients.
  1600. */
  1601. for (node = engine->clients; node;
  1602. node = jack_slist_next (node)) {
  1603. jack_client_internal_t *client =
  1604. (jack_client_internal_t *) node->data;
  1605. if (client->control->type == ClientExternal) {
  1606. if (kill (client->control->pid, 0)) {
  1607. VERBOSE(engine,
  1608. "client %s has died/exited\n",
  1609. client->control->name);
  1610. client->error++;
  1611. }
  1612. }
  1613. DEBUG ("client %s errors = %d", client->control->name,
  1614. client->error);
  1615. }
  1616. }
  1617. jack_engine_post_process (engine);
  1618. if (delayed_usecs > engine->control->max_delayed_usecs)
  1619. engine->control->max_delayed_usecs = delayed_usecs;
  1620. ret = 0;
  1621. unlock:
  1622. jack_unlock_graph (engine);
  1623. DEBUG("cycle finished, status = %d", ret);
  1624. return ret;
  1625. }
  1626. static void
  1627. jack_engine_driver_exit (jack_engine_t* engine)
  1628. {
  1629. /* tell anyone waiting that the driver exited. */
  1630. kill (engine->wait_pid, SIGUSR2);
  1631. engine->driver = NULL;
  1632. }
  1633. static int
  1634. jack_run_cycle (jack_engine_t *engine, jack_nframes_t nframes,
  1635. float delayed_usecs)
  1636. {
  1637. jack_nframes_t left;
  1638. jack_nframes_t b_size = engine->control->buffer_size;
  1639. jack_frame_timer_t* timer = &engine->control->frame_timer;
  1640. if (timer->reset_pending) {
  1641. /* post xrun-handling */
  1642. jack_nframes_t period_size_guess =
  1643. engine->control->current_time.frame_rate *
  1644. ((timer->next_wakeup - timer->current_wakeup) / 1000000.0);
  1645. timer->frames +=
  1646. ((engine->driver->last_wait_ust -
  1647. engine->control->frame_timer.next_wakeup) /
  1648. period_size_guess) *
  1649. period_size_guess;
  1650. timer->current_wakeup = engine->driver->last_wait_ust;
  1651. timer->next_wakeup = engine->driver->last_wait_ust +
  1652. engine->driver->period_usecs;
  1653. timer->reset_pending = 0;
  1654. } else if (engine->first_wakeup) {
  1655. /* the first wakeup */
  1656. timer->next_wakeup =
  1657. engine->driver->last_wait_ust +
  1658. engine->driver->period_usecs;
  1659. engine->first_wakeup = 0;
  1660. } else {
  1661. /* normal condition */
  1662. jack_inc_frame_time (engine, nframes);
  1663. }
  1664. if (engine->verbose) {
  1665. if (nframes != b_size) {
  1666. VERBOSE (engine,
  1667. "late driver wakeup: nframes to process = %"
  1668. PRIu32 ".\n", nframes);
  1669. }
  1670. }
  1671. /* run as many cycles as it takes to consume nframes */
  1672. for (left = nframes; left >= b_size; left -= b_size) {
  1673. if (jack_run_one_cycle (engine, b_size, delayed_usecs)) {
  1674. jack_error ("cycle execution failure, exiting");
  1675. return EIO;
  1676. }
  1677. }
  1678. return 0;
  1679. }
  1680. void
  1681. jack_engine_delete (jack_engine_t *engine)
  1682. {
  1683. int i;
  1684. if (engine == NULL)
  1685. return;
  1686. VERBOSE (engine, "starting server engine shutdown\n");
  1687. engine->control->engine_ok = 0; /* tell clients we're going away */
  1688. /* shutdown master socket to prevent new clients arriving */
  1689. shutdown (engine->fds[0], SHUT_RDWR);
  1690. // close (engine->fds[0]);
  1691. /* now really tell them we're going away */
  1692. for (i = 0; i < engine->pfd_max; ++i) {
  1693. shutdown (engine->pfd[i].fd, SHUT_RDWR);
  1694. }
  1695. if (engine->driver) {
  1696. jack_driver_t* driver = engine->driver;
  1697. VERBOSE (engine, "stopping driver\n");
  1698. driver->stop (driver);
  1699. // VERBOSE (engine, "detaching driver\n");
  1700. // driver->detach (driver, engine);
  1701. VERBOSE (engine, "unloading driver\n");
  1702. jack_driver_unload (driver);
  1703. engine->driver = NULL;
  1704. }
  1705. VERBOSE (engine, "freeing shared port segments\n");
  1706. for (i = 0; i < engine->control->n_port_types; ++i) {
  1707. jack_release_shm (&engine->port_segment[i]);
  1708. jack_destroy_shm (&engine->port_segment[i]);
  1709. }
  1710. /* stop the other engine threads */
  1711. VERBOSE (engine, "stopping server thread\n");
  1712. #if JACK_USE_MACH_THREADS
  1713. // MacOSX pthread_cancel still not implemented correctly in Darwin
  1714. mach_port_t machThread = pthread_mach_thread_np (engine->server_thread);
  1715. thread_terminate (machThread);
  1716. #else
  1717. pthread_cancel (engine->server_thread);
  1718. pthread_join (engine->server_thread, NULL);
  1719. #endif
  1720. #ifndef JACK_USE_MACH_THREADS
  1721. /* Cancel the watchdog thread and wait for it to terminate.
  1722. *
  1723. * The watchdog thread is not used on MacOSX since CoreAudio
  1724. * drivers already contain a similar mechanism.
  1725. */
  1726. if (engine->control->real_time) {
  1727. VERBOSE (engine, "stopping watchdog thread\n");
  1728. pthread_cancel (engine->watchdog_thread);
  1729. pthread_join (engine->watchdog_thread, NULL);
  1730. }
  1731. #endif
  1732. VERBOSE (engine, "last xrun delay: %.3f usecs\n",
  1733. engine->control->xrun_delayed_usecs);
  1734. VERBOSE (engine, "max delay reported by backend: %.3f usecs\n",
  1735. engine->control->max_delayed_usecs);
  1736. /* free engine control shm segment */
  1737. engine->control = NULL;
  1738. VERBOSE (engine, "freeing engine shared memory\n");
  1739. jack_release_shm (&engine->control_shm);
  1740. jack_destroy_shm (&engine->control_shm);
  1741. VERBOSE (engine, "max usecs: %.3f, ", engine->max_usecs);
  1742. VERBOSE (engine, "engine deleted\n");
  1743. free (engine);
  1744. jack_messagebuffer_exit();
  1745. }
  1746. void
  1747. jack_port_clear_connections (jack_engine_t *engine,
  1748. jack_port_internal_t *port)
  1749. {
  1750. JSList *node, *next;
  1751. for (node = port->connections; node; ) {
  1752. next = jack_slist_next (node);
  1753. jack_port_disconnect_internal (
  1754. engine, ((jack_connection_internal_t *)
  1755. node->data)->source,
  1756. ((jack_connection_internal_t *)
  1757. node->data)->destination);
  1758. node = next;
  1759. }
  1760. jack_slist_free (port->connections);
  1761. port->connections = 0;
  1762. }
  1763. static void
  1764. jack_deliver_event_to_all (jack_engine_t *engine, jack_event_t *event)
  1765. {
  1766. JSList *node;
  1767. jack_lock_graph (engine);
  1768. for (node = engine->clients; node; node = jack_slist_next (node)) {
  1769. jack_deliver_event (engine,
  1770. (jack_client_internal_t *) node->data,
  1771. event);
  1772. }
  1773. jack_unlock_graph (engine);
  1774. }
  1775. static int
  1776. jack_deliver_event (jack_engine_t *engine, jack_client_internal_t *client,
  1777. jack_event_t *event)
  1778. {
  1779. char status;
  1780. /* caller must hold the graph lock */
  1781. DEBUG ("delivering event (type %d)", event->type);
  1782. /* we are not RT-constrained here, so use kill(2) to beef up
  1783. our check on a client's continued well-being
  1784. */
  1785. if (client->control->dead
  1786. || (client->control->type == ClientExternal
  1787. && kill (client->control->pid, 0))) {
  1788. DEBUG ("client %s is dead - no event sent",
  1789. client->control->name);
  1790. return 0;
  1791. }
  1792. DEBUG ("client %s is still alive", client->control->name);
  1793. if (jack_client_is_internal (client)) {
  1794. switch (event->type) {
  1795. case PortConnected:
  1796. case PortDisconnected:
  1797. jack_client_handle_port_connection
  1798. (client->control->private_client, event);
  1799. break;
  1800. case BufferSizeChange:
  1801. jack_client_invalidate_port_buffers
  1802. (client->control->private_client);
  1803. if (client->control->bufsize) {
  1804. client->control->bufsize
  1805. (event->x.n,
  1806. client->control->bufsize_arg);
  1807. }
  1808. break;
  1809. case SampleRateChange:
  1810. if (client->control->srate) {
  1811. client->control->srate
  1812. (event->x.n,
  1813. client->control->srate_arg);
  1814. }
  1815. break;
  1816. case GraphReordered:
  1817. if (client->control->graph_order) {
  1818. client->control->graph_order
  1819. (client->control->graph_order_arg);
  1820. }
  1821. break;
  1822. case XRun:
  1823. if (client->control->xrun) {
  1824. client->control->xrun
  1825. (client->control->xrun_arg);
  1826. }
  1827. break;
  1828. default:
  1829. /* internal clients don't need to know */
  1830. break;
  1831. }
  1832. } else {
  1833. if (client->control->active) {
  1834. /* there's a thread waiting for events, so
  1835. * it's worth telling the client */
  1836. DEBUG ("engine writing on event fd");
  1837. if (write (client->event_fd, event, sizeof (*event))
  1838. != sizeof (*event)) {
  1839. jack_error ("cannot send event to client [%s]"
  1840. " (%s)", client->control->name,
  1841. strerror (errno));
  1842. client->error++;
  1843. }
  1844. DEBUG ("engine reading from event fd");
  1845. if (!client->error &&
  1846. (read (client->event_fd, &status, sizeof (status))
  1847. != sizeof (status))) {
  1848. jack_error ("cannot read event response from "
  1849. "client [%s] (%s)",
  1850. client->control->name,
  1851. strerror (errno));
  1852. client->error++;
  1853. }
  1854. if (status != 0) {
  1855. jack_error ("bad status for client event "
  1856. "handling (type = %d)",
  1857. event->type);
  1858. client->error++;
  1859. }
  1860. }
  1861. }
  1862. DEBUG ("event delivered");
  1863. return 0;
  1864. }
  1865. int
  1866. jack_rechain_graph (jack_engine_t *engine)
  1867. {
  1868. JSList *node, *next;
  1869. unsigned long n;
  1870. int err = 0;
  1871. jack_client_internal_t *client, *subgraph_client, *next_client;
  1872. jack_event_t event;
  1873. int upstream_is_jackd;
  1874. jack_clear_fifos (engine);
  1875. subgraph_client = 0;
  1876. VERBOSE(engine, "++ jack_rechain_graph():\n");
  1877. event.type = GraphReordered;
  1878. for (n = 0, node = engine->clients, next = NULL; node; node = next) {
  1879. next = jack_slist_next (node);
  1880. if (((jack_client_internal_t *) node->data)->control->active) {
  1881. client = (jack_client_internal_t *) node->data;
  1882. /* find the next active client. its ok for
  1883. * this to be NULL */
  1884. while (next) {
  1885. if (((jack_client_internal_t *)
  1886. next->data)->control->active) {
  1887. break;
  1888. }
  1889. next = jack_slist_next (next);
  1890. };
  1891. if (next == NULL) {
  1892. next_client = NULL;
  1893. } else {
  1894. next_client = (jack_client_internal_t *)
  1895. next->data;
  1896. }
  1897. client->execution_order = n;
  1898. client->next_client = next_client;
  1899. if (jack_client_is_internal (client)) {
  1900. /* break the chain for the current
  1901. * subgraph. the server will wait for
  1902. * chain on the nth FIFO, and will
  1903. * then execute this internal
  1904. * client. */
  1905. if (subgraph_client) {
  1906. subgraph_client->subgraph_wait_fd =
  1907. jack_get_fifo_fd (engine, n);
  1908. VERBOSE (engine, "client %s: wait_fd="
  1909. "%d, execution_order="
  1910. "%lu.\n",
  1911. subgraph_client->
  1912. control->name,
  1913. subgraph_client->
  1914. subgraph_wait_fd, n);
  1915. n++;
  1916. }
  1917. VERBOSE (engine, "client %s: internal "
  1918. "client, execution_order="
  1919. "%lu.\n",
  1920. client->control->name, n);
  1921. /* this does the right thing for
  1922. * internal clients too
  1923. */
  1924. jack_deliver_event (engine, client, &event);
  1925. subgraph_client = 0;
  1926. } else {
  1927. if (subgraph_client == NULL) {
  1928. /* start a new subgraph. the
  1929. * engine will start the chain
  1930. * by writing to the nth
  1931. * FIFO.
  1932. */
  1933. subgraph_client = client;
  1934. subgraph_client->subgraph_start_fd =
  1935. jack_get_fifo_fd (engine, n);
  1936. VERBOSE (engine, "client %s: "
  1937. "start_fd=%d, execution"
  1938. "_order=%lu.\n",
  1939. subgraph_client->
  1940. control->name,
  1941. subgraph_client->
  1942. subgraph_start_fd, n);
  1943. /* this external client after
  1944. this will have jackd as its
  1945. upstream connection.
  1946. */
  1947. upstream_is_jackd = 1;
  1948. }
  1949. else {
  1950. VERBOSE (engine, "client %s: in"
  1951. " subgraph after %s, "
  1952. "execution_order="
  1953. "%lu.\n",
  1954. client->control->name,
  1955. subgraph_client->
  1956. control->name, n);
  1957. subgraph_client->subgraph_wait_fd = -1;
  1958. /* this external client after
  1959. this will have another
  1960. client as its upstream
  1961. connection.
  1962. */
  1963. upstream_is_jackd = 0;
  1964. }
  1965. /* make sure fifo for 'n + 1' exists
  1966. * before issuing client reorder
  1967. */
  1968. (void) jack_get_fifo_fd(
  1969. engine, client->execution_order + 1);
  1970. event.x.n = client->execution_order;
  1971. event.y.n = upstream_is_jackd;
  1972. jack_deliver_event (engine, client, &event);
  1973. n++;
  1974. }
  1975. }
  1976. }
  1977. if (subgraph_client) {
  1978. subgraph_client->subgraph_wait_fd =
  1979. jack_get_fifo_fd (engine, n);
  1980. VERBOSE (engine, "client %s: wait_fd=%d, "
  1981. "execution_order=%lu (last client).\n",
  1982. subgraph_client->control->name,
  1983. subgraph_client->subgraph_wait_fd, n);
  1984. }
  1985. VERBOSE (engine, "-- jack_rechain_graph()\n");
  1986. return err;
  1987. }
  1988. static jack_nframes_t
  1989. jack_get_port_total_latency (jack_engine_t *engine,
  1990. jack_port_internal_t *port, int hop_count,
  1991. int toward_port)
  1992. {
  1993. JSList *node;
  1994. jack_nframes_t latency;
  1995. jack_nframes_t max_latency = 0;
  1996. /* call tree must hold engine->client_lock. */
  1997. latency = port->shared->latency;
  1998. /* we don't prevent cyclic graphs, so we have to do something
  1999. to bottom out in the event that they are created.
  2000. */
  2001. if (hop_count > 8) {
  2002. return latency;
  2003. }
  2004. for (node = port->connections; node; node = jack_slist_next (node)) {
  2005. jack_nframes_t this_latency;
  2006. jack_connection_internal_t *connection;
  2007. connection = (jack_connection_internal_t *) node->data;
  2008. if ((toward_port &&
  2009. (connection->source->shared == port->shared)) ||
  2010. (!toward_port &&
  2011. (connection->destination->shared == port->shared))) {
  2012. continue;
  2013. }
  2014. /* if we're a destination in the connection, recurse
  2015. on the source to get its total latency
  2016. */
  2017. if (connection->destination == port) {
  2018. if (connection->source->shared->flags
  2019. & JackPortIsTerminal) {
  2020. this_latency = connection->source->
  2021. shared->latency;
  2022. } else {
  2023. this_latency =
  2024. jack_get_port_total_latency (
  2025. engine, connection->source,
  2026. hop_count + 1,
  2027. toward_port);
  2028. }
  2029. } else {
  2030. /* "port" is the source, so get the latency of
  2031. * the destination */
  2032. if (connection->destination->shared->flags
  2033. & JackPortIsTerminal) {
  2034. this_latency = connection->destination->
  2035. shared->latency;
  2036. } else {
  2037. this_latency =
  2038. jack_get_port_total_latency (
  2039. engine,
  2040. connection->destination,
  2041. hop_count + 1,
  2042. toward_port);
  2043. }
  2044. }
  2045. if (this_latency > max_latency) {
  2046. max_latency = this_latency;
  2047. }
  2048. }
  2049. return latency + max_latency;
  2050. }
  2051. static void
  2052. jack_compute_all_port_total_latencies (jack_engine_t *engine)
  2053. {
  2054. jack_port_shared_t *shared = engine->control->ports;
  2055. unsigned int i;
  2056. int toward_port;
  2057. for (i = 0; i < engine->control->port_max; i++) {
  2058. if (shared[i].in_use) {
  2059. if (shared[i].flags & JackPortIsOutput) {
  2060. toward_port = FALSE;
  2061. } else {
  2062. toward_port = TRUE;
  2063. }
  2064. shared[i].total_latency =
  2065. jack_get_port_total_latency (
  2066. engine, &engine->internal_ports[i],
  2067. 0, toward_port);
  2068. }
  2069. }
  2070. }
  2071. /* How the sort works:
  2072. *
  2073. * Each client has a "sortfeeds" list of clients indicating which clients
  2074. * it should be considered as feeding for the purposes of sorting the
  2075. * graph. This list differs from the clients it /actually/ feeds in the
  2076. * following ways:
  2077. *
  2078. * 1. Connections from a client to itself are disregarded
  2079. *
  2080. * 2. Connections to a driver client are disregarded
  2081. *
  2082. * 3. If a connection from A to B is a feedback connection (ie there was
  2083. * already a path from B to A when the connection was made) then instead
  2084. * of B appearing on A's sortfeeds list, A will appear on B's sortfeeds
  2085. * list.
  2086. *
  2087. * If client A is on client B's sortfeeds list, client A must come after
  2088. * client B in the execution order. The above 3 rules ensure that the
  2089. * sortfeeds relation is always acyclic so that all ordering constraints
  2090. * can actually be met.
  2091. *
  2092. * Each client also has a "truefeeds" list which is the same as sortfeeds
  2093. * except that feedback connections appear normally instead of reversed.
  2094. * This is used to detect whether the graph has become acyclic.
  2095. *
  2096. */
  2097. void
  2098. jack_sort_graph (jack_engine_t *engine)
  2099. {
  2100. /* called, obviously, must hold engine->client_lock */
  2101. engine->clients = jack_slist_sort (engine->clients,
  2102. (JCompareFunc) jack_client_sort);
  2103. jack_compute_all_port_total_latencies (engine);
  2104. jack_rechain_graph (engine);
  2105. }
  2106. static int
  2107. jack_client_sort (jack_client_internal_t *a, jack_client_internal_t *b)
  2108. {
  2109. /* drivers are forced to the front, ie considered as sources
  2110. rather than sinks for purposes of the sort */
  2111. if (jack_client_feeds_transitive (a, b) ||
  2112. (a->control->type == ClientDriver &&
  2113. b->control->type != ClientDriver)) {
  2114. return -1;
  2115. } else if (jack_client_feeds_transitive (b, a) ||
  2116. (b->control->type == ClientDriver &&
  2117. a->control->type != ClientDriver)) {
  2118. return 1;
  2119. } else {
  2120. return 0;
  2121. }
  2122. }
  2123. /* transitive closure of the relation expressed by the sortfeeds lists. */
  2124. static int
  2125. jack_client_feeds_transitive (jack_client_internal_t *source,
  2126. jack_client_internal_t *dest )
  2127. {
  2128. jack_client_internal_t *med;
  2129. JSList *node;
  2130. if (jack_slist_find (source->sortfeeds, dest)) {
  2131. return 1;
  2132. }
  2133. for (node = source->sortfeeds; node; node = jack_slist_next (node)) {
  2134. med = (jack_client_internal_t *) node->data;
  2135. if (jack_client_feeds_transitive (med, dest)) {
  2136. return 1;
  2137. }
  2138. }
  2139. return 0;
  2140. }
  2141. /**
  2142. * Checks whether the graph has become acyclic and if so modifies client
  2143. * sortfeeds lists to turn leftover feedback connections into normal ones.
  2144. * This lowers latency, but at the expense of some data corruption.
  2145. */
  2146. static void
  2147. jack_check_acyclic (jack_engine_t *engine)
  2148. {
  2149. JSList *srcnode, *dstnode, *portnode, *connnode;
  2150. jack_client_internal_t *src, *dst;
  2151. jack_port_internal_t *port;
  2152. jack_connection_internal_t *conn;
  2153. int stuck;
  2154. int unsortedclients = 0;
  2155. VERBOSE (engine, "checking for graph become acyclic\n");
  2156. for (srcnode = engine->clients; srcnode;
  2157. srcnode = jack_slist_next (srcnode)) {
  2158. src = (jack_client_internal_t *) srcnode->data;
  2159. src->tfedcount = src->fedcount;
  2160. unsortedclients++;
  2161. }
  2162. stuck = FALSE;
  2163. /* find out whether a normal sort would have been possible */
  2164. while (unsortedclients && !stuck) {
  2165. stuck = TRUE;
  2166. for (srcnode = engine->clients; srcnode;
  2167. srcnode = jack_slist_next (srcnode)) {
  2168. src = (jack_client_internal_t *) srcnode->data;
  2169. if (!src->tfedcount) {
  2170. stuck = FALSE;
  2171. unsortedclients--;
  2172. src->tfedcount = -1;
  2173. for (dstnode = src->truefeeds; dstnode;
  2174. dstnode = jack_slist_next (dstnode)) {
  2175. dst = (jack_client_internal_t *)
  2176. dstnode->data;
  2177. dst->tfedcount--;
  2178. }
  2179. }
  2180. }
  2181. }
  2182. if (stuck) {
  2183. VERBOSE (engine, "graph is still cyclic\n" );
  2184. } else {
  2185. VERBOSE (engine, "graph has become acyclic\n");
  2186. /* turn feedback connections around in sortfeeds */
  2187. for (srcnode = engine->clients; srcnode;
  2188. srcnode = jack_slist_next (srcnode)) {
  2189. src = (jack_client_internal_t *) srcnode->data;
  2190. for (portnode = src->ports; portnode;
  2191. portnode = jack_slist_next (portnode)) {
  2192. port = (jack_port_internal_t *) portnode->data;
  2193. for (connnode = port->connections; connnode;
  2194. connnode = jack_slist_next (connnode)) {
  2195. conn = (jack_connection_internal_t*)
  2196. connnode->data;
  2197. if (conn->dir == -1 )
  2198. /*&&
  2199. conn->srcclient == src) */{
  2200. VERBOSE (engine,
  2201. "reversing connection from "
  2202. "%s to %s\n",
  2203. conn->srcclient->control->name,
  2204. conn->dstclient->control->name);
  2205. conn->dir = 1;
  2206. conn->dstclient->sortfeeds =
  2207. jack_slist_remove
  2208. (conn->dstclient->sortfeeds,
  2209. conn->srcclient);
  2210. conn->srcclient->sortfeeds =
  2211. jack_slist_prepend
  2212. (conn->srcclient->sortfeeds,
  2213. conn->dstclient );
  2214. }
  2215. }
  2216. }
  2217. }
  2218. engine->feedbackcount = 0;
  2219. }
  2220. }
  2221. /**
  2222. * Dumps current engine configuration to stderr.
  2223. */
  2224. void jack_dump_configuration(jack_engine_t *engine, int take_lock)
  2225. {
  2226. JSList *clientnode, *portnode, *connectionnode;
  2227. jack_client_internal_t *client;
  2228. jack_client_control_t *ctl;
  2229. jack_port_internal_t *port;
  2230. jack_connection_internal_t* connection;
  2231. int n, m, o;
  2232. fprintf(stderr, "engine.c: <-- dump begins -->\n");
  2233. if (take_lock) {
  2234. jack_lock_graph (engine);
  2235. }
  2236. for (n = 0, clientnode = engine->clients; clientnode;
  2237. clientnode = jack_slist_next (clientnode)) {
  2238. client = (jack_client_internal_t *) clientnode->data;
  2239. ctl = client->control;
  2240. fprintf (stderr, "client #%d: %s (type: %d, process? %s,"
  2241. " start=%d wait=%d\n",
  2242. ++n,
  2243. ctl->name,
  2244. ctl->type,
  2245. ctl->process ? "yes" : "no",
  2246. client->subgraph_start_fd,
  2247. client->subgraph_wait_fd);
  2248. for(m = 0, portnode = client->ports; portnode;
  2249. portnode = jack_slist_next (portnode)) {
  2250. port = (jack_port_internal_t *) portnode->data;
  2251. fprintf(stderr, "\t port #%d: %s\n", ++m,
  2252. port->shared->name);
  2253. for(o = 0, connectionnode = port->connections;
  2254. connectionnode;
  2255. connectionnode =
  2256. jack_slist_next (connectionnode)) {
  2257. connection = (jack_connection_internal_t *)
  2258. connectionnode->data;
  2259. fprintf(stderr, "\t\t connection #%d: %s %s\n",
  2260. ++o,
  2261. (port->shared->flags
  2262. & JackPortIsInput)? "<-": "->",
  2263. (port->shared->flags & JackPortIsInput)?
  2264. connection->source->shared->name:
  2265. connection->destination->shared->name);
  2266. }
  2267. }
  2268. }
  2269. if (take_lock) {
  2270. jack_unlock_graph (engine);
  2271. }
  2272. fprintf(stderr, "engine.c: <-- dump ends -->\n");
  2273. }
  2274. static int
  2275. jack_port_do_connect (jack_engine_t *engine,
  2276. const char *source_port,
  2277. const char *destination_port)
  2278. {
  2279. jack_connection_internal_t *connection;
  2280. jack_port_internal_t *srcport, *dstport;
  2281. jack_port_id_t src_id, dst_id;
  2282. jack_client_internal_t *srcclient, *dstclient;
  2283. JSList *it;
  2284. if ((srcport = jack_get_port_by_name (engine, source_port)) == NULL) {
  2285. jack_error ("unknown source port in attempted connection [%s]",
  2286. source_port);
  2287. return -1;
  2288. }
  2289. if ((dstport = jack_get_port_by_name (engine, destination_port))
  2290. == NULL) {
  2291. jack_error ("unknown destination port in attempted connection"
  2292. " [%s]", destination_port);
  2293. return -1;
  2294. }
  2295. if ((dstport->shared->flags & JackPortIsInput) == 0) {
  2296. jack_error ("destination port in attempted connection of"
  2297. " %s and %s is not an input port",
  2298. source_port, destination_port);
  2299. return -1;
  2300. }
  2301. if ((srcport->shared->flags & JackPortIsOutput) == 0) {
  2302. jack_error ("source port in attempted connection of %s and"
  2303. " %s is not an output port",
  2304. source_port, destination_port);
  2305. return -1;
  2306. }
  2307. if (srcport->shared->locked) {
  2308. jack_error ("source port %s is locked against connection"
  2309. " changes", source_port);
  2310. return -1;
  2311. }
  2312. if (dstport->shared->locked) {
  2313. jack_error ("destination port %s is locked against connection"
  2314. " changes", destination_port);
  2315. return -1;
  2316. }
  2317. if (srcport->shared->ptype_id != dstport->shared->ptype_id) {
  2318. jack_error ("ports used in attemped connection are not of "
  2319. "the same data type");
  2320. return -1;
  2321. }
  2322. if ((srcclient = jack_client_internal_by_id (engine,
  2323. srcport->shared->client_id))
  2324. == 0) {
  2325. jack_error ("unknown client set as owner of port - "
  2326. "cannot connect");
  2327. return -1;
  2328. }
  2329. if (!srcclient->control->active) {
  2330. jack_error ("cannot connect ports owned by inactive clients;"
  2331. " \"%s\" is not active", srcclient->control->name);
  2332. return -1;
  2333. }
  2334. if ((dstclient = jack_client_internal_by_id (engine,
  2335. dstport->shared->client_id))
  2336. == 0) {
  2337. jack_error ("unknown client set as owner of port - cannot "
  2338. "connect");
  2339. return -1;
  2340. }
  2341. if (!dstclient->control->active) {
  2342. jack_error ("cannot connect ports owned by inactive clients;"
  2343. " \"%s\" is not active", dstclient->control->name);
  2344. return -1;
  2345. }
  2346. for (it = srcport->connections; it; it = it->next) {
  2347. if (((jack_connection_internal_t *)it->data)->destination
  2348. == dstport) {
  2349. return EEXIST;
  2350. }
  2351. }
  2352. connection = (jack_connection_internal_t *)
  2353. malloc (sizeof (jack_connection_internal_t));
  2354. connection->source = srcport;
  2355. connection->destination = dstport;
  2356. connection->srcclient = srcclient;
  2357. connection->dstclient = dstclient;
  2358. src_id = srcport->shared->id;
  2359. dst_id = dstport->shared->id;
  2360. jack_lock_graph (engine);
  2361. if (dstport->connections && !dstport->shared->has_mixdown) {
  2362. jack_port_type_info_t *port_type =
  2363. jack_port_type_info (engine, dstport);
  2364. jack_error ("cannot make multiple connections to a port of"
  2365. " type [%s]", port_type->type_name);
  2366. free (connection);
  2367. jack_unlock_graph (engine);
  2368. return -1;
  2369. } else {
  2370. if (dstclient->control->type == ClientDriver)
  2371. {
  2372. /* Ignore output connections to drivers for purposes
  2373. of sorting. Drivers are executed first in the sort
  2374. order anyway, and we don't want to treat graphs
  2375. such as driver -> client -> driver as containing
  2376. feedback */
  2377. VERBOSE (engine,
  2378. "connect %s and %s (output)\n",
  2379. srcport->shared->name,
  2380. dstport->shared->name);
  2381. connection->dir = 1;
  2382. }
  2383. else if (srcclient != dstclient) {
  2384. srcclient->truefeeds = jack_slist_prepend
  2385. (srcclient->truefeeds, dstclient);
  2386. dstclient->fedcount++;
  2387. if (jack_client_feeds_transitive (dstclient,
  2388. srcclient ) ||
  2389. (dstclient->control->type == ClientDriver &&
  2390. srcclient->control->type != ClientDriver)) {
  2391. /* dest is running before source so
  2392. this is a feedback connection */
  2393. VERBOSE (engine,
  2394. "connect %s and %s (feedback)\n",
  2395. srcport->shared->name,
  2396. dstport->shared->name);
  2397. dstclient->sortfeeds = jack_slist_prepend
  2398. (dstclient->sortfeeds, srcclient);
  2399. connection->dir = -1;
  2400. engine->feedbackcount++;
  2401. VERBOSE (engine,
  2402. "feedback count up to %d\n",
  2403. engine->feedbackcount);
  2404. } else {
  2405. /* this is not a feedback connection */
  2406. VERBOSE (engine,
  2407. "connect %s and %s (forward)\n",
  2408. srcport->shared->name,
  2409. dstport->shared->name);
  2410. srcclient->sortfeeds = jack_slist_prepend
  2411. (srcclient->sortfeeds, dstclient);
  2412. connection->dir = 1;
  2413. }
  2414. }
  2415. else
  2416. {
  2417. /* this is a connection to self */
  2418. VERBOSE (engine,
  2419. "connect %s and %s (self)\n",
  2420. srcport->shared->name,
  2421. dstport->shared->name);
  2422. connection->dir = 0;
  2423. }
  2424. dstport->connections =
  2425. jack_slist_prepend (dstport->connections, connection);
  2426. srcport->connections =
  2427. jack_slist_prepend (srcport->connections, connection);
  2428. DEBUG ("actually sorted the graph...");
  2429. jack_send_connection_notification (engine,
  2430. srcport->shared->client_id,
  2431. src_id, dst_id, TRUE);
  2432. jack_send_connection_notification (engine,
  2433. dstport->shared->client_id,
  2434. dst_id, src_id, TRUE);
  2435. jack_sort_graph (engine);
  2436. }
  2437. jack_unlock_graph (engine);
  2438. return 0;
  2439. }
  2440. int
  2441. jack_port_disconnect_internal (jack_engine_t *engine,
  2442. jack_port_internal_t *srcport,
  2443. jack_port_internal_t *dstport )
  2444. {
  2445. JSList *node;
  2446. jack_connection_internal_t *connect;
  2447. int ret = -1;
  2448. jack_port_id_t src_id, dst_id;
  2449. int check_acyclic = engine->feedbackcount;
  2450. /* call tree **** MUST HOLD **** engine->client_lock. */
  2451. for (node = srcport->connections; node;
  2452. node = jack_slist_next (node)) {
  2453. connect = (jack_connection_internal_t *) node->data;
  2454. if (connect->source == srcport &&
  2455. connect->destination == dstport) {
  2456. VERBOSE (engine, "DIS-connect %s and %s\n",
  2457. srcport->shared->name,
  2458. dstport->shared->name);
  2459. srcport->connections =
  2460. jack_slist_remove (srcport->connections,
  2461. connect);
  2462. dstport->connections =
  2463. jack_slist_remove (dstport->connections,
  2464. connect);
  2465. src_id = srcport->shared->id;
  2466. dst_id = dstport->shared->id;
  2467. /* this is a bit harsh, but it basically says
  2468. that if we actually do a disconnect, and
  2469. its the last one, then make sure that any
  2470. input monitoring is turned off on the
  2471. srcport. this isn't ideal for all
  2472. situations, but it works better for most of
  2473. them.
  2474. */
  2475. if (srcport->connections == NULL) {
  2476. srcport->shared->monitor_requests = 0;
  2477. }
  2478. jack_send_connection_notification (
  2479. engine, srcport->shared->client_id, src_id,
  2480. dst_id, FALSE);
  2481. jack_send_connection_notification (
  2482. engine, dstport->shared->client_id, dst_id,
  2483. src_id, FALSE);
  2484. if (connect->dir) {
  2485. jack_client_internal_t *src;
  2486. jack_client_internal_t *dst;
  2487. src = jack_client_internal_by_id
  2488. (engine, srcport->shared->client_id);
  2489. dst = jack_client_internal_by_id
  2490. (engine, dstport->shared->client_id);
  2491. src->truefeeds = jack_slist_remove
  2492. (src->truefeeds, dst);
  2493. dst->fedcount--;
  2494. if (connect->dir == 1) {
  2495. /* normal connection: remove dest from
  2496. source's sortfeeds list */
  2497. src->sortfeeds = jack_slist_remove
  2498. (src->sortfeeds, dst);
  2499. } else {
  2500. /* feedback connection: remove source
  2501. from dest's sortfeeds list */
  2502. dst->sortfeeds = jack_slist_remove
  2503. (dst->sortfeeds, src);
  2504. engine->feedbackcount--;
  2505. VERBOSE (engine,
  2506. "feedback count down to %d\n",
  2507. engine->feedbackcount);
  2508. }
  2509. } /* else self-connection: do nothing */
  2510. free (connect);
  2511. ret = 0;
  2512. break;
  2513. }
  2514. }
  2515. if (check_acyclic) {
  2516. jack_check_acyclic (engine);
  2517. }
  2518. jack_sort_graph (engine);
  2519. return ret;
  2520. }
  2521. static int
  2522. jack_port_do_disconnect_all (jack_engine_t *engine,
  2523. jack_port_id_t port_id)
  2524. {
  2525. if (port_id >= engine->control->port_max) {
  2526. jack_error ("illegal port ID in attempted disconnection [%"
  2527. PRIu32 "]", port_id);
  2528. return -1;
  2529. }
  2530. VERBOSE (engine, "clear connections for %s\n",
  2531. engine->internal_ports[port_id].shared->name);
  2532. jack_lock_graph (engine);
  2533. jack_port_clear_connections (engine, &engine->internal_ports[port_id]);
  2534. jack_sort_graph (engine);
  2535. jack_unlock_graph (engine);
  2536. return 0;
  2537. }
  2538. static int
  2539. jack_port_do_disconnect (jack_engine_t *engine,
  2540. const char *source_port,
  2541. const char *destination_port)
  2542. {
  2543. jack_port_internal_t *srcport, *dstport;
  2544. int ret = -1;
  2545. if ((srcport = jack_get_port_by_name (engine, source_port)) == NULL) {
  2546. jack_error ("unknown source port in attempted disconnection"
  2547. " [%s]", source_port);
  2548. return -1;
  2549. }
  2550. if ((dstport = jack_get_port_by_name (engine, destination_port))
  2551. == NULL) {
  2552. jack_error ("unknown destination port in attempted"
  2553. " disconnection [%s]", destination_port);
  2554. return -1;
  2555. }
  2556. jack_lock_graph (engine);
  2557. ret = jack_port_disconnect_internal (engine, srcport, dstport);
  2558. jack_unlock_graph (engine);
  2559. return ret;
  2560. }
  2561. int
  2562. jack_get_fifo_fd (jack_engine_t *engine, unsigned int which_fifo)
  2563. {
  2564. /* caller must hold client_lock */
  2565. char path[PATH_MAX+1];
  2566. struct stat statbuf;
  2567. snprintf (path, sizeof (path), "%s-%d", engine->fifo_prefix,
  2568. which_fifo);
  2569. DEBUG ("%s", path);
  2570. if (stat (path, &statbuf)) {
  2571. if (errno == ENOENT) {
  2572. if (mkfifo(path, 0666) < 0){
  2573. jack_error ("cannot create inter-client FIFO"
  2574. " [%s] (%s)\n", path,
  2575. strerror (errno));
  2576. return -1;
  2577. }
  2578. } else {
  2579. jack_error ("cannot check on FIFO %d\n", which_fifo);
  2580. return -1;
  2581. }
  2582. } else {
  2583. if (!S_ISFIFO(statbuf.st_mode)) {
  2584. jack_error ("FIFO %d (%s) already exists, but is not"
  2585. " a FIFO!\n", which_fifo, path);
  2586. return -1;
  2587. }
  2588. }
  2589. if (which_fifo >= engine->fifo_size) {
  2590. unsigned int i;
  2591. engine->fifo = (int *)
  2592. realloc (engine->fifo,
  2593. sizeof (int) * (engine->fifo_size + 16));
  2594. for (i = engine->fifo_size; i < engine->fifo_size + 16; i++) {
  2595. engine->fifo[i] = -1;
  2596. }
  2597. engine->fifo_size += 16;
  2598. }
  2599. if (engine->fifo[which_fifo] < 0) {
  2600. if ((engine->fifo[which_fifo] =
  2601. open (path, O_RDWR|O_CREAT|O_NONBLOCK, 0666)) < 0) {
  2602. jack_error ("cannot open fifo [%s] (%s)", path,
  2603. strerror (errno));
  2604. return -1;
  2605. }
  2606. DEBUG ("opened engine->fifo[%d] == %d (%s)",
  2607. which_fifo, engine->fifo[which_fifo], path);
  2608. }
  2609. return engine->fifo[which_fifo];
  2610. }
  2611. static void
  2612. jack_clear_fifos (jack_engine_t *engine)
  2613. {
  2614. /* caller must hold client_lock */
  2615. unsigned int i;
  2616. char buf[16];
  2617. /* this just drains the existing FIFO's of any data left in
  2618. them by aborted clients, etc. there is only ever going to
  2619. be 0, 1 or 2 bytes in them, but we'll allow for up to 16.
  2620. */
  2621. for (i = 0; i < engine->fifo_size; i++) {
  2622. if (engine->fifo[i] >= 0) {
  2623. int nread = read (engine->fifo[i], buf, sizeof (buf));
  2624. if (nread < 0 && errno != EAGAIN) {
  2625. jack_error ("clear fifo[%d] error: %s",
  2626. i, strerror (errno));
  2627. }
  2628. }
  2629. }
  2630. }
  2631. static int
  2632. jack_use_driver (jack_engine_t *engine, jack_driver_t *driver)
  2633. {
  2634. if (engine->driver) {
  2635. engine->driver->detach (engine->driver, engine);
  2636. engine->driver = 0;
  2637. }
  2638. if (driver) {
  2639. if (driver->attach (driver, engine))
  2640. return -1;
  2641. engine->rolling_interval =
  2642. jack_rolling_interval (driver->period_usecs);
  2643. }
  2644. engine->driver = driver;
  2645. return 0;
  2646. }
  2647. /* PORT RELATED FUNCTIONS */
  2648. static jack_port_id_t
  2649. jack_get_free_port (jack_engine_t *engine)
  2650. {
  2651. jack_port_id_t i;
  2652. pthread_mutex_lock (&engine->port_lock);
  2653. for (i = 0; i < engine->port_max; i++) {
  2654. if (engine->control->ports[i].in_use == 0) {
  2655. engine->control->ports[i].in_use = 1;
  2656. break;
  2657. }
  2658. }
  2659. pthread_mutex_unlock (&engine->port_lock);
  2660. if (i == engine->port_max) {
  2661. return (jack_port_id_t) -1;
  2662. }
  2663. return i;
  2664. }
  2665. void
  2666. jack_port_release (jack_engine_t *engine, jack_port_internal_t *port)
  2667. {
  2668. pthread_mutex_lock (&engine->port_lock);
  2669. port->shared->in_use = 0;
  2670. if (port->buffer_info) {
  2671. jack_port_buffer_list_t *blist =
  2672. jack_port_buffer_list (engine, port);
  2673. pthread_mutex_lock (&blist->lock);
  2674. blist->freelist =
  2675. jack_slist_prepend (blist->freelist,
  2676. port->buffer_info);
  2677. port->buffer_info = NULL;
  2678. pthread_mutex_unlock (&blist->lock);
  2679. }
  2680. pthread_mutex_unlock (&engine->port_lock);
  2681. }
  2682. jack_port_internal_t *
  2683. jack_get_port_internal_by_name (jack_engine_t *engine, const char *name)
  2684. {
  2685. jack_port_id_t id;
  2686. pthread_mutex_lock (&engine->port_lock);
  2687. for (id = 0; id < engine->port_max; id++) {
  2688. if (strcmp (engine->control->ports[id].name, name) == 0) {
  2689. break;
  2690. }
  2691. }
  2692. pthread_mutex_unlock (&engine->port_lock);
  2693. if (id != engine->port_max) {
  2694. return &engine->internal_ports[id];
  2695. } else {
  2696. return NULL;
  2697. }
  2698. }
  2699. int
  2700. jack_port_do_register (jack_engine_t *engine, jack_request_t *req)
  2701. {
  2702. jack_port_id_t port_id;
  2703. jack_port_shared_t *shared;
  2704. jack_port_internal_t *port;
  2705. jack_client_internal_t *client;
  2706. unsigned long i;
  2707. for (i = 0; i < engine->control->n_port_types; ++i) {
  2708. if (strcmp (req->x.port_info.type,
  2709. engine->control->port_types[i].type_name) == 0) {
  2710. break;
  2711. }
  2712. }
  2713. if (i == engine->control->n_port_types) {
  2714. jack_error ("cannot register a port of type \"%s\"",
  2715. req->x.port_info.type);
  2716. return -1;
  2717. }
  2718. jack_lock_graph (engine);
  2719. if ((client = jack_client_internal_by_id (engine,
  2720. req->x.port_info.client_id))
  2721. == NULL) {
  2722. jack_error ("unknown client id in port registration request");
  2723. return -1;
  2724. }
  2725. jack_unlock_graph (engine);
  2726. if ((port_id = jack_get_free_port (engine)) == (jack_port_id_t) -1) {
  2727. jack_error ("no ports available!");
  2728. return -1;
  2729. }
  2730. shared = &engine->control->ports[port_id];
  2731. strcpy (shared->name, req->x.port_info.name);
  2732. shared->ptype_id = engine->control->port_types[i].ptype_id;
  2733. shared->client_id = req->x.port_info.client_id;
  2734. shared->flags = req->x.port_info.flags;
  2735. shared->latency = 0;
  2736. shared->monitor_requests = 0;
  2737. shared->locked = 0;
  2738. port = &engine->internal_ports[port_id];
  2739. port->shared = shared;
  2740. port->connections = 0;
  2741. port->buffer_info = NULL;
  2742. if (jack_port_assign_buffer (engine, port)) {
  2743. jack_error ("cannot assign buffer for port");
  2744. return -1;
  2745. }
  2746. jack_lock_graph (engine);
  2747. client->ports = jack_slist_prepend (client->ports, port);
  2748. jack_port_registration_notify (engine, port_id, TRUE);
  2749. jack_unlock_graph (engine);
  2750. VERBOSE (engine, "registered port %s, offset = %u\n",
  2751. shared->name, (unsigned int)shared->offset);
  2752. req->x.port_info.port_id = port_id;
  2753. return 0;
  2754. }
  2755. int
  2756. jack_port_do_unregister (jack_engine_t *engine, jack_request_t *req)
  2757. {
  2758. jack_client_internal_t *client;
  2759. jack_port_shared_t *shared;
  2760. jack_port_internal_t *port;
  2761. if (req->x.port_info.port_id < 0 ||
  2762. req->x.port_info.port_id > engine->port_max) {
  2763. jack_error ("invalid port ID %" PRIu32
  2764. " in unregister request",
  2765. req->x.port_info.port_id);
  2766. return -1;
  2767. }
  2768. shared = &engine->control->ports[req->x.port_info.port_id];
  2769. if (shared->client_id != req->x.port_info.client_id) {
  2770. jack_error ("Client %" PRIu32
  2771. " is not allowed to remove port %s",
  2772. req->x.port_info.client_id, shared->name);
  2773. return -1;
  2774. }
  2775. jack_lock_graph (engine);
  2776. if ((client = jack_client_internal_by_id (engine, shared->client_id))
  2777. == NULL) {
  2778. jack_error ("unknown client id in port registration request");
  2779. jack_unlock_graph (engine);
  2780. return -1;
  2781. }
  2782. port = &engine->internal_ports[req->x.port_info.port_id];
  2783. jack_port_clear_connections (engine, port);
  2784. jack_port_release (engine,
  2785. &engine->internal_ports[req->x.port_info.port_id]);
  2786. client->ports = jack_slist_remove (client->ports, port);
  2787. jack_port_registration_notify (engine, req->x.port_info.port_id,
  2788. FALSE);
  2789. jack_unlock_graph (engine);
  2790. return 0;
  2791. }
  2792. int
  2793. jack_do_get_port_connections (jack_engine_t *engine, jack_request_t *req,
  2794. int reply_fd)
  2795. {
  2796. jack_port_internal_t *port;
  2797. JSList *node;
  2798. unsigned int i;
  2799. int ret = -1;
  2800. int internal = FALSE;
  2801. jack_lock_graph (engine);
  2802. port = &engine->internal_ports[req->x.port_info.port_id];
  2803. DEBUG ("Getting connections for port '%s'.", port->shared->name);
  2804. req->x.port_connections.nports = jack_slist_length (port->connections);
  2805. req->status = 0;
  2806. /* figure out if this is an internal or external client */
  2807. for (node = engine->clients; node; node = jack_slist_next (node)) {
  2808. if (((jack_client_internal_t *) node->data)->request_fd
  2809. == reply_fd) {
  2810. internal = jack_client_is_internal(
  2811. (jack_client_internal_t *) node->data);
  2812. break;
  2813. }
  2814. }
  2815. if (!internal) {
  2816. if (write (reply_fd, req, sizeof (*req))
  2817. < (ssize_t) sizeof (req)) {
  2818. jack_error ("cannot write GetPortConnections result "
  2819. "to client via fd = %d (%s)",
  2820. reply_fd, strerror (errno));
  2821. goto out;
  2822. }
  2823. } else {
  2824. req->x.port_connections.ports = (const char **)
  2825. malloc (sizeof (char *)
  2826. * req->x.port_connections.nports);
  2827. }
  2828. if (req->type == GetPortConnections) {
  2829. for (i = 0, node = port->connections; node;
  2830. node = jack_slist_next (node), ++i) {
  2831. jack_port_id_t port_id;
  2832. if (((jack_connection_internal_t *) node->data)->source
  2833. == port) {
  2834. port_id = ((jack_connection_internal_t *)
  2835. node->data)->destination->shared->id;
  2836. } else {
  2837. port_id = ((jack_connection_internal_t *)
  2838. node->data)->source->shared->id;
  2839. }
  2840. if (internal) {
  2841. /* internal client asking for
  2842. * names. store in malloc'ed space,
  2843. * client frees
  2844. */
  2845. req->x.port_connections.ports[i] =
  2846. engine->control->ports[port_id].name;
  2847. } else {
  2848. /* external client asking for
  2849. * names. we write the port id's to
  2850. * the reply fd.
  2851. */
  2852. if (write (reply_fd, &port_id,
  2853. sizeof (port_id))
  2854. < (ssize_t) sizeof (port_id)) {
  2855. jack_error ("cannot write port id "
  2856. "to client");
  2857. goto out;
  2858. }
  2859. }
  2860. }
  2861. }
  2862. ret = 0;
  2863. out:
  2864. req->status = ret;
  2865. jack_unlock_graph (engine);
  2866. return ret;
  2867. }
  2868. void
  2869. jack_port_registration_notify (jack_engine_t *engine,
  2870. jack_port_id_t port_id, int yn)
  2871. {
  2872. jack_event_t event;
  2873. jack_client_internal_t *client;
  2874. JSList *node;
  2875. event.type = (yn ? PortRegistered : PortUnregistered);
  2876. event.x.port_id = port_id;
  2877. for (node = engine->clients; node; node = jack_slist_next (node)) {
  2878. client = (jack_client_internal_t *) node->data;
  2879. if (!client->control->active) {
  2880. continue;
  2881. }
  2882. if (client->control->port_register) {
  2883. if (jack_deliver_event (engine, client, &event)) {
  2884. jack_error ("cannot send port registration"
  2885. " notification to %s (%s)",
  2886. client->control->name,
  2887. strerror (errno));
  2888. }
  2889. }
  2890. }
  2891. }
  2892. int
  2893. jack_port_assign_buffer (jack_engine_t *engine, jack_port_internal_t *port)
  2894. {
  2895. jack_port_buffer_list_t *blist =
  2896. jack_port_buffer_list (engine, port);
  2897. jack_port_buffer_info_t *bi;
  2898. if (port->shared->flags & JackPortIsInput) {
  2899. port->shared->offset = 0;
  2900. return 0;
  2901. }
  2902. pthread_mutex_lock (&blist->lock);
  2903. if (blist->freelist == NULL) {
  2904. jack_port_type_info_t *port_type =
  2905. jack_port_type_info (engine, port);
  2906. jack_error ("all %s port buffers in use!",
  2907. port_type->type_name);
  2908. pthread_mutex_unlock (&blist->lock);
  2909. return -1;
  2910. }
  2911. bi = (jack_port_buffer_info_t *) blist->freelist->data;
  2912. blist->freelist = jack_slist_remove (blist->freelist, bi);
  2913. port->shared->offset = bi->offset;
  2914. port->buffer_info = bi;
  2915. pthread_mutex_unlock (&blist->lock);
  2916. return 0;
  2917. }
  2918. static jack_port_internal_t *
  2919. jack_get_port_by_name (jack_engine_t *engine, const char *name)
  2920. {
  2921. jack_port_id_t id;
  2922. /* Note the potential race on "in_use". Other design
  2923. elements prevent this from being a problem.
  2924. */
  2925. for (id = 0; id < engine->port_max; id++) {
  2926. if (engine->control->ports[id].in_use &&
  2927. strcmp (engine->control->ports[id].name, name) == 0) {
  2928. return &engine->internal_ports[id];
  2929. }
  2930. }
  2931. return NULL;
  2932. }
  2933. static int
  2934. jack_send_connection_notification (jack_engine_t *engine,
  2935. jack_client_id_t client_id,
  2936. jack_port_id_t self_id,
  2937. jack_port_id_t other_id, int connected)
  2938. {
  2939. jack_client_internal_t *client;
  2940. jack_event_t event;
  2941. if ((client = jack_client_internal_by_id (engine, client_id)) == NULL) {
  2942. jack_error ("no such client %" PRIu32
  2943. " during connection notification", client_id);
  2944. return -1;
  2945. }
  2946. if (client->control->active) {
  2947. event.type = (connected ? PortConnected : PortDisconnected);
  2948. event.x.self_id = self_id;
  2949. event.y.other_id = other_id;
  2950. if (jack_deliver_event (engine, client, &event)) {
  2951. jack_error ("cannot send port connection notification"
  2952. " to client %s (%s)",
  2953. client->control->name, strerror (errno));
  2954. return -1;
  2955. }
  2956. }
  2957. return 0;
  2958. }