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.

1535 lines
34KB

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