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.

1657 lines
41KB

  1. /*
  2. Copyright (C) 2001-2003 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. #if defined(linux)
  17. #include <sys/poll.h>
  18. #elif defined(__APPLE__) && defined(__POWERPC__)
  19. #include "pThreadUtilities.h"
  20. #include "ipc.h"
  21. #include "fakepoll.h"
  22. #endif
  23. #include <sys/socket.h>
  24. #include <sys/un.h>
  25. #include <pthread.h>
  26. #include <errno.h>
  27. #include <fcntl.h>
  28. #include <sys/types.h>
  29. #include <sys/ipc.h>
  30. #include <sys/mman.h>
  31. #include <stdarg.h>
  32. #include <stdio.h>
  33. #include <stdint.h>
  34. #include <regex.h>
  35. #include <math.h>
  36. #include <config.h>
  37. #include <jack/jack.h>
  38. #include <jack/internal.h>
  39. #include <jack/engine.h>
  40. #include <jack/pool.h>
  41. #include <jack/error.h>
  42. #include <jack/time.h>
  43. #include <jack/jslist.h>
  44. #include <jack/version.h>
  45. #include <jack/shm.h>
  46. #include "local.h"
  47. #ifdef WITH_TIMESTAMPS
  48. #include <jack/timestamps.h>
  49. #endif /* WITH_TIMESTAMPS */
  50. #ifdef DEFAULT_TMP_DIR
  51. char *jack_server_dir = DEFAULT_TMP_DIR;
  52. #else
  53. char *jack_server_dir = "/tmp";
  54. #endif
  55. void
  56. jack_set_server_dir (const char *path)
  57. {
  58. jack_server_dir = strdup (path);
  59. }
  60. static pthread_mutex_t client_lock;
  61. static pthread_cond_t client_ready;
  62. void *jack_zero_filled_buffer = 0;
  63. #define event_fd pollfd[0].fd
  64. #define graph_wait_fd pollfd[1].fd
  65. typedef struct {
  66. int status;
  67. struct _jack_client *client;
  68. const char *client_name;
  69. } client_info;
  70. void
  71. jack_error (const char *fmt, ...)
  72. {
  73. va_list ap;
  74. char buffer[300];
  75. va_start (ap, fmt);
  76. vsnprintf (buffer, sizeof(buffer), fmt, ap);
  77. jack_error_callback (buffer);
  78. va_end (ap);
  79. }
  80. void default_jack_error_callback (const char *desc)
  81. {
  82. fprintf(stderr, "%s\n", desc);
  83. }
  84. void (*jack_error_callback)(const char *desc) = &default_jack_error_callback;
  85. static int
  86. oop_client_deliver_request (void *ptr, jack_request_t *req)
  87. {
  88. jack_client_t *client = (jack_client_t*) ptr;
  89. if (write (client->request_fd, req, sizeof (*req)) != sizeof (*req)) {
  90. jack_error ("cannot send request type %d to server", req->type);
  91. req->status = -1;
  92. }
  93. if (read (client->request_fd, req, sizeof (*req)) != sizeof (*req)) {
  94. jack_error ("cannot read result for request type %d from server (%s)", req->type, strerror (errno));
  95. req->status = -1;
  96. }
  97. return req->status;
  98. }
  99. int
  100. jack_client_deliver_request (const jack_client_t *client, jack_request_t *req)
  101. {
  102. /* indirect through the function pointer that was set
  103. either by jack_client_new() (external) or handle_new_client()
  104. in the server.
  105. */
  106. return client->control->deliver_request (client->control->deliver_arg, req);
  107. }
  108. jack_client_t *
  109. jack_client_alloc ()
  110. {
  111. jack_client_t *client;
  112. client = (jack_client_t *) malloc (sizeof (jack_client_t));
  113. client->pollfd = (struct pollfd *) malloc (sizeof (struct pollfd) * 2);
  114. client->pollmax = 2;
  115. client->request_fd = -1;
  116. client->event_fd = -1;
  117. client->graph_wait_fd = -1;
  118. client->graph_next_fd = -1;
  119. client->port_segments = NULL;
  120. client->ports = NULL;
  121. client->engine = NULL;
  122. client->control = 0;
  123. client->thread_ok = FALSE;
  124. client->first_active = TRUE;
  125. client->on_shutdown = NULL;
  126. return client;
  127. }
  128. jack_client_t *
  129. jack_client_alloc_internal (jack_client_control_t *cc, jack_control_t *ec)
  130. {
  131. jack_client_t* client;
  132. client = jack_client_alloc ();
  133. client->control = cc;
  134. client->engine = ec;
  135. return client;
  136. }
  137. static void
  138. jack_client_free (jack_client_t *client)
  139. {
  140. if (client->pollfd) {
  141. free (client->pollfd);
  142. }
  143. free (client);
  144. }
  145. void
  146. jack_client_invalidate_port_buffers (jack_client_t *client)
  147. {
  148. JSList *node;
  149. jack_port_t *port;
  150. /* This releases all local memory owned by input ports
  151. and sets the buffer pointer to NULL. This will cause
  152. jack_port_get_buffer() to reallocate space for the
  153. buffer on the next call (if there is one).
  154. */
  155. for (node = client->ports; node; node = jack_slist_next (node)) {
  156. port = (jack_port_t *) node->data;
  157. if (port->shared->flags & JackPortIsInput) {
  158. if (port->client_segment_base == 0) {
  159. jack_pool_release ((void *) port->shared->offset);
  160. port->client_segment_base = 0;
  161. port->shared->offset = 0;
  162. }
  163. }
  164. }
  165. }
  166. int
  167. jack_client_handle_port_connection (jack_client_t *client, jack_event_t *event)
  168. {
  169. jack_port_t *control_port;
  170. jack_port_t *other;
  171. JSList *node;
  172. switch (event->type) {
  173. case PortConnected:
  174. other = jack_port_new (client, event->y.other_id, client->engine);
  175. control_port = jack_port_by_id (client, event->x.self_id);
  176. pthread_mutex_lock (&control_port->connection_lock);
  177. control_port->connections = jack_slist_prepend (control_port->connections, (void*)other);
  178. pthread_mutex_unlock (&control_port->connection_lock);
  179. break;
  180. case PortDisconnected:
  181. control_port = jack_port_by_id (client, event->x.self_id);
  182. pthread_mutex_lock (&control_port->connection_lock);
  183. for (node = control_port->connections; node; node = jack_slist_next (node)) {
  184. other = (jack_port_t *) node->data;
  185. if (other->shared->id == event->y.other_id) {
  186. control_port->connections = jack_slist_remove_link (control_port->connections, node);
  187. jack_slist_free_1 (node);
  188. free (other);
  189. break;
  190. }
  191. }
  192. pthread_mutex_unlock (&control_port->connection_lock);
  193. break;
  194. default:
  195. /* impossible */
  196. break;
  197. }
  198. return 0;
  199. }
  200. static int
  201. jack_handle_reorder (jack_client_t *client, jack_event_t *event)
  202. {
  203. char path[PATH_MAX+1];
  204. if (client->graph_wait_fd >= 0) {
  205. DEBUG ("closing graph_wait_fd==%d", client->graph_wait_fd);
  206. close (client->graph_wait_fd);
  207. client->graph_wait_fd = -1;
  208. }
  209. if (client->graph_next_fd >= 0) {
  210. DEBUG ("closing graph_next_fd==%d", client->graph_next_fd);
  211. close (client->graph_next_fd);
  212. client->graph_next_fd = -1;
  213. }
  214. sprintf (path, "%s-%lu", client->fifo_prefix, event->x.n);
  215. if ((client->graph_wait_fd = open (path, O_RDONLY|O_NONBLOCK)) < 0) {
  216. jack_error ("cannot open specified fifo [%s] for reading (%s)", path, strerror (errno));
  217. return -1;
  218. }
  219. DEBUG ("opened new graph_wait_fd %d (%s)", client->graph_wait_fd, path);
  220. sprintf (path, "%s-%lu", client->fifo_prefix, event->x.n+1);
  221. if ((client->graph_next_fd = open (path, O_WRONLY|O_NONBLOCK)) < 0) {
  222. jack_error ("cannot open specified fifo [%s] for writing (%s)", path, strerror (errno));
  223. return -1;
  224. }
  225. DEBUG ("opened new graph_next_fd %d (%s)", client->graph_next_fd, path);
  226. /* If the client registered its own callback for graph order events,
  227. execute it now.
  228. */
  229. if (client->control->graph_order) {
  230. client->control->graph_order (client->control->graph_order_arg);
  231. }
  232. return 0;
  233. }
  234. static int
  235. server_connect (int which)
  236. {
  237. int fd;
  238. struct sockaddr_un addr;
  239. if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) {
  240. jack_error ("cannot create client socket (%s)", strerror (errno));
  241. return -1;
  242. }
  243. addr.sun_family = AF_UNIX;
  244. snprintf (addr.sun_path, sizeof (addr.sun_path) - 1, "%s/jack_%d", jack_server_dir, which);
  245. if (connect (fd, (struct sockaddr *) &addr, sizeof (addr)) < 0) {
  246. jack_error ("cannot connect to jack server", strerror (errno));
  247. close (fd);
  248. return -1;
  249. }
  250. return fd;
  251. }
  252. static int
  253. server_event_connect (jack_client_t *client)
  254. {
  255. int fd;
  256. struct sockaddr_un addr;
  257. jack_client_connect_ack_request_t req;
  258. jack_client_connect_ack_result_t res;
  259. if ((fd = socket (AF_UNIX, SOCK_STREAM, 0)) < 0) {
  260. jack_error ("cannot create client event socket (%s)", strerror (errno));
  261. return -1;
  262. }
  263. addr.sun_family = AF_UNIX;
  264. snprintf (addr.sun_path, sizeof (addr.sun_path) - 1, "%s/jack_ack_0", jack_server_dir);
  265. if (connect (fd, (struct sockaddr *) &addr, sizeof (addr)) < 0) {
  266. jack_error ("cannot connect to jack server for events", strerror (errno));
  267. close (fd);
  268. return -1;
  269. }
  270. req.client_id = client->control->id;
  271. if (write (fd, &req, sizeof (req)) != sizeof (req)) {
  272. jack_error ("cannot write event connect request to server (%s)", strerror (errno));
  273. close (fd);
  274. return -1;
  275. }
  276. if (read (fd, &res, sizeof (res)) != sizeof (res)) {
  277. jack_error ("cannot read event connect result from server (%s)", strerror (errno));
  278. close (fd);
  279. return -1;
  280. }
  281. if (res.status != 0) {
  282. close (fd);
  283. return -1;
  284. }
  285. return fd;
  286. }
  287. static int
  288. jack_request_client (ClientType type, const char* client_name, const char* so_name,
  289. const char* so_data, jack_client_connect_result_t *res, int *req_fd)
  290. {
  291. jack_client_connect_request_t req;
  292. *req_fd = -1;
  293. memset (&req, 0, sizeof (req));
  294. if (strlen (client_name) > sizeof (req.name) - 1) {
  295. jack_error ("\"%s\" is too long to be used as a JACK client name.\n"
  296. "Please use %lu characters or less.",
  297. client_name, sizeof (req.name) - 1);
  298. return -1;
  299. }
  300. if (strlen (so_name) > sizeof (req.object_path) - 1) {
  301. jack_error ("\"%s\" is too long to be used as a JACK shared object name.\n"
  302. "Please use %lu characters or less.",
  303. so_name, sizeof (req.object_path) - 1);
  304. return -1;
  305. }
  306. if (strlen (so_data) > sizeof (req.object_data) - 1) {
  307. jack_error ("\"%s\" is too long to be used as a JACK shared object data string.\n"
  308. "Please use %lu characters or less.",
  309. so_data, sizeof (req.object_data) - 1);
  310. return -1;
  311. }
  312. if ((*req_fd = server_connect (0)) < 0) {
  313. jack_error ("cannot connect to default JACK server");
  314. goto fail;
  315. }
  316. req.load = TRUE;
  317. req.type = type;
  318. snprintf (req.name, sizeof (req.name), "%s", client_name);
  319. snprintf (req.object_path, sizeof (req.object_path), "%s", so_name);
  320. snprintf (req.object_data, sizeof (req.object_data), "%s", so_data);
  321. if (write (*req_fd, &req, sizeof (req)) != sizeof (req)) {
  322. jack_error ("cannot send request to jack server (%s)", strerror (errno));
  323. goto fail;
  324. }
  325. if (read (*req_fd, res, sizeof (*res)) != sizeof (*res)) {
  326. if (errno == 0) {
  327. /* server shut the socket */
  328. jack_error ("could not attach as client (duplicate client name?)");
  329. goto fail;
  330. }
  331. jack_error ("cannot read response from jack server (%s)", strerror (errno));
  332. goto fail;
  333. }
  334. if (res->status) {
  335. jack_error ("could not attach as client (duplicate client name?)");
  336. goto fail;
  337. }
  338. if (res->protocol_v != jack_protocol_version){
  339. jack_error ("application linked against too wrong of a version of libjack.");
  340. goto fail;
  341. }
  342. switch (type) {
  343. case ClientDriver:
  344. case ClientInternal:
  345. close (*req_fd);
  346. *req_fd = -1;
  347. break;
  348. default:
  349. break;
  350. }
  351. return 0;
  352. fail:
  353. if (*req_fd >= 0) {
  354. close (*req_fd);
  355. *req_fd = -1;
  356. }
  357. return -1;
  358. }
  359. jack_client_t *
  360. jack_client_new (const char *client_name)
  361. {
  362. int req_fd = -1;
  363. int ev_fd = -1;
  364. jack_client_connect_result_t res;
  365. jack_client_t *client;
  366. void *addr;
  367. int i;
  368. int shmid;
  369. jack_port_type_info_t* type_info;
  370. /* external clients need this initialized; internal clients
  371. will use the setup in the server's address space.
  372. */
  373. jack_init_time ();
  374. if (jack_request_client (ClientExternal, client_name, "", "", &res, &req_fd)) {
  375. return NULL;
  376. }
  377. client = jack_client_alloc ();
  378. strcpy (client->fifo_prefix, res.fifo_prefix);
  379. client->request_fd = req_fd;
  380. client->pollfd[0].events = POLLIN|POLLERR|POLLHUP|POLLNVAL;
  381. client->pollfd[1].events = POLLIN|POLLERR|POLLHUP|POLLNVAL;
  382. /* attach the engine control/info block */
  383. if ((addr = jack_get_shm (res.control_shm_name, res.control_size, O_RDWR,
  384. 0, (PROT_READ|PROT_WRITE), &shmid)) == MAP_FAILED) {
  385. jack_error ("cannot attached engine control shared memory segment");
  386. goto fail;
  387. }
  388. client->engine = (jack_control_t *) addr;
  389. /* now attach the client control block */
  390. if ((addr = jack_get_shm (res.client_shm_name, sizeof (jack_client_control_t), O_RDWR,
  391. 0, (PROT_READ|PROT_WRITE), &shmid)) == MAP_FAILED) {
  392. jack_error ("cannot attached client control shared memory segment");
  393. goto fail;
  394. }
  395. client->control = (jack_client_control_t *) addr;
  396. /* nobody else needs to access this shared memory any more, so
  397. destroy it. because we have our own link to it, it won't vanish
  398. till we exit.
  399. */
  400. jack_destroy_shm (res.client_shm_name);
  401. /* read incoming port type information so that we can get shared memory
  402. information for each one.
  403. */
  404. type_info = (jack_port_type_info_t *) malloc (sizeof (jack_port_type_info_t) * res.n_port_types);
  405. if (read (req_fd, type_info, sizeof (jack_port_type_info_t) * res.n_port_types) !=
  406. sizeof (jack_port_type_info_t) * res.n_port_types) {
  407. jack_error ("cannot read port type information during client connection");
  408. free (type_info);
  409. goto fail;
  410. }
  411. for (i = 0; i < res.n_port_types; ++i) {
  412. jack_client_handle_new_port_type (client, type_info[i].shm_info.shm_name, type_info[i].shm_info.size, 0);
  413. }
  414. free (type_info);
  415. /* set up the client so that it does the right thing for an external client */
  416. client->control->deliver_request = oop_client_deliver_request;
  417. client->control->deliver_arg = client;
  418. if ((ev_fd = server_event_connect (client)) < 0) {
  419. jack_error ("cannot connect to server for event stream (%s)", strerror (errno));
  420. goto fail;
  421. }
  422. client->event_fd = ev_fd;
  423. #if defined(__APPLE__) && defined(__POWERPC__)
  424. /* specific ressources for server/client real-time thread communication */
  425. client->clienttask = mach_task_self();
  426. if (task_get_bootstrap_port(client->clienttask, &client->bp)){
  427. jack_error ("Can't find bootstrap port");
  428. goto fail;
  429. }
  430. if (allocate_mach_clientport(client, res.portnum) < 0) {
  431. jack_error("Can't allocate mach port");
  432. goto fail;
  433. };
  434. #endif
  435. return client;
  436. fail:
  437. if (client->engine) {
  438. munmap ((char *) client->engine, res.control_size);
  439. }
  440. if (client->control) {
  441. munmap ((char *) client->control, sizeof (jack_client_control_t));
  442. }
  443. if (req_fd >= 0) {
  444. close (req_fd);
  445. }
  446. if (ev_fd >= 0) {
  447. close (ev_fd);
  448. }
  449. return 0;
  450. }
  451. int
  452. jack_internal_client_new (const char *client_name, const char *so_name, const char *so_data)
  453. {
  454. jack_client_connect_result_t res;
  455. int req_fd;
  456. return jack_request_client (ClientInternal, client_name, so_name, so_data, &res, &req_fd);
  457. }
  458. void
  459. jack_internal_client_close (const char *client_name)
  460. {
  461. jack_client_connect_request_t req;
  462. int fd;
  463. req.load = FALSE;
  464. snprintf (req.name, sizeof (req.name), "%s", client_name);
  465. if ((fd = server_connect (0)) < 0) {
  466. jack_error ("cannot connect to default JACK server.");
  467. return;
  468. }
  469. if (write (fd, &req, sizeof (req)) != sizeof(req)) {
  470. jack_error ("cannot deliver ClientUnload request to JACK server.");
  471. }
  472. /* no response to this request */
  473. close (fd);
  474. return;
  475. }
  476. void
  477. jack_client_handle_new_port_type (jack_client_t *client, shm_name_t shm_name, size_t size, void* addr)
  478. {
  479. jack_port_segment_info_t *si;
  480. int shmid;
  481. /* Lookup, attach and register the port/buffer segments in use
  482. right now.
  483. */
  484. if (client->control->type == ClientExternal) {
  485. if ((addr = jack_get_shm(shm_name, size, O_RDWR, 0, (PROT_READ|PROT_WRITE), &shmid)) == MAP_FAILED) {
  486. jack_error ("cannot attached port segment shared memory (%s)", strerror (errno));
  487. return;
  488. }
  489. } else {
  490. /* client is in same address space as server, so just use `addr' directly */
  491. }
  492. si = (jack_port_segment_info_t *) malloc (sizeof (jack_port_segment_info_t));
  493. strcpy (si->shm_name, shm_name);
  494. si->address = addr;
  495. si->size = size;
  496. /* the first chunk of the first port segment is always set by the engine
  497. to be a conveniently-sized, zero-filled lump of memory.
  498. */
  499. if (client->port_segments == NULL) {
  500. jack_zero_filled_buffer = si->address;
  501. }
  502. client->port_segments = jack_slist_prepend (client->port_segments, si);
  503. }
  504. static void *
  505. jack_client_thread (void *arg)
  506. {
  507. jack_client_t *client = (jack_client_t *) arg;
  508. jack_client_control_t *control = client->control;
  509. jack_event_t event;
  510. char status = 0;
  511. char c;
  512. int err = 0;
  513. pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  514. pthread_mutex_lock (&client_lock);
  515. client->thread_ok = TRUE;
  516. client->thread_id = pthread_self();
  517. pthread_cond_signal (&client_ready);
  518. pthread_mutex_unlock (&client_lock);
  519. client->control->pid = getpid();
  520. DEBUG ("client thread is now running");
  521. while (err == 0) {
  522. if (client->engine->engine_ok == 0) {
  523. jack_error ("engine unexpectedly shutdown; thread exiting\n");
  524. if (client->on_shutdown) {
  525. client->on_shutdown (client->on_shutdown_arg);
  526. }
  527. pthread_exit (0);
  528. }
  529. DEBUG ("client polling on event_fd and graph_wait_fd...");
  530. if (poll (client->pollfd, client->pollmax, 1000) < 0) {
  531. if (errno == EINTR) {
  532. printf ("poll interrupted\n");
  533. continue;
  534. }
  535. jack_error ("poll failed in client (%s)", strerror (errno));
  536. status = -1;
  537. break;
  538. }
  539. pthread_testcancel();
  540. /* get an accurate timestamp on waking from poll for a process()
  541. cycle.
  542. */
  543. if (client->pollfd[1].revents & POLLIN) {
  544. control->awake_at = jack_get_microseconds();
  545. }
  546. if (client->pollfd[0].revents & ~POLLIN || client->control->dead) {
  547. goto zombie;
  548. }
  549. if (client->pollfd[0].revents & POLLIN) {
  550. DEBUG ("client receives an event, now reading on event fd");
  551. /* server has sent us an event. process the event and reply */
  552. if (read (client->event_fd, &event, sizeof (event)) != sizeof (event)) {
  553. jack_error ("cannot read server event (%s)", strerror (errno));
  554. err++;
  555. break;
  556. }
  557. status = 0;
  558. switch (event.type) {
  559. case PortRegistered:
  560. if (control->port_register) {
  561. control->port_register (event.x.port_id, TRUE, control->port_register_arg);
  562. }
  563. break;
  564. case PortUnregistered:
  565. if (control->port_register) {
  566. control->port_register (event.x.port_id, FALSE, control->port_register_arg);
  567. }
  568. break;
  569. case GraphReordered:
  570. status = jack_handle_reorder (client, &event);
  571. break;
  572. case PortConnected:
  573. case PortDisconnected:
  574. status = jack_client_handle_port_connection (client, &event);
  575. break;
  576. case BufferSizeChange:
  577. jack_client_invalidate_port_buffers (client);
  578. if (control->bufsize) {
  579. status = control->bufsize (control->nframes, control->bufsize_arg);
  580. }
  581. break;
  582. case SampleRateChange:
  583. if (control->srate) {
  584. status = control->srate (control->nframes, control->srate_arg);
  585. }
  586. break;
  587. case XRun:
  588. if (control->xrun) {
  589. status = control->xrun (control->xrun_arg);
  590. }
  591. break;
  592. case NewPortType:
  593. jack_client_handle_new_port_type (client, event.x.shm_name, event.z.size, event.y.addr);
  594. break;
  595. }
  596. DEBUG ("client has dealt with the event, writing response on event fd");
  597. if (write (client->event_fd, &status, sizeof (status)) != sizeof (status)) {
  598. jack_error ("cannot send event response to engine (%s)", strerror (errno));
  599. err++;
  600. break;
  601. }
  602. }
  603. if (client->pollfd[1].revents & POLLIN) {
  604. #ifdef WITH_TIMESTAMPS
  605. jack_reset_timestamps ();
  606. #endif
  607. DEBUG ("client %d signalled at %Lu, awake for process at %Lu (delay = %Lu usecs) (wakeup on graph_wait_fd==%d)",
  608. getpid(),
  609. control->signalled_at,
  610. control->awake_at,
  611. control->awake_at - control->signalled_at,
  612. client->pollfd[1].fd);
  613. control->state = Running;
  614. if (control->process) {
  615. if (control->process (control->nframes, control->process_arg) == 0) {
  616. control->state = Finished;
  617. }
  618. } else {
  619. control->state = Finished;
  620. }
  621. control->finished_at = jack_get_microseconds();
  622. #ifdef WITH_TIMESTAMPS
  623. jack_timestamp ("finished");
  624. #endif
  625. /* pass the execution token along */
  626. DEBUG ("client finished processing at %Lu (elapsed = %Lu usecs), writing on graph_next_fd==%d",
  627. control->finished_at,
  628. control->finished_at - control->awake_at,
  629. client->graph_next_fd);
  630. if (write (client->graph_next_fd, &c, sizeof (c)) != sizeof (c)) {
  631. jack_error ("cannot continue execution of the processing graph (%s)", strerror(errno));
  632. err++;
  633. break;
  634. }
  635. DEBUG ("client sent message to next stage by %Lu, client reading on graph_wait_fd==%d",
  636. jack_get_microseconds(), client->graph_wait_fd);
  637. #ifdef WITH_TIMESTAMPS
  638. jack_timestamp ("read pending byte from wait");
  639. #endif
  640. DEBUG("reading cleanup byte from pipe\n");
  641. if ((read (client->graph_wait_fd, &c, sizeof (c)) != sizeof (c))) {
  642. DEBUG ("WARNING: READ FAILED!");
  643. /*
  644. jack_error ("cannot complete execution of the processing graph (%s)", strerror(errno));
  645. err++;
  646. break;
  647. */
  648. }
  649. /* check if we were killed during the process cycle (or whatever) */
  650. if (client->control->dead) {
  651. goto zombie;
  652. }
  653. DEBUG("process cycle fully complete\n");
  654. #ifdef WITH_TIMESTAMPS
  655. jack_timestamp ("read done");
  656. jack_dump_timestamps (stdout);
  657. #endif
  658. }
  659. }
  660. return (void *) ((intptr_t)err);
  661. zombie:
  662. if (client->on_shutdown) {
  663. jack_error ("zombified - calling shutdown handler");
  664. client->on_shutdown (client->on_shutdown_arg);
  665. } else {
  666. jack_error ("zombified - exiting from JACK");
  667. jack_client_close (client); /* Need a fix : possibly make client crash if zombified without shutdown handler */
  668. }
  669. pthread_exit (0);
  670. /*NOTREACHED*/
  671. return 0;
  672. }
  673. #if defined(__APPLE__) && defined(__POWERPC__)
  674. /* real-time thread : separated from the normal client thread, it will communicate with the server using fast mach RPC mechanism */
  675. static void *
  676. jack_client_process_thread (void *arg)
  677. {
  678. jack_client_t *client = (jack_client_t *) arg;
  679. jack_client_control_t *control = client->control;
  680. int err = 0;
  681. client->control->pid = getpid();
  682. DEBUG ("client process thread is now running");
  683. pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
  684. while (err == 0) {
  685. if (jack_client_suspend(client) < 0) {
  686. pthread_exit (0);
  687. }
  688. control->awake_at = jack_get_microseconds();
  689. DEBUG ("client resumed");
  690. control->state = Running;
  691. if (control->process) {
  692. if (control->process (control->nframes, control->process_arg) == 0) {
  693. control->state = Finished;
  694. }
  695. } else {
  696. control->state = Finished;
  697. }
  698. control->finished_at = jack_get_microseconds();
  699. #ifdef WITH_TIMESTAMPS
  700. jack_timestamp ("finished");
  701. #endif
  702. DEBUG ("client finished processing at %Lu (elapsed = %f usecs)",
  703. control->finished_at, ((float)(control->finished_at - control->awake_at)));
  704. /* check if we were killed during the process cycle (or whatever) */
  705. if (client->control->dead) {
  706. jack_error ("jack_client_process_thread : client->control->dead");
  707. goto zombie;
  708. }
  709. DEBUG("process cycle fully complete\n");
  710. }
  711. return (void *) ((intptr_t)err);
  712. zombie:
  713. jack_error ("jack_client_process_thread : zombified");
  714. if (client->on_shutdown) {
  715. jack_error ("zombified - calling shutdown handler");
  716. client->on_shutdown (client->on_shutdown_arg);
  717. } else {
  718. jack_error ("zombified - exiting from JACK");
  719. jack_client_close (client); /* Need a fix : possibly make client crash if zombified without shutdown handler */
  720. }
  721. pthread_exit (0);
  722. /*NOTREACHED*/
  723. return 0;
  724. }
  725. #endif
  726. static int
  727. jack_start_thread (jack_client_t *client)
  728. {
  729. pthread_attr_t *attributes = 0;
  730. #ifdef USE_CAPABILITIES
  731. int policy = SCHED_OTHER;
  732. struct sched_param client_param, temp_param;
  733. #endif
  734. if (client->engine->real_time) {
  735. /* Get the client thread to run as an RT-FIFO
  736. scheduled thread of appropriate priority.
  737. */
  738. struct sched_param rt_param;
  739. attributes = (pthread_attr_t *) malloc (sizeof (pthread_attr_t));
  740. pthread_attr_init (attributes);
  741. if (pthread_attr_setschedpolicy (attributes, SCHED_FIFO)) {
  742. jack_error ("cannot set FIFO scheduling class for RT thread");
  743. return -1;
  744. }
  745. if (pthread_attr_setscope (attributes, PTHREAD_SCOPE_SYSTEM)) {
  746. jack_error ("Cannot set scheduling scope for RT thread");
  747. return -1;
  748. }
  749. memset (&rt_param, 0, sizeof (rt_param));
  750. rt_param.sched_priority = client->engine->client_priority;
  751. if (pthread_attr_setschedparam (attributes, &rt_param)) {
  752. jack_error ("Cannot set scheduling priority for RT thread (%s)", strerror (errno));
  753. return -1;
  754. }
  755. #if defined(linux)
  756. if (mlockall (MCL_CURRENT | MCL_FUTURE) != 0) {
  757. jack_error ("cannot lock down memory for RT thread (%s)", strerror (errno));
  758. return -1;
  759. }
  760. #elif defined(__APPLE__) && defined(__POWERPC__)
  761. // To be implemented
  762. #endif
  763. }
  764. if (pthread_create (&client->thread, attributes, jack_client_thread, client)) {
  765. #ifdef USE_CAPABILITIES
  766. if (client->engine->real_time && client->engine->has_capabilities) {
  767. /* we are probably dealing with a broken glibc so try
  768. to work around the bug, see below for more details
  769. */
  770. goto capabilities_workaround;
  771. }
  772. #endif
  773. return -1;
  774. }
  775. #if defined(__APPLE__) && defined(__POWERPC__)
  776. /* a spcial real-time thread to call the "process" callback. It will communicate with the server using fast mach RPC mechanism */
  777. if (pthread_create (&client->process_thread, attributes, jack_client_process_thread, client)) {
  778. jack_error("pthread_create failed for process_thread \n");
  779. return -1;
  780. }
  781. if (client->engine->real_time){
  782. /* time constraint thread */
  783. setThreadToPriority(client->process_thread, 96, true, 10000000);
  784. }else{
  785. /* fixed priority thread */
  786. setThreadToPriority(client->process_thread, 63, true, 10000000);
  787. }
  788. #endif
  789. return 0;
  790. #ifdef USE_CAPABILITIES
  791. /* we get here only with engine running realtime and capabilities */
  792. capabilities_workaround:
  793. /* the version of glibc I've played with has a bug that makes
  794. that code fail when running under a non-root user but with the
  795. proper realtime capabilities (in short, pthread_attr_setschedpolicy
  796. does not check for capabilities, only for the uid being
  797. zero). Newer versions apparently have this fixed. This
  798. workaround temporarily switches the client thread to the
  799. proper scheduler and priority, then starts the realtime
  800. thread so that it can inherit them and finally switches the
  801. client thread back to what it was before. Sigh. For ardour
  802. I have to check again and switch the thread explicitly to
  803. realtime, don't know why or how to debug - nando
  804. */
  805. /* get current scheduler and parameters of the client process */
  806. if ((policy = sched_getscheduler (0)) < 0) {
  807. jack_error ("Cannot get current client scheduler: %s", strerror(errno));
  808. return -1;
  809. }
  810. memset (&client_param, 0, sizeof (client_param));
  811. if (sched_getparam (0, &client_param)) {
  812. jack_error ("Cannot get current client scheduler parameters: %s", strerror(errno));
  813. return -1;
  814. }
  815. /* temporarily change the client process to SCHED_FIFO so that
  816. the realtime thread can inherit the scheduler and priority
  817. */
  818. memset (&temp_param, 0, sizeof (temp_param));
  819. temp_param.sched_priority = client->engine->client_priority;
  820. if (sched_setscheduler(0, SCHED_FIFO, &temp_param)) {
  821. jack_error ("Cannot temporarily set client to RT scheduler: %s", strerror(errno));
  822. return -1;
  823. }
  824. /* prepare the attributes for the realtime thread */
  825. attributes = (pthread_attr_t *) malloc (sizeof (pthread_attr_t));
  826. pthread_attr_init (attributes);
  827. if (pthread_attr_setscope (attributes, PTHREAD_SCOPE_SYSTEM)) {
  828. sched_setscheduler (0, policy, &client_param);
  829. jack_error ("Cannot set scheduling scope for RT thread");
  830. return -1;
  831. }
  832. if (pthread_attr_setinheritsched (attributes, PTHREAD_INHERIT_SCHED)) {
  833. sched_setscheduler (0, policy, &client_param);
  834. jack_error ("Cannot set scheduler inherit policy for RT thread");
  835. return -1;
  836. }
  837. /* create the RT thread */
  838. if (pthread_create (&client->thread, attributes, jack_client_thread, client)) {
  839. sched_setscheduler (0, policy, &client_param);
  840. return -1;
  841. }
  842. /* return the client process to the scheduler it was in before */
  843. if (sched_setscheduler (0, policy, &client_param)) {
  844. jack_error ("Cannot reset original client scheduler: %s", strerror(errno));
  845. return -1;
  846. }
  847. /* check again... inheritance of policy and priority works in jack_simple_client
  848. but not in ardour! So I check again and force the policy if it is not set
  849. correctly. This does not really really work either, the manager thread
  850. of the linuxthreads implementation is left running with SCHED_OTHER,
  851. that is presumably very bad.
  852. */
  853. memset (&client_param, 0, sizeof (client_param));
  854. if (pthread_getschedparam(client->thread, &policy, &client_param) == 0) {
  855. if (policy != SCHED_FIFO) {
  856. /* jack_error ("RT thread did not go SCHED_FIFO, trying again"); */
  857. memset (&client_param, 0, sizeof (client_param));
  858. client_param.sched_priority = client->engine->client_priority;
  859. if (pthread_setschedparam (client->thread, SCHED_FIFO, &client_param)) {
  860. jack_error ("Cannot set (again) FIFO scheduling class for RT thread\n");
  861. return -1;
  862. }
  863. }
  864. }
  865. return 0;
  866. #endif
  867. }
  868. int
  869. jack_activate (jack_client_t *client)
  870. {
  871. jack_request_t req;
  872. /* we need to scribble on our stack to ensure that its memory pages are
  873. * actually mapped (more important for mlockall(2) usage in
  874. * jack_start_thread())
  875. */
  876. #if defined(linux)
  877. #define BIG_ENOUGH_STACK 1048576
  878. #elif defined(__APPLE__) && defined(__POWERPC__)
  879. #define BIG_ENOUGH_STACK 10000 // a bigger stack make the application crash...
  880. #endif
  881. char buf[BIG_ENOUGH_STACK];
  882. int i;
  883. for (i = 0; i < BIG_ENOUGH_STACK; i++) {
  884. buf[i] = (char) (i & 0xff);
  885. }
  886. #undef BIG_ENOUGH_STACK
  887. if (client->control->type == ClientInternal || client->control->type == ClientDriver) {
  888. goto startit;
  889. }
  890. /* get the pid of the client process to pass it to engine */
  891. client->control->pid = getpid ();
  892. #ifdef USE_CAPABILITIES
  893. if (client->engine->has_capabilities != 0 &&
  894. client->control->pid != 0 && client->engine->real_time != 0) {
  895. /* we need to ask the engine for realtime capabilities
  896. before trying to start the realtime thread
  897. */
  898. req.type = SetClientCapabilities;
  899. req.x.client_id = client->control->id;
  900. jack_client_deliver_request (client, &req);
  901. if (req.status) {
  902. /* what to do? engine is running realtime, it is using capabilities and has
  903. them (otherwise we would not get an error return) but for some reason it
  904. could not give the client the required capabilities, so for now downgrade
  905. the client so that it still runs, albeit non-realtime - nando
  906. */
  907. jack_error ("could not receive realtime capabilities, client will run non-realtime");
  908. /* XXX wrong, this is a property of the engine
  909. client->engine->real_time = 0;
  910. */
  911. }
  912. }
  913. #endif
  914. if (client->first_active) {
  915. pthread_mutex_init (&client_lock, NULL);
  916. pthread_cond_init (&client_ready, NULL);
  917. pthread_mutex_lock (&client_lock);
  918. if (jack_start_thread (client)) {
  919. pthread_mutex_unlock (&client_lock);
  920. return -1;
  921. }
  922. pthread_cond_wait (&client_ready, &client_lock);
  923. pthread_mutex_unlock (&client_lock);
  924. if (!client->thread_ok) {
  925. jack_error ("could not start client thread");
  926. return -1;
  927. }
  928. client->first_active = FALSE;
  929. }
  930. startit:
  931. req.type = ActivateClient;
  932. req.x.client_id = client->control->id;
  933. return jack_client_deliver_request (client, &req);
  934. }
  935. int
  936. jack_deactivate (jack_client_t *client)
  937. {
  938. jack_request_t req;
  939. req.type = DeactivateClient;
  940. req.x.client_id = client->control->id;
  941. return jack_client_deliver_request (client, &req);
  942. }
  943. int
  944. jack_client_close (jack_client_t *client)
  945. {
  946. JSList *node;
  947. void *status;
  948. if (client->control->active) {
  949. jack_deactivate (client);
  950. }
  951. if (client->control->type == ClientExternal) {
  952. /* stop the thread that communicates with the jack server, only if it was actually running */
  953. if (client->thread_ok){
  954. pthread_cancel (client->thread);
  955. pthread_join (client->thread, &status);
  956. }
  957. munmap ((char *) client->control, sizeof (jack_client_control_t));
  958. munmap ((char *) client->engine, sizeof (jack_control_t));
  959. for (node = client->port_segments; node; node = jack_slist_next (node)) {
  960. jack_port_segment_info_t *si = (jack_port_segment_info_t *) node->data;
  961. munmap ((char *) si->address, si->size);
  962. free (node->data);
  963. }
  964. jack_slist_free (client->port_segments);
  965. if (client->graph_wait_fd) {
  966. close (client->graph_wait_fd);
  967. }
  968. if (client->graph_next_fd) {
  969. close (client->graph_next_fd);
  970. }
  971. close (client->event_fd);
  972. close (client->request_fd);
  973. }
  974. for (node = client->ports; node; node = jack_slist_next (node)) {
  975. free (node->data);
  976. }
  977. jack_slist_free (client->ports);
  978. jack_client_free (client);
  979. return 0;
  980. }
  981. int jack_is_realtime (jack_client_t *client)
  982. {
  983. return client->engine->real_time;
  984. }
  985. unsigned long jack_get_buffer_size (jack_client_t *client)
  986. {
  987. return client->engine->buffer_size;
  988. }
  989. unsigned long jack_get_sample_rate (jack_client_t *client)
  990. {
  991. return client->engine->current_time.frame_rate;
  992. }
  993. int
  994. jack_connect (jack_client_t *client, const char *source_port, const char *destination_port)
  995. {
  996. jack_request_t req;
  997. req.type = ConnectPorts;
  998. snprintf (req.x.connect.source_port, sizeof (req.x.connect.source_port), "%s", source_port);
  999. snprintf (req.x.connect.destination_port, sizeof (req.x.connect.destination_port), "%s", destination_port);
  1000. return jack_client_deliver_request (client, &req);
  1001. }
  1002. int
  1003. jack_port_disconnect (jack_client_t *client, jack_port_t *port)
  1004. {
  1005. jack_request_t req;
  1006. pthread_mutex_lock (&port->connection_lock);
  1007. if (port->connections == NULL) {
  1008. pthread_mutex_unlock (&port->connection_lock);
  1009. return 0;
  1010. }
  1011. pthread_mutex_unlock (&port->connection_lock);
  1012. req.type = DisconnectPort;
  1013. req.x.port_info.port_id = port->shared->id;
  1014. return jack_client_deliver_request (client, &req);
  1015. }
  1016. int
  1017. jack_disconnect (jack_client_t *client, const char *source_port, const char *destination_port)
  1018. {
  1019. jack_request_t req;
  1020. req.type = DisconnectPorts;
  1021. snprintf (req.x.connect.source_port, sizeof (req.x.connect.source_port), "%s", source_port);
  1022. snprintf (req.x.connect.destination_port, sizeof (req.x.connect.destination_port), "%s", destination_port);
  1023. return jack_client_deliver_request (client, &req);
  1024. }
  1025. int
  1026. jack_engine_takeover_timebase (jack_client_t *client)
  1027. {
  1028. jack_request_t req;
  1029. req.type = SetTimeBaseClient;
  1030. req.x.client_id = client->control->id;
  1031. return jack_client_deliver_request (client, &req);
  1032. }
  1033. void
  1034. jack_set_error_function (void (*func) (const char *))
  1035. {
  1036. jack_error_callback = func;
  1037. }
  1038. int
  1039. jack_set_graph_order_callback (jack_client_t *client, JackGraphOrderCallback callback, void *arg)
  1040. {
  1041. if (client->control->active) {
  1042. jack_error ("You cannot set callbacks on an active client.");
  1043. return -1;
  1044. }
  1045. client->control->graph_order = callback;
  1046. client->control->graph_order_arg = arg;
  1047. return 0;
  1048. }
  1049. int jack_set_xrun_callback (jack_client_t *client, JackXRunCallback callback, void *arg)
  1050. {
  1051. if (client->control->active) {
  1052. jack_error ("You cannot set callbacks on an active client.");
  1053. return -1;
  1054. }
  1055. client->control->xrun = callback;
  1056. client->control->xrun_arg = arg;
  1057. return 0;
  1058. }
  1059. int
  1060. jack_set_process_callback (jack_client_t *client, JackProcessCallback callback, void *arg)
  1061. {
  1062. if (client->control->active) {
  1063. jack_error ("You cannot set callbacks on an active client.");
  1064. return -1;
  1065. }
  1066. client->control->process_arg = arg;
  1067. client->control->process = callback;
  1068. return 0;
  1069. }
  1070. int
  1071. jack_set_buffer_size_callback (jack_client_t *client, JackBufferSizeCallback callback, void *arg)
  1072. {
  1073. client->control->bufsize_arg = arg;
  1074. client->control->bufsize = callback;
  1075. return 0;
  1076. }
  1077. int
  1078. jack_set_sample_rate_callback (jack_client_t *client, JackSampleRateCallback callback, void *arg)
  1079. {
  1080. if (client->control->active) {
  1081. jack_error ("You cannot set callbacks on an active client.");
  1082. return -1;
  1083. }
  1084. client->control->srate_arg = arg;
  1085. client->control->srate = callback;
  1086. /* Now invoke it */
  1087. callback (client->engine->current_time.frame_rate, arg);
  1088. return 0;
  1089. }
  1090. int
  1091. jack_set_port_registration_callback(jack_client_t *client, JackPortRegistrationCallback callback, void *arg)
  1092. {
  1093. if (client->control->active) {
  1094. jack_error ("You cannot set callbacks on an active client.");
  1095. return -1;
  1096. }
  1097. client->control->port_register_arg = arg;
  1098. client->control->port_register = callback;
  1099. return 0;
  1100. }
  1101. int
  1102. jack_get_process_start_fd (jack_client_t *client)
  1103. {
  1104. /* once this has been called, the client thread
  1105. does not sleep on the graph wait fd.
  1106. */
  1107. client->pollmax = 1;
  1108. return client->graph_wait_fd;
  1109. }
  1110. int
  1111. jack_get_process_done_fd (jack_client_t *client)
  1112. {
  1113. return client->graph_next_fd;
  1114. }
  1115. void
  1116. jack_on_shutdown (jack_client_t *client, void (*function)(void *arg), void *arg)
  1117. {
  1118. client->on_shutdown = function;
  1119. client->on_shutdown_arg = arg;
  1120. }
  1121. const char **
  1122. jack_get_ports (jack_client_t *client,
  1123. const char *port_name_pattern,
  1124. const char *type_name_pattern,
  1125. unsigned long flags)
  1126. {
  1127. jack_control_t *engine;
  1128. const char **matching_ports;
  1129. unsigned long match_cnt;
  1130. jack_port_shared_t *psp;
  1131. unsigned long i;
  1132. regex_t port_regex;
  1133. regex_t type_regex;
  1134. int matching;
  1135. engine = client->engine;
  1136. if (port_name_pattern && port_name_pattern[0]) {
  1137. regcomp (&port_regex, port_name_pattern, REG_EXTENDED|REG_NOSUB);
  1138. }
  1139. if (type_name_pattern && type_name_pattern[0]) {
  1140. regcomp (&type_regex, type_name_pattern, REG_EXTENDED|REG_NOSUB);
  1141. }
  1142. psp = engine->ports;
  1143. match_cnt = 0;
  1144. matching_ports = (const char **) malloc (sizeof (char *) * engine->port_max);
  1145. for (i = 0; i < engine->port_max; i++) {
  1146. matching = 1;
  1147. if (!psp[i].in_use) {
  1148. continue;
  1149. }
  1150. if (flags) {
  1151. if ((psp[i].flags & flags) != flags) {
  1152. matching = 0;
  1153. }
  1154. }
  1155. if (matching && port_name_pattern && port_name_pattern[0]) {
  1156. if (regexec (&port_regex, psp[i].name, 0, NULL, 0)) {
  1157. matching = 0;
  1158. }
  1159. }
  1160. if (matching && type_name_pattern && type_name_pattern[0]) {
  1161. if (regexec (&type_regex, psp[i].type_info.type_name, 0, NULL, 0)) {
  1162. matching = 0;
  1163. }
  1164. }
  1165. if (matching) {
  1166. matching_ports[match_cnt++] = psp[i].name;
  1167. }
  1168. }
  1169. matching_ports[match_cnt] = 0;
  1170. if (match_cnt == 0) {
  1171. free (matching_ports);
  1172. matching_ports = 0;
  1173. }
  1174. return matching_ports;
  1175. }
  1176. static inline void
  1177. jack_read_frame_time (const jack_client_t *client, jack_frame_timer_t *copy)
  1178. {
  1179. int tries = 0;
  1180. do {
  1181. /* throttle the busy wait if we don't get
  1182. the answer very quickly.
  1183. */
  1184. if (tries > 10) {
  1185. usleep (20);
  1186. tries = 0;
  1187. }
  1188. *copy = client->engine->frame_timer;
  1189. tries++;
  1190. } while (copy->guard1 != copy->guard2);
  1191. }
  1192. jack_nframes_t
  1193. jack_frames_since_cycle_start (const jack_client_t *client)
  1194. {
  1195. float usecs;
  1196. usecs = jack_get_microseconds() - client->engine->current_time.usecs;
  1197. return (jack_nframes_t) floor ((((float) client->engine->current_time.frame_rate) / 1000000.0f) * usecs);
  1198. }
  1199. jack_nframes_t
  1200. jack_frame_time (const jack_client_t *client)
  1201. {
  1202. jack_frame_timer_t current;
  1203. float usecs;
  1204. jack_nframes_t elapsed;
  1205. jack_read_frame_time (client, &current);
  1206. usecs = jack_get_microseconds() - current.stamp;
  1207. elapsed = (jack_nframes_t) floor ((((float) client->engine->current_time.frame_rate) / 1000000.0f) * usecs);
  1208. return current.frames + elapsed;
  1209. }
  1210. /* TRANSPORT CONTROL */
  1211. void
  1212. jack_get_transport_info (jack_client_t *client,
  1213. jack_transport_info_t *info)
  1214. {
  1215. *info = client->engine->current_time;
  1216. }
  1217. void
  1218. jack_set_transport_info (jack_client_t *client,
  1219. jack_transport_info_t *info)
  1220. {
  1221. client->engine->pending_time = *info;
  1222. }
  1223. float
  1224. jack_cpu_load (jack_client_t *client)
  1225. {
  1226. return client->engine->cpu_load;
  1227. }
  1228. pthread_t
  1229. jack_client_thread_id (jack_client_t *client)
  1230. {
  1231. return client->thread_id;
  1232. }
  1233. #if defined(linux)
  1234. jack_time_t
  1235. jack_get_mhz (void)
  1236. {
  1237. FILE *f = fopen("/proc/cpuinfo", "r");
  1238. if (f == 0)
  1239. {
  1240. perror("can't open /proc/cpuinfo\n");
  1241. exit(1);
  1242. }
  1243. for ( ; ; )
  1244. {
  1245. jack_time_t mhz;
  1246. int ret;
  1247. char buf[1000];
  1248. if (fgets(buf, sizeof(buf), f) == NULL)
  1249. {
  1250. fprintf(stderr, "cannot locate cpu MHz in /proc/cpuinfo\n");
  1251. exit(1);
  1252. }
  1253. #ifdef __powerpc__
  1254. ret = sscanf(buf, "clock\t: %LuMHz", &mhz);
  1255. #else
  1256. ret = sscanf(buf, "cpu MHz : %Lu", &mhz);
  1257. #endif /* __powerpc__ */
  1258. if (ret == 1)
  1259. {
  1260. fclose(f);
  1261. return mhz;
  1262. }
  1263. }
  1264. }
  1265. jack_time_t __jack_cpu_mhz;
  1266. void jack_init_time ()
  1267. {
  1268. __jack_cpu_mhz = jack_get_mhz ();
  1269. }
  1270. #elif defined(__APPLE__) && defined(__POWERPC__)
  1271. double __jack_time_ratio;
  1272. void jack_init_time ()
  1273. {
  1274. mach_timebase_info_data_t info;
  1275. mach_timebase_info(&info);
  1276. __jack_time_ratio = ((float)info.numer/info.denom) / 1000;
  1277. }
  1278. #endif