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.

1358 lines
31KB

  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/poll.h>
  25. #include <stdarg.h>
  26. #include <stdio.h>
  27. #include <regex.h>
  28. #include <jack/jack.h>
  29. #include <jack/internal.h>
  30. #include <jack/engine.h>
  31. #include <jack/pool.h>
  32. #include <jack/error.h>
  33. char *jack_temp_dir = "/tmp";
  34. void
  35. jack_set_temp_dir (const char *path)
  36. {
  37. jack_temp_dir = strdup (path);
  38. }
  39. static jack_port_t *jack_port_new (jack_client_t *client, jack_port_id_t port_id, jack_control_t *control);
  40. static pthread_mutex_t client_lock;
  41. static pthread_cond_t client_ready;
  42. static void *jack_zero_filled_buffer = 0;
  43. struct _jack_client {
  44. jack_control_t *engine;
  45. jack_client_control_t *control;
  46. struct pollfd *pollfd;
  47. int pollmax;
  48. int graph_next_fd;
  49. int request_fd;
  50. GSList *port_segments;
  51. GSList *ports;
  52. pthread_t thread;
  53. char fifo_prefix[FIFO_NAME_SIZE+1];
  54. void (*on_shutdown)(void *arg);
  55. void *on_shutdown_arg;
  56. char thread_ok : 1;
  57. char first_active : 1;
  58. };
  59. #define event_fd pollfd[0].fd
  60. #define graph_wait_fd pollfd[1].fd
  61. typedef struct {
  62. int status;
  63. struct _jack_client *client;
  64. const char *client_name;
  65. } client_info;
  66. static void
  67. default_jack_error (const char *fmt, ...)
  68. {
  69. va_list ap;
  70. va_start (ap, fmt);
  71. vfprintf (stderr, fmt, ap);
  72. va_end (ap);
  73. fputc ('\n', stderr);
  74. }
  75. void (*jack_error)(const char *fmt, ...) = &default_jack_error;
  76. jack_client_t *
  77. jack_client_alloc ()
  78. {
  79. jack_client_t *client;
  80. client = (jack_client_t *) malloc (sizeof (jack_client_t));
  81. client->pollfd = (struct pollfd *) malloc (sizeof (struct pollfd) * 2);
  82. client->pollmax = 2;
  83. client->request_fd = -1;
  84. client->event_fd = -1;
  85. client->graph_wait_fd = -1;
  86. client->graph_next_fd = -1;
  87. client->port_segments = NULL;
  88. client->ports = NULL;
  89. client->engine = NULL;
  90. client->control = 0;
  91. client->thread_ok = FALSE;
  92. client->first_active = TRUE;
  93. client->on_shutdown = NULL;
  94. return client;
  95. }
  96. static jack_port_t *
  97. jack_port_by_id (jack_client_t *client, jack_port_id_t id)
  98. {
  99. GSList *node;
  100. for (node = client->ports; node; node = g_slist_next (node)) {
  101. if (((jack_port_t *) node->data)->shared->id == id) {
  102. return (jack_port_t *) node->data;
  103. }
  104. }
  105. return NULL;
  106. }
  107. static jack_port_id_t
  108. jack_port_id_by_name (jack_client_t *client, const char *port_name)
  109. {
  110. jack_port_id_t id, limit;
  111. jack_port_shared_t *port;
  112. limit = client->engine->port_max;
  113. port = &client->engine->ports[0];
  114. for (id = 0; id < limit; id++) {
  115. if (port[id].in_use && strcmp (port[id].name, port_name) == 0) {
  116. return port[id].id;
  117. }
  118. }
  119. return NoPort;
  120. }
  121. static void
  122. jack_client_invalidate_port_buffers (jack_client_t *client)
  123. {
  124. GSList *node;
  125. jack_port_t *port;
  126. /* This releases all local memory owned by input ports
  127. and sets the buffer pointer to NULL. This will cause
  128. jack_port_get_buffer() to reallocate space for the
  129. buffer on the next call (if there is one).
  130. */
  131. for (node = client->ports; node; node = g_slist_next (node)) {
  132. port = (jack_port_t *) node->data;
  133. if (port->shared->flags & JackPortIsInput) {
  134. /* XXX release buffer */
  135. port->client_segment_base = NULL;
  136. }
  137. }
  138. }
  139. int
  140. jack_client_handle_port_connection (jack_client_t *client, jack_event_t *event)
  141. {
  142. jack_port_t *control_port;
  143. jack_port_t *other;
  144. GSList *node;
  145. switch (event->type) {
  146. case PortConnected:
  147. other = jack_port_new (client, event->y.other_id, client->engine);
  148. control_port = jack_port_by_id (client, event->x.self_id);
  149. control_port->connections = g_slist_prepend (control_port->connections, other);
  150. break;
  151. case PortDisconnected:
  152. control_port = jack_port_by_id (client, event->x.self_id);
  153. for (node = control_port->connections; node; node = g_slist_next (node)) {
  154. other = (jack_port_t *) node->data;
  155. if (other->shared->id == event->y.other_id) {
  156. printf ("%s DIS-connecting and %s\n", control_port->shared->name, other->shared->name);
  157. control_port->connections = g_slist_remove_link (control_port->connections, node);
  158. g_slist_free_1 (node);
  159. free (other);
  160. break;
  161. }
  162. }
  163. break;
  164. default:
  165. /* impossible */
  166. break;
  167. }
  168. return 0;
  169. }
  170. static int
  171. jack_handle_reorder (jack_client_t *client, jack_event_t *event)
  172. {
  173. char path[FIFO_NAME_SIZE+1];
  174. if (client->graph_wait_fd >= 0) {
  175. close (client->graph_wait_fd);
  176. client->graph_wait_fd = -1;
  177. }
  178. if (client->graph_next_fd >= 0) {
  179. close (client->graph_next_fd);
  180. client->graph_next_fd = -1;
  181. }
  182. sprintf (path, "%s-%lu", client->fifo_prefix, event->x.n);
  183. if ((client->graph_wait_fd = open (path, O_RDONLY)) <= 0) {
  184. jack_error ("cannot open specified fifo [%s] for reading (%s)", path, strerror (errno));
  185. return -1;
  186. }
  187. sprintf (path, "%s-%lu", client->fifo_prefix, event->x.n+1);
  188. if ((client->graph_next_fd = open (path, O_WRONLY)) < 0) {
  189. jack_error ("cannot open specified fifo [%s] for writing (%s)", path, strerror (errno));
  190. return -1;
  191. }
  192. return 0;
  193. }
  194. static int
  195. server_connect (int which)
  196. {
  197. int fd;
  198. struct sockaddr_un addr;
  199. if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) {
  200. jack_error ("cannot create client socket (%s)", strerror (errno));
  201. return -1;
  202. }
  203. addr.sun_family = AF_UNIX;
  204. g_snprintf (addr.sun_path, sizeof (addr.sun_path) - 1, "%s/jack_%d", jack_temp_dir, which);
  205. if (connect (fd, (struct sockaddr *) &addr, sizeof (addr)) < 0) {
  206. jack_error ("cannot connect to jack server", strerror (errno));
  207. close (fd);
  208. return -1;
  209. }
  210. return fd;
  211. }
  212. static int
  213. server_event_connect (jack_client_t *client)
  214. {
  215. int fd;
  216. struct sockaddr_un addr;
  217. jack_client_connect_ack_request_t req;
  218. jack_client_connect_ack_result_t res;
  219. if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) {
  220. jack_error ("cannot create client event socket (%s)", strerror (errno));
  221. return -1;
  222. }
  223. addr.sun_family = AF_UNIX;
  224. g_snprintf (addr.sun_path, sizeof (addr.sun_path) - 1, "%s/jack_ack_0", jack_temp_dir);
  225. if (connect (fd, (struct sockaddr *) &addr, sizeof (addr)) < 0) {
  226. jack_error ("cannot connect to jack server for events", strerror (errno));
  227. close (fd);
  228. return -1;
  229. }
  230. req.client_id = client->control->id;
  231. if (write (fd, &req, sizeof (req)) != sizeof (req)) {
  232. jack_error ("cannot write event connect request to server (%s)", strerror (errno));
  233. close (fd);
  234. return -1;
  235. }
  236. if (read (fd, &res, sizeof (res)) != sizeof (res)) {
  237. jack_error ("cannot read event connect result from server (%s)", strerror (errno));
  238. close (fd);
  239. return -1;
  240. }
  241. if (res.status != 0) {
  242. close (fd);
  243. return -1;
  244. }
  245. return fd;
  246. }
  247. jack_client_t *
  248. jack_client_new (const char *client_name)
  249. {
  250. int req_fd = -1;
  251. int ev_fd = -1;
  252. void *addr;
  253. jack_client_connect_request_t req;
  254. jack_client_connect_result_t res;
  255. jack_port_segment_info_t *si;
  256. jack_client_t *client;
  257. int client_shm_id;
  258. int control_shm_id;
  259. int port_segment_shm_id;
  260. int n;
  261. if (strlen (client_name) > sizeof (req.name) - 1) {
  262. jack_error ("\"%s\" is too long to be used as a JACK client name.\n"
  263. "Please use %lu characters or less.",
  264. sizeof (req.name) - 1);
  265. return NULL;
  266. }
  267. if ((req_fd = server_connect (0)) < 0) {
  268. jack_error ("cannot connect to default JACK server");
  269. return NULL;
  270. }
  271. req.type = ClientOutOfProcess;
  272. strncpy (req.name, client_name, sizeof (req.name) - 1);
  273. if (write (req_fd, &req, sizeof (req)) != sizeof (req)) {
  274. jack_error ("cannot send request to jack server (%s)", strerror (errno));
  275. close (req_fd);
  276. return NULL;
  277. }
  278. if ((n = read (req_fd, &res, sizeof (res))) != sizeof (res)) {
  279. jack_error ("cannot read response from jack server (%s)", strerror (errno));
  280. close (req_fd);
  281. return NULL;
  282. }
  283. if (res.status) {
  284. close (req_fd);
  285. jack_error ("could not attach as client");
  286. return NULL;
  287. }
  288. client = jack_client_alloc ();
  289. strcpy (client->fifo_prefix, res.fifo_prefix);
  290. client->request_fd = req_fd;
  291. client->pollfd[0].events = POLLIN|POLLERR|POLLHUP|POLLNVAL;
  292. client->pollfd[1].events = POLLIN|POLLERR|POLLHUP|POLLNVAL;
  293. /* Lookup, attach and register the port/buffer segments in use
  294. right now.
  295. */
  296. if ((port_segment_shm_id = shmget (res.port_segment_key, 0, 0)) < 0) {
  297. jack_error ("cannot determine shared memory segment for port segment key 0x%x (%s)", res.port_segment_key, strerror (errno));
  298. goto fail;
  299. }
  300. if ((addr = shmat (port_segment_shm_id, 0, 0)) == (void *) -1) {
  301. jack_error ("cannot attached port segment shared memory (%s)", strerror (errno));
  302. goto fail;
  303. }
  304. si = (jack_port_segment_info_t *) malloc (sizeof (jack_port_segment_info_t));
  305. si->shm_key = res.port_segment_key;
  306. si->address = addr;
  307. /* the first chunk of the first port segment is always set by the engine
  308. to be a conveniently-sized, zero-filled lump of memory.
  309. */
  310. if (client->port_segments == NULL) {
  311. jack_zero_filled_buffer = si->address;
  312. }
  313. client->port_segments = g_slist_prepend (client->port_segments, si);
  314. /* attach the engine control/info block */
  315. if ((control_shm_id = shmget (res.control_key, 0, 0)) < 0) {
  316. jack_error ("cannot determine shared memory segment for control key 0x%x", res.control_key);
  317. goto fail;
  318. }
  319. if ((addr = shmat (control_shm_id, 0, 0)) == (void *) -1) {
  320. jack_error ("cannot attached engine control shared memory segment");
  321. goto fail;
  322. }
  323. client->engine = (jack_control_t *) addr;
  324. /* now attach the client control block */
  325. if ((client_shm_id = shmget (res.client_key, 0, 0)) < 0) {
  326. jack_error ("cannot determine shared memory segment for client key 0x%x", res.client_key);
  327. goto fail;
  328. }
  329. if ((addr = shmat (client_shm_id, 0, 0)) == (void *) -1) {
  330. jack_error ("cannot attached client control shared memory segment");
  331. goto fail;
  332. }
  333. client->control = (jack_client_control_t *) addr;
  334. if ((ev_fd = server_event_connect (client)) < 0) {
  335. jack_error ("cannot connect to server for event stream (%s)", strerror (errno));
  336. goto fail;
  337. }
  338. client->event_fd = ev_fd;
  339. return client;
  340. fail:
  341. if (client->engine) {
  342. shmdt (client->engine);
  343. }
  344. if (client->control) {
  345. shmdt ((char *) client->control);
  346. }
  347. if (req_fd >= 0) {
  348. close (req_fd);
  349. }
  350. if (ev_fd >= 0) {
  351. close (ev_fd);
  352. }
  353. return 0;
  354. }
  355. static void *
  356. jack_client_thread (void *arg)
  357. {
  358. jack_client_t *client = (jack_client_t *) arg;
  359. jack_client_control_t *control = client->control;
  360. jack_event_t event;
  361. char status = 0;
  362. char c;
  363. int err = 0;
  364. pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  365. pthread_mutex_lock (&client_lock);
  366. client->thread_ok = TRUE;
  367. pthread_cond_signal (&client_ready);
  368. pthread_mutex_unlock (&client_lock);
  369. while (err == 0) {
  370. if (poll (client->pollfd, client->pollmax, 1000) < 0) {
  371. if (errno == EINTR) {
  372. printf ("poll interrupted\n");
  373. continue;
  374. }
  375. jack_error ("poll failed in client (%s)", strerror (errno));
  376. status = -1;
  377. break;
  378. }
  379. if (client->pollfd[0].revents & ~POLLIN) {
  380. jack_error ("engine has shut down socket; thread exiting");
  381. if (client->on_shutdown) {
  382. client->on_shutdown (client->on_shutdown_arg);
  383. }
  384. pthread_exit (0);
  385. }
  386. if (client->pollfd[0].revents & POLLIN) {
  387. /* server has sent us an event. process the event and reply */
  388. if (read (client->event_fd, &event, sizeof (event)) != sizeof (event)) {
  389. jack_error ("cannot read server event (%s)", strerror (errno));
  390. err++;
  391. break;
  392. }
  393. status = 0;
  394. switch (event.type) {
  395. case PortRegistered:
  396. if (control->port_register) {
  397. control->port_register (event.x.port_id, TRUE, control->port_register_arg);
  398. }
  399. break;
  400. case PortUnregistered:
  401. if (control->port_register) {
  402. control->port_register (event.x.port_id, FALSE, control->port_register_arg);
  403. }
  404. break;
  405. case GraphReordered:
  406. status = jack_handle_reorder (client, &event);
  407. break;
  408. case PortConnected:
  409. case PortDisconnected:
  410. status = jack_client_handle_port_connection (client, &event);
  411. break;
  412. case BufferSizeChange:
  413. jack_client_invalidate_port_buffers (client);
  414. if (control->bufsize) {
  415. status = control->bufsize (control->nframes, control->bufsize_arg);
  416. }
  417. break;
  418. case SampleRateChange:
  419. if (control->srate) {
  420. status = control->srate (control->nframes, control->srate_arg);
  421. }
  422. break;
  423. case NewPortBufferSegment:
  424. break;
  425. case PortMonitor:
  426. if (control->port_monitor) {
  427. control->port_monitor (event.x.port_id, TRUE, control->port_monitor_arg);
  428. }
  429. break;
  430. case PortUnMonitor:
  431. if (control->port_monitor) {
  432. control->port_monitor (event.x.port_id, FALSE, control->port_monitor_arg);
  433. }
  434. break;
  435. }
  436. if (write (client->event_fd, &status, sizeof (status)) != sizeof (status)) {
  437. jack_error ("cannot send event response to engine (%s)", strerror (errno));
  438. err++;
  439. break;
  440. }
  441. }
  442. if (client->pollfd[1].revents & POLLIN) {
  443. /* the previous stage of the graph has told us to
  444. process().
  445. */
  446. control->state = JACK_CLIENT_STATE_TRIGGERED;
  447. if (read (client->graph_wait_fd, &c, sizeof (c)) != sizeof (c)) {
  448. jack_error ("cannot clean up byte from inter-client pipe (%s)", strerror (errno));
  449. err++;
  450. break;
  451. }
  452. status = control->process (control->nframes, control->process_arg);
  453. if (!status) {
  454. control->state = JACK_CLIENT_STATE_FINISHED;
  455. }
  456. /* this may fail. if it does, the engine will discover
  457. it due a cycle timeout, which is about
  458. the best we can do without a lot of mostly wasted
  459. effort.
  460. */
  461. write (client->graph_next_fd, &c, 1);
  462. }
  463. }
  464. return (void *) err;
  465. }
  466. static int
  467. jack_start_thread (jack_client_t *client)
  468. {
  469. pthread_attr_t *attributes = 0;
  470. if (client->engine->real_time) {
  471. /* Get the client thread to run as an RT-FIFO
  472. scheduled thread of appropriate priority.
  473. */
  474. struct sched_param rt_param;
  475. attributes = (pthread_attr_t *) malloc (sizeof (pthread_attr_t));
  476. pthread_attr_init (attributes);
  477. if (pthread_attr_setschedpolicy (attributes, SCHED_FIFO)) {
  478. jack_error ("cannot set FIFO scheduling class for RT thread");
  479. return -1;
  480. }
  481. if (pthread_attr_setscope (attributes, PTHREAD_SCOPE_SYSTEM)) {
  482. jack_error ("Cannot set scheduling scope for RT thread");
  483. return -1;
  484. }
  485. memset (&rt_param, 0, sizeof (rt_param));
  486. rt_param.sched_priority = client->engine->client_priority;
  487. if (pthread_attr_setschedparam (attributes, &rt_param)) {
  488. jack_error ("Cannot set scheduling priority for RT thread (%s)", strerror (errno));
  489. return -1;
  490. }
  491. }
  492. if (pthread_create (&client->thread, attributes, jack_client_thread, client)) {
  493. return -1;
  494. }
  495. return 0;
  496. }
  497. int
  498. jack_activate (jack_client_t *client)
  499. {
  500. jack_request_t req;
  501. if (client->control->type == ClientOutOfProcess && client->first_active) {
  502. pthread_mutex_init (&client_lock, NULL);
  503. pthread_cond_init (&client_ready, NULL);
  504. pthread_mutex_lock (&client_lock);
  505. if (jack_start_thread (client)) {
  506. pthread_mutex_unlock (&client_lock);
  507. return -1;
  508. }
  509. pthread_cond_wait (&client_ready, &client_lock);
  510. pthread_mutex_unlock (&client_lock);
  511. if (!client->thread_ok) {
  512. jack_error ("could not start client thread");
  513. return -1;
  514. }
  515. client->first_active = FALSE;
  516. }
  517. req.type = ActivateClient;
  518. req.x.client_id = client->control->id;
  519. if (write (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  520. jack_error ("cannot send activate client request to server");
  521. return -1;
  522. }
  523. if (read (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  524. jack_error ("cannot read activate client result from server (%s)", strerror (errno));
  525. return -1;
  526. }
  527. return req.status;
  528. }
  529. int
  530. jack_deactivate (jack_client_t *client)
  531. {
  532. jack_request_t req;
  533. req.type = DeactivateClient;
  534. req.x.client_id = client->control->id;
  535. if (write (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  536. jack_error ("cannot send activate client request to server");
  537. return -1;
  538. }
  539. if (read (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  540. jack_error ("cannot read activate client result from server (%s)", strerror (errno));
  541. return -1;
  542. }
  543. return req.status;
  544. }
  545. int
  546. jack_client_close (jack_client_t *client)
  547. {
  548. GSList *node;
  549. jack_request_t req;
  550. void *status;
  551. req.type = DropClient;
  552. req.x.client_id = client->control->id;
  553. /* stop the thread */
  554. pthread_cancel (client->thread);
  555. pthread_join (client->thread, &status);
  556. shmdt ((char *) client->control);
  557. shmdt (client->engine);
  558. for (node = client->port_segments; node; node = g_slist_next (node)) {
  559. jack_port_segment_info_t *si;
  560. si = (jack_port_segment_info_t *) node->data;
  561. shmdt (si->address);
  562. free (si);
  563. }
  564. g_slist_free (client->port_segments);
  565. g_slist_free (client->ports);
  566. if (client->graph_wait_fd) {
  567. close (client->graph_wait_fd);
  568. }
  569. if (client->graph_next_fd) {
  570. close (client->graph_next_fd);
  571. }
  572. if (write (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  573. jack_error ("cannot send drop client request to server");
  574. req.status = -1;
  575. }
  576. close (client->event_fd);
  577. close (client->request_fd);
  578. free (client->pollfd);
  579. free (client);
  580. return req.status;
  581. }
  582. int
  583. jack_load_client (const char *client_name, const char *path_to_so)
  584. {
  585. int fd;
  586. jack_client_connect_request_t req;
  587. jack_client_connect_result_t res;
  588. if ((fd = server_connect (0)) < 0) {
  589. jack_error ("cannot connect to jack server");
  590. return 0;
  591. }
  592. req.type = ClientDynamic;
  593. strncpy (req.name, client_name, sizeof (req.name) - 1);
  594. req.name[sizeof(req.name)-1] = '\0';
  595. strncpy (req.object_path, path_to_so, sizeof (req.name) - 1);
  596. req.object_path[sizeof(req.object_path)-1] = '\0';
  597. if (write (fd, &req, sizeof (req)) != sizeof (req)) {
  598. jack_error ("cannot send request to jack server (%s)", strerror (errno));
  599. close (fd);
  600. return 0;
  601. }
  602. if (read (fd, &res, sizeof (res)) != sizeof (res)) {
  603. jack_error ("cannot read response from jack server (%s)", strerror (errno));
  604. close (fd);
  605. return 0;
  606. }
  607. close (fd);
  608. return res.status;
  609. }
  610. jack_client_t *
  611. jack_driver_become_client (const char *client_name)
  612. {
  613. int fd;
  614. jack_client_connect_request_t req;
  615. jack_client_connect_result_t res;
  616. jack_client_t *client = 0;
  617. int port_segment_shm_id;
  618. jack_port_segment_info_t *si;
  619. void *addr;
  620. if ((fd = server_connect (0)) < 0) {
  621. jack_error ("cannot connect to jack server");
  622. return 0;
  623. }
  624. req.type = ClientDriver;
  625. strncpy (req.name, client_name, sizeof (req.name) - 1);
  626. req.name[sizeof(req.name)-1] = '\0';
  627. if (write (fd, &req, sizeof (req)) != sizeof (req)) {
  628. jack_error ("cannot send request to jack server (%s)", strerror (errno));
  629. close (fd);
  630. return 0;
  631. }
  632. if (read (fd, &res, sizeof (res)) != sizeof (res)) {
  633. jack_error ("cannot read response from jack server (%s)", strerror (errno));
  634. close (fd);
  635. return 0;
  636. }
  637. if (res.status) {
  638. return 0;
  639. }
  640. client = jack_client_alloc ();
  641. client->request_fd = fd;
  642. client->control = res.client_control;
  643. client->engine = res.engine_control;
  644. /* Lookup, attach and register the port/buffer segments in use
  645. right now.
  646. */
  647. if ((port_segment_shm_id = shmget (res.port_segment_key, 0, 0)) < 0) {
  648. jack_error ("cannot determine shared memory segment for port segment key 0x%x (%s)", res.port_segment_key, strerror (errno));
  649. return NULL;
  650. }
  651. if ((addr = shmat (port_segment_shm_id, 0, 0)) == (void *) -1) {
  652. jack_error ("cannot attached port segment shared memory (%s)", strerror (errno));
  653. return NULL;
  654. }
  655. si = (jack_port_segment_info_t *) malloc (sizeof (jack_port_segment_info_t));
  656. si->shm_key = res.port_segment_key;
  657. si->address = addr;
  658. /* the first chunk of the first port segment is always set by the engine
  659. to be a conveniently-sized, zero-filled lump of memory.
  660. */
  661. if (client->port_segments == NULL) {
  662. jack_zero_filled_buffer = si->address;
  663. }
  664. client->port_segments = g_slist_prepend (client->port_segments, si);
  665. /* allow the engine to act on the client's behalf
  666. when dealing with in-process clients.
  667. */
  668. client->control->private_internal_client = client;
  669. return client;
  670. }
  671. unsigned long jack_get_buffer_size (jack_client_t *client)
  672. {
  673. return client->engine->buffer_size;
  674. }
  675. unsigned long jack_get_sample_rate (jack_client_t *client)
  676. {
  677. return client->engine->sample_rate;
  678. }
  679. static jack_port_t *
  680. jack_port_new (jack_client_t *client, jack_port_id_t port_id, jack_control_t *control)
  681. {
  682. jack_port_t *port;
  683. jack_port_shared_t *shared;
  684. jack_port_segment_info_t *si;
  685. GSList *node;
  686. shared = &control->ports[port_id];
  687. port = (jack_port_t *) malloc (sizeof (jack_port_t));
  688. port->client_segment_base = NULL;
  689. port->shared = shared;
  690. port->connections = 0;
  691. port->tied = NULL;
  692. si = NULL;
  693. for (node = client->port_segments; node; node = g_slist_next (node)) {
  694. si = (jack_port_segment_info_t *) node->data;
  695. if (si->shm_key == port->shared->shm_key) {
  696. break;
  697. }
  698. }
  699. if (si == NULL) {
  700. jack_error ("cannot find port segment to match newly registered port\n");
  701. return NULL;
  702. }
  703. port->client_segment_base = si->address;
  704. return port;
  705. }
  706. jack_port_t *
  707. jack_port_register (jack_client_t *client,
  708. const char *port_name,
  709. const char *port_type,
  710. unsigned long flags,
  711. unsigned long buffer_size)
  712. {
  713. jack_request_t req;
  714. jack_port_t *port = 0;
  715. /* before we get started, check a few basics */
  716. if (flags & JackPortCanMonitor) {
  717. if (client->control->port_monitor == NULL) {
  718. jack_error ("you cannot register ports with PortCanMonitor "
  719. "without a port monitor callback");
  720. return NULL;
  721. }
  722. }
  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. client->ports = g_slist_prepend (client->ports, port);
  744. return port;
  745. }
  746. int
  747. jack_port_unregister (jack_client_t *client, jack_port_t *port)
  748. {
  749. jack_request_t req;
  750. req.type = UnRegisterPort;
  751. req.x.port_info.port_id = port->shared->id;
  752. req.x.port_info.client_id = client->control->id;
  753. if (write (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  754. jack_error ("cannot send port registration request to server");
  755. return -1;
  756. }
  757. if (read (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  758. jack_error ("cannot read port registration result from server");
  759. return -1;
  760. }
  761. return req.status;
  762. }
  763. int
  764. jack_port_connect (jack_client_t *client, const char *source_port, const char *destination_port)
  765. {
  766. jack_request_t req;
  767. req.type = ConnectPorts;
  768. strncpy (req.x.connect.source_port, source_port, sizeof (req.x.connect.source_port) - 1);
  769. req.x.connect.source_port[sizeof(req.x.connect.source_port) - 1] = '\0';
  770. strncpy (req.x.connect.destination_port, destination_port, sizeof (req.x.connect.destination_port) - 1);
  771. req.x.connect.destination_port[sizeof(req.x.connect.destination_port) - 1] = '\0';
  772. if (write (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  773. jack_error ("cannot send port connection request to server");
  774. return -1;
  775. }
  776. if (read (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  777. jack_error ("cannot read port connection result from server");
  778. return -1;
  779. }
  780. return req.status;
  781. }
  782. int
  783. jack_port_disconnect (jack_client_t *client, const char *source_port, const char *destination_port)
  784. {
  785. jack_request_t req;
  786. req.type = DisconnectPorts;
  787. strncpy (req.x.connect.source_port, source_port, sizeof (req.x.connect.source_port) - 1);
  788. req.x.connect.source_port[sizeof(req.x.connect.source_port) - 1] = '\0';
  789. strncpy (req.x.connect.destination_port, destination_port, sizeof (req.x.connect.destination_port) - 1);
  790. req.x.connect.destination_port[sizeof(req.x.connect.destination_port) - 1] = '\0';
  791. if (write (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  792. jack_error ("cannot send port connection request to server");
  793. return -1;
  794. }
  795. if (read (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  796. jack_error ("cannot read port connection result from server");
  797. return -1;
  798. }
  799. return req.status;
  800. }
  801. int
  802. jack_engine_takeover_timebase (jack_client_t *client)
  803. {
  804. jack_request_t req;
  805. req.type = SetTimeBaseClient;
  806. req.x.client_id = client->control->id;
  807. if (write (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  808. jack_error ("cannot send set time base request to server");
  809. return -1;
  810. }
  811. if (read (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  812. jack_error ("cannot read set time base result from server");
  813. return -1;
  814. }
  815. return req.status;
  816. }
  817. void
  818. jack_update_time (jack_client_t *client, nframes_t time)
  819. {
  820. client->control->frame_time = time;
  821. }
  822. void
  823. jack_set_error_function (void (*func) (const char *, ...))
  824. {
  825. jack_error = func;
  826. }
  827. void *
  828. jack_port_get_buffer (jack_port_t *port, nframes_t nframes)
  829. {
  830. GSList *node, *next;
  831. /* Output port. The buffer was assigned by the engine
  832. when the port was registered.
  833. */
  834. if (port->shared->flags & JackPortIsOutput) {
  835. if (port->tied) {
  836. return jack_port_get_buffer (port->tied, nframes);
  837. }
  838. return jack_port_buffer (port);
  839. }
  840. /* Input port.
  841. */
  842. if ((node = port->connections) == NULL) {
  843. /* no connections; return a zero-filled buffer */
  844. return jack_zero_filled_buffer;
  845. }
  846. if ((next = g_slist_next (node)) == NULL) {
  847. /* one connection: use zero-copy mode - just pass
  848. the buffer of the connected (output) port.
  849. */
  850. return jack_port_buffer (((jack_port_t *) node->data));
  851. }
  852. /* multiple connections. use a local buffer and mixdown
  853. the incoming data to that buffer. we have already
  854. established the existence of a mixdown function
  855. during the connection process.
  856. */
  857. if (port->client_segment_base == NULL) {
  858. port->client_segment_base = 0;
  859. port->shared->offset = (size_t) jack_pool_alloc (port->shared->type_info.buffer_scale_factor * sizeof (sample_t) * nframes);
  860. }
  861. port->shared->type_info.mixdown (port, nframes);
  862. return jack_port_buffer (port);
  863. }
  864. int
  865. jack_port_tie (jack_port_t *dst, jack_port_t *src)
  866. {
  867. if (dst->shared->client_id != src->shared->client_id) {
  868. jack_error ("cannot tie ports not owned by the same client");
  869. return -1;
  870. }
  871. if (dst->shared->flags & JackPortIsOutput) {
  872. jack_error ("cannot tie an input port");
  873. return -1;
  874. }
  875. dst->tied = src;
  876. return 0;
  877. }
  878. int
  879. jack_port_untie (jack_port_t *port)
  880. {
  881. if (port->tied == NULL) {
  882. jack_error ("port \"%s\" is not tied", port->shared->name);
  883. return -1;
  884. }
  885. port->tied = NULL;
  886. return 0;
  887. }
  888. int
  889. jack_set_process_callback (jack_client_t *client, JackProcessCallback callback, void *arg)
  890. {
  891. if (client->control->active) {
  892. return -1;
  893. }
  894. client->control->process_arg = arg;
  895. client->control->process = callback;
  896. return 0;
  897. }
  898. int
  899. jack_set_buffer_size_callback (jack_client_t *client, JackBufferSizeCallback callback, void *arg)
  900. {
  901. if (client->control->active) {
  902. return -1;
  903. }
  904. client->control->bufsize_arg = arg;
  905. client->control->bufsize = callback;
  906. /* Now invoke it */
  907. callback (client->engine->buffer_size, arg);
  908. return 0;
  909. }
  910. int
  911. jack_set_sample_rate_callback (jack_client_t *client, JackSampleRateCallback callback, void *arg)
  912. {
  913. if (client->control->active) {
  914. return -1;
  915. }
  916. client->control->srate_arg = arg;
  917. client->control->srate = callback;
  918. /* Now invoke it */
  919. callback (client->engine->sample_rate, arg);
  920. return 0;
  921. }
  922. int
  923. jack_set_port_registration_callback(jack_client_t *client, JackPortRegistrationCallback callback, void *arg)
  924. {
  925. if (client->control->active) {
  926. return -1;
  927. }
  928. client->control->port_register_arg = arg;
  929. client->control->port_register = callback;
  930. return 0;
  931. }
  932. int
  933. jack_set_port_monitor_callback (jack_client_t *client, JackPortMonitorCallback callback, void *arg)
  934. {
  935. if (client->control->active) {
  936. return -1;
  937. }
  938. client->control->port_monitor_arg = arg;
  939. client->control->port_monitor = callback;
  940. return 0;
  941. }
  942. int
  943. jack_get_process_start_fd (jack_client_t *client)
  944. {
  945. /* once this has been called, the client thread
  946. does not sleep on the graph wait fd.
  947. */
  948. client->pollmax = 1;
  949. return client->graph_wait_fd;
  950. }
  951. int
  952. jack_get_process_done_fd (jack_client_t *client)
  953. {
  954. return client->graph_next_fd;
  955. }
  956. int
  957. jack_port_request_monitor (jack_client_t *client, const char *port_name, int onoff)
  958. {
  959. jack_request_t req;
  960. int n;
  961. req.type = (onoff ? RequestPortMonitor : RequestPortUnMonitor);
  962. req.x.port_info.port_id = jack_port_id_by_name (client, port_name);
  963. if (write (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  964. jack_error ("cannot send request to jack server (%s)", strerror (errno));
  965. return -1;
  966. }
  967. if ((n = read (client->request_fd, &req, sizeof (req))) != sizeof (req)) {
  968. jack_error ("cannot read response from jack server (%s)", strerror (errno));
  969. return -1;
  970. }
  971. return req.status;
  972. }
  973. const char *
  974. jack_port_name (const jack_port_t *port)
  975. {
  976. return port->shared->name;
  977. }
  978. void
  979. jack_on_shutdown (jack_client_t *client, void (*function)(void *arg), void *arg)
  980. {
  981. client->on_shutdown = function;
  982. client->on_shutdown_arg = arg;
  983. }
  984. char * const *
  985. jack_get_ports (jack_client_t *client,
  986. const char *port_name_pattern,
  987. const char *type_name_pattern,
  988. unsigned long flags)
  989. {
  990. jack_control_t *engine;
  991. char **matching_ports;
  992. unsigned long match_cnt;
  993. jack_port_shared_t *psp;
  994. unsigned long i;
  995. regex_t port_regex;
  996. regex_t type_regex;
  997. int matching;
  998. engine = client->engine;
  999. if (port_name_pattern) {
  1000. regcomp (&port_regex, port_name_pattern, REG_EXTENDED|REG_NOSUB);
  1001. }
  1002. if (type_name_pattern) {
  1003. regcomp (&type_regex, type_name_pattern, REG_EXTENDED|REG_NOSUB);
  1004. }
  1005. psp = engine->ports;
  1006. match_cnt = 0;
  1007. matching_ports = (char **) malloc (sizeof (char *) * engine->port_max);
  1008. for (i = 0; i < engine->port_max; i++) {
  1009. matching = 1;
  1010. if (!psp[i].in_use) {
  1011. continue;
  1012. }
  1013. if (flags) {
  1014. if ((psp[i].flags & flags) != flags) {
  1015. matching = 0;
  1016. }
  1017. }
  1018. if (matching && port_name_pattern) {
  1019. if (regexec (&port_regex, psp[i].name, 0, NULL, 0)) {
  1020. matching = 0;
  1021. }
  1022. }
  1023. if (matching && type_name_pattern) {
  1024. if (regexec (&type_regex, psp[i].type_info.type_name, 0, NULL, 0)) {
  1025. matching = 0;
  1026. }
  1027. }
  1028. if (matching) {
  1029. matching_ports[match_cnt++] = psp[i].name;
  1030. }
  1031. }
  1032. matching_ports[match_cnt] = 0;
  1033. return matching_ports;
  1034. }