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.

4273 lines
102KB

  1. /* -*- mode: c; c-file-style: "bsd"; -*- */
  2. /*
  3. Copyright (C) 2001-2003 Paul Davis
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. $Id$
  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/internal.h>
  36. #include <jack/engine.h>
  37. #include <jack/driver.h>
  38. #include <jack/version.h>
  39. #include <jack/shm.h>
  40. #include <jack/thread.h>
  41. #include <sysdeps/poll.h>
  42. #include <sysdeps/ipc.h>
  43. #ifdef USE_MLOCK
  44. #include <sys/mman.h>
  45. #endif /* USE_MLOCK */
  46. #ifdef USE_CAPABILITIES
  47. /* capgetp and capsetp are linux only extensions, not posix */
  48. #undef _POSIX_SOURCE
  49. #include <sys/capability.h>
  50. #endif
  51. #include "transengine.h"
  52. #define JACK_ERROR_WITH_SOCKETS 10000000
  53. typedef struct {
  54. jack_port_internal_t *source;
  55. jack_port_internal_t *destination;
  56. } jack_connection_internal_t;
  57. typedef struct _jack_driver_info {
  58. jack_driver_t *(*initialize)(jack_client_t*, const JSList *);
  59. void (*finish);
  60. char (*client_name);
  61. dlhandle handle;
  62. } jack_driver_info_t;
  63. static int jack_port_assign_buffer (jack_engine_t *,
  64. jack_port_internal_t *);
  65. static jack_port_internal_t *jack_get_port_by_name (jack_engine_t *,
  66. const char *name);
  67. static void jack_zombify_client (jack_engine_t *engine,
  68. jack_client_internal_t *client);
  69. static void jack_remove_client (jack_engine_t *engine,
  70. jack_client_internal_t *client);
  71. static void jack_client_delete (jack_engine_t *, jack_client_internal_t *);
  72. static jack_client_internal_t
  73. *jack_setup_client_control (jack_engine_t *engine, int fd,
  74. jack_client_connect_request_t *);
  75. static void jack_sort_graph (jack_engine_t *engine);
  76. static int jack_rechain_graph (jack_engine_t *engine);
  77. static int jack_get_fifo_fd (jack_engine_t *engine, unsigned int which_fifo);
  78. static void jack_clear_fifos (jack_engine_t *engine);
  79. static int jack_port_do_connect (jack_engine_t *engine,
  80. const char *source_port,
  81. const char *destination_port);
  82. static int jack_port_do_disconnect (jack_engine_t *engine,
  83. const char *source_port,
  84. const char *destination_port);
  85. static int jack_port_do_disconnect_all (jack_engine_t *engine,
  86. jack_port_id_t);
  87. static int jack_port_do_unregister (jack_engine_t *engine, jack_request_t *);
  88. static int jack_port_do_register (jack_engine_t *engine, jack_request_t *);
  89. static int jack_do_get_port_connections (jack_engine_t *engine,
  90. jack_request_t *req, int reply_fd);
  91. static void jack_port_release (jack_engine_t *engine, jack_port_internal_t *);
  92. static void jack_port_clear_connections (jack_engine_t *engine,
  93. jack_port_internal_t *port);
  94. static int jack_port_disconnect_internal (jack_engine_t *engine,
  95. jack_port_internal_t *src,
  96. jack_port_internal_t *dst,
  97. int sort_graph);
  98. static void jack_port_registration_notify (jack_engine_t *,
  99. jack_port_id_t, int);
  100. static int jack_send_connection_notification (jack_engine_t *,
  101. jack_client_id_t,
  102. jack_port_id_t,
  103. jack_port_id_t, int);
  104. static int jack_deliver_event (jack_engine_t *, jack_client_internal_t *,
  105. jack_event_t *);
  106. static void jack_deliver_event_to_all (jack_engine_t *engine,
  107. jack_event_t *event);
  108. static void jack_engine_post_process (jack_engine_t *);
  109. static int internal_client_request (void*, jack_request_t *);
  110. static int jack_use_driver (jack_engine_t *engine, jack_driver_t *driver);
  111. static int jack_run_cycle (jack_engine_t *engine, jack_nframes_t nframes,
  112. float delayed_usecs);
  113. static void jack_engine_notify_clients_about_delay (jack_engine_t *engine,
  114. float delayed_usecs);
  115. static void jack_engine_driver_exit (jack_engine_t* engine);
  116. static int jack_start_freewheeling (jack_engine_t* engine);
  117. static int jack_stop_freewheeling (jack_engine_t* engine);
  118. static int jack_start_watchdog (jack_engine_t *engine);
  119. static char *client_state_names[] = {
  120. "Not triggered",
  121. "Triggered",
  122. "Running",
  123. "Finished"
  124. };
  125. static inline int
  126. jack_client_is_internal (jack_client_internal_t *client)
  127. {
  128. return (client->control->type == ClientInternal) ||
  129. (client->control->type == ClientDriver);
  130. }
  131. static inline int
  132. jack_rolling_interval (jack_time_t period_usecs)
  133. {
  134. return floor ((JACK_ENGINE_ROLLING_INTERVAL * 1000.0f) / period_usecs);
  135. }
  136. static inline void
  137. jack_engine_reset_rolling_usecs (jack_engine_t *engine)
  138. {
  139. memset (engine->rolling_client_usecs, 0,
  140. sizeof (engine->rolling_client_usecs));
  141. engine->rolling_client_usecs_index = 0;
  142. engine->rolling_client_usecs_cnt = 0;
  143. if (engine->driver) {
  144. engine->rolling_interval =
  145. jack_rolling_interval (engine->driver->period_usecs);
  146. } else {
  147. engine->rolling_interval = JACK_ENGINE_ROLLING_INTERVAL;
  148. }
  149. engine->spare_usecs = 0;
  150. }
  151. static inline jack_port_type_info_t *
  152. jack_port_type_info (jack_engine_t *engine, jack_port_internal_t *port)
  153. {
  154. /* Returns a pointer to the port type information in the
  155. engine's shared control structure.
  156. */
  157. return &engine->control->port_types[port->shared->ptype_id];
  158. }
  159. static inline jack_port_buffer_list_t *
  160. jack_port_buffer_list (jack_engine_t *engine, jack_port_internal_t *port)
  161. {
  162. /* Points to the engine's private port buffer list struct. */
  163. return &engine->port_buffers[port->shared->ptype_id];
  164. }
  165. static int
  166. make_sockets (int fd[2])
  167. {
  168. struct sockaddr_un addr;
  169. int i;
  170. /* First, the master server socket */
  171. if ((fd[0] = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) {
  172. jack_error ("cannot create server socket (%s)",
  173. strerror (errno));
  174. return -1;
  175. }
  176. addr.sun_family = AF_UNIX;
  177. for (i = 0; i < 999; i++) {
  178. snprintf (addr.sun_path, sizeof (addr.sun_path) - 1,
  179. "%s/jack_%d_%d", jack_server_dir, getuid (), i);
  180. if (access (addr.sun_path, F_OK) != 0) {
  181. break;
  182. }
  183. }
  184. if (i == 999) {
  185. jack_error ("all possible server socket names in use!!!");
  186. close (fd[0]);
  187. return -1;
  188. }
  189. if (bind (fd[0], (struct sockaddr *) &addr, sizeof (addr)) < 0) {
  190. jack_error ("cannot bind server to socket (%s)",
  191. strerror (errno));
  192. close (fd[0]);
  193. return -1;
  194. }
  195. if (listen (fd[0], 1) < 0) {
  196. jack_error ("cannot enable listen on server socket (%s)",
  197. strerror (errno));
  198. close (fd[0]);
  199. return -1;
  200. }
  201. /* Now the client/server event ack server socket */
  202. if ((fd[1] = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) {
  203. jack_error ("cannot create event ACK socket (%s)",
  204. strerror (errno));
  205. close (fd[0]);
  206. return -1;
  207. }
  208. addr.sun_family = AF_UNIX;
  209. for (i = 0; i < 999; i++) {
  210. snprintf (addr.sun_path, sizeof (addr.sun_path) - 1,
  211. "%s/jack_%d_ack_%d", jack_server_dir, getuid (), i);
  212. if (access (addr.sun_path, F_OK) != 0) {
  213. break;
  214. }
  215. }
  216. if (i == 999) {
  217. jack_error ("all possible server ACK socket names in use!!!");
  218. close (fd[0]);
  219. close (fd[1]);
  220. return -1;
  221. }
  222. if (bind (fd[1], (struct sockaddr *) &addr, sizeof (addr)) < 0) {
  223. jack_error ("cannot bind server to socket (%s)",
  224. strerror (errno));
  225. close (fd[0]);
  226. close (fd[1]);
  227. return -1;
  228. }
  229. if (listen (fd[1], 1) < 0) {
  230. jack_error ("cannot enable listen on server socket (%s)",
  231. strerror (errno));
  232. close (fd[0]);
  233. close (fd[1]);
  234. return -1;
  235. }
  236. return 0;
  237. }
  238. void
  239. jack_cleanup_files ()
  240. {
  241. DIR *dir;
  242. struct dirent *dirent;
  243. /* its important that we remove all files that jackd creates
  244. because otherwise subsequent attempts to start jackd will
  245. believe that an instance is already running.
  246. */
  247. if ((dir = opendir (jack_server_dir)) == NULL) {
  248. fprintf (stderr, "jack(%d): cannot open jack FIFO directory "
  249. "(%s)\n", getpid(), strerror (errno));
  250. return;
  251. }
  252. while ((dirent = readdir (dir)) != NULL) {
  253. /* jack-99999999- is 14 chars long */
  254. char name_prefix1[15];
  255. char name_prefix2[15];
  256. snprintf (name_prefix1, sizeof (name_prefix1), "jack-%d-", getuid ());
  257. snprintf (name_prefix2, sizeof (name_prefix2), "jack_%d_", getuid ());
  258. if (strncmp (dirent->d_name, name_prefix1, strlen(name_prefix1)) == 0 ||
  259. strncmp (dirent->d_name, name_prefix2, strlen(name_prefix2)) == 0) {
  260. char fullpath[PATH_MAX+1];
  261. snprintf (fullpath, sizeof (fullpath), "%s/%s",
  262. jack_server_dir, dirent->d_name);
  263. unlink (fullpath);
  264. }
  265. }
  266. closedir (dir);
  267. }
  268. void
  269. jack_engine_place_port_buffers (jack_engine_t* engine,
  270. jack_port_type_id_t ptid,
  271. jack_shmsize_t one_buffer,
  272. jack_shmsize_t size,
  273. unsigned long nports)
  274. {
  275. jack_shmsize_t offset; /* shared memory offset */
  276. jack_port_buffer_info_t *bi;
  277. jack_port_buffer_list_t* pti = &engine->port_buffers[ptid];
  278. pthread_mutex_lock (&pti->lock);
  279. offset = 0;
  280. if (pti->info) {
  281. /* Buffer info array already allocated for this port
  282. * type. This must be a resize operation, so
  283. * recompute the buffer offsets, but leave the free
  284. * list alone.
  285. */
  286. int i;
  287. bi = pti->info;
  288. while (offset < size) {
  289. bi->offset = offset;
  290. offset += one_buffer;
  291. ++bi;
  292. }
  293. /* update any existing output port offsets */
  294. for (i = 0; i < engine->port_max; i++) {
  295. jack_port_shared_t *port = &engine->control->ports[i];
  296. if (port->in_use &&
  297. (port->flags & JackPortIsOutput) &&
  298. port->ptype_id == ptid) {
  299. bi = engine->internal_ports[i].buffer_info;
  300. if (bi) {
  301. port->offset = bi->offset;
  302. }
  303. }
  304. }
  305. } else {
  306. /* Allocate an array of buffer info structures for all
  307. * the buffers in the segment. Chain them to the free
  308. * list in memory address order, offset zero must come
  309. * first.
  310. */
  311. bi = pti->info = (jack_port_buffer_info_t *)
  312. malloc (nports * sizeof (jack_port_buffer_info_t));
  313. while (offset < size) {
  314. bi->offset = offset;
  315. pti->freelist = jack_slist_append (pti->freelist, bi);
  316. offset += one_buffer;
  317. ++bi;
  318. }
  319. /* allocate the first buffer of the audio port segment
  320. * for a zero-filled area
  321. */
  322. if (ptid == JACK_AUDIO_PORT_TYPE) {
  323. engine->silent_buffer = (jack_port_buffer_info_t *)
  324. pti->freelist->data;
  325. pti->freelist =
  326. jack_slist_remove_link (pti->freelist,
  327. pti->freelist);
  328. }
  329. }
  330. pthread_mutex_unlock (&pti->lock);
  331. }
  332. // JOQ: this should have a return code...
  333. static void
  334. jack_resize_port_segment (jack_engine_t *engine,
  335. jack_port_type_id_t ptid,
  336. unsigned long nports)
  337. {
  338. jack_event_t event;
  339. jack_shmsize_t one_buffer; /* size of one buffer */
  340. jack_shmsize_t size; /* segment size */
  341. jack_port_type_info_t* port_type = &engine->control->port_types[ptid];
  342. jack_shm_info_t* shm_info = &engine->port_segment[ptid];
  343. if (port_type->buffer_scale_factor < 0) {
  344. one_buffer = port_type->buffer_size;
  345. } else {
  346. one_buffer = sizeof (jack_default_audio_sample_t)
  347. * port_type->buffer_scale_factor
  348. * engine->control->buffer_size;
  349. }
  350. size = nports * one_buffer;
  351. if (shm_info->attached_at == 0) {
  352. char name[64];
  353. /* no segment allocated, yet */
  354. snprintf (name, sizeof(name), "/jck-[%s]",
  355. port_type->type_name);
  356. if (jack_shmalloc (name, size, shm_info)) {
  357. jack_error ("cannot create new port segment of %d"
  358. " bytes, name = %s (%s)",
  359. size, name,
  360. strerror (errno));
  361. return;
  362. }
  363. if (jack_attach_shm (shm_info)) {
  364. jack_error ("cannot attach to new port segment "
  365. "(name=%s) (%s)", name, strerror (errno));
  366. return;
  367. }
  368. engine->control->port_types[ptid].shm_registry_index =
  369. shm_info->index;
  370. } else {
  371. /* resize existing buffer segment */
  372. if (jack_resize_shm (shm_info, size)) {
  373. jack_error ("cannot resize port segment to %d bytes,"
  374. " (%s)", size,
  375. strerror (errno));
  376. return;
  377. }
  378. }
  379. jack_engine_place_port_buffers (engine, ptid, one_buffer, size, nports);
  380. if (ptid == JACK_AUDIO_PORT_TYPE) {
  381. /* Always zero `nframes' samples, it could have
  382. * changed. The server's global variable
  383. * jack_zero_filled_buffer is for internal clients.
  384. * External clients will set their copies during the
  385. * AttachPortSegment event. */
  386. jack_zero_filled_buffer =
  387. jack_shm_addr (shm_info)
  388. + engine->silent_buffer->offset;
  389. memset (jack_zero_filled_buffer, 0, one_buffer);
  390. }
  391. #ifdef USE_MLOCK
  392. if (engine->control->real_time) {
  393. /* Although we've called mlockall(CURRENT|FUTURE), the
  394. * Linux VM manager still allows newly allocated pages
  395. * to fault on first reference. This mlock() ensures
  396. * that any new pages are present before restarting
  397. * the process cycle. Since memory locks do not
  398. * stack, they can still be unlocked with a single
  399. * munlockall().
  400. */
  401. int rc = mlock (jack_shm_addr (shm_info), size);
  402. if (rc < 0) {
  403. jack_error("JACK: unable to mlock() port buffers: "
  404. "%s", strerror(errno));
  405. }
  406. }
  407. #endif /* USE_MLOCK */
  408. /* Tell everybody about this segment. */
  409. event.type = AttachPortSegment;
  410. event.y.ptid = ptid;
  411. jack_deliver_event_to_all (engine, &event);
  412. }
  413. /* The driver invokes this callback both initially and whenever its
  414. * buffer size changes.
  415. */
  416. static int
  417. jack_driver_buffer_size (jack_engine_t *engine, jack_nframes_t nframes)
  418. {
  419. int i;
  420. jack_event_t event;
  421. JSList *node;
  422. VERBOSE (engine, "new buffer size %" PRIu32 "\n", nframes);
  423. engine->control->buffer_size = nframes;
  424. if (engine->driver)
  425. engine->rolling_interval =
  426. jack_rolling_interval (engine->driver->period_usecs);
  427. for (i = 0; i < engine->control->n_port_types; ++i) {
  428. jack_resize_port_segment (engine, i, engine->control->port_max);
  429. }
  430. /* update shared client copy of nframes */
  431. jack_lock_graph (engine);
  432. for (node = engine->clients; node; node = jack_slist_next (node)) {
  433. jack_client_internal_t *client = node->data;
  434. client->control->nframes = nframes;
  435. }
  436. jack_unlock_graph (engine);
  437. event.type = BufferSizeChange;
  438. jack_deliver_event_to_all (engine, &event);
  439. return 0;
  440. }
  441. /* handle client SetBufferSize request */
  442. int
  443. jack_set_buffer_size_request (jack_engine_t *engine, jack_nframes_t nframes)
  444. {
  445. /* precondition: caller holds the request_lock */
  446. int rc;
  447. jack_driver_t* driver = engine->driver;
  448. if (driver == NULL)
  449. return ENXIO; /* no such device */
  450. if (!jack_power_of_two(nframes)) {
  451. jack_error("buffer size %" PRIu32 " not a power of 2",
  452. nframes);
  453. return EINVAL;
  454. }
  455. rc = driver->bufsize(driver, nframes);
  456. if (rc != 0)
  457. jack_error("driver does not support %" PRIu32
  458. "-frame buffers", nframes);
  459. return rc;
  460. }
  461. static JSList *
  462. jack_process_internal(jack_engine_t *engine, JSList *node,
  463. jack_nframes_t nframes)
  464. {
  465. jack_client_internal_t *client;
  466. jack_client_control_t *ctl;
  467. client = (jack_client_internal_t *) node->data;
  468. ctl = client->control;
  469. /* internal client ("plugin") */
  470. DEBUG ("invoking an internal client's callbacks");
  471. ctl->state = Running;
  472. engine->current_client = client;
  473. /* XXX how to time out an internal client? */
  474. if (ctl->sync_cb)
  475. jack_call_sync_client (ctl->private_client);
  476. if (ctl->process)
  477. if (ctl->process (nframes, ctl->process_arg)) {
  478. jack_error ("internal client %s failed", ctl->name);
  479. engine->process_errors++;
  480. }
  481. if (ctl->timebase_cb)
  482. jack_call_timebase_master (ctl->private_client);
  483. ctl->state = Finished;
  484. if (engine->process_errors)
  485. return NULL; /* will stop the loop */
  486. else
  487. return jack_slist_next (node);
  488. }
  489. #ifdef JACK_USE_MACH_THREADS
  490. static JSList *
  491. jack_process_external(jack_engine_t *engine, JSList *node)
  492. {
  493. jack_client_internal_t * client = (jack_client_internal_t *) node->data;
  494. jack_client_control_t *ctl;
  495. client = (jack_client_internal_t *) node->data;
  496. ctl = client->control;
  497. engine->current_client = client;
  498. // a race exists if we do this after the write(2)
  499. ctl->state = Triggered;
  500. ctl->signalled_at = jack_get_microseconds();
  501. ctl->awake_at = 0;
  502. ctl->finished_at = 0;
  503. if (jack_client_resume(client) < 0) {
  504. jack_error("Client will be removed\n");
  505. ctl->state = Finished;
  506. }
  507. return jack_slist_next (node);
  508. }
  509. #else /* !JACK_USE_MACH_THREADS */
  510. static JSList *
  511. jack_process_external(jack_engine_t *engine, JSList *node)
  512. {
  513. int status = 0;
  514. char c;
  515. struct pollfd pfd[1];
  516. int poll_timeout;
  517. jack_client_internal_t *client;
  518. jack_client_control_t *ctl;
  519. jack_time_t now, then;
  520. client = (jack_client_internal_t *) node->data;
  521. ctl = client->control;
  522. /* external subgraph */
  523. /* a race exists if we do this after the write(2) */
  524. ctl->state = Triggered;
  525. ctl->signalled_at = jack_get_microseconds();
  526. ctl->awake_at = 0;
  527. ctl->finished_at = 0;
  528. engine->current_client = client;
  529. DEBUG ("calling process() on an external subgraph, fd==%d",
  530. client->subgraph_start_fd);
  531. if (write (client->subgraph_start_fd, &c, sizeof (c)) != sizeof (c)) {
  532. jack_error ("cannot initiate graph processing (%s)",
  533. strerror (errno));
  534. engine->process_errors++;
  535. return NULL; /* will stop the loop */
  536. }
  537. then = jack_get_microseconds ();
  538. if (engine->freewheeling) {
  539. poll_timeout = 10000; /* 10 seconds */
  540. } else {
  541. poll_timeout = (engine->control->real_time == 0 ?
  542. engine->client_timeout_msecs :
  543. 1 + engine->driver->period_usecs/1000);
  544. }
  545. pfd[0].fd = client->subgraph_wait_fd;
  546. pfd[0].events = POLLERR|POLLIN|POLLHUP|POLLNVAL;
  547. DEBUG ("waiting on fd==%d for process() subgraph to finish",
  548. client->subgraph_wait_fd);
  549. if (poll (pfd, 1, poll_timeout) < 0) {
  550. jack_error ("poll on subgraph processing failed (%s)",
  551. strerror (errno));
  552. status = -1;
  553. }
  554. DEBUG ("\n\n\n\n\n back from subgraph poll, revents = 0x%x\n\n\n", pfd[0].revents);
  555. if (pfd[0].revents & ~POLLIN) {
  556. jack_error ("subgraph starting at %s lost client",
  557. client->control->name);
  558. status = -2;
  559. }
  560. if (pfd[0].revents & POLLIN) {
  561. status = 0;
  562. } else {
  563. jack_error ("subgraph starting at %s timed out "
  564. "(subgraph_wait_fd=%d, status = %d, state = %s)",
  565. client->control->name,
  566. client->subgraph_wait_fd, status,
  567. client_state_names[client->control->state]);
  568. status = 1;
  569. }
  570. now = jack_get_microseconds ();
  571. if (status != 0) {
  572. VERBOSE (engine, "at %" PRIu64
  573. " waiting on %d for %" PRIu64
  574. " usecs, status = %d sig = %" PRIu64
  575. " awa = %" PRIu64 " fin = %" PRIu64
  576. " dur=%" PRIu64 "\n",
  577. now,
  578. client->subgraph_wait_fd,
  579. now - then,
  580. status,
  581. ctl->signalled_at,
  582. ctl->awake_at,
  583. ctl->finished_at,
  584. ctl->finished_at? (ctl->finished_at -
  585. ctl->signalled_at): 0);
  586. /* we can only consider the timeout a client error if
  587. * it actually woke up. its possible that the kernel
  588. * scheduler screwed us up and never woke up the
  589. * client in time. sigh.
  590. */
  591. if (ctl->awake_at > 0) {
  592. ctl->timed_out++;
  593. }
  594. engine->process_errors++;
  595. return NULL; /* will stop the loop */
  596. } else {
  597. DEBUG ("reading byte from subgraph_wait_fd==%d",
  598. client->subgraph_wait_fd);
  599. if (read (client->subgraph_wait_fd, &c, sizeof(c))
  600. != sizeof (c)) {
  601. jack_error ("pp: cannot clean up byte from graph wait "
  602. "fd (%s)", strerror (errno));
  603. client->error++;
  604. return NULL; /* will stop the loop */
  605. }
  606. }
  607. /* Move to next internal client (or end of client list) */
  608. while (node) {
  609. if (jack_client_is_internal ((jack_client_internal_t *)
  610. node->data)) {
  611. break;
  612. }
  613. node = jack_slist_next (node);
  614. }
  615. return node;
  616. }
  617. #endif /* JACK_USE_MACH_THREADS */
  618. static int
  619. jack_engine_process (jack_engine_t *engine, jack_nframes_t nframes)
  620. {
  621. /* precondition: caller has graph_lock */
  622. jack_client_internal_t *client;
  623. JSList *node;
  624. engine->process_errors = 0;
  625. engine->watchdog_check = 1;
  626. for (node = engine->clients; node; node = jack_slist_next (node)) {
  627. jack_client_control_t *ctl =
  628. ((jack_client_internal_t *) node->data)->control;
  629. ctl->state = NotTriggered;
  630. ctl->nframes = nframes;
  631. ctl->timed_out = 0;
  632. }
  633. for (node = engine->clients; engine->process_errors == 0 && node; ) {
  634. client = (jack_client_internal_t *) node->data;
  635. DEBUG ("considering client %s for processing",
  636. client->control->name);
  637. if (!client->control->active || client->control->dead) {
  638. node = jack_slist_next (node);
  639. } else if (jack_client_is_internal (client)) {
  640. node = jack_process_internal (engine, node, nframes);
  641. } else {
  642. node = jack_process_external (engine, node);
  643. }
  644. }
  645. return engine->process_errors > 0;
  646. }
  647. static void
  648. jack_calc_cpu_load(jack_engine_t *engine)
  649. {
  650. jack_time_t cycle_end = jack_get_microseconds ();
  651. /* store the execution time for later averaging */
  652. engine->rolling_client_usecs[engine->rolling_client_usecs_index++] =
  653. cycle_end - engine->control->current_time.usecs;
  654. if (engine->rolling_client_usecs_index >= JACK_ENGINE_ROLLING_COUNT) {
  655. engine->rolling_client_usecs_index = 0;
  656. }
  657. /* every so often, recompute the current maximum use over the
  658. last JACK_ENGINE_ROLLING_COUNT client iterations.
  659. */
  660. if (++engine->rolling_client_usecs_cnt
  661. % engine->rolling_interval == 0) {
  662. float max_usecs = 0.0f;
  663. int i;
  664. for (i = 0; i < JACK_ENGINE_ROLLING_COUNT; i++) {
  665. if (engine->rolling_client_usecs[i] > max_usecs) {
  666. max_usecs = engine->rolling_client_usecs[i];
  667. }
  668. }
  669. if (max_usecs > engine->max_usecs) {
  670. engine->max_usecs = max_usecs;
  671. }
  672. if (max_usecs < engine->driver->period_usecs) {
  673. engine->spare_usecs =
  674. engine->driver->period_usecs - max_usecs;
  675. } else {
  676. engine->spare_usecs = 0;
  677. }
  678. engine->control->cpu_load =
  679. (1.0f - (engine->spare_usecs /
  680. engine->driver->period_usecs)) * 50.0f
  681. + (engine->control->cpu_load * 0.5f);
  682. VERBOSE (engine, "load = %.4f max usecs: %.3f, "
  683. "spare = %.3f\n", engine->control->cpu_load,
  684. max_usecs, engine->spare_usecs);
  685. }
  686. }
  687. static void
  688. jack_remove_clients (jack_engine_t* engine)
  689. {
  690. JSList *tmp, *node;
  691. int need_sort = FALSE;
  692. jack_client_internal_t *client;
  693. /* remove all dead clients */
  694. for (node = engine->clients; node; ) {
  695. tmp = jack_slist_next (node);
  696. client = (jack_client_internal_t *) node->data;
  697. if (client->error) {
  698. /* if we have a communication problem with the
  699. client, remove it. otherwise, turn it into
  700. a zombie. the client will/should realize
  701. this and will close its sockets. then
  702. we'll end up back here again and will
  703. finally remove the client.
  704. */
  705. if (client->error >= JACK_ERROR_WITH_SOCKETS) {
  706. VERBOSE (engine, "removing failed "
  707. "client %s state = %s errors"
  708. " = %d\n",
  709. client->control->name,
  710. client_state_names[
  711. client->control->state
  712. ],
  713. client->error);
  714. jack_remove_client (engine,
  715. (jack_client_internal_t *)
  716. node->data);
  717. } else {
  718. VERBOSE (engine, "client failure: "
  719. "client %s state = %s errors"
  720. " = %d\n",
  721. client->control->name,
  722. client_state_names[
  723. client->control->state
  724. ],
  725. client->error);
  726. jack_zombify_client (engine,
  727. (jack_client_internal_t *)
  728. node->data);
  729. client->error = 0;
  730. }
  731. need_sort = TRUE;
  732. }
  733. node = tmp;
  734. }
  735. if (need_sort) {
  736. jack_sort_graph (engine);
  737. }
  738. jack_engine_reset_rolling_usecs (engine);
  739. }
  740. static void
  741. jack_engine_post_process (jack_engine_t *engine)
  742. {
  743. /* precondition: caller holds the graph lock. */
  744. jack_client_control_t *ctl;
  745. jack_client_internal_t *client;
  746. JSList *node;
  747. int need_remove = FALSE;
  748. jack_transport_cycle_end (engine);
  749. /* find any clients that need removal due to timeouts, etc. */
  750. for (node = engine->clients; node; node = jack_slist_next (node) ) {
  751. client = (jack_client_internal_t *) node->data;
  752. ctl = client->control;
  753. /* this check is invalid for internal clients and
  754. external clients with no process callback.
  755. */
  756. if (!jack_client_is_internal (client) && ctl->process) {
  757. if (ctl->awake_at != 0 &&
  758. ctl->state > NotTriggered &&
  759. ctl->state != Finished &&
  760. ctl->timed_out++) {
  761. VERBOSE(engine, "client %s error: awake_at = %"
  762. PRIu64
  763. " state = %d timed_out = %d\n",
  764. ctl->name,
  765. ctl->awake_at,
  766. ctl->state,
  767. ctl->timed_out);
  768. client->error++;
  769. }
  770. }
  771. if (client->error) {
  772. need_remove = TRUE;
  773. }
  774. }
  775. if (need_remove) {
  776. jack_remove_clients (engine);
  777. }
  778. jack_calc_cpu_load (engine);
  779. }
  780. static int
  781. jack_load_client (jack_engine_t *engine, jack_client_internal_t *client,
  782. const char *so_name)
  783. {
  784. const char *errstr;
  785. char path_to_so[PATH_MAX+1];
  786. snprintf (path_to_so, sizeof (path_to_so), ADDON_DIR "/%s.so", so_name);
  787. client->handle = dlopen (path_to_so, RTLD_NOW|RTLD_GLOBAL);
  788. if (client->handle == 0) {
  789. if ((errstr = dlerror ()) != 0) {
  790. jack_error ("can't load \"%s\": %s", path_to_so,
  791. errstr);
  792. } else {
  793. jack_error ("bizarre error loading shared object %s",
  794. so_name);
  795. }
  796. return -1;
  797. }
  798. client->initialize = dlsym (client->handle, "jack_initialize");
  799. if ((errstr = dlerror ()) != 0) {
  800. jack_error ("no initialize function in shared object %s\n",
  801. so_name);
  802. dlclose (client->handle);
  803. client->handle = 0;
  804. return -1;
  805. }
  806. client->finish = (void (*)(void *)) dlsym (client->handle,
  807. "jack_finish");
  808. if ((errstr = dlerror ()) != 0) {
  809. jack_error ("no finish function in in shared object %s",
  810. so_name);
  811. dlclose (client->handle);
  812. client->handle = 0;
  813. return -1;
  814. }
  815. return 0;
  816. }
  817. static void
  818. jack_client_unload (jack_client_internal_t *client)
  819. {
  820. if (client->handle) {
  821. if (client->finish) {
  822. client->finish (client->control->process_arg);
  823. }
  824. dlclose (client->handle);
  825. }
  826. }
  827. static jack_client_internal_t *
  828. jack_client_lookup_name (jack_engine_t *engine, char *name)
  829. {
  830. JSList *node;
  831. jack_lock_graph (engine);
  832. for (node = engine->clients; node; node = jack_slist_next (node)) {
  833. jack_client_internal_t *client = node->data;
  834. if (strncmp(name, (char *) client->control->name,
  835. JACK_CLIENT_NAME_SIZE) == 0) {
  836. jack_unlock_graph (engine);
  837. return client; /* name exists */
  838. }
  839. }
  840. jack_unlock_graph (engine);
  841. return NULL; /* not found */
  842. }
  843. /* generate a unique client name
  844. *
  845. * returns 0 if successful, updates name in place
  846. */
  847. static inline int
  848. jack_generate_unique_name (jack_engine_t *engine, char *name)
  849. {
  850. int tens, ones;
  851. int length = strlen (name);
  852. if (length > JACK_CLIENT_NAME_SIZE - 4) {
  853. jack_error ("%s exists and is too long to make unique", name);
  854. return 1; /* failure */
  855. }
  856. /* generate a unique name by appending "-01".."-99" */
  857. name[length++] = '-';
  858. tens = length++;
  859. ones = length++;
  860. name[tens] = '0';
  861. name[ones] = '1';
  862. name[length] = '\0';
  863. while (jack_client_lookup_name (engine, name)) {
  864. if (name[ones] == '9') {
  865. if (name[tens] == '9') {
  866. jack_error ("client %s has 99 extra"
  867. " instances already", name);
  868. return 1; /* give up */
  869. }
  870. name[tens]++;
  871. name[ones] = '0';
  872. } else {
  873. name[ones]++;
  874. }
  875. }
  876. return 0;
  877. }
  878. static jack_client_internal_t *
  879. setup_client (jack_engine_t *engine, int client_fd,
  880. jack_client_connect_request_t *req,
  881. jack_client_connect_result_t *res)
  882. {
  883. jack_client_internal_t *client;
  884. /* Since this thread already holds the request_lock, no other
  885. * new client will be created at the same time. So, testing a
  886. * name for uniqueness is valid here. */
  887. //JOQ: watch out for internal clients, they come here too
  888. if (jack_client_lookup_name (engine, req->name)) {
  889. res->open_status |= JackNameNotUnique;
  890. if (req->options & JackUseExactName) {
  891. jack_error ("cannot create new client; %s already"
  892. " exists", req->name);
  893. return NULL;
  894. }
  895. if (jack_generate_unique_name(engine, req->name)) {
  896. return NULL; /* failure */
  897. }
  898. }
  899. /* create a client struct for this client name */
  900. client = jack_setup_client_control (engine, client_fd, req);
  901. if (client == NULL) {
  902. jack_error ("cannot create new client object");
  903. return NULL;
  904. }
  905. VERBOSE (engine, "new client: %s, id = %" PRIu32
  906. " type %d @ %p fd = %d\n",
  907. client->control->name, client->control->id,
  908. req->type, client->control, client_fd);
  909. res->protocol_v = jack_protocol_version;
  910. res->client_shm = client->control_shm;
  911. res->engine_shm = engine->control_shm;
  912. res->realtime = engine->control->real_time;
  913. res->realtime_priority = engine->rtpriority - 1;
  914. strncpy (res->name, req->name, sizeof(res->name));
  915. #ifdef JACK_USE_MACH_THREADS
  916. /* specific resources for server/client real-time thread
  917. * communication */
  918. res->portnum = client->portnum;
  919. #endif
  920. if (jack_client_is_internal(client)) {
  921. /* set up the pointers necessary for the request
  922. * system to work. */
  923. client->control->deliver_request = internal_client_request;
  924. client->control->deliver_arg = engine;
  925. /* the client is in the same address space */
  926. res->client_control = client->control;
  927. res->engine_control = engine->control;
  928. } else {
  929. strcpy (res->fifo_prefix, engine->fifo_prefix);
  930. }
  931. /* add new client to the clients list */
  932. jack_lock_graph (engine);
  933. engine->clients = jack_slist_prepend (engine->clients, client);
  934. jack_engine_reset_rolling_usecs (engine);
  935. switch (client->control->type) {
  936. case ClientDriver:
  937. case ClientInternal:
  938. /* an internal client still needs to be able to make
  939. regular JACK API calls, which need a jack_client_t
  940. structure. create one here for it.
  941. */
  942. client->control->private_client =
  943. jack_client_alloc_internal (client->control, engine);
  944. jack_unlock_graph (engine);
  945. /* call its initialization function */
  946. if (client->control->type == ClientInternal) {
  947. if (client->initialize (client->control->private_client,
  948. req->object_data)) {
  949. /* failed: clean up client data */
  950. VERBOSE (engine,
  951. "%s jack_initialize() failed!\n",
  952. client->control->name);
  953. jack_lock_graph (engine);
  954. jack_remove_client (engine, client);
  955. jack_unlock_graph (engine);
  956. return NULL;
  957. }
  958. }
  959. /* its good to go */
  960. break;
  961. default: /* external client */
  962. if (engine->pfd_max >= engine->pfd_size) {
  963. engine->pfd = (struct pollfd *)
  964. realloc (engine->pfd, sizeof (struct pollfd)
  965. * (engine->pfd_size + 16));
  966. engine->pfd_size += 16;
  967. }
  968. engine->pfd[engine->pfd_max].fd = client->request_fd;
  969. engine->pfd[engine->pfd_max].events =
  970. POLLIN|POLLPRI|POLLERR|POLLHUP|POLLNVAL;
  971. engine->pfd_max++;
  972. jack_unlock_graph (engine);
  973. break;
  974. }
  975. return client;
  976. }
  977. static jack_driver_info_t *
  978. jack_load_driver (jack_engine_t *engine, jack_driver_desc_t * driver_desc)
  979. {
  980. const char *errstr;
  981. jack_driver_info_t *info;
  982. info = (jack_driver_info_t *) calloc (1, sizeof (*info));
  983. info->handle = dlopen (driver_desc->file, RTLD_NOW|RTLD_GLOBAL);
  984. if (info->handle == NULL) {
  985. if ((errstr = dlerror ()) != 0) {
  986. jack_error ("can't load \"%s\": %s", driver_desc->file,
  987. errstr);
  988. } else {
  989. jack_error ("bizarre error loading driver shared "
  990. "object %s", driver_desc->file);
  991. }
  992. goto fail;
  993. }
  994. info->initialize = dlsym (info->handle, "driver_initialize");
  995. if ((errstr = dlerror ()) != 0) {
  996. jack_error ("no initialize function in shared object %s\n",
  997. driver_desc->file);
  998. goto fail;
  999. }
  1000. info->finish = dlsym (info->handle, "driver_finish");
  1001. if ((errstr = dlerror ()) != 0) {
  1002. jack_error ("no finish function in in shared driver object %s",
  1003. driver_desc->file);
  1004. goto fail;
  1005. }
  1006. info->client_name = (char *) dlsym (info->handle, "driver_client_name");
  1007. if ((errstr = dlerror ()) != 0) {
  1008. jack_error ("no client name in in shared driver object %s",
  1009. driver_desc->file);
  1010. goto fail;
  1011. }
  1012. return info;
  1013. fail:
  1014. if (info->handle) {
  1015. dlclose (info->handle);
  1016. }
  1017. free (info);
  1018. return NULL;
  1019. }
  1020. void
  1021. jack_driver_unload (jack_driver_t *driver)
  1022. {
  1023. driver->finish (driver);
  1024. dlclose (driver->handle);
  1025. }
  1026. int
  1027. jack_engine_load_driver (jack_engine_t *engine, jack_driver_desc_t * driver_desc, JSList * driver_params)
  1028. {
  1029. jack_client_connect_request_t req;
  1030. jack_client_connect_result_t res;
  1031. jack_client_internal_t *client;
  1032. jack_driver_t *driver;
  1033. jack_driver_info_t *info;
  1034. if ((info = jack_load_driver (engine, driver_desc)) == NULL) {
  1035. return -1;
  1036. }
  1037. req.type = ClientDriver;
  1038. snprintf (req.name, sizeof (req.name), "%s", info->client_name);
  1039. if ((client = setup_client (engine, -1, &req, &res)) == NULL) {
  1040. return -1;
  1041. }
  1042. if ((driver = info->initialize (client->control->private_client,
  1043. driver_params)) == NULL) {
  1044. free (info);
  1045. return -1;
  1046. }
  1047. driver->handle = info->handle;
  1048. driver->finish = info->finish;
  1049. driver->internal_client = client;
  1050. free (info);
  1051. if (jack_use_driver (engine, driver)) {
  1052. jack_driver_unload (driver);
  1053. jack_client_delete (engine, client);
  1054. return -1;
  1055. }
  1056. engine->driver_desc = driver_desc;
  1057. engine->driver_params = driver_params;
  1058. if (engine->control->real_time) {
  1059. /* Stephane Letz : letz@grame.fr
  1060. Watch dog thread is not needed on MacOSX since CoreAudio drivers
  1061. already contains a similar mechanism.
  1062. */
  1063. #ifndef JACK_USE_MACH_THREADS
  1064. if (jack_start_watchdog (engine)) {
  1065. return -1;
  1066. }
  1067. engine->watchdog_check = 1;
  1068. #endif
  1069. }
  1070. return 0;
  1071. }
  1072. static int
  1073. handle_unload_client (jack_engine_t *engine, int client_fd,
  1074. jack_client_connect_request_t *req)
  1075. {
  1076. JSList *node;
  1077. jack_client_connect_result_t res;
  1078. res.status = -1;
  1079. VERBOSE (engine, "unloading client \"%s\"\n", req->name);
  1080. jack_lock_graph (engine);
  1081. for (node = engine->clients; node; node = jack_slist_next (node)) {
  1082. if (strcmp ((char *) ((jack_client_internal_t *)
  1083. node->data)->control->name,
  1084. req->name) == 0) {
  1085. jack_remove_client (engine, (jack_client_internal_t *)
  1086. node->data);
  1087. res.status = 0;
  1088. break;
  1089. }
  1090. }
  1091. jack_unlock_graph (engine);
  1092. return 0;
  1093. }
  1094. static int
  1095. handle_new_client (jack_engine_t *engine, int client_fd)
  1096. {
  1097. jack_client_internal_t *client;
  1098. jack_client_connect_request_t req;
  1099. jack_client_connect_result_t res;
  1100. //JOQ: fix messy overloading of `status'
  1101. res.status = 0;
  1102. res.open_status = 0;
  1103. if (read (client_fd, &req, sizeof (req)) != sizeof (req)) {
  1104. jack_error ("cannot read connection request from client");
  1105. return -1;
  1106. }
  1107. if (!req.load) {
  1108. return handle_unload_client (engine, client_fd, &req);
  1109. }
  1110. if ((client = setup_client (engine, client_fd, &req, &res)) == NULL) {
  1111. return -1;
  1112. }
  1113. if (write (client->request_fd, &res, sizeof (res)) != sizeof (res)) {
  1114. jack_error ("cannot write connection response to client");
  1115. jack_client_delete (engine, client);
  1116. return -1;
  1117. }
  1118. switch (client->control->type) {
  1119. case ClientDriver:
  1120. case ClientInternal:
  1121. close (client_fd);
  1122. break;
  1123. default:
  1124. break;
  1125. }
  1126. return 0;
  1127. }
  1128. static int
  1129. handle_client_ack_connection (jack_engine_t *engine, int client_fd)
  1130. {
  1131. jack_client_internal_t *client;
  1132. jack_client_connect_ack_request_t req;
  1133. jack_client_connect_ack_result_t res;
  1134. if (read (client_fd, &req, sizeof (req)) != sizeof (req)) {
  1135. jack_error ("cannot read ACK connection request from client");
  1136. return -1;
  1137. }
  1138. if ((client = jack_client_internal_by_id (engine, req.client_id))
  1139. == NULL) {
  1140. jack_error ("unknown client ID in ACK connection request");
  1141. return -1;
  1142. }
  1143. client->event_fd = client_fd;
  1144. res.status = 0;
  1145. if (write (client->event_fd, &res, sizeof (res)) != sizeof (res)) {
  1146. jack_error ("cannot write ACK connection response to client");
  1147. return -1;
  1148. }
  1149. return 0;
  1150. }
  1151. #ifdef USE_CAPABILITIES
  1152. static int check_capabilities (jack_engine_t *engine)
  1153. {
  1154. cap_t caps = cap_init();
  1155. cap_flag_value_t cap;
  1156. pid_t pid;
  1157. int have_all_caps = 1;
  1158. if (caps == NULL) {
  1159. VERBOSE (engine, "check: could not allocate capability"
  1160. " working storage\n");
  1161. return 0;
  1162. }
  1163. pid = getpid ();
  1164. cap_clear (caps);
  1165. if (capgetp (pid, caps)) {
  1166. VERBOSE (engine, "check: could not get capabilities "
  1167. "for process %d\n", pid);
  1168. return 0;
  1169. }
  1170. /* check that we are able to give capabilites to other processes */
  1171. cap_get_flag(caps, CAP_SETPCAP, CAP_EFFECTIVE, &cap);
  1172. if (cap == CAP_CLEAR) {
  1173. have_all_caps = 0;
  1174. goto done;
  1175. }
  1176. /* check that we have the capabilities we want to transfer */
  1177. cap_get_flag(caps, CAP_SYS_NICE, CAP_EFFECTIVE, &cap);
  1178. if (cap == CAP_CLEAR) {
  1179. have_all_caps = 0;
  1180. goto done;
  1181. }
  1182. cap_get_flag(caps, CAP_SYS_RESOURCE, CAP_EFFECTIVE, &cap);
  1183. if (cap == CAP_CLEAR) {
  1184. have_all_caps = 0;
  1185. goto done;
  1186. }
  1187. cap_get_flag(caps, CAP_IPC_LOCK, CAP_EFFECTIVE, &cap);
  1188. if (cap == CAP_CLEAR) {
  1189. have_all_caps = 0;
  1190. goto done;
  1191. }
  1192. done:
  1193. cap_free (caps);
  1194. return have_all_caps;
  1195. }
  1196. static int give_capabilities (jack_engine_t *engine, pid_t pid)
  1197. {
  1198. cap_t caps = cap_init();
  1199. const unsigned caps_size = 3;
  1200. cap_value_t cap_list[] = {CAP_SYS_NICE, CAP_SYS_RESOURCE, CAP_IPC_LOCK};
  1201. if (caps == NULL) {
  1202. VERBOSE (engine, "give: could not allocate capability"
  1203. " working storage\n");
  1204. return -1;
  1205. }
  1206. cap_clear(caps);
  1207. if (capgetp (pid, caps)) {
  1208. VERBOSE (engine, "give: could not get current "
  1209. "capabilities for process %d\n", pid);
  1210. cap_clear(caps);
  1211. }
  1212. cap_set_flag(caps, CAP_EFFECTIVE, caps_size, cap_list , CAP_SET);
  1213. cap_set_flag(caps, CAP_INHERITABLE, caps_size, cap_list , CAP_SET);
  1214. cap_set_flag(caps, CAP_PERMITTED, caps_size, cap_list , CAP_SET);
  1215. if (capsetp (pid, caps)) {
  1216. cap_free (caps);
  1217. return -1;
  1218. }
  1219. cap_free (caps);
  1220. return 0;
  1221. }
  1222. static int
  1223. jack_set_client_capabilities (jack_engine_t *engine, jack_client_id_t id)
  1224. {
  1225. JSList *node;
  1226. int ret = -1;
  1227. jack_lock_graph (engine);
  1228. for (node = engine->clients; node; node = jack_slist_next (node)) {
  1229. jack_client_internal_t *client =
  1230. (jack_client_internal_t *) node->data;
  1231. if (client->control->id == id) {
  1232. /* before sending this request the client has
  1233. already checked that the engine has
  1234. realtime capabilities, that it is running
  1235. realtime and that the pid is defined
  1236. */
  1237. ret = give_capabilities (engine, client->control->pid);
  1238. if (ret) {
  1239. jack_error ("could not give capabilities to "
  1240. "process %d\n",
  1241. client->control->pid);
  1242. } else {
  1243. VERBOSE (engine, "gave capabilities to"
  1244. " process %d\n",
  1245. client->control->pid);
  1246. }
  1247. }
  1248. }
  1249. jack_unlock_graph (engine);
  1250. return ret;
  1251. }
  1252. #endif /* USE_CAPABILITIES */
  1253. static int
  1254. jack_client_activate (jack_engine_t *engine, jack_client_id_t id)
  1255. {
  1256. jack_client_internal_t *client;
  1257. JSList *node;
  1258. int ret = -1;
  1259. jack_lock_graph (engine);
  1260. for (node = engine->clients; node; node = jack_slist_next (node)) {
  1261. if (((jack_client_internal_t *) node->data)->control->id
  1262. == id) {
  1263. client = (jack_client_internal_t *) node->data;
  1264. client->control->active = TRUE;
  1265. jack_transport_activate(engine, client);
  1266. /* we call this to make sure the FIFO is
  1267. * built+ready by the time the client needs
  1268. * it. we don't care about the return value at
  1269. * this point.
  1270. */
  1271. jack_get_fifo_fd (engine,
  1272. ++engine->external_client_cnt);
  1273. jack_sort_graph (engine);
  1274. ret = 0;
  1275. break;
  1276. }
  1277. }
  1278. jack_unlock_graph (engine);
  1279. return ret;
  1280. }
  1281. static int
  1282. jack_client_do_deactivate (jack_engine_t *engine,
  1283. jack_client_internal_t *client, int sort_graph)
  1284. {
  1285. /* caller must hold engine->client_lock and must have checked for and/or
  1286. * cleared all connections held by client. */
  1287. client->control->active = FALSE;
  1288. jack_transport_client_exit (engine, client);
  1289. if (!jack_client_is_internal (client) &&
  1290. engine->external_client_cnt > 0) {
  1291. engine->external_client_cnt--;
  1292. }
  1293. if (sort_graph) {
  1294. jack_sort_graph (engine);
  1295. }
  1296. return 0;
  1297. }
  1298. static void
  1299. jack_client_disconnect (jack_engine_t *engine, jack_client_internal_t *client)
  1300. {
  1301. JSList *node;
  1302. jack_port_internal_t *port;
  1303. /* call tree **** MUST HOLD *** engine->client_lock */
  1304. for (node = client->ports; node; node = jack_slist_next (node)) {
  1305. port = (jack_port_internal_t *) node->data;
  1306. jack_port_clear_connections (engine, port);
  1307. jack_port_registration_notify (engine, port->shared->id, FALSE);
  1308. jack_port_release (engine, port);
  1309. }
  1310. jack_slist_free (client->ports);
  1311. jack_slist_free (client->fed_by);
  1312. client->fed_by = 0;
  1313. client->ports = 0;
  1314. }
  1315. static int
  1316. jack_client_deactivate (jack_engine_t *engine, jack_client_id_t id)
  1317. {
  1318. JSList *node;
  1319. int ret = -1;
  1320. jack_lock_graph (engine);
  1321. for (node = engine->clients; node; node = jack_slist_next (node)) {
  1322. jack_client_internal_t *client =
  1323. (jack_client_internal_t *) node->data;
  1324. if (client->control->id == id) {
  1325. JSList *portnode;
  1326. jack_port_internal_t *port;
  1327. for (portnode = client->ports; portnode;
  1328. portnode = jack_slist_next (portnode)) {
  1329. port = (jack_port_internal_t *) portnode->data;
  1330. jack_port_clear_connections (engine, port);
  1331. }
  1332. ret = jack_client_do_deactivate (engine, client, TRUE);
  1333. break;
  1334. }
  1335. }
  1336. jack_unlock_graph (engine);
  1337. return ret;
  1338. }
  1339. static int
  1340. handle_client_socket_error (jack_engine_t *engine, int fd)
  1341. {
  1342. jack_client_internal_t *client = 0;
  1343. JSList *node;
  1344. #ifndef DEFER_CLIENT_REMOVE_TO_AUDIO_THREAD
  1345. jack_lock_graph (engine);
  1346. for (node = engine->clients; node; node = jack_slist_next (node)) {
  1347. if (jack_client_is_internal((jack_client_internal_t *)
  1348. node->data)) {
  1349. continue;
  1350. }
  1351. if (((jack_client_internal_t *) node->data)->request_fd == fd) {
  1352. client = (jack_client_internal_t *) node->data;
  1353. break;
  1354. }
  1355. }
  1356. if (client) {
  1357. VERBOSE (engine, "removing failed client %s state = "
  1358. "%s errors = %d\n", client->control->name,
  1359. client_state_names[client->control->state],
  1360. client->error);
  1361. jack_remove_client(engine, client);
  1362. jack_sort_graph (engine);
  1363. }
  1364. jack_unlock_graph (engine);
  1365. #else
  1366. jack_lock_graph (engine);
  1367. for (node = engine->clients; node; node = jack_slist_next (node)) {
  1368. if (jack_client_is_internal((jack_client_internal_t *)
  1369. node->data)) {
  1370. continue;
  1371. }
  1372. if (((jack_client_internal_t *) node->data)->request_fd == fd) {
  1373. client = (jack_client_internal_t *) node->data;
  1374. if (client->error < JACK_ERROR_WITH_SOCKETS) {
  1375. client->error += JACK_ERROR_WITH_SOCKETS;
  1376. }
  1377. break;
  1378. }
  1379. }
  1380. jack_unlock_graph (engine);
  1381. #endif
  1382. return 0;
  1383. }
  1384. static void
  1385. do_request (jack_engine_t *engine, jack_request_t *req, int *reply_fd)
  1386. {
  1387. pthread_mutex_lock (&engine->request_lock);
  1388. DEBUG ("got a request of type %d", req->type);
  1389. switch (req->type) {
  1390. case RegisterPort:
  1391. req->status = jack_port_do_register (engine, req);
  1392. break;
  1393. case UnRegisterPort:
  1394. req->status = jack_port_do_unregister (engine, req);
  1395. break;
  1396. case ConnectPorts:
  1397. req->status = jack_port_do_connect
  1398. (engine, req->x.connect.source_port,
  1399. req->x.connect.destination_port);
  1400. break;
  1401. case DisconnectPort:
  1402. req->status = jack_port_do_disconnect_all
  1403. (engine, req->x.port_info.port_id);
  1404. break;
  1405. case DisconnectPorts:
  1406. req->status = jack_port_do_disconnect
  1407. (engine, req->x.connect.source_port,
  1408. req->x.connect.destination_port);
  1409. break;
  1410. case ActivateClient:
  1411. req->status = jack_client_activate (engine, req->x.client_id);
  1412. break;
  1413. case DeactivateClient:
  1414. req->status = jack_client_deactivate (engine, req->x.client_id);
  1415. break;
  1416. case SetTimeBaseClient:
  1417. req->status = jack_timebase_set (engine,
  1418. req->x.timebase.client_id,
  1419. req->x.timebase.conditional);
  1420. break;
  1421. case ResetTimeBaseClient:
  1422. req->status = jack_timebase_reset (engine, req->x.client_id);
  1423. break;
  1424. case SetSyncClient:
  1425. req->status =
  1426. jack_transport_client_set_sync (engine,
  1427. req->x.client_id);
  1428. break;
  1429. case ResetSyncClient:
  1430. req->status =
  1431. jack_transport_client_reset_sync (engine,
  1432. req->x.client_id);
  1433. break;
  1434. case SetSyncTimeout:
  1435. req->status = jack_transport_set_sync_timeout (engine,
  1436. req->x.timeout);
  1437. break;
  1438. #ifdef USE_CAPABILITIES
  1439. case SetClientCapabilities:
  1440. req->status = jack_set_client_capabilities (engine,
  1441. req->x.client_id);
  1442. break;
  1443. #endif /* USE_CAPABILITIES */
  1444. case GetPortConnections:
  1445. case GetPortNConnections:
  1446. if ((req->status =
  1447. jack_do_get_port_connections (engine, req, *reply_fd))
  1448. == 0) {
  1449. /* we have already replied, don't do it again */
  1450. *reply_fd = -1;
  1451. }
  1452. break;
  1453. case FreeWheel:
  1454. req->status = jack_start_freewheeling (engine);
  1455. break;
  1456. case StopFreeWheel:
  1457. req->status = jack_stop_freewheeling (engine);
  1458. break;
  1459. case SetBufferSize:
  1460. req->status = jack_set_buffer_size_request (engine,
  1461. req->x.nframes);
  1462. default:
  1463. /* some requests are handled entirely on the client
  1464. * side, by adjusting the shared memory area(s) */
  1465. break;
  1466. }
  1467. pthread_mutex_unlock (&engine->request_lock);
  1468. DEBUG ("status of request: %d", req->status);
  1469. }
  1470. static int
  1471. internal_client_request (void* ptr, jack_request_t *request)
  1472. {
  1473. do_request ((jack_engine_t*) ptr, request, 0);
  1474. return request->status;
  1475. }
  1476. static int
  1477. handle_external_client_request (jack_engine_t *engine, int fd)
  1478. {
  1479. jack_request_t req;
  1480. jack_client_internal_t *client = 0;
  1481. int reply_fd;
  1482. JSList *node;
  1483. ssize_t r;
  1484. DEBUG ("HIT: before lock");
  1485. jack_lock_graph (engine);
  1486. DEBUG ("HIT: before for");
  1487. for (node = engine->clients; node; node = jack_slist_next (node)) {
  1488. if (((jack_client_internal_t *) node->data)->request_fd == fd) {
  1489. DEBUG ("HIT: in for");
  1490. client = (jack_client_internal_t *) node->data;
  1491. break;
  1492. }
  1493. }
  1494. DEBUG ("HIT: after for");
  1495. jack_unlock_graph (engine);
  1496. if (client == NULL) {
  1497. jack_error ("client input on unknown fd %d!", fd);
  1498. return -1;
  1499. }
  1500. if ((r = read (client->request_fd, &req, sizeof (req)))
  1501. < (ssize_t) sizeof (req)) {
  1502. jack_error ("cannot read request from client (%d/%d/%s)",
  1503. r, sizeof(req), strerror (errno));
  1504. client->error++;
  1505. return -1;
  1506. }
  1507. reply_fd = client->request_fd;
  1508. do_request (engine, &req, &reply_fd);
  1509. if (reply_fd >= 0) {
  1510. DEBUG ("replying to client");
  1511. if (write (reply_fd, &req, sizeof (req))
  1512. < (ssize_t) sizeof (req)) {
  1513. jack_error ("cannot write request result to client");
  1514. return -1;
  1515. }
  1516. } else {
  1517. DEBUG ("*not* replying to client");
  1518. }
  1519. return 0;
  1520. }
  1521. static void *
  1522. jack_server_thread (void *arg)
  1523. {
  1524. jack_engine_t *engine = (jack_engine_t *) arg;
  1525. struct sockaddr_un client_addr;
  1526. socklen_t client_addrlen;
  1527. struct pollfd *pfd;
  1528. int client_socket;
  1529. int done = 0;
  1530. int i;
  1531. int max;
  1532. engine->pfd[0].fd = engine->fds[0];
  1533. engine->pfd[0].events = POLLIN|POLLERR;
  1534. engine->pfd[1].fd = engine->fds[1];
  1535. engine->pfd[1].events = POLLIN|POLLERR;
  1536. engine->pfd_max = 2;
  1537. while (!done) {
  1538. DEBUG ("start while");
  1539. /* XXX race here with new external clients
  1540. causing engine->pfd to be reallocated.
  1541. I don't know how to solve this
  1542. short of copying the entire
  1543. contents of the pfd struct. Ick.
  1544. */
  1545. max = engine->pfd_max;
  1546. pfd = engine->pfd;
  1547. if (poll (pfd, max, 10000) < 0) {
  1548. if (errno == EINTR) {
  1549. continue;
  1550. }
  1551. jack_error ("poll failed (%s)", strerror (errno));
  1552. break;
  1553. }
  1554. DEBUG("server thread back from poll");
  1555. /* Stephane Letz: letz@grame.fr : has to be added otherwise pthread_cancel does not work on MacOSX */
  1556. pthread_testcancel();
  1557. /* check each client socket before handling other request*/
  1558. for (i = 2; i < max; i++) {
  1559. if (pfd[i].fd < 0) {
  1560. continue;
  1561. }
  1562. if (pfd[i].revents & ~POLLIN) {
  1563. handle_client_socket_error (engine, pfd[i].fd);
  1564. } else if (pfd[i].revents & POLLIN) {
  1565. if (handle_external_client_request
  1566. (engine, pfd[i].fd)) {
  1567. jack_error ("could not handle external"
  1568. " client request");
  1569. #ifdef JACK_USE_MACH_THREADS
  1570. /* poll is implemented using
  1571. select (see the macosx/fakepoll
  1572. code). When the socket is closed
  1573. select does not return any error,
  1574. POLLIN is true and the next read
  1575. will return 0 bytes. This
  1576. behaviour is diffrent from the
  1577. Linux poll behaviour. Thus we use
  1578. this condition as a socket error
  1579. and remove the client.
  1580. */
  1581. handle_client_socket_error(engine, pfd[i].fd);
  1582. #endif /* JACK_USE_MACH_THREADS */
  1583. }
  1584. }
  1585. }
  1586. /* check the master server socket */
  1587. if (pfd[0].revents & POLLERR) {
  1588. jack_error ("error on server socket");
  1589. break;
  1590. }
  1591. if (engine->control->engine_ok && pfd[0].revents & POLLIN) {
  1592. DEBUG ("pfd[0].revents & POLLIN");
  1593. memset (&client_addr, 0, sizeof (client_addr));
  1594. client_addrlen = sizeof (client_addr);
  1595. if ((client_socket =
  1596. accept (engine->fds[0],
  1597. (struct sockaddr *) &client_addr,
  1598. &client_addrlen)) < 0) {
  1599. jack_error ("cannot accept new connection (%s)",
  1600. strerror (errno));
  1601. } else if (handle_new_client (engine, client_socket)
  1602. < 0) {
  1603. jack_error ("cannot complete new client "
  1604. "connection process");
  1605. close (client_socket);
  1606. }
  1607. }
  1608. /* check the ACK server socket */
  1609. if (pfd[1].revents & POLLERR) {
  1610. jack_error ("error on server ACK socket");
  1611. break;
  1612. }
  1613. if (engine->control->engine_ok && pfd[1].revents & POLLIN) {
  1614. DEBUG ("pfd[1].revents & POLLIN");
  1615. memset (&client_addr, 0, sizeof (client_addr));
  1616. client_addrlen = sizeof (client_addr);
  1617. if ((client_socket =
  1618. accept (engine->fds[1],
  1619. (struct sockaddr *) &client_addr,
  1620. &client_addrlen)) < 0) {
  1621. jack_error ("cannot accept new ACK connection"
  1622. " (%s)", strerror (errno));
  1623. } else if (handle_client_ack_connection
  1624. (engine, client_socket)) {
  1625. jack_error ("cannot complete client ACK "
  1626. "connection process");
  1627. close (client_socket);
  1628. }
  1629. }
  1630. }
  1631. return 0;
  1632. }
  1633. jack_engine_t *
  1634. jack_engine_new (int realtime, int rtpriority,
  1635. int do_mlock, int do_unlock,
  1636. int temporary,
  1637. int verbose, int client_timeout, unsigned int port_max, pid_t wait_pid,
  1638. JSList *drivers)
  1639. {
  1640. jack_engine_t *engine;
  1641. unsigned int i;
  1642. #ifdef USE_CAPABILITIES
  1643. uid_t uid = getuid ();
  1644. uid_t euid = geteuid ();
  1645. #endif /* USE_CAPABILITIES */
  1646. jack_init_time ();
  1647. engine = (jack_engine_t *) malloc (sizeof (jack_engine_t));
  1648. engine->drivers = drivers;
  1649. engine->driver = NULL;
  1650. engine->driver_desc = NULL;
  1651. engine->driver_params = NULL;
  1652. engine->set_sample_rate = jack_set_sample_rate;
  1653. engine->set_buffer_size = jack_driver_buffer_size;
  1654. engine->run_cycle = jack_run_cycle;
  1655. engine->delay = jack_engine_notify_clients_about_delay;
  1656. engine->driver_exit = jack_engine_driver_exit;
  1657. engine->transport_cycle_start = jack_transport_cycle_start;
  1658. engine->client_timeout_msecs = client_timeout;
  1659. engine->next_client_id = 1;
  1660. engine->port_max = port_max;
  1661. engine->rtpriority = rtpriority;
  1662. engine->silent_buffer = 0;
  1663. engine->verbose = verbose;
  1664. engine->temporary = temporary;
  1665. engine->freewheeling = 0;
  1666. engine->wait_pid = wait_pid;
  1667. jack_engine_reset_rolling_usecs (engine);
  1668. engine->max_usecs = 0.0f;
  1669. pthread_mutex_init (&engine->client_lock, 0);
  1670. pthread_mutex_init (&engine->port_lock, 0);
  1671. pthread_mutex_init (&engine->request_lock, 0);
  1672. engine->clients = 0;
  1673. engine->pfd_size = 16;
  1674. engine->pfd_max = 0;
  1675. engine->pfd = (struct pollfd *) malloc (sizeof (struct pollfd)
  1676. * engine->pfd_size);
  1677. engine->fifo_size = 16;
  1678. engine->fifo = (int *) malloc (sizeof (int) * engine->fifo_size);
  1679. for (i = 0; i < engine->fifo_size; i++) {
  1680. engine->fifo[i] = -1;
  1681. }
  1682. engine->external_client_cnt = 0;
  1683. srandom (time ((time_t *) 0));
  1684. if (jack_initialize_shm ()) {
  1685. return 0;
  1686. }
  1687. if (jack_shmalloc ("/jack-engine",
  1688. sizeof (jack_control_t)
  1689. + ((sizeof (jack_port_shared_t) * engine->port_max)),
  1690. &engine->control_shm)) {
  1691. jack_error ("cannot create engine control shared memory "
  1692. "segment (%s)", strerror (errno));
  1693. return 0;
  1694. }
  1695. if (jack_attach_shm (&engine->control_shm)) {
  1696. jack_error ("cannot attach to engine control shared memory (%s)",
  1697. strerror (errno));
  1698. jack_destroy_shm (&engine->control_shm);
  1699. return 0;
  1700. }
  1701. engine->control = (jack_control_t *)
  1702. jack_shm_addr (&engine->control_shm);
  1703. /* Setup port type information from builtins. buffer space is
  1704. * allocated when the driver calls jack_driver_buffer_size().
  1705. */
  1706. for (i = 0; jack_builtin_port_types[i].type_name[0]; ++i) {
  1707. memcpy (&engine->control->port_types[i],
  1708. &jack_builtin_port_types[i],
  1709. sizeof (jack_port_type_info_t));
  1710. VERBOSE (engine, "registered builtin port type %s\n",
  1711. engine->control->port_types[i].type_name);
  1712. /* the port type id is index into port_types array */
  1713. engine->control->port_types[i].ptype_id = i;
  1714. /* be sure to initialize mutex correctly */
  1715. pthread_mutex_init (&engine->port_buffers[i].lock, NULL);
  1716. /* set buffer list info correctly */
  1717. engine->port_buffers[i].freelist = NULL;
  1718. engine->port_buffers[i].info = NULL;
  1719. /* mark each port segment as not allocated */
  1720. engine->port_segment[i].index = -1;
  1721. engine->port_segment[i].attached_at = 0;
  1722. }
  1723. engine->control->n_port_types = i;
  1724. /* Mark all ports as available */
  1725. for (i = 0; i < engine->port_max; i++) {
  1726. engine->control->ports[i].in_use = 0;
  1727. engine->control->ports[i].id = i;
  1728. }
  1729. /* allocate internal port structures so that we can keep track
  1730. * of port connections.
  1731. */
  1732. engine->internal_ports = (jack_port_internal_t *)
  1733. malloc (sizeof (jack_port_internal_t) * engine->port_max);
  1734. for (i = 0; i < engine->port_max; i++) {
  1735. engine->internal_ports[i].connections = 0;
  1736. }
  1737. if (make_sockets (engine->fds) < 0) {
  1738. jack_error ("cannot create server sockets");
  1739. return 0;
  1740. }
  1741. engine->control->port_max = engine->port_max;
  1742. engine->control->real_time = realtime;
  1743. engine->control->client_priority = (realtime
  1744. ? engine->rtpriority - 1
  1745. : 0);
  1746. engine->control->do_mlock = do_mlock;
  1747. engine->control->do_munlock = do_unlock;
  1748. engine->control->cpu_load = 0;
  1749. engine->control->xrun_delayed_usecs = 0;
  1750. engine->control->buffer_size = 0;
  1751. jack_transport_init (engine);
  1752. jack_set_sample_rate (engine, 0);
  1753. engine->control->internal = 0;
  1754. engine->control->has_capabilities = 0;
  1755. #ifdef JACK_USE_MACH_THREADS
  1756. /* specific resources for server/client real-time thread
  1757. * communication */
  1758. engine->servertask = mach_task_self();
  1759. if (task_get_bootstrap_port(engine->servertask, &engine->bp)){
  1760. jack_error("Jackd: Can't find bootstrap mach port");
  1761. return 0;
  1762. }
  1763. engine->portnum = 0;
  1764. #endif /* JACK_USE_MACH_THREADS */
  1765. #ifdef USE_CAPABILITIES
  1766. if (uid == 0 || euid == 0) {
  1767. VERBOSE (engine, "running with uid=%d and euid=%d, "
  1768. "will not try to use capabilites\n",
  1769. uid, euid);
  1770. } else {
  1771. /* only try to use capabilities if not running as root */
  1772. engine->control->has_capabilities = check_capabilities (engine);
  1773. if (engine->control->has_capabilities == 0) {
  1774. VERBOSE (engine, "required capabilities not "
  1775. "available\n");
  1776. }
  1777. if (engine->verbose) {
  1778. size_t size;
  1779. cap_t cap = cap_init();
  1780. capgetp(0, cap);
  1781. VERBOSE (engine, "capabilities: %s\n",
  1782. cap_to_text(cap, &size));
  1783. }
  1784. }
  1785. #endif /* USE_CAPABILITIES */
  1786. #ifdef USE_MLOCK
  1787. if (realtime && do_mlock && (mlockall (MCL_CURRENT | MCL_FUTURE) != 0)) {
  1788. jack_error ("cannot lock down memory for jackd (%s)",
  1789. strerror (errno));
  1790. #ifdef ENSURE_MLOCK
  1791. return NULL;
  1792. #endif /* ENSURE_MLOCK */
  1793. }
  1794. #endif /* USE_MLOCK */
  1795. engine->control->engine_ok = 1;
  1796. snprintf (engine->fifo_prefix, sizeof (engine->fifo_prefix),
  1797. "%s/jack-%d-ack-fifo-%d", jack_server_dir, getuid (), getpid());
  1798. (void) jack_get_fifo_fd (engine, 0);
  1799. jack_create_thread (&engine->server_thread, 0, FALSE, &jack_server_thread,
  1800. engine);
  1801. return engine;
  1802. }
  1803. static void *
  1804. jack_watchdog_thread (void *arg)
  1805. {
  1806. jack_engine_t *engine = (jack_engine_t *) arg;
  1807. engine->watchdog_check = 0;
  1808. while (1) {
  1809. sleep (5);
  1810. if ( engine->watchdog_check == 0) {
  1811. jack_error ("jackd watchdog: timeout - killing jackd");
  1812. /* Kill the current client's process group. */
  1813. if (engine->current_client) {
  1814. kill (-engine->current_client->
  1815. control->pgrp, SIGKILL);
  1816. }
  1817. /* kill our process group, try to get a dump */
  1818. kill (-getpgrp(), SIGABRT);
  1819. /*NOTREACHED*/
  1820. exit (1);
  1821. }
  1822. engine->watchdog_check = 0;
  1823. }
  1824. }
  1825. static int
  1826. jack_start_watchdog (jack_engine_t *engine)
  1827. {
  1828. int watchdog_priority = engine->rtpriority + 10;
  1829. int max_priority = sched_get_priority_max (SCHED_FIFO);
  1830. if ((max_priority != -1) &&
  1831. (max_priority < watchdog_priority))
  1832. watchdog_priority = max_priority;
  1833. if (jack_create_thread (&engine->watchdog_thread, watchdog_priority, TRUE,
  1834. jack_watchdog_thread, engine)) {
  1835. jack_error ("cannot start watchdog thread");
  1836. return -1;
  1837. }
  1838. return 0;
  1839. }
  1840. static void
  1841. jack_engine_notify_clients_about_delay (jack_engine_t *engine,
  1842. float delayed_usecs)
  1843. {
  1844. JSList *node;
  1845. jack_event_t event;
  1846. engine->control->xrun_delayed_usecs = delayed_usecs;
  1847. event.type = XRun;
  1848. jack_lock_graph (engine);
  1849. for (node = engine->clients; node; node = jack_slist_next (node)) {
  1850. jack_deliver_event (engine,
  1851. (jack_client_internal_t *) node->data,
  1852. &event);
  1853. }
  1854. jack_unlock_graph (engine);
  1855. }
  1856. static inline void
  1857. jack_inc_frame_time (jack_engine_t *engine, jack_nframes_t amount)
  1858. {
  1859. jack_frame_timer_t *time = &engine->control->frame_timer;
  1860. // atomic_inc (&time->guard1, 1);
  1861. // really need a memory barrier here
  1862. time->guard1++;
  1863. time->frames += amount;
  1864. time->stamp = engine->driver->last_wait_ust;
  1865. // atomic_inc (&time->guard2, 1);
  1866. // might need a memory barrier here
  1867. time->guard2++;
  1868. }
  1869. static void*
  1870. jack_engine_freewheel (void *arg)
  1871. {
  1872. jack_engine_t* engine = (jack_engine_t *) arg;
  1873. VERBOSE (engine, "freewheel thread starting ...\n");
  1874. /* we should not be running SCHED_FIFO, so we don't
  1875. have to do anything about scheduling.
  1876. */
  1877. while (engine->freewheeling) {
  1878. jack_lock_graph (engine);
  1879. if (jack_engine_process (engine,
  1880. engine->control->buffer_size)) {
  1881. jack_error ("process cycle within freewheel failed");
  1882. jack_unlock_graph (engine);
  1883. break;
  1884. }
  1885. jack_unlock_graph (engine);
  1886. }
  1887. VERBOSE (engine, "freewheel came to an end, naturally\n");
  1888. return 0;
  1889. }
  1890. static int
  1891. jack_start_freewheeling (jack_engine_t* engine)
  1892. {
  1893. jack_event_t event;
  1894. if (engine->freewheeling) {
  1895. return 0;
  1896. }
  1897. if (engine->driver == NULL) {
  1898. jack_error ("cannot start freewheeling without a driver!");
  1899. return -1;
  1900. }
  1901. /* stop driver before telling anyone about it so
  1902. there are no more process() calls being handled.
  1903. */
  1904. if (engine->driver->stop (engine->driver)) {
  1905. jack_error ("could not stop driver for freewheeling");
  1906. return -1;
  1907. }
  1908. engine->freewheeling = 1;
  1909. event.type = StartFreewheel;
  1910. jack_deliver_event_to_all (engine, &event);
  1911. if (jack_create_thread (&engine->freewheel_thread, 0, FALSE,
  1912. jack_engine_freewheel, engine)) {
  1913. jack_error ("could not start create freewheel thread");
  1914. return -1;
  1915. }
  1916. return 0;
  1917. }
  1918. static int
  1919. jack_stop_freewheeling (jack_engine_t* engine)
  1920. {
  1921. jack_event_t event;
  1922. void *ftstatus;
  1923. if (!engine->freewheeling) {
  1924. return 0;
  1925. }
  1926. if (engine->driver == NULL) {
  1927. jack_error ("cannot start freewheeling without a driver!");
  1928. return -1;
  1929. }
  1930. if (!engine->freewheeling) {
  1931. VERBOSE (engine, "stop freewheel when not freewheeling\n");
  1932. return 0;
  1933. }
  1934. /* tell the freewheel thread to stop, and wait for it
  1935. to exit.
  1936. */
  1937. engine->freewheeling = 0;
  1938. VERBOSE (engine, "freewheeling stopped, waiting for thread\n");
  1939. pthread_join (engine->freewheel_thread, &ftstatus);
  1940. VERBOSE (engine, "freewheel thread has returned\n");
  1941. /* tell everyone we've stopped */
  1942. event.type = StopFreewheel;
  1943. jack_deliver_event_to_all (engine, &event);
  1944. /* restart the driver */
  1945. if (engine->driver->start (engine->driver)) {
  1946. jack_error ("could not restart driver after freewheeling");
  1947. return -1;
  1948. }
  1949. return 0;
  1950. }
  1951. static int
  1952. jack_run_one_cycle (jack_engine_t *engine, jack_nframes_t nframes,
  1953. float delayed_usecs)
  1954. {
  1955. jack_driver_t* driver = engine->driver;
  1956. int ret = -1;
  1957. static int consecutive_excessive_delays = 0;
  1958. #define WORK_SCALE 1.0f
  1959. if (engine->control->real_time &&
  1960. engine->spare_usecs &&
  1961. ((WORK_SCALE * engine->spare_usecs) <= delayed_usecs)) {
  1962. fprintf (stderr, "delay of %.3f usecs exceeds estimated spare"
  1963. " time of %.3f; restart ...\n",
  1964. delayed_usecs, WORK_SCALE * engine->spare_usecs);
  1965. if (++consecutive_excessive_delays > 10) {
  1966. jack_error ("too many consecutive interrupt delays "
  1967. "... engine pausing");
  1968. return -1; /* will exit the thread loop */
  1969. }
  1970. jack_engine_notify_clients_about_delay (engine, delayed_usecs);
  1971. return 0;
  1972. } else {
  1973. consecutive_excessive_delays = 0;
  1974. }
  1975. if (jack_try_lock_graph (engine)) {
  1976. /* engine can't run. just throw away an entire cycle */
  1977. driver->null_cycle (driver, nframes);
  1978. return 0;
  1979. }
  1980. if (!engine->freewheeling) {
  1981. DEBUG("waiting for driver read\n");
  1982. if (driver->read (driver, nframes)) {
  1983. goto unlock;
  1984. }
  1985. }
  1986. DEBUG("run process\n");
  1987. if (jack_engine_process (engine, nframes) == 0) {
  1988. if (!engine->freewheeling) {
  1989. if (driver->write (driver, nframes)) {
  1990. goto unlock;
  1991. }
  1992. }
  1993. } else {
  1994. JSList *node;
  1995. DEBUG ("engine process cycle failed");
  1996. /* we are already late, or something else went wrong,
  1997. so it can't hurt to check the existence of all
  1998. clients.
  1999. */
  2000. for (node = engine->clients; node;
  2001. node = jack_slist_next (node)) {
  2002. jack_client_internal_t *client =
  2003. (jack_client_internal_t *) node->data;
  2004. if (client->control->type == ClientExternal) {
  2005. if (kill (client->control->pid, 0)) {
  2006. VERBOSE(engine,
  2007. "client %s has died/exited\n",
  2008. client->control->name);
  2009. client->error++;
  2010. }
  2011. }
  2012. DEBUG ("client %s errors = %d", client->control->name,
  2013. client->error);
  2014. }
  2015. }
  2016. jack_engine_post_process (engine);
  2017. jack_inc_frame_time (engine, nframes);
  2018. ret = 0;
  2019. unlock:
  2020. jack_unlock_graph (engine);
  2021. DEBUG("cycle finished, status = %d", ret);
  2022. return ret;
  2023. }
  2024. static void
  2025. jack_engine_driver_exit (jack_engine_t* engine)
  2026. {
  2027. /* tell anyone waiting that the driver exited. */
  2028. kill (engine->wait_pid, SIGUSR2);
  2029. engine->driver = NULL;
  2030. }
  2031. static int
  2032. jack_run_cycle (jack_engine_t *engine, jack_nframes_t nframes,
  2033. float delayed_usecs)
  2034. {
  2035. jack_nframes_t left;
  2036. jack_nframes_t b_size = engine->control->buffer_size;
  2037. if (engine->verbose) {
  2038. if (nframes != b_size) {
  2039. VERBOSE (engine,
  2040. "late driver wakeup: nframes to process = %"
  2041. PRIu32 ".\n", nframes);
  2042. }
  2043. }
  2044. /* run as many cycles as it takes to consume nframes */
  2045. for (left = nframes; left >= b_size; left -= b_size) {
  2046. if (jack_run_one_cycle (engine, b_size, delayed_usecs)) {
  2047. jack_error ("cycle execution failure, exiting");
  2048. return EIO;
  2049. }
  2050. }
  2051. return 0;
  2052. }
  2053. void
  2054. jack_engine_delete (jack_engine_t *engine)
  2055. {
  2056. int i;
  2057. if (engine == NULL)
  2058. return;
  2059. engine->control->engine_ok = 0; /* tell clients we're going away */
  2060. /* shutdown master socket to prevent new clients arriving */
  2061. // close (engine->fds[0]);
  2062. if (engine->driver) {
  2063. jack_driver_t* driver = engine->driver;
  2064. VERBOSE (engine, "stopping driver\n");
  2065. driver->stop (driver);
  2066. // VERBOSE (engine, "detaching driver\n");
  2067. // driver->detach (driver, engine);
  2068. VERBOSE (engine, "unloading driver\n");
  2069. jack_driver_unload (driver);
  2070. engine->driver = NULL;
  2071. }
  2072. VERBOSE (engine, "freeing shared port segments\n");
  2073. for (i = 0; i < engine->control->n_port_types; ++i) {
  2074. jack_release_shm (&engine->port_segment[i]);
  2075. jack_destroy_shm (&engine->port_segment[i]);
  2076. }
  2077. /* stop the other engine threads */
  2078. VERBOSE (engine, "stopping server thread\n");
  2079. #if JACK_USE_MACH_THREADS
  2080. // MacOSX pthread_cancel still not implemented correctly in Darwin
  2081. mach_port_t machThread = pthread_mach_thread_np (engine->server_thread);
  2082. thread_terminate (machThread);
  2083. #else
  2084. pthread_cancel (engine->server_thread);
  2085. pthread_join (engine->server_thread, NULL);
  2086. #endif
  2087. #ifndef JACK_USE_MACH_THREADS
  2088. /* Cancel the watchdog thread and wait for it to terminate.
  2089. *
  2090. * The watchdog thread is not used on MacOSX since CoreAudio
  2091. * drivers already contain a similar mechanism.
  2092. */
  2093. if (engine->control->real_time) {
  2094. VERBOSE (engine, "stopping watchdog thread\n");
  2095. pthread_cancel (engine->watchdog_thread);
  2096. pthread_join (engine->watchdog_thread, NULL);
  2097. }
  2098. #endif
  2099. VERBOSE (engine, "last xrun delay: %.3f usecs\n",
  2100. engine->control->xrun_delayed_usecs);
  2101. /* free engine control shm segment */
  2102. engine->control = NULL;
  2103. VERBOSE (engine, "freeing engine shared memory\n");
  2104. jack_release_shm (&engine->control_shm);
  2105. jack_destroy_shm (&engine->control_shm);
  2106. VERBOSE (engine, "max usecs: %.3f, ", engine->max_usecs);
  2107. VERBOSE (engine, "engine deleted\n");
  2108. free (engine);
  2109. }
  2110. /* Set up the engine's client internal and control structures for both
  2111. * internal and external clients. */
  2112. static jack_client_internal_t *
  2113. jack_setup_client_control (jack_engine_t *engine, int fd,
  2114. jack_client_connect_request_t *req)
  2115. {
  2116. jack_client_internal_t *client;
  2117. client = (jack_client_internal_t *)
  2118. malloc (sizeof (jack_client_internal_t));
  2119. client->request_fd = fd;
  2120. client->event_fd = -1;
  2121. client->ports = 0;
  2122. client->fed_by = 0;
  2123. client->execution_order = UINT_MAX;
  2124. client->next_client = NULL;
  2125. client->handle = NULL;
  2126. client->finish = NULL;
  2127. client->error = 0;
  2128. if (req->type != ClientExternal) {
  2129. client->control = (jack_client_control_t *)
  2130. malloc (sizeof (jack_client_control_t));
  2131. } else {
  2132. char shm_name[PATH_MAX+1];
  2133. snprintf (shm_name, sizeof (shm_name), "/jack-c-%s", req->name);
  2134. if (jack_shmalloc (shm_name,
  2135. sizeof (jack_client_control_t),
  2136. &client->control_shm)) {
  2137. jack_error ("cannot create client control block for %s",
  2138. req->name);
  2139. free (client);
  2140. return 0;
  2141. }
  2142. if (jack_attach_shm (&client->control_shm)) {
  2143. jack_error ("cannot attach to client control block "
  2144. "for %s (%s)", req->name, strerror (errno));
  2145. jack_destroy_shm (&client->control_shm);
  2146. free (client);
  2147. return 0;
  2148. }
  2149. client->control = (jack_client_control_t *)
  2150. jack_shm_addr (&client->control_shm);
  2151. }
  2152. client->control->type = req->type;
  2153. client->control->active = 0;
  2154. client->control->dead = FALSE;
  2155. client->control->timed_out = 0;
  2156. client->control->id = engine->next_client_id++;
  2157. strcpy ((char *) client->control->name, req->name);
  2158. client->subgraph_start_fd = -1;
  2159. client->subgraph_wait_fd = -1;
  2160. client->control->process = NULL;
  2161. client->control->process_arg = NULL;
  2162. client->control->bufsize = NULL;
  2163. client->control->bufsize_arg = NULL;
  2164. client->control->srate = NULL;
  2165. client->control->srate_arg = NULL;
  2166. client->control->xrun = NULL;
  2167. client->control->xrun_arg = NULL;
  2168. client->control->port_register = NULL;
  2169. client->control->port_register_arg = NULL;
  2170. client->control->graph_order = NULL;
  2171. client->control->graph_order_arg = NULL;
  2172. jack_transport_client_new (client);
  2173. #ifdef JACK_USE_MACH_THREADS
  2174. /* specific resources for server/client real-time thread
  2175. * communication */
  2176. allocate_mach_serverport(engine, client);
  2177. client->running = FALSE;
  2178. #endif
  2179. if (req->type == ClientInternal) {
  2180. if (jack_load_client (engine, client, req->object_path)) {
  2181. jack_error ("cannot dynamically load client from"
  2182. " \"%s\"", req->object_path);
  2183. jack_client_delete (engine, client);
  2184. return NULL;
  2185. }
  2186. }
  2187. return client;
  2188. }
  2189. static void
  2190. jack_port_clear_connections (jack_engine_t *engine,
  2191. jack_port_internal_t *port)
  2192. {
  2193. JSList *node, *next;
  2194. for (node = port->connections; node; ) {
  2195. next = jack_slist_next (node);
  2196. jack_port_disconnect_internal (
  2197. engine, ((jack_connection_internal_t *)
  2198. node->data)->source,
  2199. ((jack_connection_internal_t *)
  2200. node->data)->destination,
  2201. FALSE);
  2202. node = next;
  2203. }
  2204. jack_slist_free (port->connections);
  2205. port->connections = 0;
  2206. }
  2207. static void
  2208. jack_zombify_client (jack_engine_t *engine, jack_client_internal_t *client)
  2209. {
  2210. VERBOSE (engine, "removing client \"%s\" from the processing chain\n",
  2211. client->control->name);
  2212. /* caller must hold the client_lock */
  2213. /* this stops jack_deliver_event() from doing anything */
  2214. client->control->dead = TRUE;
  2215. jack_client_disconnect (engine, client);
  2216. jack_client_do_deactivate (engine, client, FALSE);
  2217. }
  2218. static void
  2219. jack_remove_client (jack_engine_t *engine, jack_client_internal_t *client)
  2220. {
  2221. unsigned int i;
  2222. JSList *node;
  2223. /* caller must hold the client_lock */
  2224. VERBOSE (engine, "removing client \"%s\"\n", client->control->name);
  2225. /* if its not already a zombie, make it so */
  2226. if (!client->control->dead) {
  2227. jack_zombify_client (engine, client);
  2228. }
  2229. if (client->control->type == ClientExternal) {
  2230. /* try to force the server thread to return from poll */
  2231. close (client->event_fd);
  2232. close (client->request_fd);
  2233. /* rearrange the pollfd array so that things work right the
  2234. next time we go into poll(2).
  2235. */
  2236. for (i = 0; i < engine->pfd_max; i++) {
  2237. if (engine->pfd[i].fd == client->request_fd) {
  2238. if (i+1 < engine->pfd_max) {
  2239. memmove (&engine->pfd[i],
  2240. &engine->pfd[i+1],
  2241. sizeof (struct pollfd)
  2242. * (engine->pfd_max - i));
  2243. }
  2244. engine->pfd_max--;
  2245. }
  2246. }
  2247. }
  2248. for (node = engine->clients; node; node = jack_slist_next (node)) {
  2249. if (((jack_client_internal_t *) node->data)->control->id
  2250. == client->control->id) {
  2251. engine->clients =
  2252. jack_slist_remove_link (engine->clients, node);
  2253. jack_slist_free_1 (node);
  2254. break;
  2255. }
  2256. }
  2257. jack_client_delete (engine, client);
  2258. /* ignore the driver, which counts as a client. */
  2259. if (engine->temporary && (jack_slist_length(engine->clients) <= 1)) {
  2260. exit(0);
  2261. }
  2262. }
  2263. static void
  2264. jack_client_delete (jack_engine_t *engine, jack_client_internal_t *client)
  2265. {
  2266. if (jack_client_is_internal (client)) {
  2267. jack_client_unload (client);
  2268. free (client->control->private_client);
  2269. free ((void *) client->control);
  2270. } else {
  2271. /* release the client segment, mark it for
  2272. destruction, and free up the shm registry
  2273. information so that it can be reused.
  2274. */
  2275. jack_release_shm (&client->control_shm);
  2276. jack_destroy_shm (&client->control_shm);
  2277. }
  2278. free (client);
  2279. }
  2280. jack_client_internal_t *
  2281. jack_client_by_name (jack_engine_t *engine, const char *name)
  2282. {
  2283. jack_client_internal_t *client = NULL;
  2284. JSList *node;
  2285. jack_lock_graph (engine);
  2286. for (node = engine->clients; node; node = jack_slist_next (node)) {
  2287. if (strcmp ((const char *) ((jack_client_internal_t *)
  2288. node->data)->control->name,
  2289. name) == 0) {
  2290. client = (jack_client_internal_t *) node->data;
  2291. break;
  2292. }
  2293. }
  2294. jack_unlock_graph (engine);
  2295. return client;
  2296. }
  2297. jack_client_internal_t *
  2298. jack_client_internal_by_id (jack_engine_t *engine, jack_client_id_t id)
  2299. {
  2300. jack_client_internal_t *client = NULL;
  2301. JSList *node;
  2302. /* call tree ***MUST HOLD*** the graph lock */
  2303. for (node = engine->clients; node; node = jack_slist_next (node)) {
  2304. if (((jack_client_internal_t *) node->data)->control->id
  2305. == id) {
  2306. client = (jack_client_internal_t *) node->data;
  2307. break;
  2308. }
  2309. }
  2310. return client;
  2311. }
  2312. static void
  2313. jack_deliver_event_to_all (jack_engine_t *engine, jack_event_t *event)
  2314. {
  2315. JSList *node;
  2316. jack_lock_graph (engine);
  2317. for (node = engine->clients; node; node = jack_slist_next (node)) {
  2318. jack_deliver_event (engine,
  2319. (jack_client_internal_t *) node->data,
  2320. event);
  2321. }
  2322. jack_unlock_graph (engine);
  2323. }
  2324. static int
  2325. jack_deliver_event (jack_engine_t *engine, jack_client_internal_t *client,
  2326. jack_event_t *event)
  2327. {
  2328. char status;
  2329. /* caller must hold the graph lock */
  2330. DEBUG ("delivering event (type %d)", event->type);
  2331. /* we are not RT-constrained here, so use kill(2) to beef up
  2332. our check on a client's continued well-being
  2333. */
  2334. if (client->control->dead
  2335. || (client->control->type == ClientExternal
  2336. && kill (client->control->pid, 0))) {
  2337. DEBUG ("client %s is dead - no event sent",
  2338. client->control->name);
  2339. return 0;
  2340. }
  2341. DEBUG ("client %s is still alive", client->control->name);
  2342. if (jack_client_is_internal (client)) {
  2343. switch (event->type) {
  2344. case PortConnected:
  2345. case PortDisconnected:
  2346. jack_client_handle_port_connection
  2347. (client->control->private_client, event);
  2348. break;
  2349. case BufferSizeChange:
  2350. jack_client_invalidate_port_buffers
  2351. (client->control->private_client);
  2352. if (client->control->bufsize) {
  2353. client->control->bufsize
  2354. (event->x.n,
  2355. client->control->bufsize_arg);
  2356. }
  2357. break;
  2358. case SampleRateChange:
  2359. if (client->control->srate) {
  2360. client->control->srate
  2361. (event->x.n,
  2362. client->control->srate_arg);
  2363. }
  2364. break;
  2365. case GraphReordered:
  2366. if (client->control->graph_order) {
  2367. client->control->graph_order
  2368. (client->control->graph_order_arg);
  2369. }
  2370. break;
  2371. case XRun:
  2372. if (client->control->xrun) {
  2373. client->control->xrun
  2374. (client->control->xrun_arg);
  2375. }
  2376. break;
  2377. default:
  2378. /* internal clients don't need to know */
  2379. break;
  2380. }
  2381. } else {
  2382. if (client->control->active) {
  2383. /* there's a thread waiting for events, so
  2384. * it's worth telling the client */
  2385. DEBUG ("engine writing on event fd");
  2386. if (write (client->event_fd, event, sizeof (*event))
  2387. != sizeof (*event)) {
  2388. jack_error ("cannot send event to client [%s]"
  2389. " (%s)", client->control->name,
  2390. strerror (errno));
  2391. client->error++;
  2392. }
  2393. DEBUG ("engine reading from event fd");
  2394. if (!client->error &&
  2395. (read (client->event_fd, &status, sizeof (status))
  2396. != sizeof (status))) {
  2397. jack_error ("cannot read event response from "
  2398. "client [%s] (%s)",
  2399. client->control->name,
  2400. strerror (errno));
  2401. client->error++;
  2402. }
  2403. if (status != 0) {
  2404. jack_error ("bad status for client event "
  2405. "handling (type = %d)",
  2406. event->type);
  2407. client->error++;
  2408. }
  2409. }
  2410. }
  2411. DEBUG ("event delivered");
  2412. return 0;
  2413. }
  2414. int
  2415. jack_rechain_graph (jack_engine_t *engine)
  2416. {
  2417. JSList *node, *next;
  2418. unsigned long n;
  2419. int err = 0;
  2420. jack_client_internal_t *client, *subgraph_client, *next_client;
  2421. jack_event_t event;
  2422. int upstream_is_jackd;
  2423. jack_clear_fifos (engine);
  2424. subgraph_client = 0;
  2425. VERBOSE(engine, "++ jack_rechain_graph():\n");
  2426. event.type = GraphReordered;
  2427. for (n = 0, node = engine->clients, next = NULL; node; node = next) {
  2428. next = jack_slist_next (node);
  2429. if (((jack_client_internal_t *) node->data)->control->active) {
  2430. client = (jack_client_internal_t *) node->data;
  2431. /* find the next active client. its ok for
  2432. * this to be NULL */
  2433. while (next) {
  2434. if (((jack_client_internal_t *)
  2435. next->data)->control->active) {
  2436. break;
  2437. }
  2438. next = jack_slist_next (next);
  2439. };
  2440. if (next == NULL) {
  2441. next_client = NULL;
  2442. } else {
  2443. next_client = (jack_client_internal_t *)
  2444. next->data;
  2445. }
  2446. client->execution_order = n;
  2447. client->next_client = next_client;
  2448. if (jack_client_is_internal (client)) {
  2449. /* break the chain for the current
  2450. * subgraph. the server will wait for
  2451. * chain on the nth FIFO, and will
  2452. * then execute this internal
  2453. * client. */
  2454. if (subgraph_client) {
  2455. subgraph_client->subgraph_wait_fd =
  2456. jack_get_fifo_fd (engine, n);
  2457. VERBOSE (engine, "client %s: wait_fd="
  2458. "%d, execution_order="
  2459. "%lu.\n",
  2460. subgraph_client->
  2461. control->name,
  2462. subgraph_client->
  2463. subgraph_wait_fd, n);
  2464. n++;
  2465. }
  2466. VERBOSE (engine, "client %s: internal "
  2467. "client, execution_order="
  2468. "%lu.\n",
  2469. client->control->name, n);
  2470. /* this does the right thing for
  2471. * internal clients too
  2472. */
  2473. jack_deliver_event (engine, client, &event);
  2474. subgraph_client = 0;
  2475. } else {
  2476. if (subgraph_client == NULL) {
  2477. /* start a new subgraph. the
  2478. * engine will start the chain
  2479. * by writing to the nth
  2480. * FIFO.
  2481. */
  2482. subgraph_client = client;
  2483. subgraph_client->subgraph_start_fd =
  2484. jack_get_fifo_fd (engine, n);
  2485. VERBOSE (engine, "client %s: "
  2486. "start_fd=%d, execution"
  2487. "_order=%lu.\n",
  2488. subgraph_client->
  2489. control->name,
  2490. subgraph_client->
  2491. subgraph_start_fd, n);
  2492. /* this external client after
  2493. this will have jackd as its
  2494. upstream connection.
  2495. */
  2496. upstream_is_jackd = 1;
  2497. }
  2498. else {
  2499. VERBOSE (engine, "client %s: in"
  2500. " subgraph after %s, "
  2501. "execution_order="
  2502. "%lu.\n",
  2503. client->control->name,
  2504. subgraph_client->
  2505. control->name, n);
  2506. subgraph_client->subgraph_wait_fd = -1;
  2507. /* this external client after
  2508. this will have another
  2509. client as its upstream
  2510. connection.
  2511. */
  2512. upstream_is_jackd = 0;
  2513. }
  2514. /* make sure fifo for 'n + 1' exists
  2515. * before issuing client reorder
  2516. */
  2517. (void) jack_get_fifo_fd(
  2518. engine, client->execution_order + 1);
  2519. event.x.n = client->execution_order;
  2520. event.y.n = upstream_is_jackd;
  2521. jack_deliver_event (engine, client, &event);
  2522. n++;
  2523. }
  2524. }
  2525. }
  2526. if (subgraph_client) {
  2527. subgraph_client->subgraph_wait_fd =
  2528. jack_get_fifo_fd (engine, n);
  2529. VERBOSE (engine, "client %s: wait_fd=%d, "
  2530. "execution_order=%lu (last client).\n",
  2531. subgraph_client->control->name,
  2532. subgraph_client->subgraph_wait_fd, n);
  2533. }
  2534. VERBOSE (engine, "-- jack_rechain_graph()\n");
  2535. return err;
  2536. }
  2537. static void
  2538. jack_trace_terminal (jack_client_internal_t *c1, jack_client_internal_t *rbase)
  2539. {
  2540. jack_client_internal_t *c2;
  2541. /* make a copy of the existing list of routes that feed
  2542. c1. this provides us with an atomic snapshot of c1's
  2543. "fed-by" state, which will be modified as we progress ...
  2544. */
  2545. JSList *existing;
  2546. JSList *node;
  2547. if (c1->fed_by == NULL) {
  2548. return;
  2549. }
  2550. existing = jack_slist_copy (c1->fed_by);
  2551. /* for each route that feeds c1, recurse, marking it as
  2552. feeding rbase as well.
  2553. */
  2554. for (node = existing; node; node = jack_slist_next (node)) {
  2555. c2 = (jack_client_internal_t *) node->data;
  2556. /* c2 is a route that feeds c1 which somehow feeds
  2557. base. mark base as being fed by c2, but don't do it
  2558. more than once.
  2559. */
  2560. if (c2 != rbase && c2 != c1) {
  2561. if (jack_slist_find (rbase->fed_by, c2) == NULL) {
  2562. rbase->fed_by =
  2563. jack_slist_prepend (rbase->fed_by, c2);
  2564. }
  2565. /* FIXME: if c2->fed_by is not up-to-date, we
  2566. may end up recursing infinitely
  2567. (kaiv)
  2568. */
  2569. if (jack_slist_find (c2->fed_by, c1) == NULL) {
  2570. /* now recurse, so that we can mark
  2571. base as being fed by all routes
  2572. that feed c2
  2573. */
  2574. jack_trace_terminal (c2, rbase);
  2575. }
  2576. }
  2577. }
  2578. jack_slist_free (existing);
  2579. }
  2580. static int
  2581. jack_client_sort (jack_client_internal_t *a, jack_client_internal_t *b)
  2582. {
  2583. if (jack_slist_find (a->fed_by, b)) {
  2584. if (jack_slist_find (b->fed_by, a)) {
  2585. /* feedback loop: if `a' is the driver
  2586. client, let that execute first.
  2587. */
  2588. if (a->control->type == ClientDriver) {
  2589. /* b comes after a */
  2590. return -1;
  2591. }
  2592. }
  2593. /* a comes after b */
  2594. return 1;
  2595. } else if (jack_slist_find (b->fed_by, a)) {
  2596. if (jack_slist_find (a->fed_by, b)) {
  2597. /* feedback loop: if `b' is the driver
  2598. client, let that execute first.
  2599. */
  2600. if (b->control->type == ClientDriver) {
  2601. /* b comes before a */
  2602. return 1;
  2603. }
  2604. }
  2605. /* b comes after a */
  2606. return -1;
  2607. } else {
  2608. /* we don't care */
  2609. return 0;
  2610. }
  2611. }
  2612. static int
  2613. jack_client_feeds (jack_client_internal_t *might,
  2614. jack_client_internal_t *target)
  2615. {
  2616. JSList *pnode, *cnode;
  2617. /* Check every port of `might' for an outbound connection to
  2618. * `target' */
  2619. for (pnode = might->ports; pnode; pnode = jack_slist_next (pnode)) {
  2620. jack_port_internal_t *port;
  2621. port = (jack_port_internal_t *) pnode->data;
  2622. for (cnode = port->connections; cnode;
  2623. cnode = jack_slist_next (cnode)) {
  2624. jack_connection_internal_t *c;
  2625. c = (jack_connection_internal_t *) cnode->data;
  2626. if (c->source->shared->client_id
  2627. == might->control->id &&
  2628. c->destination->shared->client_id
  2629. == target->control->id) {
  2630. return 1;
  2631. }
  2632. }
  2633. }
  2634. return 0;
  2635. }
  2636. static jack_nframes_t
  2637. jack_get_port_total_latency (jack_engine_t *engine,
  2638. jack_port_internal_t *port, int hop_count,
  2639. int toward_port)
  2640. {
  2641. JSList *node;
  2642. jack_nframes_t latency;
  2643. jack_nframes_t max_latency = 0;
  2644. /* call tree must hold engine->client_lock. */
  2645. latency = port->shared->latency;
  2646. /* we don't prevent cyclic graphs, so we have to do something
  2647. to bottom out in the event that they are created.
  2648. */
  2649. if (hop_count > 8) {
  2650. return latency;
  2651. }
  2652. for (node = port->connections; node; node = jack_slist_next (node)) {
  2653. jack_nframes_t this_latency;
  2654. jack_connection_internal_t *connection;
  2655. connection = (jack_connection_internal_t *) node->data;
  2656. if ((toward_port &&
  2657. (connection->source->shared == port->shared)) ||
  2658. (!toward_port &&
  2659. (connection->destination->shared == port->shared))) {
  2660. continue;
  2661. }
  2662. /* if we're a destination in the connection, recurse
  2663. on the source to get its total latency
  2664. */
  2665. if (connection->destination == port) {
  2666. if (connection->source->shared->flags
  2667. & JackPortIsTerminal) {
  2668. this_latency = connection->source->
  2669. shared->latency;
  2670. } else {
  2671. this_latency =
  2672. jack_get_port_total_latency (
  2673. engine, connection->source,
  2674. hop_count + 1,
  2675. toward_port);
  2676. }
  2677. } else {
  2678. /* "port" is the source, so get the latency of
  2679. * the destination */
  2680. if (connection->destination->shared->flags
  2681. & JackPortIsTerminal) {
  2682. this_latency = connection->destination->
  2683. shared->latency;
  2684. } else {
  2685. this_latency =
  2686. jack_get_port_total_latency (
  2687. engine,
  2688. connection->destination,
  2689. hop_count + 1,
  2690. toward_port);
  2691. }
  2692. }
  2693. if (this_latency > max_latency) {
  2694. max_latency = this_latency;
  2695. }
  2696. }
  2697. return latency + max_latency;
  2698. }
  2699. static void
  2700. jack_compute_all_port_total_latencies (jack_engine_t *engine)
  2701. {
  2702. jack_port_shared_t *shared = engine->control->ports;
  2703. unsigned int i;
  2704. int toward_port;
  2705. for (i = 0; i < engine->control->port_max; i++) {
  2706. if (shared[i].in_use) {
  2707. if (shared[i].flags & JackPortIsOutput) {
  2708. toward_port = FALSE;
  2709. } else {
  2710. toward_port = TRUE;
  2711. }
  2712. shared[i].total_latency =
  2713. jack_get_port_total_latency (
  2714. engine, &engine->internal_ports[i],
  2715. 0, toward_port);
  2716. }
  2717. }
  2718. }
  2719. /**
  2720. * Sorts the network of clients using the following
  2721. * algorithm:
  2722. *
  2723. * 1) figure out who is connected to whom:
  2724. *
  2725. * foreach client1
  2726. * foreach input port
  2727. * foreach client2
  2728. * foreach output port
  2729. * if client1->input port connected to client2->output port
  2730. * mark client1 fed by client 2
  2731. *
  2732. * 2) trace the connections as terminal arcs in the graph so that
  2733. * if client A feeds client B who feeds client C, mark client C
  2734. * as fed by client A as well as client B, and so forth.
  2735. *
  2736. * 3) now sort according to whether or not client1->fed_by (client2) is true.
  2737. * if the condition is true, client2 must execute before client1
  2738. *
  2739. */
  2740. static void
  2741. jack_sort_graph (jack_engine_t *engine)
  2742. {
  2743. JSList *node, *onode;
  2744. jack_client_internal_t *client;
  2745. jack_client_internal_t *oclient;
  2746. /* called, obviously, must hold engine->client_lock */
  2747. for (node = engine->clients; node; node = jack_slist_next (node)) {
  2748. client = (jack_client_internal_t *) node->data;
  2749. jack_slist_free (client->fed_by);
  2750. client->fed_by = 0;
  2751. for (onode = engine->clients; onode;
  2752. onode = jack_slist_next (onode)) {
  2753. oclient = (jack_client_internal_t *) onode->data;
  2754. if (jack_client_feeds (oclient, client)) {
  2755. client->fed_by =
  2756. jack_slist_prepend (client->fed_by,
  2757. oclient);
  2758. }
  2759. }
  2760. }
  2761. for (node = engine->clients; node; node = jack_slist_next (node)) {
  2762. jack_trace_terminal ((jack_client_internal_t *) node->data,
  2763. (jack_client_internal_t *) node->data);
  2764. }
  2765. engine->clients = jack_slist_sort (engine->clients,
  2766. (JCompareFunc) jack_client_sort);
  2767. jack_compute_all_port_total_latencies (engine);
  2768. jack_rechain_graph (engine);
  2769. }
  2770. /**
  2771. * Dumps current engine configuration to stderr.
  2772. */
  2773. void jack_dump_configuration(jack_engine_t *engine, int take_lock)
  2774. {
  2775. JSList *clientnode, *portnode, *connectionnode;
  2776. jack_client_internal_t *client;
  2777. jack_client_control_t *ctl;
  2778. jack_port_internal_t *port;
  2779. jack_connection_internal_t* connection;
  2780. int n, m, o;
  2781. fprintf(stderr, "engine.c: <-- dump begins -->\n");
  2782. if (take_lock) {
  2783. jack_lock_graph (engine);
  2784. }
  2785. for (n = 0, clientnode = engine->clients; clientnode;
  2786. clientnode = jack_slist_next (clientnode)) {
  2787. client = (jack_client_internal_t *) clientnode->data;
  2788. ctl = client->control;
  2789. fprintf (stderr, "client #%d: %s (type: %d, process? %s, fed"
  2790. " by %d clients) start=%d wait=%d\n",
  2791. ++n,
  2792. ctl->name,
  2793. ctl->type,
  2794. ctl->process ? "yes" : "no",
  2795. jack_slist_length(client->fed_by),
  2796. client->subgraph_start_fd,
  2797. client->subgraph_wait_fd);
  2798. for(m = 0, portnode = client->ports; portnode;
  2799. portnode = jack_slist_next (portnode)) {
  2800. port = (jack_port_internal_t *) portnode->data;
  2801. fprintf(stderr, "\t port #%d: %s\n", ++m,
  2802. port->shared->name);
  2803. for(o = 0, connectionnode = port->connections;
  2804. connectionnode;
  2805. connectionnode =
  2806. jack_slist_next (connectionnode)) {
  2807. connection = (jack_connection_internal_t *)
  2808. connectionnode->data;
  2809. fprintf(stderr, "\t\t connection #%d: %s %s\n",
  2810. ++o,
  2811. (port->shared->flags
  2812. & JackPortIsInput)? "<-": "->",
  2813. (port->shared->flags & JackPortIsInput)?
  2814. connection->source->shared->name:
  2815. connection->destination->shared->name);
  2816. }
  2817. }
  2818. }
  2819. if (take_lock) {
  2820. jack_unlock_graph (engine);
  2821. }
  2822. fprintf(stderr, "engine.c: <-- dump ends -->\n");
  2823. }
  2824. static int
  2825. jack_port_do_connect (jack_engine_t *engine,
  2826. const char *source_port,
  2827. const char *destination_port)
  2828. {
  2829. jack_connection_internal_t *connection;
  2830. jack_port_internal_t *srcport, *dstport;
  2831. jack_port_id_t src_id, dst_id;
  2832. jack_client_internal_t *client;
  2833. JSList *it;
  2834. if ((srcport = jack_get_port_by_name (engine, source_port)) == NULL) {
  2835. jack_error ("unknown source port in attempted connection [%s]",
  2836. source_port);
  2837. return -1;
  2838. }
  2839. if ((dstport = jack_get_port_by_name (engine, destination_port))
  2840. == NULL) {
  2841. jack_error ("unknown destination port in attempted connection"
  2842. " [%s]", destination_port);
  2843. return -1;
  2844. }
  2845. if ((dstport->shared->flags & JackPortIsInput) == 0) {
  2846. jack_error ("destination port in attempted connection of"
  2847. " %s and %s is not an input port",
  2848. source_port, destination_port);
  2849. return -1;
  2850. }
  2851. if ((srcport->shared->flags & JackPortIsOutput) == 0) {
  2852. jack_error ("source port in attempted connection of %s and"
  2853. " %s is not an output port",
  2854. source_port, destination_port);
  2855. return -1;
  2856. }
  2857. if (srcport->shared->locked) {
  2858. jack_error ("source port %s is locked against connection"
  2859. " changes", source_port);
  2860. return -1;
  2861. }
  2862. if (dstport->shared->locked) {
  2863. jack_error ("destination port %s is locked against connection"
  2864. " changes", destination_port);
  2865. return -1;
  2866. }
  2867. if (srcport->shared->ptype_id != dstport->shared->ptype_id) {
  2868. jack_error ("ports used in attemped connection are not of "
  2869. "the same data type");
  2870. return -1;
  2871. }
  2872. if ((client = jack_client_internal_by_id (engine,
  2873. srcport->shared->client_id))
  2874. == 0) {
  2875. jack_error ("unknown client set as owner of port - "
  2876. "cannot connect");
  2877. return -1;
  2878. }
  2879. if (!client->control->active) {
  2880. jack_error ("cannot connect ports owned by inactive clients;"
  2881. " \"%s\" is not active", client->control->name);
  2882. return -1;
  2883. }
  2884. if ((client = jack_client_internal_by_id (engine,
  2885. dstport->shared->client_id))
  2886. == 0) {
  2887. jack_error ("unknown client set as owner of port - cannot "
  2888. "connect");
  2889. return -1;
  2890. }
  2891. if (!client->control->active) {
  2892. jack_error ("cannot connect ports owned by inactive clients;"
  2893. " \"%s\" is not active", client->control->name);
  2894. return -1;
  2895. }
  2896. for (it = srcport->connections; it; it = it->next) {
  2897. if (((jack_connection_internal_t *)it->data)->destination
  2898. == dstport) {
  2899. return EEXIST;
  2900. }
  2901. }
  2902. connection = (jack_connection_internal_t *)
  2903. malloc (sizeof (jack_connection_internal_t));
  2904. connection->source = srcport;
  2905. connection->destination = dstport;
  2906. src_id = srcport->shared->id;
  2907. dst_id = dstport->shared->id;
  2908. jack_lock_graph (engine);
  2909. if (dstport->connections && !dstport->shared->has_mixdown) {
  2910. jack_port_type_info_t *port_type =
  2911. jack_port_type_info (engine, dstport);
  2912. jack_error ("cannot make multiple connections to a port of"
  2913. " type [%s]", port_type->type_name);
  2914. free (connection);
  2915. jack_unlock_graph (engine);
  2916. return -1;
  2917. } else {
  2918. VERBOSE (engine, "connect %s and %s\n",
  2919. srcport->shared->name,
  2920. dstport->shared->name);
  2921. dstport->connections =
  2922. jack_slist_prepend (dstport->connections, connection);
  2923. srcport->connections =
  2924. jack_slist_prepend (srcport->connections, connection);
  2925. jack_sort_graph (engine);
  2926. DEBUG ("actually sorted the graph...");
  2927. jack_send_connection_notification (engine,
  2928. srcport->shared->client_id,
  2929. src_id, dst_id, TRUE);
  2930. jack_send_connection_notification (engine,
  2931. dstport->shared->client_id,
  2932. dst_id, src_id, TRUE);
  2933. }
  2934. jack_unlock_graph (engine);
  2935. return 0;
  2936. }
  2937. int
  2938. jack_port_disconnect_internal (jack_engine_t *engine,
  2939. jack_port_internal_t *srcport,
  2940. jack_port_internal_t *dstport,
  2941. int sort_graph)
  2942. {
  2943. JSList *node;
  2944. jack_connection_internal_t *connect;
  2945. int ret = -1;
  2946. jack_port_id_t src_id, dst_id;
  2947. /* call tree **** MUST HOLD **** engine->client_lock. */
  2948. for (node = srcport->connections; node;
  2949. node = jack_slist_next (node)) {
  2950. connect = (jack_connection_internal_t *) node->data;
  2951. if (connect->source == srcport &&
  2952. connect->destination == dstport) {
  2953. VERBOSE (engine, "DIS-connect %s and %s\n",
  2954. srcport->shared->name,
  2955. dstport->shared->name);
  2956. srcport->connections =
  2957. jack_slist_remove (srcport->connections,
  2958. connect);
  2959. dstport->connections =
  2960. jack_slist_remove (dstport->connections,
  2961. connect);
  2962. src_id = srcport->shared->id;
  2963. dst_id = dstport->shared->id;
  2964. /* this is a bit harsh, but it basically says
  2965. that if we actually do a disconnect, and
  2966. its the last one, then make sure that any
  2967. input monitoring is turned off on the
  2968. srcport. this isn't ideal for all
  2969. situations, but it works better for most of
  2970. them.
  2971. */
  2972. if (srcport->connections == NULL) {
  2973. srcport->shared->monitor_requests = 0;
  2974. }
  2975. jack_send_connection_notification (
  2976. engine, srcport->shared->client_id, src_id,
  2977. dst_id, FALSE);
  2978. jack_send_connection_notification (
  2979. engine, dstport->shared->client_id, dst_id,
  2980. src_id, FALSE);
  2981. free (connect);
  2982. ret = 0;
  2983. break;
  2984. }
  2985. }
  2986. if (sort_graph) {
  2987. jack_sort_graph (engine);
  2988. }
  2989. return ret;
  2990. }
  2991. static int
  2992. jack_port_do_disconnect_all (jack_engine_t *engine,
  2993. jack_port_id_t port_id)
  2994. {
  2995. if (port_id >= engine->control->port_max) {
  2996. jack_error ("illegal port ID in attempted disconnection [%"
  2997. PRIu32 "]", port_id);
  2998. return -1;
  2999. }
  3000. VERBOSE (engine, "clear connections for %s\n",
  3001. engine->internal_ports[port_id].shared->name);
  3002. jack_lock_graph (engine);
  3003. jack_port_clear_connections (engine, &engine->internal_ports[port_id]);
  3004. jack_sort_graph (engine);
  3005. jack_unlock_graph (engine);
  3006. return 0;
  3007. }
  3008. static int
  3009. jack_port_do_disconnect (jack_engine_t *engine,
  3010. const char *source_port,
  3011. const char *destination_port)
  3012. {
  3013. jack_port_internal_t *srcport, *dstport;
  3014. int ret = -1;
  3015. if ((srcport = jack_get_port_by_name (engine, source_port)) == NULL) {
  3016. jack_error ("unknown source port in attempted disconnection"
  3017. " [%s]", source_port);
  3018. return -1;
  3019. }
  3020. if ((dstport = jack_get_port_by_name (engine, destination_port))
  3021. == NULL) {
  3022. jack_error ("unknown destination port in attempted connection"
  3023. " [%s]", destination_port);
  3024. return -1;
  3025. }
  3026. jack_lock_graph (engine);
  3027. ret = jack_port_disconnect_internal (engine, srcport, dstport, TRUE);
  3028. jack_unlock_graph (engine);
  3029. return ret;
  3030. }
  3031. static int
  3032. jack_get_fifo_fd (jack_engine_t *engine, unsigned int which_fifo)
  3033. {
  3034. /* caller must hold client_lock */
  3035. char path[PATH_MAX+1];
  3036. struct stat statbuf;
  3037. snprintf (path, sizeof (path), "%s-%d", engine->fifo_prefix,
  3038. which_fifo);
  3039. DEBUG ("%s", path);
  3040. if (stat (path, &statbuf)) {
  3041. if (errno == ENOENT) {
  3042. if (mkfifo(path, 0666) < 0){
  3043. jack_error ("cannot create inter-client FIFO"
  3044. " [%s] (%s)\n", path,
  3045. strerror (errno));
  3046. return -1;
  3047. }
  3048. } else {
  3049. jack_error ("cannot check on FIFO %d\n", which_fifo);
  3050. return -1;
  3051. }
  3052. } else {
  3053. if (!S_ISFIFO(statbuf.st_mode)) {
  3054. jack_error ("FIFO %d (%s) already exists, but is not"
  3055. " a FIFO!\n", which_fifo, path);
  3056. return -1;
  3057. }
  3058. }
  3059. if (which_fifo >= engine->fifo_size) {
  3060. unsigned int i;
  3061. engine->fifo = (int *)
  3062. realloc (engine->fifo,
  3063. sizeof (int) * (engine->fifo_size + 16));
  3064. for (i = engine->fifo_size; i < engine->fifo_size + 16; i++) {
  3065. engine->fifo[i] = -1;
  3066. }
  3067. engine->fifo_size += 16;
  3068. }
  3069. if (engine->fifo[which_fifo] < 0) {
  3070. if ((engine->fifo[which_fifo] =
  3071. open (path, O_RDWR|O_CREAT|O_NONBLOCK, 0666)) < 0) {
  3072. jack_error ("cannot open fifo [%s] (%s)", path,
  3073. strerror (errno));
  3074. return -1;
  3075. }
  3076. DEBUG ("opened engine->fifo[%d] == %d (%s)",
  3077. which_fifo, engine->fifo[which_fifo], path);
  3078. }
  3079. return engine->fifo[which_fifo];
  3080. }
  3081. static void
  3082. jack_clear_fifos (jack_engine_t *engine)
  3083. {
  3084. /* caller must hold client_lock */
  3085. unsigned int i;
  3086. char buf[16];
  3087. /* this just drains the existing FIFO's of any data left in
  3088. them by aborted clients, etc. there is only ever going to
  3089. be 0, 1 or 2 bytes in them, but we'll allow for up to 16.
  3090. */
  3091. for (i = 0; i < engine->fifo_size; i++) {
  3092. if (engine->fifo[i] >= 0) {
  3093. int nread = read (engine->fifo[i], buf, sizeof (buf));
  3094. if (nread < 0 && errno != EAGAIN) {
  3095. jack_error ("clear fifo[%d] error: %s",
  3096. i, strerror (errno));
  3097. }
  3098. }
  3099. }
  3100. }
  3101. static int
  3102. jack_use_driver (jack_engine_t *engine, jack_driver_t *driver)
  3103. {
  3104. if (engine->driver) {
  3105. engine->driver->detach (engine->driver, engine);
  3106. engine->driver = 0;
  3107. }
  3108. if (driver) {
  3109. if (driver->attach (driver, engine))
  3110. return -1;
  3111. engine->rolling_interval =
  3112. jack_rolling_interval (driver->period_usecs);
  3113. }
  3114. engine->driver = driver;
  3115. return 0;
  3116. }
  3117. /* PORT RELATED FUNCTIONS */
  3118. static jack_port_id_t
  3119. jack_get_free_port (jack_engine_t *engine)
  3120. {
  3121. jack_port_id_t i;
  3122. pthread_mutex_lock (&engine->port_lock);
  3123. for (i = 0; i < engine->port_max; i++) {
  3124. if (engine->control->ports[i].in_use == 0) {
  3125. engine->control->ports[i].in_use = 1;
  3126. break;
  3127. }
  3128. }
  3129. pthread_mutex_unlock (&engine->port_lock);
  3130. if (i == engine->port_max) {
  3131. return (jack_port_id_t) -1;
  3132. }
  3133. return i;
  3134. }
  3135. static void
  3136. jack_port_release (jack_engine_t *engine, jack_port_internal_t *port)
  3137. {
  3138. pthread_mutex_lock (&engine->port_lock);
  3139. port->shared->in_use = 0;
  3140. if (port->buffer_info) {
  3141. jack_port_buffer_list_t *blist =
  3142. jack_port_buffer_list (engine, port);
  3143. pthread_mutex_lock (&blist->lock);
  3144. blist->freelist =
  3145. jack_slist_prepend (blist->freelist,
  3146. port->buffer_info);
  3147. port->buffer_info = NULL;
  3148. pthread_mutex_unlock (&blist->lock);
  3149. }
  3150. pthread_mutex_unlock (&engine->port_lock);
  3151. }
  3152. jack_port_internal_t *
  3153. jack_get_port_internal_by_name (jack_engine_t *engine, const char *name)
  3154. {
  3155. jack_port_id_t id;
  3156. pthread_mutex_lock (&engine->port_lock);
  3157. for (id = 0; id < engine->port_max; id++) {
  3158. if (strcmp (engine->control->ports[id].name, name) == 0) {
  3159. break;
  3160. }
  3161. }
  3162. pthread_mutex_unlock (&engine->port_lock);
  3163. if (id != engine->port_max) {
  3164. return &engine->internal_ports[id];
  3165. } else {
  3166. return NULL;
  3167. }
  3168. }
  3169. int
  3170. jack_port_do_register (jack_engine_t *engine, jack_request_t *req)
  3171. {
  3172. jack_port_id_t port_id;
  3173. jack_port_shared_t *shared;
  3174. jack_port_internal_t *port;
  3175. jack_client_internal_t *client;
  3176. unsigned long i;
  3177. for (i = 0; i < engine->control->n_port_types; ++i) {
  3178. if (strcmp (req->x.port_info.type,
  3179. engine->control->port_types[i].type_name) == 0) {
  3180. break;
  3181. }
  3182. }
  3183. if (i == engine->control->n_port_types) {
  3184. jack_error ("cannot register a port of type \"%s\"",
  3185. req->x.port_info.type);
  3186. return -1;
  3187. }
  3188. jack_lock_graph (engine);
  3189. if ((client = jack_client_internal_by_id (engine,
  3190. req->x.port_info.client_id))
  3191. == NULL) {
  3192. jack_error ("unknown client id in port registration request");
  3193. return -1;
  3194. }
  3195. jack_unlock_graph (engine);
  3196. if ((port_id = jack_get_free_port (engine)) == (jack_port_id_t) -1) {
  3197. jack_error ("no ports available!");
  3198. return -1;
  3199. }
  3200. shared = &engine->control->ports[port_id];
  3201. strcpy (shared->name, req->x.port_info.name);
  3202. shared->ptype_id = engine->control->port_types[i].ptype_id;
  3203. shared->client_id = req->x.port_info.client_id;
  3204. shared->flags = req->x.port_info.flags;
  3205. shared->latency = 0;
  3206. shared->monitor_requests = 0;
  3207. shared->locked = 0;
  3208. port = &engine->internal_ports[port_id];
  3209. port->shared = shared;
  3210. port->connections = 0;
  3211. port->buffer_info = NULL;
  3212. if (jack_port_assign_buffer (engine, port)) {
  3213. jack_error ("cannot assign buffer for port");
  3214. return -1;
  3215. }
  3216. jack_lock_graph (engine);
  3217. client->ports = jack_slist_prepend (client->ports, port);
  3218. jack_port_registration_notify (engine, port_id, TRUE);
  3219. jack_unlock_graph (engine);
  3220. VERBOSE (engine, "registered port %s, offset = %u\n",
  3221. shared->name, (unsigned int)shared->offset);
  3222. req->x.port_info.port_id = port_id;
  3223. return 0;
  3224. }
  3225. int
  3226. jack_port_do_unregister (jack_engine_t *engine, jack_request_t *req)
  3227. {
  3228. jack_client_internal_t *client;
  3229. jack_port_shared_t *shared;
  3230. jack_port_internal_t *port;
  3231. if (req->x.port_info.port_id < 0 ||
  3232. req->x.port_info.port_id > engine->port_max) {
  3233. jack_error ("invalid port ID %" PRIu32
  3234. " in unregister request",
  3235. req->x.port_info.port_id);
  3236. return -1;
  3237. }
  3238. shared = &engine->control->ports[req->x.port_info.port_id];
  3239. if (shared->client_id != req->x.port_info.client_id) {
  3240. jack_error ("Client %" PRIu32
  3241. " is not allowed to remove port %s",
  3242. req->x.port_info.client_id, shared->name);
  3243. return -1;
  3244. }
  3245. jack_lock_graph (engine);
  3246. if ((client = jack_client_internal_by_id (engine, shared->client_id))
  3247. == NULL) {
  3248. jack_error ("unknown client id in port registration request");
  3249. jack_unlock_graph (engine);
  3250. return -1;
  3251. }
  3252. port = &engine->internal_ports[req->x.port_info.port_id];
  3253. jack_port_clear_connections (engine, port);
  3254. jack_port_release (engine,
  3255. &engine->internal_ports[req->x.port_info.port_id]);
  3256. client->ports = jack_slist_remove (client->ports, port);
  3257. jack_port_registration_notify (engine, req->x.port_info.port_id,
  3258. FALSE);
  3259. jack_unlock_graph (engine);
  3260. return 0;
  3261. }
  3262. int
  3263. jack_do_get_port_connections (jack_engine_t *engine, jack_request_t *req,
  3264. int reply_fd)
  3265. {
  3266. jack_port_internal_t *port;
  3267. JSList *node;
  3268. unsigned int i;
  3269. int ret = -1;
  3270. int internal = FALSE;
  3271. jack_lock_graph (engine);
  3272. port = &engine->internal_ports[req->x.port_info.port_id];
  3273. DEBUG ("Getting connections for port '%s'.", port->shared->name);
  3274. req->x.port_connections.nports = jack_slist_length (port->connections);
  3275. req->status = 0;
  3276. /* figure out if this is an internal or external client */
  3277. for (node = engine->clients; node; node = jack_slist_next (node)) {
  3278. if (((jack_client_internal_t *) node->data)->request_fd
  3279. == reply_fd) {
  3280. internal = jack_client_is_internal(
  3281. (jack_client_internal_t *) node->data);
  3282. break;
  3283. }
  3284. }
  3285. if (!internal) {
  3286. if (write (reply_fd, req, sizeof (*req))
  3287. < (ssize_t) sizeof (req)) {
  3288. jack_error ("cannot write GetPortConnections result "
  3289. "to client via fd = %d (%s)",
  3290. reply_fd, strerror (errno));
  3291. goto out;
  3292. }
  3293. } else {
  3294. req->x.port_connections.ports = (const char **)
  3295. malloc (sizeof (char *)
  3296. * req->x.port_connections.nports);
  3297. }
  3298. if (req->type == GetPortConnections) {
  3299. for (i = 0, node = port->connections; node;
  3300. node = jack_slist_next (node), ++i) {
  3301. jack_port_id_t port_id;
  3302. if (((jack_connection_internal_t *) node->data)->source
  3303. == port) {
  3304. port_id = ((jack_connection_internal_t *)
  3305. node->data)->destination->shared->id;
  3306. } else {
  3307. port_id = ((jack_connection_internal_t *)
  3308. node->data)->source->shared->id;
  3309. }
  3310. if (internal) {
  3311. /* internal client asking for
  3312. * names. store in malloc'ed space,
  3313. * client frees
  3314. */
  3315. req->x.port_connections.ports[i] =
  3316. engine->control->ports[port_id].name;
  3317. } else {
  3318. /* external client asking for
  3319. * names. we write the port id's to
  3320. * the reply fd.
  3321. */
  3322. if (write (reply_fd, &port_id,
  3323. sizeof (port_id))
  3324. < (ssize_t) sizeof (port_id)) {
  3325. jack_error ("cannot write port id "
  3326. "to client");
  3327. goto out;
  3328. }
  3329. }
  3330. }
  3331. }
  3332. ret = 0;
  3333. out:
  3334. req->status = ret;
  3335. jack_unlock_graph (engine);
  3336. return ret;
  3337. }
  3338. void
  3339. jack_port_registration_notify (jack_engine_t *engine,
  3340. jack_port_id_t port_id, int yn)
  3341. {
  3342. jack_event_t event;
  3343. jack_client_internal_t *client;
  3344. JSList *node;
  3345. event.type = (yn ? PortRegistered : PortUnregistered);
  3346. event.x.port_id = port_id;
  3347. for (node = engine->clients; node; node = jack_slist_next (node)) {
  3348. client = (jack_client_internal_t *) node->data;
  3349. if (!client->control->active) {
  3350. continue;
  3351. }
  3352. if (client->control->port_register) {
  3353. if (jack_deliver_event (engine, client, &event)) {
  3354. jack_error ("cannot send port registration"
  3355. " notification to %s (%s)",
  3356. client->control->name,
  3357. strerror (errno));
  3358. }
  3359. }
  3360. }
  3361. }
  3362. int
  3363. jack_port_assign_buffer (jack_engine_t *engine, jack_port_internal_t *port)
  3364. {
  3365. jack_port_buffer_list_t *blist =
  3366. jack_port_buffer_list (engine, port);
  3367. jack_port_buffer_info_t *bi;
  3368. if (port->shared->flags & JackPortIsInput) {
  3369. port->shared->offset = 0;
  3370. return 0;
  3371. }
  3372. pthread_mutex_lock (&blist->lock);
  3373. if (blist->freelist == NULL) {
  3374. jack_port_type_info_t *port_type =
  3375. jack_port_type_info (engine, port);
  3376. jack_error ("all %s port buffers in use!",
  3377. port_type->type_name);
  3378. pthread_mutex_unlock (&blist->lock);
  3379. return -1;
  3380. }
  3381. bi = (jack_port_buffer_info_t *) blist->freelist->data;
  3382. blist->freelist = jack_slist_remove (blist->freelist, bi);
  3383. port->shared->offset = bi->offset;
  3384. port->buffer_info = bi;
  3385. pthread_mutex_unlock (&blist->lock);
  3386. return 0;
  3387. }
  3388. static jack_port_internal_t *
  3389. jack_get_port_by_name (jack_engine_t *engine, const char *name)
  3390. {
  3391. jack_port_id_t id;
  3392. /* Note the potential race on "in_use". Other design
  3393. elements prevent this from being a problem.
  3394. */
  3395. for (id = 0; id < engine->port_max; id++) {
  3396. if (engine->control->ports[id].in_use &&
  3397. strcmp (engine->control->ports[id].name, name) == 0) {
  3398. return &engine->internal_ports[id];
  3399. }
  3400. }
  3401. return NULL;
  3402. }
  3403. static int
  3404. jack_send_connection_notification (jack_engine_t *engine,
  3405. jack_client_id_t client_id,
  3406. jack_port_id_t self_id,
  3407. jack_port_id_t other_id, int connected)
  3408. {
  3409. jack_client_internal_t *client;
  3410. jack_event_t event;
  3411. if ((client = jack_client_internal_by_id (engine, client_id)) == NULL) {
  3412. jack_error ("no such client %" PRIu32
  3413. " during connection notification", client_id);
  3414. return -1;
  3415. }
  3416. if (client->control->active) {
  3417. event.type = (connected ? PortConnected : PortDisconnected);
  3418. event.x.self_id = self_id;
  3419. event.y.other_id = other_id;
  3420. if (jack_deliver_event (engine, client, &event)) {
  3421. jack_error ("cannot send port connection notification"
  3422. " to client %s (%s)",
  3423. client->control->name, strerror (errno));
  3424. return -1;
  3425. }
  3426. }
  3427. return 0;
  3428. }