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.

534 lines
14KB

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