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.

1962 lines
45KB

  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. static void
  319. _start_server (void)
  320. {
  321. FILE* fp = 0;
  322. char filename[255];
  323. char arguments[255];
  324. char buffer[255];
  325. char* command = 0;
  326. size_t pos = 0;
  327. size_t result = 0;
  328. char** argv = 0;
  329. int i = 0;
  330. int good = 0;
  331. int ret;
  332. snprintf(filename, 255, "%s/.jackdrc", getenv("HOME"));
  333. fp = fopen(filename, "r");
  334. if (!fp) {
  335. fp = fopen("/etc/jackd.conf", "r");
  336. }
  337. if (fp) {
  338. arguments[0] = '\0';
  339. ret = fscanf(fp, "%s", buffer);
  340. while(ret != 0 && ret != EOF) {
  341. strcat(arguments, buffer);
  342. strcat(arguments, " ");
  343. ret = fscanf(fp, "%s", buffer);
  344. }
  345. if (strlen(arguments) > 0) {
  346. good = 1;
  347. }
  348. }
  349. if (!good) {
  350. #if defined(USE_CAPABILITIES)
  351. command = JACK_LOCATION "/jackstart";
  352. strncpy(arguments, JACK_LOCATION "/jackstart -T -R -d"
  353. JACK_DEFAULT_DRIVER " -p 512", 255);
  354. #else /* !USE_CAPABILITIES */
  355. command = JACK_LOCATION "/jackd";
  356. strncpy(arguments, JACK_LOCATION "/jackd -T -d"
  357. JACK_DEFAULT_DRIVER, 255);
  358. #endif /* USE_CAPABILITIES */
  359. } else {
  360. result = strcspn(arguments, " ");
  361. command = (char*)malloc(result+1);
  362. strncpy(command, arguments, result);
  363. command[result] = '\0';
  364. }
  365. argv = (char**)malloc(255);
  366. while(1) {
  367. result = strcspn(arguments+pos, " ");
  368. if (result == 0) {
  369. break;
  370. }
  371. argv[i] = (char*)malloc(result+1);
  372. strncpy(argv[i], arguments+pos, result);
  373. argv[i][result] = '\0';
  374. pos += result+1;
  375. ++i;
  376. }
  377. argv[i] = 0;
  378. execv (command, argv);
  379. /* If execv() succeeds, it does not return. There's no point
  380. * in calling jack_error() here in the child process. */
  381. perror ("exec of JACK server failed");
  382. }
  383. int
  384. start_server (void)
  385. {
  386. pid_t pid1, pid2;
  387. if (getenv("JACK_NO_START_SERVER")) {
  388. return 1;
  389. }
  390. switch (pid1 = fork()) {
  391. case -1:
  392. return 1;
  393. case 0:
  394. switch (pid2 = fork()) {
  395. case -1:
  396. return 1;
  397. case 0:
  398. _start_server();
  399. }
  400. _exit(0);
  401. }
  402. return 0;
  403. }
  404. static int
  405. jack_request_client (ClientType type, const char* client_name,
  406. const char* so_name, const char* so_data,
  407. jack_client_connect_result_t *res, int *req_fd)
  408. {
  409. jack_client_connect_request_t req;
  410. *req_fd = -1;
  411. memset (&req, 0, sizeof (req));
  412. if (strlen (client_name) >= sizeof (req.name)) {
  413. jack_error ("\"%s\" is too long to be used as a JACK client"
  414. " name.\n"
  415. "Please use %lu characters or less.",
  416. client_name, sizeof (req.name));
  417. return -1;
  418. }
  419. if (strlen (so_name) > sizeof (req.object_path) - 1) {
  420. jack_error ("\"%s\" is too long to be used as a JACK shared"
  421. " object name.\n"
  422. "Please use %lu characters or less.",
  423. so_name, sizeof (req.object_path) - 1);
  424. return -1;
  425. }
  426. if (strlen (so_data) > sizeof (req.object_data) - 1) {
  427. jack_error ("\"%s\" is too long to be used as a JACK shared"
  428. " object data string.\n"
  429. "Please use %lu characters or less.",
  430. so_data, sizeof (req.object_data) - 1);
  431. return -1;
  432. }
  433. if ((*req_fd = server_connect (0)) < 0) {
  434. if (start_server() == 0) {
  435. sleep(2);
  436. if ((*req_fd = server_connect (0)) < 0) {
  437. goto fail;
  438. }
  439. } else {
  440. goto fail;
  441. }
  442. }
  443. req.load = TRUE;
  444. req.type = type;
  445. snprintf (req.name, sizeof (req.name), "%s", client_name);
  446. snprintf (req.object_path, sizeof (req.object_path), "%s", so_name);
  447. snprintf (req.object_data, sizeof (req.object_data), "%s", so_data);
  448. if (write (*req_fd, &req, sizeof (req)) != sizeof (req)) {
  449. jack_error ("cannot send request to jack server (%s)",
  450. strerror (errno));
  451. goto fail;
  452. }
  453. if (read (*req_fd, res, sizeof (*res)) != sizeof (*res)) {
  454. if (errno == 0) {
  455. /* server shut the socket */
  456. jack_error ("could not attach as client "
  457. "(duplicate client name?)");
  458. goto fail;
  459. }
  460. jack_error ("cannot read response from jack server (%s)",
  461. strerror (errno));
  462. goto fail;
  463. }
  464. if (res->status) {
  465. jack_error ("could not attach as client "
  466. "(duplicate client name?)");
  467. goto fail;
  468. }
  469. if (res->protocol_v != jack_protocol_version){
  470. jack_error ("application linked against incompatible libjack"
  471. " version.");
  472. goto fail;
  473. }
  474. switch (type) {
  475. case ClientDriver:
  476. case ClientInternal:
  477. close (*req_fd);
  478. *req_fd = -1;
  479. break;
  480. default:
  481. break;
  482. }
  483. return 0;
  484. fail:
  485. if (*req_fd >= 0) {
  486. close (*req_fd);
  487. *req_fd = -1;
  488. }
  489. return -1;
  490. }
  491. int
  492. jack_attach_port_segment (jack_client_t *client, jack_port_type_id_t ptid)
  493. {
  494. /* Lookup, attach and register the port/buffer segments in use
  495. * right now.
  496. */
  497. if (client->control->type != ClientExternal) {
  498. jack_error("Only external clients need attach port segments");
  499. abort();
  500. }
  501. /* make sure we have space to store the port
  502. segment information.
  503. */
  504. if (ptid >= client->n_port_types) {
  505. client->port_segment = (jack_shm_info_t*)
  506. realloc (client->port_segment,
  507. sizeof (jack_shm_info_t) * (ptid+1));
  508. memset (&client->port_segment[client->n_port_types],
  509. 0,
  510. sizeof (jack_shm_info_t) *
  511. (ptid - client->n_port_types));
  512. client->n_port_types = ptid + 1;
  513. } else {
  514. /* release any previous segment */
  515. #if JACK_USE_MACH_THREADS
  516. /* Stephane Letz : letz@grame.fr
  517. Need a fix : this crash on MacOSX : temporary removed
  518. jack_release_shm (&client->port_segment[ptid]);
  519. */
  520. #else
  521. jack_release_shm (&client->port_segment[ptid]);
  522. #endif
  523. }
  524. /* get the index into the shm registry */
  525. client->port_segment[ptid].index =
  526. client->engine->port_types[ptid].shm_registry_index;
  527. /* attach the relevant segment */
  528. if (jack_attach_shm (&client->port_segment[ptid])) {
  529. jack_error ("cannot attach port segment shared memory"
  530. " (%s)", strerror (errno));
  531. return -1;
  532. }
  533. /* The first chunk of the audio port segment will be set by
  534. * the engine to be a zero-filled buffer. This hasn't been
  535. * done yet, but it will happen before the process cycle
  536. * (re)starts.
  537. */
  538. if (ptid == JACK_AUDIO_PORT_TYPE) {
  539. jack_zero_filled_buffer =
  540. jack_shm_addr (&client->port_segment[ptid]);
  541. }
  542. return 0;
  543. }
  544. jack_client_t *
  545. jack_client_new (const char *client_name)
  546. {
  547. int req_fd = -1;
  548. int ev_fd = -1;
  549. jack_client_connect_result_t res;
  550. jack_client_t *client;
  551. jack_port_type_id_t ptid;
  552. /* external clients need this initialized; internal clients
  553. will use the setup in the server's address space.
  554. */
  555. jack_init_time ();
  556. if (jack_initialize_shm ()) {
  557. jack_error ("Unable to initialize shared memory.");
  558. return NULL;
  559. }
  560. if (jack_request_client (ClientExternal, client_name, "", "",
  561. &res, &req_fd)) {
  562. return NULL;
  563. }
  564. client = jack_client_alloc ();
  565. strcpy (client->fifo_prefix, res.fifo_prefix);
  566. client->request_fd = req_fd;
  567. client->pollfd[0].events = POLLIN|POLLERR|POLLHUP|POLLNVAL;
  568. client->pollfd[1].events = POLLIN|POLLERR|POLLHUP|POLLNVAL;
  569. /* attach the engine control/info block */
  570. client->engine_shm = res.engine_shm;
  571. if (jack_attach_shm (&client->engine_shm)) {
  572. jack_error ("cannot attached engine control shared memory"
  573. " segment");
  574. goto fail;
  575. }
  576. client->engine = (jack_control_t *) jack_shm_addr (&client->engine_shm);
  577. /* now attach the client control block */
  578. client->control_shm = res.client_shm;
  579. if (jack_attach_shm (&client->control_shm)) {
  580. jack_error ("cannot attached client control shared memory"
  581. " segment");
  582. goto fail;
  583. }
  584. client->control = (jack_client_control_t *)
  585. jack_shm_addr (&client->control_shm);
  586. /* nobody else needs to access this shared memory any more, so
  587. destroy it. because we have our own attachment to it, it won't
  588. vanish till we exit (and release it).
  589. */
  590. jack_destroy_shm (&client->control_shm);
  591. client->n_port_types = client->engine->n_port_types;
  592. client->port_segment = (jack_shm_info_t *)
  593. malloc (sizeof (jack_shm_info_t) * client->n_port_types);
  594. for (ptid = 0; ptid < client->n_port_types; ++ptid) {
  595. client->port_segment[ptid].index =
  596. client->engine->port_types[ptid].shm_registry_index;
  597. jack_attach_port_segment (client, ptid);
  598. }
  599. /* set up the client so that it does the right thing for an
  600. * external client
  601. */
  602. client->control->deliver_request = oop_client_deliver_request;
  603. client->control->deliver_arg = client;
  604. if ((ev_fd = server_event_connect (client)) < 0) {
  605. goto fail;
  606. }
  607. client->event_fd = ev_fd;
  608. #ifdef JACK_USE_MACH_THREADS
  609. /* specific resources for server/client real-time thread
  610. * communication */
  611. client->clienttask = mach_task_self();
  612. if (task_get_bootstrap_port(client->clienttask, &client->bp)){
  613. jack_error ("Can't find bootstrap port");
  614. goto fail;
  615. }
  616. if (allocate_mach_clientport(client, res.portnum) < 0) {
  617. jack_error("Can't allocate mach port");
  618. goto fail;
  619. };
  620. #endif /* JACK_USE_MACH_THREADS */
  621. return client;
  622. fail:
  623. if (client->engine) {
  624. jack_release_shm (&client->engine_shm);
  625. client->engine = 0;
  626. }
  627. if (client->control) {
  628. jack_release_shm (&client->control_shm);
  629. client->control = 0;
  630. }
  631. if (req_fd >= 0) {
  632. close (req_fd);
  633. }
  634. if (ev_fd >= 0) {
  635. close (ev_fd);
  636. }
  637. return 0;
  638. }
  639. int
  640. jack_internal_client_new (const char *client_name, const char *so_name, const char *so_data)
  641. {
  642. jack_client_connect_result_t res;
  643. int req_fd;
  644. return jack_request_client (ClientInternal, client_name, so_name, so_data, &res, &req_fd);
  645. }
  646. void
  647. jack_internal_client_close (const char *client_name)
  648. {
  649. jack_client_connect_request_t req;
  650. int fd;
  651. req.load = FALSE;
  652. snprintf (req.name, sizeof (req.name), "%s", client_name);
  653. if ((fd = server_connect (0)) < 0) {
  654. return;
  655. }
  656. if (write (fd, &req, sizeof (req)) != sizeof(req)) {
  657. jack_error ("cannot deliver ClientUnload request to JACK server.");
  658. }
  659. /* no response to this request */
  660. close (fd);
  661. return;
  662. }
  663. #if JACK_USE_MACH_THREADS
  664. int
  665. jack_drop_real_time_scheduling (pthread_t thread)
  666. {
  667. setThreadToPriority(thread, 31, false, 10000000);
  668. return 0;
  669. }
  670. int
  671. jack_acquire_real_time_scheduling (pthread_t thread, int priority) //priority is unused
  672. {
  673. setThreadToPriority(thread, 96, true, 10000000);
  674. return 0;
  675. }
  676. #else
  677. int
  678. jack_drop_real_time_scheduling (pthread_t thread)
  679. {
  680. struct sched_param rtparam;
  681. int x;
  682. memset (&rtparam, 0, sizeof (rtparam));
  683. rtparam.sched_priority = 0;
  684. if ((x = pthread_setschedparam (thread, SCHED_OTHER, &rtparam)) != 0) {
  685. jack_error ("cannot switch to normal scheduling priority(%s)\n", strerror (errno));
  686. return -1;
  687. }
  688. return 0;
  689. }
  690. int
  691. jack_acquire_real_time_scheduling (pthread_t thread, int priority)
  692. {
  693. struct sched_param rtparam;
  694. int x;
  695. memset (&rtparam, 0, sizeof (rtparam));
  696. rtparam.sched_priority = priority;
  697. if ((x = pthread_setschedparam (thread, SCHED_FIFO, &rtparam)) != 0) {
  698. jack_error ("cannot use real-time scheduling (FIFO/%d) "
  699. "(%d: %s)", rtparam.sched_priority, x,
  700. strerror (x));
  701. return -1;
  702. }
  703. return 0;
  704. }
  705. #endif
  706. int
  707. jack_set_freewheel (jack_client_t* client, int onoff)
  708. {
  709. jack_request_t request;
  710. request.type = onoff ? FreeWheel : StopFreeWheel;
  711. return jack_client_deliver_request (client, &request);
  712. }
  713. void
  714. jack_start_freewheel (jack_client_t* client)
  715. {
  716. jack_client_control_t *control = client->control;
  717. if (client->engine->real_time) {
  718. #if JACK_USE_MACH_THREADS
  719. jack_drop_real_time_scheduling (client->process_thread);
  720. #else
  721. jack_drop_real_time_scheduling (client->thread);
  722. #endif
  723. }
  724. if (control->freewheel_cb) {
  725. control->freewheel_cb (1, control->freewheel_arg);
  726. }
  727. }
  728. void
  729. jack_stop_freewheel (jack_client_t* client)
  730. {
  731. jack_client_control_t *control = client->control;
  732. if (control->freewheel_cb) {
  733. control->freewheel_cb (0, control->freewheel_arg);
  734. }
  735. if (client->engine->real_time) {
  736. #if JACK_USE_MACH_THREADS
  737. jack_acquire_real_time_scheduling (client->process_thread,
  738. client->engine->client_priority);
  739. #else
  740. jack_acquire_real_time_scheduling (client->thread,
  741. client->engine->client_priority);
  742. #endif
  743. }
  744. }
  745. static void *
  746. jack_client_thread (void *arg)
  747. {
  748. jack_client_t *client = (jack_client_t *) arg;
  749. jack_client_control_t *control = client->control;
  750. jack_event_t event;
  751. char status = 0;
  752. char c;
  753. int err = 0;
  754. pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  755. pthread_mutex_lock (&client_lock);
  756. client->thread_ok = TRUE;
  757. client->thread_id = pthread_self();
  758. pthread_cond_signal (&client_ready);
  759. pthread_mutex_unlock (&client_lock);
  760. client->control->pid = getpid();
  761. client->control->pgrp = getpgrp();
  762. DEBUG ("client thread is now running");
  763. if (client->control->thread_init) {
  764. DEBUG ("calling client thread init callback");
  765. client->control->thread_init (client->control->thread_init_arg);
  766. }
  767. while (err == 0) {
  768. if (client->engine->engine_ok == 0) {
  769. if (client->on_shutdown)
  770. client->on_shutdown (client->on_shutdown_arg);
  771. else
  772. jack_error ("engine unexpectedly shutdown; "
  773. "thread exiting\n");
  774. pthread_exit (0);
  775. }
  776. DEBUG ("client polling on event_fd and graph_wait_fd...");
  777. if (poll (client->pollfd, client->pollmax, 1000) < 0) {
  778. if (errno == EINTR) {
  779. continue;
  780. }
  781. jack_error ("poll failed in client (%s)",
  782. strerror (errno));
  783. status = -1;
  784. break;
  785. }
  786. /* get an accurate timestamp on waking from poll for a
  787. * process() cycle.
  788. */
  789. if (client->pollfd[1].revents & POLLIN) {
  790. control->awake_at = jack_get_microseconds();
  791. }
  792. DEBUG ("pfd[0].revents = 0x%x pfd[1].revents = 0x%x",
  793. client->pollfd[0].revents,
  794. client->pollfd[1].revents);
  795. pthread_testcancel();
  796. if ((client->pollfd[0].revents & ~POLLIN) ||
  797. client->control->dead) {
  798. goto zombie;
  799. }
  800. if (client->pollfd[0].revents & POLLIN) {
  801. DEBUG ("client receives an event, "
  802. "now reading on event fd");
  803. /* server has sent us an event. process the
  804. * event and reply */
  805. if (read (client->event_fd, &event, sizeof (event))
  806. != sizeof (event)) {
  807. jack_error ("cannot read server event (%s)",
  808. strerror (errno));
  809. err++;
  810. break;
  811. }
  812. status = 0;
  813. switch (event.type) {
  814. case PortRegistered:
  815. if (control->port_register) {
  816. control->port_register
  817. (event.x.port_id, TRUE,
  818. control->port_register_arg);
  819. }
  820. break;
  821. case PortUnregistered:
  822. if (control->port_register) {
  823. control->port_register
  824. (event.x.port_id, FALSE,
  825. control->port_register_arg);
  826. }
  827. break;
  828. case GraphReordered:
  829. status = jack_handle_reorder (client, &event);
  830. break;
  831. case PortConnected:
  832. case PortDisconnected:
  833. status = jack_client_handle_port_connection
  834. (client, &event);
  835. break;
  836. case BufferSizeChange:
  837. jack_client_invalidate_port_buffers (client);
  838. if (control->bufsize) {
  839. status = control->bufsize
  840. (control->nframes,
  841. control->bufsize_arg);
  842. }
  843. break;
  844. case SampleRateChange:
  845. if (control->srate) {
  846. status = control->srate
  847. (control->nframes,
  848. control->srate_arg);
  849. }
  850. break;
  851. case XRun:
  852. if (control->xrun) {
  853. status = control->xrun
  854. (control->xrun_arg);
  855. }
  856. break;
  857. case AttachPortSegment:
  858. jack_attach_port_segment (client, event.y.ptid);
  859. break;
  860. case StartFreewheel:
  861. jack_start_freewheel (client);
  862. break;
  863. case StopFreewheel:
  864. jack_stop_freewheel (client);
  865. break;
  866. }
  867. DEBUG ("client has dealt with the event, writing "
  868. "response on event fd");
  869. if (write (client->event_fd, &status, sizeof (status))
  870. != sizeof (status)) {
  871. jack_error ("cannot send event response to "
  872. "engine (%s)", strerror (errno));
  873. err++;
  874. break;
  875. }
  876. }
  877. if (client->pollfd[1].revents & ~POLLIN) {
  878. goto zombie;
  879. }
  880. if (client->pollfd[1].revents & POLLIN) {
  881. #ifdef WITH_TIMESTAMPS
  882. jack_reset_timestamps ();
  883. #endif
  884. DEBUG ("client %d signalled at %" PRIu64
  885. ", awake for process at %" PRIu64
  886. " (delay = %" PRIu64
  887. " usecs) (wakeup on graph_wait_fd==%d)",
  888. getpid(),
  889. control->signalled_at,
  890. control->awake_at,
  891. control->awake_at - control->signalled_at,
  892. client->pollfd[1].fd);
  893. control->state = Running;
  894. if (control->sync_cb)
  895. jack_call_sync_client (client);
  896. if (control->process) {
  897. if (control->process (control->nframes,
  898. control->process_arg)
  899. == 0) {
  900. control->state = Finished;
  901. }
  902. } else {
  903. control->state = Finished;
  904. }
  905. if (control->timebase_cb)
  906. jack_call_timebase_master (client);
  907. control->finished_at = jack_get_microseconds();
  908. #ifdef WITH_TIMESTAMPS
  909. jack_timestamp ("finished");
  910. #endif
  911. /* pass the execution token along */
  912. DEBUG ("client finished processing at %" PRIu64
  913. " (elapsed = %" PRIu64
  914. " usecs), writing on graph_next_fd==%d",
  915. control->finished_at,
  916. control->finished_at - control->awake_at,
  917. client->graph_next_fd);
  918. if (write (client->graph_next_fd, &c, sizeof (c))
  919. != sizeof (c)) {
  920. jack_error ("cannot continue execution of the "
  921. "processing graph (%s)",
  922. strerror(errno));
  923. err++;
  924. break;
  925. }
  926. DEBUG ("client sent message to next stage by %" PRIu64
  927. ", client reading on graph_wait_fd==%d",
  928. jack_get_microseconds(), client->graph_wait_fd);
  929. #ifdef WITH_TIMESTAMPS
  930. jack_timestamp ("read pending byte from wait");
  931. #endif
  932. DEBUG("reading cleanup byte from pipe\n");
  933. if ((read (client->graph_wait_fd, &c, sizeof (c))
  934. != sizeof (c))) {
  935. DEBUG ("WARNING: READ FAILED!");
  936. #if 0
  937. jack_error ("cannot complete execution of the "
  938. "processing graph (%s)",
  939. strerror(errno));
  940. err++;
  941. break;
  942. #endif
  943. }
  944. /* check if we were killed during the process
  945. * cycle (or whatever) */
  946. if (client->control->dead) {
  947. goto zombie;
  948. }
  949. DEBUG("process cycle fully complete\n");
  950. #ifdef WITH_TIMESTAMPS
  951. jack_timestamp ("read done");
  952. jack_dump_timestamps (stdout);
  953. #endif
  954. }
  955. }
  956. return (void *) ((intptr_t)err);
  957. zombie:
  958. if (client->on_shutdown) {
  959. jack_error ("zombified - calling shutdown handler");
  960. client->on_shutdown (client->on_shutdown_arg);
  961. } else {
  962. jack_error ("zombified - exiting from JACK");
  963. jack_client_close (client);
  964. /* Need a fix : possibly make client crash if
  965. * zombified without shutdown handler
  966. */
  967. }
  968. pthread_exit (0);
  969. /*NOTREACHED*/
  970. return 0;
  971. }
  972. #ifdef JACK_USE_MACH_THREADS
  973. /* real-time thread : separated from the normal client thread, it will
  974. * communicate with the server using fast mach RPC mechanism */
  975. static void *
  976. jack_client_process_thread (void *arg)
  977. {
  978. jack_client_t *client = (jack_client_t *) arg;
  979. jack_client_control_t *control = client->control;
  980. int err = 0;
  981. client->control->pid = getpid();
  982. DEBUG ("client process thread is now running");
  983. pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  984. client->rt_thread_ok = TRUE;
  985. while (err == 0) {
  986. if (jack_client_suspend(client) < 0) {
  987. jack_error ("jack_client_process_thread : resume error");
  988. goto zombie;
  989. }
  990. control->awake_at = jack_get_microseconds();
  991. DEBUG ("client resumed");
  992. control->state = Running;
  993. if (control->sync_cb)
  994. jack_call_sync_client (client);
  995. if (control->process) {
  996. if (control->process (control->nframes,
  997. control->process_arg) == 0) {
  998. control->state = Finished;
  999. }
  1000. } else {
  1001. control->state = Finished;
  1002. }
  1003. if (control->timebase_cb)
  1004. jack_call_timebase_master (client);
  1005. control->finished_at = jack_get_microseconds();
  1006. #ifdef WITH_TIMESTAMPS
  1007. jack_timestamp ("finished");
  1008. #endif
  1009. DEBUG ("client finished processing at %Lu (elapsed = %f usecs)",
  1010. control->finished_at, ((float)(control->finished_at - control->awake_at)));
  1011. /* check if we were killed during the process cycle (or whatever) */
  1012. if (client->control->dead) {
  1013. jack_error ("jack_client_process_thread : client->control->dead");
  1014. goto zombie;
  1015. }
  1016. DEBUG("process cycle fully complete\n");
  1017. }
  1018. return (void *) ((intptr_t)err);
  1019. zombie:
  1020. jack_error ("jack_client_process_thread : zombified");
  1021. client->rt_thread_ok = FALSE;
  1022. if (client->on_shutdown) {
  1023. jack_error ("zombified - calling shutdown handler");
  1024. client->on_shutdown (client->on_shutdown_arg);
  1025. } else {
  1026. jack_error ("zombified - exiting from JACK");
  1027. jack_client_close (client); /* Need a fix : possibly make client crash if zombified without shutdown handler */
  1028. }
  1029. pthread_exit (0);
  1030. /*NOTREACHED*/
  1031. return 0;
  1032. }
  1033. #endif /* JACK_USE_MACH_THREADS */
  1034. static int
  1035. jack_start_thread (jack_client_t *client)
  1036. {
  1037. #ifndef JACK_USE_MACH_THREADS
  1038. int policy = SCHED_OTHER;
  1039. struct sched_param client_param, temp_param;
  1040. #endif
  1041. pthread_attr_t *attributes = 0;
  1042. if (client->engine->real_time) {
  1043. /* Get the client thread to run as an RT-FIFO
  1044. scheduled thread of appropriate priority.
  1045. */
  1046. struct sched_param rt_param;
  1047. attributes = (pthread_attr_t *)
  1048. malloc (sizeof (pthread_attr_t));
  1049. pthread_attr_init (attributes);
  1050. if (pthread_attr_setschedpolicy (attributes, SCHED_FIFO)) {
  1051. jack_error ("cannot set FIFO scheduling class for RT "
  1052. "thread");
  1053. return -1;
  1054. }
  1055. if (pthread_attr_setscope (attributes, PTHREAD_SCOPE_SYSTEM)) {
  1056. jack_error ("Cannot set scheduling scope for RT "
  1057. "thread");
  1058. return -1;
  1059. }
  1060. memset (&rt_param, 0, sizeof (rt_param));
  1061. rt_param.sched_priority = client->engine->client_priority;
  1062. if (pthread_attr_setschedparam (attributes, &rt_param)) {
  1063. jack_error ("Cannot set scheduling priority for RT "
  1064. "thread (%s)", strerror (errno));
  1065. return -1;
  1066. }
  1067. #ifdef USE_MLOCK
  1068. if (client->engine->do_mlock
  1069. && (mlockall (MCL_CURRENT | MCL_FUTURE) != 0)) {
  1070. jack_error ("cannot lock down memory for RT thread (%s)",
  1071. strerror (errno));
  1072. #ifdef ENSURE_MLOCK
  1073. return -1;
  1074. #endif /* ENSURE_MLOCK */
  1075. }
  1076. #endif /* USE_MLOCK */
  1077. }
  1078. #ifdef JACK_USE_MACH_THREADS
  1079. if (pthread_create (&client->thread, attributes,
  1080. jack_client_thread, client)) {
  1081. return -1;
  1082. }
  1083. /* a special real-time thread to call the "process"
  1084. * callback. It will communicate with the server using fast
  1085. * mach RPC mechanism */
  1086. if (pthread_create (&client->process_thread, attributes,
  1087. jack_client_process_thread, client)) {
  1088. jack_error("pthread_create failed for process_thread \n");
  1089. return -1;
  1090. }
  1091. if (client->engine->real_time){
  1092. /* time constraint thread */
  1093. setThreadToPriority(client->process_thread, 96, TRUE, 10000000);
  1094. }else{
  1095. /* fixed priority thread */
  1096. setThreadToPriority(client->process_thread, 63, TRUE, 10000000);
  1097. }
  1098. #else /* !JACK_USE_MACH_THREADS */
  1099. if (pthread_create (&client->thread, attributes,
  1100. jack_client_thread, client) == 0) {
  1101. return 0;
  1102. }
  1103. if (!client->engine->real_time) {
  1104. return -1;
  1105. }
  1106. /* the version of glibc I've played with has a bug that makes
  1107. that code fail when running under a non-root user but with the
  1108. proper realtime capabilities (in short, pthread_attr_setschedpolicy
  1109. does not check for capabilities, only for the uid being
  1110. zero). Newer versions apparently have this fixed. This
  1111. workaround temporarily switches the client thread to the
  1112. proper scheduler and priority, then starts the realtime
  1113. thread so that it can inherit them and finally switches the
  1114. client thread back to what it was before. Sigh. For ardour
  1115. I have to check again and switch the thread explicitly to
  1116. realtime, don't know why or how to debug - nando
  1117. */
  1118. /* get current scheduler and parameters of the client process */
  1119. if ((policy = sched_getscheduler (0)) < 0) {
  1120. jack_error ("Cannot get current client scheduler: %s",
  1121. strerror(errno));
  1122. return -1;
  1123. }
  1124. memset (&client_param, 0, sizeof (client_param));
  1125. if (sched_getparam (0, &client_param)) {
  1126. jack_error ("Cannot get current client scheduler "
  1127. "parameters: %s", strerror(errno));
  1128. return -1;
  1129. }
  1130. /* temporarily change the client process to SCHED_FIFO so that
  1131. the realtime thread can inherit the scheduler and priority
  1132. */
  1133. memset (&temp_param, 0, sizeof (temp_param));
  1134. temp_param.sched_priority = client->engine->client_priority;
  1135. if (sched_setscheduler(0, SCHED_FIFO, &temp_param)) {
  1136. jack_error ("Cannot temporarily set client to RT scheduler:"
  1137. " %s", strerror(errno));
  1138. return -1;
  1139. }
  1140. /* prepare the attributes for the realtime thread */
  1141. attributes = (pthread_attr_t *) malloc (sizeof (pthread_attr_t));
  1142. pthread_attr_init (attributes);
  1143. if (pthread_attr_setscope (attributes, PTHREAD_SCOPE_SYSTEM)) {
  1144. sched_setscheduler (0, policy, &client_param);
  1145. jack_error ("Cannot set scheduling scope for RT thread");
  1146. return -1;
  1147. }
  1148. if (pthread_attr_setinheritsched (attributes, PTHREAD_INHERIT_SCHED)) {
  1149. sched_setscheduler (0, policy, &client_param);
  1150. jack_error ("Cannot set scheduler inherit policy for RT "
  1151. "thread");
  1152. return -1;
  1153. }
  1154. /* create the RT thread */
  1155. if (pthread_create (&client->thread, attributes,
  1156. jack_client_thread, client)) {
  1157. sched_setscheduler (0, policy, &client_param);
  1158. return -1;
  1159. }
  1160. /* return the client process to the scheduler it was in before */
  1161. if (sched_setscheduler (0, policy, &client_param)) {
  1162. jack_error ("Cannot reset original client scheduler: %s",
  1163. strerror(errno));
  1164. return -1;
  1165. }
  1166. /* check again... inheritance of policy and priority works in
  1167. jack_simple_client but not in ardour! So I check again and
  1168. force the policy if it is not set correctly. This does not
  1169. really really work either, the manager thread of the
  1170. linuxthreads implementation is left running with
  1171. SCHED_OTHER, that is presumably very bad.
  1172. */
  1173. memset (&client_param, 0, sizeof (client_param));
  1174. if (pthread_getschedparam(client->thread, &policy,
  1175. &client_param) == 0) {
  1176. if (policy != SCHED_FIFO) {
  1177. memset (&client_param, 0, sizeof (client_param));
  1178. client_param.sched_priority =
  1179. client->engine->client_priority;
  1180. if (pthread_setschedparam (client->thread, SCHED_FIFO,
  1181. &client_param)) {
  1182. jack_error ("Cannot set (again) FIFO scheduling"
  1183. " class for RT thread\n");
  1184. return -1;
  1185. }
  1186. }
  1187. }
  1188. #endif /* JACK_USE_MACH_THREADS */
  1189. return 0;
  1190. }
  1191. int
  1192. jack_activate (jack_client_t *client)
  1193. {
  1194. jack_request_t req;
  1195. /* we need to scribble on our stack to ensure that its memory
  1196. * pages are actually mapped (more important for mlockall(2)
  1197. * usage in jack_start_thread())
  1198. */
  1199. char buf[JACK_THREAD_STACK_TOUCH];
  1200. int i;
  1201. for (i = 0; i < JACK_THREAD_STACK_TOUCH; i++) {
  1202. buf[i] = (char) (i & 0xff);
  1203. }
  1204. if (client->control->type == ClientInternal ||
  1205. client->control->type == ClientDriver) {
  1206. goto startit;
  1207. }
  1208. /* get the pid of the client process to pass it to engine */
  1209. client->control->pid = getpid ();
  1210. #ifdef USE_CAPABILITIES
  1211. if (client->engine->has_capabilities != 0 &&
  1212. client->control->pid != 0 && client->engine->real_time != 0) {
  1213. /* we need to ask the engine for realtime capabilities
  1214. before trying to start the realtime thread
  1215. */
  1216. req.type = SetClientCapabilities;
  1217. req.x.client_id = client->control->id;
  1218. jack_client_deliver_request (client, &req);
  1219. if (req.status) {
  1220. /* what to do? engine is running realtime, it
  1221. is using capabilities and has them
  1222. (otherwise we would not get an error
  1223. return) but for some reason it could not
  1224. give the client the required capabilities,
  1225. so for now downgrade the client so that it
  1226. still runs, albeit non-realtime - nando
  1227. */
  1228. jack_error ("could not receive realtime capabilities, "
  1229. "client will run non-realtime");
  1230. /* XXX wrong, this is a property of the engine
  1231. client->engine->real_time = 0;
  1232. */
  1233. }
  1234. }
  1235. #endif /* USE_CAPABILITIES */
  1236. if (client->first_active) {
  1237. pthread_mutex_init (&client_lock, NULL);
  1238. pthread_cond_init (&client_ready, NULL);
  1239. pthread_mutex_lock (&client_lock);
  1240. if (jack_start_thread (client)) {
  1241. pthread_mutex_unlock (&client_lock);
  1242. return -1;
  1243. }
  1244. pthread_cond_wait (&client_ready, &client_lock);
  1245. pthread_mutex_unlock (&client_lock);
  1246. if (!client->thread_ok) {
  1247. jack_error ("could not start client thread");
  1248. return -1;
  1249. }
  1250. client->first_active = FALSE;
  1251. }
  1252. startit:
  1253. req.type = ActivateClient;
  1254. req.x.client_id = client->control->id;
  1255. return jack_client_deliver_request (client, &req);
  1256. }
  1257. int
  1258. jack_deactivate (jack_client_t *client)
  1259. {
  1260. jack_request_t req;
  1261. req.type = DeactivateClient;
  1262. req.x.client_id = client->control->id;
  1263. return jack_client_deliver_request (client, &req);
  1264. }
  1265. int
  1266. jack_client_close (jack_client_t *client)
  1267. {
  1268. JSList *node;
  1269. void *status;
  1270. if (client->control->active) {
  1271. jack_deactivate (client);
  1272. }
  1273. if (client->control->type == ClientExternal) {
  1274. #if JACK_USE_MACH_THREADS
  1275. if (client->rt_thread_ok) {
  1276. // MacOSX pthread_cancel not implemented in
  1277. // Darwin 5.5, 6.4
  1278. mach_port_t machThread =
  1279. pthread_mach_thread_np (client->process_thread);
  1280. thread_terminate (machThread);
  1281. }
  1282. #endif
  1283. /* stop the thread that communicates with the jack
  1284. * server, only if it was actually running
  1285. */
  1286. if (client->thread_ok){
  1287. pthread_cancel (client->thread);
  1288. pthread_join (client->thread, &status);
  1289. }
  1290. if (client->control) {
  1291. jack_release_shm (&client->control_shm);
  1292. client->control = NULL;
  1293. }
  1294. if (client->engine) {
  1295. jack_release_shm (&client->engine_shm);
  1296. client->engine = NULL;
  1297. }
  1298. if (client->port_segment) {
  1299. jack_port_type_id_t ptid;
  1300. for (ptid = 0; ptid < client->n_port_types; ++ptid) {
  1301. jack_release_shm (&client->port_segment[ptid]);
  1302. }
  1303. free (client->port_segment);
  1304. client->port_segment = NULL;
  1305. }
  1306. if (client->graph_wait_fd) {
  1307. close (client->graph_wait_fd);
  1308. }
  1309. if (client->graph_next_fd) {
  1310. close (client->graph_next_fd);
  1311. }
  1312. close (client->event_fd);
  1313. close (client->request_fd);
  1314. }
  1315. for (node = client->ports; node; node = jack_slist_next (node)) {
  1316. free (node->data);
  1317. }
  1318. jack_slist_free (client->ports);
  1319. jack_client_free (client);
  1320. return 0;
  1321. }
  1322. int
  1323. jack_is_realtime (jack_client_t *client)
  1324. {
  1325. return client->engine->real_time;
  1326. }
  1327. jack_nframes_t
  1328. jack_get_buffer_size (jack_client_t *client)
  1329. {
  1330. return client->engine->buffer_size;
  1331. }
  1332. int
  1333. jack_set_buffer_size (jack_client_t *client, jack_nframes_t nframes)
  1334. {
  1335. #ifdef DO_BUFFER_RESIZE
  1336. jack_request_t req;
  1337. req.type = SetBufferSize;
  1338. req.x.nframes = nframes;
  1339. return jack_client_deliver_request (client, &req);
  1340. #else
  1341. return ENOSYS;
  1342. #endif /* DO_BUFFER_RESIZE */
  1343. }
  1344. int
  1345. jack_connect (jack_client_t *client, const char *source_port,
  1346. const char *destination_port)
  1347. {
  1348. jack_request_t req;
  1349. req.type = ConnectPorts;
  1350. snprintf (req.x.connect.source_port,
  1351. sizeof (req.x.connect.source_port), "%s", source_port);
  1352. snprintf (req.x.connect.destination_port,
  1353. sizeof (req.x.connect.destination_port),
  1354. "%s", destination_port);
  1355. return jack_client_deliver_request (client, &req);
  1356. }
  1357. int
  1358. jack_port_disconnect (jack_client_t *client, jack_port_t *port)
  1359. {
  1360. jack_request_t req;
  1361. pthread_mutex_lock (&port->connection_lock);
  1362. if (port->connections == NULL) {
  1363. pthread_mutex_unlock (&port->connection_lock);
  1364. return 0;
  1365. }
  1366. pthread_mutex_unlock (&port->connection_lock);
  1367. req.type = DisconnectPort;
  1368. req.x.port_info.port_id = port->shared->id;
  1369. return jack_client_deliver_request (client, &req);
  1370. }
  1371. int
  1372. jack_disconnect (jack_client_t *client, const char *source_port,
  1373. const char *destination_port)
  1374. {
  1375. jack_request_t req;
  1376. req.type = DisconnectPorts;
  1377. snprintf (req.x.connect.source_port,
  1378. sizeof (req.x.connect.source_port), "%s", source_port);
  1379. snprintf (req.x.connect.destination_port,
  1380. sizeof (req.x.connect.destination_port),
  1381. "%s", destination_port);
  1382. return jack_client_deliver_request (client, &req);
  1383. }
  1384. void
  1385. jack_set_error_function (void (*func) (const char *))
  1386. {
  1387. jack_error_callback = func;
  1388. }
  1389. int
  1390. jack_set_graph_order_callback (jack_client_t *client,
  1391. JackGraphOrderCallback callback, void *arg)
  1392. {
  1393. if (client->control->active) {
  1394. jack_error ("You cannot set callbacks on an active client.");
  1395. return -1;
  1396. }
  1397. client->control->graph_order = callback;
  1398. client->control->graph_order_arg = arg;
  1399. return 0;
  1400. }
  1401. int jack_set_xrun_callback (jack_client_t *client,
  1402. JackXRunCallback callback, void *arg)
  1403. {
  1404. if (client->control->active) {
  1405. jack_error ("You cannot set callbacks on an active client.");
  1406. return -1;
  1407. }
  1408. client->control->xrun = callback;
  1409. client->control->xrun_arg = arg;
  1410. return 0;
  1411. }
  1412. int
  1413. jack_set_process_callback (jack_client_t *client,
  1414. JackProcessCallback callback, void *arg)
  1415. {
  1416. if (client->control->active) {
  1417. jack_error ("You cannot set callbacks on an active client.");
  1418. return -1;
  1419. }
  1420. client->control->process_arg = arg;
  1421. client->control->process = callback;
  1422. return 0;
  1423. }
  1424. int
  1425. jack_set_thread_init_callback (jack_client_t *client,
  1426. JackThreadInitCallback callback, void *arg)
  1427. {
  1428. if (client->control->active) {
  1429. jack_error ("You cannot set callbacks on an active client.");
  1430. return -1;
  1431. }
  1432. client->control->thread_init_arg = arg;
  1433. client->control->thread_init = callback;
  1434. return 0;
  1435. }
  1436. int
  1437. jack_set_freewheel_callback (jack_client_t *client,
  1438. JackFreewheelCallback callback, void *arg)
  1439. {
  1440. if (client->control->active) {
  1441. jack_error ("You cannot set callbacks on an active client.");
  1442. return -1;
  1443. }
  1444. client->control->freewheel_arg = arg;
  1445. client->control->freewheel_cb = callback;
  1446. return 0;
  1447. }
  1448. int
  1449. jack_set_buffer_size_callback (jack_client_t *client,
  1450. JackBufferSizeCallback callback, void *arg)
  1451. {
  1452. client->control->bufsize_arg = arg;
  1453. client->control->bufsize = callback;
  1454. return 0;
  1455. }
  1456. int
  1457. jack_set_port_registration_callback(jack_client_t *client,
  1458. JackPortRegistrationCallback callback,
  1459. void *arg)
  1460. {
  1461. if (client->control->active) {
  1462. jack_error ("You cannot set callbacks on an active client.");
  1463. return -1;
  1464. }
  1465. client->control->port_register_arg = arg;
  1466. client->control->port_register = callback;
  1467. return 0;
  1468. }
  1469. int
  1470. jack_get_process_start_fd (jack_client_t *client)
  1471. {
  1472. /* once this has been called, the client thread
  1473. does not sleep on the graph wait fd.
  1474. */
  1475. client->pollmax = 1;
  1476. return client->graph_wait_fd;
  1477. }
  1478. int
  1479. jack_get_process_done_fd (jack_client_t *client)
  1480. {
  1481. return client->graph_next_fd;
  1482. }
  1483. void
  1484. jack_on_shutdown (jack_client_t *client, void (*function)(void *arg), void *arg)
  1485. {
  1486. client->on_shutdown = function;
  1487. client->on_shutdown_arg = arg;
  1488. }
  1489. const char **
  1490. jack_get_ports (jack_client_t *client,
  1491. const char *port_name_pattern,
  1492. const char *type_name_pattern,
  1493. unsigned long flags)
  1494. {
  1495. jack_control_t *engine;
  1496. const char **matching_ports;
  1497. unsigned long match_cnt;
  1498. jack_port_shared_t *psp;
  1499. unsigned long i;
  1500. regex_t port_regex;
  1501. regex_t type_regex;
  1502. int matching;
  1503. engine = client->engine;
  1504. if (port_name_pattern && port_name_pattern[0]) {
  1505. regcomp (&port_regex, port_name_pattern,
  1506. REG_EXTENDED|REG_NOSUB);
  1507. }
  1508. if (type_name_pattern && type_name_pattern[0]) {
  1509. regcomp (&type_regex, type_name_pattern,
  1510. REG_EXTENDED|REG_NOSUB);
  1511. }
  1512. psp = engine->ports;
  1513. match_cnt = 0;
  1514. matching_ports = (const char **)
  1515. malloc (sizeof (char *) * engine->port_max);
  1516. for (i = 0; i < engine->port_max; i++) {
  1517. matching = 1;
  1518. if (!psp[i].in_use) {
  1519. continue;
  1520. }
  1521. if (flags) {
  1522. if ((psp[i].flags & flags) != flags) {
  1523. matching = 0;
  1524. }
  1525. }
  1526. if (matching && port_name_pattern && port_name_pattern[0]) {
  1527. if (regexec (&port_regex, psp[i].name, 0, NULL, 0)) {
  1528. matching = 0;
  1529. }
  1530. }
  1531. if (matching && type_name_pattern && type_name_pattern[0]) {
  1532. jack_port_type_id_t ptid = psp[i].ptype_id;
  1533. if (regexec (&type_regex,
  1534. engine->port_types[ptid].type_name,
  1535. 0, NULL, 0)) {
  1536. matching = 0;
  1537. }
  1538. }
  1539. if (matching) {
  1540. matching_ports[match_cnt++] = psp[i].name;
  1541. }
  1542. }
  1543. matching_ports[match_cnt] = 0;
  1544. if (match_cnt == 0) {
  1545. free (matching_ports);
  1546. matching_ports = 0;
  1547. }
  1548. return matching_ports;
  1549. }
  1550. float
  1551. jack_cpu_load (jack_client_t *client)
  1552. {
  1553. return client->engine->cpu_load;
  1554. }
  1555. pthread_t
  1556. jack_client_thread_id (jack_client_t *client)
  1557. {
  1558. return client->thread_id;
  1559. }
  1560. int
  1561. jack_client_name_size(void)
  1562. {
  1563. return JACK_CLIENT_NAME_SIZE;
  1564. }
  1565. int
  1566. jack_port_name_size(void)
  1567. {
  1568. return JACK_PORT_NAME_SIZE;
  1569. }
  1570. int
  1571. jack_port_type_size(void)
  1572. {
  1573. return JACK_PORT_TYPE_SIZE;
  1574. }