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.

479 lines
13KB

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