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.

4873 lines
129KB

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