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.

2084 lines
49KB

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