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"
37 using namespace ARDOUR;
40 PBD::Signal2<void,boost::shared_ptr<Port>, boost::shared_ptr<Port> > Port::PostDisconnect;
41 PBD::Signal0<void> Port::PortDrop;
43 AudioEngine* Port::_engine = 0;
44 bool Port::_connecting_blocked = false;
45 pframes_t Port::_global_port_buffer_offset = 0;
46 pframes_t Port::_cycle_nframes = 0;
48 /** @param n Port short name */
49 Port::Port (std::string const & n, DataType t, Flags f)
50 : _port_buffer_offset (0)
53 , _last_monitor (false)
55 _private_playback_latency.min = 0;
56 _private_playback_latency.max = 0;
57 _private_capture_latency.min = 0;
58 _private_capture_latency.max = 0;
60 /* Unfortunately we have to pass the DataType into this constructor so that
61 we can create the right kind of JACK port; aside from this we'll use the
62 virtual function type () to establish type.
65 assert (_name.find_first_of (':') == std::string::npos);
67 if (!_engine->connected()) {
68 throw failed_constructor ();
71 if ((_jack_port = jack_port_register (_engine->jack (), _name.c_str (), t.to_jack_type (), _flags, 0)) == 0) {
72 cerr << "Failed to register JACK port \"" << _name << "\", reason is unknown from here\n";
73 throw failed_constructor ();
76 PortDrop.connect_same_thread (drop_connection, boost::bind (&Port::drop, this));
79 /** Port destructor */
89 if (_engine->jack ()) {
90 jack_port_unregister (_engine->jack (), _jack_port);
96 /** @return true if this port is connected to anything */
98 Port::connected () const
100 return (jack_port_connected (_jack_port) != 0);
104 Port::disconnect_all ()
106 jack_port_disconnect (_engine->jack(), _jack_port);
107 _connections.clear ();
109 /* a cheaper, less hacky way to do boost::shared_from_this() ...
111 boost::shared_ptr<Port> pself = _engine->get_port_by_name (name());
112 PostDisconnect (pself, boost::shared_ptr<Port>()); // emit signal
117 /** @param o Port name
118 * @return true if this port is connected to o, otherwise false.
121 Port::connected_to (std::string const & o) const
123 if (!_engine->connected()) {
124 /* in some senses, this answer isn't the right one all the time,
125 because we know about our connections and will re-establish
126 them when we reconnect to JACK.
131 return jack_port_connected_to (_jack_port,
132 _engine->make_port_name_non_relative(o).c_str ());
135 /** @param o Filled in with port full names of ports that we are connected to */
137 Port::get_connections (std::vector<std::string> & c) const
141 if (_engine->connected()) {
142 const char** jc = jack_port_get_connections (_jack_port);
144 for (int i = 0; jc[i]; ++i) {
161 Port::connect (std::string const & other)
163 std::string const other_shrt = _engine->make_port_name_non_relative (other);
164 std::string const this_shrt = _engine->make_port_name_non_relative (_name);
168 if (_connecting_blocked) {
172 if (sends_output ()) {
173 r = jack_connect (_engine->jack (), this_shrt.c_str (), other_shrt.c_str ());
175 r = jack_connect (_engine->jack (), other_shrt.c_str (), this_shrt.c_str());
179 _connections.insert (other);
186 Port::disconnect (std::string const & other)
188 std::string const other_fullname = _engine->make_port_name_non_relative (other);
189 std::string const this_fullname = _engine->make_port_name_non_relative (_name);
193 if (sends_output ()) {
194 r = jack_disconnect (_engine->jack (), this_fullname.c_str (), other_fullname.c_str ());
196 r = jack_disconnect (_engine->jack (), other_fullname.c_str (), this_fullname.c_str ());
200 _connections.erase (other);
203 /* a cheaper, less hacky way to do boost::shared_from_this() ...
205 boost::shared_ptr<Port> pself = _engine->get_port_by_name (name());
206 boost::shared_ptr<Port> pother = _engine->get_port_by_name (other);
208 if (pself && pother) {
209 /* Disconnecting from another Ardour port: need to allow
210 a check on whether this may affect anything that we
213 PostDisconnect (pself, pother); // emit signal
221 Port::connected_to (Port* o) const
223 return connected_to (o->name ());
227 Port::connect (Port* o)
229 return connect (o->name ());
233 Port::disconnect (Port* o)
235 return disconnect (o->name ());
239 Port::set_engine (AudioEngine* e)
245 Port::ensure_jack_monitors_input (bool yn)
247 jack_port_ensure_monitor (_jack_port, yn);
251 Port::jack_monitoring_input () const
253 return jack_port_monitoring_input (_jack_port);
259 _last_monitor = false;
263 Port::cycle_start (pframes_t)
265 _port_buffer_offset = 0;
269 Port::increment_port_buffer_offset (pframes_t nframes)
271 _port_buffer_offset += nframes;
275 Port::set_public_latency_range (jack_latency_range_t& range, bool playback) const
277 /* this sets the visible latency that the rest of JACK sees. because we do latency
278 compensation, all (most) of our visible port latency values are identical.
281 if (!jack_port_set_latency_range) {
285 DEBUG_TRACE (DEBUG::Latency,
286 string_compose ("SET PORT %1 %4 PUBLIC latency now [%2 - %3]\n",
287 name(), range.min, range.max,
288 (playback ? "PLAYBACK" : "CAPTURE")));;
290 jack_port_set_latency_range (_jack_port,
291 (playback ? JackPlaybackLatency : JackCaptureLatency),
296 Port::set_private_latency_range (jack_latency_range_t& range, bool playback)
299 _private_playback_latency = range;
300 DEBUG_TRACE (DEBUG::Latency, string_compose (
301 "SET PORT %1 playback PRIVATE latency now [%2 - %3]\n",
303 _private_playback_latency.min,
304 _private_playback_latency.max));
306 _private_capture_latency = range;
307 DEBUG_TRACE (DEBUG::Latency, string_compose (
308 "SET PORT %1 capture PRIVATE latency now [%2 - %3]\n",
310 _private_capture_latency.min,
311 _private_capture_latency.max));
314 /* push to public (JACK) location so that everyone else can see it */
316 set_public_latency_range (range, playback);
319 const jack_latency_range_t&
320 Port::private_latency_range (bool playback) const
323 DEBUG_TRACE (DEBUG::Latency, string_compose (
324 "GET PORT %1 playback PRIVATE latency now [%2 - %3]\n",
326 _private_playback_latency.min,
327 _private_playback_latency.max));
328 return _private_playback_latency;
330 DEBUG_TRACE (DEBUG::Latency, string_compose (
331 "GET PORT %1 capture PRIVATE latency now [%2 - %3]\n",
333 _private_playback_latency.min,
334 _private_playback_latency.max));
335 return _private_capture_latency;
340 Port::public_latency_range (bool /*playback*/) const
342 jack_latency_range_t r;
344 jack_port_get_latency_range (_jack_port,
345 sends_output() ? JackPlaybackLatency : JackCaptureLatency,
347 DEBUG_TRACE (DEBUG::Latency, string_compose (
348 "GET PORT %1: %4 PUBLIC latency range %2 .. %3\n",
349 name(), r.min, r.max,
350 sends_output() ? "PLAYBACK" : "CAPTURE"));
355 Port::get_connected_latency_range (jack_latency_range_t& range, bool playback) const
357 if (!jack_port_get_latency_range) {
361 vector<string> connections;
362 jack_client_t* jack = _engine->jack();
367 PBD::warning << _("get_connected_latency_range() called while disconnected from JACK") << endmsg;
371 get_connections (connections);
373 if (!connections.empty()) {
375 range.min = ~((jack_nframes_t) 0);
378 DEBUG_TRACE (DEBUG::Latency, string_compose ("%1: %2 connections to check for latency range\n", name(), connections.size()));
380 for (vector<string>::const_iterator c = connections.begin();
381 c != connections.end(); ++c) {
383 jack_latency_range_t lr;
385 if (!AudioEngine::instance()->port_is_mine (*c)) {
387 /* port belongs to some other JACK client, use
388 * JACK to lookup its latency information.
391 jack_port_t* remote_port = jack_port_by_name (_engine->jack(), (*c).c_str());
394 jack_port_get_latency_range (
396 (playback ? JackPlaybackLatency : JackCaptureLatency),
399 DEBUG_TRACE (DEBUG::Latency, string_compose (
400 "\t%1 <-> %2 : latter has latency range %3 .. %4\n",
401 name(), *c, lr.min, lr.max));
403 range.min = min (range.min, lr.min);
404 range.max = max (range.max, lr.max);
409 /* port belongs to this instance of ardour,
410 so look up its latency information
411 internally, because our published/public
412 values already contain our plugin
413 latency compensation.
416 boost::shared_ptr<Port> remote_port = AudioEngine::instance()->get_port_by_name (*c);
418 lr = remote_port->private_latency_range ((playback ? JackPlaybackLatency : JackCaptureLatency));
419 DEBUG_TRACE (DEBUG::Latency, string_compose (
420 "\t%1 <-LOCAL-> %2 : latter has latency range %3 .. %4\n",
421 name(), *c, lr.min, lr.max));
423 range.min = min (range.min, lr.min);
424 range.max = max (range.max, lr.max);
430 DEBUG_TRACE (DEBUG::Latency, string_compose ("%1: not connected to anything\n", name()));
435 DEBUG_TRACE (DEBUG::Latency, string_compose ("%1: final connected latency range [ %2 .. %3 ] \n", name(), range.min, range.max));
441 jack_client_t* jack = _engine->jack();
447 _jack_port = jack_port_register (jack, _name.c_str(), type().to_jack_type(), _flags, 0);
449 if (_jack_port == 0) {
450 PBD::error << string_compose (_("could not reregister %1"), _name) << endmsg;
463 /* caller must hold process lock; intended to be used only after reestablish() */
465 for (std::set<string>::iterator i = _connections.begin(); i != _connections.end(); ++i) {
474 /** @param n Short port name (no JACK client name) */
476 Port::set_name (std::string const & n)
482 int const r = jack_port_set_name (_jack_port, n.c_str());
485 _engine->port_renamed (_name, n);
494 Port::request_jack_monitors_input (bool yn)
496 jack_port_request_monitor (_jack_port, yn);
500 Port::physically_connected () const
502 const char** jc = jack_port_get_connections (_jack_port);
505 for (int i = 0; jc[i]; ++i) {
507 jack_port_t* port = jack_port_by_name (_engine->jack(), jc[i]);
509 if (port && (jack_port_flags (port) & JackPortIsPhysical)) {