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.
21 #include "libardour-config.h"
24 #include <jack/weakjack.h> // so that we can test for new functions at runtime
26 #include "pbd/compose.h"
27 #include "pbd/error.h"
28 #include "pbd/failed_constructor.h"
30 #include "ardour/audioengine.h"
31 #include "ardour/debug.h"
32 #include "ardour/port.h"
33 #include "ardour/port_engine.h"
38 using namespace ARDOUR;
41 PBD::Signal2<void,boost::shared_ptr<Port>, boost::shared_ptr<Port> > Port::PostDisconnect;
42 PBD::Signal0<void> Port::PortDrop;
44 bool Port::_connecting_blocked = false;
45 pframes_t Port::_global_port_buffer_offset = 0;
46 pframes_t Port::_cycle_nframes = 0;
48 /* a handy define to shorten what would otherwise be a needlessly verbose
51 #define port_engine AudioEngine::instance()->port_engine()
53 /** @param n Port short name */
54 Port::Port (std::string const & n, DataType t, PortFlags f)
55 : _port_buffer_offset (0)
58 , _last_monitor (false)
60 _private_playback_latency.min = 0;
61 _private_playback_latency.max = 0;
62 _private_capture_latency.min = 0;
63 _private_capture_latency.max = 0;
65 /* Unfortunately we have to pass the DataType into this constructor so that
66 we can create the right kind of JACK port; aside from this we'll use the
67 virtual function type () to establish type.
70 assert (_name.find_first_of (':') == std::string::npos);
72 if (!port_engine.connected()) {
73 throw failed_constructor ();
76 if ((_jack_port = port_engine.register_port (_name, t.to_port_type (), _flags)) == 0) {
77 cerr << "Failed to register JACK port \"" << _name << "\", reason is unknown from here\n";
78 throw failed_constructor ();
81 PortDrop.connect_same_thread (drop_connection, boost::bind (&Port::drop, this));
84 /** Port destructor */
94 port_engine.unregister_port (port_handle);
99 /** @return true if this port is connected to anything */
101 Port::connected () const
103 return (port_engine.connected (_port_handle) != 0);
107 Port::disconnect_all ()
109 jack_port_disconnect (_engine->jack(), _jack_port);
110 _connections.clear ();
112 /* a cheaper, less hacky way to do boost::shared_from_this() ...
114 boost::shared_ptr<Port> pself = _engine->get_port_by_name (name());
115 PostDisconnect (pself, boost::shared_ptr<Port>()); // emit signal
120 /** @param o Port name
121 * @return true if this port is connected to o, otherwise false.
124 Port::connected_to (std::string const & o) const
126 if (!_engine->connected()) {
127 /* in some senses, this answer isn't the right one all the time,
128 because we know about our connections and will re-establish
129 them when we reconnect to JACK.
134 return jack_port_connected_to (_jack_port,
135 _engine->make_port_name_non_relative(o).c_str ());
138 /** @param o Filled in with port full names of ports that we are connected to */
140 Port::get_connections (std::vector<std::string> & c) const
144 if (_engine->connected()) {
145 const char** jc = jack_port_get_connections (_jack_port);
147 for (int i = 0; jc[i]; ++i) {
164 Port::connect (std::string const & other)
166 std::string const other_shrt = _engine->make_port_name_non_relative (other);
167 std::string const this_shrt = _engine->make_port_name_non_relative (_name);
171 if (_connecting_blocked) {
175 if (sends_output ()) {
176 r = jack_connect (_engine->jack (), this_shrt.c_str (), other_shrt.c_str ());
178 r = jack_connect (_engine->jack (), other_shrt.c_str (), this_shrt.c_str());
182 _connections.insert (other);
189 Port::disconnect (std::string const & other)
191 std::string const other_fullname = _engine->make_port_name_non_relative (other);
192 std::string const this_fullname = _engine->make_port_name_non_relative (_name);
196 if (sends_output ()) {
197 r = _engine->disconnect (this_fullname, other_fullname);
199 r = _engine->disconnect (other_fullname, this_fullname);
203 _connections.erase (other);
206 /* a cheaper, less hacky way to do boost::shared_from_this() ...
208 boost::shared_ptr<Port> pself = _engine->get_port_by_name (name());
209 boost::shared_ptr<Port> pother = _engine->get_port_by_name (other);
211 if (pself && pother) {
212 /* Disconnecting from another Ardour port: need to allow
213 a check on whether this may affect anything that we
216 PostDisconnect (pself, pother); // emit signal
224 Port::connected_to (Port* o) const
226 return connected_to (o->name ());
230 Port::connect (Port* o)
232 return connect (o->name ());
236 Port::disconnect (Port* o)
238 return disconnect (o->name ());
242 Port::request_input_monitoring (bool yn)
244 port_eengine.request_input_monitoring (_port_handle, yn);
248 Port::ensure_input_monitoring (bool yn)
250 port_engine.ensure_input_monitoring (_port_handle, yn);
254 Port::monitoring_input () const
257 return port_engine.monitoring_input (_port_handle);
263 _last_monitor = false;
267 Port::cycle_start (pframes_t)
269 _port_buffer_offset = 0;
273 Port::increment_port_buffer_offset (pframes_t nframes)
275 _port_buffer_offset += nframes;
279 Port::set_public_latency_range (jack_latency_range_t& range, bool playback) const
281 /* this sets the visible latency that the rest of JACK sees. because we do latency
282 compensation, all (most) of our visible port latency values are identical.
285 if (!jack_port_set_latency_range) {
289 DEBUG_TRACE (DEBUG::Latency,
290 string_compose ("SET PORT %1 %4 PUBLIC latency now [%2 - %3]\n",
291 name(), range.min, range.max,
292 (playback ? "PLAYBACK" : "CAPTURE")));;
294 jack_port_set_latency_range (_jack_port,
295 (playback ? JackPlaybackLatency : JackCaptureLatency),
300 Port::set_private_latency_range (jack_latency_range_t& range, bool playback)
303 _private_playback_latency = range;
304 DEBUG_TRACE (DEBUG::Latency, string_compose (
305 "SET PORT %1 playback PRIVATE latency now [%2 - %3]\n",
307 _private_playback_latency.min,
308 _private_playback_latency.max));
310 _private_capture_latency = range;
311 DEBUG_TRACE (DEBUG::Latency, string_compose (
312 "SET PORT %1 capture PRIVATE latency now [%2 - %3]\n",
314 _private_capture_latency.min,
315 _private_capture_latency.max));
318 /* push to public (JACK) location so that everyone else can see it */
320 set_public_latency_range (range, playback);
323 const jack_latency_range_t&
324 Port::private_latency_range (bool playback) const
327 DEBUG_TRACE (DEBUG::Latency, string_compose (
328 "GET PORT %1 playback PRIVATE latency now [%2 - %3]\n",
330 _private_playback_latency.min,
331 _private_playback_latency.max));
332 return _private_playback_latency;
334 DEBUG_TRACE (DEBUG::Latency, string_compose (
335 "GET PORT %1 capture PRIVATE latency now [%2 - %3]\n",
337 _private_playback_latency.min,
338 _private_playback_latency.max));
339 return _private_capture_latency;
344 Port::public_latency_range (bool /*playback*/) const
346 jack_latency_range_t r;
348 jack_port_get_latency_range (_jack_port,
349 sends_output() ? JackPlaybackLatency : JackCaptureLatency,
351 DEBUG_TRACE (DEBUG::Latency, string_compose (
352 "GET PORT %1: %4 PUBLIC latency range %2 .. %3\n",
353 name(), r.min, r.max,
354 sends_output() ? "PLAYBACK" : "CAPTURE"));
359 Port::get_connected_latency_range (jack_latency_range_t& range, bool playback) const
361 if (!jack_port_get_latency_range) {
365 vector<string> connections;
366 jack_client_t* jack = _engine->jack();
371 PBD::warning << _("get_connected_latency_range() called while disconnected from JACK") << endmsg;
375 get_connections (connections);
377 if (!connections.empty()) {
379 range.min = ~((jack_nframes_t) 0);
382 DEBUG_TRACE (DEBUG::Latency, string_compose ("%1: %2 connections to check for latency range\n", name(), connections.size()));
384 for (vector<string>::const_iterator c = connections.begin();
385 c != connections.end(); ++c) {
387 jack_latency_range_t lr;
389 if (!AudioEngine::instance()->port_is_mine (*c)) {
391 /* port belongs to some other JACK client, use
392 * JACK to lookup its latency information.
395 jack_port_t* remote_port = jack_port_by_name (_engine->jack(), (*c).c_str());
398 jack_port_get_latency_range (
400 (playback ? JackPlaybackLatency : JackCaptureLatency),
403 DEBUG_TRACE (DEBUG::Latency, string_compose (
404 "\t%1 <-> %2 : latter has latency range %3 .. %4\n",
405 name(), *c, lr.min, lr.max));
407 range.min = min (range.min, lr.min);
408 range.max = max (range.max, lr.max);
413 /* port belongs to this instance of ardour,
414 so look up its latency information
415 internally, because our published/public
416 values already contain our plugin
417 latency compensation.
420 boost::shared_ptr<Port> remote_port = AudioEngine::instance()->get_port_by_name (*c);
422 lr = remote_port->private_latency_range ((playback ? JackPlaybackLatency : JackCaptureLatency));
423 DEBUG_TRACE (DEBUG::Latency, string_compose (
424 "\t%1 <-LOCAL-> %2 : latter has latency range %3 .. %4\n",
425 name(), *c, lr.min, lr.max));
427 range.min = min (range.min, lr.min);
428 range.max = max (range.max, lr.max);
434 DEBUG_TRACE (DEBUG::Latency, string_compose ("%1: not connected to anything\n", name()));
439 DEBUG_TRACE (DEBUG::Latency, string_compose ("%1: final connected latency range [ %2 .. %3 ] \n", name(), range.min, range.max));
445 jack_client_t* jack = _engine->jack();
451 _jack_port = jack_port_register (jack, _name.c_str(), type().to_jack_type(), _flags, 0);
453 if (_jack_port == 0) {
454 PBD::error << string_compose (_("could not reregister %1"), _name) << endmsg;
467 /* caller must hold process lock; intended to be used only after reestablish() */
469 for (std::set<string>::iterator i = _connections.begin(); i != _connections.end(); ++i) {
478 /** @param n Short port name (no JACK client name) */
480 Port::set_name (std::string const & n)
486 int const r = jack_port_set_name (_jack_port, n.c_str());
489 _engine->port_renamed (_name, n);
498 Port::physically_connected () const
500 const char** jc = jack_port_get_connections (_jack_port);
503 for (int i = 0; jc[i]; ++i) {
505 jack_port_t* port = jack_port_by_name (_engine->jack(), jc[i]);
507 if (port && (jack_port_flags (port) & JackPortIsPhysical)) {