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.

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