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.

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