continue the saga of Rewrite The XML Node So That It Can Be Used To Set Another Objec...
[ardour.git] / libs / ardour / io_processor.cc
1 /*
2     Copyright (C) 2001 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 <fstream>
21 #include <algorithm>
22 #include <string>
23 #include <cerrno>
24 #include <unistd.h>
25 #include <sstream>
26
27
28 #include "pbd/xml++.h"
29 #include "pbd/enumwriter.h"
30
31 #include "ardour/io_processor.h"
32 #include "ardour/session.h"
33 #include "ardour/utils.h"
34 #include "ardour/send.h"
35 #include "ardour/port_insert.h"
36 #include "ardour/plugin_insert.h"
37 #include "ardour/io.h"
38 #include "ardour/route.h"
39
40 #include "i18n.h"
41
42 using namespace std;
43 using namespace ARDOUR;
44 using namespace PBD;
45
46 /* create an IOProcessor that proxies to a new IO object */
47
48 IOProcessor::IOProcessor (Session& s, bool with_input, bool with_output,
49                           const string& proc_name, const string io_name, DataType dtype)
50         : Processor(s, proc_name)
51 {
52         /* these are true in this constructor whether we actually create the associated
53            IO objects or not.
54         */
55
56         _own_input = true;
57         _own_output = true;
58
59         if (with_input) {
60                 _input.reset (new IO(s, io_name.empty() ? proc_name : io_name, IO::Input, dtype));
61         }
62
63         if (with_output) {
64                 _output.reset (new IO(s, io_name.empty() ? proc_name : io_name, IO::Output, dtype));
65         }
66 }
67
68 /* create an IOProcessor that proxies to an existing IO object */
69
70 IOProcessor::IOProcessor (Session& s, boost::shared_ptr<IO> in, boost::shared_ptr<IO> out,
71                           const string& proc_name, DataType /*dtype*/)
72         : Processor(s, proc_name)
73         , _input (in)
74         , _output (out)
75 {
76         if (in) {
77                 _own_input = false;
78         } else {
79                 _own_input = true;
80         }
81
82         if (out) {
83                 _own_output = false;
84         } else {
85                 _own_output = true;
86         }
87 }
88
89 IOProcessor::~IOProcessor ()
90 {
91 }
92
93 void
94 IOProcessor::set_input (boost::shared_ptr<IO> io)
95 {
96         /* CALLER MUST HOLD PROCESS LOCK */
97
98         _input = io;
99         _own_input = false;
100 }
101
102 void
103 IOProcessor::set_output (boost::shared_ptr<IO> io)
104 {
105         /* CALLER MUST HOLD PROCESS LOCK */
106
107         _output = io;
108         _own_output = false;
109 }
110
111 XMLNode&
112 IOProcessor::state (bool full_state)
113 {
114         XMLNode& node (Processor::state (full_state));
115
116         if (_own_input) {
117                 node.add_property ("own-input", "yes");
118                 if (_input) {
119                         XMLNode& i (_input->state (full_state));
120                         // i.name() = X_("output");
121                         node.add_child_nocopy (i);
122                 }
123         } else {
124                 node.add_property ("own-input", "no");
125                 if (_input) {
126                         node.add_property ("input", _input->name());
127                 }
128         }
129
130         if (_own_output) {
131                 node.add_property ("own-output", "yes");
132                 if (_output) {
133                         XMLNode& o (_output->state (full_state));
134                         node.add_child_nocopy (o);
135                 }
136         } else {
137                 node.add_property ("own-output", "no");
138                 if (_output) {
139                         node.add_property ("output", _output->name());
140                 }
141         }
142
143         return node;
144 }
145
146 int
147 IOProcessor::set_state (const XMLNode& node, int version)
148 {
149         if (version < 3000) {
150                 return set_state_2X (node, version);
151         }
152
153         const XMLProperty *prop;
154         const XMLNode *io_node = 0;
155
156         Processor::set_state(node, version);
157
158
159         if ((prop = node.property ("own-input")) != 0) {
160                 _own_input = string_is_affirmative (prop->value());
161         }
162
163         if ((prop = node.property ("own-output")) != 0) {
164                 _own_output = string_is_affirmative (prop->value());
165         }
166
167         /* don't attempt to set state for a proxied IO that we don't own */
168
169         XMLNodeList nlist = node.children();
170         XMLNodeIterator niter;
171         const string instr = enum_2_string (IO::Input);
172         const string outstr = enum_2_string (IO::Output);
173         
174         if (_own_input) {
175                 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
176                         const XMLProperty* prop;
177                         if ((prop = (*niter)->property ("name")) != 0) {
178                                 if (_name == prop->value()) {
179                                         if ((prop = (*niter)->property ("direction")) != 0) {
180                                                 if (prop->value() == instr) {
181                                                         io_node = (*niter);
182                                                         break;
183                                                 }
184                                         }
185                                 }
186                         }
187                 }
188                 
189                 if (io_node) {
190                         _input->set_state(*io_node, version);
191                         
192                         // legacy sessions: use IO name
193                         if ((prop = node.property ("name")) == 0) {
194                                 set_name (_input->name());
195                         }
196                         
197                 } else {
198                         /* no input, which is OK */
199                 }
200                 
201         }
202         
203         if (_own_output) {
204                 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
205                         if ((*niter)->name() == "IO") {
206                                 const XMLProperty* prop;
207                                 if ((prop = (*niter)->property ("name")) != 0) {
208                                         if (_name == prop->value()) {
209                                                 if ((prop = (*niter)->property ("direction")) != 0) {
210                                                         if (prop->value() == outstr) {
211                                                                 io_node = (*niter);
212                                                                 break;
213                                                         }
214                                                 }
215                                         }
216                                 }
217                         }
218                 }
219                 
220                 if (io_node) {
221                         _output->set_state(*io_node, version);
222                         
223                         // legacy sessions: use IO name
224                         if ((prop = node.property ("name")) == 0) {
225                                 set_name (_output->name());
226                         }
227                 } else {
228                         /* no output, which is OK */
229                 }
230         }
231
232         return 0;
233 }
234
235 int
236 IOProcessor::set_state_2X (const XMLNode& node, int version)
237 {
238         _own_input = _own_output = true;
239
240         Processor::set_state_2X (node, version);
241
242         return 0;
243 }
244
245 void
246 IOProcessor::silence (framecnt_t nframes)
247 {
248         if (_own_output && _output) {
249                 _output->silence (nframes);
250         }
251 }
252
253 void
254 IOProcessor::increment_port_buffer_offset (pframes_t offset)
255 {
256         if (_own_output && _output) {
257                 _output->increment_port_buffer_offset (offset);
258         }
259 }
260
261 ChanCount
262 IOProcessor::natural_output_streams() const
263 {
264         return _output ? _output->n_ports() : ChanCount::ZERO;
265 }
266
267 ChanCount
268 IOProcessor::natural_input_streams () const
269 {
270         return _input ? _input->n_ports() : ChanCount::ZERO;
271 }
272
273 bool
274 IOProcessor::set_name (const std::string& name)
275 {
276         bool ret = SessionObject::set_name (name);
277
278         if (ret && _own_input && _input) {
279                 ret = _input->set_name (name);
280         }
281
282         if (ret && _own_output && _output) {
283                 ret = _output->set_name (name);
284         }
285
286         return ret;
287 }
288
289 bool
290 IOProcessor::feeds (boost::shared_ptr<Route> other) const
291 {
292         return _output && _output->connected_to (other->input());
293 }
294
295 void
296 IOProcessor::disconnect ()
297 {
298         if (_input) {
299                 _input->disconnect (this);
300         }
301
302         if (_output) {
303                 _output->disconnect (this);
304         }
305 }
306
307 /** Set up the XML description of a send so that we will not
308  *  reset its name or bitslot during ::set_state()
309  *  @param state XML send state.
310  *  @param session Session.
311  */
312 void
313 IOProcessor::prepare_for_reset (XMLNode &state, const std::string& name)
314 {
315         state.add_property ("ignore-bitslot", "1");
316         state.add_property ("ignore-name", "1");
317
318         XMLNode* io_node = state.child (IO::state_node_name.c_str());
319
320         if (io_node) {
321                 IO::prepare_for_reset (*io_node, name);
322         }
323 }