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::start_declick_for_stop >,
155 a_row < DeclickToStop, declick_done, Stopped, &T::stop_playback >,
156 a_row < Rolling, locate, DeclickToLocate, &T::start_declick_for_locate >,
157 a_row < DeclickToLocate, declick_done, WaitingForLocate, &T::start_locate_after_declick >,
158 row < WaitingForLocate, locate_done, Rolling, &T::roll_after_locate, &T::should_roll_after_locate >,
159 a_row < NotWaitingForButler, butler_required, WaitingForButler, &T::schedule_butler_for_transport_work >,
160 a_row < WaitingForButler, butler_required, WaitingForButler, &T::schedule_butler_for_transport_work >,
161 _row < WaitingForButler, butler_done, NotWaitingForButler >,
162 a_row < WaitingForLocate, locate, WaitingForLocate, &T::interrupt_locate >,
163 a_row < DeclickToLocate, locate, DeclickToLocate, &T::interrupt_locate >,
167 #define defer(start_state,ev) boost::msm::front::Row<start_state, ev, start_state, boost::msm::front::Defer, boost::msm::front::none >
169 defer (DeclickToLocate, start_transport),
170 defer (DeclickToLocate, stop_transport),
171 defer (DeclickToStop, start_transport),
172 defer (WaitingForLocate, start_transport),
173 defer (WaitingForLocate, stop_transport)
179 TransportFSM::current_state () const
182 s << enum_2_string (_motion_state) << '/' << enum_2_string (_butler_state);
187 TransportFSM::bad_transition (Event const & ev)
189 error << "bad transition, current state = " << current_state() << " event = " << enum_2_string (ev.type) << endmsg;
190 std::cerr << "bad transition, current state = " << current_state() << " event = " << enum_2_string (ev.type) << std::endl;
194 TransportFSM::process_event (Event& ev, bool already_deferred, bool& deferred)
196 DEBUG_TRACE (DEBUG::TFSMEvents, string_compose ("process %1\n", enum_2_string (ev.type)));
203 switch (_motion_state) {
205 transition (Rolling);
210 case DeclickToLocate:
211 case WaitingForLocate:
212 if (!already_deferred) {
218 if (!already_deferred) {
224 bad_transition (ev); return false;
230 switch (_motion_state) {
232 transition (DeclickToStop);
233 start_declick_for_stop (ev);
237 case DeclickToLocate:
238 case WaitingForLocate:
239 if (!already_deferred) {
245 bad_transition (ev); return false;
251 DEBUG_TRACE (DEBUG::TFSMEvents, string_compose ("locate, with roll = %1 flush = %2 target = %3 loop %4 force %5\n",
257 switch (_motion_state) {
259 transition (WaitingForLocate);
260 start_locate_while_stopped (ev);
264 /* no state transitions. Just do a realtime
265 locate and continue rolling. Note that
266 ev.with_roll is ignored and assumed to be
267 true because we're looping.
269 locate_for_loop (ev);
271 transition (DeclickToLocate);
272 start_declick_for_locate (ev);
275 case WaitingForLocate:
276 case DeclickToLocate:
277 interrupt_locate (ev);
280 bad_transition (ev); return false;
285 switch (_motion_state) {
286 case WaitingForLocate:
287 if (should_not_roll_after_locate()) {
288 transition (Stopped);
289 /* already stopped, nothing to do */
291 transition (Rolling);
292 roll_after_locate ();
296 bad_transition (ev); return false;
301 switch (_motion_state) {
302 case DeclickToLocate:
303 transition (WaitingForLocate);
304 start_locate_after_declick ();
307 transition (Stopped);
311 bad_transition (ev); return false;
316 switch (_butler_state) {
317 case NotWaitingForButler:
318 transition (WaitingForButler);
319 schedule_butler_for_transport_work ();
321 case WaitingForButler:
322 schedule_butler_for_transport_work ();
325 bad_transition (ev); return false;
330 switch (_butler_state) {
331 case WaitingForButler:
332 transition (NotWaitingForButler);
335 bad_transition (ev); return false;
343 /* transition actions */
346 TransportFSM::start_playback ()
348 DEBUG_TRACE (DEBUG::TFSMEvents, "start_playback\n");
350 _last_locate.target = max_samplepos;
351 current_roll_after_locate_status = boost::none;
353 api->start_transport();
357 TransportFSM::stop_playback ()
359 DEBUG_TRACE (DEBUG::TFSMEvents, "stop_playback\n");
361 _last_locate.target = max_samplepos;
362 current_roll_after_locate_status = boost::none;
364 api->stop_transport (_last_stop.abort, _last_stop.clear_state);
368 TransportFSM::start_declick_for_stop (Event const & s)
370 assert (s.type == StopTransport);
371 DEBUG_TRACE (DEBUG::TFSMEvents, "start_declick_for_stop\n");
376 TransportFSM::start_declick_for_locate (Event const & l)
378 assert (l.type == Locate);
379 DEBUG_TRACE (DEBUG::TFSMEvents, "start_declick_for_locate\n");
382 if (!current_roll_after_locate_status) {
384 if (api->speed() != 0.) {
385 current_roll_after_locate_status = true;
387 current_roll_after_locate_status = api->should_roll_after_locate();
390 current_roll_after_locate_status = (api->speed() != 0.);
394 _last_stop = Event (StopTransport, false, false);
398 TransportFSM::start_locate_while_stopped (Event const & l) const
400 assert (l.type == Locate);
401 DEBUG_TRACE (DEBUG::TFSMEvents, "start_locate_while_stopped\n");
403 current_roll_after_locate_status = api->should_roll_after_locate();
404 api->locate (l.target, current_roll_after_locate_status.get(), l.with_flush, l.with_loop, l.force);
408 TransportFSM::locate_for_loop (Event const & l)
410 assert (l.type == Locate);
411 DEBUG_TRACE (DEBUG::TFSMEvents, "locate_for_loop\n");
412 current_roll_after_locate_status = l.with_roll;
413 api->locate (l.target, l.with_roll, l.with_flush, l.with_loop, l.force);
417 TransportFSM::start_locate_after_declick () const
419 DEBUG_TRACE (DEBUG::TFSMEvents, "start_locate_after_declick\n");
420 const bool roll = current_roll_after_locate_status ? current_roll_after_locate_status.get() : _last_locate.with_roll;
421 api->locate (_last_locate.target, roll, _last_locate.with_flush, _last_locate.with_loop, _last_locate.force);
425 TransportFSM::interrupt_locate (Event const & l) const
427 assert (l.type == Locate);
428 DEBUG_TRACE (DEBUG::TFSMEvents, string_compose ("interrupt to %1 versus %2\n", l.target, _last_locate.target));
430 /* Because of snapping (e.g. of mouse position) we could be
431 * interrupting an existing locate to the same position. If we go ahead
432 * with this, the code in Session::do_locate() will notice that it's a
433 * repeat position, will do nothing, will queue a "locate_done" event
434 * that will arrive in the next process cycle. But this event may be
435 * processed before the original (real) locate has completed in the
436 * butler thread, and processing it may transition us back to Rolling
437 * before some (or even all) tracks are actually ready.
439 * So, we must avoid this from happening, and this seems like the
443 if (l.target == _last_locate.target && !l.force) {
446 /* maintain original "with-roll" choice of initial locate, even though
447 * we are interrupting the locate to start a new one.
449 api->locate (l.target, false, l.with_flush, l.with_loop, l.force);
453 TransportFSM::schedule_butler_for_transport_work () const
455 api->schedule_butler_for_transport_work ();
459 TransportFSM::should_roll_after_locate () const
463 if (current_roll_after_locate_status) {
464 roll = current_roll_after_locate_status.get();
466 roll = api->should_roll_after_locate ();
469 DEBUG_TRACE (DEBUG::TFSMEvents, string_compose ("should_roll_after_locate() ? %1\n", roll));
474 TransportFSM::roll_after_locate () const
476 DEBUG_TRACE (DEBUG::TFSMEvents, "rolling after locate\n");
477 current_roll_after_locate_status = boost::none;
478 api->start_transport ();
482 TransportFSM::defer (Event& ev)
484 DEBUG_TRACE (DEBUG::TFSMEvents, string_compose ("Defer %1 during %2\n", enum_2_string (ev.type), current_state()));
485 deferred_events.push_back (ev);
489 TransportFSM::transition (MotionState ms)
491 const MotionState old = _motion_state;
493 DEBUG_TRACE (DEBUG::TFSMState, string_compose ("Leave %1, enter %2\n", enum_2_string (old), current_state()));
497 TransportFSM::transition (ButlerState bs)
499 const ButlerState old = _butler_state;
501 DEBUG_TRACE (DEBUG::TFSMState, string_compose ("Leave %1, enter %2\n", enum_2_string (old), current_state()));
505 TransportFSM::enqueue (Event* ev)
507 DEBUG_TRACE (DEBUG::TFSMState, string_compose ("queue tfsm event %1\n", enum_2_string (ev->type)));
508 queued_events.push_back (*ev);