fix crash when copy'ing latent plugins
[ardour.git] / libs / ardour / unknown_processor.cc
1 /*
2     Copyright (C) 2010 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "ardour/audio_buffer.h"
21 #include "ardour/unknown_processor.h"
22
23 #include "pbd/i18n.h"
24
25 using namespace std;
26 using namespace ARDOUR;
27
28 UnknownProcessor::UnknownProcessor (Session& s, XMLNode const & state)
29         : Processor (s, "")
30         , _state (state)
31         , have_ioconfig (false)
32         , saved_input (0)
33         , saved_output (0)
34 {
35         XMLProperty const * prop = state.property (X_("name"));
36         if (prop) {
37                 set_name (prop->value ());
38                 _display_to_user = true;
39         }
40
41         int have_io = 0;
42         XMLNodeList kids = state.children ();
43         for (XMLNodeIterator i = kids.begin(); i != kids.end(); ++i) {
44                 if ((*i)->name() == X_("ConfiguredInput")) {
45                         have_io |= 1;
46                         saved_input = new ChanCount(**i);
47                 }
48                 if ((*i)->name() == X_("ConfiguredOutput")) {
49                         have_io |= 2;
50                         saved_output = new ChanCount(**i);
51                 }
52         }
53         have_ioconfig = (have_io == 3);
54 }
55
56 UnknownProcessor::~UnknownProcessor () {
57         delete saved_input;;
58         delete saved_output;
59 }
60
61 XMLNode &
62 UnknownProcessor::state (bool)
63 {
64         return *(new XMLNode (_state));
65 }
66
67 bool
68 UnknownProcessor::can_support_io_configuration (const ChanCount &in, ChanCount & out) {
69         if (have_ioconfig && in == *saved_input) {
70                 out = *saved_output;
71                 return true;
72         } else if (!have_ioconfig) {
73                 /* pass for old sessions.
74                  *
75                  * session load assumes processor config succeeds.
76                  * if initial configuration fails, processors downstream
77                  * remain unconfigured and at least the panner will assert/segfault.
78                  *
79                  * This may still result in impossible setup, however
80                  * Route::configure_processors_unlocked() ignores configure_io() return value
81                  * in the inner loop and configures all available processors.
82                  *
83                  * It can still lead to segfaults IFF the track has no inputs and this is a
84                  * generator (processor_max_streams will be zero).
85                  */
86                 PBD::warning << _("Using plugin-stub with unknown i/o configuration for: ") << name() << endmsg;
87 #if 0
88                 /* No output channels are fine (or should be, there may be edge-cases with e.g sends).
89                  *
90                  * Discussion needed.
91                  *
92                  * This is the safer option (no audio output, no possible damage)
93                  * and the way to go in the long run.
94                  * An even better solution is to disable the route if there are missing plugins
95                  * and/or impossible configurations.
96                  *
97                  * Currently no output channels results in awkward GUI route display and also
98                  * breaks semantics in mixbus (which assumes that the route has channels required
99                  * for the always present mixer-strip plugin).
100                  */
101                 out = ChanCount ();
102 #else
103                 out = in;
104 #endif
105                 return true;
106         } else {
107                 PBD::error << _("Using plugin-stub with mismatching i/o configuration for: ") << name() << endmsg;
108                 out = in;
109         }
110         return true;
111 }
112
113 void
114 UnknownProcessor::run (BufferSet& bufs, framepos_t /*start_frame*/, framepos_t /*end_frame*/, double /*speed*/, pframes_t nframes, bool)
115 {
116         if (!have_ioconfig) {
117                 return;
118         }
119         // silence excess output buffers
120         for (uint32_t i = saved_input->n_audio(); i < saved_output->n_audio(); ++i) {
121                 bufs.get_audio (i).silence (nframes);
122         }
123 }