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.

3508 lines
84KB

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