add finite state machine to control/manage transport state
[ardour.git] / libs / ardour / session_butler.cc
1 /*
2  * Copyright (C) 1999-2019 Paul Davis <paul@linuxaudiosystems.com>
3  * Copyright (C) 2005-2006 Taybin Rutkin <taybin@taybin.com>
4  * Copyright (C) 2006-2014 David Robillard <d@drobilla.net>
5  * Copyright (C) 2006 Jesse Chappell <jesse@essej.net>
6  * Copyright (C) 2010-2012 Carl Hetherington <carl@carlh.net>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22
23 #include "pbd/error.h"
24 #include "pbd/pthread_utils.h"
25 #include "pbd/stacktrace.h"
26
27 #include "ardour/butler.h"
28 #include "ardour/disk_reader.h"
29 #include "ardour/route.h"
30 #include "ardour/session.h"
31 #include "ardour/session_event.h"
32 #include "ardour/track.h"
33 #include "ardour/types.h"
34
35 #include "pbd/i18n.h"
36
37 using namespace std;
38 using namespace ARDOUR;
39 using namespace PBD;
40
41 /*---------------------------------------------------------------------------
42  BUTLER THREAD
43  ---------------------------------------------------------------------------*/
44
45 void
46 Session::adjust_playback_buffering ()
47 {
48         request_stop (false, false);
49         SessionEvent *ev = new SessionEvent (SessionEvent::AdjustPlaybackBuffering, SessionEvent::Add, SessionEvent::Immediate, 0, 0, 0.0);
50         queue_event (ev);
51 }
52
53 void
54 Session::adjust_capture_buffering ()
55 {
56         request_stop (false, false);
57         SessionEvent *ev = new SessionEvent (SessionEvent::AdjustCaptureBuffering, SessionEvent::Add, SessionEvent::Immediate, 0, 0, 0.0);
58         queue_event (ev);
59 }
60
61 void
62 Session::schedule_playback_buffering_adjustment ()
63 {
64         add_post_transport_work (PostTransportAdjustPlaybackBuffering);
65         DiskReader::inc_no_disk_output ();
66         _butler->schedule_transport_work ();
67 }
68
69 void
70 Session::schedule_capture_buffering_adjustment ()
71 {
72         add_post_transport_work (PostTransportAdjustCaptureBuffering);
73         _butler->schedule_transport_work ();
74 }
75
76 void
77 Session::request_overwrite_buffer (boost::shared_ptr<Route> r)
78 {
79         boost::shared_ptr<Track> t = boost::dynamic_pointer_cast<Track> (r);
80         if (!t) {
81                 return;
82         }
83
84         SessionEvent *ev = new SessionEvent (SessionEvent::Overwrite, SessionEvent::Add, SessionEvent::Immediate, 0, 0, 0.0);
85         ev->set_ptr (t.get());
86         queue_event (ev);
87 }
88
89 /** Process thread. */
90 void
91 Session::overwrite_some_buffers (Track* t)
92 {
93         if (actively_recording()) {
94                 return;
95         }
96
97         if (t) {
98
99                 t->set_pending_overwrite ();
100
101         } else {
102
103                 boost::shared_ptr<RouteList> rl = routes.reader();
104                 for (RouteList::iterator i = rl->begin(); i != rl->end(); ++i) {
105                         boost::shared_ptr<Track> tr = boost::dynamic_pointer_cast<Track> (*i);
106                         if (tr) {
107                                 tr->set_pending_overwrite ();
108                         }
109                 }
110         }
111
112         add_post_transport_work (PostTransportOverWrite);
113
114         _butler->schedule_transport_work ();
115 }
116
117 uint32_t
118 Session::playback_load ()
119 {
120         return (uint32_t) g_atomic_int_get (&_playback_load);
121 }
122
123 uint32_t
124 Session::capture_load ()
125 {
126         return (uint32_t) g_atomic_int_get (&_capture_load);
127 }