+void
+Port::set_private_latency_range (LatencyRange& range, bool playback)
+{
+ if (playback) {
+ _private_playback_latency = range;
+ DEBUG_TRACE (DEBUG::Latency, string_compose (
+ "SET PORT %1 playback PRIVATE latency now [%2 - %3]\n",
+ name(),
+ _private_playback_latency.min,
+ _private_playback_latency.max));
+ } else {
+ _private_capture_latency = range;
+ DEBUG_TRACE (DEBUG::Latency, string_compose (
+ "SET PORT %1 capture PRIVATE latency now [%2 - %3]\n",
+ name(),
+ _private_capture_latency.min,
+ _private_capture_latency.max));
+ }
+
+ /* push to public (port system) location so that everyone else can see it */
+
+ set_public_latency_range (range, playback);
+}
+
+const LatencyRange&
+Port::private_latency_range (bool playback) const
+{
+ if (playback) {
+ DEBUG_TRACE (DEBUG::Latency, string_compose (
+ "GET PORT %1 playback PRIVATE latency now [%2 - %3]\n",
+ name(),
+ _private_playback_latency.min,
+ _private_playback_latency.max));
+ return _private_playback_latency;
+ } else {
+ DEBUG_TRACE (DEBUG::Latency, string_compose (
+ "GET PORT %1 capture PRIVATE latency now [%2 - %3]\n",
+ name(),
+ _private_playback_latency.min,
+ _private_playback_latency.max));
+ return _private_capture_latency;
+ }
+}
+
+LatencyRange
+Port::public_latency_range (bool /*playback*/) const
+{
+ LatencyRange r;
+
+
+ if (_port_handle) {
+ r = port_engine.get_latency_range (_port_handle, sends_output() ? true : false);
+
+ DEBUG_TRACE (DEBUG::Latency, string_compose (
+ "GET PORT %1: %4 PUBLIC latency range %2 .. %3\n",
+ name(), r.min, r.max,
+ sends_output() ? "PLAYBACK" : "CAPTURE"));
+ }
+
+ return r;
+}
+
+void
+Port::get_connected_latency_range (LatencyRange& range, bool playback) const
+{
+ vector<string> connections;
+
+ get_connections (connections);
+
+ if (!connections.empty()) {
+
+ range.min = ~((pframes_t) 0);
+ range.max = 0;
+
+ DEBUG_TRACE (DEBUG::Latency, string_compose ("%1: %2 connections to check for latency range\n", name(), connections.size()));
+
+ for (vector<string>::const_iterator c = connections.begin();
+ c != connections.end(); ++c) {
+
+ LatencyRange lr;
+
+ if (!AudioEngine::instance()->port_is_mine (*c)) {
+
+ /* port belongs to some other port-system client, use
+ * the port engine to lookup its latency information.
+ */
+
+ PortEngine::PortHandle remote_port = port_engine.get_port_by_name (*c);
+
+ if (remote_port) {
+ lr = port_engine.get_latency_range (remote_port, playback);
+
+ DEBUG_TRACE (DEBUG::Latency, string_compose (
+ "\t%1 <-> %2 : latter has latency range %3 .. %4\n",
+ name(), *c, lr.min, lr.max));
+
+ range.min = min (range.min, lr.min);
+ range.max = max (range.max, lr.max);
+ }
+
+ } else {
+
+ /* port belongs to this instance of ardour,
+ so look up its latency information
+ internally, because our published/public
+ values already contain our plugin
+ latency compensation.
+ */
+
+ boost::shared_ptr<Port> remote_port = AudioEngine::instance()->get_port_by_name (*c);
+ if (remote_port) {
+ lr = remote_port->private_latency_range ((playback ? true : false));
+ DEBUG_TRACE (DEBUG::Latency, string_compose (
+ "\t%1 <-LOCAL-> %2 : latter has latency range %3 .. %4\n",
+ name(), *c, lr.min, lr.max));
+
+ range.min = min (range.min, lr.min);
+ range.max = max (range.max, lr.max);
+ }
+ }
+ }
+
+ } else {
+ DEBUG_TRACE (DEBUG::Latency, string_compose ("%1: not connected to anything\n", name()));
+ range.min = 0;
+ range.max = 0;
+ }
+
+ DEBUG_TRACE (DEBUG::Latency, string_compose ("%1: final connected latency range [ %2 .. %3 ] \n", name(), range.min, range.max));
+}
+
+int
+Port::reestablish ()
+{
+ DEBUG_TRACE (DEBUG::Ports, string_compose ("re-establish %1 port %2\n", type().to_string(), _name));
+ _port_handle = port_engine.register_port (_name, type(), _flags);
+
+ if (_port_handle == 0) {
+ PBD::error << string_compose (_("could not reregister %1"), _name) << endmsg;
+ return -1;
+ }
+
+ reset ();
+
+ return 0;
+}
+
+
+int
+Port::reconnect ()
+{
+ /* caller must hold process lock; intended to be used only after reestablish() */
+
+ DEBUG_TRACE (DEBUG::Ports, string_compose ("Connect %1 to %2 destinations\n",name(), _connections.size()));
+
+ for (std::set<string>::iterator i = _connections.begin(); i != _connections.end(); ++i) {
+ if (connect (*i)) {
+ return -1;
+ }
+ }
+
+ return 0;
+}
+
+/** @param n Short port name (no port-system client name) */
+int
+Port::set_name (std::string const & n)
+{
+ if (n == _name || !_port_handle) {
+ return 0;
+ }
+
+ int const r = port_engine.set_port_name (_port_handle, n);
+
+ if (r == 0) {
+ AudioEngine::instance()->port_renamed (_name, n);
+ _name = n;
+ }
+
+
+ return r;
+}
+
+bool
+Port::physically_connected () const
+{
+ if (!_port_handle) {
+ return false;
+ }
+
+ return port_engine.physically_connected (_port_handle);
+}
+
+XMLNode&
+Port::get_state () const
+{
+ XMLNode* root = new XMLNode (state_node_name);
+
+ root->add_property (X_("name"), AudioEngine::instance()->make_port_name_relative (name()));
+
+ if (receives_input()) {
+ root->add_property (X_("direction"), X_("input"));
+ } else {
+ root->add_property (X_("direction"), X_("output"));
+ }
+
+ vector<string> c;