fix a bad transition in the transportFSM.
[ardour.git] / libs / ardour / session_playlists.cc
index 8e25cbfd9d737866bdaeeefdaee804c25fa97ddc..0201c3e71dbe953d67c13463f6c7e46b22a7fea2 100644 (file)
@@ -1,34 +1,35 @@
 /*
-    Copyright (C) 2009 Paul Davis
-
-    This program is free software; you can redistribute it and/or modify
-    it under the terms of the GNU General Public License as published by
-    the Free Software Foundation; either version 2 of the License, or
-    (at your option) any later version.
-
-    This program is distributed in the hope that it will be useful,
-    but WITHOUT ANY WARRANTY; without even the implied warranty of
-    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-    GNU General Public License for more details.
-
-    You should have received a copy of the GNU General Public License
-    along with this program; if not, write to the Free Software
-    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
-
-*/
+ * Copyright (C) 2009-2011 Carl Hetherington <carl@carlh.net>
+ * Copyright (C) 2009-2016 Paul Davis <paul@linuxaudiosystems.com>
+ * Copyright (C) 2011-2012 David Robillard <d@drobilla.net>
+ * Copyright (C) 2013-2016 John Emmas <john@creativepost.co.uk>
+ * Copyright (C) 2015-2019 Robin Gareus <robin@gareus.org>
+ * Copyright (C) 2016 Tim Mayberry <mojofunk@gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
 #include <vector>
 
-#include "pbd/xml++.h"
-#include "pbd/compose.h"
 #include "ardour/debug.h"
-#include "ardour/session_playlists.h"
 #include "ardour/playlist.h"
-#include "ardour/region.h"
 #include "ardour/playlist_factory.h"
-#include "ardour/session.h"
-#include "ardour/source.h"
+#include "ardour/session_playlists.h"
 #include "ardour/track.h"
-#include "i18n.h"
+#include "pbd/i18n.h"
+#include "pbd/compose.h"
+#include "pbd/xml++.h"
 
 using namespace std;
 using namespace PBD;
@@ -72,7 +73,7 @@ SessionPlaylists::~SessionPlaylists ()
 bool
 SessionPlaylists::add (boost::shared_ptr<Playlist> playlist)
 {
-       Glib::Mutex::Lock lm (lock);
+       Glib::Threads::Mutex::Lock lm (lock);
 
        bool const existing = find (playlists.begin(), playlists.end(), playlist) != playlists.end();
 
@@ -99,7 +100,7 @@ SessionPlaylists::remove_weak (boost::weak_ptr<Playlist> playlist)
 void
 SessionPlaylists::remove (boost::shared_ptr<Playlist> playlist)
 {
-       Glib::Mutex::Lock lm (lock);
+       Glib::Threads::Mutex::Lock lm (lock);
 
        List::iterator i;
 
@@ -114,6 +115,30 @@ SessionPlaylists::remove (boost::shared_ptr<Playlist> playlist)
        }
 }
 
+void
+SessionPlaylists::update_tracking ()
+{
+       /* This is intended to be called during session-load, after loading
+        * playlists and re-assigning them to tracks (refcnt is up to date).
+        * Check playlist refcnt, move unused playlist to unused_playlists
+        * array (which may be the case when loading old sessions)
+        */
+       for (List::iterator i = playlists.begin(); i != playlists.end(); ) {
+               if ((*i)->hidden () || (*i)->used ()) {
+                       ++i;
+                       continue;
+               }
+
+               warning << _("Session State: Unused playlist was listed as used.") << endmsg;
+
+               assert (unused_playlists.find (*i) == unused_playlists.end());
+               unused_playlists.insert (*i);
+
+               List::iterator rm = i;
+               ++i;
+                playlists.erase (rm);
+       }
+}
 
 void
 SessionPlaylists::track (bool inuse, boost::weak_ptr<Playlist> wpl)
@@ -132,7 +157,7 @@ SessionPlaylists::track (bool inuse, boost::weak_ptr<Playlist> wpl)
        }
 
        {
-               Glib::Mutex::Lock lm (lock);
+               Glib::Threads::Mutex::Lock lm (lock);
 
                if (!inuse) {
 
@@ -157,14 +182,14 @@ SessionPlaylists::track (bool inuse, boost::weak_ptr<Playlist> wpl)
 uint32_t
 SessionPlaylists::n_playlists () const
 {
-       Glib::Mutex::Lock lm (lock);
+       Glib::Threads::Mutex::Lock lm (lock);
        return playlists.size();
 }
 
 boost::shared_ptr<Playlist>
 SessionPlaylists::by_name (string name)
 {
-       Glib::Mutex::Lock lm (lock);
+       Glib::Threads::Mutex::Lock lm (lock);
 
        for (List::iterator i = playlists.begin(); i != playlists.end(); ++i) {
                if ((*i)->name() == name) {
@@ -184,7 +209,7 @@ SessionPlaylists::by_name (string name)
 boost::shared_ptr<Playlist>
 SessionPlaylists::by_id (const PBD::ID& id)
 {
-       Glib::Mutex::Lock lm (lock);
+       Glib::Threads::Mutex::Lock lm (lock);
 
        for (List::iterator i = playlists.begin(); i != playlists.end(); ++i) {
                if ((*i)->id() == id) {
@@ -204,7 +229,7 @@ SessionPlaylists::by_id (const PBD::ID& id)
 void
 SessionPlaylists::unassigned (std::list<boost::shared_ptr<Playlist> > & list)
 {
-       Glib::Mutex::Lock lm (lock);
+       Glib::Threads::Mutex::Lock lm (lock);
 
        for (List::iterator i = playlists.begin(); i != playlists.end(); ++i) {
                if (!(*i)->get_orig_track_id().to_s().compare ("0")) {
@@ -222,13 +247,13 @@ SessionPlaylists::unassigned (std::list<boost::shared_ptr<Playlist> > & list)
 void
 SessionPlaylists::get (vector<boost::shared_ptr<Playlist> >& s) const
 {
-       Glib::Mutex::Lock lm (lock);
+       Glib::Threads::Mutex::Lock lm (lock);
 
-       for (List::iterator i = playlists.begin(); i != playlists.end(); ++i) {
+       for (List::const_iterator i = playlists.begin(); i != playlists.end(); ++i) {
                s.push_back (*i);
        }
 
-       for (List::iterator i = unused_playlists.begin(); i != unused_playlists.end(); ++i) {
+       for (List::const_iterator i = unused_playlists.begin(); i != unused_playlists.end(); ++i) {
                s.push_back (*i);
        }
 }
@@ -236,7 +261,7 @@ SessionPlaylists::get (vector<boost::shared_ptr<Playlist> >& s) const
 void
 SessionPlaylists::destroy_region (boost::shared_ptr<Region> r)
 {
-       Glib::Mutex::Lock lm (lock);
+       Glib::Threads::Mutex::Lock lm (lock);
 
        for (List::iterator i = playlists.begin(); i != playlists.end(); ++i) {
                 (*i)->destroy_region (r);
@@ -247,7 +272,6 @@ SessionPlaylists::destroy_region (boost::shared_ptr<Region> r)
        }
 }
 
-
 void
 SessionPlaylists::find_equivalent_playlist_regions (boost::shared_ptr<Region> region, vector<boost::shared_ptr<Region> >& result)
 {
@@ -255,25 +279,39 @@ SessionPlaylists::find_equivalent_playlist_regions (boost::shared_ptr<Region> re
                (*i)->get_region_list_equivalent_regions (region, result);
 }
 
-/** Return the number of playlists (not regions) that contain @a src */
+/** Return the number of playlists (not regions) that contain @a src
+ *  Important: this counts usage in both used and not-used playlists.
+ */
 uint32_t
 SessionPlaylists::source_use_count (boost::shared_ptr<const Source> src) const
 {
        uint32_t count = 0;
 
+       /* XXXX this can go wildly wrong in the presence of circular references
+        * between compound regions.
+        */
+
        for (List::const_iterator p = playlists.begin(); p != playlists.end(); ++p) {
                 if ((*p)->uses_source (src)) {
                         ++count;
                         break;
                 }
        }
+
+       for (List::const_iterator p = unused_playlists.begin(); p != unused_playlists.end(); ++p) {
+                if ((*p)->uses_source (src)) {
+                        ++count;
+                        break;
+                }
+       }
+
        return count;
 }
 
 void
 SessionPlaylists::sync_all_regions_with_regions ()
 {
-       Glib::Mutex::Lock lm (lock);
+       Glib::Threads::Mutex::Lock lm (lock);
 
        for (List::const_iterator p = playlists.begin(); p != playlists.end(); ++p) {
                 (*p)->sync_all_regions_with_regions ();
@@ -292,28 +330,63 @@ SessionPlaylists::update_after_tempo_map_change ()
        }
 }
 
+namespace {
+struct id_compare
+{
+       bool operator()(const boost::shared_ptr<Playlist>& p1, const boost::shared_ptr<Playlist>& p2)
+       {
+               return p1->id () < p2->id ();
+       }
+};
+
+typedef std::set<boost::shared_ptr<Playlist> > List;
+typedef std::set<boost::shared_ptr<Playlist>, id_compare> IDSortedList;
+
+static void
+get_id_sorted_playlists (const List& playlists, IDSortedList& id_sorted_playlists)
+{
+       for (List::const_iterator i = playlists.begin(); i != playlists.end(); ++i) {
+               id_sorted_playlists.insert(*i);
+       }
+}
+
+} // anonymous namespace
+
 void
-SessionPlaylists::add_state (XMLNode* node, bool full_state)
+SessionPlaylists::add_state (XMLNode* node, bool save_template, bool include_unused)
 {
        XMLNode* child = node->add_child ("Playlists");
-       for (List::iterator i = playlists.begin(); i != playlists.end(); ++i) {
-               if (!(*i)->hidden()) {
-                        if (full_state) {
-                                child->add_child_nocopy ((*i)->get_state());
-                        } else {
-                                child->add_child_nocopy ((*i)->get_template());
-                        }
-                }
+
+       IDSortedList id_sorted_playlists;
+       get_id_sorted_playlists (playlists, id_sorted_playlists);
+
+       for (IDSortedList::iterator i = id_sorted_playlists.begin (); i != id_sorted_playlists.end (); ++i) {
+               if (!(*i)->hidden ()) {
+                       if (save_template) {
+                               child->add_child_nocopy ((*i)->get_template ());
+                       } else {
+                               child->add_child_nocopy ((*i)->get_state ());
+                       }
+               }
+       }
+
+       if (!include_unused) {
+               return;
        }
 
        child = node->add_child ("UnusedPlaylists");
-       for (List::iterator i = unused_playlists.begin(); i != unused_playlists.end(); ++i) {
+
+       IDSortedList id_sorted_unused_playlists;
+       get_id_sorted_playlists (unused_playlists, id_sorted_unused_playlists);
+
+       for (IDSortedList::iterator i = id_sorted_unused_playlists.begin ();
+            i != id_sorted_unused_playlists.end (); ++i) {
                if (!(*i)->hidden()) {
                        if (!(*i)->empty()) {
-                               if (full_state) {
-                                       child->add_child_nocopy ((*i)->get_state());
-                               } else {
+                               if (save_template) {
                                        child->add_child_nocopy ((*i)->get_template());
+                               } else {
+                                       child->add_child_nocopy ((*i)->get_state());
                                }
                        }
                }
@@ -326,15 +399,39 @@ SessionPlaylists::maybe_delete_unused (boost::function<int(boost::shared_ptr<Pla
 {
        vector<boost::shared_ptr<Playlist> > playlists_tbd;
 
+       bool delete_remaining = false;
+       bool keep_remaining = false;
+
        for (List::iterator x = unused_playlists.begin(); x != unused_playlists.end(); ++x) {
 
+               if (keep_remaining) {
+                       break;
+               }
+
+               if (delete_remaining) {
+                       playlists_tbd.push_back (*x);
+                       continue;
+               }
+
                int status = ask (*x);
 
                switch (status) {
                case -1:
+                       // abort
                        return true;
 
-               case 0:
+               case -2:
+                       // keep this and all later
+                       keep_remaining = true;
+                       break;
+
+               case 2:
+                       // delete this and all later
+                       delete_remaining = true;
+
+                       /* fallthrough */
+               case 1:
+                       // delete this
                        playlists_tbd.push_back (*x);
                        break;
 
@@ -414,7 +511,7 @@ SessionPlaylists::XMLPlaylistFactory (Session& session, const XMLNode& node)
 boost::shared_ptr<Crossfade>
 SessionPlaylists::find_crossfade (const PBD::ID& id)
 {
-       Glib::Mutex::Lock lm (lock);
+       Glib::Threads::Mutex::Lock lm (lock);
 
        boost::shared_ptr<Crossfade> c;
 
@@ -438,34 +535,84 @@ SessionPlaylists::find_crossfade (const PBD::ID& id)
 uint32_t
 SessionPlaylists::region_use_count (boost::shared_ptr<Region> region) const
 {
-       Glib::Mutex::Lock lm (lock);
+       Glib::Threads::Mutex::Lock lm (lock);
         uint32_t cnt = 0;
 
-       for (List::iterator i = playlists.begin(); i != playlists.end(); ++i) {
+       for (List::const_iterator i = playlists.begin(); i != playlists.end(); ++i) {
                 cnt += (*i)->region_use_count (region);
        }
 
-       for (List::iterator i = unused_playlists.begin(); i != unused_playlists.end(); ++i) {
+       for (List::const_iterator i = unused_playlists.begin(); i != unused_playlists.end(); ++i) {
                 cnt += (*i)->region_use_count (region);
        }
 
        return cnt;
 }
 
+vector<boost::shared_ptr<Playlist> >
+SessionPlaylists::get_used () const
+{
+       vector<boost::shared_ptr<Playlist> > pl;
+
+       Glib::Threads::Mutex::Lock lm (lock);
+
+       for (List::const_iterator i = playlists.begin(); i != playlists.end(); ++i) {
+               pl.push_back (*i);
+       }
+
+       return pl;
+}
+
+vector<boost::shared_ptr<Playlist> >
+SessionPlaylists::get_unused () const
+{
+       vector<boost::shared_ptr<Playlist> > pl;
+
+       Glib::Threads::Mutex::Lock lm (lock);
+
+       for (List::const_iterator i = unused_playlists.begin(); i != unused_playlists.end(); ++i) {
+               pl.push_back (*i);
+       }
+
+       return pl;
+}
+
 /** @return list of Playlists that are associated with a track */
 vector<boost::shared_ptr<Playlist> >
 SessionPlaylists::playlists_for_track (boost::shared_ptr<Track> tr) const
 {
        vector<boost::shared_ptr<Playlist> > pl;
        get (pl);
-       
+
        vector<boost::shared_ptr<Playlist> > pl_tr;
 
        for (vector<boost::shared_ptr<Playlist> >::iterator i = pl.begin(); i != pl.end(); ++i) {
-               if (((*i)->get_orig_track_id() == tr->id()) || (tr->playlist()->id() == (*i)->id())) {
+               if ( ((*i)->get_orig_track_id() == tr->id()) ||
+                       (tr->playlist()->id() == (*i)->id())    ||
+                       ((*i)->shared_with (tr->id())) )
+               {
                        pl_tr.push_back (*i);
                }
        }
 
        return pl_tr;
 }
+
+void
+SessionPlaylists::foreach (boost::function<void(boost::shared_ptr<const Playlist>)> functor, bool incl_unused)
+{
+       Glib::Threads::Mutex::Lock lm (lock);
+       for (List::iterator i = playlists.begin(); i != playlists.end(); i++) {
+               if (!(*i)->hidden()) {
+                       functor (*i);
+               }
+       }
+       if (!incl_unused) {
+               return;
+       }
+       for (List::iterator i = unused_playlists.begin(); i != unused_playlists.end(); i++) {
+               if (!(*i)->hidden()) {
+                       functor (*i);
+               }
+       }
+}