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.

4834 lines
128KB

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