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 #ifndef PLATFORM_WINDOWS
25 #include <jack/weakjack.h> // so that we can test for new functions at runtime
28 #include "pbd/compose.h"
29 #include "pbd/error.h"
30 #include "pbd/failed_constructor.h"
32 #include "ardour/audioengine.h"
33 #include "ardour/debug.h"
34 #include "ardour/port.h"
39 using namespace ARDOUR;
42 PBD::Signal2<void,boost::shared_ptr<Port>, boost::shared_ptr<Port> > Port::PostDisconnect;
43 PBD::Signal0<void> Port::PortDrop;
45 AudioEngine* Port::_engine = 0;
46 bool Port::_connecting_blocked = false;
47 pframes_t Port::_global_port_buffer_offset = 0;
48 pframes_t Port::_cycle_nframes = 0;
50 /** @param n Port short name */
51 Port::Port (std::string const & n, DataType t, Flags f)
52 : _port_buffer_offset (0)
55 , _last_monitor (false)
57 _private_playback_latency.min = 0;
58 _private_playback_latency.max = 0;
59 _private_capture_latency.min = 0;
60 _private_capture_latency.max = 0;
62 /* Unfortunately we have to pass the DataType into this constructor so that
63 we can create the right kind of JACK port; aside from this we'll use the
64 virtual function type () to establish type.
67 assert (_name.find_first_of (':') == std::string::npos);
69 if (!_engine->connected()) {
70 throw failed_constructor ();
73 if ((_jack_port = jack_port_register (_engine->jack (), _name.c_str (), t.to_jack_type (), _flags, 0)) == 0) {
74 cerr << "Failed to register JACK port \"" << _name << "\", reason is unknown from here\n";
75 throw failed_constructor ();
78 PortDrop.connect_same_thread (drop_connection, boost::bind (&Port::drop, this));
81 /** Port destructor */
91 if (_engine->jack ()) {
92 jack_port_unregister (_engine->jack (), _jack_port);
98 /** @return true if this port is connected to anything */
100 Port::connected () const
102 return (jack_port_connected (_jack_port) != 0);
106 Port::disconnect_all ()
108 jack_port_disconnect (_engine->jack(), _jack_port);
109 _connections.clear ();
111 /* a cheaper, less hacky way to do boost::shared_from_this() ...
113 boost::shared_ptr<Port> pself = _engine->get_port_by_name (name());
114 PostDisconnect (pself, boost::shared_ptr<Port>()); // emit signal
119 /** @param o Port name
120 * @return true if this port is connected to o, otherwise false.
123 Port::connected_to (std::string const & o) const
125 if (!_engine->connected()) {
126 /* in some senses, this answer isn't the right one all the time,
127 because we know about our connections and will re-establish
128 them when we reconnect to JACK.
133 return jack_port_connected_to (_jack_port,
134 _engine->make_port_name_non_relative(o).c_str ());
137 /** @param o Filled in with port full names of ports that we are connected to */
139 Port::get_connections (std::vector<std::string> & c) const
143 if (_engine->connected()) {
144 const char** jc = jack_port_get_connections (_jack_port);
146 for (int i = 0; jc[i]; ++i) {
163 Port::connect (std::string const & other)
165 std::string const other_shrt = _engine->make_port_name_non_relative (other);
166 std::string const this_shrt = _engine->make_port_name_non_relative (_name);
170 if (_connecting_blocked) {
174 if (sends_output ()) {
175 r = jack_connect (_engine->jack (), this_shrt.c_str (), other_shrt.c_str ());
177 r = jack_connect (_engine->jack (), other_shrt.c_str (), this_shrt.c_str());
181 _connections.insert (other);
188 Port::disconnect (std::string const & other)
190 std::string const other_fullname = _engine->make_port_name_non_relative (other);
191 std::string const this_fullname = _engine->make_port_name_non_relative (_name);
195 if (sends_output ()) {
196 r = jack_disconnect (_engine->jack (), this_fullname.c_str (), other_fullname.c_str ());
198 r = jack_disconnect (_engine->jack (), other_fullname.c_str (), this_fullname.c_str ());
202 _connections.erase (other);
205 /* a cheaper, less hacky way to do boost::shared_from_this() ...
207 boost::shared_ptr<Port> pself = _engine->get_port_by_name (name());
208 boost::shared_ptr<Port> pother = _engine->get_port_by_name (other);
210 if (pself && pother) {
211 /* Disconnecting from another Ardour port: need to allow
212 a check on whether this may affect anything that we
215 PostDisconnect (pself, pother); // emit signal
223 Port::connected_to (Port* o) const
225 return connected_to (o->name ());
229 Port::connect (Port* o)
231 return connect (o->name ());
235 Port::disconnect (Port* o)
237 return disconnect (o->name ());
241 Port::set_engine (AudioEngine* e)
247 Port::ensure_jack_monitors_input (bool yn)
249 jack_port_ensure_monitor (_jack_port, yn);
253 Port::jack_monitoring_input () const
255 return jack_port_monitoring_input (_jack_port);
261 _last_monitor = false;
265 Port::cycle_start (pframes_t)
267 _port_buffer_offset = 0;
271 Port::increment_port_buffer_offset (pframes_t nframes)
273 _port_buffer_offset += nframes;
277 Port::set_public_latency_range (jack_latency_range_t& range, bool playback) const
279 /* this sets the visible latency that the rest of JACK sees. because we do latency
280 compensation, all (most) of our visible port latency values are identical.
283 if (!jack_port_set_latency_range) {
287 DEBUG_TRACE (DEBUG::Latency,
288 string_compose ("SET PORT %1 %4 PUBLIC latency now [%2 - %3]\n",
289 name(), range.min, range.max,
290 (playback ? "PLAYBACK" : "CAPTURE")));;
292 jack_port_set_latency_range (_jack_port,
293 (playback ? JackPlaybackLatency : JackCaptureLatency),
298 Port::set_private_latency_range (jack_latency_range_t& range, bool playback)
301 _private_playback_latency = range;
302 DEBUG_TRACE (DEBUG::Latency, string_compose (
303 "SET PORT %1 playback PRIVATE latency now [%2 - %3]\n",
305 _private_playback_latency.min,
306 _private_playback_latency.max));
308 _private_capture_latency = range;
309 DEBUG_TRACE (DEBUG::Latency, string_compose (
310 "SET PORT %1 capture PRIVATE latency now [%2 - %3]\n",
312 _private_capture_latency.min,
313 _private_capture_latency.max));
316 /* push to public (JACK) location so that everyone else can see it */
318 set_public_latency_range (range, playback);
321 const jack_latency_range_t&
322 Port::private_latency_range (bool playback) const
325 DEBUG_TRACE (DEBUG::Latency, string_compose (
326 "GET PORT %1 playback PRIVATE latency now [%2 - %3]\n",
328 _private_playback_latency.min,
329 _private_playback_latency.max));
330 return _private_playback_latency;
332 DEBUG_TRACE (DEBUG::Latency, string_compose (
333 "GET PORT %1 capture PRIVATE latency now [%2 - %3]\n",
335 _private_playback_latency.min,
336 _private_playback_latency.max));
337 return _private_capture_latency;
342 Port::public_latency_range (bool /*playback*/) const
344 jack_latency_range_t r;
346 jack_port_get_latency_range (_jack_port,
347 sends_output() ? JackPlaybackLatency : JackCaptureLatency,
349 DEBUG_TRACE (DEBUG::Latency, string_compose (
350 "GET PORT %1: %4 PUBLIC latency range %2 .. %3\n",
351 name(), r.min, r.max,
352 sends_output() ? "PLAYBACK" : "CAPTURE"));
357 Port::get_connected_latency_range (jack_latency_range_t& range, bool playback) const
359 if (!jack_port_get_latency_range) {
363 vector<string> connections;
364 jack_client_t* jack = _engine->jack();
369 PBD::warning << _("get_connected_latency_range() called while disconnected from JACK") << endmsg;
373 get_connections (connections);
375 if (!connections.empty()) {
377 range.min = ~((jack_nframes_t) 0);
380 DEBUG_TRACE (DEBUG::Latency, string_compose ("%1: %2 connections to check for latency range\n", name(), connections.size()));
382 for (vector<string>::const_iterator c = connections.begin();
383 c != connections.end(); ++c) {
385 jack_latency_range_t lr;
387 if (!AudioEngine::instance()->port_is_mine (*c)) {
389 /* port belongs to some other JACK client, use
390 * JACK to lookup its latency information.
393 jack_port_t* remote_port = jack_port_by_name (_engine->jack(), (*c).c_str());
396 jack_port_get_latency_range (
398 (playback ? JackPlaybackLatency : JackCaptureLatency),
401 DEBUG_TRACE (DEBUG::Latency, string_compose (
402 "\t%1 <-> %2 : latter has latency range %3 .. %4\n",
403 name(), *c, lr.min, lr.max));
405 range.min = min (range.min, lr.min);
406 range.max = max (range.max, lr.max);
411 /* port belongs to this instance of ardour,
412 so look up its latency information
413 internally, because our published/public
414 values already contain our plugin
415 latency compensation.
418 boost::shared_ptr<Port> remote_port = AudioEngine::instance()->get_port_by_name (*c);
420 lr = remote_port->private_latency_range ((playback ? JackPlaybackLatency : JackCaptureLatency));
421 DEBUG_TRACE (DEBUG::Latency, string_compose (
422 "\t%1 <-LOCAL-> %2 : latter has latency range %3 .. %4\n",
423 name(), *c, lr.min, lr.max));
425 range.min = min (range.min, lr.min);
426 range.max = max (range.max, lr.max);
432 DEBUG_TRACE (DEBUG::Latency, string_compose ("%1: not connected to anything\n", name()));
437 DEBUG_TRACE (DEBUG::Latency, string_compose ("%1: final connected latency range [ %2 .. %3 ] \n", name(), range.min, range.max));
443 jack_client_t* jack = _engine->jack();
449 _jack_port = jack_port_register (jack, _name.c_str(), type().to_jack_type(), _flags, 0);
451 if (_jack_port == 0) {
452 PBD::error << string_compose (_("could not reregister %1"), _name) << endmsg;
465 /* caller must hold process lock; intended to be used only after reestablish() */
467 for (std::set<string>::iterator i = _connections.begin(); i != _connections.end(); ++i) {
476 /** @param n Short port name (no JACK client name) */
478 Port::set_name (std::string const & n)
484 int const r = jack_port_set_name (_jack_port, n.c_str());
487 _engine->port_renamed (_name, n);
496 Port::request_jack_monitors_input (bool yn)
498 jack_port_request_monitor (_jack_port, yn);
502 Port::physically_connected () const
504 const char** jc = jack_port_get_connections (_jack_port);
507 for (int i = 0; jc[i]; ++i) {
509 jack_port_t* port = jack_port_by_name (_engine->jack(), jc[i]);
511 if (port && (jack_port_flags (port) & JackPortIsPhysical)) {