to avoid deadlock in JACK1 scenarios, do not invoke AudioEngine::update_latencies...
[ardour.git] / libs / ardour / port_insert.cc
1 /*
2  * Copyright (C) 2000-2017 Paul Davis <paul@linuxaudiosystems.com>
3  * Copyright (C) 2007-2012 David Robillard <d@drobilla.net>
4  * Copyright (C) 2009-2012 Carl Hetherington <carl@carlh.net>
5  * Copyright (C) 2013-2019 Robin Gareus <robin@gareus.org>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #include <string>
23
24 #include "pbd/xml++.h"
25
26 #include "ardour/audio_port.h"
27 #include "ardour/audioengine.h"
28 #include "ardour/delivery.h"
29 #include "ardour/io.h"
30 #include "ardour/mtdm.h"
31 #include "ardour/port_insert.h"
32 #include "ardour/session.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 string
42 PortInsert::name_and_id_new_insert (Session& s, uint32_t& bitslot)
43 {
44         bitslot = s.next_insert_id ();
45         return string_compose (_("insert %1"), bitslot+ 1);
46 }
47
48 PortInsert::PortInsert (Session& s, boost::shared_ptr<Pannable> pannable, boost::shared_ptr<MuteMaster> mm)
49         : IOProcessor (s, true, true, name_and_id_new_insert (s, _bitslot), "", DataType::AUDIO, true)
50         , _out (new Delivery (s, _output, pannable, mm, _name, Delivery::Insert))
51 {
52         _mtdm = 0;
53         _latency_detect = false;
54         _latency_flush_samples = 0;
55         _measured_latency = 0;
56 }
57
58 PortInsert::~PortInsert ()
59 {
60         _session.unmark_insert_id (_bitslot);
61         delete _mtdm;
62 }
63
64 void
65 PortInsert::set_pre_fader (bool p)
66 {
67         Processor::set_pre_fader (p);
68         _out->set_pre_fader (p);
69 }
70
71 void
72 PortInsert::start_latency_detection ()
73 {
74         delete _mtdm;
75         _mtdm = new MTDM (_session.sample_rate());
76         _latency_flush_samples = 0;
77         _latency_detect = true;
78         _measured_latency = 0;
79 }
80
81 void
82 PortInsert::stop_latency_detection ()
83 {
84         _latency_flush_samples = effective_latency() + _session.engine().samples_per_cycle();
85         _latency_detect = false;
86 }
87
88 void
89 PortInsert::set_measured_latency (samplecnt_t n)
90 {
91         _measured_latency = n;
92 }
93
94 samplecnt_t
95 PortInsert::latency() const
96 {
97         /* because we deliver and collect within the same cycle,
98            all I/O is necessarily delayed by at least samples_per_cycle().
99
100            if the return port for insert has its own latency, we
101            need to take that into account too.
102         */
103
104         if (_measured_latency == 0) {
105                 return _session.engine().samples_per_cycle() + _input->latency();
106         } else {
107                 return _measured_latency;
108         }
109 }
110
111 void
112 PortInsert::run (BufferSet& bufs, samplepos_t start_sample, samplepos_t end_sample, double speed, pframes_t nframes, bool)
113 {
114         if (_output->n_ports().n_total() == 0) {
115                 return;
116         }
117
118         if (_latency_detect) {
119
120                 if (_input->n_ports().n_audio() != 0) {
121
122                         AudioBuffer& outbuf (_output->ports().nth_audio_port(0)->get_audio_buffer (nframes));
123                         Sample* in = _input->ports().nth_audio_port(0)->get_audio_buffer (nframes).data();
124                         Sample* out = outbuf.data();
125
126                         _mtdm->process (nframes, in, out);
127
128                         outbuf.set_written (true);
129                 }
130
131                 return;
132
133         } else if (_latency_flush_samples) {
134
135                 /* wait for the entire input buffer to drain before picking up input again so that we can't
136                  * hear the remnants of whatever MTDM pumped into the pipeline.
137                  */
138
139                 silence (nframes, start_sample);
140
141                 if (_latency_flush_samples > nframes) {
142                         _latency_flush_samples -= nframes;
143                 } else {
144                         _latency_flush_samples = 0;
145                 }
146
147                 return;
148         }
149
150         if (!_active && !_pending_active) {
151                 /* deliver silence */
152                 silence (nframes, start_sample);
153                 goto out;
154         }
155
156         _out->run (bufs, start_sample, end_sample, speed, nframes, true);
157         _input->collect_input (bufs, nframes, ChanCount::ZERO);
158
159 out:
160         _active = _pending_active;
161 }
162
163 XMLNode&
164 PortInsert::state ()
165 {
166         XMLNode& node = IOProcessor::state ();
167         node.set_property ("type", "port");
168         node.set_property ("bitslot", _bitslot);
169         node.set_property ("latency", _measured_latency);
170         node.set_property ("block-size", _session.get_block_size());
171
172         return node;
173 }
174
175 int
176 PortInsert::set_state (const XMLNode& node, int version)
177 {
178         XMLNodeList nlist = node.children();
179         XMLNodeIterator niter;
180         XMLPropertyList plist;
181
182         const XMLNode* insert_node = &node;
183
184         // legacy sessions: search for child Redirect node
185         for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
186                 if ((*niter)->name() == "Redirect") {
187                         insert_node = *niter;
188                         break;
189                 }
190         }
191
192         IOProcessor::set_state (*insert_node, version);
193
194         std::string type_str;
195         if (!node.get_property ("type", type_str)) {
196                 error << _("XML node describing port insert is missing the `type' field") << endmsg;
197                 return -1;
198         }
199
200         if (type_str != "port") {
201                 error << _("non-port insert XML used for port plugin insert") << endmsg;
202                 return -1;
203         }
204
205         uint32_t blocksize = 0;
206         node.get_property ("block-size", blocksize);
207
208         //if the jack period is the same as when the value was saved, we can recall our latency..
209         if ( (_session.get_block_size() == blocksize) ) {
210                 node.get_property ("latency", _measured_latency);
211         }
212
213         if (!node.property ("ignore-bitslot")) {
214                 uint32_t bitslot;
215                 if (node.get_property ("bitslot", bitslot)) {
216                         _session.unmark_insert_id (_bitslot);
217                         _bitslot = bitslot;
218                         _session.mark_insert_id (_bitslot);
219                 } else {
220                         _bitslot = _session.next_insert_id();
221                 }
222         }
223
224         return 0;
225 }
226
227 ARDOUR::samplecnt_t
228 PortInsert::signal_latency() const
229 {
230         /* because we deliver and collect within the same cycle,
231          * all I/O is necessarily delayed by at least samples_per_cycle().
232          *
233          * if the return port for insert has its own latency, we
234          * need to take that into account too.
235          */
236
237         if (_measured_latency == 0) {
238                 return _session.engine().samples_per_cycle()
239                        + _input->connected_latency (false);
240                        + _output->connected_latency (true);
241         } else {
242                 return _measured_latency;
243         }
244 }
245
246 /** Caller must hold process lock */
247 bool
248 PortInsert::configure_io (ChanCount in, ChanCount out)
249 {
250 #ifndef PLATFORM_WINDOWS
251         assert (!AudioEngine::instance()->process_lock().trylock());
252 #endif
253
254         /* for an insert, processor input corresponds to IO output, and vice versa */
255
256         if (_input->ensure_io (in, false, this) != 0) {
257                 return false;
258         }
259
260         if (_output->ensure_io (out, false, this) != 0) {
261                 return false;
262         }
263
264         return Processor::configure_io (in, out);
265 }
266
267 bool
268 PortInsert::can_support_io_configuration (const ChanCount& in, ChanCount& out)
269 {
270         out = in;
271         return true;
272 }
273
274 bool
275 PortInsert::set_name (const std::string& name)
276 {
277         bool ret = Processor::set_name (name);
278
279         ret = (ret && _input->set_name (name) && _output->set_name (name));
280
281         return ret;
282 }
283
284 void
285 PortInsert::activate ()
286 {
287         IOProcessor::activate ();
288
289         _out->activate ();
290 }
291
292 void
293 PortInsert::deactivate ()
294 {
295         IOProcessor::deactivate ();
296
297         _out->deactivate ();
298 }