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.

1240 lines
28KB

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