diff --git a/libs/backends/alsa/alsa_audiobackend.cc b/libs/backends/alsa/alsa_audiobackend.cc index 0d598a4192..8e3958ee4e 100644 --- a/libs/backends/alsa/alsa_audiobackend.cc +++ b/libs/backends/alsa/alsa_audiobackend.cc @@ -55,6 +55,7 @@ ALSADeviceInfo AlsaAudioBackend::_output_audio_device_info; AlsaAudioBackend::AlsaAudioBackend (AudioEngine& e, AudioBackendInfo& info) : AudioBackend (e, info) + , PortEngineSharedImpl (e, s_instance_name) , _pcmi (0) , _run (false) , _active (false) @@ -75,8 +76,6 @@ AlsaAudioBackend::AlsaAudioBackend (AudioEngine& e, AudioBackendInfo& info) , _midi_device_thread_active (false) , _dsp_load (0) , _processed_samples (0) - , _portmap (new PortMap) - , _ports (new PortIndex) , _port_change_flag (false) { _instance_name = s_instance_name; @@ -465,12 +464,12 @@ AlsaAudioBackend::update_systemic_audio_latencies () LatencyRange lr; lr.min = lr.max = lcpp + (_measure_latency ? 0 : _systemic_audio_input_latency); - for (std::vector::const_iterator it = _system_outputs.begin (); it != _system_outputs.end (); ++it) { + for (std::vector::const_iterator it = _system_outputs.begin (); it != _system_outputs.end (); ++it) { set_latency_range (*it, true, lr); } lr.min = lr.max = (_measure_latency ? 0 : _systemic_audio_output_latency); - for (std::vector::const_iterator it = _system_inputs.begin (); it != _system_inputs.end (); ++it) { + for (std::vector::const_iterator it = _system_inputs.begin (); it != _system_inputs.end (); ++it) { set_latency_range (*it, false, lr); } update_latencies (); @@ -481,7 +480,7 @@ AlsaAudioBackend::update_systemic_midi_latencies () { pthread_mutex_lock (&_device_port_mutex); uint32_t i = 0; - for (std::vector::iterator it = _system_midi_out.begin (); it != _system_midi_out.end (); ++it, ++i) { + for (std::vector::iterator it = _system_midi_out.begin (); it != _system_midi_out.end (); ++it, ++i) { assert (_rmidi_out.size() > i); AlsaMidiOut *rm = _rmidi_out.at(i); struct AlsaMidiDeviceInfo * nfo = midi_device_info (rm->name()); @@ -492,7 +491,7 @@ AlsaAudioBackend::update_systemic_midi_latencies () } i = 0; - for (std::vector::const_iterator it = _system_midi_in.begin (); it != _system_midi_in.end (); ++it, ++i) { + for (std::vector::const_iterator it = _system_midi_in.begin (); it != _system_midi_in.end (); ++it, ++i) { assert (_rmidi_in.size() > i); AlsaMidiIO *rm = _rmidi_in.at(i); struct AlsaMidiDeviceInfo * nfo = midi_device_info (rm->name()); @@ -686,7 +685,7 @@ AlsaAudioBackend::set_midi_device_enabled (std::string const device, bool enable // remove all ports provided by the given device pthread_mutex_lock (&_device_port_mutex); uint32_t i = 0; - for (std::vector::iterator it = _system_midi_out.begin (); it != _system_midi_out.end ();) { + for (std::vector::iterator it = _system_midi_out.begin (); it != _system_midi_out.end ();) { assert (_rmidi_out.size() > i); AlsaMidiOut *rm = _rmidi_out.at(i); if (rm->name () != device) { ++it; ++i; continue; } @@ -699,7 +698,7 @@ AlsaAudioBackend::set_midi_device_enabled (std::string const device, bool enable } i = 0; - for (std::vector::iterator it = _system_midi_in.begin (); it != _system_midi_in.end ();) { + for (std::vector::iterator it = _system_midi_in.begin (); it != _system_midi_in.end ();) { assert (_rmidi_in.size() > i); AlsaMidiIn *rm = _rmidi_in.at(i); if (rm->name () != device) { ++it; ++i; continue; } @@ -755,23 +754,7 @@ AlsaAudioBackend::_start (bool for_latency_measurement) return BackendReinitializationError; } - { - RCUWriter index_writer (_ports); - RCUWriter map_writer (_portmap); - - boost::shared_ptr ps = index_writer.get_copy(); - boost::shared_ptr pm = map_writer.get_copy (); - - if (ps->size () || pm->size ()) { - PBD::warning << _("AlsaAudioBackend: recovering from unclean shutdown, port registry is not empty.") << endmsg; - _system_inputs.clear(); - _system_outputs.clear(); - _system_midi_in.clear(); - _system_midi_out.clear(); - ps->clear(); - pm->clear(); - } - } + clear_ports (); /* reset internal state */ _dsp_load = 0; @@ -1191,219 +1174,6 @@ AlsaAudioBackend::my_name () const return _instance_name; } -uint32_t -AlsaAudioBackend::port_name_size () const -{ - return 256; -} - -int -AlsaAudioBackend::set_port_name (PortEngine::PortHandle port, const std::string& name) -{ - std::string newname (_instance_name + ":" + name); - if (!valid_port (port)) { - PBD::error << _("AlsaBackend::set_port_name: Invalid Port") << endmsg; - return -1; - } - if (find_port (newname)) { - PBD::error << _("AlsaBackend::set_port_name: Port with given name already exists") << endmsg; - return -1; - } - - AlsaPort* p = static_cast(port); - - { - RCUWriter map_writer (_portmap); - boost::shared_ptr pm = map_writer.get_copy (); - - pm->erase (p->name()); - pm->insert (make_pair (newname, p)); - } - - return p->set_name (newname); -} - -std::string -AlsaAudioBackend::get_port_name (PortEngine::PortHandle port) const -{ - if (!valid_port (port)) { - PBD::warning << _("AlsaBackend::get_port_name: Invalid Port(s)") << endmsg; - return std::string (); - } - return static_cast(port)->name (); -} - -PortFlags -AlsaAudioBackend::get_port_flags (PortEngine::PortHandle port) const -{ - if (!valid_port (port)) { - PBD::warning << _("AlsaBackend::get_port_flags: Invalid Port(s)") << endmsg; - return PortFlags (0); - } - return static_cast(port)->flags (); -} - -int -AlsaAudioBackend::get_port_property (PortHandle port, const std::string& key, std::string& value, std::string& type) const -{ - if (!valid_port (port)) { - PBD::warning << _("AlsaBackend::get_port_property: Invalid Port(s)") << endmsg; - return -1; - } - if (key == "http://jackaudio.org/metadata/pretty-name") { - type = ""; - value = static_cast(port)->pretty_name (); - if (!value.empty()) { - return 0; - } - } - return -1; -} - -int -AlsaAudioBackend::set_port_property (PortHandle port, const std::string& key, const std::string& value, const std::string& type) -{ - if (!valid_port (port)) { - PBD::warning << _("AlsaBackend::set_port_property: Invalid Port(s)") << endmsg; - return -1; - } - if (key == "http://jackaudio.org/metadata/pretty-name" && type.empty ()) { - static_cast(port)->set_pretty_name (value); - return 0; - } - return -1; -} - -PortEngine::PortHandle -AlsaAudioBackend::get_port_by_name (const std::string& name) const -{ - PortHandle port = (PortHandle) find_port (name); - return port; -} - -int -AlsaAudioBackend::get_ports ( - const std::string& port_name_pattern, - DataType type, PortFlags flags, - std::vector& port_names) const -{ - int rv = 0; - regex_t port_regex; - bool use_regexp = false; - if (port_name_pattern.size () > 0) { - if (!regcomp (&port_regex, port_name_pattern.c_str (), REG_EXTENDED|REG_NOSUB)) { - use_regexp = true; - } - } - - boost::shared_ptr p = _ports.reader (); - - for (PortIndex::const_iterator i = p->begin (); i != p->end (); ++i) { - AlsaPort* port = *i; - if ((port->type () == type) && flags == (port->flags () & flags)) { - if (!use_regexp || !regexec (&port_regex, port->name ().c_str (), 0, NULL, 0)) { - port_names.push_back (port->name ()); - ++rv; - } - } - } - if (use_regexp) { - regfree (&port_regex); - } - return rv; -} - -DataType -AlsaAudioBackend::port_data_type (PortEngine::PortHandle port) const -{ - if (!valid_port (port)) { - return DataType::NIL; - } - return static_cast(port)->type (); -} - -PortEngine::PortHandle -AlsaAudioBackend::register_port ( - const std::string& name, - ARDOUR::DataType type, - ARDOUR::PortFlags flags) -{ - if (name.size () == 0) { return 0; } - if (flags & IsPhysical) { return 0; } - return add_port (_instance_name + ":" + name, type, flags); -} - -PortEngine::PortHandle -AlsaAudioBackend::add_port ( - const std::string& name, - ARDOUR::DataType type, - ARDOUR::PortFlags flags) -{ - assert(name.size ()); - if (find_port (name)) { - PBD::error << _("AlsaBackend::register_port: Port already exists:") - << " (" << name << ")" << endmsg; - return 0; - } - AlsaPort* port = NULL; - switch (type) { - case DataType::AUDIO: - port = new AlsaAudioPort (*this, name, flags); - break; - case DataType::MIDI: - port = new AlsaMidiPort (*this, name, flags); - break; - default: - PBD::error << _("AlsaBackend::register_port: Invalid Data Type.") << endmsg; - return 0; - } - - { - RCUWriter index_writer (_ports); - RCUWriter map_writer (_portmap); - - boost::shared_ptr ps = index_writer.get_copy (); - boost::shared_ptr pm = map_writer.get_copy (); - - ps->insert (port); - pm->insert (make_pair (name, port)); - } - - return port; -} - -void -AlsaAudioBackend::unregister_port (PortEngine::PortHandle port_handle) -{ - if (!_run) { - return; - } - - AlsaPort* port = static_cast(port_handle); - - { - RCUWriter index_writer (_ports); - RCUWriter map_writer (_portmap); - - boost::shared_ptr ps = index_writer.get_copy (); - boost::shared_ptr pm = map_writer.get_copy (); - - PortIndex::iterator i = std::find (ps->begin(), ps->end(), static_cast(port_handle)); - - if (i == ps->end ()) { - PBD::error << _("AlsaBackend::unregister_port: Failed to find port") << endmsg; - return; - } - - disconnect_all(port_handle); - - pm->erase (port->name()); - ps->erase (i); - } - - delete port; -} - int AlsaAudioBackend::register_system_audio_ports() { @@ -1422,7 +1192,7 @@ AlsaAudioBackend::register_system_audio_ports() PortHandle p = add_port(std::string(tmp), DataType::AUDIO, static_cast(IsOutput | IsPhysical | IsTerminal)); if (!p) return -1; set_latency_range (p, false, lr); - AlsaPort *ap = static_cast(p); + BackendPort *ap = static_cast(p); //ap->set_pretty_name ("") _system_inputs.push_back (ap); } @@ -1434,7 +1204,7 @@ AlsaAudioBackend::register_system_audio_ports() PortHandle p = add_port(std::string(tmp), DataType::AUDIO, static_cast(IsInput | IsPhysical | IsTerminal)); if (!p) return -1; set_latency_range (p, true, lr); - AlsaPort *ap = static_cast(p); + BackendPort *ap = static_cast(p); //ap->set_pretty_name ("") _system_outputs.push_back (ap); } @@ -1583,18 +1353,18 @@ AlsaAudioBackend::stop_listen_for_midi_device_changes () void AlsaAudioBackend::update_system_port_latecies () { - for (std::vector::const_iterator it = _system_inputs.begin (); it != _system_inputs.end (); ++it) { + for (std::vector::const_iterator it = _system_inputs.begin (); it != _system_inputs.end (); ++it) { (*it)->update_connected_latency (true); } - for (std::vector::const_iterator it = _system_outputs.begin (); it != _system_outputs.end (); ++it) { + for (std::vector::const_iterator it = _system_outputs.begin (); it != _system_outputs.end (); ++it) { (*it)->update_connected_latency (false); } pthread_mutex_lock (&_device_port_mutex); - for (std::vector::const_iterator it = _system_midi_in.begin (); it != _system_midi_in.end (); ++it) { + for (std::vector::const_iterator it = _system_midi_in.begin (); it != _system_midi_in.end (); ++it) { (*it)->update_connected_latency (true); } - for (std::vector::const_iterator it = _system_midi_out.begin (); it != _system_midi_out.end (); ++it) { + for (std::vector::const_iterator it = _system_midi_out.begin (); it != _system_midi_out.end (); ++it) { (*it)->update_connected_latency (false); } pthread_mutex_unlock (&_device_port_mutex); @@ -1603,10 +1373,10 @@ AlsaAudioBackend::update_system_port_latecies () if ((*s)->dead) { continue; } - for (std::vector::const_iterator it = (*s)->inputs.begin (); it != (*s)->inputs.end (); ++it) { + for (std::vector::const_iterator it = (*s)->inputs.begin (); it != (*s)->inputs.end (); ++it) { (*it)->update_connected_latency (true); } - for (std::vector::const_iterator it = (*s)->outputs.begin (); it != (*s)->outputs.end (); ++it) { + for (std::vector::const_iterator it = (*s)->outputs.begin (); it != (*s)->outputs.end (); ++it) { (*it)->update_connected_latency (false); } } @@ -1703,7 +1473,7 @@ AlsaAudioBackend::register_system_midi_ports(const std::string device) lr.min = lr.max = (nfo->systemic_output_latency); set_latency_range (p, true, lr); static_cast(p)->set_n_periods(_periods_per_cycle); // TODO check MIDI alignment - AlsaPort *ap = static_cast(p); + BackendPort *ap = static_cast(p); ap->set_pretty_name (replace_name_io (i->first, false)); pthread_mutex_lock (&_device_port_mutex); _system_midi_out.push_back (ap); @@ -1750,7 +1520,7 @@ AlsaAudioBackend::register_system_midi_ports(const std::string device) LatencyRange lr; lr.min = lr.max = (nfo->systemic_input_latency); set_latency_range (p, false, lr); - AlsaPort *ap = static_cast(p); + BackendPort *ap = static_cast(p); ap->set_pretty_name (replace_name_io (i->first, true)); pthread_mutex_lock (&_device_port_mutex); _system_midi_in.push_back (ap); @@ -1762,155 +1532,6 @@ AlsaAudioBackend::register_system_midi_ports(const std::string device) return 0; } -void -AlsaAudioBackend::unregister_ports (bool system_only) -{ - _system_inputs.clear(); - _system_outputs.clear(); - _system_midi_in.clear(); - _system_midi_out.clear(); - - RCUWriter index_writer (_ports); - RCUWriter map_writer (_portmap); - - boost::shared_ptr ps = index_writer.get_copy (); - boost::shared_ptr pm = map_writer.get_copy (); - - - for (PortIndex::iterator i = ps->begin (); i != ps->end ();) { - PortIndex::iterator cur = i++; - AlsaPort* port = *cur; - if (! system_only || (port->is_physical () && port->is_terminal ())) { - port->disconnect_all (); - pm->erase (port->name()); - delete port; - ps->erase (cur); - } - } -} - -int -AlsaAudioBackend::connect (const std::string& src, const std::string& dst) -{ - AlsaPort* src_port = find_port (src); - AlsaPort* dst_port = find_port (dst); - - if (!src_port) { - PBD::error << _("AlsaBackend::connect: Invalid Source port:") - << " (" << src <<")" << endmsg; - return -1; - } - if (!dst_port) { - PBD::error << _("AlsaBackend::connect: Invalid Destination port:") - << " (" << dst <<")" << endmsg; - return -1; - } - return src_port->connect (dst_port); -} - -int -AlsaAudioBackend::disconnect (const std::string& src, const std::string& dst) -{ - AlsaPort* src_port = find_port (src); - AlsaPort* dst_port = find_port (dst); - - if (!src_port || !dst_port) { - PBD::error << _("AlsaBackend::disconnect: Invalid Port(s)") << endmsg; - return -1; - } - return src_port->disconnect (dst_port); -} - -int -AlsaAudioBackend::connect (PortEngine::PortHandle src, const std::string& dst) -{ - AlsaPort* dst_port = find_port (dst); - if (!valid_port (src)) { - PBD::error << _("AlsaBackend::connect: Invalid Source Port Handle") << endmsg; - return -1; - } - if (!dst_port) { - PBD::error << _("AlsaBackend::connect: Invalid Destination Port") - << " (" << dst << ")" << endmsg; - return -1; - } - return static_cast(src)->connect (dst_port); -} - -int -AlsaAudioBackend::disconnect (PortEngine::PortHandle src, const std::string& dst) -{ - AlsaPort* dst_port = find_port (dst); - if (!valid_port (src) || !dst_port) { - PBD::error << _("AlsaBackend::disconnect: Invalid Port(s)") << endmsg; - return -1; - } - return static_cast(src)->disconnect (dst_port); -} - -int -AlsaAudioBackend::disconnect_all (PortEngine::PortHandle port) -{ - if (!valid_port (port)) { - PBD::error << _("AlsaBackend::disconnect_all: Invalid Port") << endmsg; - return -1; - } - static_cast(port)->disconnect_all (); - return 0; -} - -bool -AlsaAudioBackend::connected (PortEngine::PortHandle port, bool /* process_callback_safe*/) -{ - if (!valid_port (port)) { - PBD::error << _("AlsaBackend::disconnect_all: Invalid Port") << endmsg; - return false; - } - return static_cast(port)->is_connected (); -} - -bool -AlsaAudioBackend::connected_to (PortEngine::PortHandle src, const std::string& dst, bool /*process_callback_safe*/) -{ - AlsaPort* dst_port = find_port (dst); -#ifndef NDEBUG - if (!valid_port (src) || !dst_port) { - PBD::error << _("AlsaBackend::connected_to: Invalid Port") << endmsg; - return false; - } -#endif - return static_cast(src)->is_connected (dst_port); -} - -bool -AlsaAudioBackend::physically_connected (PortEngine::PortHandle port, bool /*process_callback_safe*/) -{ - if (!valid_port (port)) { - PBD::error << _("AlsaBackend::physically_connected: Invalid Port") << endmsg; - return false; - } - return static_cast(port)->is_physically_connected (); -} - -int -AlsaAudioBackend::get_connections (PortEngine::PortHandle port, std::vector& names, bool /*process_callback_safe*/) -{ - if (!valid_port (port)) { - PBD::error << _("AlsaBackend::get_connections: Invalid Port") << endmsg; - return -1; - } - - assert (0 == names.size ()); - - const std::set& connected_ports = static_cast(port)->get_connections (); - - for (std::set::const_iterator i = connected_ports.begin (); i != connected_ports.end (); ++i) { - names.push_back ((*i)->name ()); - } - - return (int)names.size (); -} - /* MIDI */ int AlsaAudioBackend::midi_event_get ( @@ -2003,7 +1624,7 @@ AlsaAudioBackend::set_latency_range (PortEngine::PortHandle port, bool for_playb if (!valid_port (port)) { PBD::error << _("AlsaPort::set_latency_range (): invalid port.") << endmsg; } - static_cast(port)->set_latency_range (latency_range, for_playback); + static_cast(port)->set_latency_range (latency_range, for_playback); } LatencyRange @@ -2016,7 +1637,7 @@ AlsaAudioBackend::get_latency_range (PortEngine::PortHandle port, bool for_playb r.max = 0; return r; } - AlsaPort *p = static_cast(port); + BackendPort *p = static_cast(port); assert(p); r = p->latency_range (for_playback); @@ -2033,90 +1654,24 @@ AlsaAudioBackend::get_latency_range (PortEngine::PortHandle port, bool for_playb return r; } -/* Discovering physical ports */ - -bool -AlsaAudioBackend::port_is_physical (PortEngine::PortHandle port) const +BackendPort* +AlsaAudioBackend::port_factory (std::string const & name, ARDOUR::DataType type, ARDOUR::PortFlags flags) { - if (!valid_port (port)) { - PBD::error << _("AlsaPort::port_is_physical (): invalid port.") << endmsg; - return false; + BackendPort* port = 0; + + switch (type) { + case DataType::AUDIO: + port = new AlsaAudioPort (*this, name, flags); + break; + case DataType::MIDI: + port = new AlsaMidiPort (*this, name, flags); + break; + default: + PBD::error << string_compose (_("%1::register_port: Invalid Data Type."), _instance_name) << endmsg; + return 0; } - return static_cast(port)->is_physical (); -} -void -AlsaAudioBackend::get_physical_outputs (DataType type, std::vector& port_names) -{ - boost::shared_ptr p = _ports.reader(); - - for (PortIndex::iterator i = p->begin (); i != p->end (); ++i) { - AlsaPort* port = *i; - if ((port->type () == type) && port->is_input () && port->is_physical ()) { - port_names.push_back (port->name ()); - } - } -} - -void -AlsaAudioBackend::get_physical_inputs (DataType type, std::vector& port_names) -{ - boost::shared_ptr p = _ports.reader(); - - for (PortIndex::iterator i = p->begin (); i != p->end (); ++i) { - AlsaPort* port = *i; - if ((port->type () == type) && port->is_output () && port->is_physical ()) { - port_names.push_back (port->name ()); - } - } -} - -ChanCount -AlsaAudioBackend::n_physical_outputs () const -{ - int n_midi = 0; - int n_audio = 0; - - boost::shared_ptr p = _ports.reader(); - - for (PortIndex::const_iterator i = p->begin (); i != p->end (); ++i) { - AlsaPort* port = *i; - if (port->is_output () && port->is_physical ()) { - switch (port->type ()) { - case DataType::AUDIO: ++n_audio; break; - case DataType::MIDI: ++n_midi; break; - default: break; - } - } - } - ChanCount cc; - cc.set (DataType::AUDIO, n_audio); - cc.set (DataType::MIDI, n_midi); - return cc; -} - -ChanCount -AlsaAudioBackend::n_physical_inputs () const -{ - int n_midi = 0; - int n_audio = 0; - - boost::shared_ptr p = _ports.reader(); - - for (PortIndex::const_iterator i = p->begin (); i != p->end (); ++i) { - AlsaPort* port = *i; - if (port->is_input () && port->is_physical ()) { - switch (port->type ()) { - case DataType::AUDIO: ++n_audio; break; - case DataType::MIDI: ++n_midi; break; - default: break; - } - } - } - ChanCount cc; - cc.set (DataType::AUDIO, n_audio); - cc.set (DataType::MIDI, n_midi); - return cc; + return port; } /* Getting access to the data buffer for a port */ @@ -2126,7 +1681,7 @@ AlsaAudioBackend::get_buffer (PortEngine::PortHandle port, pframes_t nframes) { assert (port); assert (valid_port (port)); - return static_cast(port)->get_buffer (nframes); + return static_cast(port)->get_buffer (nframes); } /* Engine Process */ @@ -2155,7 +1710,7 @@ AlsaAudioBackend::main_process_thread () /* warm up */ int cnt = std::max (8, (int)(_samplerate / _samples_per_period) / 2); for (int w = 0; w < cnt; ++w) { - for (std::vector::const_iterator it = _system_inputs.begin (); it != _system_inputs.end (); ++it) { + for (std::vector::const_iterator it = _system_inputs.begin (); it != _system_inputs.end (); ++it) { memset ((*it)->get_buffer (_samples_per_period), 0, _samples_per_period * sizeof (Sample)); } if (engine.process_callback (_samples_per_period)) { @@ -2212,10 +1767,10 @@ AlsaAudioBackend::main_process_thread () if ((*s)->halt) { /* slave died, unregister its ports (not rt-safe, but no matter) */ PBD::error << _("ALSA Slave device halted") << endmsg; - for (std::vector::const_iterator it = (*s)->inputs.begin (); it != (*s)->inputs.end (); ++it) { + for (std::vector::const_iterator it = (*s)->inputs.begin (); it != (*s)->inputs.end (); ++it) { unregister_port (*it); } - for (std::vector::const_iterator it = (*s)->outputs.begin (); it != (*s)->outputs.end (); ++it) { + for (std::vector::const_iterator it = (*s)->outputs.begin (); it != (*s)->outputs.end (); ++it) { unregister_port (*it); } (*s)->inputs.clear (); @@ -2255,7 +1810,7 @@ AlsaAudioBackend::main_process_thread () no_proc_errors = 0; _pcmi->capt_init (_samples_per_period); - for (std::vector::const_iterator it = _system_inputs.begin (); it != _system_inputs.end (); ++it, ++i) { + for (std::vector::const_iterator it = _system_inputs.begin (); it != _system_inputs.end (); ++it, ++i) { _pcmi->capt_chan (i, (float*)((*it)->get_buffer(_samples_per_period)), _samples_per_period); } _pcmi->capt_done (_samples_per_period); @@ -2265,7 +1820,7 @@ AlsaAudioBackend::main_process_thread () continue; } i = 0; - for (std::vector::const_iterator it = (*s)->inputs.begin (); it != (*s)->inputs.end (); ++it, ++i) { + for (std::vector::const_iterator it = (*s)->inputs.begin (); it != (*s)->inputs.end (); ++it, ++i) { (*s)->capt_chan (i, (float*)((*it)->get_buffer(_samples_per_period)), _samples_per_period); } } @@ -2274,7 +1829,7 @@ AlsaAudioBackend::main_process_thread () pthread_mutex_lock (&_device_port_mutex); /* de-queue incoming midi*/ i = 0; - for (std::vector::const_iterator it = _system_midi_in.begin (); it != _system_midi_in.end (); ++it, ++i) { + for (std::vector::const_iterator it = _system_midi_in.begin (); it != _system_midi_in.end (); ++it, ++i) { assert (_rmidi_in.size() > i); AlsaMidiIn *rm = _rmidi_in.at(i); void *bptr = (*it)->get_buffer(0); @@ -2290,7 +1845,7 @@ AlsaAudioBackend::main_process_thread () } pthread_mutex_unlock (&_device_port_mutex); - for (std::vector::const_iterator it = _system_outputs.begin (); it != _system_outputs.end (); ++it) { + for (std::vector::const_iterator it = _system_outputs.begin (); it != _system_outputs.end (); ++it) { memset ((*it)->get_buffer (_samples_per_period), 0, _samples_per_period * sizeof (Sample)); } @@ -2304,13 +1859,13 @@ AlsaAudioBackend::main_process_thread () /* only used when adding/removing MIDI device/system ports */ pthread_mutex_lock (&_device_port_mutex); - for (std::vector::iterator it = _system_midi_out.begin (); it != _system_midi_out.end (); ++it) { + for (std::vector::iterator it = _system_midi_out.begin (); it != _system_midi_out.end (); ++it) { static_cast(*it)->next_period(); } /* queue outgoing midi */ i = 0; - for (std::vector::const_iterator it = _system_midi_out.begin (); it != _system_midi_out.end (); ++it, ++i) { + for (std::vector::const_iterator it = _system_midi_out.begin (); it != _system_midi_out.end (); ++it, ++i) { assert (_rmidi_out.size() > i); const AlsaMidiBuffer * src = static_cast(*it)->const_buffer(); AlsaMidiOut *rm = _rmidi_out.at(i); @@ -2324,7 +1879,7 @@ AlsaAudioBackend::main_process_thread () /* write back audio */ i = 0; _pcmi->play_init (_samples_per_period); - for (std::vector::const_iterator it = _system_outputs.begin (); it != _system_outputs.end (); ++it, ++i) { + for (std::vector::const_iterator it = _system_outputs.begin (); it != _system_outputs.end (); ++it, ++i) { _pcmi->play_chan (i, (const float*)(*it)->get_buffer (_samples_per_period), _samples_per_period); } for (; i < _pcmi->nplay (); ++i) { @@ -2337,7 +1892,7 @@ AlsaAudioBackend::main_process_thread () continue; } i = 0; - for (std::vector::const_iterator it = (*s)->outputs.begin (); it != (*s)->outputs.end (); ++it, ++i) { + for (std::vector::const_iterator it = (*s)->outputs.begin (); it != (*s)->outputs.end (); ++it, ++i) { (*s)->play_chan (i, (float*)((*it)->get_buffer(_samples_per_period)), _samples_per_period); } (*s)->cycle_end (); @@ -2365,14 +1920,14 @@ AlsaAudioBackend::main_process_thread () // Freewheelin' // zero audio input buffers - for (std::vector::const_iterator it = _system_inputs.begin (); it != _system_inputs.end (); ++it) { + for (std::vector::const_iterator it = _system_inputs.begin (); it != _system_inputs.end (); ++it) { memset ((*it)->get_buffer (_samples_per_period), 0, _samples_per_period * sizeof (Sample)); } clock1 = g_get_monotonic_time(); uint32_t i = 0; pthread_mutex_lock (&_device_port_mutex); - for (std::vector::const_iterator it = _system_midi_in.begin (); it != _system_midi_in.end (); ++it, ++i) { + for (std::vector::const_iterator it = _system_midi_in.begin (); it != _system_midi_in.end (); ++it, ++i) { static_cast((*it)->get_buffer(0))->clear (); AlsaMidiIn *rm = _rmidi_in.at(i); void *bptr = (*it)->get_buffer(0); @@ -2398,7 +1953,7 @@ AlsaAudioBackend::main_process_thread () // drop all outgoing MIDI messages pthread_mutex_lock (&_device_port_mutex); - for (std::vector::const_iterator it = _system_midi_out.begin (); it != _system_midi_out.end (); ++it) { + for (std::vector::const_iterator it = _system_midi_out.begin (); it != _system_midi_out.end (); ++it) { void *bptr = (*it)->get_buffer(0); midi_clear(bptr); } @@ -2478,7 +2033,7 @@ AlsaAudioBackend::add_slave (const char* device, } while (1); PortHandle p = add_port(std::string(tmp), DataType::AUDIO, static_cast(IsOutput | IsPhysical | IsTerminal)); if (!p) goto errout; - AlsaPort *ap = static_cast(p); + BackendPort *ap = static_cast(p); s->inputs.push_back (ap); } @@ -2494,7 +2049,7 @@ AlsaAudioBackend::add_slave (const char* device, } while (1); PortHandle p = add_port(std::string(tmp), DataType::AUDIO, static_cast(IsInput | IsPhysical | IsTerminal)); if (!p) goto errout; - AlsaPort *ap = static_cast(p); + BackendPort *ap = static_cast(p); s->outputs.push_back (ap); } @@ -2550,12 +2105,12 @@ AlsaAudioBackend::AudioSlave::update_latencies (uint32_t play, uint32_t capt) { LatencyRange lr; lr.min = lr.max = (capt); - for (std::vector::const_iterator it = inputs.begin (); it != inputs.end (); ++it) { + for (std::vector::const_iterator it = inputs.begin (); it != inputs.end (); ++it) { (*it)->set_latency_range (lr, false); } lr.min = lr.max = play; - for (std::vector::const_iterator it = outputs.begin (); it != outputs.end (); ++it) { + for (std::vector::const_iterator it = outputs.begin (); it != outputs.end (); ++it) { (*it)->set_latency_range (lr, true); } printf (" ----- SLAVE LATENCY play=%d capt=%d\n", play, capt); // XXX DEBUG @@ -2623,159 +2178,10 @@ extern "C" ARDOURBACKEND_API ARDOUR::AudioBackendInfo* descriptor () /******************************************************************************/ -AlsaPort::AlsaPort (AlsaAudioBackend &b, const std::string& name, PortFlags flags) - : _alsa_backend (b) - , _name (name) - , _flags (flags) -{ - _capture_latency_range.min = 0; - _capture_latency_range.max = 0; - _playback_latency_range.min = 0; - _playback_latency_range.max = 0; -} - -AlsaPort::~AlsaPort () { - disconnect_all (); -} - -int AlsaPort::connect (AlsaPort *port) -{ - if (!port) { - PBD::error << _("AlsaPort::connect (): invalid (null) port") << endmsg; - return -1; - } - - if (type () != port->type ()) { - PBD::error << _("AlsaPort::connect (): wrong port-type") << endmsg; - return -1; - } - - if (is_output () && port->is_output ()) { - PBD::error << _("AlsaPort::connect (): cannot inter-connect output ports.") << endmsg; - return -1; - } - - if (is_input () && port->is_input ()) { - PBD::error << _("AlsaPort::connect (): cannot inter-connect input ports.") << endmsg; - return -1; - } - - if (this == port) { - PBD::error << _("AlsaPort::connect (): cannot self-connect ports.") << endmsg; - return -1; - } - - if (is_connected (port)) { -#if 0 // don't bother to warn about this for now. just ignore it - PBD::error << _("AlsaPort::connect (): ports are already connected:") - << " (" << name () << ") -> (" << port->name () << ")" - << endmsg; -#endif - return -1; - } - - _connect (port, true); - return 0; -} - -void AlsaPort::_connect (AlsaPort *port, bool callback) -{ - _connections.insert (port); - if (callback) { - port->_connect (this, false); - _alsa_backend.port_connect_callback (name(), port->name(), true); - } -} - -int AlsaPort::disconnect (AlsaPort *port) -{ - if (!port) { - PBD::error << _("AlsaPort::disconnect (): invalid (null) port") << endmsg; - return -1; - } - - if (!is_connected (port)) { - PBD::error << _("AlsaPort::disconnect (): ports are not connected:") - << " (" << name () << ") -> (" << port->name () << ")" - << endmsg; - return -1; - } - _disconnect (port, true); - return 0; -} - -void AlsaPort::_disconnect (AlsaPort *port, bool callback) -{ - std::set::iterator it = _connections.find (port); - assert (it != _connections.end ()); - _connections.erase (it); - if (callback) { - port->_disconnect (this, false); - _alsa_backend.port_connect_callback (name(), port->name(), false); - } -} - - -void AlsaPort::disconnect_all () -{ - while (!_connections.empty ()) { - std::set::iterator it = _connections.begin (); - (*it)->_disconnect (this, false); - _alsa_backend.port_connect_callback (name(), (*it)->name(), false); - _connections.erase (it); - } -} - -bool -AlsaPort::is_connected (const AlsaPort *port) const -{ - return _connections.find (const_cast(port)) != _connections.end (); -} - -bool AlsaPort::is_physically_connected () const -{ - for (std::set::const_iterator it = _connections.begin (); it != _connections.end (); ++it) { - if ((*it)->is_physical ()) { - return true; - } - } - return false; -} - -void -AlsaPort::set_latency_range (const LatencyRange &latency_range, bool for_playback) -{ - if (for_playback) { - _playback_latency_range = latency_range; - } else { - _capture_latency_range = latency_range; - } - - for (std::set::const_iterator it = _connections.begin (); it != _connections.end (); ++it) { - if ((*it)->is_physical ()) { - (*it)->update_connected_latency (is_input ()); - } - } -} - -void -AlsaPort::update_connected_latency (bool for_playback) -{ - LatencyRange lr; - lr.min = lr.max = 0; - for (std::set::const_iterator it = _connections.begin (); it != _connections.end (); ++it) { - LatencyRange l; - l = (*it)->latency_range (for_playback); - lr.min = std::max (lr.min, l.min); - lr.max = std::max (lr.max, l.max); - } - set_latency_range (lr, for_playback); -} - /******************************************************************************/ AlsaAudioPort::AlsaAudioPort (AlsaAudioBackend &b, const std::string& name, PortFlags flags) - : AlsaPort (b, name, flags) + : BackendPort (b, name, flags) { memset (_buffer, 0, sizeof (_buffer)); mlock(_buffer, sizeof (_buffer)); @@ -2786,8 +2192,8 @@ AlsaAudioPort::~AlsaAudioPort () { } void* AlsaAudioPort::get_buffer (pframes_t n_samples) { if (is_input ()) { - const std::set& connections = get_connections (); - std::set::const_iterator it = connections.begin (); + const std::set& connections = get_connections (); + std::set::const_iterator it = connections.begin (); if (it == connections.end ()) { memset (_buffer, 0, n_samples * sizeof (Sample)); } else { @@ -2810,7 +2216,7 @@ void* AlsaAudioPort::get_buffer (pframes_t n_samples) AlsaMidiPort::AlsaMidiPort (AlsaAudioBackend &b, const std::string& name, PortFlags flags) - : AlsaPort (b, name, flags) + : BackendPort (b, name, flags) , _n_periods (1) , _bufperiod (0) { @@ -2835,8 +2241,8 @@ void* AlsaMidiPort::get_buffer (pframes_t /* nframes */) { if (is_input ()) { (_buffer[_bufperiod]).clear (); - const std::set& connections = get_connections (); - for (std::set::const_iterator i = connections.begin (); + const std::set& connections = get_connections (); + for (std::set::const_iterator i = connections.begin (); i != connections.end (); ++i) { const AlsaMidiBuffer * src = static_cast(*i)->const_buffer (); @@ -2955,3 +2361,4 @@ AlsaDeviceReservation::reservation_stdout (std::string d, size_t /* s */) _reservation_succeeded = true; } } + diff --git a/libs/backends/alsa/alsa_audiobackend.h b/libs/backends/alsa/alsa_audiobackend.h index 51783749e1..a2d32ba4fd 100644 --- a/libs/backends/alsa/alsa_audiobackend.h +++ b/libs/backends/alsa/alsa_audiobackend.h @@ -35,6 +35,7 @@ #include "ardour/audio_backend.h" #include "ardour/dsp_load_calculator.h" +#include "ardour/port_engine_shared.h" #include "ardour/system_exec.h" #include "ardour/types.h" @@ -65,61 +66,7 @@ class AlsaMidiEvent { typedef std::vector AlsaMidiBuffer; -class AlsaPort { - protected: - AlsaPort (AlsaAudioBackend &b, const std::string&, PortFlags); - public: - virtual ~AlsaPort (); - - const std::string& name () const { return _name; } - const std::string& pretty_name () const { return _pretty_name; } - PortFlags flags () const { return _flags; } - - int set_name (const std::string &name) { _name = name; return 0; } - int set_pretty_name (const std::string &name) { _pretty_name = name; return 0; } - - virtual DataType type () const = 0; - - bool is_input () const { return flags () & IsInput; } - bool is_output () const { return flags () & IsOutput; } - bool is_physical () const { return flags () & IsPhysical; } - bool is_terminal () const { return flags () & IsTerminal; } - bool is_connected () const { return _connections.size () != 0; } - bool is_connected (const AlsaPort *port) const; - bool is_physically_connected () const; - - const std::set& get_connections () const { return _connections; } - - int connect (AlsaPort *port); - int disconnect (AlsaPort *port); - void disconnect_all (); - - virtual void* get_buffer (pframes_t nframes) = 0; - - const LatencyRange latency_range (bool for_playback) const - { - return for_playback ? _playback_latency_range : _capture_latency_range; - } - - void set_latency_range (const LatencyRange &latency_range, bool for_playback); - - void update_connected_latency (bool for_playback); - - private: - AlsaAudioBackend &_alsa_backend; - std::string _name; - std::string _pretty_name; - const PortFlags _flags; - LatencyRange _capture_latency_range; - LatencyRange _playback_latency_range; - std::set _connections; - - void _connect (AlsaPort* , bool); - void _disconnect (AlsaPort* , bool); - -}; // class AlsaPort - -class AlsaAudioPort : public AlsaPort { +class AlsaAudioPort : public BackendPort { public: AlsaAudioPort (AlsaAudioBackend &b, const std::string&, PortFlags); ~AlsaAudioPort (); @@ -134,7 +81,7 @@ class AlsaAudioPort : public AlsaPort { Sample _buffer[8192]; }; // class AlsaAudioPort -class AlsaMidiPort : public AlsaPort { +class AlsaMidiPort : public BackendPort { public: AlsaMidiPort (AlsaAudioBackend &b, const std::string&, PortFlags); ~AlsaMidiPort (); @@ -170,7 +117,8 @@ class AlsaDeviceReservation bool _reservation_succeeded; }; -class AlsaAudioBackend : public AudioBackend { +class AlsaAudioBackend : public AudioBackend, public PortEngineSharedImpl +{ friend class AlsaPort; public: AlsaAudioBackend (AudioEngine& e, AudioBackendInfo& info); @@ -234,6 +182,34 @@ class AlsaAudioBackend : public AudioBackend { bool can_set_systemic_midi_latencies () const { return true; } + /* PortEngine API - forwarded to PortEngineSharedImpl */ + + bool port_is_physical (PortEngine::PortHandle ph) const { return PortEngineSharedImpl::port_is_physical (ph); } + void get_physical_outputs (DataType type, std::vector& results) { PortEngineSharedImpl::get_physical_outputs (type, results); } + void get_physical_inputs (DataType type, std::vector& results) { PortEngineSharedImpl::get_physical_inputs (type, results); } + ChanCount n_physical_outputs () const { return PortEngineSharedImpl::n_physical_outputs (); } + ChanCount n_physical_inputs () const { return PortEngineSharedImpl::n_physical_inputs (); } + uint32_t port_name_size () const { return PortEngineSharedImpl::port_name_size(); } + int set_port_name (PortEngine::PortHandle ph, const std::string& name) { return PortEngineSharedImpl::set_port_name (ph, name); } + std::string get_port_name (PortEngine::PortHandle ph) const { return PortEngineSharedImpl::get_port_name (ph); } + PortFlags get_port_flags (PortEngine::PortHandle ph) const { return PortEngineSharedImpl::get_port_flags (ph); } + PortEngine::PortHandle get_port_by_name (std::string const & name) const { return PortEngineSharedImpl::get_port_by_name (name); } + int get_port_property (PortEngine::PortHandle ph, const std::string& key, std::string& value, std::string& type) const { return PortEngineSharedImpl::get_port_property (ph, key, value, type); } + int set_port_property (PortEngine::PortHandle ph, const std::string& key, const std::string& value, const std::string& type) { return PortEngineSharedImpl::set_port_property (ph, key, value, type); } + int get_ports (const std::string& port_name_pattern, DataType type, PortFlags flags, std::vector& results) const { return PortEngineSharedImpl::get_ports (port_name_pattern, type, flags, results); } + DataType port_data_type (PortEngine::PortHandle ph) const { return PortEngineSharedImpl::port_data_type (ph); } + PortEngine::PortHandle register_port (const std::string& shortname, ARDOUR::DataType type, ARDOUR::PortFlags flags) { return PortEngineSharedImpl::register_port (shortname, type, flags); } + void unregister_port (PortHandle ph) { if (!_run) return; PortEngineSharedImpl::unregister_port (ph); } + int connect (const std::string& src, const std::string& dst) { return PortEngineSharedImpl::connect (src, dst); } + int disconnect (const std::string& src, const std::string& dst) { return PortEngineSharedImpl::disconnect (src, dst); } + int connect (PortEngine::PortHandle ph, const std::string& other) { return PortEngineSharedImpl::connect (ph, other); } + int disconnect (PortEngine::PortHandle ph, const std::string& other) { return PortEngineSharedImpl::disconnect (ph, other); } + int disconnect_all (PortEngine::PortHandle ph) { return PortEngineSharedImpl::disconnect_all (ph); } + bool connected (PortEngine::PortHandle ph, bool process_callback_safe) { return PortEngineSharedImpl::connected (ph, process_callback_safe); } + bool connected_to (PortEngine::PortHandle ph, const std::string& other, bool process_callback_safe) { return PortEngineSharedImpl::connected_to (ph, other, process_callback_safe); } + bool physically_connected (PortEngine::PortHandle ph, bool process_callback_safe) { return PortEngineSharedImpl::physically_connected (ph, process_callback_safe); } + int get_connections (PortEngine::PortHandle ph, std::vector& results, bool process_callback_safe) { return PortEngineSharedImpl::get_connections (ph, results, process_callback_safe); } + /* External control app */ std::string control_app_name () const { return std::string (); } void launch_control_app () {} @@ -272,35 +248,8 @@ class AlsaAudioBackend : public AudioBackend { void* private_handle () const; const std::string& my_name () const; - uint32_t port_name_size () const; - int set_port_name (PortHandle, const std::string&); - std::string get_port_name (PortHandle) const; - PortFlags get_port_flags (PortHandle) const; - PortHandle get_port_by_name (const std::string&) const; - - int get_port_property (PortHandle, const std::string& key, std::string& value, std::string& type) const; - int set_port_property (PortHandle, const std::string& key, const std::string& value, const std::string& type); - - int get_ports (const std::string& port_name_pattern, DataType type, PortFlags flags, std::vector&) const; - - DataType port_data_type (PortHandle) const; - - PortHandle register_port (const std::string& shortname, ARDOUR::DataType, ARDOUR::PortFlags); - void unregister_port (PortHandle); - - int connect (const std::string& src, const std::string& dst); - int disconnect (const std::string& src, const std::string& dst); - int connect (PortHandle, const std::string&); - int disconnect (PortHandle, const std::string&); - int disconnect_all (PortHandle); - - bool connected (PortHandle, bool process_callback_safe); - bool connected_to (PortHandle, const std::string&, bool process_callback_safe); - bool physically_connected (PortHandle, bool process_callback_safe); - int get_connections (PortHandle, std::vector&, bool process_callback_safe); - - /* MIDI */ + /* MIDI */ int midi_event_get (pframes_t& timestamp, size_t& size, uint8_t const** buf, void* port_buffer, uint32_t event_index); int midi_event_put (void* port_buffer, pframes_t timestamp, const uint8_t* buffer, size_t size); uint32_t get_midi_event_count (void* port_buffer); @@ -318,14 +267,6 @@ class AlsaAudioBackend : public AudioBackend { void set_latency_range (PortHandle, bool for_playback, LatencyRange); LatencyRange get_latency_range (PortHandle, bool for_playback); - /* Discovering physical ports */ - - bool port_is_physical (PortHandle) const; - void get_physical_outputs (DataType type, std::vector&); - void get_physical_inputs (DataType type, std::vector&); - ChanCount n_physical_outputs () const; - ChanCount n_physical_inputs () const; - /* Getting access to the data buffer for a port */ void* get_buffer (PortHandle, pframes_t); @@ -420,30 +361,13 @@ class AlsaAudioBackend : public AudioBackend { }; /* port engine */ - PortHandle add_port (const std::string& shortname, ARDOUR::DataType, ARDOUR::PortFlags); + + BackendPort* port_factory (std::string const & name, ARDOUR::DataType dt, ARDOUR::PortFlags flags); + int register_system_audio_ports (); int register_system_midi_ports (const std::string device = ""); - void unregister_ports (bool system_only = false); void update_system_port_latecies (); - std::vector _system_inputs; - std::vector _system_outputs; - std::vector _system_midi_in; - std::vector _system_midi_out; - - struct SortByPortName - { - bool operator ()(const AlsaPort* lhs, const AlsaPort* rhs) const - { - return PBD::naturally_less (lhs->name ().c_str (), rhs->name ().c_str ()); - } - }; - - typedef std::map PortMap; // fast lookup in _ports - typedef std::set PortIndex; // fast lookup in _ports - SerializedRCUManager _portmap; - SerializedRCUManager _ports; - std::vector _rmidi_out; std::vector _rmidi_in; @@ -472,20 +396,6 @@ class AlsaAudioBackend : public AudioBackend { pthread_mutex_unlock (&_port_callback_mutex); } - bool valid_port (PortHandle port) const { - boost::shared_ptr p = _ports.reader(); - return std::find (p->begin(), p->end(), static_cast(port)) != p->end (); - } - - AlsaPort* find_port (const std::string& port_name) const { - boost::shared_ptr p = _portmap.reader(); - PortMap::const_iterator it = p->find (port_name); - if (it == p->end()) { - return NULL; - } - return (*it).second; - } - void update_systemic_audio_latencies (); void update_systemic_midi_latencies (); @@ -512,8 +422,8 @@ class AlsaAudioBackend : public AudioBackend { bool halt; bool dead; - std::vector inputs; - std::vector outputs; + std::vector inputs; + std::vector outputs; PBD::Signal0 UpdateLatency; PBD::ScopedConnection latency_connection;