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.

476 lines
12KB

  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 <jack/internal.h>
  23. #include "local.h"
  24. /********************* Internal functions *********************/
  25. /* generate a unique non-zero ID, different for each call */
  26. jack_unique_t
  27. jack_generate_unique_id (jack_control_t *ectl)
  28. {
  29. /* The jack_unique_t is an opaque type. Its structure is only
  30. * known here. We use the least significant word of the CPU
  31. * cycle counter. For SMP, I would like to include the
  32. * current thread ID, since only one thread runs on a CPU at a
  33. * time.
  34. *
  35. * But, pthread_self() is broken on my Debian GNU/Linux
  36. * system, it always seems to return 16384. That's useless.
  37. * So, I'm using the process ID instead. With Linux 2.4 and
  38. * Linuxthreads there is an LWP for each thread so this works,
  39. * but is not portable. :-(
  40. */
  41. volatile union {
  42. jack_unique_t unique;
  43. struct {
  44. pid_t pid;
  45. unsigned long cycle;
  46. } field;
  47. } id;
  48. id.field.cycle = (unsigned long) get_cycles();
  49. id.field.pid = getpid();
  50. // JOQ: Alternatively, we could keep a sequence number in
  51. // shared memory, using <asm/atomic.h> to increment it. I
  52. // really like the simplicity of that approach. But I hate
  53. // forcing JACK to either depend on that pesky header file, or
  54. // maintain its own like ardour does.
  55. // fprintf (stderr, "unique ID 0x%llx, process %d, cycle 0x%lx\n",
  56. // id.unique, id.field.pid, id.field.cycle);
  57. return id.unique;
  58. }
  59. static inline void
  60. jack_read_frame_time (const jack_client_t *client, jack_frame_timer_t *copy)
  61. {
  62. int tries = 0;
  63. do {
  64. /* throttle the busy wait if we don't get
  65. the answer very quickly.
  66. */
  67. if (tries > 10) {
  68. usleep (20);
  69. tries = 0;
  70. }
  71. *copy = client->engine->frame_timer;
  72. tries++;
  73. } while (copy->guard1 != copy->guard2);
  74. }
  75. /* copy a JACK transport position structure (thread-safe) */
  76. void
  77. jack_transport_copy_position (jack_position_t *from, jack_position_t *to)
  78. {
  79. int tries = 0;
  80. long timeout = 1000;
  81. do {
  82. /* throttle the busy wait if we don't get the answer
  83. * very quickly. */
  84. if (tries > 10) {
  85. usleep (20);
  86. tries = 0;
  87. /* debug code to avoid system hangs... */
  88. if (--timeout == 0) {
  89. jack_error("hung in loop copying position");
  90. abort();
  91. }
  92. }
  93. *to = *from;
  94. tries++;
  95. } while (to->unique_1 != to->unique_2);
  96. }
  97. static inline int
  98. jack_transport_request_new_pos (jack_client_t *client, jack_position_t *pos)
  99. {
  100. jack_control_t *ectl = client->engine;
  101. /* carefully copy requested postion into shared memory */
  102. pos->unique_1 = pos->unique_2 = jack_generate_unique_id(ectl);
  103. jack_transport_copy_position (pos, &ectl->request_time);
  104. return 0;
  105. }
  106. /******************** Callback invocations ********************/
  107. void
  108. jack_call_sync_client (jack_client_t *client)
  109. {
  110. jack_client_control_t *control = client->control;
  111. jack_control_t *ectl = client->engine;
  112. /* Make sure we are still slow-sync; is_slowsync is set in a
  113. * critical section; sync_cb is not. */
  114. if ((ectl->new_pos || control->sync_poll || control->sync_new) &&
  115. control->is_slowsync) {
  116. if (control->sync_cb (ectl->transport_state,
  117. &ectl->current_time,
  118. control->sync_arg)) {
  119. if (control->sync_poll) {
  120. control->sync_poll = 0;
  121. ectl->sync_remain--;
  122. }
  123. }
  124. control->sync_new = 0;
  125. }
  126. }
  127. void
  128. jack_call_timebase_master (jack_client_t *client)
  129. {
  130. jack_client_control_t *control = client->control;
  131. jack_control_t *ectl = client->engine;
  132. int new_pos = ectl->new_pos;
  133. /* Make sure we're still the master. Test is_timebase, which
  134. * is set in a critical section; timebase_cb is not. */
  135. if (control->is_timebase) {
  136. if (client->new_timebase) { /* first callback? */
  137. client->new_timebase = 0;
  138. new_pos = 1;
  139. }
  140. if ((ectl->transport_state == JackTransportRolling) ||
  141. new_pos) {
  142. control->timebase_cb (ectl->transport_state,
  143. control->nframes,
  144. &ectl->pending_time,
  145. new_pos,
  146. control->timebase_arg);
  147. }
  148. } else {
  149. /* another master took over, so resign */
  150. client->new_timebase = 0;
  151. control->timebase_cb = NULL;
  152. control->timebase_arg = NULL;
  153. }
  154. }
  155. /************************* API functions *************************/
  156. jack_nframes_t
  157. jack_frames_since_cycle_start (const jack_client_t *client)
  158. {
  159. float usecs;
  160. jack_control_t *ectl = client->engine;
  161. usecs = jack_get_microseconds() - ectl->current_time.usecs;
  162. return (jack_nframes_t) floor ((((float) ectl->current_time.frame_rate)
  163. / 1000000.0f) * usecs);
  164. }
  165. jack_nframes_t
  166. jack_frame_time (const jack_client_t *client)
  167. {
  168. jack_frame_timer_t current;
  169. float usecs;
  170. jack_nframes_t elapsed;
  171. jack_control_t *ectl = client->engine;
  172. jack_read_frame_time (client, &current);
  173. usecs = jack_get_microseconds() - current.stamp;
  174. elapsed = (jack_nframes_t)
  175. floor ((((float) ectl->current_time.frame_rate)
  176. / 1000000.0f) * usecs);
  177. return current.frames + elapsed;
  178. }
  179. unsigned long
  180. jack_get_sample_rate (jack_client_t *client)
  181. {
  182. return client->engine->current_time.frame_rate;
  183. }
  184. int
  185. jack_set_sample_rate_callback (jack_client_t *client,
  186. JackSampleRateCallback callback, void *arg)
  187. {
  188. if (client->control->active) {
  189. jack_error ("You cannot set callbacks on an active client.");
  190. return -1;
  191. }
  192. client->control->srate_arg = arg;
  193. client->control->srate = callback;
  194. /* Now invoke it */
  195. callback (client->engine->current_time.frame_rate, arg);
  196. return 0;
  197. }
  198. int
  199. jack_release_timebase (jack_client_t *client)
  200. {
  201. int rc;
  202. jack_request_t req;
  203. jack_client_control_t *ctl = client->control;
  204. req.type = ResetTimeBaseClient;
  205. req.x.client_id = ctl->id;
  206. rc = jack_client_deliver_request (client, &req);
  207. if (rc == 0) {
  208. client->new_timebase = 0;
  209. ctl->timebase_cb = NULL;
  210. ctl->timebase_arg = NULL;
  211. }
  212. return rc;
  213. }
  214. int
  215. jack_set_sync_callback (jack_client_t *client,
  216. JackSyncCallback sync_callback, void *arg)
  217. {
  218. jack_client_control_t *ctl = client->control;
  219. jack_request_t req;
  220. int rc;
  221. if (sync_callback)
  222. req.type = SetSyncClient;
  223. else
  224. req.type = ResetSyncClient;
  225. req.x.client_id = ctl->id;
  226. rc = jack_client_deliver_request (client, &req);
  227. if (rc == 0) {
  228. ctl->sync_cb = sync_callback;
  229. ctl->sync_arg = arg;
  230. }
  231. return rc;
  232. }
  233. int
  234. jack_set_sync_timeout (jack_client_t *client, jack_time_t usecs)
  235. {
  236. jack_request_t req;
  237. req.type = SetSyncTimeout;
  238. req.x.timeout = usecs;
  239. return jack_client_deliver_request (client, &req);
  240. }
  241. int
  242. jack_set_timebase_callback (jack_client_t *client, int conditional,
  243. JackTimebaseCallback timebase_cb, void *arg)
  244. {
  245. int rc;
  246. jack_request_t req;
  247. jack_client_control_t *ctl = client->control;
  248. req.type = SetTimeBaseClient;
  249. req.x.timebase.client_id = ctl->id;
  250. req.x.timebase.conditional = conditional;
  251. rc = jack_client_deliver_request (client, &req);
  252. if (rc == 0) {
  253. client->new_timebase = 1;
  254. ctl->timebase_arg = arg;
  255. ctl->timebase_cb = timebase_cb;
  256. }
  257. return rc;
  258. }
  259. int
  260. jack_transport_locate (jack_client_t *client, jack_nframes_t frame)
  261. {
  262. jack_position_t pos;
  263. pos.frame = frame;
  264. pos.valid = 0;
  265. return jack_transport_request_new_pos (client, &pos);
  266. }
  267. jack_transport_state_t
  268. jack_transport_query (jack_client_t *client, jack_position_t *pos)
  269. {
  270. jack_control_t *ectl = client->engine;
  271. if (pos)
  272. /* the guarded copy makes this function work in any
  273. * thread */
  274. jack_transport_copy_position (&ectl->current_time, pos);
  275. return ectl->transport_state;
  276. }
  277. int
  278. jack_transport_reposition (jack_client_t *client, jack_position_t *pos)
  279. {
  280. /* copy the input, to avoid modifying its contents */
  281. jack_position_t tmp = *pos;
  282. /* validate input */
  283. if (tmp.valid & ~JACK_POSITION_MASK) /* unknown field present? */
  284. return EINVAL;
  285. return jack_transport_request_new_pos (client, &tmp);
  286. }
  287. void
  288. jack_transport_start (jack_client_t *client)
  289. {
  290. client->engine->transport_cmd = TransportCommandStart;
  291. }
  292. void
  293. jack_transport_stop (jack_client_t *client)
  294. {
  295. client->engine->transport_cmd = TransportCommandStop;
  296. }
  297. #ifdef OLD_TRANSPORT
  298. /************* Compatibility with old transport API. *************/
  299. int
  300. jack_engine_takeover_timebase (jack_client_t *client)
  301. {
  302. jack_request_t req;
  303. req.type = SetTimeBaseClient;
  304. req.x.timebase.client_id = client->control->id;
  305. req.x.timebase.conditional = 0;
  306. return jack_client_deliver_request (client, &req);
  307. }
  308. void
  309. jack_get_transport_info (jack_client_t *client,
  310. jack_transport_info_t *info)
  311. {
  312. jack_control_t *ectl = client->engine;
  313. /* check that this is the process thread */
  314. if (!pthread_equal(client->thread_id, pthread_self())) {
  315. jack_error("Invalid thread for jack_get_transport_info().");
  316. abort(); /* kill this client */
  317. }
  318. info->usecs = ectl->current_time.usecs;
  319. info->frame_rate = ectl->current_time.frame_rate;
  320. info->transport_state = ectl->transport_state;
  321. info->frame = ectl->current_time.frame;
  322. info->valid = (ectl->current_time.valid |
  323. JackTransportState | JackTransportPosition);
  324. if (info->valid & JackTransportBBT) {
  325. info->bar = ectl->current_time.bar;
  326. info->beat = ectl->current_time.beat;
  327. info->tick = ectl->current_time.tick;
  328. info->bar_start_tick = ectl->current_time.bar_start_tick;
  329. info->beats_per_bar = ectl->current_time.beats_per_bar;
  330. info->beat_type = ectl->current_time.beat_type;
  331. info->ticks_per_beat = ectl->current_time.ticks_per_beat;
  332. info->beats_per_minute = ectl->current_time.beats_per_minute;
  333. }
  334. }
  335. static int first_error = 1;
  336. void
  337. jack_set_transport_info (jack_client_t *client,
  338. jack_transport_info_t *info)
  339. {
  340. jack_control_t *ectl = client->engine;
  341. if (!client->control->is_timebase) { /* not timebase master? */
  342. if (first_error)
  343. jack_error ("Called jack_set_transport_info(), "
  344. "but not timebase master.");
  345. first_error = 0;
  346. /* JOQ: I would prefer to ignore this request, but
  347. * that would break ardour 0.9-beta2. So, let's allow
  348. * it for now. */
  349. // return;
  350. }
  351. /* check that this is the process thread */
  352. if (!pthread_equal(client->thread_id, pthread_self())) {
  353. jack_error ("Invalid thread for jack_set_transport_info().");
  354. abort(); /* kill this client */
  355. }
  356. /* is there a new state? */
  357. if ((info->valid & JackTransportState) &&
  358. (info->transport_state != ectl->transport_state)) {
  359. if (info->transport_state == JackTransportStopped)
  360. ectl->transport_cmd = TransportCommandStop;
  361. else if ((info->transport_state == JackTransportRolling) &&
  362. (ectl->transport_state != JackTransportStarting))
  363. ectl->transport_cmd = TransportCommandStart;
  364. /* silently ignore anything else */
  365. }
  366. if (info->valid & JackTransportPosition)
  367. ectl->pending_time.frame = info->frame;
  368. else
  369. ectl->pending_time.frame = ectl->current_time.frame;
  370. ectl->pending_time.valid = (info->valid & JACK_POSITION_MASK);
  371. if (info->valid & JackTransportBBT) {
  372. ectl->pending_time.bar = info->bar;
  373. ectl->pending_time.beat = info->beat;
  374. ectl->pending_time.tick = info->tick;
  375. ectl->pending_time.bar_start_tick = info->bar_start_tick;
  376. ectl->pending_time.beats_per_bar = info->beats_per_bar;
  377. ectl->pending_time.beat_type = info->beat_type;
  378. ectl->pending_time.ticks_per_beat = info->ticks_per_beat;
  379. ectl->pending_time.beats_per_minute = info->beats_per_minute;
  380. }
  381. }
  382. #endif /* OLD_TRANSPORT */