2 Copyright (C) 2009 Paul Davis
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include "pbd/xml++.h"
22 #include "pbd/compose.h"
23 #include "ardour/debug.h"
24 #include "ardour/session_playlists.h"
25 #include "ardour/playlist.h"
26 #include "ardour/region.h"
27 #include "ardour/playlist_factory.h"
28 #include "ardour/session.h"
29 #include "ardour/source.h"
34 using namespace ARDOUR;
36 SessionPlaylists::~SessionPlaylists ()
38 DEBUG_TRACE (DEBUG::Destruction, "delete playlists\n");
40 for (List::iterator i = playlists.begin(); i != playlists.end(); ) {
41 SessionPlaylists::List::iterator tmp;
46 DEBUG_TRACE(DEBUG::Destruction, string_compose ("Dropping for used playlist %1 ; pre-ref = %2\n", (*i)->name(), (*i).use_count()));
47 boost::shared_ptr<Playlist> keeper (*i);
48 (*i)->drop_references ();
53 DEBUG_TRACE (DEBUG::Destruction, "delete unused playlists\n");
54 for (List::iterator i = unused_playlists.begin(); i != unused_playlists.end(); ) {
60 DEBUG_TRACE(DEBUG::Destruction, string_compose ("Dropping for unused playlist %1 ; pre-ref = %2\n", (*i)->name(), (*i).use_count()));
61 boost::shared_ptr<Playlist> keeper (*i);
62 (*i)->drop_references ();
68 unused_playlists.clear ();
72 SessionPlaylists::add (boost::shared_ptr<Playlist> playlist)
74 Glib::Mutex::Lock lm (lock);
76 bool const existing = find (playlists.begin(), playlists.end(), playlist) != playlists.end();
79 playlists.insert (playlists.begin(), playlist);
80 playlist->InUse.connect_same_thread (*this, boost::bind (&SessionPlaylists::track, this, _1, boost::weak_ptr<Playlist>(playlist)));
87 SessionPlaylists::remove (boost::shared_ptr<Playlist> playlist)
89 Glib::Mutex::Lock lm (lock);
93 i = find (playlists.begin(), playlists.end(), playlist);
94 if (i != playlists.end()) {
98 i = find (unused_playlists.begin(), unused_playlists.end(), playlist);
99 if (i != unused_playlists.end()) {
100 unused_playlists.erase (i);
106 SessionPlaylists::track (bool inuse, boost::weak_ptr<Playlist> wpl)
108 boost::shared_ptr<Playlist> pl(wpl.lock());
117 /* its not supposed to be visible */
122 Glib::Mutex::Lock lm (lock);
126 unused_playlists.insert (pl);
128 if ((x = playlists.find (pl)) != playlists.end()) {
135 playlists.insert (pl);
137 if ((x = unused_playlists.find (pl)) != unused_playlists.end()) {
138 unused_playlists.erase (x);
145 SessionPlaylists::n_playlists () const
147 Glib::Mutex::Lock lm (lock);
148 return playlists.size();
151 boost::shared_ptr<Playlist>
152 SessionPlaylists::by_name (string name)
154 Glib::Mutex::Lock lm (lock);
156 for (List::iterator i = playlists.begin(); i != playlists.end(); ++i) {
157 if ((*i)->name() == name) {
162 for (List::iterator i = unused_playlists.begin(); i != unused_playlists.end(); ++i) {
163 if ((*i)->name() == name) {
168 return boost::shared_ptr<Playlist>();
171 boost::shared_ptr<Playlist>
172 SessionPlaylists::by_id (const PBD::ID& id)
174 Glib::Mutex::Lock lm (lock);
176 for (List::iterator i = playlists.begin(); i != playlists.end(); ++i) {
177 if ((*i)->id() == id) {
182 for (List::iterator i = unused_playlists.begin(); i != unused_playlists.end(); ++i) {
183 if ((*i)->id() == id) {
188 return boost::shared_ptr<Playlist>();
192 SessionPlaylists::unassigned (std::list<boost::shared_ptr<Playlist> > & list)
194 Glib::Mutex::Lock lm (lock);
196 for (List::iterator i = playlists.begin(); i != playlists.end(); ++i) {
197 if (!(*i)->get_orig_diskstream_id().to_s().compare ("0")) {
202 for (List::iterator i = unused_playlists.begin(); i != unused_playlists.end(); ++i) {
203 if (!(*i)->get_orig_diskstream_id().to_s().compare ("0")) {
210 SessionPlaylists::get (vector<boost::shared_ptr<Playlist> >& s)
212 Glib::Mutex::Lock lm (lock);
214 for (List::iterator i = playlists.begin(); i != playlists.end(); ++i) {
218 for (List::iterator i = unused_playlists.begin(); i != unused_playlists.end(); ++i) {
224 SessionPlaylists::destroy_region (boost::shared_ptr<Region> r)
226 Glib::Mutex::Lock lm (lock);
228 for (List::iterator i = playlists.begin(); i != playlists.end(); ++i) {
229 (*i)->destroy_region (r);
232 for (List::iterator i = unused_playlists.begin(); i != unused_playlists.end(); ++i) {
233 (*i)->destroy_region (r);
239 SessionPlaylists::find_equivalent_playlist_regions (boost::shared_ptr<Region> region, vector<boost::shared_ptr<Region> >& result)
241 for (List::iterator i = playlists.begin(); i != playlists.end(); ++i)
242 (*i)->get_region_list_equivalent_regions (region, result);
245 /** Return the number of playlists (not regions) that contain @a src */
247 SessionPlaylists::source_use_count (boost::shared_ptr<const Source> src) const
251 for (List::const_iterator p = playlists.begin(); p != playlists.end(); ++p) {
252 if ((*p)->uses_source (src)) {
261 SessionPlaylists::sync_all_regions_with_regions ()
263 Glib::Mutex::Lock lm (lock);
265 for (List::const_iterator p = playlists.begin(); p != playlists.end(); ++p) {
266 (*p)->sync_all_regions_with_regions ();
271 SessionPlaylists::update_after_tempo_map_change ()
273 for (List::iterator i = playlists.begin(); i != playlists.end(); ++i) {
274 (*i)->update_after_tempo_map_change ();
277 for (List::iterator i = unused_playlists.begin(); i != unused_playlists.end(); ++i) {
278 (*i)->update_after_tempo_map_change ();
283 SessionPlaylists::add_state (XMLNode* node, bool full_state)
285 XMLNode* child = node->add_child ("Playlists");
286 for (List::iterator i = playlists.begin(); i != playlists.end(); ++i) {
287 if (!(*i)->hidden()) {
289 child->add_child_nocopy ((*i)->get_state());
291 child->add_child_nocopy ((*i)->get_template());
296 child = node->add_child ("UnusedPlaylists");
297 for (List::iterator i = unused_playlists.begin(); i != unused_playlists.end(); ++i) {
298 if (!(*i)->hidden()) {
299 if (!(*i)->empty()) {
301 child->add_child_nocopy ((*i)->get_state());
303 child->add_child_nocopy ((*i)->get_template());
310 /** @return true for `stop cleanup', otherwise false */
312 SessionPlaylists::maybe_delete_unused (boost::function<int(boost::shared_ptr<Playlist>)> ask)
314 vector<boost::shared_ptr<Playlist> > playlists_tbd;
316 for (List::iterator x = unused_playlists.begin(); x != unused_playlists.end(); ++x) {
318 int status = ask (*x);
325 playlists_tbd.push_back (*x);
334 /* now delete any that were marked for deletion */
336 for (vector<boost::shared_ptr<Playlist> >::iterator x = playlists_tbd.begin(); x != playlists_tbd.end(); ++x) {
337 boost::shared_ptr<Playlist> keeper (*x);
338 (*x)->drop_references ();
341 playlists_tbd.clear ();
347 SessionPlaylists::load (Session& session, const XMLNode& node)
350 XMLNodeConstIterator niter;
351 boost::shared_ptr<Playlist> playlist;
353 nlist = node.children();
355 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
357 if ((playlist = XMLPlaylistFactory (session, **niter)) == 0) {
358 error << _("Session: cannot create Playlist from XML description.") << endmsg;
366 SessionPlaylists::load_unused (Session& session, const XMLNode& node)
369 XMLNodeConstIterator niter;
370 boost::shared_ptr<Playlist> playlist;
372 nlist = node.children();
374 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
376 if ((playlist = XMLPlaylistFactory (session, **niter)) == 0) {
377 error << _("Session: cannot create Playlist from XML description.") << endmsg;
381 // now manually untrack it
383 track (false, boost::weak_ptr<Playlist> (playlist));
389 boost::shared_ptr<Playlist>
390 SessionPlaylists::XMLPlaylistFactory (Session& session, const XMLNode& node)
393 return PlaylistFactory::create (session, node);
396 catch (failed_constructor& err) {
397 return boost::shared_ptr<Playlist>();
401 boost::shared_ptr<Crossfade>
402 SessionPlaylists::find_crossfade (const PBD::ID& id)
404 Glib::Mutex::Lock lm (lock);
406 boost::shared_ptr<Crossfade> c;
408 for (List::iterator i = playlists.begin(); i != playlists.end(); ++i) {
409 c = (*i)->find_crossfade (id);
415 for (List::iterator i = unused_playlists.begin(); i != unused_playlists.end(); ++i) {
416 c = (*i)->find_crossfade (id);
422 return boost::shared_ptr<Crossfade> ();
426 SessionPlaylists::region_use_count (boost::shared_ptr<Region> region) const
428 Glib::Mutex::Lock lm (lock);
431 for (List::iterator i = playlists.begin(); i != playlists.end(); ++i) {
432 cnt += (*i)->region_use_count (region);
435 for (List::iterator i = unused_playlists.begin(); i != unused_playlists.end(); ++i) {
436 cnt += (*i)->region_use_count (region);