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.

3614 lines
87KB

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