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.

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