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.

2106 lines
48KB

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