the big Route structure refactor. !!!! THIS WILL ***NOT LOAD*** PRIOR 3.0 or 2.X...
[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 #include <sigc++/bind.h>
28
29 #include "pbd/xml++.h"
30 #include "pbd/enumwriter.h"
31
32 #include "ardour/io_processor.h"
33 #include "ardour/session.h"
34 #include "ardour/utils.h"
35 #include "ardour/send.h"
36 #include "ardour/port_insert.h"
37 #include "ardour/plugin_insert.h"
38 #include "ardour/io.h"
39 #include "ardour/route.h"
40
41 #include "i18n.h"
42
43 using namespace std;
44 using namespace ARDOUR;
45 using namespace PBD;
46
47 /* create an IOProcessor that proxies to a new IO object */
48
49 IOProcessor::IOProcessor (Session& s, bool with_input, bool with_output,
50                           const string& proc_name, const string io_name, DataType dtype)
51         : Processor(s, proc_name)
52 {
53         /* these are true in this constructor whether we actually create the associated
54            IO objects or not.
55         */
56
57         _own_input = true;
58         _own_output = true;
59
60         if (with_input) {
61                 _input.reset (new IO(s, io_name.empty() ? proc_name : io_name, IO::Input, dtype));
62         }
63
64         if (with_output) {
65                 _output.reset (new IO(s, io_name.empty() ? proc_name : io_name, IO::Output, dtype));
66         }
67 }
68
69 /* create an IOProcessor that proxies to an existing IO object */
70
71 IOProcessor::IOProcessor (Session& s, boost::shared_ptr<IO> in, boost::shared_ptr<IO> out, 
72                           const string& proc_name, DataType dtype)
73         : Processor(s, proc_name)
74         , _input (in)
75         , _output (out)
76 {
77         _own_input = false;
78         _own_output = false;
79 }
80
81 IOProcessor::~IOProcessor ()
82 {
83         notify_callbacks ();
84 }
85
86 void
87 IOProcessor::set_input (boost::shared_ptr<IO> io)
88 {
89         /* CALLER MUST HOLD PROCESS LOCK */
90
91         _input = io;
92         _own_input = false;
93 }
94
95 void
96 IOProcessor::set_output (boost::shared_ptr<IO> io)
97 {
98         /* CALLER MUST HOLD PROCESS LOCK */
99
100         _output = io;
101         _own_output = false;
102 }
103
104 XMLNode&
105 IOProcessor::state (bool full_state)
106 {
107         XMLNode& node (Processor::state (full_state));
108         
109         if (_own_input) {
110                 XMLNode& i (_input->state (full_state));
111                 // i.name() = X_("output");
112                 node.add_child_nocopy (i);
113                 node.add_property ("own-input", "yes");
114         } else {
115                 node.add_property ("own-input", "no");
116                 if (_input) {
117                         node.add_property ("input", _input->name());
118                 }
119         }
120         
121         if (_own_output) {
122                 XMLNode& o (_output->state (full_state));
123                 // o.name() = X_("output");
124                 node.add_child_nocopy (o);
125                 node.add_property ("own-output", "yes");
126         } else {
127                 node.add_property ("own-output", "no");
128                 if (_output) {
129                         node.add_property ("output", _output->name());
130                 }
131         }
132
133         return node;
134 }
135
136 int
137 IOProcessor::set_state (const XMLNode& node)
138 {
139         const XMLProperty *prop;
140         const XMLNode *io_node = 0;
141
142         Processor::set_state(node);
143
144         if ((prop = node.property ("own-input")) != 0) {
145                 _own_input = (prop->value() == "yes");
146         }
147
148         if ((prop = node.property ("own-output")) != 0) {
149                 _own_output = (prop->value() == "yes");
150         }
151
152         cerr << _name << " own input = " << _own_input << " output = " << _own_output << endl;
153         
154         /* don't attempt to set state for a proxied IO that we don't own */
155
156         XMLNodeList nlist = node.children();
157         XMLNodeIterator niter;
158         
159         if (_own_input) {
160                 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
161                         if ((*niter)->name() == "input") {
162                                 io_node = (*niter);
163                                 break;
164                         }
165                 }
166                 
167                 if (io_node) {
168                         _input->set_state(*io_node);
169                         
170                         // legacy sessions: use IO name
171                         if ((prop = node.property ("name")) == 0) {
172                                 set_name (_input->name());
173                         }
174                         
175                 } else {
176                         error << _("XML node describing an IOProcessor is missing an IO node") << endmsg;
177                         return -1;
178                 }
179         }
180         
181         if (_own_output) {
182                 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
183                         if ((*niter)->name() == "output") {
184                                 io_node = (*niter);
185                                 break;
186                         }
187                 }
188                 
189                 if (io_node) {
190                         _output->set_state(*io_node);
191                         
192                         // legacy sessions: use IO name
193                         if ((prop = node.property ("name")) == 0) {
194                                 set_name (_output->name());
195                         }
196                 } 
197         }
198
199         return 0;
200 }
201
202 void
203 IOProcessor::silence (nframes_t nframes)
204 {
205         if (_own_output && _output) {
206                 _output->silence (nframes);
207         }
208 }
209
210 ChanCount
211 IOProcessor::output_streams() const
212 {
213         return _output ? _output->n_ports() : ChanCount::ZERO;
214 }
215
216 ChanCount
217 IOProcessor::input_streams () const
218 {
219         return _input ? _input->n_ports() : ChanCount::ZERO;
220 }
221
222 ChanCount
223 IOProcessor::natural_output_streams() const
224 {
225         return _output ? _output->n_ports() : ChanCount::ZERO;
226 }
227
228 ChanCount
229 IOProcessor::natural_input_streams () const
230 {
231         return _input ? _input->n_ports() : ChanCount::ZERO;
232 }
233
234 bool
235 IOProcessor::set_name (const std::string& name)
236 {
237         bool ret = SessionObject::set_name (name);
238
239         if (ret && _own_input && _input) {
240                 ret = _input->set_name (name);
241         }
242
243         if (ret && _own_output && _output) {
244                 ret = _output->set_name (name);
245         }
246
247         return ret;
248 }