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.

4716 lines
119KB

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