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.

545 lines
13KB

  1. /*
  2. JACK transport client interface -- runs in the client process.
  3. Copyright (C) 2001-2003 Paul Davis
  4. Copyright (C) 2003 Jack O'Quin
  5. This program is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public License
  7. as published by the Free Software Foundation; either version 2.1
  8. of the License, or (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with this program; if not, write to the Free
  15. Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  16. 02111-1307, USA.
  17. */
  18. #include <config.h>
  19. #include <errno.h>
  20. #include <math.h>
  21. #include <stdio.h>
  22. #include "atomicity.h"
  23. #include "internal.h"
  24. #include "local.h"
  25. /********************* Internal functions *********************/
  26. /* generate a unique non-zero ID, different for each call */
  27. jack_unique_t
  28. jack_generate_unique_id (jack_control_t *ectl)
  29. {
  30. /* The jack_unique_t is an opaque type. */
  31. return exchange_and_add(&ectl->seq_number, 1);
  32. }
  33. static inline void
  34. jack_read_frame_time (const jack_client_t *client, jack_frame_timer_t *copy)
  35. {
  36. int tries = 0;
  37. long timeout = 1000;
  38. do {
  39. /* throttle the busy wait if we don't get
  40. the answer very quickly.
  41. XXX This is disgusting. on a UP
  42. system, it needs to sleep
  43. if the first try didn't work. on an SMP
  44. system, it should wait for half of
  45. the context switch time before
  46. sleeping.
  47. */
  48. if (tries > 10) {
  49. usleep (20);
  50. tries = 0;
  51. /* debug code to avoid system hangs... */
  52. if (--timeout == 0) {
  53. jack_error ("hung in loop copying position A");
  54. abort();
  55. }
  56. }
  57. *copy = client->engine->frame_timer;
  58. tries++;
  59. } while (copy->guard1 != copy->guard2);
  60. }
  61. /* copy a JACK transport position structure (thread-safe) */
  62. void
  63. jack_transport_copy_position (jack_position_t *from, jack_position_t *to)
  64. {
  65. int tries = 0;
  66. long timeout = 1000;
  67. do {
  68. /* throttle the busy wait if we don't get the answer
  69. * very quickly. See comment above about this
  70. * design.
  71. */
  72. if (tries > 10) {
  73. usleep (20);
  74. tries = 0;
  75. /* debug code to avoid system hangs... */
  76. if (--timeout == 0) {
  77. jack_error ("hung in loop copying position B");
  78. abort();
  79. }
  80. }
  81. *to = *from;
  82. tries++;
  83. } while (to->unique_1 != to->unique_2);
  84. }
  85. static inline int
  86. jack_transport_request_new_pos (jack_client_t *client, jack_position_t *pos)
  87. {
  88. jack_control_t *ectl = client->engine;
  89. /* distinguish this request from all others */
  90. pos->unique_1 = pos->unique_2 = jack_generate_unique_id(ectl);
  91. /* clients may not set these fields */
  92. pos->usecs = ectl->current_time.usecs;
  93. pos->frame_rate = ectl->current_time.frame_rate;
  94. /* carefully copy requested postion into shared memory */
  95. jack_transport_copy_position (pos, &ectl->request_time);
  96. return 0;
  97. }
  98. /******************** Callback invocations ********************/
  99. void
  100. jack_call_sync_client (jack_client_t *client)
  101. {
  102. jack_client_control_t *control = client->control;
  103. jack_control_t *ectl = client->engine;
  104. /* Make sure still active and slow-sync; active_slowsync is
  105. * set in a critical section; sync_cb is not. */
  106. if ((ectl->new_pos || control->sync_poll || control->sync_new) &&
  107. control->active_slowsync) {
  108. if (client->sync_cb (ectl->transport_state,
  109. &ectl->current_time,
  110. client->sync_arg)) {
  111. if (control->sync_poll) {
  112. control->sync_poll = 0;
  113. ectl->sync_remain--;
  114. }
  115. }
  116. control->sync_new = 0;
  117. }
  118. }
  119. void
  120. jack_call_timebase_master (jack_client_t *client)
  121. {
  122. jack_client_control_t *control = client->control;
  123. jack_control_t *ectl = client->engine;
  124. int new_pos = (int) ectl->pending_pos;
  125. /* Make sure this is still the master; is_timebase is set in a
  126. * critical section; timebase_cb is not. */
  127. if (control->is_timebase) {
  128. if (control->timebase_new) { /* first callback? */
  129. control->timebase_new = 0;
  130. new_pos = 1;
  131. }
  132. if ((ectl->transport_state == JackTransportRolling) ||
  133. new_pos) {
  134. client->timebase_cb (ectl->transport_state,
  135. ectl->buffer_size,
  136. &ectl->pending_time,
  137. new_pos,
  138. client->timebase_arg);
  139. }
  140. } else {
  141. /* another master took over, so resign */
  142. client->timebase_cb = NULL;
  143. client->timebase_arg = NULL;
  144. control->timebase_cb_cbset = FALSE;
  145. }
  146. }
  147. /************************* API functions *************************/
  148. jack_nframes_t
  149. jack_get_current_transport_frame (const jack_client_t *client)
  150. {
  151. jack_position_t position;
  152. float usecs;
  153. jack_nframes_t elapsed;
  154. jack_transport_state_t tstate;
  155. /* get the current transport position information.
  156. this is thread-safe and atomic with respect
  157. to the structure contents.
  158. */
  159. tstate = jack_transport_query (client, &position);
  160. if (tstate != JackTransportRolling) {
  161. return position.frame;
  162. }
  163. /* compute the elapsed usecs then audio frames since
  164. the transport info was last updated
  165. */
  166. usecs = jack_get_microseconds() - position.usecs;
  167. elapsed = (jack_nframes_t) floor ((((float) position.frame_rate)
  168. / 1000000.0f) * usecs);
  169. /* return the estimated transport frame position
  170. */
  171. return position.frame + elapsed;
  172. }
  173. jack_nframes_t
  174. jack_frames_since_cycle_start (const jack_client_t *client)
  175. {
  176. float usecs;
  177. jack_control_t *ectl = client->engine;
  178. usecs = jack_get_microseconds() - ectl->current_time.usecs;
  179. return (jack_nframes_t) floor ((((float) ectl->current_time.frame_rate)
  180. / 1000000.0f) * usecs);
  181. }
  182. int
  183. jack_get_cycle_times (const jack_client_t *client,
  184. jack_nframes_t *current_frames,
  185. jack_time_t *current_usecs,
  186. jack_time_t *next_usecs,
  187. float *period_usecs)
  188. {
  189. jack_frame_timer_t time;
  190. jack_read_frame_time (client, &time);
  191. if (time.initialized) {
  192. *current_frames = time.frames;
  193. *current_usecs = time.current_wakeup;
  194. *next_usecs = time.next_wakeup;
  195. *period_usecs = time.period_usecs;
  196. return 0;
  197. }
  198. return 1;
  199. }
  200. jack_time_t
  201. jack_get_time()
  202. {
  203. return jack_get_microseconds();
  204. }
  205. jack_nframes_t
  206. jack_time_to_frames(const jack_client_t *client, jack_time_t usecs)
  207. {
  208. jack_frame_timer_t time;
  209. jack_control_t *ectl = client->engine;
  210. jack_read_frame_time (client, &time);
  211. if (time.initialized) {
  212. /*
  213. Make sure we have signed differences. It would make a lot of sense
  214. to use the standard signed intNN_t types everywhere instead of e.g.
  215. jack_nframes_t and jack_time_t. This would at least ensure that the
  216. types used below are the correct ones. There is no way to get a type
  217. that would be 'a signed version of jack_time_t' for example - the
  218. types below are inherently fragile and there is no automatic way to
  219. check they are the correct ones. The only way is to check manually
  220. against jack/types.h. FA - 16/02/2012
  221. */
  222. int64_t du = usecs - time.current_wakeup;
  223. int64_t dp = time.next_wakeup - time.current_wakeup;
  224. return time.frames + (int32_t) rint ((double) du / (double) dp
  225. * ectl->buffer_size);
  226. }
  227. return 0;
  228. }
  229. jack_nframes_t
  230. jack_frame_time (const jack_client_t *client)
  231. {
  232. jack_time_t now = jack_get_microseconds();
  233. return jack_time_to_frames(client, now);
  234. }
  235. jack_nframes_t
  236. jack_last_frame_time (const jack_client_t *client)
  237. {
  238. return client->engine->frame_timer.frames;
  239. }
  240. jack_time_t
  241. jack_frames_to_time(const jack_client_t *client, jack_nframes_t frames)
  242. {
  243. jack_frame_timer_t time;
  244. jack_control_t *ectl = client->engine;
  245. jack_read_frame_time (client, &time);
  246. if (time.initialized) {
  247. /*
  248. Make sure we have signed differences. It would make a lot of sense
  249. to use the standard signed intNN_t types everywhere instead of e.g.
  250. jack_nframes_t and jack_time_t. This would at least ensure that the
  251. types used below are the correct ones. There is no way to get a type
  252. that would be 'a signed version of jack_time_t' for example - the
  253. types below are inherently fragile and there is no automatic way to
  254. check they are the correct ones. The only way is to check manually
  255. against jack/types.h. FA - 16/02/2012
  256. */
  257. int32_t df = frames - time.frames;
  258. int64_t dp = time.next_wakeup - time.current_wakeup;
  259. return time.current_wakeup + (int64_t) rint ((double) df * (double) dp
  260. / ectl->buffer_size);
  261. }
  262. return 0;
  263. }
  264. jack_nframes_t
  265. jack_get_sample_rate (jack_client_t *client)
  266. {
  267. return client->engine->current_time.frame_rate;
  268. }
  269. int
  270. jack_set_sample_rate_callback (jack_client_t *client,
  271. JackSampleRateCallback callback, void *arg)
  272. {
  273. if (client->control->active) {
  274. jack_error ("You cannot set callbacks on an active client.");
  275. return -1;
  276. }
  277. client->srate_arg = arg;
  278. client->srate = callback;
  279. client->control->srate_cbset = (callback != NULL);
  280. /* Now invoke it */
  281. callback (client->engine->current_time.frame_rate, arg);
  282. return 0;
  283. }
  284. int
  285. jack_release_timebase (jack_client_t *client)
  286. {
  287. int rc;
  288. jack_request_t req;
  289. jack_client_control_t *ctl = client->control;
  290. VALGRIND_MEMSET (&req, 0, sizeof (req));
  291. req.type = ResetTimeBaseClient;
  292. req.x.client_id = ctl->id;
  293. rc = jack_client_deliver_request (client, &req);
  294. if (rc == 0) {
  295. client->timebase_cb = NULL;
  296. client->timebase_arg = NULL;
  297. ctl->timebase_cb_cbset = 0;
  298. }
  299. return rc;
  300. }
  301. int
  302. jack_set_sync_callback (jack_client_t *client,
  303. JackSyncCallback sync_callback, void *arg)
  304. {
  305. jack_client_control_t *ctl = client->control;
  306. jack_request_t req;
  307. int rc;
  308. VALGRIND_MEMSET (&req, 0, sizeof (req));
  309. if (sync_callback)
  310. req.type = SetSyncClient;
  311. else
  312. req.type = ResetSyncClient;
  313. req.x.client_id = ctl->id;
  314. rc = jack_client_deliver_request (client, &req);
  315. if (rc == 0) {
  316. client->sync_cb = sync_callback;
  317. client->sync_arg = arg;
  318. ctl->sync_cb_cbset = TRUE;
  319. }
  320. return rc;
  321. }
  322. int
  323. jack_set_sync_timeout (jack_client_t *client, jack_time_t usecs)
  324. {
  325. jack_request_t req;
  326. VALGRIND_MEMSET (&req, 0, sizeof (req));
  327. req.type = SetSyncTimeout;
  328. req.x.timeout = usecs;
  329. return jack_client_deliver_request (client, &req);
  330. }
  331. int
  332. jack_set_timebase_callback (jack_client_t *client, int conditional,
  333. JackTimebaseCallback timebase_cb, void *arg)
  334. {
  335. int rc;
  336. jack_request_t req;
  337. jack_client_control_t *ctl = client->control;
  338. VALGRIND_MEMSET (&req, 0, sizeof (req));
  339. req.type = SetTimeBaseClient;
  340. req.x.timebase.client_id = ctl->id;
  341. req.x.timebase.conditional = conditional;
  342. rc = jack_client_deliver_request (client, &req);
  343. if (rc == 0) {
  344. client->timebase_arg = arg;
  345. client->timebase_cb = timebase_cb;
  346. ctl->timebase_cb_cbset = TRUE;
  347. }
  348. return rc;
  349. }
  350. int
  351. jack_transport_locate (jack_client_t *client, jack_nframes_t frame)
  352. {
  353. jack_position_t pos;
  354. pos.frame = frame;
  355. pos.valid = 0;
  356. return jack_transport_request_new_pos (client, &pos);
  357. }
  358. jack_transport_state_t
  359. jack_transport_query (const jack_client_t *client, jack_position_t *pos)
  360. {
  361. jack_control_t *ectl = client->engine;
  362. if (pos) {
  363. /* the guarded copy makes this function work in any
  364. * thread
  365. */
  366. jack_transport_copy_position (&ectl->current_time, pos);
  367. }
  368. return ectl->transport_state;
  369. }
  370. int
  371. jack_transport_reposition (jack_client_t *client, const jack_position_t *pos)
  372. {
  373. /* copy the input, to avoid modifying its contents */
  374. jack_position_t tmp = *pos;
  375. /* validate input */
  376. if (tmp.valid & ~JACK_POSITION_MASK) /* unknown field present? */
  377. return EINVAL;
  378. return jack_transport_request_new_pos (client, &tmp);
  379. }
  380. void
  381. jack_transport_start (jack_client_t *client)
  382. {
  383. client->engine->transport_cmd = TransportCommandStart;
  384. }
  385. void
  386. jack_transport_stop (jack_client_t *client)
  387. {
  388. client->engine->transport_cmd = TransportCommandStop;
  389. }
  390. #ifdef OLD_TRANSPORT
  391. /************* Compatibility with old transport API. *************/
  392. int
  393. jack_engine_takeover_timebase (jack_client_t *client)
  394. {
  395. jack_error ("jack_engine_takeover_timebase() is no longer supported.");
  396. return ENOSYS;
  397. }
  398. void
  399. jack_get_transport_info (jack_client_t *client,
  400. jack_transport_info_t *info)
  401. {
  402. jack_control_t *ectl = client->engine;
  403. static int first_time = 1;
  404. if (first_time)
  405. jack_error ("jack_get_transport_info() is deprecated.");
  406. first_time = 0;
  407. /* check that this is the process thread */
  408. if (!pthread_equal(client->thread_id, pthread_self())) {
  409. jack_error("Invalid thread for jack_get_transport_info().");
  410. abort(); /* kill this client */
  411. }
  412. info->usecs = ectl->current_time.usecs;
  413. info->frame_rate = ectl->current_time.frame_rate;
  414. info->transport_state = ectl->transport_state;
  415. info->frame = ectl->current_time.frame;
  416. info->valid = (ectl->current_time.valid |
  417. JackTransportState | JackTransportPosition);
  418. if (info->valid & JackTransportBBT) {
  419. info->bar = ectl->current_time.bar;
  420. info->beat = ectl->current_time.beat;
  421. info->tick = ectl->current_time.tick;
  422. info->bar_start_tick = ectl->current_time.bar_start_tick;
  423. info->beats_per_bar = ectl->current_time.beats_per_bar;
  424. info->beat_type = ectl->current_time.beat_type;
  425. info->ticks_per_beat = ectl->current_time.ticks_per_beat;
  426. info->beats_per_minute = ectl->current_time.beats_per_minute;
  427. }
  428. }
  429. void
  430. jack_set_transport_info (jack_client_t *client,
  431. jack_transport_info_t *info)
  432. {
  433. static int first_time = 1;
  434. if (first_time)
  435. jack_error ("jack_set_transport_info() no longer supported.");
  436. first_time = 0;
  437. }
  438. #endif /* OLD_TRANSPORT */