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"
26 #include <jack/weakjack.h> // so that we can test for new functions at runtime
28 #include "pbd/error.h"
29 #include "pbd/compose.h"
31 #include "ardour/debug.h"
32 #include "ardour/port.h"
33 #include "ardour/audioengine.h"
34 #include "pbd/failed_constructor.h"
39 using namespace ARDOUR;
42 PBD::Signal2<void,boost::shared_ptr<Port>, boost::shared_ptr<Port> > Port::PostDisconnect;
44 AudioEngine* Port::_engine = 0;
45 bool Port::_connecting_blocked = false;
46 pframes_t Port::_global_port_buffer_offset = 0;
47 pframes_t Port::_cycle_nframes = 0;
49 /** @param n Port short name */
50 Port::Port (std::string const & n, DataType t, Flags f)
51 : _port_buffer_offset (0)
54 , _last_monitor (false)
56 _private_playback_latency.min = 0;
57 _private_playback_latency.max = 0;
58 _private_capture_latency.min = 0;
59 _private_capture_latency.max = 0;
61 /* Unfortunately we have to pass the DataType into this constructor so that
62 we can create the right kind of JACK port; aside from this we'll use the
63 virtual function type () to establish type.
66 assert (_name.find_first_of (':') == std::string::npos);
68 if (!_engine->connected()) {
69 throw failed_constructor ();
72 if ((_jack_port = jack_port_register (_engine->jack (), _name.c_str (), t.to_jack_type (), _flags, 0)) == 0) {
73 cerr << "Failed to register JACK port \"" << _name << "\", reason is unknown from here\n";
74 throw failed_constructor ();
78 /** Port destructor */
81 if (_engine->jack ()) {
82 jack_port_unregister (_engine->jack (), _jack_port);
86 /** @return true if this port is connected to anything */
88 Port::connected () const
90 return (jack_port_connected (_jack_port) != 0);
94 Port::disconnect_all ()
96 jack_port_disconnect (_engine->jack(), _jack_port);
97 _connections.clear ();
99 /* a cheaper, less hacky way to do boost::shared_from_this() ...
101 boost::shared_ptr<Port> pself = _engine->get_port_by_name (name());
102 PostDisconnect (pself, boost::shared_ptr<Port>()); // emit signal
107 /** @param o Port name
108 * @return true if this port is connected to o, otherwise false.
111 Port::connected_to (std::string const & o) const
113 if (!_engine->connected()) {
114 /* in some senses, this answer isn't the right one all the time,
115 because we know about our connections and will re-establish
116 them when we reconnect to JACK.
121 return jack_port_connected_to (_jack_port,
122 _engine->make_port_name_non_relative(o).c_str ());
125 /** @param o Filled in with port full names of ports that we are connected to */
127 Port::get_connections (std::vector<std::string> & c) const
131 if (_engine->connected()) {
132 const char** jc = jack_port_get_connections (_jack_port);
134 for (int i = 0; jc[i]; ++i) {
151 Port::connect (std::string const & other)
153 std::string const other_shrt = _engine->make_port_name_non_relative (other);
154 std::string const this_shrt = _engine->make_port_name_non_relative (_name);
158 if (_connecting_blocked) {
162 if (sends_output ()) {
163 r = jack_connect (_engine->jack (), this_shrt.c_str (), other_shrt.c_str ());
165 r = jack_connect (_engine->jack (), other_shrt.c_str (), this_shrt.c_str());
169 _connections.insert (other);
176 Port::disconnect (std::string const & other)
178 std::string const other_fullname = _engine->make_port_name_non_relative (other);
179 std::string const this_fullname = _engine->make_port_name_non_relative (_name);
183 if (sends_output ()) {
184 r = jack_disconnect (_engine->jack (), this_fullname.c_str (), other_fullname.c_str ());
186 r = jack_disconnect (_engine->jack (), other_fullname.c_str (), this_fullname.c_str ());
190 _connections.erase (other);
193 /* a cheaper, less hacky way to do boost::shared_from_this() ...
195 boost::shared_ptr<Port> pself = _engine->get_port_by_name (name());
196 boost::shared_ptr<Port> pother = _engine->get_port_by_name (other);
198 if (pself && pother) {
199 /* Disconnecting from another Ardour port: need to allow
200 a check on whether this may affect anything that we
203 PostDisconnect (pself, pother); // emit signal
211 Port::connected_to (Port* o) const
213 return connected_to (o->name ());
217 Port::connect (Port* o)
219 return connect (o->name ());
223 Port::disconnect (Port* o)
225 return disconnect (o->name ());
229 Port::set_engine (AudioEngine* e)
235 Port::ensure_jack_monitors_input (bool yn)
237 jack_port_ensure_monitor (_jack_port, yn);
241 Port::jack_monitoring_input () const
243 return jack_port_monitoring_input (_jack_port);
249 _last_monitor = false;
253 Port::cycle_start (pframes_t)
255 _port_buffer_offset = 0;
259 Port::increment_port_buffer_offset (pframes_t nframes)
261 _port_buffer_offset += nframes;
265 Port::set_public_latency_range (jack_latency_range_t& range, bool playback) const
267 /* this sets the visible latency that the rest of JACK sees. because we do latency
268 compensation, all (most) of our visible port latency values are identical.
271 if (!jack_port_set_latency_range) {
275 DEBUG_TRACE (DEBUG::Latency,
276 string_compose ("SET PORT %1 %4 PUBLIC latency now [%2 - %3]\n",
277 name(), range.min, range.max,
278 (playback ? "PLAYBACK" : "CAPTURE")));;
280 jack_port_set_latency_range (_jack_port,
281 (playback ? JackPlaybackLatency : JackCaptureLatency),
286 Port::set_private_latency_range (jack_latency_range_t& range, bool playback)
289 _private_playback_latency = range;
290 DEBUG_TRACE (DEBUG::Latency, string_compose (
291 "SET PORT %1 playback PRIVATE latency now [%2 - %3]\n",
293 _private_playback_latency.min,
294 _private_playback_latency.max));
296 _private_capture_latency = range;
297 DEBUG_TRACE (DEBUG::Latency, string_compose (
298 "SET PORT %1 capture PRIVATE latency now [%2 - %3]\n",
300 _private_capture_latency.min,
301 _private_capture_latency.max));
304 /* push to public (JACK) location so that everyone else can see it */
306 set_public_latency_range (range, playback);
309 const jack_latency_range_t&
310 Port::private_latency_range (bool playback) const
313 DEBUG_TRACE (DEBUG::Latency, string_compose (
314 "GET PORT %1 playback PRIVATE latency now [%2 - %3]\n",
316 _private_playback_latency.min,
317 _private_playback_latency.max));
318 return _private_playback_latency;
320 DEBUG_TRACE (DEBUG::Latency, string_compose (
321 "GET PORT %1 capture PRIVATE latency now [%2 - %3]\n",
323 _private_playback_latency.min,
324 _private_playback_latency.max));
325 return _private_capture_latency;
330 Port::public_latency_range (bool /*playback*/) const
332 jack_latency_range_t r;
334 jack_port_get_latency_range (_jack_port,
335 sends_output() ? JackPlaybackLatency : JackCaptureLatency,
337 DEBUG_TRACE (DEBUG::Latency, string_compose (
338 "GET PORT %1: %4 PUBLIC latency range %2 .. %3\n",
339 name(), r.min, r.max,
340 sends_output() ? "PLAYBACK" : "CAPTURE"));
345 Port::get_connected_latency_range (jack_latency_range_t& range, bool playback) const
347 if (!jack_port_get_latency_range) {
351 vector<string> connections;
352 jack_client_t* jack = _engine->jack();
357 PBD::warning << _("get_connected_latency_range() called while disconnected from JACK") << endmsg;
361 get_connections (connections);
363 if (!connections.empty()) {
365 range.min = ~((jack_nframes_t) 0);
368 DEBUG_TRACE (DEBUG::Latency, string_compose ("%1: %2 connections to check for latency range\n", name(), connections.size()));
370 for (vector<string>::const_iterator c = connections.begin();
371 c != connections.end(); ++c) {
373 jack_latency_range_t lr;
375 if (!AudioEngine::instance()->port_is_mine (*c)) {
377 /* port belongs to some other JACK client, use
378 * JACK to lookup its latency information.
381 jack_port_t* remote_port = jack_port_by_name (_engine->jack(), (*c).c_str());
384 jack_port_get_latency_range (
386 (playback ? JackPlaybackLatency : JackCaptureLatency),
389 DEBUG_TRACE (DEBUG::Latency, string_compose (
390 "\t%1 <-> %2 : latter has latency range %3 .. %4\n",
391 name(), *c, lr.min, lr.max));
393 range.min = min (range.min, lr.min);
394 range.max = max (range.max, lr.max);
399 /* port belongs to this instance of ardour,
400 so look up its latency information
401 internally, because our published/public
402 values already contain our plugin
403 latency compensation.
406 boost::shared_ptr<Port> remote_port = AudioEngine::instance()->get_port_by_name (*c);
408 lr = remote_port->private_latency_range ((playback ? JackPlaybackLatency : JackCaptureLatency));
409 DEBUG_TRACE (DEBUG::Latency, string_compose (
410 "\t%1 <-LOCAL-> %2 : latter has latency range %3 .. %4\n",
411 name(), *c, lr.min, lr.max));
413 range.min = min (range.min, lr.min);
414 range.max = max (range.max, lr.max);
420 DEBUG_TRACE (DEBUG::Latency, string_compose ("%1: not connected to anything\n", name()));
425 DEBUG_TRACE (DEBUG::Latency, string_compose ("%1: final connected latency range [ %2 .. %3 ] \n", name(), range.min, range.max));
431 jack_client_t* jack = _engine->jack();
437 _jack_port = jack_port_register (jack, _name.c_str(), type().to_jack_type(), _flags, 0);
439 if (_jack_port == 0) {
440 PBD::error << string_compose (_("could not reregister %1"), _name) << endmsg;
453 /* caller must hold process lock; intended to be used only after reestablish() */
455 for (std::set<string>::iterator i = _connections.begin(); i != _connections.end(); ++i) {
464 /** @param n Short port name (no JACK client name) */
466 Port::set_name (std::string const & n)
472 int const r = jack_port_set_name (_jack_port, n.c_str());
475 _engine->port_renamed (_name, n);
484 Port::request_jack_monitors_input (bool yn)
486 jack_port_request_monitor (_jack_port, yn);
490 Port::physically_connected () const
492 const char** jc = jack_port_get_connections (_jack_port);
495 for (int i = 0; jc[i]; ++i) {
497 jack_port_t* port = jack_port_by_name (_engine->jack(), jc[i]);
499 if (port && (jack_port_flags (port) & JackPortIsPhysical)) {