Merged with trunk R1283.
[ardour.git] / libs / ardour / source.cc
index eebc64d46392a52804a9e36c02d28c8309d9499c..db2147493aaf971e4ea353e8ed3fa4489a9b8568 100644 (file)
@@ -34,6 +34,7 @@
 #include <pbd/pthread_utils.h>
 
 #include <ardour/source.h>
+#include <ardour/playlist.h>
 
 #include "i18n.h"
 
@@ -42,25 +43,35 @@ using std::max;
 
 using namespace ARDOUR;
 
-Source::Source (string name)
+Source::Source (Session& s, string name, DataType type)
+       : _session (s)
+       , _type(type)
 {
+       assert(_name.find("/") == string::npos);
+
        _name = name;
-       _use_cnt = 0;
        _timestamp = 0;
+       _length = 0;
+       _in_use = 0;
 }
 
-Source::Source (const XMLNode& node) 
+Source::Source (Session& s, const XMLNode& node) 
+       : _session (s)
+       , _type(DataType::AUDIO)
 {
-       _use_cnt = 0;
        _timestamp = 0;
+       _length = 0;
+       _in_use = 0;
 
-       if (set_state (node)) {
+       if (set_state (node) || _type == DataType::NIL) {
                throw failed_constructor();
        }
+       assert(_name.find("/") == string::npos);
 }
 
 Source::~Source ()
 {
+       notify_callbacks ();
 }
 
 XMLNode&
@@ -70,7 +81,8 @@ Source::get_state ()
        char buf[64];
 
        node->add_property ("name", _name);
-       _id.print (buf);
+       node->add_property ("type", _type.to_string());
+       _id.print (buf, sizeof (buf));
        node->add_property ("id", buf);
 
        if (_timestamp != 0) {
@@ -98,22 +110,51 @@ Source::set_state (const XMLNode& node)
                return -1;
        }
 
+       if ((prop = node.property ("type")) != 0) {
+               _type = DataType(prop->value());
+       }
+
        if ((prop = node.property ("timestamp")) != 0) {
                sscanf (prop->value().c_str(), "%ld", &_timestamp);
        }
+       assert(_name.find("/") == string::npos);
 
        return 0;
 }
 
 void
-Source::use ()
+Source::update_length (jack_nframes_t pos, jack_nframes_t cnt)
 {
-       _use_cnt++;
+       if (pos + cnt > _length) {
+               _length = pos+cnt;
+       }
 }
 
 void
-Source::release ()
+Source::add_playlist (boost::shared_ptr<Playlist> pl)
 {
-       if (_use_cnt) --_use_cnt;
+       _playlists.insert (pl);
+       pl->GoingAway.connect (bind (mem_fun (*this, &Source::remove_playlist), boost::weak_ptr<Playlist> (pl)));
 }
 
+void
+Source::remove_playlist (boost::weak_ptr<Playlist> wpl)
+{
+       boost::shared_ptr<Playlist> pl (wpl.lock());
+
+       if (!pl) {
+               return;
+       }
+
+       std::set<boost::shared_ptr<Playlist> >::iterator x;
+
+       if ((x = _playlists.find (pl)) != _playlists.end()) {
+               _playlists.erase (x);
+       }
+}
+
+uint32_t
+Source::used () const
+{
+       return _playlists.size();
+}