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.

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