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.

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