..and CoreAudio.

This commit is contained in:
Robin Gareus 2016-04-24 20:45:50 +02:00
parent 800c8182c6
commit fde99e68f7
2 changed files with 55 additions and 46 deletions

View File

@ -497,13 +497,14 @@ CoreAudioBackend::_start (bool for_latency_measurement)
return BackendReinitializationError; return BackendReinitializationError;
} }
if (_ports.size()) { if (_ports.size () || _portmap.size ()) {
PBD::warning << _("CoreAudioBackend: recovering from unclean shutdown, port registry is not empty.") << endmsg; PBD::warning << _("CoreAudioBackend: recovering from unclean shutdown, port registry is not empty.") << endmsg;
_system_inputs.clear(); _system_inputs.clear();
_system_outputs.clear(); _system_outputs.clear();
_system_midi_in.clear(); _system_midi_in.clear();
_system_midi_out.clear(); _system_midi_out.clear();
_ports.clear(); _ports.clear();
_portmap.clear();
} }
uint32_t device1 = name_to_id(_input_audio_device); uint32_t device1 = name_to_id(_input_audio_device);
@ -970,8 +971,9 @@ CoreAudioBackend::get_ports (
use_regexp = true; use_regexp = true;
} }
} }
for (size_t i = 0; i < _ports.size (); ++i) {
CoreBackendPort* port = _ports[i]; for (PortIndex::iterator i = _ports.begin (); i != _ports.end (); ++i) {
CoreBackendPort* port = *i;
if ((port->type () == type) && flags == (port->flags () & flags)) { if ((port->type () == type) && flags == (port->flags () & flags)) {
if (!use_regexp || !regexec (&port_regex, port->name ().c_str (), 0, NULL, 0)) { if (!use_regexp || !regexec (&port_regex, port->name ().c_str (), 0, NULL, 0)) {
port_names.push_back (port->name ()); port_names.push_back (port->name ());
@ -1030,7 +1032,8 @@ CoreAudioBackend::add_port (
return 0; return 0;
} }
_ports.push_back (port); _ports.insert (port);
_portmap.insert (make_pair (name, port));
return port; return port;
} }
@ -1042,12 +1045,13 @@ CoreAudioBackend::unregister_port (PortEngine::PortHandle port_handle)
return; return;
} }
CoreBackendPort* port = static_cast<CoreBackendPort*>(port_handle); CoreBackendPort* port = static_cast<CoreBackendPort*>(port_handle);
std::vector<CoreBackendPort*>::iterator i = std::find (_ports.begin (), _ports.end (), static_cast<CoreBackendPort*>(port_handle)); PortIndex::iterator i = _ports.find (static_cast<CoreBackendPort*>(port_handle));
if (i == _ports.end ()) { if (i == _ports.end ()) {
PBD::warning << _("CoreAudioBackend::unregister_port: Failed to find port") << endmsg; PBD::warning << _("CoreAudioBackend::unregister_port: Failed to find port") << endmsg;
return; return;
} }
disconnect_all(port_handle); disconnect_all(port_handle);
_portmap.erase (port->name());
_ports.erase (i); _ports.erase (i);
delete port; delete port;
} }
@ -1200,19 +1204,19 @@ CoreAudioBackend::coremidi_rediscover()
void void
CoreAudioBackend::unregister_ports (bool system_only) CoreAudioBackend::unregister_ports (bool system_only)
{ {
size_t i = 0;
_system_inputs.clear(); _system_inputs.clear();
_system_outputs.clear(); _system_outputs.clear();
_system_midi_in.clear(); _system_midi_in.clear();
_system_midi_out.clear(); _system_midi_out.clear();
while (i < _ports.size ()) {
CoreBackendPort* port = _ports[i]; for (PortIndex::iterator i = _ports.begin (); i != _ports.end ();) {
PortIndex::iterator cur = i++;
CoreBackendPort* port = *cur;
if (! system_only || (port->is_physical () && port->is_terminal ())) { if (! system_only || (port->is_physical () && port->is_terminal ())) {
port->disconnect_all (); port->disconnect_all ();
_portmap.erase (port->name());
delete port; delete port;
_ports.erase (_ports.begin() + i); _ports.erase (cur);
} else {
++i;
} }
} }
} }
@ -1301,10 +1305,12 @@ bool
CoreAudioBackend::connected_to (PortEngine::PortHandle src, const std::string& dst, bool /*process_callback_safe*/) CoreAudioBackend::connected_to (PortEngine::PortHandle src, const std::string& dst, bool /*process_callback_safe*/)
{ {
CoreBackendPort* dst_port = find_port (dst); CoreBackendPort* dst_port = find_port (dst);
#ifndef NDEBUG
if (!valid_port (src) || !dst_port) { if (!valid_port (src) || !dst_port) {
PBD::warning << _("CoreAudioBackend::connected_to: Invalid Port") << endmsg; PBD::warning << _("CoreAudioBackend::connected_to: Invalid Port") << endmsg;
return false; return false;
} }
#endif
return static_cast<CoreBackendPort*>(src)->is_connected (dst_port); return static_cast<CoreBackendPort*>(src)->is_connected (dst_port);
} }
@ -1328,9 +1334,9 @@ CoreAudioBackend::get_connections (PortEngine::PortHandle port, std::vector<std:
assert (0 == names.size ()); assert (0 == names.size ());
const std::vector<CoreBackendPort*>& connected_ports = static_cast<CoreBackendPort*>(port)->get_connections (); const std::set<CoreBackendPort*>& connected_ports = static_cast<CoreBackendPort*>(port)->get_connections ();
for (std::vector<CoreBackendPort*>::const_iterator i = connected_ports.begin (); i != connected_ports.end (); ++i) { for (std::set<CoreBackendPort*>::const_iterator i = connected_ports.begin (); i != connected_ports.end (); ++i) {
names.push_back ((*i)->name ()); names.push_back ((*i)->name ());
} }
@ -1473,8 +1479,8 @@ CoreAudioBackend::port_is_physical (PortEngine::PortHandle port) const
void void
CoreAudioBackend::get_physical_outputs (DataType type, std::vector<std::string>& port_names) CoreAudioBackend::get_physical_outputs (DataType type, std::vector<std::string>& port_names)
{ {
for (size_t i = 0; i < _ports.size (); ++i) { for (PortIndex::iterator i = _ports.begin (); i != _ports.end (); ++i) {
CoreBackendPort* port = _ports[i]; CoreBackendPort* port = *i;
if ((port->type () == type) && port->is_input () && port->is_physical ()) { if ((port->type () == type) && port->is_input () && port->is_physical ()) {
port_names.push_back (port->name ()); port_names.push_back (port->name ());
} }
@ -1484,8 +1490,8 @@ CoreAudioBackend::get_physical_outputs (DataType type, std::vector<std::string>&
void void
CoreAudioBackend::get_physical_inputs (DataType type, std::vector<std::string>& port_names) CoreAudioBackend::get_physical_inputs (DataType type, std::vector<std::string>& port_names)
{ {
for (size_t i = 0; i < _ports.size (); ++i) { for (PortIndex::iterator i = _ports.begin (); i != _ports.end (); ++i) {
CoreBackendPort* port = _ports[i]; CoreBackendPort* port = *i;
if ((port->type () == type) && port->is_output () && port->is_physical ()) { if ((port->type () == type) && port->is_output () && port->is_physical ()) {
port_names.push_back (port->name ()); port_names.push_back (port->name ());
} }
@ -1497,8 +1503,8 @@ CoreAudioBackend::n_physical_outputs () const
{ {
int n_midi = 0; int n_midi = 0;
int n_audio = 0; int n_audio = 0;
for (size_t i = 0; i < _ports.size (); ++i) { for (PortIndex::iterator i = _ports.begin (); i != _ports.end (); ++i) {
CoreBackendPort* port = _ports[i]; CoreBackendPort* port = *i;
if (port->is_output () && port->is_physical ()) { if (port->is_output () && port->is_physical ()) {
switch (port->type ()) { switch (port->type ()) {
case DataType::AUDIO: ++n_audio; break; case DataType::AUDIO: ++n_audio; break;
@ -1518,8 +1524,8 @@ CoreAudioBackend::n_physical_inputs () const
{ {
int n_midi = 0; int n_midi = 0;
int n_audio = 0; int n_audio = 0;
for (size_t i = 0; i < _ports.size (); ++i) { for (PortIndex::iterator i = _ports.begin (); i != _ports.end (); ++i) {
CoreBackendPort* port = _ports[i]; CoreBackendPort* port = *i;
if (port->is_input () && port->is_physical ()) { if (port->is_input () && port->is_physical ()) {
switch (port->type ()) { switch (port->type ()) {
case DataType::AUDIO: ++n_audio; break; case DataType::AUDIO: ++n_audio; break;
@ -1976,7 +1982,7 @@ int CoreBackendPort::connect (CoreBackendPort *port)
void CoreBackendPort::_connect (CoreBackendPort *port, bool callback) void CoreBackendPort::_connect (CoreBackendPort *port, bool callback)
{ {
_connections.push_back (port); _connections.insert (port);
if (callback) { if (callback) {
port->_connect (this, false); port->_connect (this, false);
_osx_backend.port_connect_callback (name(), port->name(), true); _osx_backend.port_connect_callback (name(), port->name(), true);
@ -2002,12 +2008,9 @@ int CoreBackendPort::disconnect (CoreBackendPort *port)
void CoreBackendPort::_disconnect (CoreBackendPort *port, bool callback) void CoreBackendPort::_disconnect (CoreBackendPort *port, bool callback)
{ {
std::vector<CoreBackendPort*>::iterator it = std::find (_connections.begin (), _connections.end (), port); std::set<CoreBackendPort*>::iterator it = _connections.find (port);
assert (it != _connections.end ()); assert (it != _connections.end ());
_connections.erase (it); _connections.erase (it);
if (callback) { if (callback) {
port->_disconnect (this, false); port->_disconnect (this, false);
_osx_backend.port_connect_callback (name(), port->name(), false); _osx_backend.port_connect_callback (name(), port->name(), false);
@ -2018,21 +2021,22 @@ void CoreBackendPort::_disconnect (CoreBackendPort *port, bool callback)
void CoreBackendPort::disconnect_all () void CoreBackendPort::disconnect_all ()
{ {
while (!_connections.empty ()) { while (!_connections.empty ()) {
_connections.back ()->_disconnect (this, false); std::set<CoreBackendPort*>::iterator it = _connections.begin ();
_osx_backend.port_connect_callback (name(), _connections.back ()->name(), false); (*it)->_disconnect (this, false);
_connections.pop_back (); _osx_backend.port_connect_callback (name(), (*it)->name(), false);
_connections.erase (it);
} }
} }
bool bool
CoreBackendPort::is_connected (const CoreBackendPort *port) const CoreBackendPort::is_connected (const CoreBackendPort *port) const
{ {
return std::find (_connections.begin (), _connections.end (), port) != _connections.end (); return _connections.find (const_cast<CoreBackendPort *>(port)) != _connections.end ();
} }
bool CoreBackendPort::is_physically_connected () const bool CoreBackendPort::is_physically_connected () const
{ {
for (std::vector<CoreBackendPort*>::const_iterator it = _connections.begin (); it != _connections.end (); ++it) { for (std::set<CoreBackendPort*>::const_iterator it = _connections.begin (); it != _connections.end (); ++it) {
if ((*it)->is_physical ()) { if ((*it)->is_physical ()) {
return true; return true;
} }
@ -2054,14 +2058,15 @@ CoreAudioPort::~CoreAudioPort () { }
void* CoreAudioPort::get_buffer (pframes_t n_samples) void* CoreAudioPort::get_buffer (pframes_t n_samples)
{ {
if (is_input ()) { if (is_input ()) {
std::vector<CoreBackendPort*>::const_iterator it = get_connections ().begin (); const std::set<CoreBackendPort *>& connections = get_connections ();
if (it == get_connections ().end ()) { std::set<CoreBackendPort*>::const_iterator it = connections.begin ();
if (it == connections.end ()) {
memset (_buffer, 0, n_samples * sizeof (Sample)); memset (_buffer, 0, n_samples * sizeof (Sample));
} else { } else {
CoreAudioPort const * source = static_cast<const CoreAudioPort*>(*it); CoreAudioPort const * source = static_cast<const CoreAudioPort*>(*it);
assert (source && source->is_output ()); assert (source && source->is_output ());
memcpy (_buffer, source->const_buffer (), n_samples * sizeof (Sample)); memcpy (_buffer, source->const_buffer (), n_samples * sizeof (Sample));
while (++it != get_connections ().end ()) { while (++it != connections.end ()) {
source = static_cast<const CoreAudioPort*>(*it); source = static_cast<const CoreAudioPort*>(*it);
assert (source && source->is_output ()); assert (source && source->is_output ());
Sample* dst = buffer (); Sample* dst = buffer ();
@ -2104,8 +2109,9 @@ void* CoreMidiPort::get_buffer (pframes_t /* nframes */)
{ {
if (is_input ()) { if (is_input ()) {
(_buffer[_bufperiod]).clear (); (_buffer[_bufperiod]).clear ();
for (std::vector<CoreBackendPort*>::const_iterator i = get_connections ().begin (); const std::set<CoreBackendPort*>& connections = get_connections ();
i != get_connections ().end (); for (std::set<CoreBackendPort*>::const_iterator i = connections.begin ();
i != connections.end ();
++i) { ++i) {
const CoreMidiBuffer * src = static_cast<const CoreMidiPort*>(*i)->const_buffer (); const CoreMidiBuffer * src = static_cast<const CoreMidiPort*>(*i)->const_buffer ();
for (CoreMidiBuffer::const_iterator it = src->begin (); it != src->end (); ++it) { for (CoreMidiBuffer::const_iterator it = src->begin (); it != src->end (); ++it) {

View File

@ -82,7 +82,7 @@ class CoreBackendPort {
bool is_connected (const CoreBackendPort *port) const; bool is_connected (const CoreBackendPort *port) const;
bool is_physically_connected () const; bool is_physically_connected () const;
const std::vector<CoreBackendPort *>& get_connections () const { return _connections; } const std::set<CoreBackendPort *>& get_connections () const { return _connections; }
int connect (CoreBackendPort *port); int connect (CoreBackendPort *port);
int disconnect (CoreBackendPort *port); int disconnect (CoreBackendPort *port);
@ -114,7 +114,7 @@ class CoreBackendPort {
const PortFlags _flags; const PortFlags _flags;
LatencyRange _capture_latency_range; LatencyRange _capture_latency_range;
LatencyRange _playback_latency_range; LatencyRange _playback_latency_range;
std::vector<CoreBackendPort*> _connections; std::set<CoreBackendPort*> _connections;
void _connect (CoreBackendPort* , bool); void _connect (CoreBackendPort* , bool);
void _disconnect (CoreBackendPort* , bool); void _disconnect (CoreBackendPort* , bool);
@ -461,12 +461,16 @@ class CoreAudioBackend : public AudioBackend {
int register_system_audio_ports (); int register_system_audio_ports ();
void unregister_ports (bool system_only = false); void unregister_ports (bool system_only = false);
std::vector<CoreBackendPort *> _ports;
std::vector<CoreBackendPort *> _system_inputs; std::vector<CoreBackendPort *> _system_inputs;
std::vector<CoreBackendPort *> _system_outputs; std::vector<CoreBackendPort *> _system_outputs;
std::vector<CoreBackendPort *> _system_midi_in; std::vector<CoreBackendPort *> _system_midi_in;
std::vector<CoreBackendPort *> _system_midi_out; std::vector<CoreBackendPort *> _system_midi_out;
typedef std::map<std::string, CoreBackendPort *> PortMap; // fast lookup in _ports
typedef std::set<CoreBackendPort *> PortIndex; // fast lookup in _ports
PortMap _portmap;
PortIndex _ports;
struct PortConnectData { struct PortConnectData {
std::string a; std::string a;
std::string b; std::string b;
@ -493,16 +497,15 @@ class CoreAudioBackend : public AudioBackend {
} }
bool valid_port (PortHandle port) const { bool valid_port (PortHandle port) const {
return std::find (_ports.begin (), _ports.end (), (CoreBackendPort*)port) != _ports.end (); return _ports.find (static_cast<CoreBackendPort*>(port)) != _ports.end ();
} }
CoreBackendPort * find_port (const std::string& port_name) const { CoreBackendPort* find_port (const std::string& port_name) const {
for (std::vector<CoreBackendPort*>::const_iterator it = _ports.begin (); it != _ports.end (); ++it) { PortMap::const_iterator it = _portmap.find (port_name);
if ((*it)->name () == port_name) { if (it == _portmap.end()) {
return *it; return NULL;
}
} }
return NULL; return (*it).second;
} }
CoreBackendPort * find_port_in (std::vector<CoreBackendPort *> plist, const std::string& port_name) const { CoreBackendPort * find_port_in (std::vector<CoreBackendPort *> plist, const std::string& port_name) const {