check that we're still connected to JACK when using jack_port_get_connections()
[ardour2.git] / libs / ardour / port.cc
blob2a89560b77d7c3d9563eefa3563f4b2923d7e763
1 /*
2 Copyright (C) 2009 Paul Davis
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.
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.
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.
20 #ifdef WAF_BUILD
21 #include "libardour-config.h"
22 #endif
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>
31 #include "i18n.h"
33 using namespace std;
34 using namespace ARDOUR;
36 AudioEngine* Port::_engine = 0;
37 pframes_t Port::_buffer_size = 0;
38 bool Port::_connecting_blocked = false;
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)
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.
52 assert (_name.find_first_of (':') == std::string::npos);
54 if (!_engine->connected()) {
55 throw failed_constructor ();
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 ();
64 /** Port destructor */
65 Port::~Port ()
67 if (_engine->jack ()) {
68 jack_port_unregister (_engine->jack (), _jack_port);
72 /** @return true if this port is connected to anything */
73 bool
74 Port::connected () const
76 return (jack_port_connected (_jack_port) != 0);
79 int
80 Port::disconnect_all ()
82 jack_port_disconnect (_engine->jack(), _jack_port);
83 _connections.clear ();
85 return 0;
88 /** @param o Port name
89 * @return true if this port is connected to o, otherwise false.
91 bool
92 Port::connected_to (std::string const & o) const
94 if (!_engine->connected()) {
95 /* in some senses, this answer isn't the right one all the time,
96 because we know about our connections and will re-establish
97 them when we reconnect to JACK.
99 return false;
102 return jack_port_connected_to (_jack_port, _engine->make_port_name_non_relative(o).c_str ());
105 /** @param o Filled in with port full names of ports that we are connected to */
107 Port::get_connections (std::vector<std::string> & c) const
109 int n = 0;
111 if (_engine->connected()) {
112 const char** jc = jack_port_get_connections (_jack_port);
113 if (jc) {
114 for (int i = 0; jc[i]; ++i) {
115 c.push_back (jc[i]);
116 ++n;
119 jack_free (jc);
123 return n;
127 Port::connect (std::string const & other)
129 std::string const other_shrt = _engine->make_port_name_non_relative (other);
130 std::string const this_shrt = _engine->make_port_name_non_relative (_name);
132 int r = 0;
134 if (_connecting_blocked) {
135 return r;
138 if (sends_output ()) {
139 r = jack_connect (_engine->jack (), this_shrt.c_str (), other_shrt.c_str ());
140 } else {
141 r = jack_connect (_engine->jack (), other_shrt.c_str (), this_shrt.c_str());
144 if (r == 0) {
145 _connections.insert (other);
148 return r;
152 Port::disconnect (std::string const & other)
154 std::string const other_shrt = _engine->make_port_name_non_relative (other);
155 std::string const this_shrt = _engine->make_port_name_non_relative (_name);
157 int r = 0;
159 if (sends_output ()) {
160 r = jack_disconnect (_engine->jack (), this_shrt.c_str (), other_shrt.c_str ());
161 } else {
162 r = jack_disconnect (_engine->jack (), other_shrt.c_str (), this_shrt.c_str ());
165 if (r == 0) {
166 _connections.erase (other);
169 return r;
173 bool
174 Port::connected_to (Port* o) const
176 return connected_to (o->name ());
180 Port::connect (Port* o)
182 return connect (o->name ());
186 Port::disconnect (Port* o)
188 return disconnect (o->name ());
191 void
192 Port::set_engine (AudioEngine* e)
194 _engine = e;
197 void
198 Port::ensure_monitor_input (bool yn)
200 jack_port_ensure_monitor (_jack_port, yn);
203 bool
204 Port::monitoring_input () const
206 return jack_port_monitoring_input (_jack_port);
209 void
210 Port::reset ()
212 _last_monitor = false;
214 // XXX
215 // _metering = 0;
216 // reset_meters ();
219 void
220 Port::recompute_total_latency () const
222 #ifdef HAVE_JACK_RECOMPUTE_LATENCY
223 jack_client_t* jack = _engine->jack();
225 if (!jack) {
226 return;
229 jack_recompute_total_latency (jack, _jack_port);
230 #endif
233 framecnt_t
234 Port::total_latency () const
236 jack_client_t* jack = _engine->jack();
238 if (!jack) {
239 return 0;
242 return jack_port_get_total_latency (jack, _jack_port);
246 Port::reestablish ()
248 jack_client_t* jack = _engine->jack();
250 if (!jack) {
251 return -1;
254 cerr << "RE-REGISTER: " << _name.c_str() << endl;
255 _jack_port = jack_port_register (jack, _name.c_str(), type().to_jack_type(), _flags, 0);
257 if (_jack_port == 0) {
258 PBD::error << string_compose (_("could not reregister %1"), _name) << endmsg;
259 return -1;
262 reset ();
264 return 0;
269 Port::reconnect ()
271 /* caller must hold process lock; intended to be used only after reestablish() */
273 for (std::set<string>::iterator i = _connections.begin(); i != _connections.end(); ++i) {
274 if (connect (*i)) {
275 return -1;
279 return 0;
282 /** @param n Short port name (no JACK client name) */
284 Port::set_name (std::string const & n)
286 if (n == _name) {
287 return 0;
290 int const r = jack_port_set_name (_jack_port, n.c_str());
292 if (r == 0) {
293 _name = n;
296 return r;
299 void
300 Port::request_monitor_input (bool yn)
302 jack_port_request_monitor (_jack_port, yn);
305 void
306 Port::set_latency (framecnt_t n)
308 jack_port_set_latency (_jack_port, n);
311 bool
312 Port::physically_connected () const
314 const char** jc = jack_port_get_connections (_jack_port);
316 if (jc) {
317 for (int i = 0; jc[i]; ++i) {
319 jack_port_t* port = jack_port_by_name (_engine->jack(), jc[i]);
321 if (port && (jack_port_flags (port) & JackPortIsPhysical)) {
322 jack_free (jc);
323 return true;
327 jack_free (jc);
330 return false;