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.

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