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