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.

3902 lines
96KB

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