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.

3624 lines
87KB

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