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.

4829 lines
127KB

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