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