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.

460 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->sync_remain = 0;
  201. ectl->sync_clients = 0;
  202. ectl->sync_timeout = 2000000; /* 2 second default */
  203. ectl->sync_time_left = 0;
  204. }
  205. /* when any client exits the graph
  206. *
  207. * precondition: caller holds the graph lock */
  208. void
  209. jack_transport_client_exit (jack_engine_t *engine,
  210. jack_client_internal_t *client)
  211. {
  212. if (client == engine->timebase_client) {
  213. engine->timebase_client->control->is_timebase = 0;
  214. engine->timebase_client = NULL;
  215. engine->control->current_time.valid = 0;
  216. engine->control->pending_time.valid = 0;
  217. VERBOSE (engine, "timebase master exit\n");
  218. }
  219. if (client->control->is_slowsync)
  220. jack_sync_poll_exit(engine, client);
  221. }
  222. /* when a new client is being created */
  223. void
  224. jack_transport_client_new (jack_client_internal_t *client)
  225. {
  226. client->control->is_timebase = 0;
  227. client->control->is_slowsync = 0;
  228. client->control->sync_poll = 0;
  229. client->control->sync_new = 0;
  230. client->control->sync_cb = NULL;
  231. client->control->sync_arg = NULL;
  232. client->control->timebase_cb = NULL;
  233. client->control->timebase_arg = NULL;
  234. }
  235. /* on ResetSyncClient request */
  236. int
  237. jack_transport_client_reset_sync (jack_engine_t *engine,
  238. jack_client_id_t client_id)
  239. {
  240. int ret;
  241. jack_client_internal_t *client;
  242. jack_lock_graph (engine);
  243. client = jack_client_internal_by_id (engine, client_id);
  244. if (client && (client->control->is_slowsync)) {
  245. jack_sync_poll_exit(engine, client);
  246. ret = 0;
  247. } else
  248. ret = EINVAL;
  249. jack_unlock_graph (engine);
  250. return ret;
  251. }
  252. /* on SetSyncClient request */
  253. int
  254. jack_transport_client_set_sync (jack_engine_t *engine,
  255. jack_client_id_t client_id)
  256. {
  257. int ret;
  258. jack_client_internal_t *client;
  259. /* The process cycle runs with this lock. */
  260. jack_lock_graph (engine);
  261. client = jack_client_internal_by_id (engine, client_id);
  262. if (client) {
  263. if (!client->control->is_slowsync) {
  264. client->control->is_slowsync = 1;
  265. engine->control->sync_clients++;
  266. }
  267. /* force poll of the new slow-sync client */
  268. jack_sync_poll_new (engine, client);
  269. ret = 0;
  270. } else
  271. ret = EINVAL;
  272. jack_unlock_graph (engine);
  273. return ret;
  274. }
  275. /* at the end of every process cycle
  276. *
  277. * Determines the transport parameters for the following cycle.
  278. * precondition: caller holds the graph lock.
  279. */
  280. void
  281. jack_transport_cycle_end (jack_engine_t *engine)
  282. {
  283. jack_control_t *ectl = engine->control;
  284. transport_command_t cmd; /* latest transport command */
  285. /* update timebase, if needed */
  286. if ((engine->timebase_client == NULL) &&
  287. (ectl->transport_state == JackTransportRolling)) {
  288. ectl->pending_time.frame =
  289. ectl->current_time.frame + ectl->buffer_size;
  290. }
  291. /* See if an asynchronous position request arrived during the
  292. * last cycle. The request_time could change during the
  293. * guarded copy. If so, we'll handle it now, but mistake it
  294. * for a new request in the following cycle. That may cause
  295. * an extra sync poll cycle, but should work. */
  296. if (ectl->request_time.unique_1 != ectl->prev_request) {
  297. ectl->prev_request = ectl->request_time.unique_1;
  298. jack_transport_copy_position(&ectl->request_time,
  299. &ectl->pending_time);
  300. VERBOSE (engine, "new transport position: %lu, id=0x%llx\n",
  301. ectl->pending_time.frame, ectl->pending_time.unique_1);
  302. ectl->new_pos = 1;
  303. } else
  304. ectl->new_pos = 0;
  305. /* Promote pending_time to current_time. Maintain the usecs
  306. * and frame_rate values, clients may not set them. */
  307. ectl->pending_time.usecs = ectl->current_time.usecs;
  308. ectl->pending_time.frame_rate = ectl->current_time.frame_rate;
  309. ectl->current_time = ectl->pending_time;
  310. /* check sync results from previous cycle */
  311. if (ectl->transport_state == JackTransportStarting) {
  312. if ((ectl->sync_remain == 0) ||
  313. (jack_sync_timeout(engine))) {
  314. ectl->transport_state = JackTransportRolling;
  315. VERBOSE (engine, "transport Rolling, %8.6f sec"
  316. " left for poll\n",
  317. (double) (ectl->sync_time_left / 1000000.0));
  318. }
  319. }
  320. /* Handle any new transport command from the last cycle. */
  321. cmd = ectl->transport_cmd;
  322. if (cmd != ectl->previous_cmd) {
  323. ectl->previous_cmd = cmd;
  324. VERBOSE (engine, "transport command: %s\n",
  325. (cmd == TransportCommandStart? "START": "STOP"));
  326. } else
  327. cmd = TransportCommandNone;
  328. /* state transition switch */
  329. switch (ectl->transport_state) {
  330. case JackTransportStopped:
  331. if (cmd == TransportCommandStart) {
  332. if (ectl->sync_clients) {
  333. ectl->transport_state = JackTransportStarting;
  334. jack_sync_poll_start(engine);
  335. } else {
  336. ectl->transport_state = JackTransportRolling;
  337. VERBOSE (engine, "transport Rolling\n");
  338. }
  339. }
  340. break;
  341. case JackTransportStarting:
  342. if (cmd == TransportCommandStop) {
  343. ectl->transport_state = JackTransportStopped;
  344. VERBOSE (engine, "transport Stopped\n");
  345. if (ectl->sync_remain)
  346. jack_sync_poll_stop(engine);
  347. } else if (ectl->new_pos) {
  348. if (ectl->sync_clients) {
  349. ectl->transport_state = JackTransportStarting;
  350. jack_sync_poll_start(engine);
  351. } else {
  352. ectl->transport_state = JackTransportRolling;
  353. VERBOSE (engine, "transport Rolling\n");
  354. }
  355. }
  356. break;
  357. case JackTransportRolling:
  358. if (cmd == TransportCommandStop) {
  359. ectl->transport_state = JackTransportStopped;
  360. VERBOSE (engine, "transport Stopped\n");
  361. if (ectl->sync_remain)
  362. jack_sync_poll_stop(engine);
  363. } else if (ectl->new_pos) {
  364. if (ectl->sync_clients) {
  365. ectl->transport_state = JackTransportStarting;
  366. jack_sync_poll_start(engine);
  367. }
  368. }
  369. break;
  370. default:
  371. jack_error ("invalid JACK transport state: %d",
  372. ectl->transport_state);
  373. }
  374. return;
  375. }
  376. /* driver callback at start of cycle */
  377. void
  378. jack_transport_cycle_start (jack_engine_t *engine, jack_time_t time)
  379. {
  380. engine->control->current_time.usecs = time;
  381. }
  382. /* on SetSyncTimeout request */
  383. int
  384. jack_transport_set_sync_timeout (jack_engine_t *engine,
  385. jack_time_t usecs)
  386. {
  387. engine->control->sync_timeout = usecs;
  388. VERBOSE (engine, "new sync timeout: %8.6f secs\n",
  389. (double) (usecs / 1000000.0));
  390. return 0;
  391. }