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.

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