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.

2044 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_set_freewheel (jack_client_t* client, int onoff)
  815. {
  816. jack_request_t request;
  817. int ret;
  818. /* Note: the thread that initiates and terminates freewheeling must
  819. be the one that called jack_activate(), because that is the only
  820. thread with RT-granting capabilities.
  821. XXX horrible design. The RT thread should acquire/drop/reacquire
  822. scheduling all by itself.
  823. */
  824. request.type = onoff ? FreeWheel : StopFreeWheel;
  825. if ((ret = jack_client_deliver_request (client, &request)) == 0) {
  826. if (onoff == 0 && client->engine->real_time) {
  827. /* get the relevant thread back to RT priority */
  828. #if JACK_USE_MACH_THREADS
  829. jack_acquire_real_time_scheduling (
  830. client->process_thread,
  831. client->engine->client_priority);
  832. #else
  833. jack_acquire_real_time_scheduling (
  834. client->thread,
  835. client->engine->client_priority);
  836. #endif
  837. }
  838. }
  839. return ret;
  840. }
  841. void
  842. jack_start_freewheel (jack_client_t* client)
  843. {
  844. jack_client_control_t *control = client->control;
  845. if (client->engine->real_time) {
  846. #if JACK_USE_MACH_THREADS
  847. jack_drop_real_time_scheduling (client->process_thread);
  848. #else
  849. jack_drop_real_time_scheduling (client->thread);
  850. #endif
  851. }
  852. if (control->freewheel_cb) {
  853. control->freewheel_cb (1, control->freewheel_arg);
  854. }
  855. }
  856. void
  857. jack_stop_freewheel (jack_client_t* client)
  858. {
  859. jack_client_control_t *control = client->control;
  860. if (control->freewheel_cb) {
  861. control->freewheel_cb (0, control->freewheel_arg);
  862. }
  863. }
  864. static void *
  865. jack_client_thread (void *arg)
  866. {
  867. jack_client_t *client = (jack_client_t *) arg;
  868. jack_client_control_t *control = client->control;
  869. jack_event_t event;
  870. char status = 0;
  871. char c = 0;
  872. int err = 0;
  873. pthread_mutex_lock (&client_lock);
  874. client->thread_ok = TRUE;
  875. client->thread_id = pthread_self();
  876. pthread_cond_signal (&client_ready);
  877. pthread_mutex_unlock (&client_lock);
  878. client->control->pid = getpid();
  879. client->control->pgrp = getpgrp();
  880. DEBUG ("client thread is now running");
  881. if (client->control->thread_init) {
  882. DEBUG ("calling client thread init callback");
  883. client->control->thread_init (client->control->thread_init_arg);
  884. }
  885. while (err == 0) {
  886. if (client->engine->engine_ok == 0) {
  887. if (client->on_shutdown)
  888. client->on_shutdown (client->on_shutdown_arg);
  889. else
  890. jack_error ("engine unexpectedly shutdown; "
  891. "thread exiting\n");
  892. pthread_exit (0);
  893. }
  894. DEBUG ("client polling on %s", client->pollmax == 2 ?
  895. "event_fd and graph_wait_fd..." :
  896. "event_fd only");
  897. if (poll (client->pollfd, client->pollmax, 1000) < 0) {
  898. if (errno == EINTR) {
  899. continue;
  900. }
  901. jack_error ("poll failed in client (%s)",
  902. strerror (errno));
  903. status = -1;
  904. break;
  905. }
  906. /* get an accurate timestamp on waking from poll for a
  907. * process() cycle.
  908. */
  909. if (client->graph_wait_fd >= 0
  910. && client->pollfd[WAIT_POLL_INDEX].revents & POLLIN) {
  911. control->awake_at = jack_get_microseconds();
  912. }
  913. DEBUG ("pfd[EVENT].revents = 0x%x pfd[WAIT].revents = 0x%x",
  914. client->pollfd[EVENT_POLL_INDEX].revents,
  915. client->pollfd[WAIT_POLL_INDEX].revents);
  916. pthread_testcancel();
  917. if (client->graph_wait_fd >= 0 &&
  918. (client->pollfd[WAIT_POLL_INDEX].revents & ~POLLIN)) {
  919. DEBUG ("\n\n\n\n\n\n\n\nWAITFD ERROR,"
  920. " ZOMBIE\n\n\n\n\n");
  921. /* our upstream "wait" connection
  922. closed, which either means that
  923. an intermediate client exited, or
  924. jackd exited, or jackd zombified
  925. us.
  926. we can discover the zombification
  927. via client->control->dead, but
  928. the other two possibilities are
  929. impossible to identify just from
  930. this situation. so we have to
  931. check what we are connected to,
  932. and act accordingly.
  933. */
  934. if (client->upstream_is_jackd) {
  935. DEBUG ("WE DIE\n");
  936. goto zombie;
  937. } else {
  938. DEBUG ("WE PUNT\n");
  939. /* don't poll on the wait fd
  940. * again until we get a
  941. * GraphReordered event.
  942. */
  943. client->graph_wait_fd = -1;
  944. client->pollmax = 1;
  945. }
  946. }
  947. if (client->control->dead) {
  948. goto zombie;
  949. }
  950. if (client->pollfd[EVENT_POLL_INDEX].revents & ~POLLIN) {
  951. /* jackd shutdown */
  952. goto zombie;
  953. }
  954. if (client->pollfd[EVENT_POLL_INDEX].revents & POLLIN) {
  955. DEBUG ("client receives an event, "
  956. "now reading on event fd");
  957. /* server has sent us an event. process the
  958. * event and reply */
  959. if (read (client->event_fd, &event, sizeof (event))
  960. != sizeof (event)) {
  961. jack_error ("cannot read server event (%s)",
  962. strerror (errno));
  963. err++;
  964. break;
  965. }
  966. status = 0;
  967. switch (event.type) {
  968. case PortRegistered:
  969. if (control->port_register) {
  970. control->port_register
  971. (event.x.port_id, TRUE,
  972. control->port_register_arg);
  973. }
  974. break;
  975. case PortUnregistered:
  976. if (control->port_register) {
  977. control->port_register
  978. (event.x.port_id, FALSE,
  979. control->port_register_arg);
  980. }
  981. break;
  982. case GraphReordered:
  983. status = jack_handle_reorder (client, &event);
  984. break;
  985. case PortConnected:
  986. case PortDisconnected:
  987. status = jack_client_handle_port_connection
  988. (client, &event);
  989. break;
  990. case BufferSizeChange:
  991. jack_client_invalidate_port_buffers (client);
  992. if (control->bufsize) {
  993. status = control->bufsize
  994. (control->nframes,
  995. control->bufsize_arg);
  996. }
  997. break;
  998. case SampleRateChange:
  999. if (control->srate) {
  1000. status = control->srate
  1001. (control->nframes,
  1002. control->srate_arg);
  1003. }
  1004. break;
  1005. case XRun:
  1006. if (control->xrun) {
  1007. status = control->xrun
  1008. (control->xrun_arg);
  1009. }
  1010. break;
  1011. case AttachPortSegment:
  1012. jack_attach_port_segment (client, event.y.ptid);
  1013. break;
  1014. case StartFreewheel:
  1015. jack_start_freewheel (client);
  1016. break;
  1017. case StopFreewheel:
  1018. jack_stop_freewheel (client);
  1019. break;
  1020. }
  1021. DEBUG ("client has dealt with the event, writing "
  1022. "response on event fd");
  1023. if (write (client->event_fd, &status, sizeof (status))
  1024. != sizeof (status)) {
  1025. jack_error ("cannot send event response to "
  1026. "engine (%s)", strerror (errno));
  1027. err++;
  1028. break;
  1029. }
  1030. }
  1031. if (client->pollfd[WAIT_POLL_INDEX].revents & POLLIN) {
  1032. #ifdef WITH_TIMESTAMPS
  1033. jack_reset_timestamps ();
  1034. #endif
  1035. DEBUG ("client %d signalled at %" PRIu64
  1036. ", awake for process at %" PRIu64
  1037. " (delay = %" PRIu64
  1038. " usecs) (wakeup on graph_wait_fd==%d)",
  1039. getpid(),
  1040. control->signalled_at,
  1041. control->awake_at,
  1042. control->awake_at - control->signalled_at,
  1043. client->pollfd[WAIT_POLL_INDEX].fd);
  1044. control->state = Running;
  1045. /* begin preemption checking */
  1046. CHECK_PREEMPTION (client->engine, TRUE);
  1047. if (control->sync_cb)
  1048. jack_call_sync_client (client);
  1049. if (control->process) {
  1050. if (control->process (control->nframes,
  1051. control->process_arg)
  1052. == 0) {
  1053. control->state = Finished;
  1054. }
  1055. } else {
  1056. control->state = Finished;
  1057. }
  1058. if (control->timebase_cb)
  1059. jack_call_timebase_master (client);
  1060. /* end preemption checking */
  1061. CHECK_PREEMPTION (client->engine, FALSE);
  1062. control->finished_at = jack_get_microseconds();
  1063. #ifdef WITH_TIMESTAMPS
  1064. jack_timestamp ("finished");
  1065. #endif
  1066. /* pass the execution token along */
  1067. DEBUG ("client finished processing at %" PRIu64
  1068. " (elapsed = %" PRIu64
  1069. " usecs), writing on graph_next_fd==%d",
  1070. control->finished_at,
  1071. control->finished_at - control->awake_at,
  1072. client->graph_next_fd);
  1073. if (write (client->graph_next_fd, &c, sizeof (c))
  1074. != sizeof (c)) {
  1075. jack_error ("cannot continue execution of the "
  1076. "processing graph (%s)",
  1077. strerror(errno));
  1078. err++;
  1079. break;
  1080. }
  1081. DEBUG ("client sent message to next stage by %" PRIu64
  1082. ", client reading on graph_wait_fd==%d",
  1083. jack_get_microseconds(), client->graph_wait_fd);
  1084. #ifdef WITH_TIMESTAMPS
  1085. jack_timestamp ("read pending byte from wait");
  1086. #endif
  1087. DEBUG("reading cleanup byte from pipe\n");
  1088. if ((read (client->graph_wait_fd, &c, sizeof (c))
  1089. != sizeof (c))) {
  1090. DEBUG ("WARNING: READ FAILED!");
  1091. #if 0
  1092. jack_error ("cannot complete execution of the "
  1093. "processing graph (%s)",
  1094. strerror(errno));
  1095. err++;
  1096. break;
  1097. #endif
  1098. }
  1099. /* check if we were killed during the process
  1100. * cycle (or whatever) */
  1101. if (client->control->dead) {
  1102. goto zombie;
  1103. }
  1104. DEBUG("process cycle fully complete\n");
  1105. #ifdef WITH_TIMESTAMPS
  1106. jack_timestamp ("read done");
  1107. jack_dump_timestamps (stdout);
  1108. #endif
  1109. }
  1110. }
  1111. return (void *) ((intptr_t)err);
  1112. zombie:
  1113. if (client->on_shutdown) {
  1114. jack_error ("zombified - calling shutdown handler");
  1115. client->on_shutdown (client->on_shutdown_arg);
  1116. } else {
  1117. jack_error ("zombified - exiting from JACK");
  1118. jack_client_close (client);
  1119. /* Need a fix : possibly make client crash if
  1120. * zombified without shutdown handler
  1121. */
  1122. }
  1123. pthread_exit (0);
  1124. /*NOTREACHED*/
  1125. return 0;
  1126. }
  1127. #ifdef JACK_USE_MACH_THREADS
  1128. /* real-time thread : separated from the normal client thread, it will
  1129. * communicate with the server using fast mach RPC mechanism */
  1130. static void *
  1131. jack_client_process_thread (void *arg)
  1132. {
  1133. jack_client_t *client = (jack_client_t *) arg;
  1134. jack_client_control_t *control = client->control;
  1135. int err = 0;
  1136. if (client->control->thread_init) {
  1137. /* this means that the init callback will be called twice -taybin*/
  1138. DEBUG ("calling client thread init callback");
  1139. client->control->thread_init (client->control->thread_init_arg);
  1140. }
  1141. client->control->pid = getpid();
  1142. DEBUG ("client process thread is now running");
  1143. client->rt_thread_ok = TRUE;
  1144. while (err == 0) {
  1145. if (jack_client_suspend(client) < 0) {
  1146. jack_error ("jack_client_process_thread :resume error");
  1147. goto zombie;
  1148. }
  1149. control->awake_at = jack_get_microseconds();
  1150. DEBUG ("client resumed");
  1151. control->state = Running;
  1152. if (control->sync_cb)
  1153. jack_call_sync_client (client);
  1154. if (control->process) {
  1155. if (control->process (control->nframes,
  1156. control->process_arg) == 0) {
  1157. control->state = Finished;
  1158. }
  1159. } else {
  1160. control->state = Finished;
  1161. }
  1162. if (control->timebase_cb)
  1163. jack_call_timebase_master (client);
  1164. control->finished_at = jack_get_microseconds();
  1165. #ifdef WITH_TIMESTAMPS
  1166. jack_timestamp ("finished");
  1167. #endif
  1168. DEBUG ("client finished processing at %Lu (elapsed = %f usecs)",
  1169. control->finished_at,
  1170. ((float)(control->finished_at - control->awake_at)));
  1171. /* check if we were killed during the process cycle
  1172. * (or whatever) */
  1173. if (client->control->dead) {
  1174. jack_error ("jack_client_process_thread: "
  1175. "client->control->dead");
  1176. goto zombie;
  1177. }
  1178. DEBUG("process cycle fully complete\n");
  1179. }
  1180. return (void *) ((intptr_t)err);
  1181. zombie:
  1182. jack_error ("jack_client_process_thread : zombified");
  1183. client->rt_thread_ok = FALSE;
  1184. if (client->on_shutdown) {
  1185. jack_error ("zombified - calling shutdown handler");
  1186. client->on_shutdown (client->on_shutdown_arg);
  1187. } else {
  1188. jack_error ("zombified - exiting from JACK");
  1189. /* Need a fix : possibly make client crash if
  1190. * zombified without shutdown handler */
  1191. jack_client_close (client);
  1192. }
  1193. pthread_exit (0);
  1194. /*NOTREACHED*/
  1195. return 0;
  1196. }
  1197. #endif /* JACK_USE_MACH_THREADS */
  1198. static int
  1199. jack_start_thread (jack_client_t *client)
  1200. {
  1201. if (client->engine->real_time) {
  1202. #ifdef USE_MLOCK
  1203. if (client->engine->do_mlock
  1204. && (mlockall (MCL_CURRENT | MCL_FUTURE) != 0)) {
  1205. jack_error ("cannot lock down memory for RT thread "
  1206. "(%s)", strerror (errno));
  1207. #ifdef ENSURE_MLOCK
  1208. return -1;
  1209. #endif /* ENSURE_MLOCK */
  1210. }
  1211. if (client->engine->do_munlock) {
  1212. cleanup_mlock ();
  1213. }
  1214. #endif /* USE_MLOCK */
  1215. }
  1216. #ifdef JACK_USE_MACH_THREADS
  1217. /* Stephane Letz : letz@grame.fr
  1218. On MacOSX, the normal thread does not need to be real-time.
  1219. */
  1220. if (jack_create_thread (&client->thread,
  1221. client->engine->client_priority,
  1222. 0,
  1223. jack_client_thread, client)) {
  1224. return -1;
  1225. }
  1226. #else
  1227. if (jack_create_thread (&client->thread,
  1228. client->engine->client_priority,
  1229. client->engine->real_time,
  1230. jack_client_thread, client)) {
  1231. return -1;
  1232. }
  1233. #endif
  1234. #ifdef JACK_USE_MACH_THREADS
  1235. /* a secondary thread that runs the process callback and uses
  1236. ultra-fast Mach primitives for inter-thread signalling.
  1237. XXX in a properly structured JACK, there would be no
  1238. need for this, because we would have client wake up
  1239. methods that encapsulated the underlying mechanism
  1240. used.
  1241. */
  1242. if (jack_create_thread(&client->process_thread,
  1243. client->engine->client_priority,
  1244. client->engine->real_time,
  1245. jack_client_process_thread, client)) {
  1246. return -1;
  1247. }
  1248. #endif /* JACK_USE_MACH_THREADS */
  1249. return 0;
  1250. }
  1251. int
  1252. jack_activate (jack_client_t *client)
  1253. {
  1254. jack_request_t req;
  1255. /* we need to scribble on our stack to ensure that its memory
  1256. * pages are actually mapped (more important for mlockall(2)
  1257. * usage in jack_start_thread())
  1258. */
  1259. char buf[JACK_THREAD_STACK_TOUCH];
  1260. int i;
  1261. for (i = 0; i < JACK_THREAD_STACK_TOUCH; i++) {
  1262. buf[i] = (char) (i & 0xff);
  1263. }
  1264. if (client->control->type == ClientInternal ||
  1265. client->control->type == ClientDriver) {
  1266. goto startit;
  1267. }
  1268. /* get the pid of the client process to pass it to engine */
  1269. client->control->pid = getpid ();
  1270. #ifdef USE_CAPABILITIES
  1271. if (client->engine->has_capabilities != 0 &&
  1272. client->control->pid != 0 && client->engine->real_time != 0) {
  1273. /* we need to ask the engine for realtime capabilities
  1274. before trying to start the realtime thread
  1275. */
  1276. req.type = SetClientCapabilities;
  1277. req.x.client_id = client->control->id;
  1278. jack_client_deliver_request (client, &req);
  1279. if (req.status) {
  1280. /* what to do? engine is running realtime, it
  1281. is using capabilities and has them
  1282. (otherwise we would not get an error
  1283. return) but for some reason it could not
  1284. give the client the required capabilities,
  1285. so for now downgrade the client so that it
  1286. still runs, albeit non-realtime - nando
  1287. */
  1288. jack_error ("could not receive realtime capabilities, "
  1289. "client will run non-realtime");
  1290. /* XXX wrong, this is a property of the engine
  1291. client->engine->real_time = 0;
  1292. */
  1293. }
  1294. }
  1295. #endif /* USE_CAPABILITIES */
  1296. if (client->first_active) {
  1297. pthread_mutex_init (&client_lock, NULL);
  1298. pthread_cond_init (&client_ready, NULL);
  1299. pthread_mutex_lock (&client_lock);
  1300. if (jack_start_thread (client)) {
  1301. pthread_mutex_unlock (&client_lock);
  1302. return -1;
  1303. }
  1304. pthread_cond_wait (&client_ready, &client_lock);
  1305. pthread_mutex_unlock (&client_lock);
  1306. if (!client->thread_ok) {
  1307. jack_error ("could not start client thread");
  1308. return -1;
  1309. }
  1310. client->first_active = FALSE;
  1311. }
  1312. startit:
  1313. req.type = ActivateClient;
  1314. req.x.client_id = client->control->id;
  1315. return jack_client_deliver_request (client, &req);
  1316. }
  1317. int
  1318. jack_deactivate (jack_client_t *client)
  1319. {
  1320. jack_request_t req;
  1321. int rc = ESRCH; /* already shut down */
  1322. if (client && client->control) { /* not shut down? */
  1323. rc = 0;
  1324. if (client->control->active) { /* still active? */
  1325. req.type = DeactivateClient;
  1326. req.x.client_id = client->control->id;
  1327. rc = jack_client_deliver_request (client, &req);
  1328. }
  1329. }
  1330. return rc;
  1331. }
  1332. int
  1333. jack_client_close (jack_client_t *client)
  1334. {
  1335. JSList *node;
  1336. void *status;
  1337. int rc;
  1338. rc = jack_deactivate (client);
  1339. if (rc == ESRCH) { /* already shut down? */
  1340. return rc;
  1341. }
  1342. if (client->control->type == ClientExternal) {
  1343. #if JACK_USE_MACH_THREADS
  1344. if (client->rt_thread_ok) {
  1345. // MacOSX pthread_cancel not implemented in
  1346. // Darwin 5.5, 6.4
  1347. mach_port_t machThread =
  1348. pthread_mach_thread_np (client->process_thread);
  1349. thread_terminate (machThread);
  1350. }
  1351. #endif
  1352. /* stop the thread that communicates with the jack
  1353. * server, only if it was actually running
  1354. */
  1355. if (client->thread_ok){
  1356. pthread_cancel (client->thread);
  1357. pthread_join (client->thread, &status);
  1358. }
  1359. if (client->control) {
  1360. jack_release_shm (&client->control_shm);
  1361. client->control = NULL;
  1362. }
  1363. if (client->engine) {
  1364. jack_release_shm (&client->engine_shm);
  1365. client->engine = NULL;
  1366. }
  1367. if (client->port_segment) {
  1368. jack_port_type_id_t ptid;
  1369. for (ptid = 0; ptid < client->n_port_types; ++ptid) {
  1370. jack_release_shm (&client->port_segment[ptid]);
  1371. }
  1372. free (client->port_segment);
  1373. client->port_segment = NULL;
  1374. }
  1375. if (client->graph_wait_fd) {
  1376. close (client->graph_wait_fd);
  1377. }
  1378. if (client->graph_next_fd) {
  1379. close (client->graph_next_fd);
  1380. }
  1381. close (client->event_fd);
  1382. if (shutdown (client->request_fd, SHUT_RDWR)) {
  1383. jack_error ("could not shutdown client request socket");
  1384. }
  1385. close (client->request_fd);
  1386. }
  1387. for (node = client->ports; node; node = jack_slist_next (node)) {
  1388. free (node->data);
  1389. }
  1390. jack_slist_free (client->ports);
  1391. for (node = client->ports_ext; node; node = jack_slist_next (node)) {
  1392. free (node->data);
  1393. }
  1394. jack_slist_free (client->ports_ext);
  1395. jack_client_free (client);
  1396. return rc;
  1397. }
  1398. int
  1399. jack_is_realtime (jack_client_t *client)
  1400. {
  1401. return client->engine->real_time;
  1402. }
  1403. jack_nframes_t
  1404. jack_get_buffer_size (jack_client_t *client)
  1405. {
  1406. return client->engine->buffer_size;
  1407. }
  1408. int
  1409. jack_set_buffer_size (jack_client_t *client, jack_nframes_t nframes)
  1410. {
  1411. #ifdef DO_BUFFER_RESIZE
  1412. jack_request_t req;
  1413. req.type = SetBufferSize;
  1414. req.x.nframes = nframes;
  1415. return jack_client_deliver_request (client, &req);
  1416. #else
  1417. return ENOSYS;
  1418. #endif /* DO_BUFFER_RESIZE */
  1419. }
  1420. int
  1421. jack_connect (jack_client_t *client, const char *source_port,
  1422. const char *destination_port)
  1423. {
  1424. jack_request_t req;
  1425. req.type = ConnectPorts;
  1426. snprintf (req.x.connect.source_port,
  1427. sizeof (req.x.connect.source_port), "%s", source_port);
  1428. snprintf (req.x.connect.destination_port,
  1429. sizeof (req.x.connect.destination_port),
  1430. "%s", destination_port);
  1431. return jack_client_deliver_request (client, &req);
  1432. }
  1433. int
  1434. jack_port_disconnect (jack_client_t *client, jack_port_t *port)
  1435. {
  1436. jack_request_t req;
  1437. pthread_mutex_lock (&port->connection_lock);
  1438. if (port->connections == NULL) {
  1439. pthread_mutex_unlock (&port->connection_lock);
  1440. return 0;
  1441. }
  1442. pthread_mutex_unlock (&port->connection_lock);
  1443. req.type = DisconnectPort;
  1444. req.x.port_info.port_id = port->shared->id;
  1445. return jack_client_deliver_request (client, &req);
  1446. }
  1447. int
  1448. jack_disconnect (jack_client_t *client, const char *source_port,
  1449. const char *destination_port)
  1450. {
  1451. jack_request_t req;
  1452. req.type = DisconnectPorts;
  1453. snprintf (req.x.connect.source_port,
  1454. sizeof (req.x.connect.source_port), "%s", source_port);
  1455. snprintf (req.x.connect.destination_port,
  1456. sizeof (req.x.connect.destination_port),
  1457. "%s", destination_port);
  1458. return jack_client_deliver_request (client, &req);
  1459. }
  1460. void
  1461. jack_set_error_function (void (*func) (const char *))
  1462. {
  1463. jack_error_callback = func;
  1464. }
  1465. int
  1466. jack_set_graph_order_callback (jack_client_t *client,
  1467. JackGraphOrderCallback callback, void *arg)
  1468. {
  1469. if (client->control->active) {
  1470. jack_error ("You cannot set callbacks on an active client.");
  1471. return -1;
  1472. }
  1473. client->control->graph_order = callback;
  1474. client->control->graph_order_arg = arg;
  1475. return 0;
  1476. }
  1477. int jack_set_xrun_callback (jack_client_t *client,
  1478. JackXRunCallback callback, void *arg)
  1479. {
  1480. if (client->control->active) {
  1481. jack_error ("You cannot set callbacks on an active client.");
  1482. return -1;
  1483. }
  1484. client->control->xrun = callback;
  1485. client->control->xrun_arg = arg;
  1486. return 0;
  1487. }
  1488. int
  1489. jack_set_process_callback (jack_client_t *client,
  1490. JackProcessCallback callback, void *arg)
  1491. {
  1492. if (client->control->active) {
  1493. jack_error ("You cannot set callbacks on an active client.");
  1494. return -1;
  1495. }
  1496. client->control->process_arg = arg;
  1497. client->control->process = callback;
  1498. return 0;
  1499. }
  1500. int
  1501. jack_set_thread_init_callback (jack_client_t *client,
  1502. JackThreadInitCallback callback, void *arg)
  1503. {
  1504. if (client->control->active) {
  1505. jack_error ("You cannot set callbacks on an active client.");
  1506. return -1;
  1507. }
  1508. client->control->thread_init_arg = arg;
  1509. client->control->thread_init = callback;
  1510. return 0;
  1511. }
  1512. int
  1513. jack_set_freewheel_callback (jack_client_t *client,
  1514. JackFreewheelCallback callback, void *arg)
  1515. {
  1516. if (client->control->active) {
  1517. jack_error ("You cannot set callbacks on an active client.");
  1518. return -1;
  1519. }
  1520. client->control->freewheel_arg = arg;
  1521. client->control->freewheel_cb = callback;
  1522. return 0;
  1523. }
  1524. int
  1525. jack_set_buffer_size_callback (jack_client_t *client,
  1526. JackBufferSizeCallback callback, void *arg)
  1527. {
  1528. client->control->bufsize_arg = arg;
  1529. client->control->bufsize = callback;
  1530. return 0;
  1531. }
  1532. int
  1533. jack_set_port_registration_callback(jack_client_t *client,
  1534. JackPortRegistrationCallback callback,
  1535. void *arg)
  1536. {
  1537. if (client->control->active) {
  1538. jack_error ("You cannot set callbacks on an active client.");
  1539. return -1;
  1540. }
  1541. client->control->port_register_arg = arg;
  1542. client->control->port_register = callback;
  1543. return 0;
  1544. }
  1545. int
  1546. jack_get_process_done_fd (jack_client_t *client)
  1547. {
  1548. return client->graph_next_fd;
  1549. }
  1550. void
  1551. jack_on_shutdown (jack_client_t *client, void (*function)(void *arg), void *arg)
  1552. {
  1553. client->on_shutdown = function;
  1554. client->on_shutdown_arg = arg;
  1555. }
  1556. const char **
  1557. jack_get_ports (jack_client_t *client,
  1558. const char *port_name_pattern,
  1559. const char *type_name_pattern,
  1560. unsigned long flags)
  1561. {
  1562. jack_control_t *engine;
  1563. const char **matching_ports;
  1564. unsigned long match_cnt;
  1565. jack_port_shared_t *psp;
  1566. unsigned long i;
  1567. regex_t port_regex;
  1568. regex_t type_regex;
  1569. int matching;
  1570. engine = client->engine;
  1571. if (port_name_pattern && port_name_pattern[0]) {
  1572. regcomp (&port_regex, port_name_pattern,
  1573. REG_EXTENDED|REG_NOSUB);
  1574. }
  1575. if (type_name_pattern && type_name_pattern[0]) {
  1576. regcomp (&type_regex, type_name_pattern,
  1577. REG_EXTENDED|REG_NOSUB);
  1578. }
  1579. psp = engine->ports;
  1580. match_cnt = 0;
  1581. matching_ports = (const char **)
  1582. malloc (sizeof (char *) * engine->port_max);
  1583. for (i = 0; i < engine->port_max; i++) {
  1584. matching = 1;
  1585. if (!psp[i].in_use) {
  1586. continue;
  1587. }
  1588. if (flags) {
  1589. if ((psp[i].flags & flags) != flags) {
  1590. matching = 0;
  1591. }
  1592. }
  1593. if (matching && port_name_pattern && port_name_pattern[0]) {
  1594. if (regexec (&port_regex, psp[i].name, 0, NULL, 0)) {
  1595. matching = 0;
  1596. }
  1597. }
  1598. if (matching && type_name_pattern && type_name_pattern[0]) {
  1599. jack_port_type_id_t ptid = psp[i].ptype_id;
  1600. if (regexec (&type_regex,
  1601. engine->port_types[ptid].type_name,
  1602. 0, NULL, 0)) {
  1603. matching = 0;
  1604. }
  1605. }
  1606. if (matching) {
  1607. matching_ports[match_cnt++] = psp[i].name;
  1608. }
  1609. }
  1610. if (port_name_pattern && port_name_pattern[0]) {
  1611. regfree (&port_regex);
  1612. }
  1613. if (type_name_pattern && type_name_pattern[0]) {
  1614. regfree (&type_regex);
  1615. }
  1616. matching_ports[match_cnt] = 0;
  1617. if (match_cnt == 0) {
  1618. free (matching_ports);
  1619. matching_ports = 0;
  1620. }
  1621. return matching_ports;
  1622. }
  1623. float
  1624. jack_cpu_load (jack_client_t *client)
  1625. {
  1626. return client->engine->cpu_load;
  1627. }
  1628. float
  1629. jack_get_xrun_delayed_usecs (jack_client_t *client)
  1630. {
  1631. return client->engine->xrun_delayed_usecs;
  1632. }
  1633. float
  1634. jack_get_max_delayed_usecs (jack_client_t *client)
  1635. {
  1636. return client->engine->max_delayed_usecs;
  1637. }
  1638. void
  1639. jack_reset_max_delayed_usecs (jack_client_t *client)
  1640. {
  1641. client->engine->max_delayed_usecs = 0.0f;
  1642. }
  1643. pthread_t
  1644. jack_client_thread_id (jack_client_t *client)
  1645. {
  1646. return client->thread_id;
  1647. }
  1648. int
  1649. jack_client_name_size(void)
  1650. {
  1651. return JACK_CLIENT_NAME_SIZE;
  1652. }
  1653. int
  1654. jack_port_name_size(void)
  1655. {
  1656. return JACK_PORT_NAME_SIZE;
  1657. }
  1658. int
  1659. jack_port_type_size(void)
  1660. {
  1661. return JACK_PORT_TYPE_SIZE;
  1662. }