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.

2039 lines
47KB

  1. /* -*- mode: c; c-file-style: "bsd"; -*- */
  2. /*
  3. Copyright (C) 2001-2003 Paul Davis
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  15. $Id$
  16. */
  17. #include <config.h>
  18. #include <pthread.h>
  19. #include <errno.h>
  20. #include <fcntl.h>
  21. #include <stdarg.h>
  22. #include <stdio.h>
  23. #ifdef HAVE_STDINT_H
  24. #include <stdint.h>
  25. #endif
  26. #include <regex.h>
  27. #include <string.h>
  28. #include <sys/types.h>
  29. #include <sys/socket.h>
  30. #include <sys/un.h>
  31. #include <sys/mman.h>
  32. #include <jack/internal.h>
  33. #include <jack/jack.h>
  34. #include <jack/engine.h>
  35. #include <jack/pool.h>
  36. #include <jack/jslist.h>
  37. #include <jack/version.h>
  38. #include <jack/shm.h>
  39. #include <jack/unlock.h>
  40. #include <jack/thread.h>
  41. #include <jack/varargs.h>
  42. #include <sysdeps/time.h>
  43. JACK_TIME_GLOBAL_DECL; /* One instance per process. */
  44. #include "local.h"
  45. #include <sysdeps/poll.h>
  46. #include <sysdeps/ipc.h>
  47. #ifdef JACK_USE_MACH_THREADS
  48. #include <sysdeps/pThreadUtilities.h>
  49. #endif
  50. #ifdef WITH_TIMESTAMPS
  51. #include <jack/timestamps.h>
  52. #endif /* WITH_TIMESTAMPS */
  53. static pthread_mutex_t client_lock;
  54. static pthread_cond_t client_ready;
  55. void *jack_zero_filled_buffer = NULL;
  56. #define EVENT_POLL_INDEX 0
  57. #define WAIT_POLL_INDEX 1
  58. #define event_fd pollfd[EVENT_POLL_INDEX].fd
  59. #define graph_wait_fd pollfd[WAIT_POLL_INDEX].fd
  60. typedef struct {
  61. int status;
  62. struct _jack_client *client;
  63. const char *client_name;
  64. } client_info;
  65. void
  66. jack_error (const char *fmt, ...)
  67. {
  68. va_list ap;
  69. char buffer[300];
  70. va_start (ap, fmt);
  71. vsnprintf (buffer, sizeof(buffer), fmt, ap);
  72. jack_error_callback (buffer);
  73. va_end (ap);
  74. }
  75. void
  76. default_jack_error_callback (const char *desc)
  77. {
  78. fprintf(stderr, "%s\n", desc);
  79. }
  80. void
  81. silent_jack_error_callback (const char *desc)
  82. {
  83. }
  84. void (*jack_error_callback)(const char *desc) = &default_jack_error_callback;
  85. static int
  86. oop_client_deliver_request (void *ptr, jack_request_t *req)
  87. {
  88. int wok, rok;
  89. jack_client_t *client = (jack_client_t*) ptr;
  90. wok = (write (client->request_fd, req, sizeof (*req))
  91. == sizeof (*req));
  92. rok = (read (client->request_fd, req, sizeof (*req))
  93. == sizeof (*req));
  94. if (wok && rok) { /* everything OK? */
  95. return req->status;
  96. }
  97. req->status = -1; /* request failed */
  98. /* check for server shutdown */
  99. if (client->engine->engine_ok == 0)
  100. return req->status;
  101. /* otherwise report errors */
  102. if (!wok)
  103. jack_error ("cannot send request type %d to server",
  104. req->type);
  105. if (!rok)
  106. jack_error ("cannot read result for request type %d from"
  107. " server (%s)", req->type, strerror (errno));
  108. return req->status;
  109. }
  110. int
  111. jack_client_deliver_request (const jack_client_t *client, jack_request_t *req)
  112. {
  113. /* indirect through the function pointer that was set either
  114. * by jack_client_open() or by jack_new_client_request() in
  115. * the server.
  116. */
  117. return client->control->deliver_request (client->control->deliver_arg,
  118. req);
  119. }
  120. jack_client_t *
  121. jack_client_alloc ()
  122. {
  123. jack_client_t *client;
  124. client = (jack_client_t *) malloc (sizeof (jack_client_t));
  125. client->pollfd = (struct pollfd *) malloc (sizeof (struct pollfd) * 2);
  126. client->pollmax = 2;
  127. client->request_fd = -1;
  128. client->event_fd = -1;
  129. client->upstream_is_jackd = 0;
  130. client->graph_wait_fd = -1;
  131. client->graph_next_fd = -1;
  132. client->ports = NULL;
  133. client->ports_ext = NULL;
  134. client->engine = NULL;
  135. client->control = NULL;
  136. client->thread_ok = FALSE;
  137. #if JACK_USE_MACH_THREADS
  138. client->rt_thread_ok = FALSE;
  139. #endif
  140. client->first_active = TRUE;
  141. client->on_shutdown = NULL;
  142. client->n_port_types = 0;
  143. client->port_segment = NULL;
  144. return client;
  145. }
  146. /*
  147. * Build the jack_client_t structure for an internal client.
  148. */
  149. jack_client_t *
  150. jack_client_alloc_internal (jack_client_control_t *cc, jack_engine_t* engine)
  151. {
  152. jack_client_t* client;
  153. client = jack_client_alloc ();
  154. client->control = cc;
  155. client->engine = engine->control;
  156. client->n_port_types = client->engine->n_port_types;
  157. client->port_segment = &engine->port_segment[0];
  158. return client;
  159. }
  160. static void
  161. jack_client_free (jack_client_t *client)
  162. {
  163. if (client->pollfd) {
  164. free (client->pollfd);
  165. }
  166. free (client);
  167. }
  168. void
  169. jack_client_invalidate_port_buffers (jack_client_t *client)
  170. {
  171. JSList *node;
  172. jack_port_t *port;
  173. /* This releases all local memory owned by input ports
  174. and sets the buffer pointer to NULL. This will cause
  175. jack_port_get_buffer() to reallocate space for the
  176. buffer on the next call (if there is one).
  177. */
  178. for (node = client->ports; node; node = jack_slist_next (node)) {
  179. port = (jack_port_t *) node->data;
  180. if (port->shared->flags & JackPortIsInput) {
  181. if (port->mix_buffer) {
  182. jack_pool_release (port->mix_buffer);
  183. port->mix_buffer = NULL;
  184. }
  185. }
  186. }
  187. }
  188. int
  189. jack_client_handle_port_connection (jack_client_t *client, jack_event_t *event)
  190. {
  191. jack_port_t *control_port;
  192. jack_port_t *other;
  193. JSList *node;
  194. int need_free = FALSE;
  195. switch (event->type) {
  196. case PortConnected:
  197. other = jack_port_new (client, event->y.other_id,
  198. client->engine);
  199. /* jack_port_by_id_int() always returns an internal
  200. * port that does not need to be deallocated */
  201. control_port = jack_port_by_id_int (client, event->x.self_id,
  202. &need_free);
  203. pthread_mutex_lock (&control_port->connection_lock);
  204. control_port->connections =
  205. jack_slist_prepend (control_port->connections,
  206. (void *) other);
  207. pthread_mutex_unlock (&control_port->connection_lock);
  208. break;
  209. case PortDisconnected:
  210. /* jack_port_by_id_int() always returns an internal
  211. * port that does not need to be deallocated */
  212. control_port = jack_port_by_id_int (client, event->x.self_id,
  213. &need_free);
  214. pthread_mutex_lock (&control_port->connection_lock);
  215. for (node = control_port->connections; node;
  216. node = jack_slist_next (node)) {
  217. other = (jack_port_t *) node->data;
  218. if (other->shared->id == event->y.other_id) {
  219. control_port->connections =
  220. jack_slist_remove_link (
  221. control_port->connections,
  222. node);
  223. jack_slist_free_1 (node);
  224. free (other);
  225. break;
  226. }
  227. }
  228. pthread_mutex_unlock (&control_port->connection_lock);
  229. break;
  230. default:
  231. /* impossible */
  232. break;
  233. }
  234. return 0;
  235. }
  236. static int
  237. jack_handle_reorder (jack_client_t *client, jack_event_t *event)
  238. {
  239. char path[PATH_MAX+1];
  240. if (client->graph_wait_fd >= 0) {
  241. DEBUG ("closing graph_wait_fd==%d", client->graph_wait_fd);
  242. close (client->graph_wait_fd);
  243. client->graph_wait_fd = -1;
  244. }
  245. if (client->graph_next_fd >= 0) {
  246. DEBUG ("closing graph_next_fd==%d", client->graph_next_fd);
  247. close (client->graph_next_fd);
  248. client->graph_next_fd = -1;
  249. }
  250. sprintf (path, "%s-%" PRIu32, client->fifo_prefix, event->x.n);
  251. if ((client->graph_wait_fd = open (path, O_RDONLY|O_NONBLOCK)) < 0) {
  252. jack_error ("cannot open specified fifo [%s] for reading (%s)",
  253. path, strerror (errno));
  254. return -1;
  255. }
  256. DEBUG ("opened new graph_wait_fd %d (%s)", client->graph_wait_fd, path);
  257. sprintf (path, "%s-%" PRIu32, client->fifo_prefix, event->x.n+1);
  258. if ((client->graph_next_fd = open (path, O_WRONLY|O_NONBLOCK)) < 0) {
  259. jack_error ("cannot open specified fifo [%s] for writing (%s)",
  260. path, strerror (errno));
  261. return -1;
  262. }
  263. client->upstream_is_jackd = event->y.n;
  264. client->pollmax = 2;
  265. DEBUG ("opened new graph_next_fd %d (%s) (upstream is jackd? %d)",
  266. client->graph_next_fd, path,
  267. client->upstream_is_jackd);
  268. /* If the client registered its own callback for graph order events,
  269. execute it now.
  270. */
  271. if (client->control->graph_order) {
  272. client->control->graph_order (client->control->graph_order_arg);
  273. }
  274. return 0;
  275. }
  276. static int
  277. server_connect (const char *server_name)
  278. {
  279. int fd;
  280. struct sockaddr_un addr;
  281. int which = 0;
  282. if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) {
  283. jack_error ("cannot create client socket (%s)",
  284. strerror (errno));
  285. return -1;
  286. }
  287. //JOQ: temporary debug message
  288. //fprintf (stderr, "DEBUG: connecting to `%s' server\n", server_name);
  289. addr.sun_family = AF_UNIX;
  290. snprintf (addr.sun_path, sizeof (addr.sun_path) - 1, "%s/jack_%d",
  291. jack_server_dir (server_name), which);
  292. if (connect (fd, (struct sockaddr *) &addr, sizeof (addr)) < 0) {
  293. close (fd);
  294. return -1;
  295. }
  296. return fd;
  297. }
  298. static int
  299. server_event_connect (jack_client_t *client, const char *server_name)
  300. {
  301. int fd;
  302. struct sockaddr_un addr;
  303. jack_client_connect_ack_request_t req;
  304. jack_client_connect_ack_result_t res;
  305. if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) {
  306. jack_error ("cannot create client event socket (%s)",
  307. strerror (errno));
  308. return -1;
  309. }
  310. addr.sun_family = AF_UNIX;
  311. snprintf (addr.sun_path, sizeof (addr.sun_path) - 1, "%s/jack_ack_0",
  312. jack_server_dir (server_name));
  313. if (connect (fd, (struct sockaddr *) &addr, sizeof (addr)) < 0) {
  314. jack_error ("cannot connect to jack server for events",
  315. strerror (errno));
  316. close (fd);
  317. return -1;
  318. }
  319. req.client_id = client->control->id;
  320. if (write (fd, &req, sizeof (req)) != sizeof (req)) {
  321. jack_error ("cannot write event connect request to server (%s)",
  322. strerror (errno));
  323. close (fd);
  324. return -1;
  325. }
  326. if (read (fd, &res, sizeof (res)) != sizeof (res)) {
  327. jack_error ("cannot read event connect result from server (%s)",
  328. strerror (errno));
  329. close (fd);
  330. return -1;
  331. }
  332. if (res.status != 0) {
  333. jack_error ("cannot connect to server for event stream (%s)",
  334. strerror (errno));
  335. close (fd);
  336. return -1;
  337. }
  338. return fd;
  339. }
  340. /* Exec the JACK server in this process. Does not return. */
  341. static void
  342. _start_server (const char *server_name)
  343. {
  344. FILE* fp = 0;
  345. char filename[255];
  346. char arguments[255];
  347. char buffer[255];
  348. char* command = 0;
  349. size_t pos = 0;
  350. size_t result = 0;
  351. char** argv = 0;
  352. int i = 0;
  353. int good = 0;
  354. int ret;
  355. snprintf(filename, 255, "%s/.jackdrc", getenv("HOME"));
  356. fp = fopen(filename, "r");
  357. if (!fp) {
  358. fp = fopen("/etc/jackd.conf", "r");
  359. }
  360. if (fp) {
  361. arguments[0] = '\0';
  362. ret = fscanf(fp, "%s", buffer);
  363. while(ret != 0 && ret != EOF) {
  364. strcat(arguments, buffer);
  365. strcat(arguments, " ");
  366. ret = fscanf(fp, "%s", buffer);
  367. }
  368. if (strlen(arguments) > 0) {
  369. good = 1;
  370. }
  371. }
  372. if (!good) {
  373. #if defined(USE_CAPABILITIES)
  374. command = JACK_LOCATION "/jackstart";
  375. strncpy(arguments, JACK_LOCATION "/jackstart -T -R -d "
  376. JACK_DEFAULT_DRIVER " -p 512", 255);
  377. #else /* !USE_CAPABILITIES */
  378. command = JACK_LOCATION "/jackd";
  379. strncpy(arguments, JACK_LOCATION "/jackd -T -d "
  380. JACK_DEFAULT_DRIVER, 255);
  381. #endif /* USE_CAPABILITIES */
  382. } else {
  383. result = strcspn(arguments, " ");
  384. command = (char *) malloc(result+1);
  385. strncpy(command, arguments, result);
  386. command[result] = '\0';
  387. }
  388. argv = (char **) malloc (255);
  389. while(1) {
  390. /* insert -T and -nserver_name in front of arguments */
  391. if (i == 1) {
  392. argv[i] = (char *) malloc(strlen ("-T") + 1);
  393. strcpy (argv[i++], "-T");
  394. if (server_name) {
  395. size_t optlen = strlen ("-n");
  396. char *buf =
  397. malloc (optlen
  398. + strlen (server_name) + 1);
  399. strcpy (buf, "-n");
  400. strcpy (buf+optlen, server_name);
  401. argv[i++] = buf;
  402. }
  403. }
  404. result = strcspn(arguments+pos, " ");
  405. if (result == 0) {
  406. break;
  407. }
  408. argv[i] = (char*)malloc(result+1);
  409. strncpy(argv[i], arguments+pos, result);
  410. argv[i][result] = '\0';
  411. pos += result+1;
  412. ++i;
  413. }
  414. argv[i] = 0;
  415. execv (command, argv);
  416. /* If execv() succeeds, it does not return. There's no point
  417. * in calling jack_error() here in the child process. */
  418. perror ("exec of JACK server failed");
  419. }
  420. int
  421. start_server (const char *server_name, jack_options_t options)
  422. {
  423. if ((options & JackNoStartServer)
  424. || getenv("JACK_NO_START_SERVER")) {
  425. return 1;
  426. }
  427. /* The double fork() forces the server to become a child of
  428. * init, which will always clean up zombie process state on
  429. * termination. This even works in cases where the server
  430. * terminates but this client does not.
  431. *
  432. * Since fork() is usually implemented using copy-on-write
  433. * virtual memory tricks, the overhead of the second fork() is
  434. * probably relatively small.
  435. */
  436. switch (fork()) {
  437. case 0: /* child process */
  438. switch (fork()) {
  439. case 0: /* grandchild process */
  440. _start_server(server_name);
  441. _exit (99); /* exec failed */
  442. case -1:
  443. _exit (98);
  444. default:
  445. _exit (0);
  446. }
  447. case -1: /* fork() error */
  448. return 1; /* failed to start server */
  449. }
  450. /* only the original parent process goes here */
  451. return 0; /* (probably) successful */
  452. }
  453. static int
  454. jack_request_client (ClientType type,
  455. const char* client_name, jack_options_t options,
  456. jack_status_t *status, jack_varargs_t *va,
  457. jack_client_connect_result_t *res, int *req_fd)
  458. {
  459. jack_client_connect_request_t req;
  460. *req_fd = -1;
  461. memset (&req, 0, sizeof (req));
  462. req.options = options;
  463. if (strlen (client_name) >= sizeof (req.name)) {
  464. jack_error ("\"%s\" is too long to be used as a JACK client"
  465. " name.\n"
  466. "Please use %lu characters or less.",
  467. client_name, sizeof (req.name));
  468. return -1;
  469. }
  470. if (va->load_name
  471. && (strlen (va->load_name) > sizeof (req.object_path) - 1)) {
  472. jack_error ("\"%s\" is too long to be used as a JACK shared"
  473. " object name.\n"
  474. "Please use %lu characters or less.",
  475. va->load_name, sizeof (req.object_path) - 1);
  476. return -1;
  477. }
  478. if (va->load_init
  479. && (strlen (va->load_init) > sizeof (req.object_data) - 1)) {
  480. jack_error ("\"%s\" is too long to be used as a JACK shared"
  481. " object data string.\n"
  482. "Please use %lu characters or less.",
  483. va->load_init, sizeof (req.object_data) - 1);
  484. return -1;
  485. }
  486. if ((*req_fd = server_connect (va->server_name)) < 0) {
  487. int trys;
  488. if (start_server(va->server_name, options)) {
  489. *status |= (JackFailure|JackServerFailed);
  490. goto fail;
  491. }
  492. trys = 5;
  493. do {
  494. sleep(1);
  495. if (--trys < 0) {
  496. *status |= (JackFailure|JackServerFailed);
  497. goto fail;
  498. }
  499. } while ((*req_fd = server_connect (va->server_name)) < 0);
  500. *status |= JackServerStarted;
  501. }
  502. /* format connection request */
  503. req.protocol_v = jack_protocol_version;
  504. req.load = TRUE;
  505. req.type = type;
  506. snprintf (req.name, sizeof (req.name),
  507. "%s", client_name);
  508. snprintf (req.object_path, sizeof (req.object_path),
  509. "%s", va->load_name);
  510. snprintf (req.object_data, sizeof (req.object_data),
  511. "%s", va->load_init);
  512. if (write (*req_fd, &req, sizeof (req)) != sizeof (req)) {
  513. jack_error ("cannot send request to jack server (%s)",
  514. strerror (errno));
  515. *status |= (JackFailure|JackServerError);
  516. goto fail;
  517. }
  518. if (read (*req_fd, res, sizeof (*res)) != sizeof (*res)) {
  519. if (errno == 0) {
  520. /* server shut the socket */
  521. jack_error ("could not attach as client");
  522. *status |= (JackFailure|JackServerError);
  523. goto fail;
  524. }
  525. if (errno == ECONNRESET) {
  526. jack_error ("could not attach as JACK client "
  527. "(server has exited)");
  528. *status |= (JackFailure|JackServerError);
  529. goto fail;
  530. }
  531. jack_error ("cannot read response from jack server (%s)",
  532. strerror (errno));
  533. *status |= (JackFailure|JackServerError);
  534. goto fail;
  535. }
  536. *status |= res->status; /* return server status bits */
  537. if (*status & JackFailure) {
  538. if (*status & JackVersionError) {
  539. jack_error ("client linked with incompatible libjack"
  540. " version.");
  541. }
  542. jack_error ("could not attach to JACK server");
  543. *status |= JackServerError;
  544. goto fail;
  545. }
  546. switch (type) {
  547. case ClientDriver:
  548. case ClientInternal:
  549. close (*req_fd);
  550. *req_fd = -1;
  551. break;
  552. default:
  553. break;
  554. }
  555. return 0;
  556. fail:
  557. if (*req_fd >= 0) {
  558. close (*req_fd);
  559. *req_fd = -1;
  560. }
  561. return -1;
  562. }
  563. int
  564. jack_attach_port_segment (jack_client_t *client, jack_port_type_id_t ptid)
  565. {
  566. /* Lookup, attach and register the port/buffer segments in use
  567. * right now.
  568. */
  569. if (client->control->type != ClientExternal) {
  570. jack_error("Only external clients need attach port segments");
  571. abort();
  572. }
  573. /* make sure we have space to store the port
  574. segment information.
  575. */
  576. if (ptid >= client->n_port_types) {
  577. client->port_segment = (jack_shm_info_t*)
  578. realloc (client->port_segment,
  579. sizeof (jack_shm_info_t) * (ptid+1));
  580. memset (&client->port_segment[client->n_port_types],
  581. 0,
  582. sizeof (jack_shm_info_t) *
  583. (ptid - client->n_port_types));
  584. client->n_port_types = ptid + 1;
  585. } else {
  586. /* release any previous segment */
  587. jack_release_shm (&client->port_segment[ptid]);
  588. }
  589. /* get the index into the shm registry */
  590. client->port_segment[ptid].index =
  591. client->engine->port_types[ptid].shm_registry_index;
  592. /* attach the relevant segment */
  593. if (jack_attach_shm (&client->port_segment[ptid])) {
  594. jack_error ("cannot attach port segment shared memory"
  595. " (%s)", strerror (errno));
  596. return -1;
  597. }
  598. /* The first chunk of the audio port segment will be set by
  599. * the engine to be a zero-filled buffer. This hasn't been
  600. * done yet, but it will happen before the process cycle
  601. * (re)starts.
  602. */
  603. if (ptid == JACK_AUDIO_PORT_TYPE) {
  604. jack_zero_filled_buffer =
  605. jack_shm_addr (&client->port_segment[ptid]);
  606. }
  607. return 0;
  608. }
  609. jack_client_t *
  610. jack_client_open (const char *client_name,
  611. jack_options_t options,
  612. jack_status_t *status, ...)
  613. {
  614. /* optional arguments: */
  615. va_list ap; /* variable argument pointer */
  616. jack_varargs_t va; /* variable arguments */
  617. int req_fd = -1;
  618. int ev_fd = -1;
  619. jack_client_connect_result_t res;
  620. jack_client_t *client;
  621. jack_port_type_id_t ptid;
  622. jack_status_t my_status;
  623. if (status == NULL) /* no status from caller? */
  624. status = &my_status; /* use local status word */
  625. *status = 0;
  626. /* validate parameters */
  627. if ((options & ~JackOpenOptions)) {
  628. *status |= (JackFailure|JackInvalidOption);
  629. return NULL;
  630. }
  631. /* parse variable arguments */
  632. va_start (ap, status);
  633. jack_varargs_parse (options, ap, &va);
  634. va_end (ap);
  635. /* External clients need this initialized. It is already set
  636. * up in the server's address space for internal clients.
  637. */
  638. jack_init_time ();
  639. if (jack_request_client (ClientExternal, client_name, options, status,
  640. &va, &res, &req_fd)) {
  641. return NULL;
  642. }
  643. /* Allocate the jack_client_t structure in local memory.
  644. * Shared memory is not accessible yet. */
  645. client = jack_client_alloc ();
  646. strcpy (client->name, res.name);
  647. strcpy (client->fifo_prefix, res.fifo_prefix);
  648. client->request_fd = req_fd;
  649. client->pollfd[EVENT_POLL_INDEX].events =
  650. POLLIN|POLLERR|POLLHUP|POLLNVAL;
  651. client->pollfd[WAIT_POLL_INDEX].events =
  652. POLLIN|POLLERR|POLLHUP|POLLNVAL;
  653. /* Don't access shared memory until server connected. */
  654. if (jack_initialize_shm (va.server_name)) {
  655. jack_error ("Unable to initialize shared memory.");
  656. *status |= (JackFailure|JackShmFailure);
  657. goto fail;
  658. }
  659. /* attach the engine control/info block */
  660. client->engine_shm = res.engine_shm;
  661. if (jack_attach_shm (&client->engine_shm)) {
  662. jack_error ("cannot attached engine control shared memory"
  663. " segment");
  664. goto fail;
  665. }
  666. client->engine = (jack_control_t *) jack_shm_addr (&client->engine_shm);
  667. /* now attach the client control block */
  668. client->control_shm = res.client_shm;
  669. if (jack_attach_shm (&client->control_shm)) {
  670. jack_error ("cannot attached client control shared memory"
  671. " segment");
  672. goto fail;
  673. }
  674. client->control = (jack_client_control_t *)
  675. jack_shm_addr (&client->control_shm);
  676. /* Nobody else needs to access this shared memory any more, so
  677. * destroy it. Because we have it attached, it won't vanish
  678. * till we exit (and release it).
  679. */
  680. jack_destroy_shm (&client->control_shm);
  681. client->n_port_types = client->engine->n_port_types;
  682. client->port_segment = (jack_shm_info_t *)
  683. malloc (sizeof (jack_shm_info_t) * client->n_port_types);
  684. for (ptid = 0; ptid < client->n_port_types; ++ptid) {
  685. client->port_segment[ptid].index =
  686. client->engine->port_types[ptid].shm_registry_index;
  687. client->port_segment[ptid].attached_at = MAP_FAILED;
  688. jack_attach_port_segment (client, ptid);
  689. }
  690. /* set up the client so that it does the right thing for an
  691. * external client
  692. */
  693. client->control->deliver_request = oop_client_deliver_request;
  694. client->control->deliver_arg = client;
  695. if ((ev_fd = server_event_connect (client, va.server_name)) < 0) {
  696. goto fail;
  697. }
  698. client->event_fd = ev_fd;
  699. #ifdef JACK_USE_MACH_THREADS
  700. /* specific resources for server/client real-time thread
  701. * communication */
  702. client->clienttask = mach_task_self();
  703. if (task_get_bootstrap_port(client->clienttask, &client->bp)){
  704. jack_error ("Can't find bootstrap port");
  705. goto fail;
  706. }
  707. if (allocate_mach_clientport(client, res.portnum) < 0) {
  708. jack_error("Can't allocate mach port");
  709. goto fail;
  710. };
  711. #endif /* JACK_USE_MACH_THREADS */
  712. return client;
  713. fail:
  714. if (client->engine) {
  715. jack_release_shm (&client->engine_shm);
  716. client->engine = 0;
  717. }
  718. if (client->control) {
  719. jack_release_shm (&client->control_shm);
  720. client->control = 0;
  721. }
  722. if (req_fd >= 0) {
  723. close (req_fd);
  724. }
  725. if (ev_fd >= 0) {
  726. close (ev_fd);
  727. }
  728. free (client);
  729. return NULL;
  730. }
  731. jack_client_t *
  732. jack_client_new (const char *client_name)
  733. {
  734. jack_options_t options = JackUseExactName;
  735. if (getenv("JACK_START_SERVER") == NULL)
  736. options |= JackNoStartServer;
  737. return jack_client_open (client_name, options, NULL);
  738. }
  739. char *
  740. jack_get_client_name (jack_client_t *client)
  741. {
  742. return client->name;
  743. }
  744. int
  745. jack_internal_client_new (const char *client_name,
  746. const char *so_name, const char *so_data)
  747. {
  748. jack_client_connect_result_t res;
  749. int req_fd;
  750. jack_varargs_t va;
  751. jack_status_t status;
  752. jack_options_t options = JackUseExactName;
  753. if (getenv("JACK_START_SERVER") == NULL)
  754. options |= JackNoStartServer;
  755. jack_varargs_init (&va);
  756. va.load_name = (char *) so_name;
  757. va.load_init = (char *) so_data;
  758. return jack_request_client (ClientInternal, client_name,
  759. options, &status, &va, &res, &req_fd);
  760. }
  761. char *
  762. jack_default_server_name (void)
  763. {
  764. char *server_name;
  765. if ((server_name = getenv("JACK_DEFAULT_SERVER")) == NULL)
  766. server_name = "default";
  767. return server_name;
  768. }
  769. char *jack_tmpdir = DEFAULT_TMP_DIR;
  770. /* returns the name of the per-user subdirectory of jack_tmpdir */
  771. char *
  772. jack_user_dir (void)
  773. {
  774. static char user_dir[PATH_MAX] = "";
  775. /* format the path name on the first call */
  776. if (user_dir[0] == '\0') {
  777. snprintf (user_dir, sizeof (user_dir), "%s/jack-%d",
  778. jack_tmpdir, getuid ());
  779. }
  780. return user_dir;
  781. }
  782. /* returns the name of the per-server subdirectory of jack_user_dir() */
  783. char *
  784. jack_server_dir (const char *server_name)
  785. {
  786. static char server_dir[PATH_MAX] = "";
  787. /* format the path name on the first call */
  788. if (server_dir[0] == '\0') {
  789. snprintf (server_dir, sizeof (server_dir), "%s/%s",
  790. jack_user_dir (), server_name);
  791. }
  792. return server_dir;
  793. }
  794. void
  795. jack_internal_client_close (const char *client_name)
  796. {
  797. jack_client_connect_request_t req;
  798. int fd;
  799. char *server_name = jack_default_server_name ();
  800. req.load = FALSE;
  801. snprintf (req.name, sizeof (req.name), "%s", client_name);
  802. if ((fd = server_connect (server_name)) < 0) {
  803. return;
  804. }
  805. if (write (fd, &req, sizeof (req)) != sizeof(req)) {
  806. jack_error ("cannot deliver ClientUnload request to JACK "
  807. "server.");
  808. }
  809. /* no response to this request */
  810. close (fd);
  811. return;
  812. }
  813. int
  814. jack_recompute_total_latencies (jack_client_t* client)
  815. {
  816. jack_request_t request;
  817. request.type = RecomputeTotalLatencies;
  818. return jack_client_deliver_request (client, &request);
  819. }
  820. int
  821. jack_set_freewheel (jack_client_t* client, int onoff)
  822. {
  823. jack_request_t request;
  824. request.type = onoff ? FreeWheel : StopFreeWheel;
  825. return jack_client_deliver_request (client, &request);
  826. }
  827. void
  828. jack_start_freewheel (jack_client_t* client)
  829. {
  830. jack_client_control_t *control = client->control;
  831. if (client->engine->real_time) {
  832. #if JACK_USE_MACH_THREADS
  833. jack_drop_real_time_scheduling (client->process_thread);
  834. #else
  835. jack_drop_real_time_scheduling (client->thread);
  836. #endif
  837. }
  838. if (control->freewheel_cb) {
  839. control->freewheel_cb (1, control->freewheel_arg);
  840. }
  841. }
  842. void
  843. jack_stop_freewheel (jack_client_t* client)
  844. {
  845. jack_client_control_t *control = client->control;
  846. if (client->engine->real_time) {
  847. #if JACK_USE_MACH_THREADS
  848. jack_acquire_real_time_scheduling (client->process_thread,
  849. client->engine->client_priority);
  850. #else
  851. jack_acquire_real_time_scheduling (client->thread,
  852. client->engine->client_priority);
  853. #endif
  854. }
  855. if (control->freewheel_cb) {
  856. control->freewheel_cb (0, control->freewheel_arg);
  857. }
  858. }
  859. static void *
  860. jack_client_thread (void *arg)
  861. {
  862. jack_client_t *client = (jack_client_t *) arg;
  863. jack_client_control_t *control = client->control;
  864. jack_event_t event;
  865. char status = 0;
  866. char c = 0;
  867. int err = 0;
  868. pthread_mutex_lock (&client_lock);
  869. client->thread_ok = TRUE;
  870. client->thread_id = pthread_self();
  871. pthread_cond_signal (&client_ready);
  872. pthread_mutex_unlock (&client_lock);
  873. client->control->pid = getpid();
  874. client->control->pgrp = getpgrp();
  875. DEBUG ("client thread is now running");
  876. if (client->control->thread_init) {
  877. DEBUG ("calling client thread init callback");
  878. client->control->thread_init (client->control->thread_init_arg);
  879. }
  880. while (err == 0) {
  881. if (client->engine->engine_ok == 0) {
  882. if (client->on_shutdown)
  883. client->on_shutdown (client->on_shutdown_arg);
  884. else
  885. jack_error ("engine unexpectedly shutdown; "
  886. "thread exiting\n");
  887. pthread_exit (0);
  888. }
  889. DEBUG ("client polling on %s", client->pollmax == 2 ?
  890. "event_fd and graph_wait_fd..." :
  891. "event_fd only");
  892. if (poll (client->pollfd, client->pollmax, 1000) < 0) {
  893. if (errno == EINTR) {
  894. continue;
  895. }
  896. jack_error ("poll failed in client (%s)",
  897. strerror (errno));
  898. status = -1;
  899. break;
  900. }
  901. /* get an accurate timestamp on waking from poll for a
  902. * process() cycle.
  903. */
  904. if (client->graph_wait_fd >= 0
  905. && client->pollfd[WAIT_POLL_INDEX].revents & POLLIN) {
  906. control->awake_at = jack_get_microseconds();
  907. }
  908. DEBUG ("pfd[EVENT].revents = 0x%x pfd[WAIT].revents = 0x%x",
  909. client->pollfd[EVENT_POLL_INDEX].revents,
  910. client->pollfd[WAIT_POLL_INDEX].revents);
  911. pthread_testcancel();
  912. if (client->graph_wait_fd >= 0 &&
  913. (client->pollfd[WAIT_POLL_INDEX].revents & ~POLLIN)) {
  914. DEBUG ("\n\n\n\n\n\n\n\nWAITFD ERROR,"
  915. " ZOMBIE\n\n\n\n\n");
  916. /* our upstream "wait" connection
  917. closed, which either means that
  918. an intermediate client exited, or
  919. jackd exited, or jackd zombified
  920. us.
  921. we can discover the zombification
  922. via client->control->dead, but
  923. the other two possibilities are
  924. impossible to identify just from
  925. this situation. so we have to
  926. check what we are connected to,
  927. and act accordingly.
  928. */
  929. if (client->upstream_is_jackd) {
  930. DEBUG ("WE DIE\n");
  931. goto zombie;
  932. } else {
  933. DEBUG ("WE PUNT\n");
  934. /* don't poll on the wait fd
  935. * again until we get a
  936. * GraphReordered event.
  937. */
  938. client->graph_wait_fd = -1;
  939. client->pollmax = 1;
  940. }
  941. }
  942. if (client->control->dead) {
  943. goto zombie;
  944. }
  945. if (client->pollfd[EVENT_POLL_INDEX].revents & ~POLLIN) {
  946. /* jackd shutdown */
  947. goto zombie;
  948. }
  949. if (client->pollfd[EVENT_POLL_INDEX].revents & POLLIN) {
  950. DEBUG ("client receives an event, "
  951. "now reading on event fd");
  952. /* server has sent us an event. process the
  953. * event and reply */
  954. if (read (client->event_fd, &event, sizeof (event))
  955. != sizeof (event)) {
  956. jack_error ("cannot read server event (%s)",
  957. strerror (errno));
  958. err++;
  959. break;
  960. }
  961. status = 0;
  962. switch (event.type) {
  963. case PortRegistered:
  964. if (control->port_register) {
  965. control->port_register
  966. (event.x.port_id, TRUE,
  967. control->port_register_arg);
  968. }
  969. break;
  970. case PortUnregistered:
  971. if (control->port_register) {
  972. control->port_register
  973. (event.x.port_id, FALSE,
  974. control->port_register_arg);
  975. }
  976. break;
  977. case GraphReordered:
  978. status = jack_handle_reorder (client, &event);
  979. break;
  980. case PortConnected:
  981. case PortDisconnected:
  982. status = jack_client_handle_port_connection
  983. (client, &event);
  984. break;
  985. case BufferSizeChange:
  986. jack_client_invalidate_port_buffers (client);
  987. if (control->bufsize) {
  988. status = control->bufsize
  989. (control->nframes,
  990. control->bufsize_arg);
  991. }
  992. break;
  993. case SampleRateChange:
  994. if (control->srate) {
  995. status = control->srate
  996. (control->nframes,
  997. control->srate_arg);
  998. }
  999. break;
  1000. case XRun:
  1001. if (control->xrun) {
  1002. status = control->xrun
  1003. (control->xrun_arg);
  1004. }
  1005. break;
  1006. case AttachPortSegment:
  1007. jack_attach_port_segment (client, event.y.ptid);
  1008. break;
  1009. case StartFreewheel:
  1010. jack_start_freewheel (client);
  1011. break;
  1012. case StopFreewheel:
  1013. jack_stop_freewheel (client);
  1014. break;
  1015. }
  1016. DEBUG ("client has dealt with the event, writing "
  1017. "response on event fd");
  1018. if (write (client->event_fd, &status, sizeof (status))
  1019. != sizeof (status)) {
  1020. jack_error ("cannot send event response to "
  1021. "engine (%s)", strerror (errno));
  1022. err++;
  1023. break;
  1024. }
  1025. }
  1026. if (client->pollfd[WAIT_POLL_INDEX].revents & POLLIN) {
  1027. #ifdef WITH_TIMESTAMPS
  1028. jack_reset_timestamps ();
  1029. #endif
  1030. DEBUG ("client %d signalled at %" PRIu64
  1031. ", awake for process at %" PRIu64
  1032. " (delay = %" PRIu64
  1033. " usecs) (wakeup on graph_wait_fd==%d)",
  1034. getpid(),
  1035. control->signalled_at,
  1036. control->awake_at,
  1037. control->awake_at - control->signalled_at,
  1038. client->pollfd[WAIT_POLL_INDEX].fd);
  1039. control->state = Running;
  1040. /* begin preemption checking */
  1041. CHECK_PREEMPTION (client->engine, TRUE);
  1042. if (control->sync_cb)
  1043. jack_call_sync_client (client);
  1044. if (control->process) {
  1045. if (control->process (control->nframes,
  1046. control->process_arg)
  1047. == 0) {
  1048. control->state = Finished;
  1049. }
  1050. } else {
  1051. control->state = Finished;
  1052. }
  1053. if (control->timebase_cb)
  1054. jack_call_timebase_master (client);
  1055. /* end preemption checking */
  1056. CHECK_PREEMPTION (client->engine, FALSE);
  1057. control->finished_at = jack_get_microseconds();
  1058. #ifdef WITH_TIMESTAMPS
  1059. jack_timestamp ("finished");
  1060. #endif
  1061. /* pass the execution token along */
  1062. DEBUG ("client finished processing at %" PRIu64
  1063. " (elapsed = %" PRIu64
  1064. " usecs), writing on graph_next_fd==%d",
  1065. control->finished_at,
  1066. control->finished_at - control->awake_at,
  1067. client->graph_next_fd);
  1068. if (write (client->graph_next_fd, &c, sizeof (c))
  1069. != sizeof (c)) {
  1070. jack_error ("cannot continue execution of the "
  1071. "processing graph (%s)",
  1072. strerror(errno));
  1073. err++;
  1074. break;
  1075. }
  1076. DEBUG ("client sent message to next stage by %" PRIu64
  1077. ", client reading on graph_wait_fd==%d",
  1078. jack_get_microseconds(), client->graph_wait_fd);
  1079. #ifdef WITH_TIMESTAMPS
  1080. jack_timestamp ("read pending byte from wait");
  1081. #endif
  1082. DEBUG("reading cleanup byte from pipe\n");
  1083. if ((read (client->graph_wait_fd, &c, sizeof (c))
  1084. != sizeof (c))) {
  1085. DEBUG ("WARNING: READ FAILED!");
  1086. #if 0
  1087. jack_error ("cannot complete execution of the "
  1088. "processing graph (%s)",
  1089. strerror(errno));
  1090. err++;
  1091. break;
  1092. #endif
  1093. }
  1094. /* check if we were killed during the process
  1095. * cycle (or whatever) */
  1096. if (client->control->dead) {
  1097. goto zombie;
  1098. }
  1099. DEBUG("process cycle fully complete\n");
  1100. #ifdef WITH_TIMESTAMPS
  1101. jack_timestamp ("read done");
  1102. jack_dump_timestamps (stdout);
  1103. #endif
  1104. }
  1105. }
  1106. return (void *) ((intptr_t)err);
  1107. zombie:
  1108. if (client->on_shutdown) {
  1109. jack_error ("zombified - calling shutdown handler");
  1110. client->on_shutdown (client->on_shutdown_arg);
  1111. } else {
  1112. jack_error ("zombified - exiting from JACK");
  1113. jack_client_close (client);
  1114. /* Need a fix : possibly make client crash if
  1115. * zombified without shutdown handler
  1116. */
  1117. }
  1118. pthread_exit (0);
  1119. /*NOTREACHED*/
  1120. return 0;
  1121. }
  1122. #ifdef JACK_USE_MACH_THREADS
  1123. /* real-time thread : separated from the normal client thread, it will
  1124. * communicate with the server using fast mach RPC mechanism */
  1125. static void *
  1126. jack_client_process_thread (void *arg)
  1127. {
  1128. jack_client_t *client = (jack_client_t *) arg;
  1129. jack_client_control_t *control = client->control;
  1130. int err = 0;
  1131. if (client->control->thread_init) {
  1132. /* this means that the init callback will be called twice -taybin*/
  1133. DEBUG ("calling client thread init callback");
  1134. client->control->thread_init (client->control->thread_init_arg);
  1135. }
  1136. client->control->pid = getpid();
  1137. DEBUG ("client process thread is now running");
  1138. client->rt_thread_ok = TRUE;
  1139. while (err == 0) {
  1140. if (jack_client_suspend(client) < 0) {
  1141. jack_error ("jack_client_process_thread :resume error");
  1142. goto zombie;
  1143. }
  1144. control->awake_at = jack_get_microseconds();
  1145. DEBUG ("client resumed");
  1146. control->state = Running;
  1147. if (control->sync_cb)
  1148. jack_call_sync_client (client);
  1149. if (control->process) {
  1150. if (control->process (control->nframes,
  1151. control->process_arg) == 0) {
  1152. control->state = Finished;
  1153. }
  1154. } else {
  1155. control->state = Finished;
  1156. }
  1157. if (control->timebase_cb)
  1158. jack_call_timebase_master (client);
  1159. control->finished_at = jack_get_microseconds();
  1160. #ifdef WITH_TIMESTAMPS
  1161. jack_timestamp ("finished");
  1162. #endif
  1163. DEBUG ("client finished processing at %Lu (elapsed = %f usecs)",
  1164. control->finished_at,
  1165. ((float)(control->finished_at - control->awake_at)));
  1166. /* check if we were killed during the process cycle
  1167. * (or whatever) */
  1168. if (client->control->dead) {
  1169. jack_error ("jack_client_process_thread: "
  1170. "client->control->dead");
  1171. goto zombie;
  1172. }
  1173. DEBUG("process cycle fully complete\n");
  1174. }
  1175. return (void *) ((intptr_t)err);
  1176. zombie:
  1177. jack_error ("jack_client_process_thread : zombified");
  1178. client->rt_thread_ok = FALSE;
  1179. if (client->on_shutdown) {
  1180. jack_error ("zombified - calling shutdown handler");
  1181. client->on_shutdown (client->on_shutdown_arg);
  1182. } else {
  1183. jack_error ("zombified - exiting from JACK");
  1184. /* Need a fix : possibly make client crash if
  1185. * zombified without shutdown handler */
  1186. jack_client_close (client);
  1187. }
  1188. pthread_exit (0);
  1189. /*NOTREACHED*/
  1190. return 0;
  1191. }
  1192. #endif /* JACK_USE_MACH_THREADS */
  1193. static int
  1194. jack_start_thread (jack_client_t *client)
  1195. {
  1196. if (client->engine->real_time) {
  1197. #ifdef USE_MLOCK
  1198. if (client->engine->do_mlock
  1199. && (mlockall (MCL_CURRENT | MCL_FUTURE) != 0)) {
  1200. jack_error ("cannot lock down memory for RT thread "
  1201. "(%s)", strerror (errno));
  1202. #ifdef ENSURE_MLOCK
  1203. return -1;
  1204. #endif /* ENSURE_MLOCK */
  1205. }
  1206. if (client->engine->do_munlock) {
  1207. cleanup_mlock ();
  1208. }
  1209. #endif /* USE_MLOCK */
  1210. }
  1211. #ifdef JACK_USE_MACH_THREADS
  1212. /* Stephane Letz : letz@grame.fr
  1213. On MacOSX, the normal thread does not need to be real-time.
  1214. */
  1215. if (jack_client_create_thread (client,
  1216. &client->thread,
  1217. client->engine->client_priority,
  1218. FALSE,
  1219. jack_client_thread, client)) {
  1220. return -1;
  1221. }
  1222. #else
  1223. if (jack_client_create_thread (client,
  1224. &client->thread,
  1225. client->engine->client_priority,
  1226. client->engine->real_time,
  1227. jack_client_thread, client)) {
  1228. return -1;
  1229. }
  1230. #endif
  1231. #ifdef JACK_USE_MACH_THREADS
  1232. /* a secondary thread that runs the process callback and uses
  1233. ultra-fast Mach primitives for inter-thread signalling.
  1234. XXX in a properly structured JACK, there would be no
  1235. need for this, because we would have client wake up
  1236. methods that encapsulated the underlying mechanism
  1237. used.
  1238. */
  1239. if (jack_client_create_thread(client,
  1240. &client->process_thread,
  1241. client->engine->client_priority,
  1242. client->engine->real_time,
  1243. jack_client_process_thread, client)) {
  1244. return -1;
  1245. }
  1246. #endif /* JACK_USE_MACH_THREADS */
  1247. return 0;
  1248. }
  1249. int
  1250. jack_activate (jack_client_t *client)
  1251. {
  1252. jack_request_t req;
  1253. /* we need to scribble on our stack to ensure that its memory
  1254. * pages are actually mapped (more important for mlockall(2)
  1255. * usage in jack_start_thread())
  1256. */
  1257. char buf[JACK_THREAD_STACK_TOUCH];
  1258. int i;
  1259. for (i = 0; i < JACK_THREAD_STACK_TOUCH; i++) {
  1260. buf[i] = (char) (i & 0xff);
  1261. }
  1262. if (client->control->type == ClientInternal ||
  1263. client->control->type == ClientDriver) {
  1264. goto startit;
  1265. }
  1266. /* get the pid of the client process to pass it to engine */
  1267. client->control->pid = getpid ();
  1268. #ifdef USE_CAPABILITIES
  1269. if (client->engine->has_capabilities != 0 &&
  1270. client->control->pid != 0 && client->engine->real_time != 0) {
  1271. /* we need to ask the engine for realtime capabilities
  1272. before trying to start the realtime thread
  1273. */
  1274. req.type = SetClientCapabilities;
  1275. req.x.client_id = client->control->id;
  1276. req.x.cap_pid = client->control->pid;
  1277. jack_client_deliver_request (client, &req);
  1278. if (req.status) {
  1279. /* what to do? engine is running realtime, it
  1280. is using capabilities and has them
  1281. (otherwise we would not get an error
  1282. return) but for some reason it could not
  1283. give the client the required capabilities.
  1284. For now, leave the client so that it
  1285. still runs, albeit non-realtime.
  1286. */
  1287. jack_error ("could not receive realtime capabilities, "
  1288. "client will run non-realtime");
  1289. }
  1290. }
  1291. #endif /* USE_CAPABILITIES */
  1292. if (client->first_active) {
  1293. pthread_mutex_init (&client_lock, NULL);
  1294. pthread_cond_init (&client_ready, NULL);
  1295. pthread_mutex_lock (&client_lock);
  1296. if (jack_start_thread (client)) {
  1297. pthread_mutex_unlock (&client_lock);
  1298. return -1;
  1299. }
  1300. pthread_cond_wait (&client_ready, &client_lock);
  1301. pthread_mutex_unlock (&client_lock);
  1302. if (!client->thread_ok) {
  1303. jack_error ("could not start client thread");
  1304. return -1;
  1305. }
  1306. client->first_active = FALSE;
  1307. }
  1308. startit:
  1309. req.type = ActivateClient;
  1310. req.x.client_id = client->control->id;
  1311. return jack_client_deliver_request (client, &req);
  1312. }
  1313. int
  1314. jack_deactivate (jack_client_t *client)
  1315. {
  1316. jack_request_t req;
  1317. int rc = ESRCH; /* already shut down */
  1318. if (client && client->control) { /* not shut down? */
  1319. rc = 0;
  1320. if (client->control->active) { /* still active? */
  1321. req.type = DeactivateClient;
  1322. req.x.client_id = client->control->id;
  1323. rc = jack_client_deliver_request (client, &req);
  1324. }
  1325. }
  1326. return rc;
  1327. }
  1328. int
  1329. jack_client_close (jack_client_t *client)
  1330. {
  1331. JSList *node;
  1332. void *status;
  1333. int rc;
  1334. rc = jack_deactivate (client);
  1335. if (rc == ESRCH) { /* already shut down? */
  1336. return rc;
  1337. }
  1338. if (client->control->type == ClientExternal) {
  1339. #if JACK_USE_MACH_THREADS
  1340. if (client->rt_thread_ok) {
  1341. // MacOSX pthread_cancel not implemented in
  1342. // Darwin 5.5, 6.4
  1343. mach_port_t machThread =
  1344. pthread_mach_thread_np (client->process_thread);
  1345. thread_terminate (machThread);
  1346. }
  1347. #endif
  1348. /* stop the thread that communicates with the jack
  1349. * server, only if it was actually running
  1350. */
  1351. if (client->thread_ok){
  1352. pthread_cancel (client->thread);
  1353. pthread_join (client->thread, &status);
  1354. }
  1355. if (client->control) {
  1356. jack_release_shm (&client->control_shm);
  1357. client->control = NULL;
  1358. }
  1359. if (client->engine) {
  1360. jack_release_shm (&client->engine_shm);
  1361. client->engine = NULL;
  1362. }
  1363. if (client->port_segment) {
  1364. jack_port_type_id_t ptid;
  1365. for (ptid = 0; ptid < client->n_port_types; ++ptid) {
  1366. jack_release_shm (&client->port_segment[ptid]);
  1367. }
  1368. free (client->port_segment);
  1369. client->port_segment = NULL;
  1370. }
  1371. if (client->graph_wait_fd) {
  1372. close (client->graph_wait_fd);
  1373. }
  1374. if (client->graph_next_fd) {
  1375. close (client->graph_next_fd);
  1376. }
  1377. close (client->event_fd);
  1378. if (shutdown (client->request_fd, SHUT_RDWR)) {
  1379. jack_error ("could not shutdown client request socket");
  1380. }
  1381. close (client->request_fd);
  1382. }
  1383. for (node = client->ports; node; node = jack_slist_next (node)) {
  1384. free (node->data);
  1385. }
  1386. jack_slist_free (client->ports);
  1387. for (node = client->ports_ext; node; node = jack_slist_next (node)) {
  1388. free (node->data);
  1389. }
  1390. jack_slist_free (client->ports_ext);
  1391. jack_client_free (client);
  1392. return rc;
  1393. }
  1394. int
  1395. jack_is_realtime (jack_client_t *client)
  1396. {
  1397. return client->engine->real_time;
  1398. }
  1399. jack_nframes_t
  1400. jack_get_buffer_size (jack_client_t *client)
  1401. {
  1402. return client->engine->buffer_size;
  1403. }
  1404. int
  1405. jack_set_buffer_size (jack_client_t *client, jack_nframes_t nframes)
  1406. {
  1407. #ifdef DO_BUFFER_RESIZE
  1408. jack_request_t req;
  1409. req.type = SetBufferSize;
  1410. req.x.nframes = nframes;
  1411. return jack_client_deliver_request (client, &req);
  1412. #else
  1413. return ENOSYS;
  1414. #endif /* DO_BUFFER_RESIZE */
  1415. }
  1416. int
  1417. jack_connect (jack_client_t *client, const char *source_port,
  1418. const char *destination_port)
  1419. {
  1420. jack_request_t req;
  1421. req.type = ConnectPorts;
  1422. snprintf (req.x.connect.source_port,
  1423. sizeof (req.x.connect.source_port), "%s", source_port);
  1424. snprintf (req.x.connect.destination_port,
  1425. sizeof (req.x.connect.destination_port),
  1426. "%s", destination_port);
  1427. return jack_client_deliver_request (client, &req);
  1428. }
  1429. int
  1430. jack_port_disconnect (jack_client_t *client, jack_port_t *port)
  1431. {
  1432. jack_request_t req;
  1433. pthread_mutex_lock (&port->connection_lock);
  1434. if (port->connections == NULL) {
  1435. pthread_mutex_unlock (&port->connection_lock);
  1436. return 0;
  1437. }
  1438. pthread_mutex_unlock (&port->connection_lock);
  1439. req.type = DisconnectPort;
  1440. req.x.port_info.port_id = port->shared->id;
  1441. return jack_client_deliver_request (client, &req);
  1442. }
  1443. int
  1444. jack_disconnect (jack_client_t *client, const char *source_port,
  1445. const char *destination_port)
  1446. {
  1447. jack_request_t req;
  1448. req.type = DisconnectPorts;
  1449. snprintf (req.x.connect.source_port,
  1450. sizeof (req.x.connect.source_port), "%s", source_port);
  1451. snprintf (req.x.connect.destination_port,
  1452. sizeof (req.x.connect.destination_port),
  1453. "%s", destination_port);
  1454. return jack_client_deliver_request (client, &req);
  1455. }
  1456. void
  1457. jack_set_error_function (void (*func) (const char *))
  1458. {
  1459. jack_error_callback = func;
  1460. }
  1461. int
  1462. jack_set_graph_order_callback (jack_client_t *client,
  1463. JackGraphOrderCallback callback, void *arg)
  1464. {
  1465. if (client->control->active) {
  1466. jack_error ("You cannot set callbacks on an active client.");
  1467. return -1;
  1468. }
  1469. client->control->graph_order = callback;
  1470. client->control->graph_order_arg = arg;
  1471. return 0;
  1472. }
  1473. int jack_set_xrun_callback (jack_client_t *client,
  1474. JackXRunCallback callback, void *arg)
  1475. {
  1476. if (client->control->active) {
  1477. jack_error ("You cannot set callbacks on an active client.");
  1478. return -1;
  1479. }
  1480. client->control->xrun = callback;
  1481. client->control->xrun_arg = arg;
  1482. return 0;
  1483. }
  1484. int
  1485. jack_set_process_callback (jack_client_t *client,
  1486. JackProcessCallback callback, void *arg)
  1487. {
  1488. if (client->control->active) {
  1489. jack_error ("You cannot set callbacks on an active client.");
  1490. return -1;
  1491. }
  1492. client->control->process_arg = arg;
  1493. client->control->process = callback;
  1494. return 0;
  1495. }
  1496. int
  1497. jack_set_thread_init_callback (jack_client_t *client,
  1498. JackThreadInitCallback callback, void *arg)
  1499. {
  1500. if (client->control->active) {
  1501. jack_error ("You cannot set callbacks on an active client.");
  1502. return -1;
  1503. }
  1504. client->control->thread_init_arg = arg;
  1505. client->control->thread_init = callback;
  1506. return 0;
  1507. }
  1508. int
  1509. jack_set_freewheel_callback (jack_client_t *client,
  1510. JackFreewheelCallback callback, void *arg)
  1511. {
  1512. if (client->control->active) {
  1513. jack_error ("You cannot set callbacks on an active client.");
  1514. return -1;
  1515. }
  1516. client->control->freewheel_arg = arg;
  1517. client->control->freewheel_cb = callback;
  1518. return 0;
  1519. }
  1520. int
  1521. jack_set_buffer_size_callback (jack_client_t *client,
  1522. JackBufferSizeCallback callback, void *arg)
  1523. {
  1524. client->control->bufsize_arg = arg;
  1525. client->control->bufsize = callback;
  1526. return 0;
  1527. }
  1528. int
  1529. jack_set_port_registration_callback(jack_client_t *client,
  1530. JackPortRegistrationCallback callback,
  1531. void *arg)
  1532. {
  1533. if (client->control->active) {
  1534. jack_error ("You cannot set callbacks on an active client.");
  1535. return -1;
  1536. }
  1537. client->control->port_register_arg = arg;
  1538. client->control->port_register = callback;
  1539. return 0;
  1540. }
  1541. int
  1542. jack_get_process_done_fd (jack_client_t *client)
  1543. {
  1544. return client->graph_next_fd;
  1545. }
  1546. void
  1547. jack_on_shutdown (jack_client_t *client, void (*function)(void *arg), void *arg)
  1548. {
  1549. client->on_shutdown = function;
  1550. client->on_shutdown_arg = arg;
  1551. }
  1552. const char **
  1553. jack_get_ports (jack_client_t *client,
  1554. const char *port_name_pattern,
  1555. const char *type_name_pattern,
  1556. unsigned long flags)
  1557. {
  1558. jack_control_t *engine;
  1559. const char **matching_ports;
  1560. unsigned long match_cnt;
  1561. jack_port_shared_t *psp;
  1562. unsigned long i;
  1563. regex_t port_regex;
  1564. regex_t type_regex;
  1565. int matching;
  1566. engine = client->engine;
  1567. if (port_name_pattern && port_name_pattern[0]) {
  1568. regcomp (&port_regex, port_name_pattern,
  1569. REG_EXTENDED|REG_NOSUB);
  1570. }
  1571. if (type_name_pattern && type_name_pattern[0]) {
  1572. regcomp (&type_regex, type_name_pattern,
  1573. REG_EXTENDED|REG_NOSUB);
  1574. }
  1575. psp = engine->ports;
  1576. match_cnt = 0;
  1577. matching_ports = (const char **)
  1578. malloc (sizeof (char *) * engine->port_max);
  1579. for (i = 0; i < engine->port_max; i++) {
  1580. matching = 1;
  1581. if (!psp[i].in_use) {
  1582. continue;
  1583. }
  1584. if (flags) {
  1585. if ((psp[i].flags & flags) != flags) {
  1586. matching = 0;
  1587. }
  1588. }
  1589. if (matching && port_name_pattern && port_name_pattern[0]) {
  1590. if (regexec (&port_regex, psp[i].name, 0, NULL, 0)) {
  1591. matching = 0;
  1592. }
  1593. }
  1594. if (matching && type_name_pattern && type_name_pattern[0]) {
  1595. jack_port_type_id_t ptid = psp[i].ptype_id;
  1596. if (regexec (&type_regex,
  1597. engine->port_types[ptid].type_name,
  1598. 0, NULL, 0)) {
  1599. matching = 0;
  1600. }
  1601. }
  1602. if (matching) {
  1603. matching_ports[match_cnt++] = psp[i].name;
  1604. }
  1605. }
  1606. if (port_name_pattern && port_name_pattern[0]) {
  1607. regfree (&port_regex);
  1608. }
  1609. if (type_name_pattern && type_name_pattern[0]) {
  1610. regfree (&type_regex);
  1611. }
  1612. matching_ports[match_cnt] = 0;
  1613. if (match_cnt == 0) {
  1614. free (matching_ports);
  1615. matching_ports = 0;
  1616. }
  1617. return matching_ports;
  1618. }
  1619. float
  1620. jack_cpu_load (jack_client_t *client)
  1621. {
  1622. return client->engine->cpu_load;
  1623. }
  1624. float
  1625. jack_get_xrun_delayed_usecs (jack_client_t *client)
  1626. {
  1627. return client->engine->xrun_delayed_usecs;
  1628. }
  1629. float
  1630. jack_get_max_delayed_usecs (jack_client_t *client)
  1631. {
  1632. return client->engine->max_delayed_usecs;
  1633. }
  1634. void
  1635. jack_reset_max_delayed_usecs (jack_client_t *client)
  1636. {
  1637. client->engine->max_delayed_usecs = 0.0f;
  1638. }
  1639. pthread_t
  1640. jack_client_thread_id (jack_client_t *client)
  1641. {
  1642. return client->thread_id;
  1643. }
  1644. int
  1645. jack_client_name_size(void)
  1646. {
  1647. return JACK_CLIENT_NAME_SIZE;
  1648. }
  1649. int
  1650. jack_port_name_size(void)
  1651. {
  1652. return JACK_PORT_NAME_SIZE;
  1653. }
  1654. int
  1655. jack_port_type_size(void)
  1656. {
  1657. return JACK_PORT_TYPE_SIZE;
  1658. }