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.

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