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.

1520 lines
36KB

  1. /*
  2. Copyright (C) 2001-2003 Paul Davis
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as published by
  5. the Free Software Foundation; either version 2.1 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. $Id$
  15. */
  16. #include <sys/socket.h>
  17. #include <sys/un.h>
  18. #include <pthread.h>
  19. #include <errno.h>
  20. #include <fcntl.h>
  21. #include <sys/types.h>
  22. #include <sys/ipc.h>
  23. #include <sys/mman.h>
  24. #include <sys/poll.h>
  25. #include <stdarg.h>
  26. #include <stdio.h>
  27. #include <regex.h>
  28. #include <math.h>
  29. #include <config.h>
  30. #include <jack/jack.h>
  31. #include <jack/internal.h>
  32. #include <jack/engine.h>
  33. #include <jack/pool.h>
  34. #include <jack/error.h>
  35. #include <jack/time.h>
  36. #include <jack/jslist.h>
  37. #include <jack/version.h>
  38. #include "local.h"
  39. #ifdef WITH_TIMESTAMPS
  40. #include <jack/timestamps.h>
  41. #endif /* WITH_TIMESTAMPS */
  42. jack_time_t __jack_cpu_mhz;
  43. char *jack_server_dir = "/tmp";
  44. void
  45. jack_set_server_dir (const char *path)
  46. {
  47. jack_server_dir = strdup (path);
  48. }
  49. static pthread_mutex_t client_lock;
  50. static pthread_cond_t client_ready;
  51. void *jack_zero_filled_buffer = 0;
  52. #define event_fd pollfd[0].fd
  53. #define graph_wait_fd pollfd[1].fd
  54. typedef struct {
  55. int status;
  56. struct _jack_client *client;
  57. const char *client_name;
  58. } client_info;
  59. void
  60. jack_unlink_shm (const char *shm_name)
  61. {
  62. shm_unlink (shm_name);
  63. }
  64. char *
  65. jack_get_shm (const char *shm_name, size_t size, int perm, int mode, int prot)
  66. {
  67. int shm_fd;
  68. char *addr;
  69. if ((shm_fd = shm_open (shm_name, perm, mode)) < 0) {
  70. jack_error ("cannot create shm segment %s (%s)", shm_name, strerror (errno));
  71. return MAP_FAILED;
  72. }
  73. if (perm & O_CREAT) {
  74. if (ftruncate (shm_fd, size) < 0) {
  75. jack_error ("cannot set size of engine shm registry (%s)", strerror (errno));
  76. return MAP_FAILED;
  77. }
  78. }
  79. if ((addr = mmap (0, size, prot, MAP_SHARED, shm_fd, 0)) == MAP_FAILED) {
  80. jack_error ("cannot mmap shm segment %s (%s)", shm_name, strerror (errno));
  81. shm_unlink (shm_name);
  82. close (shm_fd);
  83. return MAP_FAILED;
  84. }
  85. close (shm_fd);
  86. return addr;
  87. }
  88. void
  89. jack_error (const char *fmt, ...)
  90. {
  91. va_list ap;
  92. char buffer[300];
  93. va_start (ap, fmt);
  94. vsnprintf (buffer, sizeof(buffer), fmt, ap);
  95. jack_error_callback (buffer);
  96. va_end (ap);
  97. }
  98. void default_jack_error_callback (const char *desc)
  99. {
  100. fprintf(stderr, "%s\n", desc);
  101. }
  102. void (*jack_error_callback)(const char *desc) = &default_jack_error_callback;
  103. static int
  104. oop_client_deliver_request (void *ptr, jack_request_t *req)
  105. {
  106. jack_client_t *client = (jack_client_t*) ptr;
  107. if (write (client->request_fd, req, sizeof (*req)) != sizeof (*req)) {
  108. jack_error ("cannot send request type %d to server", req->type);
  109. req->status = -1;
  110. }
  111. if (read (client->request_fd, req, sizeof (*req)) != sizeof (*req)) {
  112. jack_error ("cannot read result for request type %d from server (%s)", req->type, strerror (errno));
  113. req->status = -1;
  114. }
  115. return req->status;
  116. }
  117. int
  118. jack_client_deliver_request (const jack_client_t *client, jack_request_t *req)
  119. {
  120. /* indirect through the function pointer that was set
  121. either by jack_client_new() (external) or handle_new_client()
  122. in the server.
  123. */
  124. return client->control->deliver_request (client->control->deliver_arg, req);
  125. }
  126. jack_client_t *
  127. jack_client_alloc ()
  128. {
  129. jack_client_t *client;
  130. client = (jack_client_t *) malloc (sizeof (jack_client_t));
  131. client->pollfd = (struct pollfd *) malloc (sizeof (struct pollfd) * 2);
  132. client->pollmax = 2;
  133. client->request_fd = -1;
  134. client->event_fd = -1;
  135. client->graph_wait_fd = -1;
  136. client->graph_next_fd = -1;
  137. client->port_segments = NULL;
  138. client->ports = NULL;
  139. client->engine = NULL;
  140. client->control = 0;
  141. client->thread_ok = FALSE;
  142. client->first_active = TRUE;
  143. client->on_shutdown = NULL;
  144. return client;
  145. }
  146. jack_client_t *
  147. jack_client_alloc_internal (jack_client_control_t *cc, jack_control_t *ec)
  148. {
  149. jack_client_t* client;
  150. client = jack_client_alloc ();
  151. client->control = cc;
  152. client->engine = ec;
  153. return client;
  154. }
  155. static void
  156. jack_client_free (jack_client_t *client)
  157. {
  158. if (client->pollfd) {
  159. free (client->pollfd);
  160. }
  161. free (client);
  162. }
  163. void
  164. jack_client_invalidate_port_buffers (jack_client_t *client)
  165. {
  166. JSList *node;
  167. jack_port_t *port;
  168. /* This releases all local memory owned by input ports
  169. and sets the buffer pointer to NULL. This will cause
  170. jack_port_get_buffer() to reallocate space for the
  171. buffer on the next call (if there is one).
  172. */
  173. for (node = client->ports; node; node = jack_slist_next (node)) {
  174. port = (jack_port_t *) node->data;
  175. if (port->shared->flags & JackPortIsInput) {
  176. if (port->client_segment_base == 0) {
  177. jack_pool_release ((void *) port->shared->offset);
  178. port->client_segment_base = 0;
  179. port->shared->offset = 0;
  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-%lu", 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-%lu", 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)", strerror (errno));
  259. return -1;
  260. }
  261. addr.sun_family = AF_UNIX;
  262. snprintf (addr.sun_path, sizeof (addr.sun_path) - 1, "%s/jack_%d", jack_server_dir, which);
  263. if (connect (fd, (struct sockaddr *) &addr, sizeof (addr)) < 0) {
  264. jack_error ("cannot connect to jack server", strerror (errno));
  265. close (fd);
  266. return -1;
  267. }
  268. return fd;
  269. }
  270. static int
  271. server_event_connect (jack_client_t *client)
  272. {
  273. int fd;
  274. struct sockaddr_un addr;
  275. jack_client_connect_ack_request_t req;
  276. jack_client_connect_ack_result_t res;
  277. if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) {
  278. jack_error ("cannot create client event socket (%s)", strerror (errno));
  279. return -1;
  280. }
  281. addr.sun_family = AF_UNIX;
  282. snprintf (addr.sun_path, sizeof (addr.sun_path) - 1, "%s/jack_ack_0", jack_server_dir);
  283. if (connect (fd, (struct sockaddr *) &addr, sizeof (addr)) < 0) {
  284. jack_error ("cannot connect to jack server for events", strerror (errno));
  285. close (fd);
  286. return -1;
  287. }
  288. req.client_id = client->control->id;
  289. if (write (fd, &req, sizeof (req)) != sizeof (req)) {
  290. jack_error ("cannot write event connect request to server (%s)", strerror (errno));
  291. close (fd);
  292. return -1;
  293. }
  294. if (read (fd, &res, sizeof (res)) != sizeof (res)) {
  295. jack_error ("cannot read event connect result from server (%s)", strerror (errno));
  296. close (fd);
  297. return -1;
  298. }
  299. if (res.status != 0) {
  300. close (fd);
  301. return -1;
  302. }
  303. return fd;
  304. }
  305. static int
  306. jack_request_client (ClientType type, const char* client_name, const char* so_name,
  307. const char* so_data, jack_client_connect_result_t *res, int *req_fd)
  308. {
  309. jack_client_connect_request_t req;
  310. *req_fd = -1;
  311. memset (&req, 0, sizeof (req));
  312. if (strlen (client_name) > sizeof (req.name) - 1) {
  313. jack_error ("\"%s\" is too long to be used as a JACK client name.\n"
  314. "Please use %lu characters or less.",
  315. client_name, sizeof (req.name) - 1);
  316. return -1;
  317. }
  318. if (strlen (so_name) > sizeof (req.object_path) - 1) {
  319. jack_error ("\"%s\" is too long to be used as a JACK shared object name.\n"
  320. "Please use %lu characters or less.",
  321. so_name, sizeof (req.object_path) - 1);
  322. return -1;
  323. }
  324. if (strlen (so_data) > sizeof (req.object_data) - 1) {
  325. jack_error ("\"%s\" is too long to be used as a JACK shared object data string.\n"
  326. "Please use %lu characters or less.",
  327. so_data, sizeof (req.object_data) - 1);
  328. return -1;
  329. }
  330. if ((*req_fd = server_connect (0)) < 0) {
  331. jack_error ("cannot connect to default JACK server");
  332. goto fail;
  333. }
  334. req.load = TRUE;
  335. req.type = type;
  336. snprintf (req.name, sizeof (req.name), "%s", client_name);
  337. snprintf (req.object_path, sizeof (req.object_path), "%s", so_name);
  338. snprintf (req.object_data, sizeof (req.object_data), "%s", so_data);
  339. if (write (*req_fd, &req, sizeof (req)) != sizeof (req)) {
  340. jack_error ("cannot send request to jack server (%s)", strerror (errno));
  341. goto fail;
  342. }
  343. if (read (*req_fd, res, sizeof (*res)) != sizeof (*res)) {
  344. if (errno == 0) {
  345. /* server shut the socket */
  346. jack_error ("could not attach as client (duplicate client name?)");
  347. goto fail;
  348. }
  349. jack_error ("cannot read response from jack server (%s)", strerror (errno));
  350. goto fail;
  351. }
  352. if (res->status) {
  353. jack_error ("could not attach as client (duplicate client name?)");
  354. goto fail;
  355. }
  356. if (res->protocol_v != jack_protocol_version){
  357. jack_error ("application linked against too wrong of a version of libjack.");
  358. goto fail;
  359. }
  360. switch (type) {
  361. case ClientDriver:
  362. case ClientInternal:
  363. close (*req_fd);
  364. *req_fd = -1;
  365. break;
  366. default:
  367. break;
  368. }
  369. return 0;
  370. fail:
  371. if (*req_fd >= 0) {
  372. close (*req_fd);
  373. *req_fd = -1;
  374. }
  375. return -1;
  376. }
  377. jack_client_t *
  378. jack_client_new (const char *client_name)
  379. {
  380. int req_fd = -1;
  381. int ev_fd = -1;
  382. jack_client_connect_result_t res;
  383. jack_client_t *client;
  384. void *addr;
  385. int i;
  386. jack_port_type_info_t* type_info;
  387. /* external clients need this initialized; internal clients
  388. will use the setup in the server's address space.
  389. */
  390. jack_init_time ();
  391. if (jack_request_client (ClientExternal, client_name, "", "", &res, &req_fd)) {
  392. return NULL;
  393. }
  394. client = jack_client_alloc ();
  395. strcpy (client->fifo_prefix, res.fifo_prefix);
  396. client->request_fd = req_fd;
  397. client->pollfd[0].events = POLLIN|POLLERR|POLLHUP|POLLNVAL;
  398. client->pollfd[1].events = POLLIN|POLLERR|POLLHUP|POLLNVAL;
  399. /* attach the engine control/info block */
  400. if ((addr = jack_get_shm (res.control_shm_name, res.control_size, O_RDWR,
  401. 0, (PROT_READ|PROT_WRITE))) == MAP_FAILED) {
  402. jack_error ("cannot attached engine control shared memory segment");
  403. goto fail;
  404. }
  405. client->engine = (jack_control_t *) addr;
  406. /* now attach the client control block */
  407. if ((addr = jack_get_shm (res.client_shm_name, sizeof (jack_client_control_t), O_RDWR,
  408. 0, (PROT_READ|PROT_WRITE))) == MAP_FAILED) {
  409. jack_error ("cannot attached client control shared memory segment");
  410. goto fail;
  411. }
  412. client->control = (jack_client_control_t *) addr;
  413. /* nobody else needs to access this shared memory any more, so
  414. unlink it.
  415. */
  416. jack_unlink_shm (res.client_shm_name);
  417. /* read incoming port type information so that we can get shared memory
  418. information for each one.
  419. */
  420. type_info = (jack_port_type_info_t *) malloc (sizeof (jack_port_type_info_t) * res.n_port_types);
  421. if (read (req_fd, type_info, sizeof (jack_port_type_info_t) * res.n_port_types) !=
  422. sizeof (jack_port_type_info_t) * res.n_port_types) {
  423. jack_error ("cannot read port type information during client connection");
  424. free (type_info);
  425. goto fail;
  426. }
  427. for (i = 0; i < res.n_port_types; ++i) {
  428. jack_client_handle_new_port_type (client, type_info[i].shm_info.shm_name, type_info[i].shm_info.size, 0);
  429. }
  430. free (type_info);
  431. /* set up the client so that it does the right thing for an external client */
  432. client->control->deliver_request = oop_client_deliver_request;
  433. client->control->deliver_arg = client;
  434. if ((ev_fd = server_event_connect (client)) < 0) {
  435. jack_error ("cannot connect to server for event stream (%s)", strerror (errno));
  436. goto fail;
  437. }
  438. client->event_fd = ev_fd;
  439. return client;
  440. fail:
  441. if (client->engine) {
  442. munmap ((char *) client->engine, res.control_size);
  443. }
  444. if (client->control) {
  445. munmap ((char *) client->control, sizeof (jack_client_control_t));
  446. }
  447. if (req_fd >= 0) {
  448. close (req_fd);
  449. }
  450. if (ev_fd >= 0) {
  451. close (ev_fd);
  452. }
  453. return 0;
  454. }
  455. int
  456. jack_internal_client_new (const char *client_name, const char *so_name, const char *so_data)
  457. {
  458. jack_client_connect_result_t res;
  459. int req_fd;
  460. return jack_request_client (ClientInternal, client_name, so_name, so_data, &res, &req_fd);
  461. }
  462. void
  463. jack_internal_client_close (const char *client_name)
  464. {
  465. jack_client_connect_request_t req;
  466. int fd;
  467. req.load = FALSE;
  468. snprintf (req.name, sizeof (req.name), "%s", client_name);
  469. if ((fd = server_connect (0)) < 0) {
  470. jack_error ("cannot connect to default JACK server.");
  471. return;
  472. }
  473. if (write (fd, &req, sizeof (req)) != sizeof(req)) {
  474. jack_error ("cannot deliver ClientUnload request to JACK server.");
  475. }
  476. /* no response to this request */
  477. close (fd);
  478. return;
  479. }
  480. void
  481. jack_client_handle_new_port_type (jack_client_t *client, shm_name_t shm_name, size_t size, void* addr)
  482. {
  483. jack_port_segment_info_t *si;
  484. /* Lookup, attach and register the port/buffer segments in use
  485. right now.
  486. */
  487. if (client->control->type == ClientExternal) {
  488. if ((addr = jack_get_shm(shm_name, size, O_RDWR, 0, (PROT_READ|PROT_WRITE))) == MAP_FAILED) {
  489. jack_error ("cannot attached port segment shared memory (%s)", strerror (errno));
  490. return;
  491. }
  492. } else {
  493. /* client is in same address space as server, so just use `addr' directly */
  494. }
  495. si = (jack_port_segment_info_t *) malloc (sizeof (jack_port_segment_info_t));
  496. strcpy (si->shm_name, shm_name);
  497. si->address = addr;
  498. si->size = size;
  499. /* the first chunk of the first port segment is always set by the engine
  500. to be a conveniently-sized, zero-filled lump of memory.
  501. */
  502. if (client->port_segments == NULL) {
  503. jack_zero_filled_buffer = si->address;
  504. }
  505. client->port_segments = jack_slist_prepend (client->port_segments, si);
  506. }
  507. static void *
  508. jack_client_thread (void *arg)
  509. {
  510. jack_client_t *client = (jack_client_t *) arg;
  511. jack_client_control_t *control = client->control;
  512. jack_event_t event;
  513. char status = 0;
  514. char c;
  515. int err = 0;
  516. pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  517. pthread_mutex_lock (&client_lock);
  518. client->thread_ok = TRUE;
  519. client->thread_id = pthread_self();
  520. pthread_cond_signal (&client_ready);
  521. pthread_mutex_unlock (&client_lock);
  522. client->control->pid = getpid();
  523. DEBUG ("client thread is now running");
  524. while (err == 0) {
  525. if (client->engine->engine_ok == 0) {
  526. jack_error ("engine unexpectedly shutdown; thread exiting\n");
  527. if (client->on_shutdown) {
  528. client->on_shutdown (client->on_shutdown_arg);
  529. }
  530. pthread_exit (0);
  531. }
  532. DEBUG ("client polling on event_fd and graph_wait_fd...");
  533. if (poll (client->pollfd, client->pollmax, 1000) < 0) {
  534. if (errno == EINTR) {
  535. printf ("poll interrupted\n");
  536. continue;
  537. }
  538. jack_error ("poll failed in client (%s)", strerror (errno));
  539. status = -1;
  540. break;
  541. }
  542. /* get an accurate timestamp on waking from poll for a process()
  543. cycle.
  544. */
  545. if (client->pollfd[1].revents & POLLIN) {
  546. control->awake_at = jack_get_microseconds();
  547. }
  548. if (client->pollfd[0].revents & ~POLLIN || client->control->dead) {
  549. goto zombie;
  550. }
  551. if (client->pollfd[0].revents & POLLIN) {
  552. DEBUG ("client receives an event, now reading on event fd");
  553. /* server has sent us an event. process the event and reply */
  554. if (read (client->event_fd, &event, sizeof (event)) != sizeof (event)) {
  555. jack_error ("cannot read server event (%s)", strerror (errno));
  556. err++;
  557. break;
  558. }
  559. status = 0;
  560. switch (event.type) {
  561. case PortRegistered:
  562. if (control->port_register) {
  563. control->port_register (event.x.port_id, TRUE, control->port_register_arg);
  564. }
  565. break;
  566. case PortUnregistered:
  567. if (control->port_register) {
  568. control->port_register (event.x.port_id, FALSE, control->port_register_arg);
  569. }
  570. break;
  571. case GraphReordered:
  572. status = jack_handle_reorder (client, &event);
  573. break;
  574. case PortConnected:
  575. case PortDisconnected:
  576. status = jack_client_handle_port_connection (client, &event);
  577. break;
  578. case BufferSizeChange:
  579. jack_client_invalidate_port_buffers (client);
  580. if (control->bufsize) {
  581. status = control->bufsize (control->nframes, control->bufsize_arg);
  582. }
  583. break;
  584. case SampleRateChange:
  585. if (control->srate) {
  586. status = control->srate (control->nframes, control->srate_arg);
  587. }
  588. break;
  589. case XRun:
  590. if (control->xrun) {
  591. status = control->xrun (control->xrun_arg);
  592. }
  593. break;
  594. case NewPortType:
  595. jack_client_handle_new_port_type (client, event.x.shm_name, event.z.size, event.y.addr);
  596. break;
  597. }
  598. DEBUG ("client has dealt with the event, writing response on event fd");
  599. if (write (client->event_fd, &status, sizeof (status)) != sizeof (status)) {
  600. jack_error ("cannot send event response to engine (%s)", strerror (errno));
  601. err++;
  602. break;
  603. }
  604. }
  605. if (client->pollfd[1].revents & POLLIN) {
  606. #ifdef WITH_TIMESTAMPS
  607. jack_reset_timestamps ();
  608. #endif
  609. DEBUG ("client %d signalled at %Lu, awake for process at %Lu (delay = %Lu usecs) (wakeup on graph_wait_fd==%d)",
  610. getpid(),
  611. control->signalled_at,
  612. control->awake_at,
  613. control->awake_at - control->signalled_at,
  614. client->pollfd[1].fd);
  615. control->state = Running;
  616. if (control->process) {
  617. if (control->process (control->nframes, control->process_arg) == 0) {
  618. control->state = Finished;
  619. }
  620. } else {
  621. control->state = Finished;
  622. }
  623. control->finished_at = jack_get_microseconds();
  624. #ifdef WITH_TIMESTAMPS
  625. jack_timestamp ("finished");
  626. #endif
  627. /* pass the execution token along */
  628. DEBUG ("client finished processing at %Lu (elapsed = %Lu usecs), writing on graph_next_fd==%d",
  629. control->finished_at,
  630. control->finished_at - control->awake_at,
  631. client->graph_next_fd);
  632. if (write (client->graph_next_fd, &c, sizeof (c)) != sizeof (c)) {
  633. jack_error ("cannot continue execution of the processing graph (%s)", strerror(errno));
  634. err++;
  635. break;
  636. }
  637. DEBUG ("client sent message to next stage by %Lu, client reading on graph_wait_fd==%d",
  638. jack_get_microseconds(), client->graph_wait_fd);
  639. #ifdef WITH_TIMESTAMPS
  640. jack_timestamp ("read pending byte from wait");
  641. #endif
  642. DEBUG("reading cleanup byte from pipe\n");
  643. if ((read (client->graph_wait_fd, &c, sizeof (c)) != sizeof (c))) {
  644. DEBUG ("WARNING: READ FAILED!");
  645. /*
  646. jack_error ("cannot complete execution of the processing graph (%s)", strerror(errno));
  647. err++;
  648. break;
  649. */
  650. }
  651. /* check if we were killed during the process cycle (or whatever) */
  652. if (client->control->dead) {
  653. goto zombie;
  654. }
  655. DEBUG("process cycle fully complete\n");
  656. #ifdef WITH_TIMESTAMPS
  657. jack_timestamp ("read done");
  658. #endif
  659. }
  660. }
  661. return (void *) ((intptr_t)err);
  662. zombie:
  663. if (client->on_shutdown) {
  664. jack_error ("zombified - calling shutdown handler");
  665. client->on_shutdown (client->on_shutdown_arg);
  666. } else {
  667. jack_error ("zombified - exiting from JACK");
  668. jack_client_close (client);
  669. }
  670. pthread_exit (0);
  671. /*NOTREACHED*/
  672. return 0;
  673. }
  674. static int
  675. jack_start_thread (jack_client_t *client)
  676. {
  677. pthread_attr_t *attributes = 0;
  678. #ifdef USE_CAPABILITIES
  679. int policy = SCHED_OTHER;
  680. struct sched_param client_param, temp_param;
  681. #endif
  682. if (client->engine->real_time) {
  683. /* Get the client thread to run as an RT-FIFO
  684. scheduled thread of appropriate priority.
  685. */
  686. struct sched_param rt_param;
  687. attributes = (pthread_attr_t *) malloc (sizeof (pthread_attr_t));
  688. pthread_attr_init (attributes);
  689. if (pthread_attr_setschedpolicy (attributes, SCHED_FIFO)) {
  690. jack_error ("cannot set FIFO scheduling class for RT thread");
  691. return -1;
  692. }
  693. if (pthread_attr_setscope (attributes, PTHREAD_SCOPE_SYSTEM)) {
  694. jack_error ("Cannot set scheduling scope for RT thread");
  695. return -1;
  696. }
  697. memset (&rt_param, 0, sizeof (rt_param));
  698. rt_param.sched_priority = client->engine->client_priority;
  699. if (pthread_attr_setschedparam (attributes, &rt_param)) {
  700. jack_error ("Cannot set scheduling priority for RT thread (%s)", strerror (errno));
  701. return -1;
  702. }
  703. if (mlockall (MCL_CURRENT|MCL_FUTURE)) {
  704. jack_error ("cannot lock down all memory (%s)", strerror (errno));
  705. return -1;
  706. }
  707. }
  708. if (pthread_create (&client->thread, attributes, jack_client_thread, client)) {
  709. #ifdef USE_CAPABILITIES
  710. if (client->engine->real_time && client->engine->has_capabilities) {
  711. /* we are probably dealing with a broken glibc so try
  712. to work around the bug, see below for more details
  713. */
  714. goto capabilities_workaround;
  715. }
  716. #endif
  717. return -1;
  718. }
  719. return 0;
  720. #ifdef USE_CAPABILITIES
  721. /* we get here only with engine running realtime and capabilities */
  722. capabilities_workaround:
  723. /* the version of glibc I've played with has a bug that makes
  724. that code fail when running under a non-root user but with the
  725. proper realtime capabilities (in short, pthread_attr_setschedpolicy
  726. does not check for capabilities, only for the uid being
  727. zero). Newer versions apparently have this fixed. This
  728. workaround temporarily switches the client thread to the
  729. proper scheduler and priority, then starts the realtime
  730. thread so that it can inherit them and finally switches the
  731. client thread back to what it was before. Sigh. For ardour
  732. I have to check again and switch the thread explicitly to
  733. realtime, don't know why or how to debug - nando
  734. */
  735. /* get current scheduler and parameters of the client process */
  736. if ((policy = sched_getscheduler (0)) < 0) {
  737. jack_error ("Cannot get current client scheduler: %s", strerror(errno));
  738. return -1;
  739. }
  740. memset (&client_param, 0, sizeof (client_param));
  741. if (sched_getparam (0, &client_param)) {
  742. jack_error ("Cannot get current client scheduler parameters: %s", strerror(errno));
  743. return -1;
  744. }
  745. /* temporarily change the client process to SCHED_FIFO so that
  746. the realtime thread can inherit the scheduler and priority
  747. */
  748. memset (&temp_param, 0, sizeof (temp_param));
  749. temp_param.sched_priority = client->engine->client_priority;
  750. if (sched_setscheduler(0, SCHED_FIFO, &temp_param)) {
  751. jack_error ("Cannot temporarily set client to RT scheduler: %s", strerror(errno));
  752. return -1;
  753. }
  754. /* prepare the attributes for the realtime thread */
  755. attributes = (pthread_attr_t *) malloc (sizeof (pthread_attr_t));
  756. pthread_attr_init (attributes);
  757. if (pthread_attr_setscope (attributes, PTHREAD_SCOPE_SYSTEM)) {
  758. sched_setscheduler (0, policy, &client_param);
  759. jack_error ("Cannot set scheduling scope for RT thread");
  760. return -1;
  761. }
  762. if (pthread_attr_setinheritsched (attributes, PTHREAD_INHERIT_SCHED)) {
  763. sched_setscheduler (0, policy, &client_param);
  764. jack_error ("Cannot set scheduler inherit policy for RT thread");
  765. return -1;
  766. }
  767. /* create the RT thread */
  768. if (pthread_create (&client->thread, attributes, jack_client_thread, client)) {
  769. sched_setscheduler (0, policy, &client_param);
  770. return -1;
  771. }
  772. /* return the client process to the scheduler it was in before */
  773. if (sched_setscheduler (0, policy, &client_param)) {
  774. jack_error ("Cannot reset original client scheduler: %s", strerror(errno));
  775. return -1;
  776. }
  777. /* check again... inheritance of policy and priority works in jack_simple_client
  778. but not in ardour! So I check again and force the policy if it is not set
  779. correctly. This does not really really work either, the manager thread
  780. of the linuxthreads implementation is left running with SCHED_OTHER,
  781. that is presumably very bad.
  782. */
  783. memset (&client_param, 0, sizeof (client_param));
  784. if (pthread_getschedparam(client->thread, &policy, &client_param) == 0) {
  785. if (policy != SCHED_FIFO) {
  786. /* jack_error ("RT thread did not go SCHED_FIFO, trying again"); */
  787. memset (&client_param, 0, sizeof (client_param));
  788. client_param.sched_priority = client->engine->client_priority;
  789. if (pthread_setschedparam (client->thread, SCHED_FIFO, &client_param)) {
  790. jack_error ("Cannot set (again) FIFO scheduling class for RT thread\n");
  791. return -1;
  792. }
  793. }
  794. }
  795. return 0;
  796. #endif
  797. }
  798. int
  799. jack_activate (jack_client_t *client)
  800. {
  801. jack_request_t req;
  802. /* we need to scribble on our stack to ensure that its memory pages are
  803. * actually mapped (more important for mlockall(2) usage in
  804. * jack_start_thread())
  805. */
  806. #define BIG_ENOUGH_STACK 1048576
  807. char buf[BIG_ENOUGH_STACK];
  808. int i;
  809. for (i = 0; i < BIG_ENOUGH_STACK; i++) {
  810. buf[i] = (char) (i & 0xff);
  811. }
  812. #undef BIG_ENOUGH_STACK
  813. if (client->control->type == ClientInternal || client->control->type == ClientDriver) {
  814. goto startit;
  815. }
  816. /* get the pid of the client process to pass it to engine */
  817. client->control->pid = getpid ();
  818. #ifdef USE_CAPABILITIES
  819. if (client->engine->has_capabilities != 0 &&
  820. client->control->pid != 0 && client->engine->real_time != 0) {
  821. /* we need to ask the engine for realtime capabilities
  822. before trying to start the realtime thread
  823. */
  824. req.type = SetClientCapabilities;
  825. req.x.client_id = client->control->id;
  826. jack_client_deliver_request (client, &req);
  827. if (req.status) {
  828. /* what to do? engine is running realtime, it is using capabilities and has
  829. them (otherwise we would not get an error return) but for some reason it
  830. could not give the client the required capabilities, so for now downgrade
  831. the client so that it still runs, albeit non-realtime - nando
  832. */
  833. jack_error ("could not receive realtime capabilities, client will run non-realtime");
  834. /* XXX wrong, this is a property of the engine
  835. client->engine->real_time = 0;
  836. */
  837. }
  838. }
  839. #endif
  840. if (client->first_active) {
  841. pthread_mutex_init (&client_lock, NULL);
  842. pthread_cond_init (&client_ready, NULL);
  843. pthread_mutex_lock (&client_lock);
  844. if (jack_start_thread (client)) {
  845. pthread_mutex_unlock (&client_lock);
  846. return -1;
  847. }
  848. pthread_cond_wait (&client_ready, &client_lock);
  849. pthread_mutex_unlock (&client_lock);
  850. if (!client->thread_ok) {
  851. jack_error ("could not start client thread");
  852. return -1;
  853. }
  854. client->first_active = FALSE;
  855. }
  856. startit:
  857. req.type = ActivateClient;
  858. req.x.client_id = client->control->id;
  859. return jack_client_deliver_request (client, &req);
  860. }
  861. int
  862. jack_deactivate (jack_client_t *client)
  863. {
  864. jack_request_t req;
  865. req.type = DeactivateClient;
  866. req.x.client_id = client->control->id;
  867. return jack_client_deliver_request (client, &req);
  868. }
  869. int
  870. jack_client_close (jack_client_t *client)
  871. {
  872. JSList *node;
  873. void *status;
  874. if (client->control->active) {
  875. jack_deactivate (client);
  876. }
  877. if (client->control->type == ClientExternal) {
  878. /* stop the thread that communicates with the jack server */
  879. pthread_cancel (client->thread);
  880. pthread_join (client->thread, &status);
  881. munmap ((char *) client->control, sizeof (jack_client_control_t));
  882. munmap ((char *) client->engine, sizeof (jack_control_t));
  883. for (node = client->port_segments; node; node = jack_slist_next (node)) {
  884. jack_port_segment_info_t *si = (jack_port_segment_info_t *) node->data;
  885. munmap ((char *) si->address, si->size);
  886. free (node->data);
  887. }
  888. jack_slist_free (client->port_segments);
  889. if (client->graph_wait_fd) {
  890. close (client->graph_wait_fd);
  891. }
  892. if (client->graph_next_fd) {
  893. close (client->graph_next_fd);
  894. }
  895. close (client->event_fd);
  896. close (client->request_fd);
  897. }
  898. for (node = client->ports; node; node = jack_slist_next (node)) {
  899. free (node->data);
  900. }
  901. jack_slist_free (client->ports);
  902. jack_client_free (client);
  903. return 0;
  904. }
  905. unsigned long jack_get_buffer_size (jack_client_t *client)
  906. {
  907. return client->engine->buffer_size;
  908. }
  909. unsigned long jack_get_sample_rate (jack_client_t *client)
  910. {
  911. return client->engine->current_time.frame_rate;
  912. }
  913. int
  914. jack_connect (jack_client_t *client, const char *source_port, const char *destination_port)
  915. {
  916. jack_request_t req;
  917. req.type = ConnectPorts;
  918. snprintf (req.x.connect.source_port, sizeof (req.x.connect.source_port), "%s", source_port);
  919. snprintf (req.x.connect.destination_port, sizeof (req.x.connect.destination_port), "%s", destination_port);
  920. return jack_client_deliver_request (client, &req);
  921. }
  922. int
  923. jack_port_disconnect (jack_client_t *client, jack_port_t *port)
  924. {
  925. jack_request_t req;
  926. pthread_mutex_lock (&port->connection_lock);
  927. if (port->connections == NULL) {
  928. pthread_mutex_unlock (&port->connection_lock);
  929. return 0;
  930. }
  931. pthread_mutex_unlock (&port->connection_lock);
  932. req.type = DisconnectPort;
  933. req.x.port_info.port_id = port->shared->id;
  934. return jack_client_deliver_request (client, &req);
  935. }
  936. int
  937. jack_disconnect (jack_client_t *client, const char *source_port, const char *destination_port)
  938. {
  939. jack_request_t req;
  940. req.type = DisconnectPorts;
  941. snprintf (req.x.connect.source_port, sizeof (req.x.connect.source_port), "%s", source_port);
  942. snprintf (req.x.connect.destination_port, sizeof (req.x.connect.destination_port), "%s", destination_port);
  943. return jack_client_deliver_request (client, &req);
  944. }
  945. int
  946. jack_engine_takeover_timebase (jack_client_t *client)
  947. {
  948. jack_request_t req;
  949. req.type = SetTimeBaseClient;
  950. req.x.client_id = client->control->id;
  951. return jack_client_deliver_request (client, &req);
  952. }
  953. void
  954. jack_set_error_function (void (*func) (const char *))
  955. {
  956. jack_error_callback = func;
  957. }
  958. int
  959. jack_set_graph_order_callback (jack_client_t *client, JackGraphOrderCallback callback, void *arg)
  960. {
  961. if (client->control->active) {
  962. jack_error ("You cannot set callbacks on an active client.");
  963. return -1;
  964. }
  965. client->control->graph_order = callback;
  966. client->control->graph_order_arg = arg;
  967. return 0;
  968. }
  969. int
  970. jack_set_process_callback (jack_client_t *client, JackProcessCallback callback, void *arg)
  971. {
  972. if (client->control->active) {
  973. jack_error ("You cannot set callbacks on an active client.");
  974. return -1;
  975. }
  976. client->control->process_arg = arg;
  977. client->control->process = callback;
  978. return 0;
  979. }
  980. int
  981. jack_set_buffer_size_callback (jack_client_t *client, JackBufferSizeCallback callback, void *arg)
  982. {
  983. client->control->bufsize_arg = arg;
  984. client->control->bufsize = callback;
  985. return 0;
  986. }
  987. int
  988. jack_set_sample_rate_callback (jack_client_t *client, JackSampleRateCallback callback, void *arg)
  989. {
  990. if (client->control->active) {
  991. jack_error ("You cannot set callbacks on an active client.");
  992. return -1;
  993. }
  994. client->control->srate_arg = arg;
  995. client->control->srate = callback;
  996. /* Now invoke it */
  997. callback (client->engine->current_time.frame_rate, arg);
  998. return 0;
  999. }
  1000. int
  1001. jack_set_port_registration_callback(jack_client_t *client, JackPortRegistrationCallback callback, void *arg)
  1002. {
  1003. if (client->control->active) {
  1004. jack_error ("You cannot set callbacks on an active client.");
  1005. return -1;
  1006. }
  1007. client->control->port_register_arg = arg;
  1008. client->control->port_register = callback;
  1009. return 0;
  1010. }
  1011. int
  1012. jack_get_process_start_fd (jack_client_t *client)
  1013. {
  1014. /* once this has been called, the client thread
  1015. does not sleep on the graph wait fd.
  1016. */
  1017. client->pollmax = 1;
  1018. return client->graph_wait_fd;
  1019. }
  1020. int
  1021. jack_get_process_done_fd (jack_client_t *client)
  1022. {
  1023. return client->graph_next_fd;
  1024. }
  1025. void
  1026. jack_on_shutdown (jack_client_t *client, void (*function)(void *arg), void *arg)
  1027. {
  1028. client->on_shutdown = function;
  1029. client->on_shutdown_arg = arg;
  1030. }
  1031. const char **
  1032. jack_get_ports (jack_client_t *client,
  1033. const char *port_name_pattern,
  1034. const char *type_name_pattern,
  1035. unsigned long flags)
  1036. {
  1037. jack_control_t *engine;
  1038. const char **matching_ports;
  1039. unsigned long match_cnt;
  1040. jack_port_shared_t *psp;
  1041. unsigned long i;
  1042. regex_t port_regex;
  1043. regex_t type_regex;
  1044. int matching;
  1045. engine = client->engine;
  1046. if (port_name_pattern && port_name_pattern[0]) {
  1047. regcomp (&port_regex, port_name_pattern, REG_EXTENDED|REG_NOSUB);
  1048. }
  1049. if (type_name_pattern && type_name_pattern[0]) {
  1050. regcomp (&type_regex, type_name_pattern, REG_EXTENDED|REG_NOSUB);
  1051. }
  1052. psp = engine->ports;
  1053. match_cnt = 0;
  1054. matching_ports = (const char **) malloc (sizeof (char *) * engine->port_max);
  1055. for (i = 0; i < engine->port_max; i++) {
  1056. matching = 1;
  1057. if (!psp[i].in_use) {
  1058. continue;
  1059. }
  1060. if (flags) {
  1061. if ((psp[i].flags & flags) != flags) {
  1062. matching = 0;
  1063. }
  1064. }
  1065. if (matching && port_name_pattern && port_name_pattern[0]) {
  1066. if (regexec (&port_regex, psp[i].name, 0, NULL, 0)) {
  1067. matching = 0;
  1068. }
  1069. }
  1070. if (matching && type_name_pattern && type_name_pattern[0]) {
  1071. if (regexec (&type_regex, psp[i].type_info.type_name, 0, NULL, 0)) {
  1072. matching = 0;
  1073. }
  1074. }
  1075. if (matching) {
  1076. matching_ports[match_cnt++] = psp[i].name;
  1077. }
  1078. }
  1079. matching_ports[match_cnt] = 0;
  1080. if (match_cnt == 0) {
  1081. free (matching_ports);
  1082. matching_ports = 0;
  1083. }
  1084. return matching_ports;
  1085. }
  1086. static inline void
  1087. jack_read_frame_time (const jack_client_t *client, jack_frame_timer_t *copy)
  1088. {
  1089. int tries = 0;
  1090. do {
  1091. /* throttle the busy wait if we don't get
  1092. the answer very quickly.
  1093. */
  1094. if (tries > 10) {
  1095. usleep (20);
  1096. tries = 0;
  1097. }
  1098. *copy = client->engine->frame_timer;
  1099. tries++;
  1100. } while (copy->guard1 != copy->guard2);
  1101. }
  1102. jack_nframes_t
  1103. jack_frames_since_cycle_start (const jack_client_t *client)
  1104. {
  1105. float usecs;
  1106. usecs = jack_get_microseconds() - client->engine->current_time.usecs;
  1107. return (jack_nframes_t) floor ((((float) client->engine->current_time.frame_rate) / 1000000.0f) * usecs);
  1108. }
  1109. jack_nframes_t
  1110. jack_frame_time (const jack_client_t *client)
  1111. {
  1112. jack_frame_timer_t current;
  1113. float usecs;
  1114. jack_nframes_t elapsed;
  1115. jack_read_frame_time (client, &current);
  1116. usecs = jack_get_microseconds() - current.stamp;
  1117. elapsed = (jack_nframes_t) floor ((((float) client->engine->current_time.frame_rate) / 1000000.0f) * usecs);
  1118. return current.frames + elapsed;
  1119. }
  1120. /* TRANSPORT CONTROL */
  1121. void
  1122. jack_get_transport_info (jack_client_t *client,
  1123. jack_transport_info_t *info)
  1124. {
  1125. *info = client->engine->current_time;
  1126. }
  1127. void
  1128. jack_set_transport_info (jack_client_t *client,
  1129. jack_transport_info_t *info)
  1130. {
  1131. client->engine->pending_time = *info;
  1132. }
  1133. float
  1134. jack_cpu_load (jack_client_t *client)
  1135. {
  1136. return client->engine->cpu_load;
  1137. }
  1138. pthread_t
  1139. jack_client_thread_id (jack_client_t *client)
  1140. {
  1141. return client->thread_id;
  1142. }
  1143. #if defined(linux)
  1144. jack_time_t
  1145. jack_get_mhz (void)
  1146. {
  1147. FILE *f = fopen("/proc/cpuinfo", "r");
  1148. if (f == 0)
  1149. {
  1150. perror("can't open /proc/cpuinfo\n");
  1151. exit(1);
  1152. }
  1153. for ( ; ; )
  1154. {
  1155. jack_time_t mhz;
  1156. int ret;
  1157. char buf[1000];
  1158. if (fgets(buf, sizeof(buf), f) == NULL)
  1159. {
  1160. fprintf(stderr, "cannot locate cpu MHz in /proc/cpuinfo\n");
  1161. exit(1);
  1162. }
  1163. #ifdef __powerpc__
  1164. ret = sscanf(buf, "clock\t: %LuMHz", &mhz);
  1165. #else
  1166. ret = sscanf(buf, "cpu MHz : %Lu", &mhz);
  1167. #endif /* __powerpc__ */
  1168. if (ret == 1)
  1169. {
  1170. fclose(f);
  1171. return mhz;
  1172. }
  1173. }
  1174. }
  1175. void jack_init_time ()
  1176. {
  1177. __jack_cpu_mhz = jack_get_mhz ();
  1178. }
  1179. #endif