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.

4878 lines
124KB

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