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.

3960 lines
98KB

  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. void* handle = driver->handle;
  837. driver->finish (driver);
  838. dlclose (handle);
  839. }
  840. int
  841. jack_engine_load_driver (jack_engine_t *engine,
  842. jack_driver_desc_t * driver_desc,
  843. JSList * driver_params)
  844. {
  845. jack_client_internal_t *client;
  846. jack_driver_t *driver;
  847. jack_driver_info_t *info;
  848. if ((info = jack_load_driver (engine, driver_desc)) == NULL) {
  849. return -1;
  850. }
  851. if ((client = jack_create_driver_client (engine, info->client_name)
  852. ) == NULL) {
  853. return -1;
  854. }
  855. if ((driver = info->initialize (client->control->private_client,
  856. driver_params)) == NULL) {
  857. free (info);
  858. return -1;
  859. }
  860. driver->handle = info->handle;
  861. driver->finish = info->finish;
  862. driver->internal_client = client;
  863. free (info);
  864. if (jack_use_driver (engine, driver) < 0) {
  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. if (r == 0) {
  1128. /* 0 byte read() signals EOF */
  1129. client->error++;
  1130. jack_client_disconnect(engine, fd);
  1131. return 0;
  1132. } else {
  1133. jack_error ("cannot read request from client (%d/%d/%s)",
  1134. r, sizeof(req), strerror (errno));
  1135. client->error++;
  1136. return -1;
  1137. }
  1138. }
  1139. reply_fd = client->request_fd;
  1140. do_request (engine, &req, &reply_fd);
  1141. if (reply_fd >= 0) {
  1142. DEBUG ("replying to client");
  1143. if (write (reply_fd, &req, sizeof (req))
  1144. < (ssize_t) sizeof (req)) {
  1145. jack_error ("cannot write request result to client");
  1146. return -1;
  1147. }
  1148. } else {
  1149. DEBUG ("*not* replying to client");
  1150. }
  1151. return 0;
  1152. }
  1153. static int
  1154. handle_client_ack_connection (jack_engine_t *engine, int client_fd)
  1155. {
  1156. jack_client_internal_t *client;
  1157. jack_client_connect_ack_request_t req;
  1158. jack_client_connect_ack_result_t res;
  1159. if (read (client_fd, &req, sizeof (req)) != sizeof (req)) {
  1160. jack_error ("cannot read ACK connection request from client");
  1161. return -1;
  1162. }
  1163. if ((client = jack_client_internal_by_id (engine, req.client_id))
  1164. == NULL) {
  1165. jack_error ("unknown client ID in ACK connection request");
  1166. return -1;
  1167. }
  1168. client->event_fd = client_fd;
  1169. res.status = 0;
  1170. if (write (client->event_fd, &res, sizeof (res)) != sizeof (res)) {
  1171. jack_error ("cannot write ACK connection response to client");
  1172. return -1;
  1173. }
  1174. return 0;
  1175. }
  1176. static void *
  1177. jack_server_thread (void *arg)
  1178. {
  1179. jack_engine_t *engine = (jack_engine_t *) arg;
  1180. struct sockaddr_un client_addr;
  1181. socklen_t client_addrlen;
  1182. struct pollfd *pfd;
  1183. int client_socket;
  1184. int done = 0;
  1185. int i;
  1186. int max;
  1187. engine->pfd[0].fd = engine->fds[0];
  1188. engine->pfd[0].events = POLLIN|POLLERR;
  1189. engine->pfd[1].fd = engine->fds[1];
  1190. engine->pfd[1].events = POLLIN|POLLERR;
  1191. engine->pfd_max = 2;
  1192. pfd = engine->pfd;
  1193. max = engine->pfd_max;
  1194. while (!done) {
  1195. DEBUG ("start while");
  1196. if (poll (pfd, max, 10000) < 0) {
  1197. if (errno == EINTR) {
  1198. continue;
  1199. }
  1200. jack_error ("poll failed (%s)", strerror (errno));
  1201. break;
  1202. }
  1203. DEBUG("server thread back from poll");
  1204. /* Stephane Letz: letz@grame.fr : has to be added
  1205. * otherwise pthread_cancel() does not work on MacOSX */
  1206. pthread_testcancel();
  1207. /* check each client socket before handling other request*/
  1208. for (i = 2; i < max; i++) {
  1209. if (pfd[i].fd < 0) {
  1210. continue;
  1211. }
  1212. if (pfd[i].revents & ~POLLIN) {
  1213. VERBOSE (engine, "client poll reports non-input condition, fd was %d", pfd[i].fd);
  1214. jack_client_disconnect (engine, pfd[i].fd);
  1215. } else if (pfd[i].revents & POLLIN) {
  1216. if (handle_external_client_request
  1217. (engine, pfd[i].fd)) {
  1218. jack_error ("could not handle external"
  1219. " client request");
  1220. #ifdef JACK_USE_MACH_THREADS
  1221. /* poll is implemented using
  1222. select (see the macosx/fakepoll
  1223. code). When the socket is closed
  1224. select does not return any error,
  1225. POLLIN is true and the next read
  1226. will return 0 bytes. This
  1227. behaviour is diffrent from the
  1228. Linux poll behaviour. Thus we use
  1229. this condition as a socket error
  1230. and remove the client.
  1231. */
  1232. jack_client_disconnect(engine, pfd[i].fd);
  1233. #endif /* JACK_USE_MACH_THREADS */
  1234. }
  1235. }
  1236. }
  1237. /* check the master server socket */
  1238. if (pfd[0].revents & POLLERR) {
  1239. jack_error ("error on server socket");
  1240. break;
  1241. }
  1242. if (engine->control->engine_ok && pfd[0].revents & POLLIN) {
  1243. DEBUG ("pfd[0].revents & POLLIN");
  1244. memset (&client_addr, 0, sizeof (client_addr));
  1245. client_addrlen = sizeof (client_addr);
  1246. if ((client_socket =
  1247. accept (engine->fds[0],
  1248. (struct sockaddr *) &client_addr,
  1249. &client_addrlen)) < 0) {
  1250. jack_error ("cannot accept new connection (%s)",
  1251. strerror (errno));
  1252. } else if (jack_client_create (engine, client_socket) < 0) {
  1253. jack_error ("cannot complete client "
  1254. "connection process");
  1255. close (client_socket);
  1256. }
  1257. }
  1258. /* Possibly, jack_client_create() may have
  1259. * realloced engine->pfd. We depend on that being
  1260. * done within this thread. That is currently true,
  1261. * since external clients are only created here.
  1262. */
  1263. pfd = engine->pfd;
  1264. max = engine->pfd_max;
  1265. /* check the ACK server socket */
  1266. if (pfd[1].revents & POLLERR) {
  1267. jack_error ("error on server ACK socket");
  1268. break;
  1269. }
  1270. if (engine->control->engine_ok && pfd[1].revents & POLLIN) {
  1271. DEBUG ("pfd[1].revents & POLLIN");
  1272. memset (&client_addr, 0, sizeof (client_addr));
  1273. client_addrlen = sizeof (client_addr);
  1274. if ((client_socket =
  1275. accept (engine->fds[1],
  1276. (struct sockaddr *) &client_addr,
  1277. &client_addrlen)) < 0) {
  1278. jack_error ("cannot accept new ACK connection"
  1279. " (%s)", strerror (errno));
  1280. } else if (handle_client_ack_connection
  1281. (engine, client_socket)) {
  1282. jack_error ("cannot complete client ACK "
  1283. "connection process");
  1284. close (client_socket);
  1285. }
  1286. }
  1287. }
  1288. return 0;
  1289. }
  1290. jack_engine_t *
  1291. jack_engine_new (int realtime, int rtpriority, int do_mlock, int do_unlock,
  1292. const char *server_name, int temporary, int verbose,
  1293. int client_timeout, unsigned int port_max, pid_t wait_pid,
  1294. jack_nframes_t frame_time_offset, int nozombies, JSList *drivers)
  1295. {
  1296. jack_engine_t *engine;
  1297. unsigned int i;
  1298. char server_dir[PATH_MAX+1] = "";
  1299. #ifdef USE_CAPABILITIES
  1300. uid_t uid = getuid ();
  1301. uid_t euid = geteuid ();
  1302. #endif /* USE_CAPABILITIES */
  1303. /* before we start allocating resources, make sure that if realtime was requested that we can
  1304. actually do it.
  1305. */
  1306. if (realtime) {
  1307. if (jack_acquire_real_time_scheduling (pthread_self(), 10) != 0) {
  1308. /* can't run realtime - time to bomb */
  1309. return NULL;
  1310. }
  1311. jack_drop_real_time_scheduling (pthread_self());
  1312. #ifdef USE_MLOCK
  1313. if (do_mlock && (mlockall (MCL_CURRENT | MCL_FUTURE) != 0)) {
  1314. jack_error ("cannot lock down memory for jackd (%s)",
  1315. strerror (errno));
  1316. #ifdef ENSURE_MLOCK
  1317. return NULL;
  1318. #endif /* ENSURE_MLOCK */
  1319. }
  1320. #endif /* USE_MLOCK */
  1321. }
  1322. /* start a thread to display messages from realtime threads */
  1323. jack_messagebuffer_init();
  1324. jack_init_time ();
  1325. engine = (jack_engine_t *) malloc (sizeof (jack_engine_t));
  1326. engine->drivers = drivers;
  1327. engine->driver = NULL;
  1328. engine->driver_desc = NULL;
  1329. engine->driver_params = NULL;
  1330. engine->set_sample_rate = jack_set_sample_rate;
  1331. engine->set_buffer_size = jack_driver_buffer_size;
  1332. engine->run_cycle = jack_run_cycle;
  1333. engine->delay = jack_engine_delay;
  1334. engine->driver_exit = jack_engine_driver_exit;
  1335. engine->transport_cycle_start = jack_transport_cycle_start;
  1336. engine->client_timeout_msecs = client_timeout;
  1337. engine->next_client_id = 1; /* 0 is a NULL client ID */
  1338. engine->port_max = port_max;
  1339. engine->rtpriority = rtpriority;
  1340. engine->silent_buffer = 0;
  1341. engine->verbose = verbose;
  1342. engine->server_name = server_name;
  1343. engine->temporary = temporary;
  1344. engine->freewheeling = 0;
  1345. engine->feedbackcount = 0;
  1346. engine->wait_pid = wait_pid;
  1347. engine->nozombies = nozombies;
  1348. engine->removing_clients = 0;
  1349. engine->audio_out_cnt = 0;
  1350. engine->audio_in_cnt = 0;
  1351. engine->midi_out_cnt = 0;
  1352. engine->midi_in_cnt = 0;
  1353. jack_engine_reset_rolling_usecs (engine);
  1354. engine->max_usecs = 0.0f;
  1355. pthread_mutex_init (&engine->client_lock, 0);
  1356. pthread_mutex_init (&engine->port_lock, 0);
  1357. pthread_mutex_init (&engine->request_lock, 0);
  1358. engine->clients = 0;
  1359. engine->pfd_size = 16;
  1360. engine->pfd_max = 0;
  1361. engine->pfd = (struct pollfd *) malloc (sizeof (struct pollfd)
  1362. * engine->pfd_size);
  1363. engine->fifo_size = 16;
  1364. engine->fifo = (int *) malloc (sizeof (int) * engine->fifo_size);
  1365. for (i = 0; i < engine->fifo_size; i++) {
  1366. engine->fifo[i] = -1;
  1367. }
  1368. engine->external_client_cnt = 0;
  1369. srandom (time ((time_t *) 0));
  1370. if (jack_shmalloc (sizeof (jack_control_t)
  1371. + ((sizeof (jack_port_shared_t) * engine->port_max)),
  1372. &engine->control_shm)) {
  1373. jack_error ("cannot create engine control shared memory "
  1374. "segment (%s)", strerror (errno));
  1375. return NULL;
  1376. }
  1377. if (jack_attach_shm (&engine->control_shm)) {
  1378. jack_error ("cannot attach to engine control shared memory"
  1379. " (%s)", strerror (errno));
  1380. jack_destroy_shm (&engine->control_shm);
  1381. return NULL;
  1382. }
  1383. engine->control = (jack_control_t *)
  1384. jack_shm_addr (&engine->control_shm);
  1385. /* Setup port type information from builtins. buffer space is
  1386. * allocated when the driver calls jack_driver_buffer_size().
  1387. */
  1388. for (i = 0; jack_builtin_port_types[i].type_name[0]; ++i) {
  1389. memcpy (&engine->control->port_types[i],
  1390. &jack_builtin_port_types[i],
  1391. sizeof (jack_port_type_info_t));
  1392. VERBOSE (engine, "registered builtin port type %s",
  1393. engine->control->port_types[i].type_name);
  1394. /* the port type id is index into port_types array */
  1395. engine->control->port_types[i].ptype_id = i;
  1396. /* be sure to initialize mutex correctly */
  1397. pthread_mutex_init (&engine->port_buffers[i].lock, NULL);
  1398. /* set buffer list info correctly */
  1399. engine->port_buffers[i].freelist = NULL;
  1400. engine->port_buffers[i].info = NULL;
  1401. /* mark each port segment as not allocated */
  1402. engine->port_segment[i].index = -1;
  1403. engine->port_segment[i].attached_at = 0;
  1404. }
  1405. engine->control->n_port_types = i;
  1406. /* Mark all ports as available */
  1407. for (i = 0; i < engine->port_max; i++) {
  1408. engine->control->ports[i].in_use = 0;
  1409. engine->control->ports[i].id = i;
  1410. engine->control->ports[i].alias1[0] = '\0';
  1411. engine->control->ports[i].alias2[0] = '\0';
  1412. }
  1413. /* allocate internal port structures so that we can keep track
  1414. * of port connections.
  1415. */
  1416. engine->internal_ports = (jack_port_internal_t *)
  1417. malloc (sizeof (jack_port_internal_t) * engine->port_max);
  1418. for (i = 0; i < engine->port_max; i++) {
  1419. engine->internal_ports[i].connections = 0;
  1420. }
  1421. if (make_sockets (engine->server_name, engine->fds) < 0) {
  1422. jack_error ("cannot create server sockets");
  1423. return NULL;
  1424. }
  1425. engine->control->port_max = engine->port_max;
  1426. engine->control->real_time = realtime;
  1427. engine->control->client_priority = (realtime
  1428. ? engine->rtpriority - 1
  1429. : 0);
  1430. engine->control->do_mlock = do_mlock;
  1431. engine->control->do_munlock = do_unlock;
  1432. engine->control->cpu_load = 0;
  1433. engine->control->xrun_delayed_usecs = 0;
  1434. engine->control->max_delayed_usecs = 0;
  1435. jack_set_clock_source (clock_source);
  1436. engine->control->clock_source = clock_source;
  1437. VERBOSE (engine, "clock source = %s", jack_clock_source_name (clock_source));
  1438. engine->control->frame_timer.frames = frame_time_offset;
  1439. engine->control->frame_timer.reset_pending = 0;
  1440. engine->control->frame_timer.current_wakeup = 0;
  1441. engine->control->frame_timer.next_wakeup = 0;
  1442. engine->control->frame_timer.initialized = 0;
  1443. engine->control->frame_timer.filter_coefficient = 0.01;
  1444. engine->control->frame_timer.second_order_integrator = 0;
  1445. engine->first_wakeup = 1;
  1446. engine->control->buffer_size = 0;
  1447. jack_transport_init (engine);
  1448. jack_set_sample_rate (engine, 0);
  1449. engine->control->internal = 0;
  1450. engine->control->has_capabilities = 0;
  1451. #ifdef JACK_USE_MACH_THREADS
  1452. /* specific resources for server/client real-time thread
  1453. * communication */
  1454. engine->servertask = mach_task_self();
  1455. if (task_get_bootstrap_port(engine->servertask, &engine->bp)){
  1456. jack_error("Jackd: Can't find bootstrap mach port");
  1457. return NULL;
  1458. }
  1459. engine->portnum = 0;
  1460. #endif /* JACK_USE_MACH_THREADS */
  1461. #ifdef USE_CAPABILITIES
  1462. if (uid == 0 || euid == 0) {
  1463. VERBOSE (engine, "running with uid=%d and euid=%d, "
  1464. "will not try to use capabilites",
  1465. uid, euid);
  1466. } else {
  1467. /* only try to use capabilities if not running as root */
  1468. engine->control->has_capabilities = check_capabilities (engine);
  1469. if (engine->control->has_capabilities == 0) {
  1470. VERBOSE (engine, "required capabilities not "
  1471. "available");
  1472. }
  1473. if (engine->verbose) {
  1474. size_t size;
  1475. cap_t cap = cap_init();
  1476. capgetp(0, cap);
  1477. VERBOSE (engine, "capabilities: %s",
  1478. cap_to_text(cap, &size));
  1479. }
  1480. }
  1481. #endif /* USE_CAPABILITIES */
  1482. engine->control->engine_ok = 1;
  1483. snprintf (engine->fifo_prefix, sizeof (engine->fifo_prefix),
  1484. "%s/jack-ack-fifo-%d",
  1485. jack_server_dir (engine->server_name, server_dir), getpid ());
  1486. (void) jack_get_fifo_fd (engine, 0);
  1487. jack_client_create_thread (NULL, &engine->server_thread, 0, FALSE,
  1488. &jack_server_thread, engine);
  1489. return engine;
  1490. }
  1491. static void
  1492. jack_engine_delay (jack_engine_t *engine, float delayed_usecs)
  1493. {
  1494. jack_event_t event;
  1495. engine->control->frame_timer.reset_pending = 1;
  1496. engine->control->xrun_delayed_usecs = delayed_usecs;
  1497. if (delayed_usecs > engine->control->max_delayed_usecs)
  1498. engine->control->max_delayed_usecs = delayed_usecs;
  1499. event.type = XRun;
  1500. jack_deliver_event_to_all (engine, &event);
  1501. }
  1502. static inline void
  1503. jack_inc_frame_time (jack_engine_t *engine, jack_nframes_t nframes)
  1504. {
  1505. jack_frame_timer_t *timer = &engine->control->frame_timer;
  1506. jack_time_t now = engine->driver->last_wait_ust; // effective time
  1507. float delta;
  1508. // really need a memory barrier here
  1509. timer->guard1++;
  1510. delta = (int64_t) now - (int64_t) timer->next_wakeup;
  1511. timer->current_wakeup = timer->next_wakeup;
  1512. timer->frames += nframes;
  1513. timer->second_order_integrator += 0.5f *
  1514. timer->filter_coefficient * delta;
  1515. timer->next_wakeup = timer->current_wakeup +
  1516. engine->driver->period_usecs +
  1517. (int64_t) floorf ((timer->filter_coefficient *
  1518. (delta + timer->second_order_integrator)));
  1519. timer->initialized = 1;
  1520. // might need a memory barrier here
  1521. timer->guard2++;
  1522. }
  1523. static void*
  1524. jack_engine_freewheel (void *arg)
  1525. {
  1526. jack_engine_t* engine = (jack_engine_t *) arg;
  1527. VERBOSE (engine, "freewheel thread starting ...");
  1528. /* we should not be running SCHED_FIFO, so we don't
  1529. have to do anything about scheduling.
  1530. */
  1531. while (engine->freewheeling) {
  1532. jack_lock_graph (engine);
  1533. if (jack_engine_process (engine,
  1534. engine->control->buffer_size)) {
  1535. jack_error ("process cycle within freewheel failed");
  1536. jack_unlock_graph (engine);
  1537. break;
  1538. }
  1539. jack_unlock_graph (engine);
  1540. }
  1541. VERBOSE (engine, "freewheel came to an end, naturally");
  1542. return 0;
  1543. }
  1544. static int
  1545. jack_start_freewheeling (jack_engine_t* engine)
  1546. {
  1547. jack_event_t event;
  1548. if (engine->freewheeling) {
  1549. return 0;
  1550. }
  1551. if (engine->driver == NULL) {
  1552. jack_error ("cannot start freewheeling without a driver!");
  1553. return -1;
  1554. }
  1555. /* stop driver before telling anyone about it so
  1556. there are no more process() calls being handled.
  1557. */
  1558. if (engine->driver->stop (engine->driver)) {
  1559. jack_error ("could not stop driver for freewheeling");
  1560. return -1;
  1561. }
  1562. engine->freewheeling = 1;
  1563. event.type = StartFreewheel;
  1564. jack_deliver_event_to_all (engine, &event);
  1565. if (jack_client_create_thread (NULL, &engine->freewheel_thread, 0, FALSE,
  1566. jack_engine_freewheel, engine)) {
  1567. jack_error ("could not start create freewheel thread");
  1568. return -1;
  1569. }
  1570. return 0;
  1571. }
  1572. static int
  1573. jack_stop_freewheeling (jack_engine_t* engine)
  1574. {
  1575. jack_event_t event;
  1576. void *ftstatus;
  1577. if (!engine->freewheeling) {
  1578. return 0;
  1579. }
  1580. if (engine->driver == NULL) {
  1581. jack_error ("cannot start freewheeling without a driver!");
  1582. return -1;
  1583. }
  1584. if (!engine->freewheeling) {
  1585. VERBOSE (engine, "stop freewheel when not freewheeling");
  1586. return 0;
  1587. }
  1588. /* tell the freewheel thread to stop, and wait for it
  1589. to exit.
  1590. */
  1591. engine->freewheeling = 0;
  1592. VERBOSE (engine, "freewheeling stopped, waiting for thread");
  1593. pthread_join (engine->freewheel_thread, &ftstatus);
  1594. VERBOSE (engine, "freewheel thread has returned");
  1595. /* tell everyone we've stopped */
  1596. event.type = StopFreewheel;
  1597. jack_deliver_event_to_all (engine, &event);
  1598. /* restart the driver */
  1599. if (engine->driver->start (engine->driver)) {
  1600. jack_error ("could not restart driver after freewheeling");
  1601. return -1;
  1602. }
  1603. return 0;
  1604. }
  1605. static int
  1606. jack_run_one_cycle (jack_engine_t *engine, jack_nframes_t nframes,
  1607. float delayed_usecs)
  1608. {
  1609. jack_driver_t* driver = engine->driver;
  1610. int ret = -1;
  1611. static int consecutive_excessive_delays = 0;
  1612. #define WORK_SCALE 1.0f
  1613. if (engine->control->real_time &&
  1614. engine->spare_usecs &&
  1615. ((WORK_SCALE * engine->spare_usecs) <= delayed_usecs)) {
  1616. MESSAGE("delay of %.3f usecs exceeds estimated spare"
  1617. " time of %.3f; restart ...\n",
  1618. delayed_usecs, WORK_SCALE * engine->spare_usecs);
  1619. if (++consecutive_excessive_delays > 10) {
  1620. jack_error ("too many consecutive interrupt delays "
  1621. "... engine pausing");
  1622. return -1; /* will exit the thread loop */
  1623. }
  1624. jack_engine_delay (engine, delayed_usecs);
  1625. return 0;
  1626. } else {
  1627. consecutive_excessive_delays = 0;
  1628. }
  1629. if (jack_try_lock_graph (engine)) {
  1630. /* engine can't run. just throw away an entire cycle */
  1631. driver->null_cycle (driver, nframes);
  1632. return 0;
  1633. }
  1634. if (!engine->freewheeling) {
  1635. DEBUG("waiting for driver read\n");
  1636. if (driver->read (driver, nframes)) {
  1637. goto unlock;
  1638. }
  1639. }
  1640. DEBUG("run process\n");
  1641. if (jack_engine_process (engine, nframes) == 0) {
  1642. if (!engine->freewheeling) {
  1643. if (driver->write (driver, nframes)) {
  1644. goto unlock;
  1645. }
  1646. }
  1647. } else {
  1648. JSList *node;
  1649. DEBUG ("engine process cycle failed");
  1650. /* we are already late, or something else went wrong,
  1651. so it can't hurt to check the existence of all
  1652. clients.
  1653. */
  1654. for (node = engine->clients; node;
  1655. node = jack_slist_next (node)) {
  1656. jack_client_internal_t *client =
  1657. (jack_client_internal_t *) node->data;
  1658. if (client->control->type == ClientExternal) {
  1659. if (kill (client->control->pid, 0)) {
  1660. VERBOSE(engine,
  1661. "client %s has died/exited",
  1662. client->control->name);
  1663. client->error++;
  1664. }
  1665. if(client->control->last_status != 0) {
  1666. VERBOSE(engine,
  1667. "client %s has nonzero process callback status (%d)\n",
  1668. client->control->name, client->control->last_status);
  1669. client->error++;
  1670. }
  1671. }
  1672. DEBUG ("client %s errors = %d", client->control->name,
  1673. client->error);
  1674. }
  1675. }
  1676. jack_engine_post_process (engine);
  1677. if (delayed_usecs > engine->control->max_delayed_usecs)
  1678. engine->control->max_delayed_usecs = delayed_usecs;
  1679. ret = 0;
  1680. unlock:
  1681. jack_unlock_graph (engine);
  1682. DEBUG("cycle finished, status = %d", ret);
  1683. return ret;
  1684. }
  1685. static void
  1686. jack_engine_driver_exit (jack_engine_t* engine)
  1687. {
  1688. /* tell anyone waiting that the driver exited. */
  1689. kill (engine->wait_pid, SIGUSR2);
  1690. engine->driver = NULL;
  1691. }
  1692. static int
  1693. jack_run_cycle (jack_engine_t *engine, jack_nframes_t nframes,
  1694. float delayed_usecs)
  1695. {
  1696. jack_nframes_t left;
  1697. jack_nframes_t b_size = engine->control->buffer_size;
  1698. jack_frame_timer_t* timer = &engine->control->frame_timer;
  1699. int no_increment = 0;
  1700. if (engine->first_wakeup) {
  1701. /* the first wakeup */
  1702. timer->next_wakeup =
  1703. engine->driver->last_wait_ust +
  1704. engine->driver->period_usecs;
  1705. engine->first_wakeup = 0;
  1706. /* if we got an xrun/delayed wakeup on the first cycle,
  1707. reset the pending flag (we have no predicted wakeups
  1708. to use), but avoid incrementing the frame timer.
  1709. */
  1710. if (timer->reset_pending) {
  1711. timer->reset_pending = 0;
  1712. no_increment = 1;
  1713. }
  1714. }
  1715. if (timer->reset_pending) {
  1716. /* post xrun-handling */
  1717. /* don't bother to increment the frame counter, because we missed 1 or more
  1718. deadlines in the backend anyway.
  1719. */
  1720. timer->current_wakeup = engine->driver->last_wait_ust;
  1721. timer->next_wakeup = engine->driver->last_wait_ust +
  1722. engine->driver->period_usecs;
  1723. timer->reset_pending = 0;
  1724. } else {
  1725. /* normal condition */
  1726. if (!no_increment) {
  1727. jack_inc_frame_time (engine, nframes);
  1728. }
  1729. }
  1730. if (engine->verbose) {
  1731. if (nframes != b_size) {
  1732. VERBOSE (engine,
  1733. "late driver wakeup: nframes to process = %"
  1734. PRIu32 ".", nframes);
  1735. }
  1736. }
  1737. /* run as many cycles as it takes to consume nframes */
  1738. for (left = nframes; left >= b_size; left -= b_size) {
  1739. if (jack_run_one_cycle (engine, b_size, delayed_usecs)) {
  1740. jack_error ("cycle execution failure, exiting");
  1741. return EIO;
  1742. }
  1743. }
  1744. return 0;
  1745. }
  1746. void
  1747. jack_engine_delete (jack_engine_t *engine)
  1748. {
  1749. int i;
  1750. if (engine == NULL)
  1751. return;
  1752. VERBOSE (engine, "starting server engine shutdown");
  1753. engine->control->engine_ok = 0; /* tell clients we're going away */
  1754. /* shutdown master socket to prevent new clients arriving */
  1755. shutdown (engine->fds[0], SHUT_RDWR);
  1756. // close (engine->fds[0]);
  1757. /* now really tell them we're going away */
  1758. for (i = 0; i < engine->pfd_max; ++i) {
  1759. shutdown (engine->pfd[i].fd, SHUT_RDWR);
  1760. }
  1761. if (engine->driver) {
  1762. jack_driver_t* driver = engine->driver;
  1763. VERBOSE (engine, "stopping driver");
  1764. driver->stop (driver);
  1765. // VERBOSE (engine, "detaching driver");
  1766. // driver->detach (driver, engine);
  1767. VERBOSE (engine, "unloading driver");
  1768. jack_driver_unload (driver);
  1769. engine->driver = NULL;
  1770. }
  1771. VERBOSE (engine, "freeing shared port segments");
  1772. for (i = 0; i < engine->control->n_port_types; ++i) {
  1773. jack_release_shm (&engine->port_segment[i]);
  1774. jack_destroy_shm (&engine->port_segment[i]);
  1775. }
  1776. /* stop the other engine threads */
  1777. VERBOSE (engine, "stopping server thread");
  1778. #if JACK_USE_MACH_THREADS
  1779. // MacOSX pthread_cancel still not implemented correctly in Darwin
  1780. mach_port_t machThread = pthread_mach_thread_np (engine->server_thread);
  1781. thread_terminate (machThread);
  1782. #else
  1783. pthread_cancel (engine->server_thread);
  1784. pthread_join (engine->server_thread, NULL);
  1785. #endif
  1786. #ifndef JACK_USE_MACH_THREADS
  1787. /* Cancel the watchdog thread and wait for it to terminate.
  1788. *
  1789. * The watchdog thread is not used on MacOSX since CoreAudio
  1790. * drivers already contain a similar mechanism.
  1791. */
  1792. if (engine->control->real_time && engine->watchdog_thread) {
  1793. VERBOSE (engine, "stopping watchdog thread");
  1794. pthread_cancel (engine->watchdog_thread);
  1795. pthread_join (engine->watchdog_thread, NULL);
  1796. }
  1797. #endif
  1798. VERBOSE (engine, "last xrun delay: %.3f usecs",
  1799. engine->control->xrun_delayed_usecs);
  1800. VERBOSE (engine, "max delay reported by backend: %.3f usecs",
  1801. engine->control->max_delayed_usecs);
  1802. /* free engine control shm segment */
  1803. engine->control = NULL;
  1804. VERBOSE (engine, "freeing engine shared memory");
  1805. jack_release_shm (&engine->control_shm);
  1806. jack_destroy_shm (&engine->control_shm);
  1807. VERBOSE (engine, "max usecs: %.3f, engine deleted", engine->max_usecs);
  1808. free (engine);
  1809. jack_messagebuffer_exit();
  1810. }
  1811. void
  1812. jack_port_clear_connections (jack_engine_t *engine,
  1813. jack_port_internal_t *port)
  1814. {
  1815. JSList *node, *next;
  1816. for (node = port->connections; node; ) {
  1817. next = jack_slist_next (node);
  1818. jack_port_disconnect_internal (
  1819. engine, ((jack_connection_internal_t *)
  1820. node->data)->source,
  1821. ((jack_connection_internal_t *)
  1822. node->data)->destination);
  1823. node = next;
  1824. }
  1825. jack_slist_free (port->connections);
  1826. port->connections = 0;
  1827. }
  1828. static void
  1829. jack_deliver_event_to_all (jack_engine_t *engine, jack_event_t *event)
  1830. {
  1831. JSList *node;
  1832. jack_lock_graph (engine);
  1833. for (node = engine->clients; node; node = jack_slist_next (node)) {
  1834. jack_deliver_event (engine,
  1835. (jack_client_internal_t *) node->data,
  1836. event);
  1837. }
  1838. jack_unlock_graph (engine);
  1839. jack_remove_clients (engine);
  1840. }
  1841. static void
  1842. 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)
  1843. {
  1844. JSList *node;
  1845. jack_event_t event;
  1846. event.type = (connected ? PortConnected : PortDisconnected);
  1847. event.x.self_id = a;
  1848. event.y.other_id = b;
  1849. /* GRAPH MUST BE LOCKED : see callers of jack_send_connection_notification()
  1850. */
  1851. jack_client_internal_t* src_client = jack_client_internal_by_id (engine, src);
  1852. jack_client_internal_t* dst_client = jack_client_internal_by_id (engine, dst);
  1853. for (node = engine->clients; node; node = jack_slist_next (node)) {
  1854. jack_client_internal_t* client = (jack_client_internal_t*) node->data;
  1855. if (src_client != client && dst_client != client && client->control->port_connect != NULL) {
  1856. /* one of the ports belong to this client or it has a port connect callback */
  1857. jack_deliver_event (engine, client, &event);
  1858. }
  1859. }
  1860. }
  1861. static int
  1862. jack_deliver_event (jack_engine_t *engine, jack_client_internal_t *client,
  1863. jack_event_t *event)
  1864. {
  1865. char status;
  1866. /* caller must hold the graph lock */
  1867. DEBUG ("delivering event (type %d)", event->type);
  1868. /* we are not RT-constrained here, so use kill(2) to beef up
  1869. our check on a client's continued well-being
  1870. */
  1871. if (client->control->dead || client->error >= JACK_ERROR_WITH_SOCKETS
  1872. || (client->control->type == ClientExternal && kill (client->control->pid, 0))) {
  1873. DEBUG ("client %s is dead - no event sent",
  1874. client->control->name);
  1875. return 0;
  1876. }
  1877. DEBUG ("client %s is still alive", client->control->name);
  1878. if (jack_client_is_internal (client)) {
  1879. switch (event->type) {
  1880. case PortConnected:
  1881. case PortDisconnected:
  1882. jack_client_handle_port_connection
  1883. (client->control->private_client, event);
  1884. break;
  1885. case BufferSizeChange:
  1886. jack_client_invalidate_port_buffers
  1887. (client->control->private_client);
  1888. if (client->control->bufsize) {
  1889. client->control->bufsize
  1890. (event->x.n,
  1891. client->control->bufsize_arg);
  1892. }
  1893. break;
  1894. case SampleRateChange:
  1895. if (client->control->srate) {
  1896. client->control->srate
  1897. (event->x.n,
  1898. client->control->srate_arg);
  1899. }
  1900. break;
  1901. case GraphReordered:
  1902. if (client->control->graph_order) {
  1903. client->control->graph_order
  1904. (client->control->graph_order_arg);
  1905. }
  1906. break;
  1907. case XRun:
  1908. if (client->control->xrun) {
  1909. client->control->xrun
  1910. (client->control->xrun_arg);
  1911. }
  1912. break;
  1913. default:
  1914. /* internal clients don't need to know */
  1915. break;
  1916. }
  1917. } else {
  1918. if (client->control->active) {
  1919. /* there's a thread waiting for events, so
  1920. * it's worth telling the client */
  1921. DEBUG ("engine writing on event fd");
  1922. if (write (client->event_fd, event, sizeof (*event))
  1923. != sizeof (*event)) {
  1924. jack_error ("cannot send event to client [%s]"
  1925. " (%s)", client->control->name,
  1926. strerror (errno));
  1927. client->error = JACK_ERROR_WITH_SOCKETS+99;
  1928. }
  1929. if (client->error) {
  1930. status = 1;
  1931. } else {
  1932. // then we check whether there really is an error.... :)
  1933. struct pollfd pfd[1];
  1934. pfd[0].fd = client->event_fd;
  1935. //pfd[0].events = POLLERR|POLLIN|POLLHUP|POLLNVAL;
  1936. pfd[0].events = POLLIN;
  1937. int poll_timeout = (engine->client_timeout_msecs > 0 ?
  1938. engine->client_timeout_msecs :
  1939. 1 + engine->driver->period_usecs/1000);
  1940. //poll_timeout = 200;
  1941. //poll_timeout = 30000; // 30 seconds
  1942. int poll_ret;
  1943. // printf("################ poll_timeout = %d (%d or 1 + %d/1000)\n", poll_timeout, engine->client_timeout_msecs, engine->driver->period_usecs);
  1944. if ( (poll_ret = poll (pfd, 1, poll_timeout)) < 0) {
  1945. DEBUG ("client event poll not ok! (-1) poll returned an error");
  1946. jack_error ("poll on subgraph processing failed (%s)", strerror (errno));
  1947. status = -1;
  1948. } else {
  1949. DEBUG ("\n\n\n\n\n back from client event poll, revents = 0x%x\n\n\n", pfd[0].revents);
  1950. if (pfd[0].revents & ~POLLIN) {
  1951. DEBUG ("client event poll not ok! (-2), revents = %d\n", pfd[0].revents);
  1952. jack_error ("subgraph starting at %s lost client", client->control->name);
  1953. status = -2;
  1954. }
  1955. if (pfd[0].revents & POLLIN) {
  1956. DEBUG ("client event poll ok!");
  1957. status = 0;
  1958. } else {
  1959. DEBUG ("client event poll not ok! (1 = poll timed out, revents = 0x%04x, poll_ret = %d)", pfd[0].revents, poll_ret);
  1960. jack_error ("subgraph starting at %s timed out "
  1961. "(subgraph_wait_fd=%d, status = %d, state = %s, revents = 0x%04x)",
  1962. client->control->name,
  1963. client->subgraph_wait_fd, status,
  1964. jack_client_state_name (client), pfd[0].revents);
  1965. status = 1;
  1966. }
  1967. }
  1968. }
  1969. if (status == 0) {
  1970. if (read (client->event_fd, &status, sizeof (status)) != sizeof (status)) {
  1971. jack_error ("cannot read event response from "
  1972. "client [%s] (%s)",
  1973. client->control->name,
  1974. strerror (errno));
  1975. client->error = JACK_ERROR_WITH_SOCKETS+99;
  1976. }
  1977. } else {
  1978. jack_error ("bad status (%d) for client event "
  1979. "handling (type = %d)",
  1980. status,event->type);
  1981. client->error = JACK_ERROR_WITH_SOCKETS+99;
  1982. }
  1983. }
  1984. }
  1985. DEBUG ("event delivered");
  1986. return 0;
  1987. }
  1988. int
  1989. jack_rechain_graph (jack_engine_t *engine)
  1990. {
  1991. JSList *node, *next;
  1992. unsigned long n;
  1993. int err = 0;
  1994. jack_client_internal_t *client, *subgraph_client, *next_client;
  1995. jack_event_t event;
  1996. int upstream_is_jackd;
  1997. jack_clear_fifos (engine);
  1998. subgraph_client = 0;
  1999. VERBOSE(engine, "++ jack_rechain_graph():");
  2000. event.type = GraphReordered;
  2001. for (n = 0, node = engine->clients, next = NULL; node; node = next) {
  2002. next = jack_slist_next (node);
  2003. VERBOSE(engine, "+++ client is now %s active ? %d",
  2004. ((jack_client_internal_t *) node->data)->control->name,
  2005. ((jack_client_internal_t *) node->data)->control->active);
  2006. if (((jack_client_internal_t *) node->data)->control->active) {
  2007. client = (jack_client_internal_t *) node->data;
  2008. /* find the next active client. its ok for
  2009. * this to be NULL */
  2010. while (next) {
  2011. if (((jack_client_internal_t *)
  2012. next->data)->control->active) {
  2013. break;
  2014. }
  2015. next = jack_slist_next (next);
  2016. };
  2017. if (next == NULL) {
  2018. next_client = NULL;
  2019. } else {
  2020. next_client = (jack_client_internal_t *)
  2021. next->data;
  2022. }
  2023. client->execution_order = n;
  2024. client->next_client = next_client;
  2025. if (jack_client_is_internal (client)) {
  2026. /* break the chain for the current
  2027. * subgraph. the server will wait for
  2028. * chain on the nth FIFO, and will
  2029. * then execute this internal
  2030. * client. */
  2031. if (subgraph_client) {
  2032. subgraph_client->subgraph_wait_fd =
  2033. jack_get_fifo_fd (engine, n);
  2034. VERBOSE (engine, "client %s: wait_fd="
  2035. "%d, execution_order="
  2036. "%lu.",
  2037. subgraph_client->
  2038. control->name,
  2039. subgraph_client->
  2040. subgraph_wait_fd, n);
  2041. n++;
  2042. }
  2043. VERBOSE (engine, "client %s: internal "
  2044. "client, execution_order="
  2045. "%lu.",
  2046. client->control->name, n);
  2047. /* this does the right thing for
  2048. * internal clients too
  2049. */
  2050. jack_deliver_event (engine, client, &event);
  2051. subgraph_client = 0;
  2052. } else {
  2053. if (subgraph_client == NULL) {
  2054. /* start a new subgraph. the
  2055. * engine will start the chain
  2056. * by writing to the nth
  2057. * FIFO.
  2058. */
  2059. subgraph_client = client;
  2060. subgraph_client->subgraph_start_fd =
  2061. jack_get_fifo_fd (engine, n);
  2062. VERBOSE (engine, "client %s: "
  2063. "start_fd=%d, execution"
  2064. "_order=%lu.",
  2065. subgraph_client->
  2066. control->name,
  2067. subgraph_client->
  2068. subgraph_start_fd, n);
  2069. /* this external client after
  2070. this will have jackd as its
  2071. upstream connection.
  2072. */
  2073. upstream_is_jackd = 1;
  2074. }
  2075. else {
  2076. VERBOSE (engine, "client %s: in"
  2077. " subgraph after %s, "
  2078. "execution_order="
  2079. "%lu.",
  2080. client->control->name,
  2081. subgraph_client->
  2082. control->name, n);
  2083. subgraph_client->subgraph_wait_fd = -1;
  2084. /* this external client after
  2085. this will have another
  2086. client as its upstream
  2087. connection.
  2088. */
  2089. upstream_is_jackd = 0;
  2090. }
  2091. /* make sure fifo for 'n + 1' exists
  2092. * before issuing client reorder
  2093. */
  2094. (void) jack_get_fifo_fd(
  2095. engine, client->execution_order + 1);
  2096. event.x.n = client->execution_order;
  2097. event.y.n = upstream_is_jackd;
  2098. jack_deliver_event (engine, client, &event);
  2099. n++;
  2100. }
  2101. }
  2102. }
  2103. if (subgraph_client) {
  2104. subgraph_client->subgraph_wait_fd =
  2105. jack_get_fifo_fd (engine, n);
  2106. VERBOSE (engine, "client %s: wait_fd=%d, "
  2107. "execution_order=%lu (last client).",
  2108. subgraph_client->control->name,
  2109. subgraph_client->subgraph_wait_fd, n);
  2110. }
  2111. jack_remove_clients (engine);
  2112. VERBOSE (engine, "-- jack_rechain_graph()");
  2113. return err;
  2114. }
  2115. static jack_nframes_t
  2116. jack_get_port_total_latency (jack_engine_t *engine,
  2117. jack_port_internal_t *port, int hop_count,
  2118. int toward_port)
  2119. {
  2120. JSList *node;
  2121. jack_nframes_t latency;
  2122. jack_nframes_t max_latency = 0;
  2123. #ifdef DEBUG_TOTAL_LATENCY_COMPUTATION
  2124. char prefix[32];
  2125. int i;
  2126. for (i = 0; i < hop_count; ++i) {
  2127. prefix[i] = '\t';
  2128. }
  2129. prefix[i] = '\0';
  2130. #endif
  2131. /* call tree must hold engine->client_lock. */
  2132. latency = port->shared->latency;
  2133. /* we don't prevent cyclic graphs, so we have to do something
  2134. to bottom out in the event that they are created.
  2135. */
  2136. if (hop_count > 8) {
  2137. return latency;
  2138. }
  2139. #ifdef DEBUG_TOTAL_LATENCY_COMPUTATION
  2140. jack_info ("%sFor port %s (%s)", prefix, port->shared->name, (toward_port ? "toward" : "away"));
  2141. #endif
  2142. for (node = port->connections; node; node = jack_slist_next (node)) {
  2143. jack_nframes_t this_latency;
  2144. jack_connection_internal_t *connection;
  2145. connection = (jack_connection_internal_t *) node->data;
  2146. if ((toward_port &&
  2147. (connection->source->shared == port->shared)) ||
  2148. (!toward_port &&
  2149. (connection->destination->shared == port->shared))) {
  2150. #ifdef DEBUG_TOTAL_LATENCY_COMPUTATION
  2151. jack_info ("%s\tskip connection %s->%s",
  2152. prefix,
  2153. connection->source->shared->name,
  2154. connection->destination->shared->name);
  2155. #endif
  2156. continue;
  2157. }
  2158. #ifdef DEBUG_TOTAL_LATENCY_COMPUTATION
  2159. jack_info ("%s\tconnection %s->%s ... ",
  2160. prefix,
  2161. connection->source->shared->name,
  2162. connection->destination->shared->name);
  2163. #endif
  2164. /* if we're a destination in the connection, recurse
  2165. on the source to get its total latency
  2166. */
  2167. if (connection->destination == port) {
  2168. if (connection->source->shared->flags
  2169. & JackPortIsTerminal) {
  2170. this_latency = connection->source->
  2171. shared->latency;
  2172. } else {
  2173. this_latency =
  2174. jack_get_port_total_latency (
  2175. engine, connection->source,
  2176. hop_count + 1,
  2177. toward_port);
  2178. }
  2179. } else {
  2180. /* "port" is the source, so get the latency of
  2181. * the destination */
  2182. if (connection->destination->shared->flags
  2183. & JackPortIsTerminal) {
  2184. this_latency = connection->destination->
  2185. shared->latency;
  2186. } else {
  2187. this_latency =
  2188. jack_get_port_total_latency (
  2189. engine,
  2190. connection->destination,
  2191. hop_count + 1,
  2192. toward_port);
  2193. }
  2194. }
  2195. if (this_latency > max_latency) {
  2196. max_latency = this_latency;
  2197. }
  2198. }
  2199. #ifdef DEBUG_TOTAL_LATENCY_COMPUTATION
  2200. jack_info ("%s\treturn %lu + %lu = %lu", prefix, latency, max_latency, latency + max_latency);
  2201. #endif
  2202. return latency + max_latency;
  2203. }
  2204. static void
  2205. jack_compute_port_total_latency (jack_engine_t* engine, jack_port_shared_t* port)
  2206. {
  2207. if (port->in_use) {
  2208. port->total_latency =
  2209. jack_get_port_total_latency (
  2210. engine, &engine->internal_ports[port->id],
  2211. 0, !(port->flags & JackPortIsOutput));
  2212. }
  2213. }
  2214. static void
  2215. jack_compute_all_port_total_latencies (jack_engine_t *engine)
  2216. {
  2217. jack_port_shared_t *shared = engine->control->ports;
  2218. unsigned int i;
  2219. int toward_port;
  2220. for (i = 0; i < engine->control->port_max; i++) {
  2221. if (shared[i].in_use) {
  2222. if (shared[i].flags & JackPortIsOutput) {
  2223. toward_port = FALSE;
  2224. } else {
  2225. toward_port = TRUE;
  2226. }
  2227. shared[i].total_latency =
  2228. jack_get_port_total_latency (
  2229. engine, &engine->internal_ports[i],
  2230. 0, toward_port);
  2231. }
  2232. }
  2233. }
  2234. /* How the sort works:
  2235. *
  2236. * Each client has a "sortfeeds" list of clients indicating which clients
  2237. * it should be considered as feeding for the purposes of sorting the
  2238. * graph. This list differs from the clients it /actually/ feeds in the
  2239. * following ways:
  2240. *
  2241. * 1. Connections from a client to itself are disregarded
  2242. *
  2243. * 2. Connections to a driver client are disregarded
  2244. *
  2245. * 3. If a connection from A to B is a feedback connection (ie there was
  2246. * already a path from B to A when the connection was made) then instead
  2247. * of B appearing on A's sortfeeds list, A will appear on B's sortfeeds
  2248. * list.
  2249. *
  2250. * If client A is on client B's sortfeeds list, client A must come after
  2251. * client B in the execution order. The above 3 rules ensure that the
  2252. * sortfeeds relation is always acyclic so that all ordering constraints
  2253. * can actually be met.
  2254. *
  2255. * Each client also has a "truefeeds" list which is the same as sortfeeds
  2256. * except that feedback connections appear normally instead of reversed.
  2257. * This is used to detect whether the graph has become acyclic.
  2258. *
  2259. */
  2260. void
  2261. jack_sort_graph (jack_engine_t *engine)
  2262. {
  2263. /* called, obviously, must hold engine->client_lock */
  2264. engine->clients = jack_slist_sort (engine->clients,
  2265. (JCompareFunc) jack_client_sort);
  2266. jack_compute_all_port_total_latencies (engine);
  2267. jack_rechain_graph (engine);
  2268. }
  2269. static int
  2270. jack_client_sort (jack_client_internal_t *a, jack_client_internal_t *b)
  2271. {
  2272. /* drivers are forced to the front, ie considered as sources
  2273. rather than sinks for purposes of the sort */
  2274. if (jack_client_feeds_transitive (a, b) ||
  2275. (a->control->type == ClientDriver &&
  2276. b->control->type != ClientDriver)) {
  2277. return -1;
  2278. } else if (jack_client_feeds_transitive (b, a) ||
  2279. (b->control->type == ClientDriver &&
  2280. a->control->type != ClientDriver)) {
  2281. return 1;
  2282. } else {
  2283. return 0;
  2284. }
  2285. }
  2286. /* transitive closure of the relation expressed by the sortfeeds lists. */
  2287. static int
  2288. jack_client_feeds_transitive (jack_client_internal_t *source,
  2289. jack_client_internal_t *dest )
  2290. {
  2291. jack_client_internal_t *med;
  2292. JSList *node;
  2293. if (jack_slist_find (source->sortfeeds, dest)) {
  2294. return 1;
  2295. }
  2296. for (node = source->sortfeeds; node; node = jack_slist_next (node)) {
  2297. med = (jack_client_internal_t *) node->data;
  2298. if (jack_client_feeds_transitive (med, dest)) {
  2299. return 1;
  2300. }
  2301. }
  2302. return 0;
  2303. }
  2304. /**
  2305. * Checks whether the graph has become acyclic and if so modifies client
  2306. * sortfeeds lists to turn leftover feedback connections into normal ones.
  2307. * This lowers latency, but at the expense of some data corruption.
  2308. */
  2309. static void
  2310. jack_check_acyclic (jack_engine_t *engine)
  2311. {
  2312. JSList *srcnode, *dstnode, *portnode, *connnode;
  2313. jack_client_internal_t *src, *dst;
  2314. jack_port_internal_t *port;
  2315. jack_connection_internal_t *conn;
  2316. int stuck;
  2317. int unsortedclients = 0;
  2318. VERBOSE (engine, "checking for graph become acyclic");
  2319. for (srcnode = engine->clients; srcnode;
  2320. srcnode = jack_slist_next (srcnode)) {
  2321. src = (jack_client_internal_t *) srcnode->data;
  2322. src->tfedcount = src->fedcount;
  2323. unsortedclients++;
  2324. }
  2325. stuck = FALSE;
  2326. /* find out whether a normal sort would have been possible */
  2327. while (unsortedclients && !stuck) {
  2328. stuck = TRUE;
  2329. for (srcnode = engine->clients; srcnode;
  2330. srcnode = jack_slist_next (srcnode)) {
  2331. src = (jack_client_internal_t *) srcnode->data;
  2332. if (!src->tfedcount) {
  2333. stuck = FALSE;
  2334. unsortedclients--;
  2335. src->tfedcount = -1;
  2336. for (dstnode = src->truefeeds; dstnode;
  2337. dstnode = jack_slist_next (dstnode)) {
  2338. dst = (jack_client_internal_t *)
  2339. dstnode->data;
  2340. dst->tfedcount--;
  2341. }
  2342. }
  2343. }
  2344. }
  2345. if (stuck) {
  2346. VERBOSE (engine, "graph is still cyclic" );
  2347. } else {
  2348. VERBOSE (engine, "graph has become acyclic");
  2349. /* turn feedback connections around in sortfeeds */
  2350. for (srcnode = engine->clients; srcnode;
  2351. srcnode = jack_slist_next (srcnode)) {
  2352. src = (jack_client_internal_t *) srcnode->data;
  2353. for (portnode = src->ports; portnode;
  2354. portnode = jack_slist_next (portnode)) {
  2355. port = (jack_port_internal_t *) portnode->data;
  2356. for (connnode = port->connections; connnode;
  2357. connnode = jack_slist_next (connnode)) {
  2358. conn = (jack_connection_internal_t*)
  2359. connnode->data;
  2360. if (conn->dir == -1 )
  2361. /*&&
  2362. conn->srcclient == src) */{
  2363. VERBOSE (engine,
  2364. "reversing connection from "
  2365. "%s to %s",
  2366. conn->srcclient->control->name,
  2367. conn->dstclient->control->name);
  2368. conn->dir = 1;
  2369. conn->dstclient->sortfeeds =
  2370. jack_slist_remove
  2371. (conn->dstclient->sortfeeds,
  2372. conn->srcclient);
  2373. conn->srcclient->sortfeeds =
  2374. jack_slist_prepend
  2375. (conn->srcclient->sortfeeds,
  2376. conn->dstclient );
  2377. }
  2378. }
  2379. }
  2380. }
  2381. engine->feedbackcount = 0;
  2382. }
  2383. }
  2384. /**
  2385. * Dumps current engine configuration.
  2386. */
  2387. void jack_dump_configuration(jack_engine_t *engine, int take_lock)
  2388. {
  2389. JSList *clientnode, *portnode, *connectionnode;
  2390. jack_client_internal_t *client;
  2391. jack_client_control_t *ctl;
  2392. jack_port_internal_t *port;
  2393. jack_connection_internal_t* connection;
  2394. int n, m, o;
  2395. jack_info ("engine.c: <-- dump begins -->");
  2396. if (take_lock) {
  2397. jack_lock_graph (engine);
  2398. }
  2399. for (n = 0, clientnode = engine->clients; clientnode;
  2400. clientnode = jack_slist_next (clientnode)) {
  2401. client = (jack_client_internal_t *) clientnode->data;
  2402. ctl = client->control;
  2403. jack_info ("client #%d: %s (type: %d, process? %s,"
  2404. " start=%d wait=%d",
  2405. ++n,
  2406. ctl->name,
  2407. ctl->type,
  2408. ctl->process ? "yes" : "no",
  2409. client->subgraph_start_fd,
  2410. client->subgraph_wait_fd);
  2411. for(m = 0, portnode = client->ports; portnode;
  2412. portnode = jack_slist_next (portnode)) {
  2413. port = (jack_port_internal_t *) portnode->data;
  2414. jack_info("\t port #%d: %s", ++m,
  2415. port->shared->name);
  2416. for(o = 0, connectionnode = port->connections;
  2417. connectionnode;
  2418. connectionnode =
  2419. jack_slist_next (connectionnode)) {
  2420. connection = (jack_connection_internal_t *)
  2421. connectionnode->data;
  2422. jack_info("\t\t connection #%d: %s %s",
  2423. ++o,
  2424. (port->shared->flags
  2425. & JackPortIsInput)? "<-": "->",
  2426. (port->shared->flags & JackPortIsInput)?
  2427. connection->source->shared->name:
  2428. connection->destination->shared->name);
  2429. }
  2430. }
  2431. }
  2432. if (take_lock) {
  2433. jack_unlock_graph (engine);
  2434. }
  2435. jack_info("engine.c: <-- dump ends -->");
  2436. }
  2437. static int
  2438. jack_port_do_connect (jack_engine_t *engine,
  2439. const char *source_port,
  2440. const char *destination_port)
  2441. {
  2442. jack_connection_internal_t *connection;
  2443. jack_port_internal_t *srcport, *dstport;
  2444. jack_port_id_t src_id, dst_id;
  2445. jack_client_internal_t *srcclient, *dstclient;
  2446. JSList *it;
  2447. if ((srcport = jack_get_port_by_name (engine, source_port)) == NULL) {
  2448. jack_error ("unknown source port in attempted connection [%s]",
  2449. source_port);
  2450. return -1;
  2451. }
  2452. if ((dstport = jack_get_port_by_name (engine, destination_port))
  2453. == NULL) {
  2454. jack_error ("unknown destination port in attempted connection"
  2455. " [%s]", destination_port);
  2456. return -1;
  2457. }
  2458. if ((dstport->shared->flags & JackPortIsInput) == 0) {
  2459. jack_error ("destination port in attempted connection of"
  2460. " %s and %s is not an input port",
  2461. source_port, destination_port);
  2462. return -1;
  2463. }
  2464. if ((srcport->shared->flags & JackPortIsOutput) == 0) {
  2465. jack_error ("source port in attempted connection of %s and"
  2466. " %s is not an output port",
  2467. source_port, destination_port);
  2468. return -1;
  2469. }
  2470. if (srcport->shared->ptype_id != dstport->shared->ptype_id) {
  2471. jack_error ("ports used in attemped connection are not of "
  2472. "the same data type");
  2473. return -1;
  2474. }
  2475. if ((srcclient = jack_client_internal_by_id (engine,
  2476. srcport->shared->client_id))
  2477. == 0) {
  2478. jack_error ("unknown client set as owner of port - "
  2479. "cannot connect");
  2480. return -1;
  2481. }
  2482. if (!srcclient->control->active) {
  2483. jack_error ("cannot connect ports owned by inactive clients;"
  2484. " \"%s\" is not active", srcclient->control->name);
  2485. return -1;
  2486. }
  2487. if ((dstclient = jack_client_internal_by_id (engine,
  2488. dstport->shared->client_id))
  2489. == 0) {
  2490. jack_error ("unknown client set as owner of port - cannot "
  2491. "connect");
  2492. return -1;
  2493. }
  2494. if (!dstclient->control->active) {
  2495. jack_error ("cannot connect ports owned by inactive clients;"
  2496. " \"%s\" is not active", dstclient->control->name);
  2497. return -1;
  2498. }
  2499. for (it = srcport->connections; it; it = it->next) {
  2500. if (((jack_connection_internal_t *)it->data)->destination
  2501. == dstport) {
  2502. return EEXIST;
  2503. }
  2504. }
  2505. connection = (jack_connection_internal_t *)
  2506. malloc (sizeof (jack_connection_internal_t));
  2507. connection->source = srcport;
  2508. connection->destination = dstport;
  2509. connection->srcclient = srcclient;
  2510. connection->dstclient = dstclient;
  2511. src_id = srcport->shared->id;
  2512. dst_id = dstport->shared->id;
  2513. jack_lock_graph (engine);
  2514. if (dstport->connections && !dstport->shared->has_mixdown) {
  2515. jack_port_type_info_t *port_type =
  2516. jack_port_type_info (engine, dstport);
  2517. jack_error ("cannot make multiple connections to a port of"
  2518. " type [%s]", port_type->type_name);
  2519. free (connection);
  2520. jack_unlock_graph (engine);
  2521. return -1;
  2522. } else {
  2523. if (dstclient->control->type == ClientDriver)
  2524. {
  2525. /* Ignore output connections to drivers for purposes
  2526. of sorting. Drivers are executed first in the sort
  2527. order anyway, and we don't want to treat graphs
  2528. such as driver -> client -> driver as containing
  2529. feedback */
  2530. VERBOSE (engine,
  2531. "connect %s and %s (output)",
  2532. srcport->shared->name,
  2533. dstport->shared->name);
  2534. connection->dir = 1;
  2535. }
  2536. else if (srcclient != dstclient) {
  2537. srcclient->truefeeds = jack_slist_prepend
  2538. (srcclient->truefeeds, dstclient);
  2539. dstclient->fedcount++;
  2540. if (jack_client_feeds_transitive (dstclient,
  2541. srcclient ) ||
  2542. (dstclient->control->type == ClientDriver &&
  2543. srcclient->control->type != ClientDriver)) {
  2544. /* dest is running before source so
  2545. this is a feedback connection */
  2546. VERBOSE (engine,
  2547. "connect %s and %s (feedback)",
  2548. srcport->shared->name,
  2549. dstport->shared->name);
  2550. dstclient->sortfeeds = jack_slist_prepend
  2551. (dstclient->sortfeeds, srcclient);
  2552. connection->dir = -1;
  2553. engine->feedbackcount++;
  2554. VERBOSE (engine,
  2555. "feedback count up to %d",
  2556. engine->feedbackcount);
  2557. } else {
  2558. /* this is not a feedback connection */
  2559. VERBOSE (engine,
  2560. "connect %s and %s (forward)",
  2561. srcport->shared->name,
  2562. dstport->shared->name);
  2563. srcclient->sortfeeds = jack_slist_prepend
  2564. (srcclient->sortfeeds, dstclient);
  2565. connection->dir = 1;
  2566. }
  2567. }
  2568. else
  2569. {
  2570. /* this is a connection to self */
  2571. VERBOSE (engine,
  2572. "connect %s and %s (self)",
  2573. srcport->shared->name,
  2574. dstport->shared->name);
  2575. connection->dir = 0;
  2576. }
  2577. dstport->connections =
  2578. jack_slist_prepend (dstport->connections, connection);
  2579. srcport->connections =
  2580. jack_slist_prepend (srcport->connections, connection);
  2581. DEBUG ("actually sorted the graph...");
  2582. jack_send_connection_notification (engine,
  2583. srcport->shared->client_id,
  2584. src_id, dst_id, TRUE);
  2585. jack_send_connection_notification (engine,
  2586. dstport->shared->client_id,
  2587. dst_id, src_id, TRUE);
  2588. /* send a port connection notification just once to everyone who cares excluding clients involved in the connection */
  2589. jack_notify_all_port_interested_clients (engine, srcport->shared->client_id, dstport->shared->client_id, src_id, dst_id, 1);
  2590. jack_sort_graph (engine);
  2591. }
  2592. jack_unlock_graph (engine);
  2593. jack_remove_clients (engine);
  2594. return 0;
  2595. }
  2596. int
  2597. jack_port_disconnect_internal (jack_engine_t *engine,
  2598. jack_port_internal_t *srcport,
  2599. jack_port_internal_t *dstport )
  2600. {
  2601. JSList *node;
  2602. jack_connection_internal_t *connect;
  2603. int ret = -1;
  2604. jack_port_id_t src_id, dst_id;
  2605. int check_acyclic = engine->feedbackcount;
  2606. /* call tree **** MUST HOLD **** engine->client_lock. */
  2607. for (node = srcport->connections; node;
  2608. node = jack_slist_next (node)) {
  2609. connect = (jack_connection_internal_t *) node->data;
  2610. if (connect->source == srcport &&
  2611. connect->destination == dstport) {
  2612. VERBOSE (engine, "DIS-connect %s and %s",
  2613. srcport->shared->name,
  2614. dstport->shared->name);
  2615. srcport->connections =
  2616. jack_slist_remove (srcport->connections,
  2617. connect);
  2618. dstport->connections =
  2619. jack_slist_remove (dstport->connections,
  2620. connect);
  2621. src_id = srcport->shared->id;
  2622. dst_id = dstport->shared->id;
  2623. /* this is a bit harsh, but it basically says
  2624. that if we actually do a disconnect, and
  2625. its the last one, then make sure that any
  2626. input monitoring is turned off on the
  2627. srcport. this isn't ideal for all
  2628. situations, but it works better for most of
  2629. them.
  2630. */
  2631. if (srcport->connections == NULL) {
  2632. srcport->shared->monitor_requests = 0;
  2633. }
  2634. jack_send_connection_notification (
  2635. engine, srcport->shared->client_id, src_id,
  2636. dst_id, FALSE);
  2637. jack_send_connection_notification (
  2638. engine, dstport->shared->client_id, dst_id,
  2639. src_id, FALSE);
  2640. /* send a port connection notification just once to everyone who cares excluding clients involved in the connection */
  2641. jack_notify_all_port_interested_clients (engine, srcport->shared->client_id, dstport->shared->client_id, src_id, dst_id, 0);
  2642. if (connect->dir) {
  2643. jack_client_internal_t *src;
  2644. jack_client_internal_t *dst;
  2645. src = jack_client_internal_by_id
  2646. (engine, srcport->shared->client_id);
  2647. dst = jack_client_internal_by_id
  2648. (engine, dstport->shared->client_id);
  2649. src->truefeeds = jack_slist_remove
  2650. (src->truefeeds, dst);
  2651. dst->fedcount--;
  2652. if (connect->dir == 1) {
  2653. /* normal connection: remove dest from
  2654. source's sortfeeds list */
  2655. src->sortfeeds = jack_slist_remove
  2656. (src->sortfeeds, dst);
  2657. } else {
  2658. /* feedback connection: remove source
  2659. from dest's sortfeeds list */
  2660. dst->sortfeeds = jack_slist_remove
  2661. (dst->sortfeeds, src);
  2662. engine->feedbackcount--;
  2663. VERBOSE (engine,
  2664. "feedback count down to %d",
  2665. engine->feedbackcount);
  2666. }
  2667. } /* else self-connection: do nothing */
  2668. free (connect);
  2669. ret = 0;
  2670. break;
  2671. }
  2672. }
  2673. jack_remove_clients (engine);
  2674. if (check_acyclic) {
  2675. jack_check_acyclic (engine);
  2676. }
  2677. jack_sort_graph (engine);
  2678. return ret;
  2679. }
  2680. static int
  2681. jack_port_do_disconnect_all (jack_engine_t *engine,
  2682. jack_port_id_t port_id)
  2683. {
  2684. if (port_id >= engine->control->port_max) {
  2685. jack_error ("illegal port ID in attempted disconnection [%"
  2686. PRIu32 "]", port_id);
  2687. return -1;
  2688. }
  2689. VERBOSE (engine, "clear connections for %s",
  2690. engine->internal_ports[port_id].shared->name);
  2691. jack_lock_graph (engine);
  2692. jack_port_clear_connections (engine, &engine->internal_ports[port_id]);
  2693. jack_sort_graph (engine);
  2694. jack_unlock_graph (engine);
  2695. return 0;
  2696. }
  2697. static int
  2698. jack_port_do_disconnect (jack_engine_t *engine,
  2699. const char *source_port,
  2700. const char *destination_port)
  2701. {
  2702. jack_port_internal_t *srcport, *dstport;
  2703. int ret = -1;
  2704. if ((srcport = jack_get_port_by_name (engine, source_port)) == NULL) {
  2705. jack_error ("unknown source port in attempted disconnection"
  2706. " [%s]", source_port);
  2707. return -1;
  2708. }
  2709. if ((dstport = jack_get_port_by_name (engine, destination_port))
  2710. == NULL) {
  2711. jack_error ("unknown destination port in attempted"
  2712. " disconnection [%s]", destination_port);
  2713. return -1;
  2714. }
  2715. jack_lock_graph (engine);
  2716. ret = jack_port_disconnect_internal (engine, srcport, dstport);
  2717. jack_unlock_graph (engine);
  2718. return ret;
  2719. }
  2720. int
  2721. jack_get_fifo_fd (jack_engine_t *engine, unsigned int which_fifo)
  2722. {
  2723. /* caller must hold client_lock */
  2724. char path[PATH_MAX+1];
  2725. struct stat statbuf;
  2726. snprintf (path, sizeof (path), "%s-%d", engine->fifo_prefix,
  2727. which_fifo);
  2728. DEBUG ("%s", path);
  2729. if (stat (path, &statbuf)) {
  2730. if (errno == ENOENT) {
  2731. if (mkfifo(path, 0666) < 0){
  2732. jack_error ("cannot create inter-client FIFO"
  2733. " [%s] (%s)\n", path,
  2734. strerror (errno));
  2735. return -1;
  2736. }
  2737. } else {
  2738. jack_error ("cannot check on FIFO %d\n", which_fifo);
  2739. return -1;
  2740. }
  2741. } else {
  2742. if (!S_ISFIFO(statbuf.st_mode)) {
  2743. jack_error ("FIFO %d (%s) already exists, but is not"
  2744. " a FIFO!\n", which_fifo, path);
  2745. return -1;
  2746. }
  2747. }
  2748. if (which_fifo >= engine->fifo_size) {
  2749. unsigned int i;
  2750. engine->fifo = (int *)
  2751. realloc (engine->fifo,
  2752. sizeof (int) * (engine->fifo_size + 16));
  2753. for (i = engine->fifo_size; i < engine->fifo_size + 16; i++) {
  2754. engine->fifo[i] = -1;
  2755. }
  2756. engine->fifo_size += 16;
  2757. }
  2758. if (engine->fifo[which_fifo] < 0) {
  2759. if ((engine->fifo[which_fifo] =
  2760. open (path, O_RDWR|O_CREAT|O_NONBLOCK, 0666)) < 0) {
  2761. jack_error ("cannot open fifo [%s] (%s)", path,
  2762. strerror (errno));
  2763. return -1;
  2764. }
  2765. DEBUG ("opened engine->fifo[%d] == %d (%s)",
  2766. which_fifo, engine->fifo[which_fifo], path);
  2767. }
  2768. return engine->fifo[which_fifo];
  2769. }
  2770. static void
  2771. jack_clear_fifos (jack_engine_t *engine)
  2772. {
  2773. /* caller must hold client_lock */
  2774. unsigned int i;
  2775. char buf[16];
  2776. /* this just drains the existing FIFO's of any data left in
  2777. them by aborted clients, etc. there is only ever going to
  2778. be 0, 1 or 2 bytes in them, but we'll allow for up to 16.
  2779. */
  2780. for (i = 0; i < engine->fifo_size; i++) {
  2781. if (engine->fifo[i] >= 0) {
  2782. int nread = read (engine->fifo[i], buf, sizeof (buf));
  2783. if (nread < 0 && errno != EAGAIN) {
  2784. jack_error ("clear fifo[%d] error: %s",
  2785. i, strerror (errno));
  2786. }
  2787. }
  2788. }
  2789. }
  2790. static int
  2791. jack_use_driver (jack_engine_t *engine, jack_driver_t *driver)
  2792. {
  2793. if (engine->driver) {
  2794. engine->driver->detach (engine->driver, engine);
  2795. engine->driver = 0;
  2796. }
  2797. engine->driver = driver;
  2798. if (driver) {
  2799. if (driver->attach (driver, engine))
  2800. return -1;
  2801. engine->rolling_interval =
  2802. jack_rolling_interval (driver->period_usecs);
  2803. }
  2804. return 0;
  2805. }
  2806. /* PORT RELATED FUNCTIONS */
  2807. static jack_port_id_t
  2808. jack_get_free_port (jack_engine_t *engine)
  2809. {
  2810. jack_port_id_t i;
  2811. pthread_mutex_lock (&engine->port_lock);
  2812. for (i = 0; i < engine->port_max; i++) {
  2813. if (engine->control->ports[i].in_use == 0) {
  2814. engine->control->ports[i].in_use = 1;
  2815. break;
  2816. }
  2817. }
  2818. pthread_mutex_unlock (&engine->port_lock);
  2819. if (i == engine->port_max) {
  2820. return (jack_port_id_t) -1;
  2821. }
  2822. return i;
  2823. }
  2824. void
  2825. jack_port_release (jack_engine_t *engine, jack_port_internal_t *port)
  2826. {
  2827. pthread_mutex_lock (&engine->port_lock);
  2828. port->shared->in_use = 0;
  2829. port->shared->alias1[0] = '\0';
  2830. port->shared->alias2[0] = '\0';
  2831. if (port->buffer_info) {
  2832. jack_port_buffer_list_t *blist =
  2833. jack_port_buffer_list (engine, port);
  2834. pthread_mutex_lock (&blist->lock);
  2835. blist->freelist =
  2836. jack_slist_prepend (blist->freelist,
  2837. port->buffer_info);
  2838. port->buffer_info = NULL;
  2839. pthread_mutex_unlock (&blist->lock);
  2840. }
  2841. pthread_mutex_unlock (&engine->port_lock);
  2842. }
  2843. jack_port_internal_t *
  2844. jack_get_port_internal_by_name (jack_engine_t *engine, const char *name)
  2845. {
  2846. jack_port_id_t id;
  2847. pthread_mutex_lock (&engine->port_lock);
  2848. for (id = 0; id < engine->port_max; id++) {
  2849. if (jack_port_name_equals (&engine->control->ports[id], name)) {
  2850. break;
  2851. }
  2852. }
  2853. pthread_mutex_unlock (&engine->port_lock);
  2854. if (id != engine->port_max) {
  2855. return &engine->internal_ports[id];
  2856. } else {
  2857. return NULL;
  2858. }
  2859. }
  2860. int
  2861. jack_port_do_register (jack_engine_t *engine, jack_request_t *req, int internal)
  2862. {
  2863. jack_port_id_t port_id;
  2864. jack_port_shared_t *shared;
  2865. jack_port_internal_t *port;
  2866. jack_client_internal_t *client;
  2867. unsigned long i;
  2868. char *backend_client_name;
  2869. size_t len;
  2870. for (i = 0; i < engine->control->n_port_types; ++i) {
  2871. if (strcmp (req->x.port_info.type,
  2872. engine->control->port_types[i].type_name) == 0) {
  2873. break;
  2874. }
  2875. }
  2876. if (i == engine->control->n_port_types) {
  2877. jack_error ("cannot register a port of type \"%s\"",
  2878. req->x.port_info.type);
  2879. return -1;
  2880. }
  2881. jack_lock_graph (engine);
  2882. if ((client = jack_client_internal_by_id (engine,
  2883. req->x.port_info.client_id))
  2884. == NULL) {
  2885. jack_error ("unknown client id in port registration request");
  2886. jack_unlock_graph (engine);
  2887. return -1;
  2888. }
  2889. if ((port = jack_get_port_by_name(engine, req->x.port_info.name)) != NULL) {
  2890. jack_error ("duplicate port name in port registration request");
  2891. jack_unlock_graph (engine);
  2892. return -1;
  2893. }
  2894. if ((port_id = jack_get_free_port (engine)) == (jack_port_id_t) -1) {
  2895. jack_error ("no ports available!");
  2896. jack_unlock_graph (engine);
  2897. return -1;
  2898. }
  2899. shared = &engine->control->ports[port_id];
  2900. if (!internal || !engine->driver)
  2901. goto fallback;
  2902. backend_client_name = (char *) engine->driver->internal_client->control->name;
  2903. len = strlen (backend_client_name);
  2904. if (strncmp (req->x.port_info.name, backend_client_name, len) != 0)
  2905. goto fallback;
  2906. /* use backend's original as an alias, use predefined names */
  2907. if (strcmp(req->x.port_info.type, JACK_DEFAULT_AUDIO_TYPE) == 0) {
  2908. if ((req->x.port_info.flags & (JackPortIsPhysical|JackPortIsInput)) == (JackPortIsPhysical|JackPortIsInput)) {
  2909. snprintf (shared->name, sizeof (shared->name), JACK_BACKEND_ALIAS ":playback_%d", ++engine->audio_out_cnt);
  2910. strcpy (shared->alias1, req->x.port_info.name);
  2911. goto next;
  2912. }
  2913. else if ((req->x.port_info.flags & (JackPortIsPhysical|JackPortIsOutput)) == (JackPortIsPhysical|JackPortIsOutput)) {
  2914. snprintf (shared->name, sizeof (shared->name), JACK_BACKEND_ALIAS ":capture_%d", ++engine->audio_in_cnt);
  2915. strcpy (shared->alias1, req->x.port_info.name);
  2916. goto next;
  2917. }
  2918. }
  2919. else if (strcmp(req->x.port_info.type, JACK_DEFAULT_MIDI_TYPE) == 0) {
  2920. if ((req->x.port_info.flags & (JackPortIsPhysical|JackPortIsInput)) == (JackPortIsPhysical|JackPortIsInput)) {
  2921. snprintf (shared->name, sizeof (shared->name), JACK_BACKEND_ALIAS ":midi_playback_%d", ++engine->midi_out_cnt);
  2922. strcpy (shared->alias1, req->x.port_info.name);
  2923. goto next;
  2924. }
  2925. else if ((req->x.port_info.flags & (JackPortIsPhysical|JackPortIsOutput)) == (JackPortIsPhysical|JackPortIsOutput)) {
  2926. snprintf (shared->name, sizeof (shared->name), JACK_BACKEND_ALIAS ":midi_capture_%d", ++engine->midi_in_cnt);
  2927. strcpy (shared->alias1, req->x.port_info.name);
  2928. goto next;
  2929. }
  2930. }
  2931. fallback:
  2932. strcpy (shared->name, req->x.port_info.name);
  2933. next:
  2934. shared->ptype_id = engine->control->port_types[i].ptype_id;
  2935. shared->client_id = req->x.port_info.client_id;
  2936. shared->flags = req->x.port_info.flags;
  2937. shared->latency = 0;
  2938. shared->monitor_requests = 0;
  2939. port = &engine->internal_ports[port_id];
  2940. port->shared = shared;
  2941. port->connections = 0;
  2942. port->buffer_info = NULL;
  2943. if (jack_port_assign_buffer (engine, port)) {
  2944. jack_error ("cannot assign buffer for port");
  2945. jack_port_release (engine, &engine->internal_ports[port_id]);
  2946. jack_unlock_graph (engine);
  2947. return -1;
  2948. }
  2949. client->ports = jack_slist_prepend (client->ports, port);
  2950. jack_port_registration_notify (engine, port_id, TRUE);
  2951. jack_unlock_graph (engine);
  2952. VERBOSE (engine, "registered port %s, offset = %u",
  2953. shared->name, (unsigned int)shared->offset);
  2954. req->x.port_info.port_id = port_id;
  2955. return 0;
  2956. }
  2957. int
  2958. jack_port_do_unregister (jack_engine_t *engine, jack_request_t *req)
  2959. {
  2960. jack_client_internal_t *client;
  2961. jack_port_shared_t *shared;
  2962. jack_port_internal_t *port;
  2963. if (req->x.port_info.port_id < 0 ||
  2964. req->x.port_info.port_id > engine->port_max) {
  2965. jack_error ("invalid port ID %" PRIu32
  2966. " in unregister request",
  2967. req->x.port_info.port_id);
  2968. return -1;
  2969. }
  2970. shared = &engine->control->ports[req->x.port_info.port_id];
  2971. if (shared->client_id != req->x.port_info.client_id) {
  2972. jack_error ("Client %" PRIu32
  2973. " is not allowed to remove port %s",
  2974. req->x.port_info.client_id, shared->name);
  2975. return -1;
  2976. }
  2977. jack_lock_graph (engine);
  2978. if ((client = jack_client_internal_by_id (engine, shared->client_id))
  2979. == NULL) {
  2980. jack_error ("unknown client id in port registration request");
  2981. jack_unlock_graph (engine);
  2982. return -1;
  2983. }
  2984. port = &engine->internal_ports[req->x.port_info.port_id];
  2985. jack_port_clear_connections (engine, port);
  2986. jack_port_release (engine,
  2987. &engine->internal_ports[req->x.port_info.port_id]);
  2988. client->ports = jack_slist_remove (client->ports, port);
  2989. jack_port_registration_notify (engine, req->x.port_info.port_id,
  2990. FALSE);
  2991. jack_unlock_graph (engine);
  2992. return 0;
  2993. }
  2994. int
  2995. jack_do_get_port_connections (jack_engine_t *engine, jack_request_t *req,
  2996. int reply_fd)
  2997. {
  2998. jack_port_internal_t *port;
  2999. JSList *node;
  3000. unsigned int i;
  3001. int ret = -1;
  3002. int internal = FALSE;
  3003. jack_lock_graph (engine);
  3004. port = &engine->internal_ports[req->x.port_info.port_id];
  3005. DEBUG ("Getting connections for port '%s'.", port->shared->name);
  3006. req->x.port_connections.nports = jack_slist_length (port->connections);
  3007. req->status = 0;
  3008. /* figure out if this is an internal or external client */
  3009. for (node = engine->clients; node; node = jack_slist_next (node)) {
  3010. if (((jack_client_internal_t *) node->data)->request_fd
  3011. == reply_fd) {
  3012. internal = jack_client_is_internal(
  3013. (jack_client_internal_t *) node->data);
  3014. break;
  3015. }
  3016. }
  3017. if (!internal) {
  3018. if (write (reply_fd, req, sizeof (*req))
  3019. < (ssize_t) sizeof (req)) {
  3020. jack_error ("cannot write GetPortConnections result "
  3021. "to client via fd = %d (%s)",
  3022. reply_fd, strerror (errno));
  3023. goto out;
  3024. }
  3025. } else {
  3026. req->x.port_connections.ports = (const char **)
  3027. malloc (sizeof (char *)
  3028. * req->x.port_connections.nports);
  3029. }
  3030. if (req->type == GetPortConnections) {
  3031. for (i = 0, node = port->connections; node;
  3032. node = jack_slist_next (node), ++i) {
  3033. jack_port_id_t port_id;
  3034. if (((jack_connection_internal_t *) node->data)->source
  3035. == port) {
  3036. port_id = ((jack_connection_internal_t *)
  3037. node->data)->destination->shared->id;
  3038. } else {
  3039. port_id = ((jack_connection_internal_t *)
  3040. node->data)->source->shared->id;
  3041. }
  3042. if (internal) {
  3043. /* internal client asking for
  3044. * names. store in malloc'ed space,
  3045. * client frees
  3046. */
  3047. req->x.port_connections.ports[i] =
  3048. engine->control->ports[port_id].name;
  3049. } else {
  3050. /* external client asking for
  3051. * names. we write the port id's to
  3052. * the reply fd.
  3053. */
  3054. if (write (reply_fd, &port_id,
  3055. sizeof (port_id))
  3056. < (ssize_t) sizeof (port_id)) {
  3057. jack_error ("cannot write port id "
  3058. "to client");
  3059. goto out;
  3060. }
  3061. }
  3062. }
  3063. }
  3064. ret = 0;
  3065. out:
  3066. req->status = ret;
  3067. jack_unlock_graph (engine);
  3068. return ret;
  3069. }
  3070. void
  3071. jack_port_registration_notify (jack_engine_t *engine,
  3072. jack_port_id_t port_id, int yn)
  3073. {
  3074. jack_event_t event;
  3075. jack_client_internal_t *client;
  3076. JSList *node;
  3077. event.type = (yn ? PortRegistered : PortUnregistered);
  3078. event.x.port_id = port_id;
  3079. for (node = engine->clients; node; node = jack_slist_next (node)) {
  3080. client = (jack_client_internal_t *) node->data;
  3081. if (!client->control->active) {
  3082. continue;
  3083. }
  3084. if (client->control->port_register) {
  3085. if (jack_deliver_event (engine, client, &event)) {
  3086. jack_error ("cannot send port registration"
  3087. " notification to %s (%s)",
  3088. client->control->name,
  3089. strerror (errno));
  3090. }
  3091. }
  3092. }
  3093. jack_remove_clients (engine);
  3094. }
  3095. void
  3096. jack_client_registration_notify (jack_engine_t *engine,
  3097. const char* name, int yn)
  3098. {
  3099. jack_event_t event;
  3100. jack_client_internal_t *client;
  3101. JSList *node;
  3102. event.type = (yn ? ClientRegistered : ClientUnregistered);
  3103. snprintf (event.x.name, sizeof (event.x.name), "%s", name);
  3104. for (node = engine->clients; node; node = jack_slist_next (node)) {
  3105. client = (jack_client_internal_t *) node->data;
  3106. if (!client->control->active) {
  3107. continue;
  3108. }
  3109. if (strcmp ((char*) client->control->name, (char*) name) == 0) {
  3110. /* do not notify client of its own registration */
  3111. continue;
  3112. }
  3113. if (client->control->client_register) {
  3114. if (jack_deliver_event (engine, client, &event)) {
  3115. jack_error ("cannot send client registration"
  3116. " notification to %s (%s)",
  3117. client->control->name,
  3118. strerror (errno));
  3119. }
  3120. }
  3121. }
  3122. jack_remove_clients (engine);
  3123. }
  3124. int
  3125. jack_port_assign_buffer (jack_engine_t *engine, jack_port_internal_t *port)
  3126. {
  3127. jack_port_buffer_list_t *blist =
  3128. jack_port_buffer_list (engine, port);
  3129. jack_port_buffer_info_t *bi;
  3130. if (port->shared->flags & JackPortIsInput) {
  3131. port->shared->offset = 0;
  3132. return 0;
  3133. }
  3134. pthread_mutex_lock (&blist->lock);
  3135. if (blist->freelist == NULL) {
  3136. jack_port_type_info_t *port_type =
  3137. jack_port_type_info (engine, port);
  3138. jack_error ("all %s port buffers in use!",
  3139. port_type->type_name);
  3140. pthread_mutex_unlock (&blist->lock);
  3141. return -1;
  3142. }
  3143. bi = (jack_port_buffer_info_t *) blist->freelist->data;
  3144. blist->freelist = jack_slist_remove (blist->freelist, bi);
  3145. port->shared->offset = bi->offset;
  3146. port->buffer_info = bi;
  3147. pthread_mutex_unlock (&blist->lock);
  3148. return 0;
  3149. }
  3150. static jack_port_internal_t *
  3151. jack_get_port_by_name (jack_engine_t *engine, const char *name)
  3152. {
  3153. jack_port_id_t id;
  3154. /* Note the potential race on "in_use". Other design
  3155. elements prevent this from being a problem.
  3156. */
  3157. for (id = 0; id < engine->port_max; id++) {
  3158. if (engine->control->ports[id].in_use &&
  3159. jack_port_name_equals (&engine->control->ports[id], name)) {
  3160. return &engine->internal_ports[id];
  3161. }
  3162. }
  3163. return NULL;
  3164. }
  3165. static int
  3166. jack_send_connection_notification (jack_engine_t *engine,
  3167. jack_client_id_t client_id,
  3168. jack_port_id_t self_id,
  3169. jack_port_id_t other_id, int connected)
  3170. {
  3171. jack_client_internal_t *client;
  3172. jack_event_t event;
  3173. if ((client = jack_client_internal_by_id (engine, client_id)) == NULL) {
  3174. jack_error ("no such client %" PRIu32
  3175. " during connection notification", client_id);
  3176. return -1;
  3177. }
  3178. if (client->control->active) {
  3179. event.type = (connected ? PortConnected : PortDisconnected);
  3180. event.x.self_id = self_id;
  3181. event.y.other_id = other_id;
  3182. if (jack_deliver_event (engine, client, &event)) {
  3183. jack_error ("cannot send port connection notification"
  3184. " to client %s (%s)",
  3185. client->control->name, strerror (errno));
  3186. return -1;
  3187. }
  3188. }
  3189. return 0;
  3190. }