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.

3559 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. jack_init_time ();
  1273. engine = (jack_engine_t *) malloc (sizeof (jack_engine_t));
  1274. engine->drivers = drivers;
  1275. engine->driver = NULL;
  1276. engine->driver_desc = NULL;
  1277. engine->driver_params = NULL;
  1278. engine->set_sample_rate = jack_set_sample_rate;
  1279. engine->set_buffer_size = jack_driver_buffer_size;
  1280. engine->run_cycle = jack_run_cycle;
  1281. engine->delay = jack_engine_notify_clients_about_delay;
  1282. engine->driver_exit = jack_engine_driver_exit;
  1283. engine->transport_cycle_start = jack_transport_cycle_start;
  1284. engine->client_timeout_msecs = client_timeout;
  1285. engine->next_client_id = 1; /* 0 is a NULL client ID */
  1286. engine->port_max = port_max;
  1287. engine->rtpriority = rtpriority;
  1288. engine->silent_buffer = 0;
  1289. engine->verbose = verbose;
  1290. engine->server_name = server_name;
  1291. engine->temporary = temporary;
  1292. engine->freewheeling = 0;
  1293. engine->wait_pid = wait_pid;
  1294. jack_engine_reset_rolling_usecs (engine);
  1295. engine->max_usecs = 0.0f;
  1296. pthread_mutex_init (&engine->client_lock, 0);
  1297. pthread_mutex_init (&engine->port_lock, 0);
  1298. pthread_mutex_init (&engine->request_lock, 0);
  1299. engine->clients = 0;
  1300. engine->pfd_size = 16;
  1301. engine->pfd_max = 0;
  1302. engine->pfd = (struct pollfd *) malloc (sizeof (struct pollfd)
  1303. * engine->pfd_size);
  1304. engine->fifo_size = 16;
  1305. engine->fifo = (int *) malloc (sizeof (int) * engine->fifo_size);
  1306. for (i = 0; i < engine->fifo_size; i++) {
  1307. engine->fifo[i] = -1;
  1308. }
  1309. engine->external_client_cnt = 0;
  1310. srandom (time ((time_t *) 0));
  1311. if (jack_initialize_shm (engine->server_name)) {
  1312. return 0;
  1313. }
  1314. if (jack_shmalloc ("/jack-engine",
  1315. sizeof (jack_control_t)
  1316. + ((sizeof (jack_port_shared_t) * engine->port_max)),
  1317. &engine->control_shm)) {
  1318. jack_error ("cannot create engine control shared memory "
  1319. "segment (%s)", strerror (errno));
  1320. return 0;
  1321. }
  1322. if (jack_attach_shm (&engine->control_shm)) {
  1323. jack_error ("cannot attach to engine control shared memory"
  1324. " (%s)", strerror (errno));
  1325. jack_destroy_shm (&engine->control_shm);
  1326. return 0;
  1327. }
  1328. engine->control = (jack_control_t *)
  1329. jack_shm_addr (&engine->control_shm);
  1330. /* Setup port type information from builtins. buffer space is
  1331. * allocated when the driver calls jack_driver_buffer_size().
  1332. */
  1333. for (i = 0; jack_builtin_port_types[i].type_name[0]; ++i) {
  1334. memcpy (&engine->control->port_types[i],
  1335. &jack_builtin_port_types[i],
  1336. sizeof (jack_port_type_info_t));
  1337. VERBOSE (engine, "registered builtin port type %s\n",
  1338. engine->control->port_types[i].type_name);
  1339. /* the port type id is index into port_types array */
  1340. engine->control->port_types[i].ptype_id = i;
  1341. /* be sure to initialize mutex correctly */
  1342. pthread_mutex_init (&engine->port_buffers[i].lock, NULL);
  1343. /* set buffer list info correctly */
  1344. engine->port_buffers[i].freelist = NULL;
  1345. engine->port_buffers[i].info = NULL;
  1346. /* mark each port segment as not allocated */
  1347. engine->port_segment[i].index = -1;
  1348. engine->port_segment[i].attached_at = 0;
  1349. }
  1350. engine->control->n_port_types = i;
  1351. /* Mark all ports as available */
  1352. for (i = 0; i < engine->port_max; i++) {
  1353. engine->control->ports[i].in_use = 0;
  1354. engine->control->ports[i].id = i;
  1355. }
  1356. /* allocate internal port structures so that we can keep track
  1357. * of port connections.
  1358. */
  1359. engine->internal_ports = (jack_port_internal_t *)
  1360. malloc (sizeof (jack_port_internal_t) * engine->port_max);
  1361. for (i = 0; i < engine->port_max; i++) {
  1362. engine->internal_ports[i].connections = 0;
  1363. }
  1364. if (make_sockets (engine->server_name, engine->fds) < 0) {
  1365. jack_error ("cannot create server sockets");
  1366. return 0;
  1367. }
  1368. engine->control->port_max = engine->port_max;
  1369. engine->control->real_time = realtime;
  1370. engine->control->client_priority = (realtime
  1371. ? engine->rtpriority - 1
  1372. : 0);
  1373. engine->control->do_mlock = do_mlock;
  1374. engine->control->do_munlock = do_unlock;
  1375. engine->control->cpu_load = 0;
  1376. engine->control->xrun_delayed_usecs = 0;
  1377. engine->control->buffer_size = 0;
  1378. jack_transport_init (engine);
  1379. jack_set_sample_rate (engine, 0);
  1380. engine->control->internal = 0;
  1381. engine->control->has_capabilities = 0;
  1382. #ifdef JACK_USE_MACH_THREADS
  1383. /* specific resources for server/client real-time thread
  1384. * communication */
  1385. engine->servertask = mach_task_self();
  1386. if (task_get_bootstrap_port(engine->servertask, &engine->bp)){
  1387. jack_error("Jackd: Can't find bootstrap mach port");
  1388. return 0;
  1389. }
  1390. engine->portnum = 0;
  1391. #endif /* JACK_USE_MACH_THREADS */
  1392. #ifdef USE_CAPABILITIES
  1393. if (uid == 0 || euid == 0) {
  1394. VERBOSE (engine, "running with uid=%d and euid=%d, "
  1395. "will not try to use capabilites\n",
  1396. uid, euid);
  1397. } else {
  1398. /* only try to use capabilities if not running as root */
  1399. engine->control->has_capabilities = check_capabilities (engine);
  1400. if (engine->control->has_capabilities == 0) {
  1401. VERBOSE (engine, "required capabilities not "
  1402. "available\n");
  1403. }
  1404. if (engine->verbose) {
  1405. size_t size;
  1406. cap_t cap = cap_init();
  1407. capgetp(0, cap);
  1408. VERBOSE (engine, "capabilities: %s\n",
  1409. cap_to_text(cap, &size));
  1410. }
  1411. }
  1412. #endif /* USE_CAPABILITIES */
  1413. #ifdef USE_MLOCK
  1414. if (realtime && do_mlock && (mlockall (MCL_CURRENT | MCL_FUTURE) != 0)) {
  1415. jack_error ("cannot lock down memory for jackd (%s)",
  1416. strerror (errno));
  1417. #ifdef ENSURE_MLOCK
  1418. return NULL;
  1419. #endif /* ENSURE_MLOCK */
  1420. }
  1421. #endif /* USE_MLOCK */
  1422. engine->control->engine_ok = 1;
  1423. snprintf (engine->fifo_prefix, sizeof (engine->fifo_prefix),
  1424. "%s/jack-%d-ack-fifo-%d",
  1425. jack_server_dir (engine->server_name),
  1426. getuid (), getpid ());
  1427. (void) jack_get_fifo_fd (engine, 0);
  1428. jack_create_thread (&engine->server_thread, 0, FALSE,
  1429. &jack_server_thread, engine);
  1430. return engine;
  1431. }
  1432. static void *
  1433. jack_watchdog_thread (void *arg)
  1434. {
  1435. jack_engine_t *engine = (jack_engine_t *) arg;
  1436. engine->watchdog_check = 0;
  1437. while (1) {
  1438. sleep (5);
  1439. if ( engine->watchdog_check == 0) {
  1440. jack_error ("jackd watchdog: timeout - killing jackd");
  1441. /* Kill the current client's process group. */
  1442. if (engine->current_client) {
  1443. kill (-engine->current_client->
  1444. control->pgrp, SIGKILL);
  1445. }
  1446. /* kill our process group, try to get a dump */
  1447. kill (-getpgrp(), SIGABRT);
  1448. /*NOTREACHED*/
  1449. exit (1);
  1450. }
  1451. engine->watchdog_check = 0;
  1452. }
  1453. }
  1454. static int
  1455. jack_start_watchdog (jack_engine_t *engine)
  1456. {
  1457. int watchdog_priority = engine->rtpriority + 10;
  1458. int max_priority = sched_get_priority_max (SCHED_FIFO);
  1459. if ((max_priority != -1) &&
  1460. (max_priority < watchdog_priority))
  1461. watchdog_priority = max_priority;
  1462. if (jack_create_thread (&engine->watchdog_thread, watchdog_priority,
  1463. TRUE, jack_watchdog_thread, engine)) {
  1464. jack_error ("cannot start watchdog thread");
  1465. return -1;
  1466. }
  1467. return 0;
  1468. }
  1469. static void
  1470. jack_engine_notify_clients_about_delay (jack_engine_t *engine,
  1471. float delayed_usecs)
  1472. {
  1473. JSList *node;
  1474. jack_event_t event;
  1475. engine->control->xrun_delayed_usecs = delayed_usecs;
  1476. event.type = XRun;
  1477. jack_lock_graph (engine);
  1478. for (node = engine->clients; node; node = jack_slist_next (node)) {
  1479. jack_deliver_event (engine,
  1480. (jack_client_internal_t *) node->data,
  1481. &event);
  1482. }
  1483. jack_unlock_graph (engine);
  1484. }
  1485. static inline void
  1486. jack_inc_frame_time (jack_engine_t *engine, jack_nframes_t amount)
  1487. {
  1488. jack_frame_timer_t *time = &engine->control->frame_timer;
  1489. // atomic_inc (&time->guard1, 1);
  1490. // really need a memory barrier here
  1491. time->guard1++;
  1492. time->frames += amount;
  1493. time->stamp = engine->driver->last_wait_ust;
  1494. // atomic_inc (&time->guard2, 1);
  1495. // might need a memory barrier here
  1496. time->guard2++;
  1497. }
  1498. static void*
  1499. jack_engine_freewheel (void *arg)
  1500. {
  1501. jack_engine_t* engine = (jack_engine_t *) arg;
  1502. VERBOSE (engine, "freewheel thread starting ...\n");
  1503. /* we should not be running SCHED_FIFO, so we don't
  1504. have to do anything about scheduling.
  1505. */
  1506. while (engine->freewheeling) {
  1507. jack_lock_graph (engine);
  1508. if (jack_engine_process (engine,
  1509. engine->control->buffer_size)) {
  1510. jack_error ("process cycle within freewheel failed");
  1511. jack_unlock_graph (engine);
  1512. break;
  1513. }
  1514. jack_unlock_graph (engine);
  1515. }
  1516. VERBOSE (engine, "freewheel came to an end, naturally\n");
  1517. return 0;
  1518. }
  1519. static int
  1520. jack_start_freewheeling (jack_engine_t* engine)
  1521. {
  1522. jack_event_t event;
  1523. if (engine->freewheeling) {
  1524. return 0;
  1525. }
  1526. if (engine->driver == NULL) {
  1527. jack_error ("cannot start freewheeling without a driver!");
  1528. return -1;
  1529. }
  1530. /* stop driver before telling anyone about it so
  1531. there are no more process() calls being handled.
  1532. */
  1533. if (engine->driver->stop (engine->driver)) {
  1534. jack_error ("could not stop driver for freewheeling");
  1535. return -1;
  1536. }
  1537. engine->freewheeling = 1;
  1538. event.type = StartFreewheel;
  1539. jack_deliver_event_to_all (engine, &event);
  1540. if (jack_create_thread (&engine->freewheel_thread, 0, FALSE,
  1541. jack_engine_freewheel, engine)) {
  1542. jack_error ("could not start create freewheel thread");
  1543. return -1;
  1544. }
  1545. return 0;
  1546. }
  1547. static int
  1548. jack_stop_freewheeling (jack_engine_t* engine)
  1549. {
  1550. jack_event_t event;
  1551. void *ftstatus;
  1552. if (!engine->freewheeling) {
  1553. return 0;
  1554. }
  1555. if (engine->driver == NULL) {
  1556. jack_error ("cannot start freewheeling without a driver!");
  1557. return -1;
  1558. }
  1559. if (!engine->freewheeling) {
  1560. VERBOSE (engine, "stop freewheel when not freewheeling\n");
  1561. return 0;
  1562. }
  1563. /* tell the freewheel thread to stop, and wait for it
  1564. to exit.
  1565. */
  1566. engine->freewheeling = 0;
  1567. VERBOSE (engine, "freewheeling stopped, waiting for thread\n");
  1568. pthread_join (engine->freewheel_thread, &ftstatus);
  1569. VERBOSE (engine, "freewheel thread has returned\n");
  1570. /* tell everyone we've stopped */
  1571. event.type = StopFreewheel;
  1572. jack_deliver_event_to_all (engine, &event);
  1573. /* restart the driver */
  1574. if (engine->driver->start (engine->driver)) {
  1575. jack_error ("could not restart driver after freewheeling");
  1576. return -1;
  1577. }
  1578. return 0;
  1579. }
  1580. static int
  1581. jack_run_one_cycle (jack_engine_t *engine, jack_nframes_t nframes,
  1582. float delayed_usecs)
  1583. {
  1584. jack_driver_t* driver = engine->driver;
  1585. int ret = -1;
  1586. static int consecutive_excessive_delays = 0;
  1587. #define WORK_SCALE 1.0f
  1588. if (engine->control->real_time &&
  1589. engine->spare_usecs &&
  1590. ((WORK_SCALE * engine->spare_usecs) <= delayed_usecs)) {
  1591. MESSAGE("delay of %.3f usecs exceeds estimated spare"
  1592. " time of %.3f; restart ...\n",
  1593. delayed_usecs, WORK_SCALE * engine->spare_usecs);
  1594. if (++consecutive_excessive_delays > 10) {
  1595. jack_error ("too many consecutive interrupt delays "
  1596. "... engine pausing");
  1597. return -1; /* will exit the thread loop */
  1598. }
  1599. jack_engine_notify_clients_about_delay (engine, delayed_usecs);
  1600. return 0;
  1601. } else {
  1602. consecutive_excessive_delays = 0;
  1603. }
  1604. jack_inc_frame_time (engine, nframes);
  1605. if (jack_try_lock_graph (engine)) {
  1606. /* engine can't run. just throw away an entire cycle */
  1607. driver->null_cycle (driver, nframes);
  1608. return 0;
  1609. }
  1610. if (!engine->freewheeling) {
  1611. DEBUG("waiting for driver read\n");
  1612. if (driver->read (driver, nframes)) {
  1613. goto unlock;
  1614. }
  1615. }
  1616. DEBUG("run process\n");
  1617. if (jack_engine_process (engine, nframes) == 0) {
  1618. if (!engine->freewheeling) {
  1619. if (driver->write (driver, nframes)) {
  1620. goto unlock;
  1621. }
  1622. }
  1623. } else {
  1624. JSList *node;
  1625. DEBUG ("engine process cycle failed");
  1626. /* we are already late, or something else went wrong,
  1627. so it can't hurt to check the existence of all
  1628. clients.
  1629. */
  1630. for (node = engine->clients; node;
  1631. node = jack_slist_next (node)) {
  1632. jack_client_internal_t *client =
  1633. (jack_client_internal_t *) node->data;
  1634. if (client->control->type == ClientExternal) {
  1635. if (kill (client->control->pid, 0)) {
  1636. VERBOSE(engine,
  1637. "client %s has died/exited\n",
  1638. client->control->name);
  1639. client->error++;
  1640. }
  1641. }
  1642. DEBUG ("client %s errors = %d", client->control->name,
  1643. client->error);
  1644. }
  1645. }
  1646. jack_engine_post_process (engine);
  1647. ret = 0;
  1648. unlock:
  1649. jack_unlock_graph (engine);
  1650. DEBUG("cycle finished, status = %d", ret);
  1651. return ret;
  1652. }
  1653. static void
  1654. jack_engine_driver_exit (jack_engine_t* engine)
  1655. {
  1656. /* tell anyone waiting that the driver exited. */
  1657. kill (engine->wait_pid, SIGUSR2);
  1658. engine->driver = NULL;
  1659. }
  1660. static int
  1661. jack_run_cycle (jack_engine_t *engine, jack_nframes_t nframes,
  1662. float delayed_usecs)
  1663. {
  1664. jack_nframes_t left;
  1665. jack_nframes_t b_size = engine->control->buffer_size;
  1666. if (engine->verbose) {
  1667. if (nframes != b_size) {
  1668. VERBOSE (engine,
  1669. "late driver wakeup: nframes to process = %"
  1670. PRIu32 ".\n", nframes);
  1671. }
  1672. }
  1673. /* run as many cycles as it takes to consume nframes */
  1674. for (left = nframes; left >= b_size; left -= b_size) {
  1675. if (jack_run_one_cycle (engine, b_size, delayed_usecs)) {
  1676. jack_error ("cycle execution failure, exiting");
  1677. return EIO;
  1678. }
  1679. }
  1680. return 0;
  1681. }
  1682. void
  1683. jack_engine_delete (jack_engine_t *engine)
  1684. {
  1685. int i;
  1686. if (engine == NULL)
  1687. return;
  1688. engine->control->engine_ok = 0; /* tell clients we're going away */
  1689. /* shutdown master socket to prevent new clients arriving */
  1690. // close (engine->fds[0]);
  1691. if (engine->driver) {
  1692. jack_driver_t* driver = engine->driver;
  1693. VERBOSE (engine, "stopping driver\n");
  1694. driver->stop (driver);
  1695. // VERBOSE (engine, "detaching driver\n");
  1696. // driver->detach (driver, engine);
  1697. VERBOSE (engine, "unloading driver\n");
  1698. jack_driver_unload (driver);
  1699. engine->driver = NULL;
  1700. }
  1701. VERBOSE (engine, "freeing shared port segments\n");
  1702. for (i = 0; i < engine->control->n_port_types; ++i) {
  1703. jack_release_shm (&engine->port_segment[i]);
  1704. jack_destroy_shm (&engine->port_segment[i]);
  1705. }
  1706. /* stop the other engine threads */
  1707. VERBOSE (engine, "stopping server thread\n");
  1708. #if JACK_USE_MACH_THREADS
  1709. // MacOSX pthread_cancel still not implemented correctly in Darwin
  1710. mach_port_t machThread = pthread_mach_thread_np (engine->server_thread);
  1711. thread_terminate (machThread);
  1712. #else
  1713. pthread_cancel (engine->server_thread);
  1714. pthread_join (engine->server_thread, NULL);
  1715. #endif
  1716. #ifndef JACK_USE_MACH_THREADS
  1717. /* Cancel the watchdog thread and wait for it to terminate.
  1718. *
  1719. * The watchdog thread is not used on MacOSX since CoreAudio
  1720. * drivers already contain a similar mechanism.
  1721. */
  1722. if (engine->control->real_time) {
  1723. VERBOSE (engine, "stopping watchdog thread\n");
  1724. pthread_cancel (engine->watchdog_thread);
  1725. pthread_join (engine->watchdog_thread, NULL);
  1726. }
  1727. #endif
  1728. VERBOSE (engine, "last xrun delay: %.3f usecs\n",
  1729. engine->control->xrun_delayed_usecs);
  1730. /* free engine control shm segment */
  1731. engine->control = NULL;
  1732. VERBOSE (engine, "freeing engine shared memory\n");
  1733. jack_release_shm (&engine->control_shm);
  1734. jack_destroy_shm (&engine->control_shm);
  1735. VERBOSE (engine, "max usecs: %.3f, ", engine->max_usecs);
  1736. VERBOSE (engine, "engine deleted\n");
  1737. free (engine);
  1738. jack_messagebuffer_exit();
  1739. }
  1740. void
  1741. jack_port_clear_connections (jack_engine_t *engine,
  1742. jack_port_internal_t *port)
  1743. {
  1744. JSList *node, *next;
  1745. for (node = port->connections; node; ) {
  1746. next = jack_slist_next (node);
  1747. jack_port_disconnect_internal (
  1748. engine, ((jack_connection_internal_t *)
  1749. node->data)->source,
  1750. ((jack_connection_internal_t *)
  1751. node->data)->destination,
  1752. FALSE);
  1753. node = next;
  1754. }
  1755. jack_slist_free (port->connections);
  1756. port->connections = 0;
  1757. }
  1758. static void
  1759. jack_deliver_event_to_all (jack_engine_t *engine, jack_event_t *event)
  1760. {
  1761. JSList *node;
  1762. jack_lock_graph (engine);
  1763. for (node = engine->clients; node; node = jack_slist_next (node)) {
  1764. jack_deliver_event (engine,
  1765. (jack_client_internal_t *) node->data,
  1766. event);
  1767. }
  1768. jack_unlock_graph (engine);
  1769. }
  1770. static int
  1771. jack_deliver_event (jack_engine_t *engine, jack_client_internal_t *client,
  1772. jack_event_t *event)
  1773. {
  1774. char status;
  1775. /* caller must hold the graph lock */
  1776. DEBUG ("delivering event (type %d)", event->type);
  1777. /* we are not RT-constrained here, so use kill(2) to beef up
  1778. our check on a client's continued well-being
  1779. */
  1780. if (client->control->dead
  1781. || (client->control->type == ClientExternal
  1782. && kill (client->control->pid, 0))) {
  1783. DEBUG ("client %s is dead - no event sent",
  1784. client->control->name);
  1785. return 0;
  1786. }
  1787. DEBUG ("client %s is still alive", client->control->name);
  1788. if (jack_client_is_internal (client)) {
  1789. switch (event->type) {
  1790. case PortConnected:
  1791. case PortDisconnected:
  1792. jack_client_handle_port_connection
  1793. (client->control->private_client, event);
  1794. break;
  1795. case BufferSizeChange:
  1796. jack_client_invalidate_port_buffers
  1797. (client->control->private_client);
  1798. if (client->control->bufsize) {
  1799. client->control->bufsize
  1800. (event->x.n,
  1801. client->control->bufsize_arg);
  1802. }
  1803. break;
  1804. case SampleRateChange:
  1805. if (client->control->srate) {
  1806. client->control->srate
  1807. (event->x.n,
  1808. client->control->srate_arg);
  1809. }
  1810. break;
  1811. case GraphReordered:
  1812. if (client->control->graph_order) {
  1813. client->control->graph_order
  1814. (client->control->graph_order_arg);
  1815. }
  1816. break;
  1817. case XRun:
  1818. if (client->control->xrun) {
  1819. client->control->xrun
  1820. (client->control->xrun_arg);
  1821. }
  1822. break;
  1823. default:
  1824. /* internal clients don't need to know */
  1825. break;
  1826. }
  1827. } else {
  1828. if (client->control->active) {
  1829. /* there's a thread waiting for events, so
  1830. * it's worth telling the client */
  1831. DEBUG ("engine writing on event fd");
  1832. if (write (client->event_fd, event, sizeof (*event))
  1833. != sizeof (*event)) {
  1834. jack_error ("cannot send event to client [%s]"
  1835. " (%s)", client->control->name,
  1836. strerror (errno));
  1837. client->error++;
  1838. }
  1839. DEBUG ("engine reading from event fd");
  1840. if (!client->error &&
  1841. (read (client->event_fd, &status, sizeof (status))
  1842. != sizeof (status))) {
  1843. jack_error ("cannot read event response from "
  1844. "client [%s] (%s)",
  1845. client->control->name,
  1846. strerror (errno));
  1847. client->error++;
  1848. }
  1849. if (status != 0) {
  1850. jack_error ("bad status for client event "
  1851. "handling (type = %d)",
  1852. event->type);
  1853. client->error++;
  1854. }
  1855. }
  1856. }
  1857. DEBUG ("event delivered");
  1858. return 0;
  1859. }
  1860. int
  1861. jack_rechain_graph (jack_engine_t *engine)
  1862. {
  1863. JSList *node, *next;
  1864. unsigned long n;
  1865. int err = 0;
  1866. jack_client_internal_t *client, *subgraph_client, *next_client;
  1867. jack_event_t event;
  1868. int upstream_is_jackd;
  1869. jack_clear_fifos (engine);
  1870. subgraph_client = 0;
  1871. VERBOSE(engine, "++ jack_rechain_graph():\n");
  1872. event.type = GraphReordered;
  1873. for (n = 0, node = engine->clients, next = NULL; node; node = next) {
  1874. next = jack_slist_next (node);
  1875. if (((jack_client_internal_t *) node->data)->control->active) {
  1876. client = (jack_client_internal_t *) node->data;
  1877. /* find the next active client. its ok for
  1878. * this to be NULL */
  1879. while (next) {
  1880. if (((jack_client_internal_t *)
  1881. next->data)->control->active) {
  1882. break;
  1883. }
  1884. next = jack_slist_next (next);
  1885. };
  1886. if (next == NULL) {
  1887. next_client = NULL;
  1888. } else {
  1889. next_client = (jack_client_internal_t *)
  1890. next->data;
  1891. }
  1892. client->execution_order = n;
  1893. client->next_client = next_client;
  1894. if (jack_client_is_internal (client)) {
  1895. /* break the chain for the current
  1896. * subgraph. the server will wait for
  1897. * chain on the nth FIFO, and will
  1898. * then execute this internal
  1899. * client. */
  1900. if (subgraph_client) {
  1901. subgraph_client->subgraph_wait_fd =
  1902. jack_get_fifo_fd (engine, n);
  1903. VERBOSE (engine, "client %s: wait_fd="
  1904. "%d, execution_order="
  1905. "%lu.\n",
  1906. subgraph_client->
  1907. control->name,
  1908. subgraph_client->
  1909. subgraph_wait_fd, n);
  1910. n++;
  1911. }
  1912. VERBOSE (engine, "client %s: internal "
  1913. "client, execution_order="
  1914. "%lu.\n",
  1915. client->control->name, n);
  1916. /* this does the right thing for
  1917. * internal clients too
  1918. */
  1919. jack_deliver_event (engine, client, &event);
  1920. subgraph_client = 0;
  1921. } else {
  1922. if (subgraph_client == NULL) {
  1923. /* start a new subgraph. the
  1924. * engine will start the chain
  1925. * by writing to the nth
  1926. * FIFO.
  1927. */
  1928. subgraph_client = client;
  1929. subgraph_client->subgraph_start_fd =
  1930. jack_get_fifo_fd (engine, n);
  1931. VERBOSE (engine, "client %s: "
  1932. "start_fd=%d, execution"
  1933. "_order=%lu.\n",
  1934. subgraph_client->
  1935. control->name,
  1936. subgraph_client->
  1937. subgraph_start_fd, n);
  1938. /* this external client after
  1939. this will have jackd as its
  1940. upstream connection.
  1941. */
  1942. upstream_is_jackd = 1;
  1943. }
  1944. else {
  1945. VERBOSE (engine, "client %s: in"
  1946. " subgraph after %s, "
  1947. "execution_order="
  1948. "%lu.\n",
  1949. client->control->name,
  1950. subgraph_client->
  1951. control->name, n);
  1952. subgraph_client->subgraph_wait_fd = -1;
  1953. /* this external client after
  1954. this will have another
  1955. client as its upstream
  1956. connection.
  1957. */
  1958. upstream_is_jackd = 0;
  1959. }
  1960. /* make sure fifo for 'n + 1' exists
  1961. * before issuing client reorder
  1962. */
  1963. (void) jack_get_fifo_fd(
  1964. engine, client->execution_order + 1);
  1965. event.x.n = client->execution_order;
  1966. event.y.n = upstream_is_jackd;
  1967. jack_deliver_event (engine, client, &event);
  1968. n++;
  1969. }
  1970. }
  1971. }
  1972. if (subgraph_client) {
  1973. subgraph_client->subgraph_wait_fd =
  1974. jack_get_fifo_fd (engine, n);
  1975. VERBOSE (engine, "client %s: wait_fd=%d, "
  1976. "execution_order=%lu (last client).\n",
  1977. subgraph_client->control->name,
  1978. subgraph_client->subgraph_wait_fd, n);
  1979. }
  1980. VERBOSE (engine, "-- jack_rechain_graph()\n");
  1981. return err;
  1982. }
  1983. static void
  1984. jack_trace_terminal (jack_client_internal_t *c1, jack_client_internal_t *rbase)
  1985. {
  1986. jack_client_internal_t *c2;
  1987. /* make a copy of the existing list of routes that feed
  1988. c1. this provides us with an atomic snapshot of c1's
  1989. "fed-by" state, which will be modified as we progress ...
  1990. */
  1991. JSList *existing;
  1992. JSList *node;
  1993. if (c1->fed_by == NULL) {
  1994. return;
  1995. }
  1996. existing = jack_slist_copy (c1->fed_by);
  1997. /* for each route that feeds c1, recurse, marking it as
  1998. feeding rbase as well.
  1999. */
  2000. for (node = existing; node; node = jack_slist_next (node)) {
  2001. c2 = (jack_client_internal_t *) node->data;
  2002. /* c2 is a route that feeds c1 which somehow feeds
  2003. base. mark base as being fed by c2, but don't do it
  2004. more than once.
  2005. */
  2006. if (c2 != rbase && c2 != c1) {
  2007. if (jack_slist_find (rbase->fed_by, c2) == NULL) {
  2008. rbase->fed_by =
  2009. jack_slist_prepend (rbase->fed_by, c2);
  2010. }
  2011. /* FIXME: if c2->fed_by is not up-to-date, we
  2012. may end up recursing infinitely
  2013. (kaiv)
  2014. */
  2015. if (jack_slist_find (c2->fed_by, c1) == NULL) {
  2016. /* now recurse, so that we can mark
  2017. base as being fed by all routes
  2018. that feed c2
  2019. */
  2020. jack_trace_terminal (c2, rbase);
  2021. }
  2022. }
  2023. }
  2024. jack_slist_free (existing);
  2025. }
  2026. static int
  2027. jack_client_sort (jack_client_internal_t *a, jack_client_internal_t *b)
  2028. {
  2029. if (jack_slist_find (a->fed_by, b)) {
  2030. if (jack_slist_find (b->fed_by, a)) {
  2031. /* feedback loop: if `a' is the driver
  2032. client, let that execute first.
  2033. */
  2034. if (a->control->type == ClientDriver) {
  2035. /* b comes after a */
  2036. return -1;
  2037. }
  2038. }
  2039. /* a comes after b */
  2040. return 1;
  2041. } else if (jack_slist_find (b->fed_by, a)) {
  2042. if (jack_slist_find (a->fed_by, b)) {
  2043. /* feedback loop: if `b' is the driver
  2044. client, let that execute first.
  2045. */
  2046. if (b->control->type == ClientDriver) {
  2047. /* b comes before a */
  2048. return 1;
  2049. }
  2050. }
  2051. /* b comes after a */
  2052. return -1;
  2053. } else {
  2054. /* we don't care */
  2055. return 0;
  2056. }
  2057. }
  2058. static int
  2059. jack_client_feeds (jack_client_internal_t *might,
  2060. jack_client_internal_t *target)
  2061. {
  2062. JSList *pnode, *cnode;
  2063. /* Check every port of `might' for an outbound connection to
  2064. * `target' */
  2065. for (pnode = might->ports; pnode; pnode = jack_slist_next (pnode)) {
  2066. jack_port_internal_t *port;
  2067. port = (jack_port_internal_t *) pnode->data;
  2068. for (cnode = port->connections; cnode;
  2069. cnode = jack_slist_next (cnode)) {
  2070. jack_connection_internal_t *c;
  2071. c = (jack_connection_internal_t *) cnode->data;
  2072. if (c->source->shared->client_id
  2073. == might->control->id &&
  2074. c->destination->shared->client_id
  2075. == target->control->id) {
  2076. return 1;
  2077. }
  2078. }
  2079. }
  2080. return 0;
  2081. }
  2082. static jack_nframes_t
  2083. jack_get_port_total_latency (jack_engine_t *engine,
  2084. jack_port_internal_t *port, int hop_count,
  2085. int toward_port)
  2086. {
  2087. JSList *node;
  2088. jack_nframes_t latency;
  2089. jack_nframes_t max_latency = 0;
  2090. /* call tree must hold engine->client_lock. */
  2091. latency = port->shared->latency;
  2092. /* we don't prevent cyclic graphs, so we have to do something
  2093. to bottom out in the event that they are created.
  2094. */
  2095. if (hop_count > 8) {
  2096. return latency;
  2097. }
  2098. for (node = port->connections; node; node = jack_slist_next (node)) {
  2099. jack_nframes_t this_latency;
  2100. jack_connection_internal_t *connection;
  2101. connection = (jack_connection_internal_t *) node->data;
  2102. if ((toward_port &&
  2103. (connection->source->shared == port->shared)) ||
  2104. (!toward_port &&
  2105. (connection->destination->shared == port->shared))) {
  2106. continue;
  2107. }
  2108. /* if we're a destination in the connection, recurse
  2109. on the source to get its total latency
  2110. */
  2111. if (connection->destination == port) {
  2112. if (connection->source->shared->flags
  2113. & JackPortIsTerminal) {
  2114. this_latency = connection->source->
  2115. shared->latency;
  2116. } else {
  2117. this_latency =
  2118. jack_get_port_total_latency (
  2119. engine, connection->source,
  2120. hop_count + 1,
  2121. toward_port);
  2122. }
  2123. } else {
  2124. /* "port" is the source, so get the latency of
  2125. * the destination */
  2126. if (connection->destination->shared->flags
  2127. & JackPortIsTerminal) {
  2128. this_latency = connection->destination->
  2129. shared->latency;
  2130. } else {
  2131. this_latency =
  2132. jack_get_port_total_latency (
  2133. engine,
  2134. connection->destination,
  2135. hop_count + 1,
  2136. toward_port);
  2137. }
  2138. }
  2139. if (this_latency > max_latency) {
  2140. max_latency = this_latency;
  2141. }
  2142. }
  2143. return latency + max_latency;
  2144. }
  2145. static void
  2146. jack_compute_all_port_total_latencies (jack_engine_t *engine)
  2147. {
  2148. jack_port_shared_t *shared = engine->control->ports;
  2149. unsigned int i;
  2150. int toward_port;
  2151. for (i = 0; i < engine->control->port_max; i++) {
  2152. if (shared[i].in_use) {
  2153. if (shared[i].flags & JackPortIsOutput) {
  2154. toward_port = FALSE;
  2155. } else {
  2156. toward_port = TRUE;
  2157. }
  2158. shared[i].total_latency =
  2159. jack_get_port_total_latency (
  2160. engine, &engine->internal_ports[i],
  2161. 0, toward_port);
  2162. }
  2163. }
  2164. }
  2165. /**
  2166. * Sorts the network of clients using the following
  2167. * algorithm:
  2168. *
  2169. * 1) figure out who is connected to whom:
  2170. *
  2171. * foreach client1
  2172. * foreach input port
  2173. * foreach client2
  2174. * foreach output port
  2175. * if client1->input port connected to client2->output port
  2176. * mark client1 fed by client 2
  2177. *
  2178. * 2) trace the connections as terminal arcs in the graph so that
  2179. * if client A feeds client B who feeds client C, mark client C
  2180. * as fed by client A as well as client B, and so forth.
  2181. *
  2182. * 3) now sort according to whether or not client1->fed_by (client2) is true.
  2183. * if the condition is true, client2 must execute before client1
  2184. *
  2185. */
  2186. void
  2187. jack_sort_graph (jack_engine_t *engine)
  2188. {
  2189. JSList *node, *onode;
  2190. jack_client_internal_t *client;
  2191. jack_client_internal_t *oclient;
  2192. /* called, obviously, must hold engine->client_lock */
  2193. for (node = engine->clients; node; node = jack_slist_next (node)) {
  2194. client = (jack_client_internal_t *) node->data;
  2195. jack_slist_free (client->fed_by);
  2196. client->fed_by = 0;
  2197. for (onode = engine->clients; onode;
  2198. onode = jack_slist_next (onode)) {
  2199. oclient = (jack_client_internal_t *) onode->data;
  2200. if (jack_client_feeds (oclient, client)) {
  2201. client->fed_by =
  2202. jack_slist_prepend (client->fed_by,
  2203. oclient);
  2204. }
  2205. }
  2206. }
  2207. for (node = engine->clients; node; node = jack_slist_next (node)) {
  2208. jack_trace_terminal ((jack_client_internal_t *) node->data,
  2209. (jack_client_internal_t *) node->data);
  2210. }
  2211. engine->clients = jack_slist_sort (engine->clients,
  2212. (JCompareFunc) jack_client_sort);
  2213. jack_compute_all_port_total_latencies (engine);
  2214. jack_rechain_graph (engine);
  2215. }
  2216. /**
  2217. * Dumps current engine configuration to stderr.
  2218. */
  2219. void jack_dump_configuration(jack_engine_t *engine, int take_lock)
  2220. {
  2221. JSList *clientnode, *portnode, *connectionnode;
  2222. jack_client_internal_t *client;
  2223. jack_client_control_t *ctl;
  2224. jack_port_internal_t *port;
  2225. jack_connection_internal_t* connection;
  2226. int n, m, o;
  2227. fprintf(stderr, "engine.c: <-- dump begins -->\n");
  2228. if (take_lock) {
  2229. jack_lock_graph (engine);
  2230. }
  2231. for (n = 0, clientnode = engine->clients; clientnode;
  2232. clientnode = jack_slist_next (clientnode)) {
  2233. client = (jack_client_internal_t *) clientnode->data;
  2234. ctl = client->control;
  2235. fprintf (stderr, "client #%d: %s (type: %d, process? %s, fed"
  2236. " by %d clients) start=%d wait=%d\n",
  2237. ++n,
  2238. ctl->name,
  2239. ctl->type,
  2240. ctl->process ? "yes" : "no",
  2241. jack_slist_length(client->fed_by),
  2242. client->subgraph_start_fd,
  2243. client->subgraph_wait_fd);
  2244. for(m = 0, portnode = client->ports; portnode;
  2245. portnode = jack_slist_next (portnode)) {
  2246. port = (jack_port_internal_t *) portnode->data;
  2247. fprintf(stderr, "\t port #%d: %s\n", ++m,
  2248. port->shared->name);
  2249. for(o = 0, connectionnode = port->connections;
  2250. connectionnode;
  2251. connectionnode =
  2252. jack_slist_next (connectionnode)) {
  2253. connection = (jack_connection_internal_t *)
  2254. connectionnode->data;
  2255. fprintf(stderr, "\t\t connection #%d: %s %s\n",
  2256. ++o,
  2257. (port->shared->flags
  2258. & JackPortIsInput)? "<-": "->",
  2259. (port->shared->flags & JackPortIsInput)?
  2260. connection->source->shared->name:
  2261. connection->destination->shared->name);
  2262. }
  2263. }
  2264. }
  2265. if (take_lock) {
  2266. jack_unlock_graph (engine);
  2267. }
  2268. fprintf(stderr, "engine.c: <-- dump ends -->\n");
  2269. }
  2270. static int
  2271. jack_port_do_connect (jack_engine_t *engine,
  2272. const char *source_port,
  2273. const char *destination_port)
  2274. {
  2275. jack_connection_internal_t *connection;
  2276. jack_port_internal_t *srcport, *dstport;
  2277. jack_port_id_t src_id, dst_id;
  2278. jack_client_internal_t *client;
  2279. JSList *it;
  2280. if ((srcport = jack_get_port_by_name (engine, source_port)) == NULL) {
  2281. jack_error ("unknown source port in attempted connection [%s]",
  2282. source_port);
  2283. return -1;
  2284. }
  2285. if ((dstport = jack_get_port_by_name (engine, destination_port))
  2286. == NULL) {
  2287. jack_error ("unknown destination port in attempted connection"
  2288. " [%s]", destination_port);
  2289. return -1;
  2290. }
  2291. if ((dstport->shared->flags & JackPortIsInput) == 0) {
  2292. jack_error ("destination port in attempted connection of"
  2293. " %s and %s is not an input port",
  2294. source_port, destination_port);
  2295. return -1;
  2296. }
  2297. if ((srcport->shared->flags & JackPortIsOutput) == 0) {
  2298. jack_error ("source port in attempted connection of %s and"
  2299. " %s is not an output port",
  2300. source_port, destination_port);
  2301. return -1;
  2302. }
  2303. if (srcport->shared->locked) {
  2304. jack_error ("source port %s is locked against connection"
  2305. " changes", source_port);
  2306. return -1;
  2307. }
  2308. if (dstport->shared->locked) {
  2309. jack_error ("destination port %s is locked against connection"
  2310. " changes", destination_port);
  2311. return -1;
  2312. }
  2313. if (srcport->shared->ptype_id != dstport->shared->ptype_id) {
  2314. jack_error ("ports used in attemped connection are not of "
  2315. "the same data type");
  2316. return -1;
  2317. }
  2318. if ((client = jack_client_internal_by_id (engine,
  2319. srcport->shared->client_id))
  2320. == 0) {
  2321. jack_error ("unknown client set as owner of port - "
  2322. "cannot connect");
  2323. return -1;
  2324. }
  2325. if (!client->control->active) {
  2326. jack_error ("cannot connect ports owned by inactive clients;"
  2327. " \"%s\" is not active", client->control->name);
  2328. return -1;
  2329. }
  2330. if ((client = jack_client_internal_by_id (engine,
  2331. dstport->shared->client_id))
  2332. == 0) {
  2333. jack_error ("unknown client set as owner of port - cannot "
  2334. "connect");
  2335. return -1;
  2336. }
  2337. if (!client->control->active) {
  2338. jack_error ("cannot connect ports owned by inactive clients;"
  2339. " \"%s\" is not active", client->control->name);
  2340. return -1;
  2341. }
  2342. for (it = srcport->connections; it; it = it->next) {
  2343. if (((jack_connection_internal_t *)it->data)->destination
  2344. == dstport) {
  2345. return EEXIST;
  2346. }
  2347. }
  2348. connection = (jack_connection_internal_t *)
  2349. malloc (sizeof (jack_connection_internal_t));
  2350. connection->source = srcport;
  2351. connection->destination = dstport;
  2352. src_id = srcport->shared->id;
  2353. dst_id = dstport->shared->id;
  2354. jack_lock_graph (engine);
  2355. if (dstport->connections && !dstport->shared->has_mixdown) {
  2356. jack_port_type_info_t *port_type =
  2357. jack_port_type_info (engine, dstport);
  2358. jack_error ("cannot make multiple connections to a port of"
  2359. " type [%s]", port_type->type_name);
  2360. free (connection);
  2361. jack_unlock_graph (engine);
  2362. return -1;
  2363. } else {
  2364. VERBOSE (engine, "connect %s and %s\n",
  2365. srcport->shared->name,
  2366. dstport->shared->name);
  2367. dstport->connections =
  2368. jack_slist_prepend (dstport->connections, connection);
  2369. srcport->connections =
  2370. jack_slist_prepend (srcport->connections, connection);
  2371. DEBUG ("actually sorted the graph...");
  2372. jack_send_connection_notification (engine,
  2373. srcport->shared->client_id,
  2374. src_id, dst_id, TRUE);
  2375. jack_send_connection_notification (engine,
  2376. dstport->shared->client_id,
  2377. dst_id, src_id, TRUE);
  2378. jack_sort_graph (engine);
  2379. }
  2380. jack_unlock_graph (engine);
  2381. return 0;
  2382. }
  2383. int
  2384. jack_port_disconnect_internal (jack_engine_t *engine,
  2385. jack_port_internal_t *srcport,
  2386. jack_port_internal_t *dstport,
  2387. int sort_graph)
  2388. {
  2389. JSList *node;
  2390. jack_connection_internal_t *connect;
  2391. int ret = -1;
  2392. jack_port_id_t src_id, dst_id;
  2393. /* call tree **** MUST HOLD **** engine->client_lock. */
  2394. for (node = srcport->connections; node;
  2395. node = jack_slist_next (node)) {
  2396. connect = (jack_connection_internal_t *) node->data;
  2397. if (connect->source == srcport &&
  2398. connect->destination == dstport) {
  2399. VERBOSE (engine, "DIS-connect %s and %s\n",
  2400. srcport->shared->name,
  2401. dstport->shared->name);
  2402. srcport->connections =
  2403. jack_slist_remove (srcport->connections,
  2404. connect);
  2405. dstport->connections =
  2406. jack_slist_remove (dstport->connections,
  2407. connect);
  2408. src_id = srcport->shared->id;
  2409. dst_id = dstport->shared->id;
  2410. /* this is a bit harsh, but it basically says
  2411. that if we actually do a disconnect, and
  2412. its the last one, then make sure that any
  2413. input monitoring is turned off on the
  2414. srcport. this isn't ideal for all
  2415. situations, but it works better for most of
  2416. them.
  2417. */
  2418. if (srcport->connections == NULL) {
  2419. srcport->shared->monitor_requests = 0;
  2420. }
  2421. jack_send_connection_notification (
  2422. engine, srcport->shared->client_id, src_id,
  2423. dst_id, FALSE);
  2424. jack_send_connection_notification (
  2425. engine, dstport->shared->client_id, dst_id,
  2426. src_id, FALSE);
  2427. free (connect);
  2428. ret = 0;
  2429. break;
  2430. }
  2431. }
  2432. if (sort_graph) {
  2433. jack_sort_graph (engine);
  2434. }
  2435. return ret;
  2436. }
  2437. static int
  2438. jack_port_do_disconnect_all (jack_engine_t *engine,
  2439. jack_port_id_t port_id)
  2440. {
  2441. if (port_id >= engine->control->port_max) {
  2442. jack_error ("illegal port ID in attempted disconnection [%"
  2443. PRIu32 "]", port_id);
  2444. return -1;
  2445. }
  2446. VERBOSE (engine, "clear connections for %s\n",
  2447. engine->internal_ports[port_id].shared->name);
  2448. jack_lock_graph (engine);
  2449. jack_port_clear_connections (engine, &engine->internal_ports[port_id]);
  2450. jack_sort_graph (engine);
  2451. jack_unlock_graph (engine);
  2452. return 0;
  2453. }
  2454. static int
  2455. jack_port_do_disconnect (jack_engine_t *engine,
  2456. const char *source_port,
  2457. const char *destination_port)
  2458. {
  2459. jack_port_internal_t *srcport, *dstport;
  2460. int ret = -1;
  2461. if ((srcport = jack_get_port_by_name (engine, source_port)) == NULL) {
  2462. jack_error ("unknown source port in attempted disconnection"
  2463. " [%s]", source_port);
  2464. return -1;
  2465. }
  2466. if ((dstport = jack_get_port_by_name (engine, destination_port))
  2467. == NULL) {
  2468. jack_error ("unknown destination port in attempted connection"
  2469. " [%s]", destination_port);
  2470. return -1;
  2471. }
  2472. jack_lock_graph (engine);
  2473. ret = jack_port_disconnect_internal (engine, srcport, dstport, TRUE);
  2474. jack_unlock_graph (engine);
  2475. return ret;
  2476. }
  2477. int
  2478. jack_get_fifo_fd (jack_engine_t *engine, unsigned int which_fifo)
  2479. {
  2480. /* caller must hold client_lock */
  2481. char path[PATH_MAX+1];
  2482. struct stat statbuf;
  2483. snprintf (path, sizeof (path), "%s-%d", engine->fifo_prefix,
  2484. which_fifo);
  2485. DEBUG ("%s", path);
  2486. if (stat (path, &statbuf)) {
  2487. if (errno == ENOENT) {
  2488. if (mkfifo(path, 0666) < 0){
  2489. jack_error ("cannot create inter-client FIFO"
  2490. " [%s] (%s)\n", path,
  2491. strerror (errno));
  2492. return -1;
  2493. }
  2494. } else {
  2495. jack_error ("cannot check on FIFO %d\n", which_fifo);
  2496. return -1;
  2497. }
  2498. } else {
  2499. if (!S_ISFIFO(statbuf.st_mode)) {
  2500. jack_error ("FIFO %d (%s) already exists, but is not"
  2501. " a FIFO!\n", which_fifo, path);
  2502. return -1;
  2503. }
  2504. }
  2505. if (which_fifo >= engine->fifo_size) {
  2506. unsigned int i;
  2507. engine->fifo = (int *)
  2508. realloc (engine->fifo,
  2509. sizeof (int) * (engine->fifo_size + 16));
  2510. for (i = engine->fifo_size; i < engine->fifo_size + 16; i++) {
  2511. engine->fifo[i] = -1;
  2512. }
  2513. engine->fifo_size += 16;
  2514. }
  2515. if (engine->fifo[which_fifo] < 0) {
  2516. if ((engine->fifo[which_fifo] =
  2517. open (path, O_RDWR|O_CREAT|O_NONBLOCK, 0666)) < 0) {
  2518. jack_error ("cannot open fifo [%s] (%s)", path,
  2519. strerror (errno));
  2520. return -1;
  2521. }
  2522. DEBUG ("opened engine->fifo[%d] == %d (%s)",
  2523. which_fifo, engine->fifo[which_fifo], path);
  2524. }
  2525. return engine->fifo[which_fifo];
  2526. }
  2527. static void
  2528. jack_clear_fifos (jack_engine_t *engine)
  2529. {
  2530. /* caller must hold client_lock */
  2531. unsigned int i;
  2532. char buf[16];
  2533. /* this just drains the existing FIFO's of any data left in
  2534. them by aborted clients, etc. there is only ever going to
  2535. be 0, 1 or 2 bytes in them, but we'll allow for up to 16.
  2536. */
  2537. for (i = 0; i < engine->fifo_size; i++) {
  2538. if (engine->fifo[i] >= 0) {
  2539. int nread = read (engine->fifo[i], buf, sizeof (buf));
  2540. if (nread < 0 && errno != EAGAIN) {
  2541. jack_error ("clear fifo[%d] error: %s",
  2542. i, strerror (errno));
  2543. }
  2544. }
  2545. }
  2546. }
  2547. static int
  2548. jack_use_driver (jack_engine_t *engine, jack_driver_t *driver)
  2549. {
  2550. if (engine->driver) {
  2551. engine->driver->detach (engine->driver, engine);
  2552. engine->driver = 0;
  2553. }
  2554. if (driver) {
  2555. if (driver->attach (driver, engine))
  2556. return -1;
  2557. engine->rolling_interval =
  2558. jack_rolling_interval (driver->period_usecs);
  2559. }
  2560. engine->driver = driver;
  2561. return 0;
  2562. }
  2563. /* PORT RELATED FUNCTIONS */
  2564. static jack_port_id_t
  2565. jack_get_free_port (jack_engine_t *engine)
  2566. {
  2567. jack_port_id_t i;
  2568. pthread_mutex_lock (&engine->port_lock);
  2569. for (i = 0; i < engine->port_max; i++) {
  2570. if (engine->control->ports[i].in_use == 0) {
  2571. engine->control->ports[i].in_use = 1;
  2572. break;
  2573. }
  2574. }
  2575. pthread_mutex_unlock (&engine->port_lock);
  2576. if (i == engine->port_max) {
  2577. return (jack_port_id_t) -1;
  2578. }
  2579. return i;
  2580. }
  2581. void
  2582. jack_port_release (jack_engine_t *engine, jack_port_internal_t *port)
  2583. {
  2584. pthread_mutex_lock (&engine->port_lock);
  2585. port->shared->in_use = 0;
  2586. if (port->buffer_info) {
  2587. jack_port_buffer_list_t *blist =
  2588. jack_port_buffer_list (engine, port);
  2589. pthread_mutex_lock (&blist->lock);
  2590. blist->freelist =
  2591. jack_slist_prepend (blist->freelist,
  2592. port->buffer_info);
  2593. port->buffer_info = NULL;
  2594. pthread_mutex_unlock (&blist->lock);
  2595. }
  2596. pthread_mutex_unlock (&engine->port_lock);
  2597. }
  2598. jack_port_internal_t *
  2599. jack_get_port_internal_by_name (jack_engine_t *engine, const char *name)
  2600. {
  2601. jack_port_id_t id;
  2602. pthread_mutex_lock (&engine->port_lock);
  2603. for (id = 0; id < engine->port_max; id++) {
  2604. if (strcmp (engine->control->ports[id].name, name) == 0) {
  2605. break;
  2606. }
  2607. }
  2608. pthread_mutex_unlock (&engine->port_lock);
  2609. if (id != engine->port_max) {
  2610. return &engine->internal_ports[id];
  2611. } else {
  2612. return NULL;
  2613. }
  2614. }
  2615. int
  2616. jack_port_do_register (jack_engine_t *engine, jack_request_t *req)
  2617. {
  2618. jack_port_id_t port_id;
  2619. jack_port_shared_t *shared;
  2620. jack_port_internal_t *port;
  2621. jack_client_internal_t *client;
  2622. unsigned long i;
  2623. for (i = 0; i < engine->control->n_port_types; ++i) {
  2624. if (strcmp (req->x.port_info.type,
  2625. engine->control->port_types[i].type_name) == 0) {
  2626. break;
  2627. }
  2628. }
  2629. if (i == engine->control->n_port_types) {
  2630. jack_error ("cannot register a port of type \"%s\"",
  2631. req->x.port_info.type);
  2632. return -1;
  2633. }
  2634. jack_lock_graph (engine);
  2635. if ((client = jack_client_internal_by_id (engine,
  2636. req->x.port_info.client_id))
  2637. == NULL) {
  2638. jack_error ("unknown client id in port registration request");
  2639. return -1;
  2640. }
  2641. jack_unlock_graph (engine);
  2642. if ((port_id = jack_get_free_port (engine)) == (jack_port_id_t) -1) {
  2643. jack_error ("no ports available!");
  2644. return -1;
  2645. }
  2646. shared = &engine->control->ports[port_id];
  2647. strcpy (shared->name, req->x.port_info.name);
  2648. shared->ptype_id = engine->control->port_types[i].ptype_id;
  2649. shared->client_id = req->x.port_info.client_id;
  2650. shared->flags = req->x.port_info.flags;
  2651. shared->latency = 0;
  2652. shared->monitor_requests = 0;
  2653. shared->locked = 0;
  2654. port = &engine->internal_ports[port_id];
  2655. port->shared = shared;
  2656. port->connections = 0;
  2657. port->buffer_info = NULL;
  2658. if (jack_port_assign_buffer (engine, port)) {
  2659. jack_error ("cannot assign buffer for port");
  2660. return -1;
  2661. }
  2662. jack_lock_graph (engine);
  2663. client->ports = jack_slist_prepend (client->ports, port);
  2664. jack_port_registration_notify (engine, port_id, TRUE);
  2665. jack_unlock_graph (engine);
  2666. VERBOSE (engine, "registered port %s, offset = %u\n",
  2667. shared->name, (unsigned int)shared->offset);
  2668. req->x.port_info.port_id = port_id;
  2669. return 0;
  2670. }
  2671. int
  2672. jack_port_do_unregister (jack_engine_t *engine, jack_request_t *req)
  2673. {
  2674. jack_client_internal_t *client;
  2675. jack_port_shared_t *shared;
  2676. jack_port_internal_t *port;
  2677. if (req->x.port_info.port_id < 0 ||
  2678. req->x.port_info.port_id > engine->port_max) {
  2679. jack_error ("invalid port ID %" PRIu32
  2680. " in unregister request",
  2681. req->x.port_info.port_id);
  2682. return -1;
  2683. }
  2684. shared = &engine->control->ports[req->x.port_info.port_id];
  2685. if (shared->client_id != req->x.port_info.client_id) {
  2686. jack_error ("Client %" PRIu32
  2687. " is not allowed to remove port %s",
  2688. req->x.port_info.client_id, shared->name);
  2689. return -1;
  2690. }
  2691. jack_lock_graph (engine);
  2692. if ((client = jack_client_internal_by_id (engine, shared->client_id))
  2693. == NULL) {
  2694. jack_error ("unknown client id in port registration request");
  2695. jack_unlock_graph (engine);
  2696. return -1;
  2697. }
  2698. port = &engine->internal_ports[req->x.port_info.port_id];
  2699. jack_port_clear_connections (engine, port);
  2700. jack_port_release (engine,
  2701. &engine->internal_ports[req->x.port_info.port_id]);
  2702. client->ports = jack_slist_remove (client->ports, port);
  2703. jack_port_registration_notify (engine, req->x.port_info.port_id,
  2704. FALSE);
  2705. jack_unlock_graph (engine);
  2706. return 0;
  2707. }
  2708. int
  2709. jack_do_get_port_connections (jack_engine_t *engine, jack_request_t *req,
  2710. int reply_fd)
  2711. {
  2712. jack_port_internal_t *port;
  2713. JSList *node;
  2714. unsigned int i;
  2715. int ret = -1;
  2716. int internal = FALSE;
  2717. jack_lock_graph (engine);
  2718. port = &engine->internal_ports[req->x.port_info.port_id];
  2719. DEBUG ("Getting connections for port '%s'.", port->shared->name);
  2720. req->x.port_connections.nports = jack_slist_length (port->connections);
  2721. req->status = 0;
  2722. /* figure out if this is an internal or external client */
  2723. for (node = engine->clients; node; node = jack_slist_next (node)) {
  2724. if (((jack_client_internal_t *) node->data)->request_fd
  2725. == reply_fd) {
  2726. internal = jack_client_is_internal(
  2727. (jack_client_internal_t *) node->data);
  2728. break;
  2729. }
  2730. }
  2731. if (!internal) {
  2732. if (write (reply_fd, req, sizeof (*req))
  2733. < (ssize_t) sizeof (req)) {
  2734. jack_error ("cannot write GetPortConnections result "
  2735. "to client via fd = %d (%s)",
  2736. reply_fd, strerror (errno));
  2737. goto out;
  2738. }
  2739. } else {
  2740. req->x.port_connections.ports = (const char **)
  2741. malloc (sizeof (char *)
  2742. * req->x.port_connections.nports);
  2743. }
  2744. if (req->type == GetPortConnections) {
  2745. for (i = 0, node = port->connections; node;
  2746. node = jack_slist_next (node), ++i) {
  2747. jack_port_id_t port_id;
  2748. if (((jack_connection_internal_t *) node->data)->source
  2749. == port) {
  2750. port_id = ((jack_connection_internal_t *)
  2751. node->data)->destination->shared->id;
  2752. } else {
  2753. port_id = ((jack_connection_internal_t *)
  2754. node->data)->source->shared->id;
  2755. }
  2756. if (internal) {
  2757. /* internal client asking for
  2758. * names. store in malloc'ed space,
  2759. * client frees
  2760. */
  2761. req->x.port_connections.ports[i] =
  2762. engine->control->ports[port_id].name;
  2763. } else {
  2764. /* external client asking for
  2765. * names. we write the port id's to
  2766. * the reply fd.
  2767. */
  2768. if (write (reply_fd, &port_id,
  2769. sizeof (port_id))
  2770. < (ssize_t) sizeof (port_id)) {
  2771. jack_error ("cannot write port id "
  2772. "to client");
  2773. goto out;
  2774. }
  2775. }
  2776. }
  2777. }
  2778. ret = 0;
  2779. out:
  2780. req->status = ret;
  2781. jack_unlock_graph (engine);
  2782. return ret;
  2783. }
  2784. void
  2785. jack_port_registration_notify (jack_engine_t *engine,
  2786. jack_port_id_t port_id, int yn)
  2787. {
  2788. jack_event_t event;
  2789. jack_client_internal_t *client;
  2790. JSList *node;
  2791. event.type = (yn ? PortRegistered : PortUnregistered);
  2792. event.x.port_id = port_id;
  2793. for (node = engine->clients; node; node = jack_slist_next (node)) {
  2794. client = (jack_client_internal_t *) node->data;
  2795. if (!client->control->active) {
  2796. continue;
  2797. }
  2798. if (client->control->port_register) {
  2799. if (jack_deliver_event (engine, client, &event)) {
  2800. jack_error ("cannot send port registration"
  2801. " notification to %s (%s)",
  2802. client->control->name,
  2803. strerror (errno));
  2804. }
  2805. }
  2806. }
  2807. }
  2808. int
  2809. jack_port_assign_buffer (jack_engine_t *engine, jack_port_internal_t *port)
  2810. {
  2811. jack_port_buffer_list_t *blist =
  2812. jack_port_buffer_list (engine, port);
  2813. jack_port_buffer_info_t *bi;
  2814. if (port->shared->flags & JackPortIsInput) {
  2815. port->shared->offset = 0;
  2816. return 0;
  2817. }
  2818. pthread_mutex_lock (&blist->lock);
  2819. if (blist->freelist == NULL) {
  2820. jack_port_type_info_t *port_type =
  2821. jack_port_type_info (engine, port);
  2822. jack_error ("all %s port buffers in use!",
  2823. port_type->type_name);
  2824. pthread_mutex_unlock (&blist->lock);
  2825. return -1;
  2826. }
  2827. bi = (jack_port_buffer_info_t *) blist->freelist->data;
  2828. blist->freelist = jack_slist_remove (blist->freelist, bi);
  2829. port->shared->offset = bi->offset;
  2830. port->buffer_info = bi;
  2831. pthread_mutex_unlock (&blist->lock);
  2832. return 0;
  2833. }
  2834. static jack_port_internal_t *
  2835. jack_get_port_by_name (jack_engine_t *engine, const char *name)
  2836. {
  2837. jack_port_id_t id;
  2838. /* Note the potential race on "in_use". Other design
  2839. elements prevent this from being a problem.
  2840. */
  2841. for (id = 0; id < engine->port_max; id++) {
  2842. if (engine->control->ports[id].in_use &&
  2843. strcmp (engine->control->ports[id].name, name) == 0) {
  2844. return &engine->internal_ports[id];
  2845. }
  2846. }
  2847. return NULL;
  2848. }
  2849. static int
  2850. jack_send_connection_notification (jack_engine_t *engine,
  2851. jack_client_id_t client_id,
  2852. jack_port_id_t self_id,
  2853. jack_port_id_t other_id, int connected)
  2854. {
  2855. jack_client_internal_t *client;
  2856. jack_event_t event;
  2857. if ((client = jack_client_internal_by_id (engine, client_id)) == NULL) {
  2858. jack_error ("no such client %" PRIu32
  2859. " during connection notification", client_id);
  2860. return -1;
  2861. }
  2862. if (client->control->active) {
  2863. event.type = (connected ? PortConnected : PortDisconnected);
  2864. event.x.self_id = self_id;
  2865. event.y.other_id = other_id;
  2866. if (jack_deliver_event (engine, client, &event)) {
  2867. jack_error ("cannot send port connection notification"
  2868. " to client %s (%s)",
  2869. client->control->name, strerror (errno));
  2870. return -1;
  2871. }
  2872. }
  2873. return 0;
  2874. }