possible fix for race between diskstream buffer overwrite and channel setup
[ardour2.git] / libs / ardour / session_playlists.cc
blob2f8ae9a8f02cbbf3381aab856c0285a22800ce2b
1 /*
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.
19 #include <vector>
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 "i18n.h"
31 using namespace std;
32 using namespace PBD;
33 using namespace ARDOUR;
35 SessionPlaylists::~SessionPlaylists ()
37 DEBUG_TRACE (DEBUG::Destruction, "delete playlists\n");
39 for (List::iterator i = playlists.begin(); i != playlists.end(); ) {
40 SessionPlaylists::List::iterator tmp;
42 tmp = i;
43 ++tmp;
45 DEBUG_TRACE(DEBUG::Destruction, string_compose ("Dropping for used playlist %1 ; pre-ref = %2\n", (*i)->name(), (*i).use_count()));
46 boost::shared_ptr<Playlist> keeper (*i);
47 (*i)->drop_references ();
49 i = tmp;
52 DEBUG_TRACE (DEBUG::Destruction, "delete unused playlists\n");
53 for (List::iterator i = unused_playlists.begin(); i != unused_playlists.end(); ) {
54 List::iterator tmp;
56 tmp = i;
57 ++tmp;
59 DEBUG_TRACE(DEBUG::Destruction, string_compose ("Dropping for unused playlist %1 ; pre-ref = %2\n", (*i)->name(), (*i).use_count()));
60 boost::shared_ptr<Playlist> keeper (*i);
61 (*i)->drop_references ();
63 i = tmp;
66 playlists.clear ();
67 unused_playlists.clear ();
70 bool
71 SessionPlaylists::add (boost::shared_ptr<Playlist> playlist)
73 Glib::Mutex::Lock lm (lock);
75 bool const existing = find (playlists.begin(), playlists.end(), playlist) != playlists.end();
77 if (!existing) {
78 playlists.insert (playlists.begin(), playlist);
79 playlist->InUse.connect_same_thread (*this, boost::bind (&SessionPlaylists::track, this, _1, boost::weak_ptr<Playlist>(playlist)));
82 return existing;
85 void
86 SessionPlaylists::remove (boost::shared_ptr<Playlist> playlist)
88 Glib::Mutex::Lock lm (lock);
90 List::iterator i;
92 i = find (playlists.begin(), playlists.end(), playlist);
93 if (i != playlists.end()) {
94 playlists.erase (i);
97 i = find (unused_playlists.begin(), unused_playlists.end(), playlist);
98 if (i != unused_playlists.end()) {
99 unused_playlists.erase (i);
104 void
105 SessionPlaylists::track (bool inuse, boost::weak_ptr<Playlist> wpl)
107 boost::shared_ptr<Playlist> pl(wpl.lock());
109 if (!pl) {
110 return;
113 List::iterator x;
115 if (pl->hidden()) {
116 /* its not supposed to be visible */
117 return;
121 Glib::Mutex::Lock lm (lock);
123 if (!inuse) {
125 unused_playlists.insert (pl);
127 if ((x = playlists.find (pl)) != playlists.end()) {
128 playlists.erase (x);
132 } else {
134 playlists.insert (pl);
136 if ((x = unused_playlists.find (pl)) != unused_playlists.end()) {
137 unused_playlists.erase (x);
143 uint32_t
144 SessionPlaylists::n_playlists () const
146 Glib::Mutex::Lock lm (lock);
147 return playlists.size();
150 boost::shared_ptr<Playlist>
151 SessionPlaylists::by_name (string name)
153 Glib::Mutex::Lock lm (lock);
155 for (List::iterator i = playlists.begin(); i != playlists.end(); ++i) {
156 if ((*i)->name() == name) {
157 return* i;
161 for (List::iterator i = unused_playlists.begin(); i != unused_playlists.end(); ++i) {
162 if ((*i)->name() == name) {
163 return* i;
167 return boost::shared_ptr<Playlist>();
170 boost::shared_ptr<Playlist>
171 SessionPlaylists::by_id (const PBD::ID& id)
173 Glib::Mutex::Lock lm (lock);
175 for (List::iterator i = playlists.begin(); i != playlists.end(); ++i) {
176 if ((*i)->id() == id) {
177 return* i;
181 for (List::iterator i = unused_playlists.begin(); i != unused_playlists.end(); ++i) {
182 if ((*i)->id() == id) {
183 return* i;
187 return boost::shared_ptr<Playlist>();
190 void
191 SessionPlaylists::unassigned (std::list<boost::shared_ptr<Playlist> > & list)
193 Glib::Mutex::Lock lm (lock);
195 for (List::iterator i = playlists.begin(); i != playlists.end(); ++i) {
196 if (!(*i)->get_orig_diskstream_id().to_s().compare ("0")) {
197 list.push_back (*i);
201 for (List::iterator i = unused_playlists.begin(); i != unused_playlists.end(); ++i) {
202 if (!(*i)->get_orig_diskstream_id().to_s().compare ("0")) {
203 list.push_back (*i);
208 void
209 SessionPlaylists::get (vector<boost::shared_ptr<Playlist> >& s)
211 Glib::Mutex::Lock lm (lock);
213 for (List::iterator i = playlists.begin(); i != playlists.end(); ++i) {
214 s.push_back (*i);
217 for (List::iterator i = unused_playlists.begin(); i != unused_playlists.end(); ++i) {
218 s.push_back (*i);
222 void
223 SessionPlaylists::find_equivalent_playlist_regions (boost::shared_ptr<Region> region, vector<boost::shared_ptr<Region> >& result)
225 for (List::iterator i = playlists.begin(); i != playlists.end(); ++i)
226 (*i)->get_region_list_equivalent_regions (region, result);
229 /** Return the number of playlists (not regions) that contain @a src */
230 uint32_t
231 SessionPlaylists::source_use_count (boost::shared_ptr<const Source> src) const
233 uint32_t count = 0;
234 for (List::const_iterator p = playlists.begin(); p != playlists.end(); ++p) {
235 for (Playlist::RegionList::const_iterator r = (*p)->region_list().begin();
236 r != (*p)->region_list().end(); ++r) {
237 if ((*r)->uses_source(src)) {
238 ++count;
239 break;
243 return count;
247 void
248 SessionPlaylists::update_after_tempo_map_change ()
250 for (List::iterator i = playlists.begin(); i != playlists.end(); ++i) {
251 (*i)->update_after_tempo_map_change ();
254 for (List::iterator i = unused_playlists.begin(); i != unused_playlists.end(); ++i) {
255 (*i)->update_after_tempo_map_change ();
259 void
260 SessionPlaylists::add_state (XMLNode* node, bool full_state)
262 XMLNode* child = node->add_child ("Playlists");
263 for (List::iterator i = playlists.begin(); i != playlists.end(); ++i) {
264 if (!(*i)->hidden()) {
265 if (!(*i)->empty()) {
266 if (full_state) {
267 child->add_child_nocopy ((*i)->get_state());
268 } else {
269 child->add_child_nocopy ((*i)->get_template());
275 child = node->add_child ("UnusedPlaylists");
276 for (List::iterator i = unused_playlists.begin(); i != unused_playlists.end(); ++i) {
277 if (!(*i)->hidden()) {
278 if (!(*i)->empty()) {
279 if (full_state) {
280 child->add_child_nocopy ((*i)->get_state());
281 } else {
282 child->add_child_nocopy ((*i)->get_template());
289 /** @return true for `stop cleanup', otherwise false */
290 bool
291 SessionPlaylists::maybe_delete_unused (boost::function<int(boost::shared_ptr<Playlist>)> ask)
293 vector<boost::shared_ptr<Playlist> > playlists_tbd;
295 for (List::iterator x = unused_playlists.begin(); x != unused_playlists.end(); ++x) {
297 int status = ask (*x);
299 switch (status) {
300 case -1:
301 return true;
303 case 0:
304 playlists_tbd.push_back (*x);
305 break;
307 default:
308 /* leave it alone */
309 break;
313 /* now delete any that were marked for deletion */
315 for (vector<boost::shared_ptr<Playlist> >::iterator x = playlists_tbd.begin(); x != playlists_tbd.end(); ++x) {
316 boost::shared_ptr<Playlist> keeper (*x);
317 (*x)->drop_references ();
320 playlists_tbd.clear ();
322 return false;
326 SessionPlaylists::load (Session& session, const XMLNode& node)
328 XMLNodeList nlist;
329 XMLNodeConstIterator niter;
330 boost::shared_ptr<Playlist> playlist;
332 nlist = node.children();
334 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
336 if ((playlist = XMLPlaylistFactory (session, **niter)) == 0) {
337 error << _("Session: cannot create Playlist from XML description.") << endmsg;
341 return 0;
345 SessionPlaylists::load_unused (Session& session, const XMLNode& node)
347 XMLNodeList nlist;
348 XMLNodeConstIterator niter;
349 boost::shared_ptr<Playlist> playlist;
351 nlist = node.children();
353 for (niter = nlist.begin(); niter != nlist.end(); ++niter) {
355 if ((playlist = XMLPlaylistFactory (session, **niter)) == 0) {
356 error << _("Session: cannot create Playlist from XML description.") << endmsg;
357 continue;
360 // now manually untrack it
362 track (false, boost::weak_ptr<Playlist> (playlist));
365 return 0;
368 boost::shared_ptr<Playlist>
369 SessionPlaylists::XMLPlaylistFactory (Session& session, const XMLNode& node)
371 try {
372 return PlaylistFactory::create (session, node);
375 catch (failed_constructor& err) {
376 return boost::shared_ptr<Playlist>();