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 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 we can
60 create the right kind of JACK port; aside from this we'll use the virtual function 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, 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 ();
100 /** @param o Port name
101 * @return true if this port is connected to o, otherwise false.
104 Port::connected_to (std::string const & o) const
106 if (!_engine->connected()) {
107 /* in some senses, this answer isn't the right one all the time,
108 because we know about our connections and will re-establish
109 them when we reconnect to JACK.
114 return jack_port_connected_to (_jack_port, _engine->make_port_name_non_relative(o).c_str ());
117 /** @param o Filled in with port full names of ports that we are connected to */
119 Port::get_connections (std::vector<std::string> & c) const
123 if (_engine->connected()) {
124 const char** jc = jack_port_get_connections (_jack_port);
126 for (int i = 0; jc[i]; ++i) {
139 Port::connect (std::string const & other)
141 std::string const other_shrt = _engine->make_port_name_non_relative (other);
142 std::string const this_shrt = _engine->make_port_name_non_relative (_name);
146 if (_connecting_blocked) {
150 if (sends_output ()) {
151 r = jack_connect (_engine->jack (), this_shrt.c_str (), other_shrt.c_str ());
153 r = jack_connect (_engine->jack (), other_shrt.c_str (), this_shrt.c_str());
157 _connections.insert (other);
164 Port::disconnect (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 (sends_output ()) {
172 r = jack_disconnect (_engine->jack (), this_shrt.c_str (), other_shrt.c_str ());
174 r = jack_disconnect (_engine->jack (), other_shrt.c_str (), this_shrt.c_str ());
178 _connections.erase (other);
186 Port::connected_to (Port* o) const
188 return connected_to (o->name ());
192 Port::connect (Port* o)
194 return connect (o->name ());
198 Port::disconnect (Port* o)
200 return disconnect (o->name ());
204 Port::set_engine (AudioEngine* e)
210 Port::ensure_monitor_input (bool yn)
212 jack_port_ensure_monitor (_jack_port, yn);
216 Port::monitoring_input () const
218 return jack_port_monitoring_input (_jack_port);
224 _last_monitor = false;
228 Port::cycle_start (pframes_t nframes)
230 _port_buffer_offset = 0;
234 Port::increment_port_buffer_offset (pframes_t nframes)
236 _port_buffer_offset += nframes;
240 Port::set_public_latency_range (jack_latency_range_t& range, bool playback) const
242 /* this sets the visible latency that the rest of JACK sees. because we do latency
243 compensation, all (most) of our visible port latency values are identical.
246 if (!jack_port_set_latency_range) {
250 DEBUG_TRACE (DEBUG::Latency, string_compose ("SET PORT %1 %4 PUBLIC latency now [%2 - %3]\n", name(),
253 (playback ? "PLAYBACK" : "CAPTURE")));;
255 jack_port_set_latency_range (_jack_port, (playback ? JackPlaybackLatency : JackCaptureLatency), &range);
259 Port::set_private_latency_range (jack_latency_range_t& range, bool playback)
262 _private_playback_latency = range;
263 DEBUG_TRACE (DEBUG::Latency, string_compose ("SET PORT %1 playback PRIVATE latency now [%2 - %3]\n", name(),
264 _private_playback_latency.min,
265 _private_playback_latency.max));
267 _private_capture_latency = range;
268 DEBUG_TRACE (DEBUG::Latency, string_compose ("SET PORT %1 capture PRIVATE latency now [%2 - %3]\n", name(),
269 _private_capture_latency.min,
270 _private_capture_latency.max));
273 /* push to public (JACK) location so that everyone else can see it */
275 set_public_latency_range (range, playback);
278 const jack_latency_range_t&
279 Port::private_latency_range (bool playback) const
282 DEBUG_TRACE (DEBUG::Latency, string_compose ("GET PORT %1 playback PRIVATE latency now [%2 - %3]\n", name(),
283 _private_playback_latency.min,
284 _private_playback_latency.max));
285 return _private_playback_latency;
287 DEBUG_TRACE (DEBUG::Latency, string_compose ("GET PORT %1 capture PRIVATE latency now [%2 - %3]\n", name(),
288 _private_playback_latency.min,
289 _private_playback_latency.max));
290 return _private_capture_latency;
295 Port::public_latency_range (bool playback) const
297 jack_latency_range_t r;
299 jack_port_get_latency_range (_jack_port,
300 sends_output() ? JackPlaybackLatency : JackCaptureLatency,
302 DEBUG_TRACE (DEBUG::Latency, string_compose ("GET PORT %1: %4 PUBLIC latency range %2 .. %3\n",
303 name(), r.min, r.max,
304 sends_output() ? "PLAYBACK" : "CAPTURE"));
309 Port::get_connected_latency_range (jack_latency_range_t& range, bool playback) const
311 if (!jack_port_get_latency_range) {
315 vector<string> connections;
316 jack_client_t* jack = _engine->jack();
321 PBD::warning << _("get_connected_latency_range() called while disconnected from JACK") << endmsg;
325 get_connections (connections);
327 if (!connections.empty()) {
329 range.min = ~((jack_nframes_t) 0);
332 for (vector<string>::iterator c = connections.begin(); c != connections.end(); ++c) {
334 cerr << "Connection between " << name() << " and " << *c << endl;
336 jack_port_t* remote_port = jack_port_by_name (_engine->jack(), (*c).c_str());
337 jack_latency_range_t lr;
340 jack_port_get_latency_range (remote_port, (playback ? JackPlaybackLatency : JackCaptureLatency), &lr);
341 DEBUG_TRACE (DEBUG::Latency, string_compose ("\t%1 <-> %2 : latter has latency range %3 .. %4\n", name(), *c, lr.min, lr.max));
342 range.min = min (range.min, lr.min);
343 range.max = max (range.max, lr.max);
345 cerr << "\t NO PORT BY NAME!\n";
359 jack_client_t* jack = _engine->jack();
365 _jack_port = jack_port_register (jack, _name.c_str(), type().to_jack_type(), _flags, 0);
367 if (_jack_port == 0) {
368 PBD::error << string_compose (_("could not reregister %1"), _name) << endmsg;
381 /* caller must hold process lock; intended to be used only after reestablish() */
383 for (std::set<string>::iterator i = _connections.begin(); i != _connections.end(); ++i) {
392 /** @param n Short port name (no JACK client name) */
394 Port::set_name (std::string const & n)
400 int const r = jack_port_set_name (_jack_port, n.c_str());
410 Port::request_monitor_input (bool yn)
412 jack_port_request_monitor (_jack_port, yn);
416 Port::physically_connected () const
418 const char** jc = jack_port_get_connections (_jack_port);
421 for (int i = 0; jc[i]; ++i) {
423 jack_port_t* port = jack_port_by_name (_engine->jack(), jc[i]);
425 if (port && (jack_port_flags (port) & JackPortIsPhysical)) {