2 * Copyright (C) 2019 Robin Gareus <robin@gareus.org>
3 * Copyright (C) 2019 Paul Davis <paul@linuxaudiosystems.com>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #include <boost/none.hpp>
24 #include "pbd/error.h"
26 #include "pbd/pthread_utils.h"
27 #include "pbd/stacktrace.h"
29 #include "ardour/debug.h"
30 #include "ardour/session.h"
31 #include "ardour/transport_fsm.h"
33 using namespace ARDOUR;
36 Pool* TransportFSM::Event::pool = 0;
39 TransportFSM::Event::init_pool ()
41 pool = new Pool (X_("Events"), sizeof (Event), 128);
45 TransportFSM::Event::operator new (size_t)
51 TransportFSM::Event::operator delete (void *ptr, size_t /*size*/)
53 return pool->release (ptr);
56 TransportFSM::TransportFSM (TransportAPI& tapi)
57 : _last_locate (Locate)
58 , _last_stop (StopTransport)
68 _motion_state = Stopped;
69 _butler_state = NotWaitingForButler;
70 _last_locate.target = max_samplepos;
74 TransportFSM::process_events ()
78 while (!queued_events.empty()) {
80 MotionState oms = _motion_state;
81 ButlerState obs = _butler_state;
83 Event* ev = &queued_events.front();
86 /* must remove from the queued_events list now, because
87 * process_event() may defer the event. This will lead to
88 * insertion into the deferred_events list, and its not possible
89 * with intrusive lists to be present in two lists at once
90 * (without additional hooks).
93 queued_events.pop_front ();
95 if (process_event (*ev, false, deferred)) { /* event processed successfully */
97 if (oms != _motion_state || obs != _butler_state) {
99 /* state changed, so now check deferred events
100 * to see if they can be processed now
103 if (!deferred_events.empty() ){
104 DEBUG_TRACE (DEBUG::TFSMEvents, string_compose ("processing %1 deferred events\n", deferred_events.size()));
106 for (EventList::iterator e = deferred_events.begin(); e != deferred_events.end(); ) {
107 Event* deferred_ev = &(*e);
109 if (process_event (*e, true, deferred2)) { /* event processed, remove from deferred */
110 e = deferred_events.erase (e);
128 /* This is the transition table from the original boost::msm
129 * implementation of this FSM. It is more easily readable and
130 * consultable. Please keep it updated as the FSM changes.
132 * Here's a hint about how to read each line of this table:
134 * "if the current state is Start and event Event arrives, new state is Next and we execute Action()"
138 * "if the current state is Start and event Event arrives, new state is Next and we execute Action() ***IF*** Guard() returns true"
140 * This new implementation, however, does not use metaprogramming to achieve all this,
141 * but just uses a large-ish switch() block.
146 Start Event Next Action Guard
147 +----------------------+----------------+------------------+---------------------+---------------------------------+
148 a_row < Stopped, start_transport, Rolling, &T::start_playback >,
149 _row < Stopped, stop_transport, Stopped >,
150 a_row < Stopped, locate, WaitingForLocate, &T::start_locate_while_stopped >,
151 g_row < WaitingForLocate, locate_done, Stopped, &T::should_not_roll_after_locate >,
152 _row < Rolling, butler_done, Rolling >,
153 _row < Rolling, start_transport, Rolling >,
154 a_row < Rolling, stop_transport, DeclickToStop, &T::stop_playback >,
155 a_row < DeclickToStop, declick_done, Stopped, >,
156 a_row < DeclickToStop, stop_transport, DeclickToStop >,
157 a_row < Rolling, locate, DeclickToLocate, &T::start_declick_for_locate >,
158 a_row < DeclickToLocate, declick_done, WaitingForLocate, &T::start_locate_after_declick >,
159 row < WaitingForLocate, locate_done, Rolling, &T::roll_after_locate, &T::should_roll_after_locate >,
160 a_row < NotWaitingForButler, butler_required, WaitingForButler, &T::schedule_butler_for_transport_work >,
161 a_row < WaitingForButler, butler_required, WaitingForButler, &T::schedule_butler_for_transport_work >,
162 _row < WaitingForButler, butler_done, NotWaitingForButler >,
163 a_row < WaitingForLocate, locate, WaitingForLocate, &T::interrupt_locate >,
164 a_row < DeclickToLocate, locate, DeclickToLocate, &T::interrupt_locate >,
168 #define defer(start_state,ev) boost::msm::front::Row<start_state, ev, start_state, boost::msm::front::Defer, boost::msm::front::none >
170 defer (DeclickToLocate, start_transport),
171 defer (DeclickToLocate, stop_transport),
172 defer (DeclickToStop, start_transport),
173 defer (WaitingForLocate, start_transport),
174 defer (WaitingForLocate, stop_transport)
180 TransportFSM::current_state () const
183 s << enum_2_string (_motion_state) << '/' << enum_2_string (_butler_state);
188 TransportFSM::bad_transition (Event const & ev)
190 error << "bad transition, current state = " << current_state() << " event = " << enum_2_string (ev.type) << endmsg;
191 std::cerr << "bad transition, current state = " << current_state() << " event = " << enum_2_string (ev.type) << std::endl;
192 PBD::stacktrace (std::cerr, 30);
196 TransportFSM::process_event (Event& ev, bool already_deferred, bool& deferred)
198 DEBUG_TRACE (DEBUG::TFSMEvents, string_compose ("process %1\n", enum_2_string (ev.type)));
205 switch (_motion_state) {
207 transition (Rolling);
212 case DeclickToLocate:
213 case WaitingForLocate:
214 if (!already_deferred) {
220 if (!already_deferred) {
226 bad_transition (ev); return false;
232 switch (_motion_state) {
234 transition (DeclickToStop);
239 case DeclickToLocate:
240 case WaitingForLocate:
241 if (!already_deferred) {
247 /* already doing it */
250 bad_transition (ev); return false;
256 DEBUG_TRACE (DEBUG::TFSMEvents, string_compose ("locate, ltd = %1 flush = %2 target = %3 loop %4 force %5\n",
257 enum_2_string (ev.ltd),
262 switch (_motion_state) {
264 transition (WaitingForLocate);
265 start_locate_while_stopped (ev);
268 if (ev.for_loop_end) {
269 /* we will finish the locate synchronously, so
270 * that after returning from
271 * ::locate_for_loop() we will already have
272 * received (and re-entrantly handled)
273 * LocateDone and returned back to Rolling.
275 * This happens because we only need to do a
276 * realtime locate and continue rolling. No
277 * disk I/O is required - the loop is
278 * automically present in buffers already.
280 * Note that ev.ltd is ignored and
281 * assumed to be true because we're looping.
283 transition (WaitingForLocate);
284 locate_for_loop (ev);
286 transition (DeclickToLocate);
287 start_declick_for_locate (ev);
290 case WaitingForLocate:
291 case DeclickToLocate:
292 interrupt_locate (ev);
295 bad_transition (ev); return false;
300 switch (_motion_state) {
301 case WaitingForLocate:
302 if (should_not_roll_after_locate()) {
303 transition (Stopped);
304 /* already stopped, nothing to do */
306 transition (Rolling);
307 roll_after_locate ();
311 bad_transition (ev); return false;
316 switch (_motion_state) {
317 case DeclickToLocate:
318 transition (WaitingForLocate);
319 start_locate_after_declick ();
322 transition (Stopped);
323 /* transport already stopped */
326 bad_transition (ev); return false;
331 switch (_butler_state) {
332 case NotWaitingForButler:
333 transition (WaitingForButler);
334 schedule_butler_for_transport_work ();
336 case WaitingForButler:
337 schedule_butler_for_transport_work ();
340 bad_transition (ev); return false;
345 switch (_butler_state) {
346 case WaitingForButler:
347 transition (NotWaitingForButler);
350 bad_transition (ev); return false;
358 /* transition actions */
361 TransportFSM::start_playback ()
363 DEBUG_TRACE (DEBUG::TFSMEvents, "start_playback\n");
365 _last_locate.target = max_samplepos;
366 current_roll_after_locate_status = boost::none;
368 api->start_transport();
372 TransportFSM::stop_playback ()
374 DEBUG_TRACE (DEBUG::TFSMEvents, "stop_playback\n");
376 _last_locate.target = max_samplepos;
377 current_roll_after_locate_status = boost::none;
379 api->stop_transport (_last_stop.abort, _last_stop.clear_state);
383 TransportFSM::set_roll_after (bool with_roll) const
385 current_roll_after_locate_status = with_roll;
389 TransportFSM::start_declick_for_locate (Event const & l)
391 assert (l.type == Locate);
392 DEBUG_TRACE (DEBUG::TFSMEvents, string_compose ("start_declick_for_locate, crals %1 ltd %2 speed %3 sral %4\n", (bool) current_roll_after_locate_status,
393 enum_2_string (l.ltd), api->speed(), api->should_roll_after_locate()));
396 if (!current_roll_after_locate_status) {
397 set_roll_after (compute_should_roll (l.ltd));
399 api->stop_transport (false, false);
403 TransportFSM::start_locate_while_stopped (Event const & l) const
405 assert (l.type == Locate);
406 DEBUG_TRACE (DEBUG::TFSMEvents, "start_locate_while_stopped\n");
408 set_roll_after (compute_should_roll (l.ltd));
410 api->locate (l.target, current_roll_after_locate_status.get(), l.with_flush, l.for_loop_end, l.force);
414 TransportFSM::compute_should_roll (LocateTransportDisposition ltd) const
421 case DoTheRightThing:
425 return api->should_roll_after_locate ();
434 TransportFSM::locate_for_loop (Event const & l)
436 assert (l.type == Locate);
437 DEBUG_TRACE (DEBUG::TFSMEvents, string_compose ("locate_for_loop, wl = %1\n", l.for_loop_end));
439 const bool should_roll = compute_should_roll (l.ltd);
442 api->locate (l.target, should_roll, l.with_flush, l.for_loop_end, l.force);
446 TransportFSM::start_locate_after_declick () const
448 DEBUG_TRACE (DEBUG::TFSMEvents, string_compose ("start_locate_after_declick, have crals ? %1 roll will be %2\n", (bool) current_roll_after_locate_status,
449 current_roll_after_locate_status ? current_roll_after_locate_status.get() : compute_should_roll (_last_locate.ltd)));
451 const bool roll = current_roll_after_locate_status ? current_roll_after_locate_status.get() : compute_should_roll (_last_locate.ltd);
452 api->locate (_last_locate.target, roll, _last_locate.with_flush, _last_locate.for_loop_end, _last_locate.force);
456 TransportFSM::interrupt_locate (Event const & l) const
458 assert (l.type == Locate);
459 DEBUG_TRACE (DEBUG::TFSMEvents, string_compose ("interrupt to %1 versus %2\n", l.target, _last_locate.target));
461 /* Because of snapping (e.g. of mouse position) we could be
462 * interrupting an existing locate to the same position. If we go ahead
463 * with this, the code in Session::do_locate() will notice that it's a
464 * repeat position, will do nothing, will queue a "locate_done" event
465 * that will arrive in the next process cycle. But this event may be
466 * processed before the original (real) locate has completed in the
467 * butler thread, and processing it may transition us back to Rolling
468 * before some (or even all) tracks are actually ready.
470 * So, we must avoid this from happening, and this seems like the
474 if (l.target == _last_locate.target && !l.force) {
477 /* maintain original "with-roll" choice of initial locate, even though
478 * we are interrupting the locate to start a new one.
480 api->locate (l.target, false, l.with_flush, l.for_loop_end, l.force);
484 TransportFSM::schedule_butler_for_transport_work () const
486 api->schedule_butler_for_transport_work ();
490 TransportFSM::should_roll_after_locate () const
494 if (current_roll_after_locate_status) {
495 roll = current_roll_after_locate_status.get();
496 current_roll_after_locate_status = boost::none; // used it
498 roll = api->should_roll_after_locate ();
501 DEBUG_TRACE (DEBUG::TFSMEvents, string_compose ("should_roll_after_locate() ? %1\n", roll));
506 TransportFSM::roll_after_locate () const
508 DEBUG_TRACE (DEBUG::TFSMEvents, string_compose ("rolling after locate, was for_loop ? %1\n", _last_locate.for_loop_end));
509 current_roll_after_locate_status = boost::none;
510 api->start_transport ();
514 TransportFSM::defer (Event& ev)
516 DEBUG_TRACE (DEBUG::TFSMEvents, string_compose ("Defer %1 during %2\n", enum_2_string (ev.type), current_state()));
517 deferred_events.push_back (ev);
521 TransportFSM::transition (MotionState ms)
523 const MotionState old = _motion_state;
525 DEBUG_TRACE (DEBUG::TFSMState, string_compose ("Leave %1, enter %2\n", enum_2_string (old), current_state()));
529 TransportFSM::transition (ButlerState bs)
531 const ButlerState old = _butler_state;
533 DEBUG_TRACE (DEBUG::TFSMState, string_compose ("Leave %1, enter %2\n", enum_2_string (old), current_state()));
537 TransportFSM::enqueue (Event* ev)
539 DEBUG_TRACE (DEBUG::TFSMState, string_compose ("queue tfsm event %1\n", enum_2_string (ev->type)));
540 queued_events.push_back (*ev);