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.

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