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.

2343 lines
53KB

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