X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=libs%2Fardour%2Fbundle.cc;h=65f0c8e127e468da830916ee799e27b4a235fe47;hb=4250940ad83200e065b966d0646c75a48bb0f73d;hp=6180836bd2969d6380fe34626a05b2c4b16ee84f;hpb=fa1e12b682e66ef6eb30de9aaad7ac919eb10634;p=ardour.git diff --git a/libs/ardour/bundle.cc b/libs/ardour/bundle.cc index 6180836bd2..65f0c8e127 100644 --- a/libs/ardour/bundle.cc +++ b/libs/ardour/bundle.cc @@ -19,14 +19,11 @@ #include -#include "pbd/failed_constructor.h" -#include "ardour/ardour.h" #include "ardour/bundle.h" #include "ardour/audioengine.h" #include "ardour/port.h" -#include "pbd/xml++.h" -#include "i18n.h" +#include "pbd/i18n.h" using namespace std; using namespace ARDOUR; @@ -70,7 +67,7 @@ Bundle::Bundle (boost::shared_ptr other) ChanCount Bundle::nchannels () const { - Glib::Mutex::Lock lm (_channel_mutex); + Glib::Threads::Mutex::Lock lm (_channel_mutex); ChanCount c; for (vector::const_iterator i = _channel.begin(); i != _channel.end(); ++i) { @@ -80,12 +77,19 @@ Bundle::nchannels () const return c; } +uint32_t +Bundle::n_total () const +{ + /* Simpler and far more efficient than nchannels.n_total() */ + return _channel.size(); +} + Bundle::PortList const & Bundle::channel_ports (uint32_t c) const { - assert (c < nchannels().n_total()); + assert (c < n_total()); - Glib::Mutex::Lock lm (_channel_mutex); + Glib::Threads::Mutex::Lock lm (_channel_mutex); return _channel[c].ports; } @@ -96,11 +100,11 @@ Bundle::channel_ports (uint32_t c) const void Bundle::add_port_to_channel (uint32_t ch, string portname) { - assert (ch < nchannels().n_total()); + assert (ch < n_total()); assert (portname.find_first_of (':') != string::npos); { - Glib::Mutex::Lock lm (_channel_mutex); + Glib::Threads::Mutex::Lock lm (_channel_mutex); _channel[ch].ports.push_back (portname); } @@ -114,12 +118,12 @@ Bundle::add_port_to_channel (uint32_t ch, string portname) void Bundle::remove_port_from_channel (uint32_t ch, string portname) { - assert (ch < nchannels().n_total()); + assert (ch < n_total()); bool changed = false; { - Glib::Mutex::Lock lm (_channel_mutex); + Glib::Threads::Mutex::Lock lm (_channel_mutex); PortList& pl = _channel[ch].ports; PortList::iterator i = find (pl.begin(), pl.end(), portname); @@ -141,11 +145,11 @@ Bundle::remove_port_from_channel (uint32_t ch, string portname) void Bundle::set_port (uint32_t ch, string portname) { - assert (ch < nchannels().n_total()); + assert (ch < n_total()); assert (portname.find_first_of (':') != string::npos); { - Glib::Mutex::Lock lm (_channel_mutex); + Glib::Threads::Mutex::Lock lm (_channel_mutex); _channel[ch].ports.clear (); _channel[ch].ports.push_back (portname); } @@ -158,7 +162,7 @@ void Bundle::add_channel (std::string const & n, DataType t) { { - Glib::Mutex::Lock lm (_channel_mutex); + Glib::Threads::Mutex::Lock lm (_channel_mutex); _channel.push_back (Channel (n, t)); } @@ -170,7 +174,7 @@ void Bundle::add_channel (std::string const & n, DataType t, PortList p) { { - Glib::Mutex::Lock lm (_channel_mutex); + Glib::Threads::Mutex::Lock lm (_channel_mutex); _channel.push_back (Channel (n, t, p)); } @@ -182,7 +186,7 @@ void Bundle::add_channel (std::string const & n, DataType t, std::string const & p) { { - Glib::Mutex::Lock lm (_channel_mutex); + Glib::Threads::Mutex::Lock lm (_channel_mutex); _channel.push_back (Channel (n, t, p)); } @@ -192,9 +196,9 @@ Bundle::add_channel (std::string const & n, DataType t, std::string const & p) bool Bundle::port_attached_to_channel (uint32_t ch, std::string portname) { - assert (ch < nchannels().n_total()); + assert (ch < n_total()); - Glib::Mutex::Lock lm (_channel_mutex); + Glib::Threads::Mutex::Lock lm (_channel_mutex); return (std::find (_channel[ch].ports.begin (), _channel[ch].ports.end (), portname) != _channel[ch].ports.end ()); } @@ -204,19 +208,25 @@ Bundle::port_attached_to_channel (uint32_t ch, std::string portname) void Bundle::remove_channel (uint32_t ch) { - assert (ch < nchannels().n_total()); + assert (ch < n_total()); - Glib::Mutex::Lock lm (_channel_mutex); + Glib::Threads::Mutex::Lock lm (_channel_mutex); _channel.erase (_channel.begin () + ch); + + lm.release(); + emit_changed (ConfigurationChanged); } /** Remove all channels */ void Bundle::remove_channels () { - Glib::Mutex::Lock lm (_channel_mutex); + Glib::Threads::Mutex::Lock lm (_channel_mutex); _channel.clear (); + + lm.release(); + emit_changed (ConfigurationChanged); } /** @param p Port name. @@ -225,7 +235,7 @@ Bundle::remove_channels () bool Bundle::offers_port (std::string p) const { - Glib::Mutex::Lock lm (_channel_mutex); + Glib::Threads::Mutex::Lock lm (_channel_mutex); for (std::vector::const_iterator i = _channel.begin(); i != _channel.end(); ++i) { for (PortList::const_iterator j = i->ports.begin(); j != i->ports.end(); ++j) { @@ -244,7 +254,7 @@ Bundle::offers_port (std::string p) const bool Bundle::offers_port_alone (std::string p) const { - Glib::Mutex::Lock lm (_channel_mutex); + Glib::Threads::Mutex::Lock lm (_channel_mutex); for (std::vector::const_iterator i = _channel.begin(); i != _channel.end(); ++i) { if (i->ports.size() == 1 && i->ports[0] == p) { @@ -262,9 +272,9 @@ Bundle::offers_port_alone (std::string p) const std::string Bundle::channel_name (uint32_t ch) const { - assert (ch < nchannels().n_total()); + assert (ch < n_total()); - Glib::Mutex::Lock lm (_channel_mutex); + Glib::Threads::Mutex::Lock lm (_channel_mutex); return _channel[ch].name; } @@ -275,10 +285,10 @@ Bundle::channel_name (uint32_t ch) const void Bundle::set_channel_name (uint32_t ch, std::string const & n) { - assert (ch < nchannels().n_total()); + assert (ch < n_total()); { - Glib::Mutex::Lock lm (_channel_mutex); + Glib::Threads::Mutex::Lock lm (_channel_mutex); _channel[ch].name = n; } @@ -292,9 +302,9 @@ Bundle::set_channel_name (uint32_t ch, std::string const & n) void Bundle::add_channels_from_bundle (boost::shared_ptr other) { - uint32_t const ch = nchannels().n_total(); + uint32_t const ch = n_total(); - for (uint32_t i = 0; i < other->nchannels().n_total(); ++i) { + for (uint32_t i = 0; i < other->n_total(); ++i) { std::stringstream s; s << other->name() << " " << other->channel_name(i); @@ -312,20 +322,37 @@ Bundle::add_channels_from_bundle (boost::shared_ptr other) * with another bundle's channels. * @param other Other bundle. * @param engine AudioEngine to use to make the connections. + * @param allow_partial whether to allow leaving unconnected channels types, + * or require that the ChanCounts match exactly (default false). */ void -Bundle::connect (boost::shared_ptr other, AudioEngine & engine) +Bundle::connect (boost::shared_ptr other, AudioEngine & engine, + bool allow_partial) { - uint32_t const N = nchannels().n_total(); - assert (N == other->nchannels().n_total()); + ChanCount our_count = nchannels(); + ChanCount other_count = other->nchannels(); - for (uint32_t i = 0; i < N; ++i) { - Bundle::PortList const & our_ports = channel_ports (i); - Bundle::PortList const & other_ports = other->channel_ports (i); + if (!allow_partial && our_count != other_count) { + assert (our_count == other_count); + return; + } - for (Bundle::PortList::const_iterator j = our_ports.begin(); j != our_ports.end(); ++j) { - for (Bundle::PortList::const_iterator k = other_ports.begin(); k != other_ports.end(); ++k) { - engine.connect (*j, *k); + for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) { + uint32_t N = our_count.n(*t); + if (N != other_count.n(*t)) + continue; + for (uint32_t i = 0; i < N; ++i) { + Bundle::PortList const & our_ports = + channel_ports (type_channel_to_overall(*t, i)); + Bundle::PortList const & other_ports = + other->channel_ports (other->type_channel_to_overall(*t, i)); + + for (Bundle::PortList::const_iterator j = our_ports.begin(); + j != our_ports.end(); ++j) { + for (Bundle::PortList::const_iterator k = other_ports.begin(); + k != other_ports.end(); ++k) { + engine.connect (*j, *k); + } } } } @@ -334,16 +361,23 @@ Bundle::connect (boost::shared_ptr other, AudioEngine & engine) void Bundle::disconnect (boost::shared_ptr other, AudioEngine & engine) { - uint32_t const N = nchannels().n_total(); - assert (N == other->nchannels().n_total()); - - for (uint32_t i = 0; i < N; ++i) { - Bundle::PortList const & our_ports = channel_ports (i); - Bundle::PortList const & other_ports = other->channel_ports (i); - - for (Bundle::PortList::const_iterator j = our_ports.begin(); j != our_ports.end(); ++j) { - for (Bundle::PortList::const_iterator k = other_ports.begin(); k != other_ports.end(); ++k) { - engine.disconnect (*j, *k); + ChanCount our_count = nchannels(); + ChanCount other_count = other->nchannels(); + + for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) { + uint32_t N = min(our_count.n(*t), other_count.n(*t)); + for (uint32_t i = 0; i < N; ++i) { + Bundle::PortList const & our_ports = + channel_ports (type_channel_to_overall(*t, i)); + Bundle::PortList const & other_ports = + other->channel_ports (other->type_channel_to_overall(*t, i)); + + for (Bundle::PortList::const_iterator j = our_ports.begin(); + j != our_ports.end(); ++j) { + for (Bundle::PortList::const_iterator k = other_ports.begin(); + k != other_ports.end(); ++k) { + engine.disconnect (*j, *k); + } } } } @@ -354,8 +388,8 @@ void Bundle::remove_ports_from_channels () { { - Glib::Mutex::Lock lm (_channel_mutex); - for (uint32_t c = 0; c < _channel.size(); ++c) { + Glib::Threads::Mutex::Lock lm (_channel_mutex); + for (uint32_t c = 0; c < n_total(); ++c) { _channel[c].ports.clear (); } @@ -370,10 +404,10 @@ Bundle::remove_ports_from_channels () void Bundle::remove_ports_from_channel (uint32_t ch) { - assert (ch < nchannels().n_total()); + assert (ch < n_total()); { - Glib::Mutex::Lock lm (_channel_mutex); + Glib::Threads::Mutex::Lock lm (_channel_mutex); _channel[ch].ports.clear (); } @@ -407,60 +441,91 @@ Bundle::emit_changed (Change c) } } +/** This must not be called in code executed as a response to a backend event, + * as it may query the backend in the same thread where it's waiting for us. + * @return true if a Bundle is connected to another. + * @param type: if not NIL, restrict the check to channels of that type. + * @param exclusive: if true, additionally check if the bundle is connected + * only to |other|, and return false if not. */ bool -Bundle::connected_to (boost::shared_ptr other, AudioEngine & engine) +Bundle::connected_to (boost::shared_ptr other, AudioEngine & engine, + DataType type, bool exclusive) { - if (_ports_are_inputs == other->_ports_are_inputs || nchannels() != other->nchannels()) { + if (_ports_are_inputs == other->_ports_are_inputs) return false; + + if (type == DataType::NIL) { + for (DataType::iterator t = DataType::begin(); + t != DataType::end(); ++t) { + if (!connected_to(other, engine, *t, exclusive)) + return false; + } + return true; } - for (uint32_t i = 0; i < nchannels().n_total(); ++i) { - Bundle::PortList const & A = channel_ports (i); - Bundle::PortList const & B = other->channel_ports (i); + uint32_t N = nchannels().n(type); + if (other->nchannels().n(type) != N) + return false; + + vector port_connections; - for (uint32_t j = 0; j < A.size(); ++j) { - for (uint32_t k = 0; k < B.size(); ++k) { + for (uint32_t i = 0; i < N; ++i) { + Bundle::PortList const & our_ports = + channel_ports (type_channel_to_overall(type, i)); + Bundle::PortList const & other_ports = + other->channel_ports (other->type_channel_to_overall(type, i)); - boost::shared_ptr p = engine.get_port_by_name (A[j]); - boost::shared_ptr q = engine.get_port_by_name (B[k]); + for (Bundle::PortList::const_iterator j = our_ports.begin(); + j != our_ports.end(); ++j) { + boost::shared_ptr p = engine.get_port_by_name(*j); + + for (Bundle::PortList::const_iterator k = other_ports.begin(); + k != other_ports.end(); ++k) { + boost::shared_ptr q = engine.get_port_by_name(*k); if (!p && !q) { return false; } - if (p && !p->connected_to (B[k])) { + if (p && !p->connected_to (*k)) { return false; - } else if (q && !q->connected_to (A[j])) { + } else if (q && !q->connected_to (*j)) { return false; } } + + if (exclusive && p) { + port_connections.clear(); + p->get_connections(port_connections); + if (port_connections.size() != other_ports.size()) + return false; + } } } return true; } -/** This must not be called in code executed as a response to a JACK event, - * as it uses jack_port_get_all_connections(). +/** This must not be called in code executed as a response to a backend event, + * as it uses the backend port_get_all_connections(). * @return true if any of this bundle's channels are connected to anything. */ bool Bundle::connected_to_anything (AudioEngine& engine) { - for (uint32_t i = 0; i < nchannels().n_total(); ++i) { + PortManager& pm (engine); + + for (uint32_t i = 0; i < n_total(); ++i) { Bundle::PortList const & ports = channel_ports (i); for (uint32_t j = 0; j < ports.size(); ++j) { - /* ports[j] may not be an Ardour port, so use JACK directly + + /* ports[j] may not be an Ardour port, so use the port manager directly rather than doing it with Port. */ - jack_port_t* jp = jack_port_by_name (engine.jack(), ports[j].c_str()); - if (jp) { - const char ** c = jack_port_get_all_connections (engine.jack(), jp); - if (c) { - jack_free (c); - return true; - } + + if (pm.connected (ports[j])) { + return true; } } } @@ -498,17 +563,22 @@ Bundle::set_name (string const & n) bool Bundle::has_same_ports (boost::shared_ptr b) const { - uint32_t const N = nchannels().n_total(); + ChanCount our_count = nchannels(); + ChanCount other_count = b->nchannels(); - if (b->nchannels().n_total() != N) { + if (our_count != other_count) return false; - } - /* XXX: probably should sort channel port lists before comparing them */ + for (DataType::iterator t = DataType::begin(); t != DataType::end(); ++t) { + uint32_t N = our_count.n(*t); + for (uint32_t i = 0; i < N; ++i) { + Bundle::PortList const & our_ports = + channel_ports (type_channel_to_overall(*t, i)); + Bundle::PortList const & other_ports = + b->channel_ports (b->type_channel_to_overall(*t, i)); - for (uint32_t i = 0; i < N; ++i) { - if (channel_ports (i) != b->channel_ports (i)) { - return false; + if (our_ports != other_ports) + return false; } } @@ -518,9 +588,9 @@ Bundle::has_same_ports (boost::shared_ptr b) const DataType Bundle::channel_type (uint32_t c) const { - assert (c < nchannels().n_total()); + assert (c < n_total()); - Glib::Mutex::Lock lm (_channel_mutex); + Glib::Threads::Mutex::Lock lm (_channel_mutex); return _channel[c].type; } @@ -528,7 +598,7 @@ ostream & operator<< (ostream& os, Bundle const & b) { os << "BUNDLE " << b.nchannels() << " channels: "; - for (uint32_t i = 0; i < b.nchannels().n_total(); ++i) { + for (uint32_t i = 0; i < b.n_total(); ++i) { os << "( "; Bundle::PortList const & pl = b.channel_ports (i); for (Bundle::PortList::const_iterator j = pl.begin(); j != pl.end(); ++j) { @@ -566,8 +636,8 @@ Bundle::type_channel_to_overall (DataType t, uint32_t c) const if (t == DataType::NIL) { return c; } - - Glib::Mutex::Lock lm (_channel_mutex); + + Glib::Threads::Mutex::Lock lm (_channel_mutex); vector::const_iterator i = _channel.begin (); @@ -589,7 +659,7 @@ Bundle::type_channel_to_overall (DataType t, uint32_t c) const ++o; } - /* NOTREACHED */ + abort(); /* NOTREACHED */ return -1; } @@ -600,11 +670,11 @@ Bundle::overall_channel_to_type (DataType t, uint32_t c) const if (t == DataType::NIL) { return c; } - - Glib::Mutex::Lock lm (_channel_mutex); + + Glib::Threads::Mutex::Lock lm (_channel_mutex); uint32_t s = 0; - + vector::const_iterator i = _channel.begin (); for (uint32_t j = 0; j < c; ++j) { if (i->type == t) {