jack1 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

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