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.

1987 lines
46KB

  1. /*
  2. Copyright (C) 2001 Paul Davis
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as published by
  5. the Free Software Foundation; either version 2.1 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  14. $Id$
  15. */
  16. #include <sys/socket.h>
  17. #include <sys/un.h>
  18. #include <pthread.h>
  19. #include <errno.h>
  20. #include <fcntl.h>
  21. #include <sys/types.h>
  22. #include <sys/ipc.h>
  23. #include <sys/shm.h>
  24. #include <sys/mman.h>
  25. #include <sys/poll.h>
  26. #include <stdarg.h>
  27. #include <stdio.h>
  28. #include <regex.h>
  29. #include <math.h>
  30. #include <jack/jack.h>
  31. #include <jack/internal.h>
  32. #include <jack/engine.h>
  33. #include <jack/pool.h>
  34. #include <jack/error.h>
  35. #include <jack/cycles.h>
  36. char *jack_temp_dir = "/tmp";
  37. void
  38. jack_set_temp_dir (const char *path)
  39. {
  40. jack_temp_dir = strdup (path);
  41. }
  42. static jack_port_t *jack_port_new (jack_client_t *client, jack_port_id_t port_id, jack_control_t *control);
  43. static pthread_mutex_t client_lock;
  44. static pthread_cond_t client_ready;
  45. void *jack_zero_filled_buffer = 0;
  46. static void jack_audio_port_mixdown (jack_port_t *port, jack_nframes_t nframes);
  47. jack_port_type_info_t builtin_port_types[] = {
  48. { JACK_DEFAULT_AUDIO_TYPE, jack_audio_port_mixdown, 1 },
  49. { "", NULL }
  50. };
  51. struct _jack_client {
  52. jack_control_t *engine;
  53. jack_client_control_t *control;
  54. struct pollfd *pollfd;
  55. int pollmax;
  56. int graph_next_fd;
  57. int request_fd;
  58. GSList *port_segments;
  59. GSList *ports;
  60. pthread_t thread;
  61. char fifo_prefix[PATH_MAX+1];
  62. void (*on_shutdown)(void *arg);
  63. void *on_shutdown_arg;
  64. char thread_ok : 1;
  65. char first_active : 1;
  66. float cpu_mhz;
  67. };
  68. #define event_fd pollfd[0].fd
  69. #define graph_wait_fd pollfd[1].fd
  70. typedef struct {
  71. int status;
  72. struct _jack_client *client;
  73. const char *client_name;
  74. } client_info;
  75. static void
  76. default_jack_error (const char *fmt, ...)
  77. {
  78. va_list ap;
  79. va_start (ap, fmt);
  80. vfprintf (stderr, fmt, ap);
  81. va_end (ap);
  82. fputc ('\n', stderr);
  83. }
  84. void (*jack_error)(const char *fmt, ...) = &default_jack_error;
  85. jack_client_t *
  86. jack_client_alloc ()
  87. {
  88. jack_client_t *client;
  89. client = (jack_client_t *) malloc (sizeof (jack_client_t));
  90. client->pollfd = (struct pollfd *) malloc (sizeof (struct pollfd) * 2);
  91. client->pollmax = 2;
  92. client->request_fd = -1;
  93. client->event_fd = -1;
  94. client->graph_wait_fd = -1;
  95. client->graph_next_fd = -1;
  96. client->port_segments = NULL;
  97. client->ports = NULL;
  98. client->engine = NULL;
  99. client->control = 0;
  100. client->thread_ok = FALSE;
  101. client->first_active = TRUE;
  102. client->on_shutdown = NULL;
  103. client->cpu_mhz = (float) jack_get_mhz ();
  104. return client;
  105. }
  106. jack_port_t *
  107. jack_port_by_id (jack_client_t *client, jack_port_id_t id)
  108. {
  109. /*
  110. if (id >= client->engine->port_max)
  111. return NULL;
  112. if (client->engine->ports[id].in_use)
  113. return jack_port_new (client, id, client->engine);
  114. */
  115. GSList *node;
  116. for (node = client->ports; node; node = g_slist_next (node)) {
  117. if (((jack_port_t *) node->data)->shared->id == id) {
  118. return (jack_port_t *) node->data;
  119. }
  120. }
  121. return NULL;
  122. }
  123. jack_port_t *
  124. jack_port_by_name (jack_client_t *client, const char *port_name)
  125. {
  126. unsigned long i, limit;
  127. jack_port_shared_t *port;
  128. limit = client->engine->port_max;
  129. port = &client->engine->ports[0];
  130. for (i = 0; i < limit; i++) {
  131. if (port[i].in_use && strcmp (port[i].name, port_name) == 0) {
  132. return jack_port_new (client, port[i].id, client->engine);
  133. }
  134. }
  135. return NULL;
  136. }
  137. static void
  138. jack_client_invalidate_port_buffers (jack_client_t *client)
  139. {
  140. GSList *node;
  141. jack_port_t *port;
  142. /* This releases all local memory owned by input ports
  143. and sets the buffer pointer to NULL. This will cause
  144. jack_port_get_buffer() to reallocate space for the
  145. buffer on the next call (if there is one).
  146. */
  147. for (node = client->ports; node; node = g_slist_next (node)) {
  148. port = (jack_port_t *) node->data;
  149. if (port->shared->flags & JackPortIsInput) {
  150. if (port->client_segment_base == 0) {
  151. jack_pool_release ((void *) port->shared->offset);
  152. port->client_segment_base = 0;
  153. port->shared->offset = 0;
  154. }
  155. }
  156. }
  157. }
  158. int
  159. jack_client_handle_port_connection (jack_client_t *client, jack_event_t *event)
  160. {
  161. jack_port_t *control_port;
  162. jack_port_t *other;
  163. GSList *node;
  164. switch (event->type) {
  165. case PortConnected:
  166. other = jack_port_new (client, event->y.other_id, client->engine);
  167. control_port = jack_port_by_id (client, event->x.self_id);
  168. pthread_mutex_lock (&control_port->connection_lock);
  169. control_port->connections = g_slist_prepend (control_port->connections, other);
  170. pthread_mutex_unlock (&control_port->connection_lock);
  171. break;
  172. case PortDisconnected:
  173. control_port = jack_port_by_id (client, event->x.self_id);
  174. pthread_mutex_lock (&control_port->connection_lock);
  175. for (node = control_port->connections; node; node = g_slist_next (node)) {
  176. other = (jack_port_t *) node->data;
  177. if (other->shared->id == event->y.other_id) {
  178. control_port->connections = g_slist_remove_link (control_port->connections, node);
  179. g_slist_free_1 (node);
  180. free (other);
  181. break;
  182. }
  183. }
  184. pthread_mutex_unlock (&control_port->connection_lock);
  185. break;
  186. default:
  187. /* impossible */
  188. break;
  189. }
  190. return 0;
  191. }
  192. static int
  193. jack_handle_reorder (jack_client_t *client, jack_event_t *event)
  194. {
  195. char path[PATH_MAX+1];
  196. if (client->graph_wait_fd >= 0) {
  197. close (client->graph_wait_fd);
  198. client->graph_wait_fd = -1;
  199. }
  200. if (client->graph_next_fd >= 0) {
  201. close (client->graph_next_fd);
  202. client->graph_next_fd = -1;
  203. }
  204. sprintf (path, "%s-%lu", client->fifo_prefix, event->x.n);
  205. if ((client->graph_wait_fd = open (path, O_RDONLY|O_NONBLOCK)) <= 0) {
  206. jack_error ("cannot open specified fifo [%s] for reading (%s)", path, strerror (errno));
  207. return -1;
  208. }
  209. sprintf (path, "%s-%lu", client->fifo_prefix, event->x.n+1);
  210. if ((client->graph_next_fd = open (path, O_WRONLY|O_NONBLOCK)) < 0) {
  211. jack_error ("cannot open specified fifo [%s] for writing (%s)", path, strerror (errno));
  212. return -1;
  213. }
  214. /* If the client registered its own callback for graph order events,
  215. execute it now.
  216. */
  217. if (client->control->graph_order) {
  218. client->control->graph_order (client->control->graph_order_arg);
  219. }
  220. return 0;
  221. }
  222. static int
  223. server_connect (int which)
  224. {
  225. int fd;
  226. struct sockaddr_un addr;
  227. if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) {
  228. jack_error ("cannot create client socket (%s)", strerror (errno));
  229. return -1;
  230. }
  231. addr.sun_family = AF_UNIX;
  232. g_snprintf (addr.sun_path, sizeof (addr.sun_path) - 1, "%s/jack_%d", jack_temp_dir, which);
  233. if (connect (fd, (struct sockaddr *) &addr, sizeof (addr)) < 0) {
  234. jack_error ("cannot connect to jack server", strerror (errno));
  235. close (fd);
  236. return -1;
  237. }
  238. return fd;
  239. }
  240. static int
  241. server_event_connect (jack_client_t *client)
  242. {
  243. int fd;
  244. struct sockaddr_un addr;
  245. jack_client_connect_ack_request_t req;
  246. jack_client_connect_ack_result_t res;
  247. if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) {
  248. jack_error ("cannot create client event socket (%s)", strerror (errno));
  249. return -1;
  250. }
  251. addr.sun_family = AF_UNIX;
  252. g_snprintf (addr.sun_path, sizeof (addr.sun_path) - 1, "%s/jack_ack_0", jack_temp_dir);
  253. if (connect (fd, (struct sockaddr *) &addr, sizeof (addr)) < 0) {
  254. jack_error ("cannot connect to jack server for events", strerror (errno));
  255. close (fd);
  256. return -1;
  257. }
  258. req.client_id = client->control->id;
  259. if (write (fd, &req, sizeof (req)) != sizeof (req)) {
  260. jack_error ("cannot write event connect request to server (%s)", strerror (errno));
  261. close (fd);
  262. return -1;
  263. }
  264. if (read (fd, &res, sizeof (res)) != sizeof (res)) {
  265. jack_error ("cannot read event connect result from server (%s)", strerror (errno));
  266. close (fd);
  267. return -1;
  268. }
  269. if (res.status != 0) {
  270. close (fd);
  271. return -1;
  272. }
  273. return fd;
  274. }
  275. jack_client_t *
  276. jack_client_new (const char *client_name)
  277. {
  278. int req_fd = -1;
  279. int ev_fd = -1;
  280. void *addr;
  281. jack_client_connect_request_t req;
  282. jack_client_connect_result_t res;
  283. jack_port_segment_info_t *si;
  284. jack_client_t *client;
  285. int client_shm_id;
  286. int control_shm_id;
  287. int port_segment_shm_id;
  288. int n;
  289. if (strlen (client_name) > sizeof (req.name) - 1) {
  290. jack_error ("\"%s\" is too long to be used as a JACK client name.\n"
  291. "Please use %lu characters or less.",
  292. sizeof (req.name) - 1);
  293. return NULL;
  294. }
  295. if ((req_fd = server_connect (0)) < 0) {
  296. jack_error ("cannot connect to default JACK server");
  297. return NULL;
  298. }
  299. req.type = ClientOutOfProcess;
  300. strncpy (req.name, client_name, sizeof (req.name) - 1);
  301. if (write (req_fd, &req, sizeof (req)) != sizeof (req)) {
  302. jack_error ("cannot send request to jack server (%s)", strerror (errno));
  303. close (req_fd);
  304. return NULL;
  305. }
  306. if ((n = read (req_fd, &res, sizeof (res))) != sizeof (res)) {
  307. if (errno == 0) {
  308. /* server shut the socket */
  309. jack_error ("could not attach as client (duplicate client name?)");
  310. close (req_fd);
  311. return NULL;
  312. }
  313. jack_error ("cannot read response from jack server (%s)", strerror (errno));
  314. close (req_fd);
  315. return NULL;
  316. }
  317. if (res.status) {
  318. close (req_fd);
  319. jack_error ("could not attach as client (duplicate client name?)");
  320. return NULL;
  321. }
  322. client = jack_client_alloc ();
  323. strcpy (client->fifo_prefix, res.fifo_prefix);
  324. client->request_fd = req_fd;
  325. client->pollfd[0].events = POLLIN|POLLERR|POLLHUP|POLLNVAL;
  326. client->pollfd[1].events = POLLIN|POLLERR|POLLHUP|POLLNVAL;
  327. /* Lookup, attach and register the port/buffer segments in use
  328. right now.
  329. */
  330. if ((port_segment_shm_id = shmget (res.port_segment_key, 0, 0)) < 0) {
  331. jack_error ("cannot determine shared memory segment for port segment key 0x%x (%s)", res.port_segment_key, strerror (errno));
  332. goto fail;
  333. }
  334. if ((addr = shmat (port_segment_shm_id, 0, 0)) == (void *) -1) {
  335. jack_error ("cannot attached port segment shared memory (%s)", strerror (errno));
  336. goto fail;
  337. }
  338. si = (jack_port_segment_info_t *) malloc (sizeof (jack_port_segment_info_t));
  339. si->shm_key = res.port_segment_key;
  340. si->address = addr;
  341. /* the first chunk of the first port segment is always set by the engine
  342. to be a conveniently-sized, zero-filled lump of memory.
  343. */
  344. if (client->port_segments == NULL) {
  345. jack_zero_filled_buffer = si->address;
  346. }
  347. client->port_segments = g_slist_prepend (client->port_segments, si);
  348. /* attach the engine control/info block */
  349. if ((control_shm_id = shmget (res.control_key, 0, 0)) < 0) {
  350. jack_error ("cannot determine shared memory segment for control key 0x%x", res.control_key);
  351. goto fail;
  352. }
  353. if ((addr = shmat (control_shm_id, 0, 0)) == (void *) -1) {
  354. jack_error ("cannot attached engine control shared memory segment");
  355. goto fail;
  356. }
  357. client->engine = (jack_control_t *) addr;
  358. /* now attach the client control block */
  359. if ((client_shm_id = shmget (res.client_key, 0, 0)) < 0) {
  360. jack_error ("cannot determine shared memory segment for client key 0x%x", res.client_key);
  361. goto fail;
  362. }
  363. if ((addr = shmat (client_shm_id, 0, 0)) == (void *) -1) {
  364. jack_error ("cannot attached client control shared memory segment");
  365. goto fail;
  366. }
  367. client->control = (jack_client_control_t *) addr;
  368. if ((ev_fd = server_event_connect (client)) < 0) {
  369. jack_error ("cannot connect to server for event stream (%s)", strerror (errno));
  370. goto fail;
  371. }
  372. client->event_fd = ev_fd;
  373. #ifdef USE_CAPABILITIES
  374. /* get the pid of the client process to pass it to engine */
  375. client->control->pid = getpid ();
  376. #endif
  377. return client;
  378. fail:
  379. if (client->engine) {
  380. shmdt (client->engine);
  381. }
  382. if (client->control) {
  383. shmdt ((char *) client->control);
  384. }
  385. if (req_fd >= 0) {
  386. close (req_fd);
  387. }
  388. if (ev_fd >= 0) {
  389. close (ev_fd);
  390. }
  391. return 0;
  392. }
  393. static void *
  394. jack_client_thread (void *arg)
  395. {
  396. jack_client_t *client = (jack_client_t *) arg;
  397. jack_client_control_t *control = client->control;
  398. jack_event_t event;
  399. char status = 0;
  400. char c;
  401. int err = 0;
  402. pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  403. pthread_mutex_lock (&client_lock);
  404. client->thread_ok = TRUE;
  405. pthread_cond_signal (&client_ready);
  406. pthread_mutex_unlock (&client_lock);
  407. while (err == 0) {
  408. if (poll (client->pollfd, client->pollmax, 1000) < 0) {
  409. if (errno == EINTR) {
  410. printf ("poll interrupted\n");
  411. continue;
  412. }
  413. jack_error ("poll failed in client (%s)", strerror (errno));
  414. status = -1;
  415. break;
  416. }
  417. if (client->pollfd[0].revents & ~POLLIN) {
  418. jack_error ("engine has shut down socket; thread exiting");
  419. if (client->on_shutdown) {
  420. client->on_shutdown (client->on_shutdown_arg);
  421. }
  422. pthread_exit (0);
  423. }
  424. if (client->pollfd[0].revents & POLLIN) {
  425. /* server has sent us an event. process the event and reply */
  426. if (read (client->event_fd, &event, sizeof (event)) != sizeof (event)) {
  427. jack_error ("cannot read server event (%s)", strerror (errno));
  428. err++;
  429. break;
  430. }
  431. status = 0;
  432. switch (event.type) {
  433. case PortRegistered:
  434. if (control->port_register) {
  435. control->port_register (event.x.port_id, TRUE, control->port_register_arg);
  436. }
  437. break;
  438. case PortUnregistered:
  439. if (control->port_register) {
  440. control->port_register (event.x.port_id, FALSE, control->port_register_arg);
  441. }
  442. break;
  443. case GraphReordered:
  444. status = jack_handle_reorder (client, &event);
  445. break;
  446. case PortConnected:
  447. case PortDisconnected:
  448. status = jack_client_handle_port_connection (client, &event);
  449. break;
  450. case BufferSizeChange:
  451. jack_client_invalidate_port_buffers (client);
  452. if (control->bufsize) {
  453. status = control->bufsize (control->nframes, control->bufsize_arg);
  454. }
  455. break;
  456. case SampleRateChange:
  457. if (control->srate) {
  458. status = control->srate (control->nframes, control->srate_arg);
  459. }
  460. break;
  461. case XRun:
  462. if (control->xrun) {
  463. status = control->xrun (control->xrun_arg);
  464. }
  465. break;
  466. case NewPortBufferSegment:
  467. break;
  468. }
  469. if (write (client->event_fd, &status, sizeof (status)) != sizeof (status)) {
  470. jack_error ("cannot send event response to engine (%s)", strerror (errno));
  471. err++;
  472. break;
  473. }
  474. }
  475. if (client->pollfd[1].revents & POLLIN) {
  476. control->signalled_at = get_cycles();
  477. control->state = Running;
  478. if (control->process) {
  479. if (control->process (control->nframes, control->process_arg) == 0) {
  480. control->state = Finished;
  481. }
  482. } else {
  483. control->state = Finished;
  484. }
  485. control->finished_at = get_cycles();
  486. /* pass the execution token along */
  487. if (write (client->graph_next_fd, &c, sizeof (c)) != sizeof (c)) {
  488. jack_error ("cannot continue execution of the processing graph (%s)", strerror(errno));
  489. err++;
  490. break;
  491. }
  492. if ((read (client->graph_wait_fd, &c, sizeof (c)) != sizeof (c))) {
  493. jack_error ("cannot complete execution of the processing graph (%s)", strerror(errno));
  494. err++;
  495. break;
  496. }
  497. }
  498. }
  499. return (void *) err;
  500. }
  501. static int
  502. jack_start_thread (jack_client_t *client)
  503. {
  504. pthread_attr_t *attributes = 0;
  505. #ifdef USE_CAPABILITIES
  506. int policy = SCHED_OTHER;
  507. struct sched_param client_param, temp_param;
  508. #endif
  509. if (client->engine->real_time) {
  510. /* Get the client thread to run as an RT-FIFO
  511. scheduled thread of appropriate priority.
  512. */
  513. struct sched_param rt_param;
  514. attributes = (pthread_attr_t *) malloc (sizeof (pthread_attr_t));
  515. pthread_attr_init (attributes);
  516. if (pthread_attr_setschedpolicy (attributes, SCHED_FIFO)) {
  517. jack_error ("cannot set FIFO scheduling class for RT thread");
  518. return -1;
  519. }
  520. if (pthread_attr_setscope (attributes, PTHREAD_SCOPE_SYSTEM)) {
  521. jack_error ("Cannot set scheduling scope for RT thread");
  522. return -1;
  523. }
  524. memset (&rt_param, 0, sizeof (rt_param));
  525. rt_param.sched_priority = client->engine->client_priority;
  526. if (pthread_attr_setschedparam (attributes, &rt_param)) {
  527. jack_error ("Cannot set scheduling priority for RT thread (%s)", strerror (errno));
  528. return -1;
  529. }
  530. if (mlockall (MCL_CURRENT|MCL_FUTURE)) {
  531. jack_error ("cannot lock down all memory (%s)", strerror (errno));
  532. return -1;
  533. }
  534. }
  535. if (pthread_create (&client->thread, attributes, jack_client_thread, client)) {
  536. #ifdef USE_CAPABILITIES
  537. if (client->engine->real_time && client->engine->has_capabilities) {
  538. /* we are probably dealing with a broken glibc so try
  539. to work around the bug, see below for more details
  540. */
  541. goto capabilities_workaround;
  542. }
  543. #endif
  544. return -1;
  545. }
  546. return 0;
  547. #ifdef USE_CAPABILITIES
  548. /* we get here only with engine running realtime and capabilities */
  549. capabilities_workaround:
  550. /* the version of glibc I've played with has a bug that makes
  551. that code fail when running under a non-root user but with the
  552. proper realtime capabilities (in short, pthread_attr_setschedpolicy
  553. does not check for capabilities, only for the uid being
  554. zero). Newer versions apparently have this fixed. Thus
  555. workaround temporarily switches the client thread to the
  556. proper scheduler and priority, then starts the realtime
  557. thread so that it can inherit them and finally switches the
  558. client thread back to what it was before. Sigh. For ardour
  559. I have to check again and switch the thread explicitly to
  560. realtime, don't know why or how to debug - nando
  561. */
  562. /* get current scheduler and parameters of the client process */
  563. if ((policy = sched_getscheduler (0)) < 0) {
  564. jack_error ("Cannot get current client scheduler: %s", strerror(errno));
  565. return -1;
  566. }
  567. memset (&client_param, 0, sizeof (client_param));
  568. if (sched_getparam (0, &client_param)) {
  569. jack_error ("Cannot get current client scheduler parameters: %s", strerror(errno));
  570. return -1;
  571. }
  572. /* temporarily change the client process to SCHED_FIFO so that
  573. the realtime thread can inherit the scheduler and priority
  574. */
  575. memset (&temp_param, 0, sizeof (temp_param));
  576. temp_param.sched_priority = client->engine->client_priority;
  577. if (sched_setscheduler(0, SCHED_FIFO, &temp_param)) {
  578. jack_error ("Cannot temporarily set client to RT scheduler: %s", strerror(errno));
  579. return -1;
  580. }
  581. /* prepare the attributes for the realtime thread */
  582. attributes = (pthread_attr_t *) malloc (sizeof (pthread_attr_t));
  583. pthread_attr_init (attributes);
  584. if (pthread_attr_setscope (attributes, PTHREAD_SCOPE_SYSTEM)) {
  585. sched_setscheduler (0, policy, &client_param);
  586. jack_error ("Cannot set scheduling scope for RT thread");
  587. return -1;
  588. }
  589. if (pthread_attr_setinheritsched (attributes, PTHREAD_INHERIT_SCHED)) {
  590. sched_setscheduler (0, policy, &client_param);
  591. jack_error ("Cannot set scheduler inherit policy for RT thread");
  592. return -1;
  593. }
  594. /* create the RT thread */
  595. if (pthread_create (&client->thread, attributes, jack_client_thread, client)) {
  596. sched_setscheduler (0, policy, &client_param);
  597. return -1;
  598. }
  599. /* return the client process to the scheduler it was in before */
  600. if (sched_setscheduler (0, policy, &client_param)) {
  601. jack_error ("Cannot reset original client scheduler: %s", strerror(errno));
  602. return -1;
  603. }
  604. /* check again... inheritance of policy and priority works in jack_simple_client
  605. but not in ardour! So I check again and force the policy if it is not set
  606. correctly. This does not really really work either, the manager thread
  607. of the linuxthreads implementation is left running with SCHED_OTHER,
  608. that is presumably very bad.
  609. */
  610. memset (&client_param, 0, sizeof (client_param));
  611. if (pthread_getschedparam(client->thread, &policy, &client_param) == 0) {
  612. if (policy != SCHED_FIFO) {
  613. /* jack_error ("RT thread did not go SCHED_FIFO, trying again"); */
  614. memset (&client_param, 0, sizeof (client_param));
  615. client_param.sched_priority = client->engine->client_priority;
  616. if (pthread_setschedparam (client->thread, SCHED_FIFO, &client_param)) {
  617. jack_error ("Cannot set (again) FIFO scheduling class for RT thread\n");
  618. return -1;
  619. }
  620. }
  621. }
  622. return 0;
  623. #endif
  624. }
  625. int
  626. jack_activate (jack_client_t *client)
  627. {
  628. jack_request_t req;
  629. #define BIG_ENOUGH_STACK 1048576
  630. char buf[BIG_ENOUGH_STACK];
  631. int i;
  632. for (i = 0; i < BIG_ENOUGH_STACK; i++) {
  633. buf[i] = (char) (i & 0xff);
  634. }
  635. #undef BIG_ENOUGH_STACK
  636. #ifdef USE_CAPABILITIES
  637. if (client->engine->has_capabilities != 0 &&
  638. client->control->pid != 0 && client->engine->real_time != 0) {
  639. /* we need to ask the engine for realtime capabilities
  640. before trying to start the realtime thread
  641. */
  642. req.type = SetClientCapabilities;
  643. req.x.client_id = client->control->id;
  644. if (write (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  645. jack_error ("cannot send set client capabilities request to server");
  646. return -1;
  647. }
  648. if (read (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  649. jack_error ("cannot read set client capabilities result from server (%s)", strerror (errno));
  650. return -1;
  651. }
  652. if (req.status) {
  653. /* what to do? engine is running realtime, it is using capabilities and has
  654. them (otherwise we would not get an error return) but for some reason it
  655. could not give the client the required capabilities, so for now downgrade
  656. the client so that it still runs, albeit non-realtime - nando
  657. */
  658. jack_error ("could not receive realtime capabilities, client will run non-realtime");
  659. /* XXX wrong, this is a property of the engine
  660. client->engine->real_time = 0;
  661. */
  662. }
  663. }
  664. #endif
  665. if (client->control->type == ClientOutOfProcess && client->first_active) {
  666. pthread_mutex_init (&client_lock, NULL);
  667. pthread_cond_init (&client_ready, NULL);
  668. pthread_mutex_lock (&client_lock);
  669. if (jack_start_thread (client)) {
  670. pthread_mutex_unlock (&client_lock);
  671. return -1;
  672. }
  673. pthread_cond_wait (&client_ready, &client_lock);
  674. pthread_mutex_unlock (&client_lock);
  675. if (!client->thread_ok) {
  676. jack_error ("could not start client thread");
  677. return -1;
  678. }
  679. client->first_active = FALSE;
  680. }
  681. req.type = ActivateClient;
  682. req.x.client_id = client->control->id;
  683. if (write (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  684. jack_error ("cannot send activate client request to server");
  685. return -1;
  686. }
  687. if (read (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  688. jack_error ("cannot read activate client result from server (%s)", strerror (errno));
  689. return -1;
  690. }
  691. return req.status;
  692. }
  693. int
  694. jack_deactivate (jack_client_t *client)
  695. {
  696. jack_request_t req;
  697. req.type = DeactivateClient;
  698. req.x.client_id = client->control->id;
  699. if (write (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  700. jack_error ("cannot send activate client request to server");
  701. return -1;
  702. }
  703. if (read (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  704. jack_error ("cannot read activate client result from server (%s)", strerror (errno));
  705. return -1;
  706. }
  707. return req.status;
  708. }
  709. int
  710. jack_client_close (jack_client_t *client)
  711. {
  712. GSList *node;
  713. void *status;
  714. /* stop the thread that communicates with the jack server */
  715. pthread_cancel (client->thread);
  716. pthread_join (client->thread, &status);
  717. shmdt ((char *) client->control);
  718. shmdt (client->engine);
  719. for (node = client->port_segments; node; node = g_slist_next (node)) {
  720. shmdt (((jack_port_segment_info_t *) node->data)->address);
  721. free (node->data);
  722. }
  723. g_slist_free (client->port_segments);
  724. for (node = client->ports; node; node = g_slist_next (node)) {
  725. free (node->data);
  726. }
  727. g_slist_free (client->ports);
  728. if (client->graph_wait_fd) {
  729. close (client->graph_wait_fd);
  730. }
  731. if (client->graph_next_fd) {
  732. close (client->graph_next_fd);
  733. }
  734. close (client->event_fd);
  735. close (client->request_fd);
  736. free (client->pollfd);
  737. free (client);
  738. return 0;
  739. }
  740. int
  741. jack_load_client (const char *client_name, const char *path_to_so)
  742. {
  743. int fd;
  744. jack_client_connect_request_t req;
  745. jack_client_connect_result_t res;
  746. if ((fd = server_connect (0)) < 0) {
  747. jack_error ("cannot connect to jack server");
  748. return 0;
  749. }
  750. req.type = ClientDynamic;
  751. strncpy (req.name, client_name, sizeof (req.name) - 1);
  752. req.name[sizeof(req.name)-1] = '\0';
  753. strncpy (req.object_path, path_to_so, sizeof (req.name) - 1);
  754. req.object_path[sizeof(req.object_path)-1] = '\0';
  755. if (write (fd, &req, sizeof (req)) != sizeof (req)) {
  756. jack_error ("cannot send request to jack server (%s)", strerror (errno));
  757. close (fd);
  758. return 0;
  759. }
  760. if (read (fd, &res, sizeof (res)) != sizeof (res)) {
  761. jack_error ("cannot read response from jack server (%s)", strerror (errno));
  762. close (fd);
  763. return 0;
  764. }
  765. close (fd);
  766. return res.status;
  767. }
  768. jack_client_t *
  769. jack_driver_become_client (const char *client_name)
  770. {
  771. int fd;
  772. jack_client_connect_request_t req;
  773. jack_client_connect_result_t res;
  774. jack_client_t *client = 0;
  775. int port_segment_shm_id;
  776. jack_port_segment_info_t *si;
  777. void *addr;
  778. if ((fd = server_connect (0)) < 0) {
  779. jack_error ("cannot connect to jack server");
  780. return 0;
  781. }
  782. req.type = ClientDriver;
  783. strncpy (req.name, client_name, sizeof (req.name) - 1);
  784. req.name[sizeof(req.name)-1] = '\0';
  785. if (write (fd, &req, sizeof (req)) != sizeof (req)) {
  786. jack_error ("cannot send request to jack server (%s)", strerror (errno));
  787. close (fd);
  788. return 0;
  789. }
  790. if (read (fd, &res, sizeof (res)) != sizeof (res)) {
  791. jack_error ("cannot read response from jack server (%s)", strerror (errno));
  792. close (fd);
  793. return 0;
  794. }
  795. if (res.status) {
  796. return 0;
  797. }
  798. client = jack_client_alloc ();
  799. client->request_fd = fd;
  800. client->control = res.client_control;
  801. client->engine = res.engine_control;
  802. /* Lookup, attach and register the port/buffer segments in use
  803. right now.
  804. */
  805. if ((port_segment_shm_id = shmget (res.port_segment_key, 0, 0)) < 0) {
  806. jack_error ("cannot determine shared memory segment for port segment key 0x%x (%s)", res.port_segment_key, strerror (errno));
  807. return NULL;
  808. }
  809. if ((addr = shmat (port_segment_shm_id, 0, 0)) == (void *) -1) {
  810. jack_error ("cannot attached port segment shared memory (%s)", strerror (errno));
  811. return NULL;
  812. }
  813. si = (jack_port_segment_info_t *) malloc (sizeof (jack_port_segment_info_t));
  814. si->shm_key = res.port_segment_key;
  815. si->address = addr;
  816. /* the first chunk of the first port segment is always set by the engine
  817. to be a conveniently-sized, zero-filled lump of memory.
  818. */
  819. if (client->port_segments == NULL) {
  820. jack_zero_filled_buffer = si->address;
  821. }
  822. client->port_segments = g_slist_prepend (client->port_segments, si);
  823. /* allow the engine to act on the client's behalf
  824. when dealing with in-process clients.
  825. */
  826. client->control->private_internal_client = client;
  827. return client;
  828. }
  829. unsigned long jack_get_buffer_size (jack_client_t *client)
  830. {
  831. return client->engine->buffer_size;
  832. }
  833. unsigned long jack_get_sample_rate (jack_client_t *client)
  834. {
  835. return client->engine->time.frame_rate;
  836. }
  837. static jack_port_t *
  838. jack_port_new (jack_client_t *client, jack_port_id_t port_id, jack_control_t *control)
  839. {
  840. jack_port_t *port;
  841. jack_port_shared_t *shared;
  842. jack_port_segment_info_t *si;
  843. GSList *node;
  844. shared = &control->ports[port_id];
  845. port = (jack_port_t *) malloc (sizeof (jack_port_t));
  846. port->client_segment_base = 0;
  847. port->shared = shared;
  848. pthread_mutex_init (&port->connection_lock, NULL);
  849. port->connections = 0;
  850. port->tied = NULL;
  851. si = NULL;
  852. for (node = client->port_segments; node; node = g_slist_next (node)) {
  853. si = (jack_port_segment_info_t *) node->data;
  854. if (si->shm_key == port->shared->shm_key) {
  855. break;
  856. }
  857. }
  858. if (si == NULL) {
  859. jack_error ("cannot find port segment to match newly registered port\n");
  860. return NULL;
  861. }
  862. port->client_segment_base = si->address;
  863. return port;
  864. }
  865. jack_port_t *
  866. jack_port_register (jack_client_t *client,
  867. const char *port_name,
  868. const char *port_type,
  869. unsigned long flags,
  870. unsigned long buffer_size)
  871. {
  872. jack_request_t req;
  873. jack_port_t *port = 0;
  874. jack_port_type_info_t *type_info;
  875. int n;
  876. req.type = RegisterPort;
  877. strcpy ((char *) req.x.port_info.name, (const char *) client->control->name);
  878. strcat ((char *) req.x.port_info.name, ":");
  879. strcat ((char *) req.x.port_info.name, port_name);
  880. strncpy (req.x.port_info.type, port_type, sizeof (req.x.port_info.type) - 1);
  881. req.x.port_info.flags = flags;
  882. req.x.port_info.buffer_size = buffer_size;
  883. req.x.port_info.client_id = client->control->id;
  884. if (write (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  885. jack_error ("cannot send port registration request to server");
  886. return 0;
  887. }
  888. if (read (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  889. jack_error ("cannot read port registration result from server");
  890. return 0;
  891. }
  892. if (req.status != 0) {
  893. return NULL;
  894. }
  895. port = jack_port_new (client, req.x.port_info.port_id, client->engine);
  896. type_info = NULL;
  897. for (n = 0; builtin_port_types[n].type_name[0]; n++) {
  898. if (strcmp (req.x.port_info.type, builtin_port_types[n].type_name) == 0) {
  899. type_info = &builtin_port_types[n];
  900. break;
  901. }
  902. }
  903. if (type_info == NULL) {
  904. /* not a builtin type, so allocate a new type_info structure,
  905. and fill it appropriately.
  906. */
  907. type_info = (jack_port_type_info_t *) malloc (sizeof (jack_port_type_info_t));
  908. snprintf ((char *) type_info->type_name, sizeof (type_info->type_name), req.x.port_info.type);
  909. type_info->mixdown = NULL; /* we have no idea how to mix this */
  910. type_info->buffer_scale_factor = -1; /* use specified port buffer size */
  911. }
  912. memcpy (&port->shared->type_info, type_info, sizeof (jack_port_type_info_t));
  913. client->ports = g_slist_prepend (client->ports, port);
  914. return port;
  915. }
  916. int
  917. jack_port_unregister (jack_client_t *client, jack_port_t *port)
  918. {
  919. jack_request_t req;
  920. req.type = UnRegisterPort;
  921. req.x.port_info.port_id = port->shared->id;
  922. req.x.port_info.client_id = client->control->id;
  923. if (write (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  924. jack_error ("cannot send port registration request to server");
  925. return -1;
  926. }
  927. if (read (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  928. jack_error ("cannot read port registration result from server");
  929. return -1;
  930. }
  931. return req.status;
  932. }
  933. int
  934. jack_connect (jack_client_t *client, const char *source_port, const char *destination_port)
  935. {
  936. jack_request_t req;
  937. req.type = ConnectPorts;
  938. strncpy (req.x.connect.source_port, source_port, sizeof (req.x.connect.source_port) - 1);
  939. req.x.connect.source_port[sizeof(req.x.connect.source_port) - 1] = '\0';
  940. strncpy (req.x.connect.destination_port, destination_port, sizeof (req.x.connect.destination_port) - 1);
  941. req.x.connect.destination_port[sizeof(req.x.connect.destination_port) - 1] = '\0';
  942. if (write (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  943. jack_error ("cannot send port connection request to server");
  944. return -1;
  945. }
  946. if (read (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  947. jack_error ("cannot read port connection result from server");
  948. return -1;
  949. }
  950. return req.status;
  951. }
  952. int
  953. jack_port_disconnect (jack_client_t *client, jack_port_t *port)
  954. {
  955. jack_request_t req;
  956. pthread_mutex_lock (&port->connection_lock);
  957. if (port->connections == NULL) {
  958. pthread_mutex_unlock (&port->connection_lock);
  959. return 0;
  960. }
  961. pthread_mutex_unlock (&port->connection_lock);
  962. req.type = DisconnectPort;
  963. req.x.port_info.port_id = port->shared->id;
  964. if (write (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  965. jack_error ("cannot send port disconnect request to server");
  966. return -1;
  967. }
  968. if (read (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  969. jack_error ("cannot read port disconnect result from server");
  970. return -1;
  971. }
  972. return req.status;
  973. }
  974. int
  975. jack_disconnect (jack_client_t *client, const char *source_port, const char *destination_port)
  976. {
  977. jack_request_t req;
  978. req.type = DisconnectPorts;
  979. strncpy (req.x.connect.source_port, source_port, sizeof (req.x.connect.source_port) - 1);
  980. req.x.connect.source_port[sizeof(req.x.connect.source_port) - 1] = '\0';
  981. strncpy (req.x.connect.destination_port, destination_port, sizeof (req.x.connect.destination_port) - 1);
  982. req.x.connect.destination_port[sizeof(req.x.connect.destination_port) - 1] = '\0';
  983. if (write (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  984. jack_error ("cannot send port connection request to server");
  985. return -1;
  986. }
  987. if (read (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  988. jack_error ("cannot read port connection result from server");
  989. return -1;
  990. }
  991. return req.status;
  992. }
  993. int
  994. jack_engine_takeover_timebase (jack_client_t *client)
  995. {
  996. jack_request_t req;
  997. req.type = SetTimeBaseClient;
  998. req.x.client_id = client->control->id;
  999. if (write (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  1000. jack_error ("cannot send set time base request to server");
  1001. return -1;
  1002. }
  1003. if (read (client->request_fd, &req, sizeof (req)) != sizeof (req)) {
  1004. jack_error ("cannot read set time base result from server");
  1005. return -1;
  1006. }
  1007. return req.status;
  1008. }
  1009. void
  1010. jack_set_error_function (void (*func) (const char *, ...))
  1011. {
  1012. jack_error = func;
  1013. }
  1014. jack_nframes_t
  1015. jack_port_get_latency (jack_port_t *port)
  1016. {
  1017. return port->shared->latency;
  1018. }
  1019. void
  1020. jack_port_set_latency (jack_port_t *port, jack_nframes_t nframes)
  1021. {
  1022. port->shared->latency = nframes;
  1023. }
  1024. void *
  1025. jack_port_get_buffer (jack_port_t *port, jack_nframes_t nframes)
  1026. {
  1027. GSList *node, *next;
  1028. /* Output port. The buffer was assigned by the engine
  1029. when the port was registered.
  1030. */
  1031. if (port->shared->flags & JackPortIsOutput) {
  1032. if (port->tied) {
  1033. return jack_port_get_buffer (port->tied, nframes);
  1034. }
  1035. return jack_port_buffer (port);
  1036. }
  1037. /* Input port.
  1038. */
  1039. /* since this can only be called from the process() callback,
  1040. and since no connections can be made/broken during this
  1041. phase (enforced by the jack server), there is no need
  1042. to take the connection lock here
  1043. */
  1044. if ((node = port->connections) == NULL) {
  1045. /* no connections; return a zero-filled buffer */
  1046. return jack_zero_filled_buffer;
  1047. }
  1048. if ((next = g_slist_next (node)) == NULL) {
  1049. /* one connection: use zero-copy mode - just pass
  1050. the buffer of the connected (output) port.
  1051. */
  1052. return jack_port_buffer (((jack_port_t *) node->data));
  1053. }
  1054. /* multiple connections. use a local buffer and mixdown
  1055. the incoming data to that buffer. we have already
  1056. established the existence of a mixdown function
  1057. during the connection process.
  1058. no port can have an offset of 0 - that offset refers
  1059. to the zero-filled area at the start of a shared port
  1060. segment area. so, use the offset to store the location
  1061. of a locally allocated buffer, and reset the client_segment_base
  1062. so that the jack_port_buffer() computation works correctly.
  1063. */
  1064. if (port->shared->offset == 0) {
  1065. port->shared->offset = (size_t) jack_pool_alloc (port->shared->type_info.buffer_scale_factor *
  1066. sizeof (jack_default_audio_sample_t) * nframes);
  1067. port->client_segment_base = 0;
  1068. }
  1069. port->shared->type_info.mixdown (port, nframes);
  1070. return (jack_default_audio_sample_t *) port->shared->offset;
  1071. }
  1072. int
  1073. jack_port_tie (jack_port_t *src, jack_port_t *dst)
  1074. {
  1075. if (dst->shared->client_id != src->shared->client_id) {
  1076. jack_error ("cannot tie ports not owned by the same client");
  1077. return -1;
  1078. }
  1079. if (dst->shared->flags & JackPortIsOutput) {
  1080. jack_error ("cannot tie an input port");
  1081. return -1;
  1082. }
  1083. dst->tied = src;
  1084. return 0;
  1085. }
  1086. int
  1087. jack_port_untie (jack_port_t *port)
  1088. {
  1089. if (port->tied == NULL) {
  1090. jack_error ("port \"%s\" is not tied", port->shared->name);
  1091. return -1;
  1092. }
  1093. port->tied = NULL;
  1094. return 0;
  1095. }
  1096. int
  1097. jack_set_graph_order_callback (jack_client_t *client, JackGraphOrderCallback callback, void *arg)
  1098. {
  1099. if (client->control->active) {
  1100. return -1;
  1101. }
  1102. client->control->graph_order = callback;
  1103. client->control->graph_order_arg = arg;
  1104. return 0;
  1105. }
  1106. int
  1107. jack_set_process_callback (jack_client_t *client, JackProcessCallback callback, void *arg)
  1108. {
  1109. if (client->control->active) {
  1110. return -1;
  1111. }
  1112. client->control->process_arg = arg;
  1113. client->control->process = callback;
  1114. return 0;
  1115. }
  1116. int
  1117. jack_set_buffer_size_callback (jack_client_t *client, JackBufferSizeCallback callback, void *arg)
  1118. {
  1119. if (client->control->active) {
  1120. return -1;
  1121. }
  1122. client->control->bufsize_arg = arg;
  1123. client->control->bufsize = callback;
  1124. /* Now invoke it */
  1125. callback (client->engine->buffer_size, arg);
  1126. return 0;
  1127. }
  1128. int
  1129. jack_set_sample_rate_callback (jack_client_t *client, JackSampleRateCallback callback, void *arg)
  1130. {
  1131. if (client->control->active) {
  1132. return -1;
  1133. }
  1134. client->control->srate_arg = arg;
  1135. client->control->srate = callback;
  1136. /* Now invoke it */
  1137. callback (client->engine->time.frame_rate, arg);
  1138. return 0;
  1139. }
  1140. int
  1141. jack_set_port_registration_callback(jack_client_t *client, JackPortRegistrationCallback callback, void *arg)
  1142. {
  1143. if (client->control->active) {
  1144. return -1;
  1145. }
  1146. client->control->port_register_arg = arg;
  1147. client->control->port_register = callback;
  1148. return 0;
  1149. }
  1150. int
  1151. jack_get_process_start_fd (jack_client_t *client)
  1152. {
  1153. /* once this has been called, the client thread
  1154. does not sleep on the graph wait fd.
  1155. */
  1156. client->pollmax = 1;
  1157. return client->graph_wait_fd;
  1158. }
  1159. int
  1160. jack_get_process_done_fd (jack_client_t *client)
  1161. {
  1162. return client->graph_next_fd;
  1163. }
  1164. int
  1165. jack_port_request_monitor_by_name (jack_client_t *client, const char *port_name, int onoff)
  1166. {
  1167. jack_port_t *port;
  1168. unsigned long i, limit;
  1169. jack_port_shared_t *ports;
  1170. limit = client->engine->port_max;
  1171. ports = &client->engine->ports[0];
  1172. for (i = 0; i < limit; i++) {
  1173. if (ports[i].in_use && strcmp (ports[i].name, port_name) == 0) {
  1174. port = jack_port_new (client, ports[i].id, client->engine);
  1175. return jack_port_request_monitor (port, onoff);
  1176. free (port);
  1177. return 0;
  1178. }
  1179. }
  1180. return -1;
  1181. }
  1182. int
  1183. jack_port_request_monitor (jack_port_t *port, int onoff)
  1184. {
  1185. if (onoff) {
  1186. port->shared->monitor_requests++;
  1187. } else if (port->shared->monitor_requests) {
  1188. port->shared->monitor_requests--;
  1189. }
  1190. if ((port->shared->flags & JackPortIsOutput) == 0) {
  1191. GSList *node;
  1192. /* this port is for input, so recurse over each of the
  1193. connected ports.
  1194. */
  1195. pthread_mutex_lock (&port->connection_lock);
  1196. for (node = port->connections; node; node = g_slist_next (node)) {
  1197. /* drop the lock because if there is a feedback loop,
  1198. we will deadlock. XXX much worse things will
  1199. happen if there is a feedback loop !!!
  1200. */
  1201. pthread_mutex_unlock (&port->connection_lock);
  1202. jack_port_request_monitor ((jack_port_t *) node->data, onoff);
  1203. pthread_mutex_lock (&port->connection_lock);
  1204. }
  1205. pthread_mutex_unlock (&port->connection_lock);
  1206. }
  1207. return 0;
  1208. }
  1209. int
  1210. jack_ensure_port_monitor_input (jack_port_t *port, int yn)
  1211. {
  1212. if (yn) {
  1213. if (port->shared->monitor_requests == 0) {
  1214. port->shared->monitor_requests++;
  1215. }
  1216. } else {
  1217. if (port->shared->monitor_requests == 1) {
  1218. port->shared->monitor_requests--;
  1219. }
  1220. }
  1221. return 0;
  1222. }
  1223. int
  1224. jack_port_monitoring_input (jack_port_t *port)
  1225. {
  1226. return port->shared->monitor_requests > 0;
  1227. }
  1228. const char *
  1229. jack_port_name (const jack_port_t *port)
  1230. {
  1231. return port->shared->name;
  1232. }
  1233. const char *
  1234. jack_port_short_name (const jack_port_t *port)
  1235. {
  1236. /* we know there is always a colon, because we put
  1237. it there ...
  1238. */
  1239. return strchr (port->shared->name, ':') + 1;
  1240. }
  1241. int
  1242. jack_port_is_mine (const jack_client_t *client, const jack_port_t *port)
  1243. {
  1244. return port->shared->client_id == client->control->id;
  1245. }
  1246. int
  1247. jack_port_flags (const jack_port_t *port)
  1248. {
  1249. return port->shared->flags;
  1250. }
  1251. const char *
  1252. jack_port_type (const jack_port_t *port)
  1253. {
  1254. return port->shared->type_info.type_name;
  1255. }
  1256. int
  1257. jack_port_set_name (jack_port_t *port, const char *new_name)
  1258. {
  1259. char *colon;
  1260. int len;
  1261. colon = strchr (port->shared->name, ':');
  1262. len = sizeof (port->shared->name) - ((int) (colon - port->shared->name)) - 2;
  1263. snprintf (colon+1, len, "%s", new_name);
  1264. return 0;
  1265. }
  1266. void
  1267. jack_on_shutdown (jack_client_t *client, void (*function)(void *arg), void *arg)
  1268. {
  1269. client->on_shutdown = function;
  1270. client->on_shutdown_arg = arg;
  1271. }
  1272. const char **
  1273. jack_get_ports (jack_client_t *client,
  1274. const char *port_name_pattern,
  1275. const char *type_name_pattern,
  1276. unsigned long flags)
  1277. {
  1278. jack_control_t *engine;
  1279. const char **matching_ports;
  1280. unsigned long match_cnt;
  1281. jack_port_shared_t *psp;
  1282. unsigned long i;
  1283. regex_t port_regex;
  1284. regex_t type_regex;
  1285. int matching;
  1286. engine = client->engine;
  1287. if (port_name_pattern && port_name_pattern[0]) {
  1288. regcomp (&port_regex, port_name_pattern, REG_EXTENDED|REG_NOSUB);
  1289. }
  1290. if (type_name_pattern && type_name_pattern[0]) {
  1291. regcomp (&type_regex, type_name_pattern, REG_EXTENDED|REG_NOSUB);
  1292. }
  1293. psp = engine->ports;
  1294. match_cnt = 0;
  1295. matching_ports = (const char **) malloc (sizeof (char *) * engine->port_max);
  1296. for (i = 0; i < engine->port_max; i++) {
  1297. matching = 1;
  1298. if (!psp[i].in_use) {
  1299. continue;
  1300. }
  1301. if (flags) {
  1302. if ((psp[i].flags & flags) != flags) {
  1303. matching = 0;
  1304. }
  1305. }
  1306. if (matching && port_name_pattern && port_name_pattern[0]) {
  1307. if (regexec (&port_regex, psp[i].name, 0, NULL, 0)) {
  1308. matching = 0;
  1309. }
  1310. }
  1311. if (matching && type_name_pattern && type_name_pattern[0]) {
  1312. if (regexec (&type_regex, psp[i].type_info.type_name, 0, NULL, 0)) {
  1313. matching = 0;
  1314. }
  1315. }
  1316. if (matching) {
  1317. matching_ports[match_cnt++] = psp[i].name;
  1318. }
  1319. }
  1320. matching_ports[match_cnt] = 0;
  1321. if (match_cnt == 0) {
  1322. free (matching_ports);
  1323. matching_ports = 0;
  1324. }
  1325. return matching_ports;
  1326. }
  1327. static inline void
  1328. jack_read_frame_time (const jack_client_t *client, jack_frame_timer_t *copy)
  1329. {
  1330. int tries = 0;
  1331. do {
  1332. /* throttle the busy wait if we don't get
  1333. the answer very quickly.
  1334. */
  1335. if (tries > 10) {
  1336. usleep (20);
  1337. tries = 0;
  1338. }
  1339. *copy = client->engine->frame_timer;
  1340. tries++;
  1341. } while (copy->guard1 != copy->guard2);
  1342. }
  1343. jack_nframes_t
  1344. jack_frames_since_cycle_start (const jack_client_t *client)
  1345. {
  1346. float usecs;
  1347. usecs = (float) (get_cycles() - client->engine->time.cycles) / client->cpu_mhz;
  1348. return (jack_nframes_t) floor ((((float) client->engine->time.frame_rate) / 1000000.0f) * usecs);
  1349. }
  1350. jack_nframes_t
  1351. jack_frame_time (const jack_client_t *client)
  1352. {
  1353. jack_frame_timer_t current;
  1354. float usecs;
  1355. jack_nframes_t elapsed;
  1356. jack_read_frame_time (client, &current);
  1357. usecs = (float) (get_cycles() - current.stamp) / client->cpu_mhz;
  1358. elapsed = (jack_nframes_t) floor ((((float) client->engine->time.frame_rate) / 1000000.0f) * usecs);
  1359. return current.frames + elapsed;
  1360. }
  1361. int
  1362. jack_port_lock (jack_client_t *client, jack_port_t *port)
  1363. {
  1364. if (port) {
  1365. port->shared->locked = 1;
  1366. return 0;
  1367. }
  1368. return -1;
  1369. }
  1370. int
  1371. jack_port_unlock (jack_client_t *client, jack_port_t *port)
  1372. {
  1373. if (port) {
  1374. port->shared->locked = 0;
  1375. return 0;
  1376. }
  1377. return -1;
  1378. }
  1379. static void
  1380. jack_audio_port_mixdown (jack_port_t *port, jack_nframes_t nframes)
  1381. {
  1382. GSList *node;
  1383. jack_port_t *input;
  1384. jack_nframes_t n;
  1385. jack_default_audio_sample_t *buffer;
  1386. jack_default_audio_sample_t *dst, *src;
  1387. /* by the time we've called this, we've already established
  1388. the existence of more than 1 connection to this input port.
  1389. */
  1390. /* no need to take connection lock, since this is called
  1391. from the process() callback, and the jack server
  1392. ensures that no changes to connections happen
  1393. during this time.
  1394. */
  1395. node = port->connections;
  1396. input = (jack_port_t *) node->data;
  1397. buffer = jack_port_buffer (port);
  1398. memcpy (buffer, jack_port_buffer (input), sizeof (jack_default_audio_sample_t) * nframes);
  1399. for (node = g_slist_next (node); node; node = g_slist_next (node)) {
  1400. input = (jack_port_t *) node->data;
  1401. n = nframes;
  1402. dst = buffer;
  1403. src = jack_port_buffer (input);
  1404. while (n--) {
  1405. *dst++ += *src++;
  1406. }
  1407. }
  1408. }
  1409. const char **
  1410. jack_port_get_connections (const jack_port_t *port)
  1411. {
  1412. const char **ret;
  1413. GSList *node;
  1414. unsigned int n;
  1415. pthread_mutex_lock (&((jack_port_t *) port)->connection_lock);
  1416. if (port->connections == NULL) {
  1417. pthread_mutex_unlock (&((jack_port_t *) port)->connection_lock);
  1418. return NULL;
  1419. }
  1420. ret = (const char **) malloc (sizeof (char *) * (g_slist_length (port->connections) + 1));
  1421. for (n = 0, node = port->connections; node; node = g_slist_next (node), n++) {
  1422. ret[n] = ((jack_port_t *) node->data)->shared->name;
  1423. }
  1424. ret[n] = NULL;
  1425. pthread_mutex_unlock (&((jack_port_t *) port)->connection_lock);
  1426. return ret;
  1427. }
  1428. int
  1429. jack_port_connected (const jack_port_t *port)
  1430. {
  1431. return port->connections != NULL;
  1432. }
  1433. int
  1434. jack_port_connected_to (const jack_port_t *port, const char *portname)
  1435. {
  1436. GSList *node;
  1437. int ret = FALSE;
  1438. pthread_mutex_lock (&((jack_port_t *) port)->connection_lock);
  1439. for (node = port->connections; node; node = g_slist_next (node)) {
  1440. jack_port_t *other_port = (jack_port_t *) node->data;
  1441. if (strcmp (other_port->shared->name, portname) == 0) {
  1442. ret = TRUE;
  1443. break;
  1444. }
  1445. }
  1446. pthread_mutex_unlock (&((jack_port_t *) port)->connection_lock);
  1447. return ret;
  1448. }
  1449. int
  1450. jack_port_connected_to_port (const jack_port_t *port, const jack_port_t *other_port)
  1451. {
  1452. GSList *node;
  1453. int ret = FALSE;
  1454. pthread_mutex_lock (&((jack_port_t *) port)->connection_lock);
  1455. for (node = port->connections; node; node = g_slist_next (node)) {
  1456. jack_port_t *this_port = (jack_port_t *) node->data;
  1457. if (other_port->shared == this_port->shared) {
  1458. ret = TRUE;
  1459. break;
  1460. }
  1461. }
  1462. pthread_mutex_unlock (&((jack_port_t *) port)->connection_lock);
  1463. return ret;
  1464. }
  1465. /* TRANSPORT CONTROL */
  1466. int
  1467. jack_get_transport_info (jack_client_t *client,
  1468. jack_transport_info_t *info)
  1469. {
  1470. jack_time_info_t *time_info = &client->engine->time;
  1471. if (info->valid & JackTransportState) {
  1472. info->state = time_info->transport_state;
  1473. }
  1474. if (info->valid & JackTransportPosition) {
  1475. info->position = time_info->frame;
  1476. }
  1477. if (info->valid & JackTransportLoop) {
  1478. info->loop_start = time_info->loop_start;
  1479. info->loop_end = time_info->loop_end;
  1480. }
  1481. return 0;
  1482. }
  1483. int
  1484. jack_set_transport_info (jack_client_t *client,
  1485. jack_transport_info_t *info)
  1486. {
  1487. jack_time_info_t *time_info = &client->engine->time;
  1488. if (info->valid & JackTransportState) {
  1489. time_info->transport_state = info->state;
  1490. }
  1491. if (info->valid & JackTransportPosition) {
  1492. time_info->frame = info->position;
  1493. }
  1494. if (info->valid & JackTransportLoop) {
  1495. time_info->loop_start = info->loop_start;
  1496. time_info->loop_end = info->loop_end;
  1497. }
  1498. return 0;
  1499. }
  1500. jack_nframes_t
  1501. jack_port_get_total_latency (jack_client_t *client, jack_port_t *port)
  1502. {
  1503. return port->shared->total_latency;
  1504. }
  1505. int
  1506. jack_get_mhz (void)
  1507. {
  1508. FILE *f = fopen("/proc/cpuinfo", "r");
  1509. if (f == 0)
  1510. {
  1511. perror("can't open /proc/cpuinfo\n");
  1512. exit(1);
  1513. }
  1514. for ( ; ; )
  1515. {
  1516. int mhz;
  1517. int ret;
  1518. char buf[1000];
  1519. if (fgets(buf, sizeof(buf), f) == NULL)
  1520. {
  1521. fprintf(stderr, "cannot locate cpu MHz in /proc/cpuinfo\n");
  1522. exit(1);
  1523. }
  1524. #ifdef __powerpc__
  1525. ret = sscanf(buf, "clock\t: %dMHz", &mhz);
  1526. #else
  1527. ret = sscanf(buf, "cpu MHz : %d", &mhz);
  1528. #endif /* __powerpc__ */
  1529. if (ret == 1)
  1530. {
  1531. fclose(f);
  1532. return mhz;
  1533. }
  1534. }
  1535. }
  1536. float
  1537. jack_cpu_load (jack_client_t *client)
  1538. {
  1539. return client->engine->cpu_load;
  1540. }