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.

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