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 
26 #include "ardour/butler.h"
27 #include "ardour/disk_reader.h"
28 #include "ardour/route.h"
29 #include "ardour/session.h"
30 #include "ardour/session_event.h"
31 #include "ardour/session_route.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
adjust_playback_buffering()46 Session::adjust_playback_buffering ()
47 {
48 	if (!loading()) {
49 		request_stop (false, false);
50 	}
51 	SessionEvent *ev = new SessionEvent (SessionEvent::AdjustPlaybackBuffering, SessionEvent::Add, SessionEvent::Immediate, 0, 0, 0.0);
52 	queue_event (ev);
53 }
54 
55 void
adjust_capture_buffering()56 Session::adjust_capture_buffering ()
57 {
58 	if (!loading()) {
59 		request_stop (false, false);
60 	}
61 	SessionEvent *ev = new SessionEvent (SessionEvent::AdjustCaptureBuffering, SessionEvent::Add, SessionEvent::Immediate, 0, 0, 0.0);
62 	queue_event (ev);
63 }
64 
65 void
schedule_playback_buffering_adjustment()66 Session::schedule_playback_buffering_adjustment ()
67 {
68 	add_post_transport_work (PostTransportAdjustPlaybackBuffering);
69 	_butler->schedule_transport_work ();
70 }
71 
72 void
schedule_capture_buffering_adjustment()73 Session::schedule_capture_buffering_adjustment ()
74 {
75 	add_post_transport_work (PostTransportAdjustCaptureBuffering);
76 	_butler->schedule_transport_work ();
77 }
78 
79 void
request_overwrite_buffer(boost::shared_ptr<Track> t,OverwriteReason why)80 Session::request_overwrite_buffer (boost::shared_ptr<Track> t, OverwriteReason why)
81 {
82 	assert (t);
83 	SessionEvent *ev = new SessionEvent (SessionEvent::Overwrite, SessionEvent::Replace, SessionEvent::Immediate, 0, 0, 0.0);
84 	ev->set_track (t);
85 	ev->overwrite = why;
86 	queue_event (ev);
87 }
88 
89 void
overwrite_some_buffers(boost::shared_ptr<Route> r,OverwriteReason why)90 Session::overwrite_some_buffers (boost::shared_ptr<Route> r, OverwriteReason why)
91 {
92 	/* this is called from the process thread while handling queued
93 	 * SessionEvents. Therefore neither playback sample or read offsets in
94 	 * tracks will change while we "queue" them all for an upcoming
95 	 * overwrite.
96 	 */
97 
98 	if (actively_recording()) {
99 		return;
100 	}
101 
102 
103 	if (r) {
104 		boost::shared_ptr<Track> t = boost::dynamic_pointer_cast<Track> (r);
105 		assert (t);
106 		t->set_pending_overwrite (why);
107 
108 	} else {
109 
110 		foreach_track (&Track::set_pending_overwrite, why);
111 	}
112 
113 	if (why == LoopChanged) {
114 		add_post_transport_work (PostTransportWork (PostTransportOverWrite|PostTransportLoopChanged));
115 	} else {
116 		add_post_transport_work (PostTransportOverWrite);
117 	}
118 
119 	_butler->schedule_transport_work ();
120 }
121 
122 uint32_t
playback_load()123 Session::playback_load ()
124 {
125 	return (uint32_t) g_atomic_int_get (&_playback_load);
126 }
127 
128 uint32_t
capture_load()129 Session::capture_load ()
130 {
131 	return (uint32_t) g_atomic_int_get (&_capture_load);
132 }
133