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.

4698 lines
119KB

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