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.

4697 lines
122KB

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