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.

1669 lines
38KB

  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 <asm/msr.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. char *jack_temp_dir = "/tmp";
  37. void
  38. jack_set_temp_dir (const char *path)
  39. {
  40. jack_temp_dir = strdup (path);
  41. }
  42. static jack_port_t *jack_port_new (jack_client_t *client, jack_port_id_t port_id, jack_control_t *control);
  43. static pthread_mutex_t client_lock;
  44. static pthread_cond_t client_ready;
  45. void *jack_zero_filled_buffer = 0;
  46. static void jack_audio_port_mixdown (jack_port_t *port, nframes_t nframes);
  47. jack_port_type_info_t builtin_port_types[] = {
  48. { JACK_DEFAULT_AUDIO_TYPE, jack_audio_port_mixdown, 1 },
  49. { "", NULL }
  50. };
  51. struct _jack_client {
  52. jack_control_t *engine;
  53. jack_client_control_t *control;
  54. struct pollfd *pollfd;
  55. int pollmax;
  56. int graph_next_fd;
  57. int request_fd;
  58. GSList *port_segments;
  59. GSList *ports;
  60. pthread_t thread;
  61. char fifo_prefix[PATH_MAX+1];
  62. void (*on_shutdown)(void *arg);
  63. void *on_shutdown_arg;
  64. char thread_ok : 1;
  65. char first_active : 1;
  66. };
  67. #define event_fd pollfd[0].fd
  68. #define graph_wait_fd pollfd[1].fd
  69. typedef struct {
  70. int status;
  71. struct _jack_client *client;
  72. const char *client_name;
  73. } client_info;
  74. static void
  75. default_jack_error (const char *fmt, ...)
  76. {
  77. va_list ap;
  78. va_start (ap, fmt);
  79. vfprintf (stderr, fmt, ap);
  80. va_end (ap);
  81. fputc ('\n', stderr);
  82. }
  83. void (*jack_error)(const char *fmt, ...) = &default_jack_error;
  84. jack_client_t *
  85. jack_client_alloc ()
  86. {
  87. jack_client_t *client;
  88. client = (jack_client_t *) malloc (sizeof (jack_client_t));
  89. client->pollfd = (struct pollfd *) malloc (sizeof (struct pollfd) * 2);
  90. client->pollmax = 2;
  91. client->request_fd = -1;
  92. client->event_fd = -1;
  93. client->graph_wait_fd = -1;
  94. client->graph_next_fd = -1;
  95. client->port_segments = NULL;
  96. client->ports = NULL;
  97. client->engine = NULL;
  98. client->control = 0;
  99. client->thread_ok = FALSE;
  100. client->first_active = TRUE;
  101. client->on_shutdown = NULL;
  102. return client;
  103. }
  104. static jack_port_t *
  105. jack_port_by_id (jack_client_t *client, jack_port_id_t id)
  106. {
  107. GSList *node;
  108. for (node = client->ports; node; node = g_slist_next (node)) {
  109. if (((jack_port_t *) node->data)->shared->id == id) {
  110. return (jack_port_t *) node->data;
  111. }
  112. }
  113. return NULL;
  114. }
  115. jack_port_t *
  116. jack_port_by_name (jack_client_t *client, const char *port_name)
  117. {
  118. unsigned long i, limit;
  119. jack_port_shared_t *port;
  120. limit = client->engine->port_max;
  121. port = &client->engine->ports[0];
  122. for (i = 0; i < limit; i++) {
  123. if (port[i].in_use && strcmp (port[i].name, port_name) == 0) {
  124. return jack_port_new (client, port[i].id, client->engine);
  125. }
  126. }
  127. return NULL;
  128. }
  129. static void
  130. jack_client_invalidate_port_buffers (jack_client_t *client)
  131. {
  132. GSList *node;
  133. jack_port_t *port;
  134. /* This releases all local memory owned by input ports
  135. and sets the buffer pointer to NULL. This will cause
  136. jack_port_get_buffer() to reallocate space for the
  137. buffer on the next call (if there is one).
  138. */
  139. for (node = client->ports; node; node = g_slist_next (node)) {
  140. port = (jack_port_t *) node->data;
  141. if (port->shared->flags & JackPortIsInput) {
  142. if (port->client_segment_base == 0) {
  143. jack_pool_release ((void *) port->shared->offset);
  144. port->client_segment_base = 0;
  145. port->shared->offset = 0;
  146. }
  147. }
  148. }
  149. }
  150. int
  151. jack_client_handle_port_connection (jack_client_t *client, jack_event_t *event)
  152. {
  153. jack_port_t *control_port;
  154. jack_port_t *other;
  155. GSList *node;
  156. switch (event->type) {
  157. case PortConnected:
  158. other = jack_port_new (client, event->y.other_id, client->engine);
  159. control_port = jack_port_by_id (client, event->x.self_id);
  160. pthread_mutex_lock (&control_port->connection_lock);
  161. control_port->connections = g_slist_prepend (control_port->connections, other);
  162. pthread_mutex_unlock (&control_port->connection_lock);
  163. break;
  164. case PortDisconnected:
  165. control_port = jack_port_by_id (client, event->x.self_id);
  166. pthread_mutex_lock (&control_port->connection_lock);
  167. for (node = control_port->connections; node; node = g_slist_next (node)) {
  168. other = (jack_port_t *) node->data;
  169. if (other->shared->id == event->y.other_id) {
  170. control_port->connections = g_slist_remove_link (control_port->connections, node);
  171. g_slist_free_1 (node);
  172. free (other);
  173. break;
  174. }
  175. }
  176. pthread_mutex_unlock (&control_port->connection_lock);
  177. break;
  178. default:
  179. /* impossible */
  180. break;
  181. }
  182. return 0;
  183. }
  184. static int
  185. jack_handle_reorder (jack_client_t *client, jack_event_t *event)
  186. {
  187. char path[PATH_MAX+1];
  188. if (client->graph_wait_fd >= 0) {
  189. close (client->graph_wait_fd);
  190. client->graph_wait_fd = -1;
  191. }
  192. if (client->graph_next_fd >= 0) {
  193. close (client->graph_next_fd);
  194. client->graph_next_fd = -1;
  195. }
  196. sprintf (path, "%s-%lu", client->fifo_prefix, event->x.n);
  197. if ((client->graph_wait_fd = open (path, O_RDONLY)) <= 0) {
  198. jack_error ("cannot open specified fifo [%s] for reading (%s)", path, strerror (errno));
  199. return -1;
  200. }
  201. sprintf (path, "%s-%lu", client->fifo_prefix, event->x.n+1);
  202. if ((client->graph_next_fd = open (path, O_WRONLY)) < 0) {
  203. jack_error ("cannot open specified fifo [%s] for writing (%s)", path, strerror (errno));
  204. return -1;
  205. }
  206. return 0;
  207. }
  208. static int
  209. server_connect (int which)
  210. {
  211. int fd;
  212. struct sockaddr_un addr;
  213. if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) {
  214. jack_error ("cannot create client socket (%s)", strerror (errno));
  215. return -1;
  216. }
  217. addr.sun_family = AF_UNIX;
  218. g_snprintf (addr.sun_path, sizeof (addr.sun_path) - 1, "%s/jack_%d", jack_temp_dir, which);
  219. if (connect (fd, (struct sockaddr *) &addr, sizeof (addr)) < 0) {
  220. jack_error ("cannot connect to jack server", strerror (errno));
  221. close (fd);
  222. return -1;
  223. }
  224. return fd;
  225. }
  226. static int
  227. server_event_connect (jack_client_t *client)
  228. {
  229. int fd;
  230. struct sockaddr_un addr;
  231. jack_client_connect_ack_request_t req;
  232. jack_client_connect_ack_result_t res;
  233. if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) {
  234. jack_error ("cannot create client event socket (%s)", strerror (errno));
  235. return -1;
  236. }
  237. addr.sun_family = AF_UNIX;
  238. g_snprintf (addr.sun_path, sizeof (addr.sun_path) - 1, "%s/jack_ack_0", jack_temp_dir);
  239. if (connect (fd, (struct sockaddr *) &addr, sizeof (addr)) < 0) {
  240. jack_error ("cannot connect to jack server for events", strerror (errno));
  241. close (fd);
  242. return -1;
  243. }
  244. req.client_id = client->control->id;
  245. if (write (fd, &req, sizeof (req)) != sizeof (req)) {
  246. jack_error ("cannot write event connect request to server (%s)", strerror (errno));
  247. close (fd);
  248. return -1;
  249. }
  250. if (read (fd, &res, sizeof (res)) != sizeof (res)) {
  251. jack_error ("cannot read event connect result from server (%s)", strerror (errno));
  252. close (fd);
  253. return -1;
  254. }
  255. if (res.status != 0) {
  256. close (fd);
  257. return -1;
  258. }
  259. return fd;
  260. }
  261. jack_client_t *
  262. jack_client_new (const char *client_name)
  263. {
  264. int req_fd = -1;
  265. int ev_fd = -1;
  266. void *addr;
  267. jack_client_connect_request_t req;
  268. jack_client_connect_result_t res;
  269. jack_port_segment_info_t *si;
  270. jack_client_t *client;
  271. int client_shm_id;
  272. int control_shm_id;
  273. int port_segment_shm_id;
  274. int n;
  275. if (strlen (client_name) > sizeof (req.name) - 1) {
  276. jack_error ("\"%s\" is too long to be used as a JACK client name.\n"
  277. "Please use %lu characters or less.",
  278. sizeof (req.name) - 1);
  279. return NULL;
  280. }
  281. if ((req_fd = server_connect (0)) < 0) {
  282. jack_error ("cannot connect to default JACK server");
  283. return NULL;
  284. }
  285. req.type = ClientOutOfProcess;
  286. strncpy (req.name, client_name, sizeof (req.name) - 1);
  287. if (write (req_fd, &req, sizeof (req)) != sizeof (req)) {
  288. jack_error ("cannot send request to jack server (%s)", strerror (errno));
  289. close (req_fd);
  290. return NULL;
  291. }
  292. if ((n = read (req_fd, &res, sizeof (res))) != sizeof (res)) {
  293. jack_error ("cannot read response from jack server (%s)", strerror (errno));
  294. close (req_fd);
  295. return NULL;
  296. }
  297. if (res.status) {
  298. close (req_fd);
  299. jack_error ("could not attach as client");
  300. return NULL;
  301. }
  302. client = jack_client_alloc ();
  303. strcpy (client->fifo_prefix, res.fifo_prefix);
  304. client->request_fd = req_fd;
  305. client->pollfd[0].events = POLLIN|POLLERR|POLLHUP|POLLNVAL;
  306. client->pollfd[1].events = POLLIN|POLLERR|POLLHUP|POLLNVAL;
  307. /* Lookup, attach and register the port/buffer segments in use
  308. right now.
  309. */
  310. if ((port_segment_shm_id = shmget (res.port_segment_key, 0, 0)) < 0) {
  311. jack_error ("cannot determine shared memory segment for port segment key 0x%x (%s)", res.port_segment_key, strerror (errno));
  312. goto fail;
  313. }
  314. if ((addr = shmat (port_segment_shm_id, 0, 0)) == (void *) -1) {
  315. jack_error ("cannot attached port segment shared memory (%s)", strerror (errno));
  316. goto fail;
  317. }
  318. si = (jack_port_segment_info_t *) malloc (sizeof (jack_port_segment_info_t));
  319. si->shm_key = res.port_segment_key;
  320. si->address = addr;
  321. /* the first chunk of the first port segment is always set by the engine
  322. to be a conveniently-sized, zero-filled lump of memory.
  323. */
  324. if (client->port_segments == NULL) {
  325. jack_zero_filled_buffer = si->address;
  326. }
  327. client->port_segments = g_slist_prepend (client->port_segments, si);
  328. /* attach the engine control/info block */
  329. if ((control_shm_id = shmget (res.control_key, 0, 0)) < 0) {
  330. jack_error ("cannot determine shared memory segment for control key 0x%x", res.control_key);
  331. goto fail;
  332. }
  333. if ((addr = shmat (control_shm_id, 0, 0)) == (void *) -1) {
  334. jack_error ("cannot attached engine control shared memory segment");
  335. goto fail;
  336. }
  337. client->engine = (jack_control_t *) addr;
  338. /* now attach the client control block */
  339. if ((client_shm_id = shmget (res.client_key, 0, 0)) < 0) {
  340. jack_error ("cannot determine shared memory segment for client key 0x%x", res.client_key);
  341. goto fail;
  342. }
  343. if ((addr = shmat (client_shm_id, 0, 0)) == (void *) -1) {
  344. jack_error ("cannot attached client control shared memory segment");
  345. goto fail;
  346. }
  347. client->control = (jack_client_control_t *) addr;
  348. if ((ev_fd = server_event_connect (client)) < 0) {
  349. jack_error ("cannot connect to server for event stream (%s)", strerror (errno));
  350. goto fail;
  351. }
  352. client->event_fd = ev_fd;
  353. return client;
  354. fail:
  355. if (client->engine) {
  356. shmdt (client->engine);
  357. }
  358. if (client->control) {
  359. shmdt ((char *) client->control);
  360. }
  361. if (req_fd >= 0) {
  362. close (req_fd);
  363. }
  364. if (ev_fd >= 0) {
  365. close (ev_fd);
  366. }
  367. return 0;
  368. }
  369. static void *
  370. jack_client_thread (void *arg)
  371. {
  372. jack_client_t *client = (jack_client_t *) arg;
  373. jack_client_control_t *control = client->control;
  374. jack_event_t event;
  375. char status = 0;
  376. char c;
  377. int err = 0;
  378. pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  379. pthread_mutex_lock (&client_lock);
  380. client->thread_ok = TRUE;
  381. pthread_cond_signal (&client_ready);
  382. pthread_mutex_unlock (&client_lock);
  383. while (err == 0) {
  384. if (poll (client->pollfd, client->pollmax, 1000) < 0) {
  385. if (errno == EINTR) {
  386. printf ("poll interrupted\n");
  387. continue;
  388. }
  389. jack_error ("poll failed in client (%s)", strerror (errno));
  390. status = -1;
  391. break;
  392. }
  393. if (client->pollfd[0].revents & ~POLLIN) {
  394. jack_error ("engine has shut down socket; thread exiting");
  395. if (client->on_shutdown) {
  396. client->on_shutdown (client->on_shutdown_arg);
  397. }
  398. pthread_exit (0);
  399. }
  400. if (client->pollfd[0].revents & POLLIN) {
  401. /* server has sent us an event. process the event and reply */
  402. if (read (client->event_fd, &event, sizeof (event)) != sizeof (event)) {
  403. jack_error ("cannot read server event (%s)", strerror (errno));
  404. err++;
  405. break;
  406. }
  407. status = 0;
  408. switch (event.type) {
  409. case PortRegistered:
  410. if (control->port_register) {
  411. control->port_register (event.x.port_id, TRUE, control->port_register_arg);
  412. }
  413. break;
  414. case PortUnregistered:
  415. if (control->port_register) {
  416. control->port_register (event.x.port_id, FALSE, control->port_register_arg);
  417. }
  418. break;
  419. case GraphReordered:
  420. status = jack_handle_reorder (client, &event);
  421. break;
  422. case PortConnected:
  423. case PortDisconnected:
  424. status = jack_client_handle_port_connection (client, &event);
  425. break;
  426. case BufferSizeChange:
  427. jack_client_invalidate_port_buffers (client);
  428. if (control->bufsize) {
  429. status = control->bufsize (control->nframes, control->bufsize_arg);
  430. }
  431. break;
  432. case SampleRateChange:
  433. if (control->srate) {
  434. status = control->srate (control->nframes, control->srate_arg);
  435. }
  436. break;
  437. case NewPortBufferSegment:
  438. break;
  439. }
  440. if (write (client->event_fd, &status, sizeof (status)) != sizeof (status)) {
  441. jack_error ("cannot send event response to engine (%s)", strerror (errno));
  442. err++;
  443. break;
  444. }
  445. }
  446. if (client->pollfd[1].revents & POLLIN) {
  447. /* the previous stage of the graph has told us to
  448. process().
  449. */
  450. if (read (client->graph_wait_fd, &c, sizeof (c)) != sizeof (c)) {
  451. jack_error ("cannot clean up byte from inter-client pipe (%s)", strerror (errno));
  452. err++;
  453. break;
  454. }
  455. control->state = Running;
  456. if (control->process) {
  457. if (control->process (control->nframes, control->process_arg) == 0) {
  458. control->state = Finished;
  459. }
  460. } else {
  461. control->state = Finished;
  462. }
  463. /* this may fail. if it does, the engine will discover
  464. it due a cycle timeout, which is about
  465. the best we can do without a lot of mostly wasted
  466. effort.
  467. */
  468. write (client->graph_next_fd, &c, 1);
  469. }
  470. }
  471. return (void *) err;
  472. }
  473. static int
  474. jack_start_thread (jack_client_t *client)
  475. {
  476. pthread_attr_t *attributes = 0;
  477. if (client->engine->real_time) {
  478. /* Get the client thread to run as an RT-FIFO
  479. scheduled thread of appropriate priority.
  480. */
  481. struct sched_param rt_param;
  482. attributes = (pthread_attr_t *) malloc (sizeof (pthread_attr_t));
  483. pthread_attr_init (attributes);
  484. if (pthread_attr_setschedpolicy (attributes, SCHED_FIFO)) {
  485. jack_error ("cannot set FIFO scheduling class for RT thread");
  486. return -1;
  487. }
  488. if (pthread_attr_setscope (attributes, PTHREAD_SCOPE_SYSTEM)) {
  489. jack_error ("Cannot set scheduling scope for RT thread");
  490. return -1;
  491. }
  492. memset (&rt_param, 0, sizeof (rt_param));
  493. rt_param.sched_priority = client->engine->client_priority;
  494. if (pthread_attr_setschedparam (attributes, &rt_param)) {
  495. jack_error ("Cannot set scheduling priority for RT thread (%s)", strerror (errno));
  496. return -1;
  497. }
  498. if (mlockall (MCL_CURRENT|MCL_FUTURE)) {
  499. jack_error ("cannot lock down all memory (%s)", strerror (errno));
  500. return -1;
  501. }
  502. }
  503. if (pthread_create (&client->thread, attributes, jack_client_thread, client)) {
  504. return -1;
  505. }
  506. return 0;
  507. }
  508. int
  509. jack_activate (jack_client_t *client)
  510. {
  511. jack_request_t req;
  512. if (client->control->type == ClientOutOfProcess && client->first_active) {
  513. pthread_mutex_init (&client_lock, NULL);
  514. pthread_cond_init (&client_ready, NULL);
  515. pthread_mutex_lock (&client_lock);
  516. if (jack_start_thread (client)) {
  517. pthread_mutex_unlock (&client_lock);
  518. return -1;
  519. }
  520. pthread_cond_wait (&client_ready, &client_lock);
  521. pthread_mutex_unlock (&client_lock);
  522. if (!client->thread_ok) {
  523. jack_error ("could not start client thread");
  524. return -1;
  525. }
  526. client->first_active = FALSE;
  527. }
  528. req.type = ActivateClient;
  529. req.x.client_id = client->control->id;
  530. if (write (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  531. jack_error ("cannot send activate client request to server");
  532. return -1;
  533. }
  534. if (read (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  535. jack_error ("cannot read activate client result from server (%s)", strerror (errno));
  536. return -1;
  537. }
  538. return req.status;
  539. }
  540. int
  541. jack_deactivate (jack_client_t *client)
  542. {
  543. jack_request_t req;
  544. req.type = DeactivateClient;
  545. req.x.client_id = client->control->id;
  546. if (write (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  547. jack_error ("cannot send activate client request to server");
  548. return -1;
  549. }
  550. if (read (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  551. jack_error ("cannot read activate client result from server (%s)", strerror (errno));
  552. return -1;
  553. }
  554. return req.status;
  555. }
  556. int
  557. jack_client_close (jack_client_t *client)
  558. {
  559. GSList *node;
  560. void *status;
  561. /* stop the thread that communicates with the jack server */
  562. pthread_cancel (client->thread);
  563. pthread_join (client->thread, &status);
  564. shmdt ((char *) client->control);
  565. shmdt (client->engine);
  566. for (node = client->port_segments; node; node = g_slist_next (node)) {
  567. shmdt (((jack_port_segment_info_t *) node->data)->address);
  568. free (node->data);
  569. }
  570. g_slist_free (client->port_segments);
  571. for (node = client->ports; node; node = g_slist_next (node)) {
  572. free (node->data);
  573. }
  574. g_slist_free (client->ports);
  575. if (client->graph_wait_fd) {
  576. close (client->graph_wait_fd);
  577. }
  578. if (client->graph_next_fd) {
  579. close (client->graph_next_fd);
  580. }
  581. close (client->event_fd);
  582. close (client->request_fd);
  583. free (client->pollfd);
  584. free (client);
  585. return 0;
  586. }
  587. int
  588. jack_load_client (const char *client_name, const char *path_to_so)
  589. {
  590. int fd;
  591. jack_client_connect_request_t req;
  592. jack_client_connect_result_t res;
  593. if ((fd = server_connect (0)) < 0) {
  594. jack_error ("cannot connect to jack server");
  595. return 0;
  596. }
  597. req.type = ClientDynamic;
  598. strncpy (req.name, client_name, sizeof (req.name) - 1);
  599. req.name[sizeof(req.name)-1] = '\0';
  600. strncpy (req.object_path, path_to_so, sizeof (req.name) - 1);
  601. req.object_path[sizeof(req.object_path)-1] = '\0';
  602. if (write (fd, &req, sizeof (req)) != sizeof (req)) {
  603. jack_error ("cannot send request to jack server (%s)", strerror (errno));
  604. close (fd);
  605. return 0;
  606. }
  607. if (read (fd, &res, sizeof (res)) != sizeof (res)) {
  608. jack_error ("cannot read response from jack server (%s)", strerror (errno));
  609. close (fd);
  610. return 0;
  611. }
  612. close (fd);
  613. return res.status;
  614. }
  615. jack_client_t *
  616. jack_driver_become_client (const char *client_name)
  617. {
  618. int fd;
  619. jack_client_connect_request_t req;
  620. jack_client_connect_result_t res;
  621. jack_client_t *client = 0;
  622. int port_segment_shm_id;
  623. jack_port_segment_info_t *si;
  624. void *addr;
  625. if ((fd = server_connect (0)) < 0) {
  626. jack_error ("cannot connect to jack server");
  627. return 0;
  628. }
  629. req.type = ClientDriver;
  630. strncpy (req.name, client_name, sizeof (req.name) - 1);
  631. req.name[sizeof(req.name)-1] = '\0';
  632. if (write (fd, &req, sizeof (req)) != sizeof (req)) {
  633. jack_error ("cannot send request to jack server (%s)", strerror (errno));
  634. close (fd);
  635. return 0;
  636. }
  637. if (read (fd, &res, sizeof (res)) != sizeof (res)) {
  638. jack_error ("cannot read response from jack server (%s)", strerror (errno));
  639. close (fd);
  640. return 0;
  641. }
  642. if (res.status) {
  643. return 0;
  644. }
  645. client = jack_client_alloc ();
  646. client->request_fd = fd;
  647. client->control = res.client_control;
  648. client->engine = res.engine_control;
  649. /* Lookup, attach and register the port/buffer segments in use
  650. right now.
  651. */
  652. if ((port_segment_shm_id = shmget (res.port_segment_key, 0, 0)) < 0) {
  653. jack_error ("cannot determine shared memory segment for port segment key 0x%x (%s)", res.port_segment_key, strerror (errno));
  654. return NULL;
  655. }
  656. if ((addr = shmat (port_segment_shm_id, 0, 0)) == (void *) -1) {
  657. jack_error ("cannot attached port segment shared memory (%s)", strerror (errno));
  658. return NULL;
  659. }
  660. si = (jack_port_segment_info_t *) malloc (sizeof (jack_port_segment_info_t));
  661. si->shm_key = res.port_segment_key;
  662. si->address = addr;
  663. /* the first chunk of the first port segment is always set by the engine
  664. to be a conveniently-sized, zero-filled lump of memory.
  665. */
  666. if (client->port_segments == NULL) {
  667. jack_zero_filled_buffer = si->address;
  668. }
  669. client->port_segments = g_slist_prepend (client->port_segments, si);
  670. /* allow the engine to act on the client's behalf
  671. when dealing with in-process clients.
  672. */
  673. client->control->private_internal_client = client;
  674. return client;
  675. }
  676. unsigned long jack_get_buffer_size (jack_client_t *client)
  677. {
  678. return client->engine->buffer_size;
  679. }
  680. unsigned long jack_get_sample_rate (jack_client_t *client)
  681. {
  682. return client->engine->time.frame_rate;
  683. }
  684. static jack_port_t *
  685. jack_port_new (jack_client_t *client, jack_port_id_t port_id, jack_control_t *control)
  686. {
  687. jack_port_t *port;
  688. jack_port_shared_t *shared;
  689. jack_port_segment_info_t *si;
  690. GSList *node;
  691. shared = &control->ports[port_id];
  692. port = (jack_port_t *) malloc (sizeof (jack_port_t));
  693. port->client_segment_base = 0;
  694. port->shared = shared;
  695. pthread_mutex_init (&port->connection_lock, NULL);
  696. port->connections = 0;
  697. port->tied = NULL;
  698. si = NULL;
  699. for (node = client->port_segments; node; node = g_slist_next (node)) {
  700. si = (jack_port_segment_info_t *) node->data;
  701. if (si->shm_key == port->shared->shm_key) {
  702. break;
  703. }
  704. }
  705. if (si == NULL) {
  706. jack_error ("cannot find port segment to match newly registered port\n");
  707. return NULL;
  708. }
  709. port->client_segment_base = si->address;
  710. return port;
  711. }
  712. jack_port_t *
  713. jack_port_register (jack_client_t *client,
  714. const char *port_name,
  715. const char *port_type,
  716. unsigned long flags,
  717. unsigned long buffer_size)
  718. {
  719. jack_request_t req;
  720. jack_port_t *port = 0;
  721. jack_port_type_info_t *type_info;
  722. int n;
  723. req.type = RegisterPort;
  724. strcpy ((char *) req.x.port_info.name, (const char *) client->control->name);
  725. strcat ((char *) req.x.port_info.name, ":");
  726. strcat ((char *) req.x.port_info.name, port_name);
  727. strncpy (req.x.port_info.type, port_type, sizeof (req.x.port_info.type) - 1);
  728. req.x.port_info.flags = flags;
  729. req.x.port_info.buffer_size = buffer_size;
  730. req.x.port_info.client_id = client->control->id;
  731. if (write (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  732. jack_error ("cannot send port registration request to server");
  733. return 0;
  734. }
  735. if (read (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  736. jack_error ("cannot read port registration result from server");
  737. return 0;
  738. }
  739. if (req.status != 0) {
  740. return NULL;
  741. }
  742. port = jack_port_new (client, req.x.port_info.port_id, client->engine);
  743. type_info = NULL;
  744. for (n = 0; builtin_port_types[n].type_name[0]; n++) {
  745. if (strcmp (req.x.port_info.type, builtin_port_types[n].type_name) == 0) {
  746. type_info = &builtin_port_types[n];
  747. break;
  748. }
  749. }
  750. if (type_info == NULL) {
  751. /* not a builtin type, so allocate a new type_info structure,
  752. and fill it appropriately.
  753. */
  754. type_info = (jack_port_type_info_t *) malloc (sizeof (jack_port_type_info_t));
  755. snprintf ((char *) type_info->type_name, sizeof (type_info->type_name), req.x.port_info.type);
  756. type_info->mixdown = NULL; /* we have no idea how to mix this */
  757. type_info->buffer_scale_factor = -1; /* use specified port buffer size */
  758. }
  759. memcpy (&port->shared->type_info, type_info, sizeof (jack_port_type_info_t));
  760. client->ports = g_slist_prepend (client->ports, port);
  761. return port;
  762. }
  763. int
  764. jack_port_unregister (jack_client_t *client, jack_port_t *port)
  765. {
  766. jack_request_t req;
  767. req.type = UnRegisterPort;
  768. req.x.port_info.port_id = port->shared->id;
  769. req.x.port_info.client_id = client->control->id;
  770. if (write (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  771. jack_error ("cannot send port registration request to server");
  772. return -1;
  773. }
  774. if (read (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  775. jack_error ("cannot read port registration result from server");
  776. return -1;
  777. }
  778. return req.status;
  779. }
  780. int
  781. jack_connect (jack_client_t *client, const char *source_port, const char *destination_port)
  782. {
  783. jack_request_t req;
  784. req.type = ConnectPorts;
  785. strncpy (req.x.connect.source_port, source_port, sizeof (req.x.connect.source_port) - 1);
  786. req.x.connect.source_port[sizeof(req.x.connect.source_port) - 1] = '\0';
  787. strncpy (req.x.connect.destination_port, destination_port, sizeof (req.x.connect.destination_port) - 1);
  788. req.x.connect.destination_port[sizeof(req.x.connect.destination_port) - 1] = '\0';
  789. if (write (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  790. jack_error ("cannot send port connection request to server");
  791. return -1;
  792. }
  793. if (read (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  794. jack_error ("cannot read port connection result from server");
  795. return -1;
  796. }
  797. return req.status;
  798. }
  799. int
  800. jack_port_disconnect (jack_client_t *client, jack_port_t *port)
  801. {
  802. jack_request_t req;
  803. pthread_mutex_lock (&port->connection_lock);
  804. if (port->connections == NULL) {
  805. pthread_mutex_unlock (&port->connection_lock);
  806. return 0;
  807. }
  808. pthread_mutex_unlock (&port->connection_lock);
  809. req.type = DisconnectPort;
  810. req.x.port_info.port_id = port->shared->id;
  811. if (write (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  812. jack_error ("cannot send port disconnect request to server");
  813. return -1;
  814. }
  815. if (read (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  816. jack_error ("cannot read port disconnect result from server");
  817. return -1;
  818. }
  819. return req.status;
  820. }
  821. int
  822. jack_disconnect (jack_client_t *client, const char *source_port, const char *destination_port)
  823. {
  824. jack_request_t req;
  825. req.type = DisconnectPorts;
  826. strncpy (req.x.connect.source_port, source_port, sizeof (req.x.connect.source_port) - 1);
  827. req.x.connect.source_port[sizeof(req.x.connect.source_port) - 1] = '\0';
  828. strncpy (req.x.connect.destination_port, destination_port, sizeof (req.x.connect.destination_port) - 1);
  829. req.x.connect.destination_port[sizeof(req.x.connect.destination_port) - 1] = '\0';
  830. if (write (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  831. jack_error ("cannot send port connection request to server");
  832. return -1;
  833. }
  834. if (read (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  835. jack_error ("cannot read port connection result from server");
  836. return -1;
  837. }
  838. return req.status;
  839. }
  840. int
  841. jack_engine_takeover_timebase (jack_client_t *client)
  842. {
  843. jack_request_t req;
  844. req.type = SetTimeBaseClient;
  845. req.x.client_id = client->control->id;
  846. if (write (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  847. jack_error ("cannot send set time base request to server");
  848. return -1;
  849. }
  850. if (read (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  851. jack_error ("cannot read set time base result from server");
  852. return -1;
  853. }
  854. return req.status;
  855. }
  856. void
  857. jack_update_time (jack_client_t *client, nframes_t time)
  858. {
  859. client->control->frame_time = time;
  860. }
  861. void
  862. jack_set_error_function (void (*func) (const char *, ...))
  863. {
  864. jack_error = func;
  865. }
  866. nframes_t
  867. jack_port_get_latency (jack_port_t *port)
  868. {
  869. return port->shared->latency;
  870. }
  871. void
  872. jack_port_set_latency (jack_port_t *port, nframes_t nframes)
  873. {
  874. port->shared->latency = nframes;
  875. }
  876. void *
  877. jack_port_get_buffer (jack_port_t *port, nframes_t nframes)
  878. {
  879. GSList *node, *next;
  880. /* Output port. The buffer was assigned by the engine
  881. when the port was registered.
  882. */
  883. if (port->shared->flags & JackPortIsOutput) {
  884. if (port->tied) {
  885. return jack_port_get_buffer (port->tied, nframes);
  886. }
  887. return jack_port_buffer (port);
  888. }
  889. /* Input port.
  890. */
  891. /* since this can only be called from the process() callback,
  892. and since no connections can be made/broken during this
  893. phase (enforced by the jack server), there is no need
  894. to take the connection lock here
  895. */
  896. if ((node = port->connections) == NULL) {
  897. /* no connections; return a zero-filled buffer */
  898. return jack_zero_filled_buffer;
  899. }
  900. if ((next = g_slist_next (node)) == NULL) {
  901. /* one connection: use zero-copy mode - just pass
  902. the buffer of the connected (output) port.
  903. */
  904. return jack_port_buffer (((jack_port_t *) node->data));
  905. }
  906. /* multiple connections. use a local buffer and mixdown
  907. the incoming data to that buffer. we have already
  908. established the existence of a mixdown function
  909. during the connection process.
  910. no port can have an offset of 0 - that offset refers
  911. to the zero-filled area at the start of a shared port
  912. segment area. so, use the offset to store the location
  913. of a locally allocated buffer, and reset the client_segment_base
  914. so that the jack_port_buffer() computation works correctly.
  915. */
  916. if (port->shared->offset == 0) {
  917. port->shared->offset = (size_t) jack_pool_alloc (port->shared->type_info.buffer_scale_factor *
  918. sizeof (sample_t) * nframes);
  919. port->client_segment_base = 0;
  920. }
  921. port->shared->type_info.mixdown (port, nframes);
  922. return (sample_t *) port->shared->offset;
  923. }
  924. int
  925. jack_port_tie (jack_port_t *src, jack_port_t *dst)
  926. {
  927. if (dst->shared->client_id != src->shared->client_id) {
  928. jack_error ("cannot tie ports not owned by the same client");
  929. return -1;
  930. }
  931. if (dst->shared->flags & JackPortIsOutput) {
  932. jack_error ("cannot tie an input port");
  933. return -1;
  934. }
  935. dst->tied = src;
  936. return 0;
  937. }
  938. int
  939. jack_port_untie (jack_port_t *port)
  940. {
  941. if (port->tied == NULL) {
  942. jack_error ("port \"%s\" is not tied", port->shared->name);
  943. return -1;
  944. }
  945. port->tied = NULL;
  946. return 0;
  947. }
  948. int
  949. jack_set_process_callback (jack_client_t *client, JackProcessCallback callback, void *arg)
  950. {
  951. if (client->control->active) {
  952. return -1;
  953. }
  954. client->control->process_arg = arg;
  955. client->control->process = callback;
  956. return 0;
  957. }
  958. int
  959. jack_set_buffer_size_callback (jack_client_t *client, JackBufferSizeCallback callback, void *arg)
  960. {
  961. if (client->control->active) {
  962. return -1;
  963. }
  964. client->control->bufsize_arg = arg;
  965. client->control->bufsize = callback;
  966. /* Now invoke it */
  967. callback (client->engine->buffer_size, arg);
  968. return 0;
  969. }
  970. int
  971. jack_set_sample_rate_callback (jack_client_t *client, JackSampleRateCallback callback, void *arg)
  972. {
  973. if (client->control->active) {
  974. return -1;
  975. }
  976. client->control->srate_arg = arg;
  977. client->control->srate = callback;
  978. /* Now invoke it */
  979. callback (client->engine->time.frame_rate, arg);
  980. return 0;
  981. }
  982. int
  983. jack_set_port_registration_callback(jack_client_t *client, JackPortRegistrationCallback callback, void *arg)
  984. {
  985. if (client->control->active) {
  986. return -1;
  987. }
  988. client->control->port_register_arg = arg;
  989. client->control->port_register = callback;
  990. return 0;
  991. }
  992. int
  993. jack_get_process_start_fd (jack_client_t *client)
  994. {
  995. /* once this has been called, the client thread
  996. does not sleep on the graph wait fd.
  997. */
  998. client->pollmax = 1;
  999. return client->graph_wait_fd;
  1000. }
  1001. int
  1002. jack_get_process_done_fd (jack_client_t *client)
  1003. {
  1004. return client->graph_next_fd;
  1005. }
  1006. int
  1007. jack_port_request_monitor_by_name (jack_client_t *client, const char *port_name, int onoff)
  1008. {
  1009. jack_port_t *port;
  1010. unsigned long i, limit;
  1011. jack_port_shared_t *ports;
  1012. limit = client->engine->port_max;
  1013. ports = &client->engine->ports[0];
  1014. for (i = 0; i < limit; i++) {
  1015. if (ports[i].in_use && strcmp (ports[i].name, port_name) == 0) {
  1016. port = jack_port_new (client, ports[i].id, client->engine);
  1017. return jack_port_request_monitor (port, onoff);
  1018. free (port);
  1019. return 0;
  1020. }
  1021. }
  1022. return -1;
  1023. }
  1024. int
  1025. jack_port_request_monitor (jack_port_t *port, int onoff)
  1026. {
  1027. if (onoff) {
  1028. port->shared->monitor_requests++;
  1029. } else if (port->shared->monitor_requests) {
  1030. port->shared->monitor_requests--;
  1031. }
  1032. if ((port->shared->flags & JackPortIsOutput) == 0) {
  1033. GSList *node;
  1034. /* this port is for input, so recurse over each of the
  1035. connected ports.
  1036. */
  1037. pthread_mutex_lock (&port->connection_lock);
  1038. for (node = port->connections; node; node = g_slist_next (node)) {
  1039. /* drop the lock because if there is a feedback loop,
  1040. we will deadlock. XXX much worse things will
  1041. happen if there is a feedback loop !!!
  1042. */
  1043. pthread_mutex_unlock (&port->connection_lock);
  1044. jack_port_request_monitor ((jack_port_t *) node->data, onoff);
  1045. pthread_mutex_lock (&port->connection_lock);
  1046. }
  1047. pthread_mutex_unlock (&port->connection_lock);
  1048. }
  1049. return 0;
  1050. }
  1051. int
  1052. jack_ensure_port_monitor_input (jack_port_t *port, int yn)
  1053. {
  1054. printf ("ENSURE PORT monitor, req = %d, status = %d\n", yn, port->shared->monitor_requests);
  1055. if (yn) {
  1056. if (port->shared->monitor_requests == 0) {
  1057. port->shared->monitor_requests++;
  1058. }
  1059. } else {
  1060. if (port->shared->monitor_requests == 1) {
  1061. port->shared->monitor_requests--;
  1062. }
  1063. }
  1064. return 0;
  1065. }
  1066. int
  1067. jack_port_monitoring_input (jack_port_t *port)
  1068. {
  1069. return port->shared->monitor_requests > 0;
  1070. }
  1071. const char *
  1072. jack_port_name (const jack_port_t *port)
  1073. {
  1074. return port->shared->name;
  1075. }
  1076. const char *
  1077. jack_port_short_name (const jack_port_t *port)
  1078. {
  1079. /* we know there is always a colon, because we put
  1080. it there ...
  1081. */
  1082. return strchr (port->shared->name, ':') + 1;
  1083. }
  1084. int
  1085. jack_port_flags (const jack_port_t *port)
  1086. {
  1087. return port->shared->flags;
  1088. }
  1089. const char *
  1090. jack_port_type (const jack_port_t *port)
  1091. {
  1092. return port->shared->type_info.type_name;
  1093. }
  1094. int
  1095. jack_port_set_name (jack_port_t *port, const char *new_name)
  1096. {
  1097. char *colon;
  1098. int len;
  1099. colon = strchr (port->shared->name, ':');
  1100. len = sizeof (port->shared->name) - ((int) (colon - port->shared->name)) - 2;
  1101. snprintf (colon+1, len, "%s", new_name);
  1102. return 0;
  1103. }
  1104. void
  1105. jack_on_shutdown (jack_client_t *client, void (*function)(void *arg), void *arg)
  1106. {
  1107. client->on_shutdown = function;
  1108. client->on_shutdown_arg = arg;
  1109. }
  1110. const char **
  1111. jack_get_ports (jack_client_t *client,
  1112. const char *port_name_pattern,
  1113. const char *type_name_pattern,
  1114. unsigned long flags)
  1115. {
  1116. jack_control_t *engine;
  1117. const char **matching_ports;
  1118. unsigned long match_cnt;
  1119. jack_port_shared_t *psp;
  1120. unsigned long i;
  1121. regex_t port_regex;
  1122. regex_t type_regex;
  1123. int matching;
  1124. engine = client->engine;
  1125. if (port_name_pattern && port_name_pattern[0]) {
  1126. regcomp (&port_regex, port_name_pattern, REG_EXTENDED|REG_NOSUB);
  1127. }
  1128. if (type_name_pattern && type_name_pattern[0]) {
  1129. regcomp (&type_regex, type_name_pattern, REG_EXTENDED|REG_NOSUB);
  1130. }
  1131. psp = engine->ports;
  1132. match_cnt = 0;
  1133. matching_ports = (const char **) malloc (sizeof (char *) * engine->port_max);
  1134. for (i = 0; i < engine->port_max; i++) {
  1135. matching = 1;
  1136. if (!psp[i].in_use) {
  1137. continue;
  1138. }
  1139. if (flags) {
  1140. if ((psp[i].flags & flags) != flags) {
  1141. matching = 0;
  1142. }
  1143. }
  1144. if (matching && port_name_pattern && port_name_pattern[0]) {
  1145. if (regexec (&port_regex, psp[i].name, 0, NULL, 0)) {
  1146. matching = 0;
  1147. }
  1148. }
  1149. if (matching && type_name_pattern && type_name_pattern[0]) {
  1150. if (regexec (&type_regex, psp[i].type_info.type_name, 0, NULL, 0)) {
  1151. matching = 0;
  1152. }
  1153. }
  1154. if (matching) {
  1155. matching_ports[match_cnt++] = psp[i].name;
  1156. }
  1157. }
  1158. matching_ports[match_cnt] = 0;
  1159. if (match_cnt == 0) {
  1160. free (matching_ports);
  1161. matching_ports = 0;
  1162. }
  1163. return matching_ports;
  1164. }
  1165. nframes_t
  1166. jack_frames_since_cycle_start (jack_client_t *client)
  1167. {
  1168. struct timeval now;
  1169. float usecs;
  1170. gettimeofday (&now, NULL);
  1171. usecs = ((now.tv_sec * 1000000) + now.tv_usec) - client->engine->time.microseconds;
  1172. return (nframes_t) floor ((((float) client->engine->time.frame_rate) / 1000000.0f) * usecs);
  1173. }
  1174. int
  1175. jack_port_lock (jack_client_t *client, jack_port_t *port)
  1176. {
  1177. if (port) {
  1178. port->shared->locked = 1;
  1179. return 0;
  1180. }
  1181. return -1;
  1182. }
  1183. int
  1184. jack_port_unlock (jack_client_t *client, jack_port_t *port)
  1185. {
  1186. if (port) {
  1187. port->shared->locked = 0;
  1188. return 0;
  1189. }
  1190. return -1;
  1191. }
  1192. static void
  1193. jack_audio_port_mixdown (jack_port_t *port, nframes_t nframes)
  1194. {
  1195. GSList *node;
  1196. jack_port_t *input;
  1197. nframes_t n;
  1198. sample_t *buffer;
  1199. sample_t *dst, *src;
  1200. printf ("audio mixdown on %s\n", port->shared->name);
  1201. /* by the time we've called this, we've already established
  1202. the existence of more than 1 connection to this input port.
  1203. */
  1204. /* no need to take connection lock, since this is called
  1205. from the process() callback, and the jack server
  1206. ensures that no changes to connections happen
  1207. during this time.
  1208. */
  1209. node = port->connections;
  1210. input = (jack_port_t *) node->data;
  1211. buffer = jack_port_buffer (port);
  1212. memcpy (buffer, jack_port_buffer (input), sizeof (sample_t) * nframes);
  1213. for (node = g_slist_next (node); node; node = g_slist_next (node)) {
  1214. input = (jack_port_t *) node->data;
  1215. n = nframes;
  1216. dst = buffer;
  1217. src = jack_port_buffer (input);
  1218. while (n--) {
  1219. *dst++ += *src++;
  1220. }
  1221. }
  1222. }
  1223. const char **
  1224. jack_port_get_connections (const jack_port_t *port)
  1225. {
  1226. const char **ret;
  1227. GSList *node;
  1228. unsigned int n;
  1229. pthread_mutex_lock (&((jack_port_t *) port)->connection_lock);
  1230. if (port->connections == NULL) {
  1231. pthread_mutex_unlock (&((jack_port_t *) port)->connection_lock);
  1232. return NULL;
  1233. }
  1234. ret = (const char **) malloc (sizeof (char *) * (g_slist_length (port->connections) + 1));
  1235. for (n = 0, node = port->connections; node; node = g_slist_next (node), n++) {
  1236. ret[n] = ((jack_port_t *) node->data)->shared->name;
  1237. }
  1238. ret[n] = NULL;
  1239. pthread_mutex_unlock (&((jack_port_t *) port)->connection_lock);
  1240. return ret;
  1241. }
  1242. int
  1243. jack_port_connected (const jack_port_t *port)
  1244. {
  1245. return port->connections != NULL;
  1246. }
  1247. int
  1248. jack_port_connected_to (const jack_port_t *port, const char *portname)
  1249. {
  1250. GSList *node;
  1251. int ret = FALSE;
  1252. pthread_mutex_lock (&((jack_port_t *) port)->connection_lock);
  1253. for (node = port->connections; node; node = g_slist_next (node)) {
  1254. jack_port_t *other_port = (jack_port_t *) node->data;
  1255. if (strcmp (other_port->shared->name, portname) == 0) {
  1256. ret = TRUE;
  1257. break;
  1258. }
  1259. }
  1260. pthread_mutex_unlock (&((jack_port_t *) port)->connection_lock);
  1261. return ret;
  1262. }
  1263. int
  1264. jack_port_connected_to_port (const jack_port_t *port, const jack_port_t *other_port)
  1265. {
  1266. GSList *node;
  1267. int ret = FALSE;
  1268. pthread_mutex_lock (&((jack_port_t *) port)->connection_lock);
  1269. for (node = port->connections; node; node = g_slist_next (node)) {
  1270. jack_port_t *this_port = (jack_port_t *) node->data;
  1271. if (other_port->shared == this_port->shared) {
  1272. ret = TRUE;
  1273. break;
  1274. }
  1275. }
  1276. pthread_mutex_unlock (&((jack_port_t *) port)->connection_lock);
  1277. return ret;
  1278. }