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.

462 lines
12KB

  1. /*
  2. JACK transport engine -- runs in the server 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 General Public License as
  7. published by the Free Software Foundation; either version 2 of the
  8. 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 General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. */
  17. #include <config.h>
  18. #include <errno.h>
  19. #include <assert.h>
  20. #include <string.h>
  21. #include <stdio.h>
  22. #include <jack/internal.h>
  23. #include <jack/engine.h>
  24. #include "transengine.h"
  25. /********************** internal functions **********************/
  26. /* initiate polling a new slow-sync client
  27. *
  28. * precondition: caller holds the graph lock. */
  29. static inline void
  30. jack_sync_poll_new (jack_engine_t *engine, jack_client_internal_t *client)
  31. {
  32. /* force sync_cb callback to run in its first cycle */
  33. engine->control->sync_time_left = engine->control->sync_timeout;
  34. client->control->sync_new = 1;
  35. if (!client->control->sync_poll) {
  36. client->control->sync_poll = 1;
  37. engine->control->sync_remain++;
  38. }
  39. // JOQ: I don't like doing this here...
  40. if (engine->control->transport_state == JackTransportRolling) {
  41. engine->control->transport_state = JackTransportStarting;
  42. VERBOSE (engine, "force transport state to Starting\n");
  43. }
  44. VERBOSE (engine, "polling sync client %lu\n", client->control->id);
  45. }
  46. /* stop polling a specific slow-sync client
  47. *
  48. * precondition: caller holds the graph lock. */
  49. static inline void
  50. jack_sync_poll_exit (jack_engine_t *engine, jack_client_internal_t *client)
  51. {
  52. if (client->control->sync_poll) {
  53. client->control->sync_poll = 0;
  54. client->control->sync_new = 0;
  55. engine->control->sync_remain--;
  56. VERBOSE (engine, "sync poll interrupted for client %lu\n",
  57. client->control->id);
  58. }
  59. client->control->is_slowsync = 0;
  60. engine->control->sync_clients--;
  61. }
  62. /* stop polling all the slow-sync clients
  63. *
  64. * precondition: caller holds the graph lock. */
  65. static void
  66. jack_sync_poll_stop (jack_engine_t *engine)
  67. {
  68. JSList *node;
  69. long poll_count = 0; /* count sync_poll clients */
  70. for (node = engine->clients; node; node = jack_slist_next (node)) {
  71. jack_client_internal_t *client =
  72. (jack_client_internal_t *) node->data;
  73. if (client->control->is_slowsync &&
  74. client->control->sync_poll) {
  75. client->control->sync_poll = 0;
  76. poll_count++;
  77. }
  78. }
  79. //JOQ: check invariant for debugging...
  80. assert (poll_count == engine->control->sync_remain);
  81. VERBOSE (engine,
  82. "sync poll halted with %ld clients and %8.6f secs remaining\n",
  83. engine->control->sync_remain,
  84. (double) (engine->control->sync_time_left / 1000000.0));
  85. engine->control->sync_remain = 0;
  86. engine->control->sync_time_left = 0;
  87. }
  88. /* start polling all the slow-sync clients
  89. *
  90. * precondition: caller holds the graph lock. */
  91. static void
  92. jack_sync_poll_start (jack_engine_t *engine)
  93. {
  94. JSList *node;
  95. long sync_count = 0; /* count slow-sync clients */
  96. for (node = engine->clients; node; node = jack_slist_next (node)) {
  97. jack_client_internal_t *client =
  98. (jack_client_internal_t *) node->data;
  99. if (client->control->is_slowsync) {
  100. client->control->sync_poll = 1;
  101. sync_count++;
  102. }
  103. }
  104. //JOQ: check invariant for debugging...
  105. assert (sync_count == engine->control->sync_clients);
  106. engine->control->sync_remain = engine->control->sync_clients;
  107. engine->control->sync_time_left = engine->control->sync_timeout;
  108. VERBOSE (engine, "transport Starting, sync poll of %ld clients "
  109. "for %8.6f secs\n", engine->control->sync_remain,
  110. (double) (engine->control->sync_time_left / 1000000.0));
  111. }
  112. /* check for sync timeout */
  113. static inline int
  114. jack_sync_timeout (jack_engine_t *engine)
  115. {
  116. jack_control_t *ectl = engine->control;
  117. jack_time_t buf_usecs =
  118. ((ectl->buffer_size * (jack_time_t) 1000000) /
  119. ectl->current_time.frame_rate);
  120. /* compare carefully, jack_time_t is unsigned */
  121. if (ectl->sync_time_left > buf_usecs) {
  122. ectl->sync_time_left -= buf_usecs;
  123. return FALSE;
  124. }
  125. /* timed out */
  126. VERBOSE (engine, "transport sync timeout\n");
  127. ectl->sync_time_left = 0;
  128. return TRUE;
  129. }
  130. /**************** subroutines used by engine.c ****************/
  131. /* driver callback */
  132. int
  133. jack_set_sample_rate (jack_engine_t *engine, jack_nframes_t nframes)
  134. {
  135. jack_control_t *ectl = engine->control;
  136. ectl->current_time.frame_rate = nframes;
  137. ectl->pending_time.frame_rate = nframes;
  138. return 0;
  139. }
  140. /* on ResetTimeBaseClient request */
  141. int
  142. jack_timebase_reset (jack_engine_t *engine, jack_client_id_t client_id)
  143. {
  144. int ret;
  145. struct _jack_client_internal *client;
  146. jack_control_t *ectl = engine->control;
  147. jack_lock_graph (engine);
  148. client = jack_client_internal_by_id (engine, client_id);
  149. if (client && (client == engine->timebase_client)) {
  150. client->control->is_timebase = 0;
  151. engine->timebase_client = NULL;
  152. ectl->pending_time.valid = 0;
  153. ret = 0;
  154. } else
  155. ret = EINVAL;
  156. jack_unlock_graph (engine);
  157. return ret;
  158. }
  159. /* on SetTimeBaseClient request */
  160. int
  161. jack_timebase_set (jack_engine_t *engine,
  162. jack_client_id_t client_id, int conditional)
  163. {
  164. int ret = 0;
  165. struct _jack_client_internal *client;
  166. jack_lock_graph (engine);
  167. client = jack_client_internal_by_id (engine, client_id);
  168. if (conditional && engine->timebase_client) {
  169. /* see if timebase master is someone else */
  170. if (client && (client != engine->timebase_client))
  171. ret = EBUSY;
  172. } else {
  173. if (client) {
  174. if (engine->timebase_client)
  175. engine->timebase_client->
  176. control->is_timebase = 0;
  177. engine->timebase_client = client;
  178. client->control->is_timebase = 1;
  179. } else
  180. ret = EINVAL;
  181. }
  182. jack_unlock_graph (engine);
  183. return ret;
  184. }
  185. /* for engine initialization */
  186. void
  187. jack_transport_init (jack_engine_t *engine)
  188. {
  189. jack_control_t *ectl = engine->control;
  190. engine->timebase_client = NULL;
  191. ectl->transport_state = JackTransportStopped;
  192. ectl->transport_cmd = TransportCommandStop;
  193. ectl->previous_cmd = TransportCommandStop;
  194. memset (&ectl->current_time, 0, sizeof(ectl->current_time));
  195. memset (&ectl->pending_time, 0, sizeof(ectl->pending_time));
  196. memset (&ectl->request_time, 0, sizeof(ectl->request_time));
  197. ectl->prev_request = 0;
  198. ectl->seq_number = 1; /* can't start at 0 */
  199. ectl->new_pos = 0;
  200. ectl->pending_pos = 0;
  201. ectl->pending_frame = 0;
  202. ectl->sync_clients = 0;
  203. ectl->sync_remain = 0;
  204. ectl->sync_timeout = 2000000; /* 2 second default */
  205. ectl->sync_time_left = 0;
  206. }
  207. /* when any client exits the graph
  208. *
  209. * precondition: caller holds the graph lock */
  210. void
  211. jack_transport_client_exit (jack_engine_t *engine,
  212. jack_client_internal_t *client)
  213. {
  214. if (client == engine->timebase_client) {
  215. engine->timebase_client->control->is_timebase = 0;
  216. engine->timebase_client = NULL;
  217. engine->control->current_time.valid = 0;
  218. engine->control->pending_time.valid = 0;
  219. VERBOSE (engine, "timebase master exit\n");
  220. }
  221. if (client->control->is_slowsync)
  222. jack_sync_poll_exit(engine, client);
  223. }
  224. /* when a new client is being created */
  225. void
  226. jack_transport_client_new (jack_client_internal_t *client)
  227. {
  228. client->control->is_timebase = 0;
  229. client->control->is_slowsync = 0;
  230. client->control->sync_poll = 0;
  231. client->control->sync_new = 0;
  232. client->control->sync_cb = NULL;
  233. client->control->sync_arg = NULL;
  234. client->control->timebase_cb = NULL;
  235. client->control->timebase_arg = NULL;
  236. }
  237. /* on ResetSyncClient request */
  238. int
  239. jack_transport_client_reset_sync (jack_engine_t *engine,
  240. jack_client_id_t client_id)
  241. {
  242. int ret;
  243. jack_client_internal_t *client;
  244. jack_lock_graph (engine);
  245. client = jack_client_internal_by_id (engine, client_id);
  246. if (client && (client->control->is_slowsync)) {
  247. jack_sync_poll_exit(engine, client);
  248. ret = 0;
  249. } else
  250. ret = EINVAL;
  251. jack_unlock_graph (engine);
  252. return ret;
  253. }
  254. /* on SetSyncClient request */
  255. int
  256. jack_transport_client_set_sync (jack_engine_t *engine,
  257. jack_client_id_t client_id)
  258. {
  259. int ret;
  260. jack_client_internal_t *client;
  261. /* The process cycle runs with this lock. */
  262. jack_lock_graph (engine);
  263. client = jack_client_internal_by_id (engine, client_id);
  264. if (client) {
  265. if (!client->control->is_slowsync) {
  266. client->control->is_slowsync = 1;
  267. engine->control->sync_clients++;
  268. }
  269. /* force poll of the new slow-sync client */
  270. jack_sync_poll_new (engine, client);
  271. ret = 0;
  272. } else
  273. ret = EINVAL;
  274. jack_unlock_graph (engine);
  275. return ret;
  276. }
  277. /* at process cycle end, set transport parameters for the next cycle
  278. *
  279. * precondition: caller holds the graph lock.
  280. */
  281. void
  282. jack_transport_cycle_end (jack_engine_t *engine)
  283. {
  284. jack_control_t *ectl = engine->control;
  285. transport_command_t cmd; /* latest transport command */
  286. /* Promote pending_time to current_time. Maintain the usecs,
  287. * frame_rate and frame values, clients may not set them. */
  288. ectl->pending_time.usecs = ectl->current_time.usecs;
  289. ectl->pending_time.frame_rate = ectl->current_time.frame_rate;
  290. ectl->pending_time.frame = ectl->pending_frame;
  291. ectl->current_time = ectl->pending_time;
  292. ectl->new_pos = ectl->pending_pos;
  293. /* check sync results from previous cycle */
  294. if (ectl->transport_state == JackTransportStarting) {
  295. if ((ectl->sync_remain == 0) ||
  296. (jack_sync_timeout(engine))) {
  297. ectl->transport_state = JackTransportRolling;
  298. VERBOSE (engine, "transport Rolling, %8.6f sec"
  299. " left for poll\n",
  300. (double) (ectl->sync_time_left / 1000000.0));
  301. }
  302. }
  303. /* Handle any new transport command from the last cycle. */
  304. cmd = ectl->transport_cmd;
  305. if (cmd != ectl->previous_cmd) {
  306. ectl->previous_cmd = cmd;
  307. VERBOSE (engine, "transport command: %s\n",
  308. (cmd == TransportCommandStart? "START": "STOP"));
  309. } else
  310. cmd = TransportCommandNone;
  311. /* state transition switch */
  312. switch (ectl->transport_state) {
  313. case JackTransportStopped:
  314. if (cmd == TransportCommandStart) {
  315. if (ectl->sync_clients) {
  316. ectl->transport_state = JackTransportStarting;
  317. jack_sync_poll_start(engine);
  318. } else {
  319. ectl->transport_state = JackTransportRolling;
  320. VERBOSE (engine, "transport Rolling\n");
  321. }
  322. }
  323. break;
  324. case JackTransportStarting:
  325. if (cmd == TransportCommandStop) {
  326. ectl->transport_state = JackTransportStopped;
  327. VERBOSE (engine, "transport Stopped\n");
  328. if (ectl->sync_remain)
  329. jack_sync_poll_stop(engine);
  330. } else if (ectl->new_pos) {
  331. if (ectl->sync_clients) {
  332. ectl->transport_state = JackTransportStarting;
  333. jack_sync_poll_start(engine);
  334. } else {
  335. ectl->transport_state = JackTransportRolling;
  336. VERBOSE (engine, "transport Rolling\n");
  337. }
  338. }
  339. break;
  340. case JackTransportRolling:
  341. if (cmd == TransportCommandStop) {
  342. ectl->transport_state = JackTransportStopped;
  343. VERBOSE (engine, "transport Stopped\n");
  344. if (ectl->sync_remain)
  345. jack_sync_poll_stop(engine);
  346. } else if (ectl->new_pos) {
  347. if (ectl->sync_clients) {
  348. ectl->transport_state = JackTransportStarting;
  349. jack_sync_poll_start(engine);
  350. }
  351. }
  352. break;
  353. default:
  354. jack_error ("invalid JACK transport state: %d",
  355. ectl->transport_state);
  356. }
  357. /* update timebase, if needed */
  358. if (ectl->transport_state == JackTransportRolling) {
  359. ectl->pending_time.frame =
  360. ectl->current_time.frame + ectl->buffer_size;
  361. }
  362. /* See if an asynchronous position request arrived during the
  363. * last cycle. The request_time could change during the
  364. * guarded copy. If so, we use the newest request. */
  365. ectl->pending_pos = 0;
  366. if (ectl->request_time.unique_1 != ectl->prev_request) {
  367. jack_transport_copy_position(&ectl->request_time,
  368. &ectl->pending_time);
  369. VERBOSE (engine, "new transport position: %lu, id=0x%llx\n",
  370. ectl->pending_time.frame, ectl->pending_time.unique_1);
  371. ectl->prev_request = ectl->pending_time.unique_1;
  372. ectl->pending_pos = 1;
  373. }
  374. /* clients can't set pending frame number, so save it here */
  375. ectl->pending_frame = ectl->pending_time.frame;
  376. }
  377. /* driver callback at start of cycle */
  378. void
  379. jack_transport_cycle_start (jack_engine_t *engine, jack_time_t time)
  380. {
  381. engine->control->current_time.usecs = time;
  382. }
  383. /* on SetSyncTimeout request */
  384. int
  385. jack_transport_set_sync_timeout (jack_engine_t *engine,
  386. jack_time_t usecs)
  387. {
  388. engine->control->sync_timeout = usecs;
  389. VERBOSE (engine, "new sync timeout: %8.6f secs\n",
  390. (double) (usecs / 1000000.0));
  391. return 0;
  392. }