1c3c99f94c2fc52796c65e9371e25fcfeae5543a
[ardour.git] / libs / ardour / port.cc
1 /*
2     Copyright (C) 2009 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 #ifdef WAF_BUILD
21 #include "libardour-config.h"
22 #endif
23
24 #include "ardour/port.h"
25 #include "ardour/audioengine.h"
26 #include "pbd/failed_constructor.h"
27 #include "pbd/error.h"
28 #include "pbd/compose.h"
29 #include <stdexcept>
30
31 #include "i18n.h"
32
33 using namespace std;
34 using namespace ARDOUR;
35
36 AudioEngine* Port::_engine = 0;
37 nframes_t Port::_buffer_size = 0;
38 bool Port::_connecting_blocked = false;
39
40 /** @param n Port short name */
41 Port::Port (std::string const & n, DataType t, Flags f)
42         : _last_monitor (false)
43         , _name (n)
44         , _flags (f)
45 {
46
47         /* Unfortunately we have to pass the DataType into this constructor so that we can
48            create the right kind of JACK port; aside from this we'll use the virtual function type ()
49            to establish type.
50         */
51
52         assert (_name.find_first_of (':') == std::string::npos);
53
54         if (!_engine->connected()) {
55                 throw failed_constructor ();
56         }
57
58         if ((_jack_port = jack_port_register (_engine->jack (), _name.c_str (), t.to_jack_type (), _flags, 0)) == 0) {
59                 cerr << "Failed to register JACK port, reason is unknown from here\n";
60                 throw failed_constructor ();
61         }
62 }
63
64 /** Port destructor */
65 Port::~Port ()
66 {
67         if (_engine->jack ()) {
68                 jack_port_unregister (_engine->jack (), _jack_port);
69         }
70 }
71
72 /** @return true if this port is connected to anything */
73 bool
74 Port::connected () const
75 {
76         return (jack_port_connected (_jack_port) != 0);
77 }
78
79 int
80 Port::disconnect_all ()
81 {
82         jack_port_disconnect (_engine->jack(), _jack_port);
83         _connections.clear ();
84
85         return 0;
86 }
87
88 /** @param o Port name
89  * @return true if this port is connected to o, otherwise false.
90  */
91 bool
92 Port::connected_to (std::string const & o) const
93 {
94         return jack_port_connected_to (_jack_port, _engine->make_port_name_non_relative(o).c_str ());
95 }
96
97 /** @param o Filled in with port full names of ports that we are connected to */
98 int
99 Port::get_connections (std::vector<std::string> & c) const
100 {
101         int n = 0;
102
103         const char** jc = jack_port_get_connections (_jack_port);
104         if (jc) {
105                 for (int i = 0; jc[i]; ++i) {
106                         c.push_back (jc[i]);
107                         ++n;
108                 }
109
110                 jack_free (jc);
111         }
112
113         return n;
114 }
115
116 int
117 Port::connect (std::string const & other)
118 {
119         /* caller must hold process lock */
120
121         std::string const other_shrt = _engine->make_port_name_non_relative (other);
122         std::string const this_shrt = _engine->make_port_name_non_relative (_name);
123
124         int r = 0;
125
126         if (_connecting_blocked) {
127                 return r;
128         }
129
130         if (sends_output ()) {
131                 r = jack_connect (_engine->jack (), this_shrt.c_str (), other_shrt.c_str ());
132         } else {
133                 r = jack_connect (_engine->jack (), other_shrt.c_str (), this_shrt.c_str());
134         }
135
136         if (r == 0) {
137                 _connections.insert (other);
138         }
139
140         return r;
141 }
142
143 int
144 Port::disconnect (std::string const & other)
145 {
146         /* caller must hold process lock */
147
148         std::string const other_shrt = _engine->make_port_name_non_relative (other);
149         std::string const this_shrt = _engine->make_port_name_non_relative (_name);
150
151         int r = 0;
152
153         if (sends_output ()) {
154                 r = jack_disconnect (_engine->jack (), this_shrt.c_str (), other_shrt.c_str ());
155         } else {
156                 r = jack_disconnect (_engine->jack (), other_shrt.c_str (), this_shrt.c_str ());
157         }
158
159         if (r == 0) {
160                 _connections.erase (other);
161         }
162
163         return r;
164 }
165
166
167 bool
168 Port::connected_to (Port* o) const
169 {
170         return connected_to (o->name ());
171 }
172
173 int
174 Port::connect (Port* o)
175 {
176         return connect (o->name ());
177 }
178
179 int
180 Port::disconnect (Port* o)
181 {
182         return disconnect (o->name ());
183 }
184
185 void
186 Port::set_engine (AudioEngine* e)
187 {
188         _engine = e;
189 }
190
191 void
192 Port::ensure_monitor_input (bool yn)
193 {
194         jack_port_ensure_monitor (_jack_port, yn);
195 }
196
197 bool
198 Port::monitoring_input () const
199 {
200         return jack_port_monitoring_input (_jack_port);
201 }
202
203 void
204 Port::reset ()
205 {
206         _last_monitor = false;
207
208         // XXX
209         // _metering = 0;
210         // reset_meters ();
211 }
212
213 void
214 Port::recompute_total_latency () const
215 {
216 #ifdef HAVE_JACK_RECOMPUTE_LATENCY
217         jack_client_t* jack = _engine->jack();
218
219         if (!jack) {
220                 return;
221         }
222
223         jack_recompute_total_latency (jack, _jack_port);
224 #endif
225 }
226
227 nframes_t
228 Port::total_latency () const
229 {
230         jack_client_t* jack = _engine->jack();
231
232         if (!jack) {
233                 return 0;
234         }
235
236         return jack_port_get_total_latency (jack, _jack_port);
237 }
238
239 int
240 Port::reestablish ()
241 {
242         jack_client_t* jack = _engine->jack();
243
244         if (!jack) {
245                 return -1;
246         }
247
248         cerr << "RE-REGISTER: " << _name.c_str() << endl;
249         _jack_port = jack_port_register (jack, _name.c_str(), type().to_jack_type(), _flags, 0);
250
251         if (_jack_port == 0) {
252                 PBD::error << string_compose (_("could not reregister %1"), _name) << endmsg;
253                 return -1;
254         }
255
256         reset ();
257
258         return 0;
259 }
260
261
262 int
263 Port::reconnect ()
264 {
265         /* caller must hold process lock; intended to be used only after reestablish() */
266
267         for (std::set<string>::iterator i = _connections.begin(); i != _connections.end(); ++i) {
268                 if (connect (*i)) {
269                         return -1;
270                 }
271         }
272
273         return 0;
274 }
275
276 /** @param n Short port name (no JACK client name) */
277 int
278 Port::set_name (std::string const & n)
279 {
280         if (n == _name) {
281                 return 0;
282         }
283
284         int const r = jack_port_set_name (_jack_port, n.c_str());
285
286         if (r == 0) {
287                 _name = n;
288         }
289
290         return r;
291 }
292
293 void
294 Port::request_monitor_input (bool yn)
295 {
296         jack_port_request_monitor (_jack_port, yn);
297 }
298
299 void
300 Port::set_latency (nframes_t n)
301 {
302         jack_port_set_latency (_jack_port, n);
303 }
304
305 bool
306 Port::physically_connected () const
307 {
308         const char** jc = jack_port_get_connections (_jack_port);
309
310         if (jc) {
311                 for (int i = 0; jc[i]; ++i) {
312
313                         jack_port_t* port = jack_port_by_name (_engine->jack(), jc[i]);
314                         
315                         if (port && (jack_port_flags (port) & JackPortIsPhysical)) {
316                                 jack_free (jc);
317                                 return true;
318                         }
319                 }
320                 
321                 jack_free (jc);
322         }
323
324         return false;
325 }
326