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.

3557 lines
85KB

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