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;
42 AudioEngine* Port::_engine = 0;
43 bool Port::_connecting_blocked = false;
44 pframes_t Port::_global_port_buffer_offset = 0;
45 pframes_t Port::_cycle_nframes = 0;
47 /** @param n Port short name */
48 Port::Port (std::string const & n, DataType t, Flags f)
49 : _port_buffer_offset (0)
52 , _last_monitor (false)
54 _private_playback_latency.min = 0;
55 _private_playback_latency.max = 0;
56 _private_capture_latency.min = 0;
57 _private_capture_latency.max = 0;
59 /* Unfortunately we have to pass the DataType into this constructor so that
60 we can create the right kind of JACK port; aside from this we'll use the
61 virtual function type () to establish type.
64 assert (_name.find_first_of (':') == std::string::npos);
66 if (!_engine->connected()) {
67 throw failed_constructor ();
70 if ((_jack_port = jack_port_register (_engine->jack (), _name.c_str (), t.to_jack_type (), _flags, 0)) == 0) {
71 cerr << "Failed to register JACK port \"" << _name << "\", reason is unknown from here\n";
72 throw failed_constructor ();
76 /** Port destructor */
79 if (_engine->jack ()) {
80 jack_port_unregister (_engine->jack (), _jack_port);
84 /** @return true if this port is connected to anything */
86 Port::connected () const
88 return (jack_port_connected (_jack_port) != 0);
92 Port::disconnect_all ()
94 jack_port_disconnect (_engine->jack(), _jack_port);
95 _connections.clear ();
97 /* a cheaper, less hacky way to do boost::shared_from_this() ...
99 boost::shared_ptr<Port> pself = _engine->get_port_by_name (name());
100 PostDisconnect (pself, boost::shared_ptr<Port>()); // emit signal
105 /** @param o Port name
106 * @return true if this port is connected to o, otherwise false.
109 Port::connected_to (std::string const & o) const
111 if (!_engine->connected()) {
112 /* in some senses, this answer isn't the right one all the time,
113 because we know about our connections and will re-establish
114 them when we reconnect to JACK.
119 return jack_port_connected_to (_jack_port,
120 _engine->make_port_name_non_relative(o).c_str ());
123 /** @param o Filled in with port full names of ports that we are connected to */
125 Port::get_connections (std::vector<std::string> & c) const
129 if (_engine->connected()) {
130 const char** jc = jack_port_get_connections (_jack_port);
132 for (int i = 0; jc[i]; ++i) {
149 Port::connect (std::string const & other)
151 std::string const other_shrt = _engine->make_port_name_non_relative (other);
152 std::string const this_shrt = _engine->make_port_name_non_relative (_name);
156 if (_connecting_blocked) {
160 if (sends_output ()) {
161 r = jack_connect (_engine->jack (), this_shrt.c_str (), other_shrt.c_str ());
163 r = jack_connect (_engine->jack (), other_shrt.c_str (), this_shrt.c_str());
167 _connections.insert (other);
174 Port::disconnect (std::string const & other)
176 std::string const other_fullname = _engine->make_port_name_non_relative (other);
177 std::string const this_fullname = _engine->make_port_name_non_relative (_name);
181 if (sends_output ()) {
182 r = jack_disconnect (_engine->jack (), this_fullname.c_str (), other_fullname.c_str ());
184 r = jack_disconnect (_engine->jack (), other_fullname.c_str (), this_fullname.c_str ());
188 _connections.erase (other);
191 /* a cheaper, less hacky way to do boost::shared_from_this() ...
193 boost::shared_ptr<Port> pself = _engine->get_port_by_name (name());
194 boost::shared_ptr<Port> pother = _engine->get_port_by_name (other);
196 if (pself && pother) {
197 /* Disconnecting from another Ardour port: need to allow
198 a check on whether this may affect anything that we
201 PostDisconnect (pself, pother); // emit signal
209 Port::connected_to (Port* o) const
211 return connected_to (o->name ());
215 Port::connect (Port* o)
217 return connect (o->name ());
221 Port::disconnect (Port* o)
223 return disconnect (o->name ());
227 Port::set_engine (AudioEngine* e)
233 Port::ensure_jack_monitors_input (bool yn)
235 jack_port_ensure_monitor (_jack_port, yn);
239 Port::jack_monitoring_input () const
241 return jack_port_monitoring_input (_jack_port);
247 _last_monitor = false;
251 Port::cycle_start (pframes_t)
253 _port_buffer_offset = 0;
257 Port::increment_port_buffer_offset (pframes_t nframes)
259 _port_buffer_offset += nframes;
263 Port::set_public_latency_range (jack_latency_range_t& range, bool playback) const
265 /* this sets the visible latency that the rest of JACK sees. because we do latency
266 compensation, all (most) of our visible port latency values are identical.
269 if (!jack_port_set_latency_range) {
273 DEBUG_TRACE (DEBUG::Latency,
274 string_compose ("SET PORT %1 %4 PUBLIC latency now [%2 - %3]\n",
275 name(), range.min, range.max,
276 (playback ? "PLAYBACK" : "CAPTURE")));;
278 jack_port_set_latency_range (_jack_port,
279 (playback ? JackPlaybackLatency : JackCaptureLatency),
284 Port::set_private_latency_range (jack_latency_range_t& range, bool playback)
287 _private_playback_latency = range;
288 DEBUG_TRACE (DEBUG::Latency, string_compose (
289 "SET PORT %1 playback PRIVATE latency now [%2 - %3]\n",
291 _private_playback_latency.min,
292 _private_playback_latency.max));
294 _private_capture_latency = range;
295 DEBUG_TRACE (DEBUG::Latency, string_compose (
296 "SET PORT %1 capture PRIVATE latency now [%2 - %3]\n",
298 _private_capture_latency.min,
299 _private_capture_latency.max));
302 /* push to public (JACK) location so that everyone else can see it */
304 set_public_latency_range (range, playback);
307 const jack_latency_range_t&
308 Port::private_latency_range (bool playback) const
311 DEBUG_TRACE (DEBUG::Latency, string_compose (
312 "GET PORT %1 playback PRIVATE latency now [%2 - %3]\n",
314 _private_playback_latency.min,
315 _private_playback_latency.max));
316 return _private_playback_latency;
318 DEBUG_TRACE (DEBUG::Latency, string_compose (
319 "GET PORT %1 capture PRIVATE latency now [%2 - %3]\n",
321 _private_playback_latency.min,
322 _private_playback_latency.max));
323 return _private_capture_latency;
328 Port::public_latency_range (bool /*playback*/) const
330 jack_latency_range_t r;
332 jack_port_get_latency_range (_jack_port,
333 sends_output() ? JackPlaybackLatency : JackCaptureLatency,
335 DEBUG_TRACE (DEBUG::Latency, string_compose (
336 "GET PORT %1: %4 PUBLIC latency range %2 .. %3\n",
337 name(), r.min, r.max,
338 sends_output() ? "PLAYBACK" : "CAPTURE"));
343 Port::get_connected_latency_range (jack_latency_range_t& range, bool playback) const
345 if (!jack_port_get_latency_range) {
349 vector<string> connections;
350 jack_client_t* jack = _engine->jack();
355 PBD::warning << _("get_connected_latency_range() called while disconnected from JACK") << endmsg;
359 get_connections (connections);
361 if (!connections.empty()) {
363 range.min = ~((jack_nframes_t) 0);
366 DEBUG_TRACE (DEBUG::Latency, string_compose ("%1: %2 connections to check for latency range\n", name(), connections.size()));
368 for (vector<string>::const_iterator c = connections.begin();
369 c != connections.end(); ++c) {
371 jack_latency_range_t lr;
373 if (!AudioEngine::instance()->port_is_mine (*c)) {
375 /* port belongs to some other JACK client, use
376 * JACK to lookup its latency information.
379 jack_port_t* remote_port = jack_port_by_name (_engine->jack(), (*c).c_str());
382 jack_port_get_latency_range (
384 (playback ? JackPlaybackLatency : JackCaptureLatency),
387 DEBUG_TRACE (DEBUG::Latency, string_compose (
388 "\t%1 <-> %2 : latter has latency range %3 .. %4\n",
389 name(), *c, lr.min, lr.max));
391 range.min = min (range.min, lr.min);
392 range.max = max (range.max, lr.max);
397 /* port belongs to this instance of ardour,
398 so look up its latency information
399 internally, because our published/public
400 values already contain our plugin
401 latency compensation.
404 boost::shared_ptr<Port> remote_port = AudioEngine::instance()->get_port_by_name (*c);
406 lr = remote_port->private_latency_range ((playback ? JackPlaybackLatency : JackCaptureLatency));
407 DEBUG_TRACE (DEBUG::Latency, string_compose (
408 "\t%1 <-LOCAL-> %2 : latter has latency range %3 .. %4\n",
409 name(), *c, lr.min, lr.max));
411 range.min = min (range.min, lr.min);
412 range.max = max (range.max, lr.max);
418 DEBUG_TRACE (DEBUG::Latency, string_compose ("%1: not connected to anything\n", name()));
423 DEBUG_TRACE (DEBUG::Latency, string_compose ("%1: final connected latency range [ %2 .. %3 ] \n", name(), range.min, range.max));
429 jack_client_t* jack = _engine->jack();
435 _jack_port = jack_port_register (jack, _name.c_str(), type().to_jack_type(), _flags, 0);
437 if (_jack_port == 0) {
438 PBD::error << string_compose (_("could not reregister %1"), _name) << endmsg;
451 /* caller must hold process lock; intended to be used only after reestablish() */
453 for (std::set<string>::iterator i = _connections.begin(); i != _connections.end(); ++i) {
462 /** @param n Short port name (no JACK client name) */
464 Port::set_name (std::string const & n)
470 int const r = jack_port_set_name (_jack_port, n.c_str());
473 _engine->port_renamed (_name, n);
482 Port::request_jack_monitors_input (bool yn)
484 jack_port_request_monitor (_jack_port, yn);
488 Port::physically_connected () const
490 const char** jc = jack_port_get_connections (_jack_port);
493 for (int i = 0; jc[i]; ++i) {
495 jack_port_t* port = jack_port_by_name (_engine->jack(), jc[i]);
497 if (port && (jack_port_flags (port) & JackPortIsPhysical)) {