Merge branch 'master' into windows
[ardour.git] / libs / ardour / io.cc
index ca34b7db6191f91d7f9462a51cd0b80edfa6c933..1349d49a0ce1c36667c4cff1b57f151ebd188778 100644 (file)
@@ -25,7 +25,7 @@
 #include <errno.h>
 
 #include <glibmm.h>
-#include <glibmm/thread.h>
+#include <glibmm/threads.h>
 
 #include "pbd/xml++.h"
 #include "pbd/replace_all.h"
 
 #include "ardour/audioengine.h"
 #include "ardour/buffer.h"
+#include "ardour/buffer_set.h"
 #include "ardour/debug.h"
 #include "ardour/io.h"
-#include "ardour/route.h"
 #include "ardour/port.h"
-#include "ardour/audio_port.h"
-#include "ardour/midi_port.h"
+#include "ardour/route.h"
 #include "ardour/session.h"
-#include "ardour/cycle_timer.h"
-#include "ardour/buffer_set.h"
-#include "ardour/meter.h"
-#include "ardour/amp.h"
 #include "ardour/user_bundle.h"
 
 #include "i18n.h"
 
-#define BLOCK_PROCESS_CALLBACK() Glib::Mutex::Lock em (AudioEngine::instance()->process_lock())
+#define BLOCK_PROCESS_CALLBACK() Glib::Threads::Mutex::Lock em (AudioEngine::instance()->process_lock())
 
 using namespace std;
 using namespace ARDOUR;
@@ -63,23 +58,27 @@ PBD::Signal1<void,ChanCount> IO::PortCountChanged;
 /** @param default_type The type of port that will be created by ensure_io
  * and friends if no type is explicitly requested (to avoid breakage).
  */
-IO::IO (Session& s, const string& name, Direction dir, DataType default_type)
+IO::IO (Session& s, const string& name, Direction dir, DataType default_type, bool sendish)
        : SessionObject (s, name)
        , _direction (dir)
        , _default_type (default_type)
+       , _sendish (sendish)
 {
        _active = true;
+       Port::PostDisconnect.connect_same_thread (*this, boost::bind (&IO::disconnect_check, this, _1, _2));
        pending_state_node = 0;
        setup_bundle ();
 }
 
-IO::IO (Session& s, const XMLNode& node, DataType dt)
+IO::IO (Session& s, const XMLNode& node, DataType dt, bool sendish)
        : SessionObject(s, "unnamed io")
        , _direction (Input)
        , _default_type (dt)
+       , _sendish (sendish)
 {
        _active = true;
        pending_state_node = 0;
+       Port::PostDisconnect.connect_same_thread (*this, boost::bind (&IO::disconnect_check, this, _1, _2));
 
        set_state (node, Stateful::loading_state_version);
        setup_bundle ();
@@ -87,7 +86,7 @@ IO::IO (Session& s, const XMLNode& node, DataType dt)
 
 IO::~IO ()
 {
-       Glib::Mutex::Lock lm (io_lock);
+       Glib::Threads::Mutex::Lock lm (io_lock);
 
        BLOCK_PROCESS_CALLBACK ();
 
@@ -96,6 +95,31 @@ IO::~IO ()
        }
 }
 
+void
+IO::disconnect_check (boost::shared_ptr<Port> a, boost::shared_ptr<Port> b)
+{
+       /* this could be called from within our own ::disconnect() method(s)
+          or from somewhere that operates directly on a port. so, we don't
+          know for sure if we can take this lock or not. if we fail,
+          we assume that its safely locked by our own ::disconnect().
+       */
+
+       Glib::Threads::Mutex::Lock tm (io_lock, Glib::Threads::TRY_LOCK);
+
+       if (tm.locked()) {
+               /* we took the lock, so we cannot be here from inside
+                * ::disconnect()
+                */
+               if (_ports.contains (a) || _ports.contains (b)) {
+                       changed (IOChange (IOChange::ConnectionsChanged), this); /* EMIT SIGNAL */              
+               }
+       } else {
+               /* we didn't get the lock, so assume that we're inside
+                * ::disconnect(), and it will call changed() appropriately.
+                */
+       }
+}
+
 void
 IO::increment_port_buffer_offset (pframes_t offset)
 {
@@ -118,22 +142,15 @@ IO::silence (framecnt_t nframes)
        }
 }
 
-void
-IO::check_bundles_connected ()
-{
-       check_bundles (_bundles_connected, ports());
-}
-
-/** Check the bundles in list to see which are connected to a given PortSet,
- *  and update list with those that are connected such that every port on every
- *  bundle channel x is connected to port x in ports.
+/** Set _bundles_connected to those bundles that are connected such that every
+ *  port on every bundle channel x is connected to port x in _ports.
  */
 void
-IO::check_bundles (std::vector<UserBundleInfo*>& list, const PortSet& ports)
+IO::check_bundles_connected ()
 {
        std::vector<UserBundleInfo*> new_list;
 
-       for (std::vector<UserBundleInfo*>::iterator i = list.begin(); i != list.end(); ++i) {
+       for (std::vector<UserBundleInfo*>::iterator i = _bundles_connected.begin(); i != _bundles_connected.end(); ++i) {
 
                uint32_t const N = (*i)->bundle->nchannels().n_total();
 
@@ -147,7 +164,7 @@ IO::check_bundles (std::vector<UserBundleInfo*>& list, const PortSet& ports)
                        /* Every port on bundle channel j must be connected to our input j */
                        Bundle::PortList const pl = (*i)->bundle->channel_ports (j);
                        for (uint32_t k = 0; k < pl.size(); ++k) {
-                               if (ports.port(j)->connected_to (pl[k]) == false) {
+                               if (_ports.port(j)->connected_to (pl[k]) == false) {
                                        ok = false;
                                        break;
                                }
@@ -165,19 +182,19 @@ IO::check_bundles (std::vector<UserBundleInfo*>& list, const PortSet& ports)
                }
        }
 
-       list = new_list;
+       _bundles_connected = new_list;
 }
 
 
 int
-IO::disconnect (Port* our_port, string other_port, void* src)
+IO::disconnect (boost::shared_ptr<Port> our_port, string other_port, void* src)
 {
        if (other_port.length() == 0 || our_port == 0) {
                return 0;
        }
 
         {
-                Glib::Mutex::Lock lm (io_lock);
+                Glib::Threads::Mutex::Lock lm (io_lock);
 
                 /* check that our_port is really one of ours */
 
@@ -203,14 +220,14 @@ IO::disconnect (Port* our_port, string other_port, void* src)
 }
 
 int
-IO::connect (Port* our_port, string other_port, void* src)
+IO::connect (boost::shared_ptr<Port> our_port, string other_port, void* src)
 {
        if (other_port.length() == 0 || our_port == 0) {
                return 0;
        }
 
        {
-               Glib::Mutex::Lock lm (io_lock);
+               Glib::Threads::Mutex::Lock lm (io_lock);
 
                /* check that our_port is really one of ours */
 
@@ -230,14 +247,14 @@ IO::connect (Port* our_port, string other_port, void* src)
 }
 
 int
-IO::remove_port (Port* port, void* src)
+IO::remove_port (boost::shared_ptr<Port> port, void* src)
 {
        ChanCount before = _ports.count ();
        ChanCount after = before;
        after.set (port->type(), after.get (port->type()) - 1);
 
-       bool const r = PortCountChanging (after); /* EMIT SIGNAL */
-       if (r) {
+       boost::optional<bool> const r = PortCountChanging (after); /* EMIT SIGNAL */
+       if (r.get_value_or (false)) {
                return -1;
        }
 
@@ -247,7 +264,7 @@ IO::remove_port (Port* port, void* src)
                BLOCK_PROCESS_CALLBACK ();
 
                {
-                       Glib::Mutex::Lock lm (io_lock);
+                       Glib::Threads::Mutex::Lock lm (io_lock);
 
                        if (_ports.remove(port)) {
                                change.type = IOChange::Type (change.type | IOChange::ConfigurationChanged);
@@ -258,7 +275,7 @@ IO::remove_port (Port* port, void* src)
                                        change.type = IOChange::Type (change.type | IOChange::ConnectionsChanged);
                                }
 
-                               _session.engine().unregister_port (*port);
+                               _session.engine().unregister_port (port);
                                check_bundles_connected ();
                        }
                }
@@ -293,12 +310,21 @@ IO::remove_port (Port* port, void* src)
 int
 IO::add_port (string destination, void* src, DataType type)
 {
-       Port* our_port;
+       boost::shared_ptr<Port> our_port;
 
        if (type == DataType::NIL) {
                type = _default_type;
        }
 
+       ChanCount before = _ports.count ();
+       ChanCount after = before;
+       after.set (type, after.get (type) + 1);
+       
+       bool const r = PortCountChanging (after); /* EMIT SIGNAL */
+       if (r) {
+               return -1;
+       }
+       
        IOChange change;
 
        {
@@ -306,7 +332,7 @@ IO::add_port (string destination, void* src, DataType type)
 
 
                {
-                       Glib::Mutex::Lock lm (io_lock);
+                       Glib::Threads::Mutex::Lock lm (io_lock);
 
                        /* Create a new port */
 
@@ -351,7 +377,7 @@ int
 IO::disconnect (void* src)
 {
        {
-               Glib::Mutex::Lock lm (io_lock);
+               Glib::Threads::Mutex::Lock lm (io_lock);
 
                for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
                        i->disconnect_all ();
@@ -369,9 +395,11 @@ IO::disconnect (void* src)
 int
 IO::ensure_ports_locked (ChanCount count, bool clear, bool& changed)
 {
+#ifndef WIN32
        assert (!AudioEngine::instance()->process_lock().trylock());
+#endif
 
-       Port* port = 0;
+       boost::shared_ptr<Port> port;
 
        changed    = false;
 
@@ -385,7 +413,7 @@ IO::ensure_ports_locked (ChanCount count, bool clear, bool& changed)
 
                        assert(port);
                        _ports.remove(port);
-                       _session.engine().unregister_port (*port);
+                       _session.engine().unregister_port (port);
 
                        changed = true;
                }
@@ -440,7 +468,9 @@ IO::ensure_ports_locked (ChanCount count, bool clear, bool& changed)
 int
 IO::ensure_ports (ChanCount count, bool clear, void* src)
 {
+#ifndef WIN32
        assert (!AudioEngine::instance()->process_lock().trylock());
+#endif
 
        bool changed = false;
 
@@ -453,7 +483,7 @@ IO::ensure_ports (ChanCount count, bool clear, void* src)
        change.before = _ports.count ();
 
        {
-               Glib::Mutex::Lock im (io_lock);
+               Glib::Threads::Mutex::Lock im (io_lock);
                if (ensure_ports_locked (count, clear, changed)) {
                        return -1;
                }
@@ -475,13 +505,15 @@ IO::ensure_ports (ChanCount count, bool clear, void* src)
 int
 IO::ensure_io (ChanCount count, bool clear, void* src)
 {
+#ifndef WIN32
        assert (!AudioEngine::instance()->process_lock().trylock());
+#endif
 
        return ensure_ports (count, clear, src);
 }
 
 XMLNode&
-IO::get_state (void)
+IO::get_state ()
 {
        return state (true);
 }
@@ -495,7 +527,7 @@ IO::state (bool /*full_state*/)
        vector<string>::iterator ci;
        int n;
        LocaleGuard lg (X_("POSIX"));
-       Glib::Mutex::Lock lm (io_lock);
+       Glib::Threads::Mutex::Lock lm (io_lock);
 
        node->add_property("name", _name);
        id().print (buf, sizeof (buf));
@@ -857,7 +889,7 @@ IO::create_ports (const XMLNode& node, int version)
        get_port_counts (node, version, n, c);
 
        {
-               Glib::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
+               Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
 
                if (ensure_ports (n, true, this)) {
                        error << string_compose(_("%1: cannot create I/O ports"), _name) << endmsg;
@@ -886,7 +918,7 @@ IO::make_connections (const XMLNode& node, int version, bool in)
                        if (prop) {
                                boost::shared_ptr<Bundle> b = find_possible_bundle (prop->value());
                                if (b) {
-                                       connect_ports_to_bundle (b, this);
+                                       connect_ports_to_bundle (b, true, this);
                                }
                        }
 
@@ -901,7 +933,7 @@ IO::make_connections (const XMLNode& node, int version, bool in)
                                continue;
                        }
 
-                       Port* p = port_by_name (prop->value());
+                       boost::shared_ptr<Port> p = port_by_name (prop->value());
 
                        if (p) {
                                for (XMLNodeConstIterator c = (*i)->children().begin(); c != (*i)->children().end(); ++c) {
@@ -927,6 +959,44 @@ IO::make_connections (const XMLNode& node, int version, bool in)
        return 0;
 }
 
+void
+IO::prepare_for_reset (XMLNode& node, const std::string& name)
+{
+       /* reset name */
+       node.add_property ("name", name);
+
+       /* now find connections and reset the name of the port
+          in one so that when we re-use it it will match
+          the name of the thing we're applying it to.
+       */
+
+       XMLProperty* prop;
+       XMLNodeList children = node.children();
+
+       for (XMLNodeIterator i = children.begin(); i != children.end(); ++i) {
+
+               if ((*i)->name() == "Port") {
+                       
+                       prop = (*i)->property (X_("name"));
+                       
+                       if (prop) {
+                               string new_name;
+                               string old = prop->value();
+                               string::size_type slash = old.find ('/');
+
+                               if (slash != string::npos) {
+                                       /* port name is of form: <IO-name>/<port-name> */
+                                       
+                                       new_name = name;
+                                       new_name += old.substr (old.find ('/'));
+                                       
+                                       prop->set_value (new_name);
+                               }
+                       }
+               }
+       }
+}
+
 
 int
 IO::make_connections_2X (const XMLNode& node, int /*version*/, bool in)
@@ -1035,7 +1105,7 @@ IO::set_ports (const string& str)
        }
 
        {
-               Glib::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
+               Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock ());
 
                // FIXME: audio-only
                if (ensure_ports (ChanCount(DataType::AUDIO, nports), true, this)) {
@@ -1177,12 +1247,18 @@ IO::latency () const
 }
 
 int
-IO::connect_ports_to_bundle (boost::shared_ptr<Bundle> c, void* src)
+IO::connect_ports_to_bundle (boost::shared_ptr<Bundle> c, bool exclusive, void* src)
 {
        BLOCK_PROCESS_CALLBACK ();
 
        {
-               Glib::Mutex::Lock lm2 (io_lock);
+               Glib::Threads::Mutex::Lock lm2 (io_lock);
+
+               if (exclusive) {
+                       for (PortSet::iterator i = _ports.begin(); i != _ports.end(); ++i) {
+                               i->disconnect_all ();
+                       }
+               }
 
                c->connect (_bundle, _session.engine());
 
@@ -1214,7 +1290,7 @@ IO::disconnect_ports_from_bundle (boost::shared_ptr<Bundle> c, void* src)
        BLOCK_PROCESS_CALLBACK ();
 
        {
-               Glib::Mutex::Lock lm2 (io_lock);
+               Glib::Threads::Mutex::Lock lm2 (io_lock);
 
                c->disconnect (_bundle, _session.engine());
 
@@ -1250,6 +1326,7 @@ IO::disable_connecting ()
 int
 IO::enable_connecting ()
 {
+       Glib::Threads::Mutex::Lock lm (AudioEngine::instance()->process_lock());
        connecting_legal = true;
        boost::optional<int> r = ConnectingLegal ();
        return r.get_value_or (0);
@@ -1258,7 +1335,7 @@ IO::enable_connecting ()
 void
 IO::bundle_changed (Bundle::Change /*c*/)
 {
-       //XXX
+       /* XXX */
 //     connect_input_ports_to_bundle (_input_bundle, this);
 }
 
@@ -1271,9 +1348,9 @@ IO::build_legal_port_name (DataType type)
        string suffix;
 
        if (type == DataType::AUDIO) {
-               suffix = _("audio");
+               suffix = X_("audio");
        } else if (type == DataType::MIDI) {
-               suffix = _("midi");
+               suffix = X_("midi");
        } else {
                throw unknown_type();
        }
@@ -1284,10 +1361,18 @@ IO::build_legal_port_name (DataType type)
           use the (new) translated name.
        */
 
-       if (_direction == Input) {
-               suffix += X_("_in");
+       if (_sendish) {
+               if (_direction == Input) {
+                       suffix += X_("_return");
+               } else {
+                       suffix += X_("_send");
+               }
        } else {
-               suffix += X_("_out");
+               if (_direction == Input) {
+                       suffix += X_("_in");
+               } else {
+                       suffix += X_("_out");
+               }
        }
 
        // allow up to 4 digits for the output port number, plus the slash, suffix and extra space
@@ -1344,14 +1429,14 @@ IO::find_port_hole (const char* base)
 }
 
 
-AudioPort*
+boost::shared_ptr<AudioPort>
 IO::audio(uint32_t n) const
 {
        return _ports.nth_audio_port (n);
 
 }
 
-MidiPort*
+boost::shared_ptr<MidiPort>
 IO::midi(uint32_t n) const
 {
        return _ports.nth_midi_port (n);
@@ -1485,10 +1570,14 @@ IO::name_from_state (const XMLNode& node)
 void
 IO::set_name_in_state (XMLNode& node, const string& new_name)
 {
-       const XMLProperty* prop;
-
-       if ((prop = node.property ("name")) != 0) {
-               node.add_property ("name", new_name);
+       node.add_property (X_("name"), new_name);
+       XMLNodeList children = node.children ();
+       for (XMLNodeIterator i = children.begin(); i != children.end(); ++i) {
+               if ((*i)->name() == X_("Port")) {
+                       string const old_name = (*i)->property(X_("name"))->value();
+                       string const old_name_second_part = old_name.substr (old_name.find_first_of ("/") + 1);
+                       (*i)->add_property (X_("name"), string_compose ("%1/%2", new_name, old_name_second_part));
+               }
        }
 }
 
@@ -1542,14 +1631,23 @@ IO::connected_to (const string& str) const
        return false;
 }
 
-/** Caller must hold process lock */
+/** Call a processor's ::run() method, giving it our buffers
+ *  Caller must hold process lock.
+ */
 void
 IO::process_input (boost::shared_ptr<Processor> proc, framepos_t start_frame, framepos_t end_frame, pframes_t nframes)
 {
        /* don't read the data into new buffers - just use the port buffers directly */
 
+       if (n_ports().n_total() == 0) {
+               /* We have no ports, so nothing to process */
+               return;
+       }
+
        _buffers.get_jack_port_addresses (_ports, nframes);
-       proc->run (_buffers, start_frame, end_frame, nframes, true);
+       if (proc) {
+               proc->run (_buffers, start_frame, end_frame, nframes, true);
+       }
 }
 
 void
@@ -1606,21 +1704,19 @@ IO::copy_to_outputs (BufferSet& bufs, DataType type, pframes_t nframes, framecnt
        }
 }
 
-Port*
+boost::shared_ptr<Port>
 IO::port_by_name (const std::string& str) const
 {
        /* to be called only from ::set_state() - no locking */
 
        for (PortSet::const_iterator i = _ports.begin(); i != _ports.end(); ++i) {
 
-               const Port& p(*i);
-
-               if (p.name() == str) {
-                       return const_cast<Port*>(&p);
+               if (i->name() == str) {
+                       return boost::const_pointer_cast<Port> (*i);
                }
        }
 
-       return 0;
+       return boost::shared_ptr<Port> ();
 }
 
 bool
@@ -1636,8 +1732,8 @@ IO::physically_connected () const
 }
 
 bool
-IO::has_port (Port* p) const
+IO::has_port (boost::shared_ptr<Port> p) const
 {
-       Glib::Mutex::Lock lm (io_lock);
+       Glib::Threads::Mutex::Lock lm (io_lock);
        return _ports.contains (p);
 }