fix crash when copy'ing latent plugins
[ardour.git] / libs / ardour / source_factory.cc
1 /*
2     Copyright (C) 2000-2006 Paul Davis
3
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.
8
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.
13
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.
17
18     $Id$
19 */
20
21 #ifdef WAF_BUILD
22 #include "libardour-config.h"
23 #endif
24
25 #include "pbd/error.h"
26 #include "pbd/convert.h"
27 #include "pbd/pthread_utils.h"
28 #include "pbd/stacktrace.h"
29
30 #include "ardour/audioplaylist.h"
31 #include "ardour/audio_playlist_source.h"
32 #include "ardour/boost_debug.h"
33 #include "ardour/midi_playlist.h"
34 #include "ardour/midi_playlist_source.h"
35 #include "ardour/source.h"
36 #include "ardour/source_factory.h"
37 #include "ardour/sndfilesource.h"
38 #include "ardour/silentfilesource.h"
39 #include "ardour/smf_source.h"
40 #include "ardour/session.h"
41
42 #ifdef  HAVE_COREAUDIO
43 #include "ardour/coreaudiosource.h"
44 #endif
45
46
47 #include "pbd/i18n.h"
48
49 using namespace ARDOUR;
50 using namespace std;
51 using namespace PBD;
52
53 PBD::Signal1<void,boost::shared_ptr<Source> > SourceFactory::SourceCreated;
54 Glib::Threads::Cond SourceFactory::PeaksToBuild;
55 Glib::Threads::Mutex SourceFactory::peak_building_lock;
56 std::list<boost::weak_ptr<AudioSource> > SourceFactory::files_with_peaks;
57
58 static int active_threads = 0;
59
60 static void
61 peak_thread_work ()
62 {
63         SessionEvent::create_per_thread_pool (X_("PeakFile Builder "), 64);
64
65         while (true) {
66
67                 SourceFactory::peak_building_lock.lock ();
68
69           wait:
70                 if (SourceFactory::files_with_peaks.empty()) {
71                         SourceFactory::PeaksToBuild.wait (SourceFactory::peak_building_lock);
72                 }
73
74                 if (SourceFactory::files_with_peaks.empty()) {
75                         goto wait;
76                 }
77
78                 boost::shared_ptr<AudioSource> as (SourceFactory::files_with_peaks.front().lock());
79                 SourceFactory::files_with_peaks.pop_front ();
80                 ++active_threads;
81                 SourceFactory::peak_building_lock.unlock ();
82
83                 if (!as) {
84                         continue;
85                 }
86
87                 as->setup_peakfile ();
88                 SourceFactory::peak_building_lock.lock ();
89                 --active_threads;
90                 SourceFactory::peak_building_lock.unlock ();
91         }
92 }
93
94 int
95 SourceFactory::peak_work_queue_length ()
96 {
97         // ideally we'd loop over the queue and check for duplicates
98         // and existing valid peak-files..
99         return SourceFactory::files_with_peaks.size () + active_threads;
100 }
101
102 void
103 SourceFactory::init ()
104 {
105         for (int n = 0; n < 2; ++n) {
106                 Glib::Threads::Thread::create (sigc::ptr_fun (::peak_thread_work));
107         }
108 }
109
110 int
111 SourceFactory::setup_peakfile (boost::shared_ptr<Source> s, bool async)
112 {
113         boost::shared_ptr<AudioSource> as (boost::dynamic_pointer_cast<AudioSource> (s));
114
115         if (as) {
116
117                 // immediately set 'peakfile-path' for empty and NoPeakFile sources
118                 if (async && !as->empty() && !(as->flags() & Source::NoPeakFile)) {
119
120                         Glib::Threads::Mutex::Lock lm (peak_building_lock);
121                         files_with_peaks.push_back (boost::weak_ptr<AudioSource> (as));
122                         PeaksToBuild.broadcast ();
123
124                 } else {
125
126                         if (as->setup_peakfile ()) {
127                                 error << string_compose("SourceFactory: could not set up peakfile for %1", as->name()) << endmsg;
128                                 return -1;
129                         }
130                 }
131         }
132
133         return 0;
134 }
135
136 boost::shared_ptr<Source>
137 SourceFactory::createSilent (Session& s, const XMLNode& node, framecnt_t nframes, float sr)
138 {
139         Source* src = new SilentFileSource (s, node, nframes, sr);
140 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
141         // boost_debug_shared_ptr_mark_interesting (src, "Source");
142 #endif
143         boost::shared_ptr<Source> ret (src);
144         // no analysis data - the file is non-existent
145         SourceCreated (ret);
146         return ret;
147 }
148
149 boost::shared_ptr<Source>
150 SourceFactory::create (Session& s, const XMLNode& node, bool defer_peaks)
151 {
152         DataType type = DataType::AUDIO;
153         XMLProperty const * prop = node.property("type");
154
155         if (prop) {
156                 type = DataType (prop->value());
157         }
158
159         if (type == DataType::AUDIO) {
160
161                 /* it could be nested */
162
163                 if (node.property ("playlist") != 0) {
164
165                         try {
166                                 boost::shared_ptr<AudioPlaylistSource> ap (new AudioPlaylistSource (s, node));
167
168                                 if (setup_peakfile (ap, true)) {
169                                         return boost::shared_ptr<Source>();
170                                 }
171
172                                 ap->check_for_analysis_data_on_disk ();
173
174                                 SourceCreated (ap);
175                                 return ap;
176
177                         } catch (failed_constructor&) {
178                                 /* oh well, so much for that then ... */
179                         }
180
181                 } else {
182
183
184                         try {
185                                 Source* src = new SndFileSource (s, node);
186 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
187                                 // boost_debug_shared_ptr_mark_interesting (src, "Source");
188 #endif
189                                 boost::shared_ptr<Source> ret (src);
190                                 if (setup_peakfile (ret, defer_peaks)) {
191                                         return boost::shared_ptr<Source>();
192                                 }
193                                 ret->check_for_analysis_data_on_disk ();
194                                 SourceCreated (ret);
195                                 return ret;
196                         }
197
198                         catch (failed_constructor& err) {
199
200 #ifdef HAVE_COREAUDIO
201
202                                 /* this is allowed to throw */
203
204                                 Source *src = new CoreAudioSource (s, node);
205 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
206                                 // boost_debug_shared_ptr_mark_interesting (src, "Source");
207 #endif
208                                 boost::shared_ptr<Source> ret (src);
209
210                                 if (setup_peakfile (ret, defer_peaks)) {
211                                         return boost::shared_ptr<Source>();
212                                 }
213
214                                 ret->check_for_analysis_data_on_disk ();
215                                 SourceCreated (ret);
216                                 return ret;
217 #else
218                                 throw; // rethrow
219 #endif
220                         }
221                 }
222         } else if (type == DataType::MIDI) {
223                 boost::shared_ptr<SMFSource> src (new SMFSource (s, node));
224                 Source::Lock lock(src->mutex());
225                 src->load_model (lock, true);
226 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
227                 // boost_debug_shared_ptr_mark_interesting (src, "Source");
228 #endif
229                 src->check_for_analysis_data_on_disk ();
230                 SourceCreated (src);
231                 return src;
232         }
233
234         return boost::shared_ptr<Source>();
235 }
236
237 boost::shared_ptr<Source>
238 SourceFactory::createExternal (DataType type, Session& s, const string& path,
239                                int chn, Source::Flag flags, bool announce, bool defer_peaks)
240 {
241         if (type == DataType::AUDIO) {
242
243                 if (!(flags & Destructive)) {
244
245                         try {
246
247                                 Source* src = new SndFileSource (s, path, chn, flags);
248 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
249                                 // boost_debug_shared_ptr_mark_interesting (src, "Source");
250 #endif
251                                 boost::shared_ptr<Source> ret (src);
252
253                                 if (setup_peakfile (ret, defer_peaks)) {
254                                         return boost::shared_ptr<Source>();
255                                 }
256
257                                 ret->check_for_analysis_data_on_disk ();
258                                 if (announce) {
259                                         SourceCreated (ret);
260                                 }
261                                 return ret;
262                         }
263
264                         catch (failed_constructor& err) {
265 #ifdef HAVE_COREAUDIO
266
267                                 Source* src = new CoreAudioSource (s, path, chn, flags);
268 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
269                                 // boost_debug_shared_ptr_mark_interesting (src, "Source");
270 #endif
271                                 boost::shared_ptr<Source> ret (src);
272                                 if (setup_peakfile (ret, defer_peaks)) {
273                                         return boost::shared_ptr<Source>();
274                                 }
275                                 ret->check_for_analysis_data_on_disk ();
276                                 if (announce) {
277                                         SourceCreated (ret);
278                                 }
279                                 return ret;
280
281 #else
282                                 throw; // rethrow
283 #endif
284                         }
285
286                 } else {
287                         // eh?
288                 }
289
290         } else if (type == DataType::MIDI) {
291
292                 boost::shared_ptr<SMFSource> src (new SMFSource (s, path));
293                 Source::Lock lock(src->mutex());
294                 src->load_model (lock, true);
295 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
296                 // boost_debug_shared_ptr_mark_interesting (src, "Source");
297 #endif
298
299                 if (announce) {
300                         SourceCreated (src);
301                 }
302
303                 return src;
304
305         }
306
307         return boost::shared_ptr<Source>();
308 }
309
310 boost::shared_ptr<Source>
311 SourceFactory::createWritable (DataType type, Session& s, const std::string& path,
312                                bool destructive, framecnt_t rate, bool announce, bool defer_peaks)
313 {
314         /* this might throw failed_constructor(), which is OK */
315
316         if (type == DataType::AUDIO) {
317                 Source* src = new SndFileSource (s, path, string(),
318                                                  s.config.get_native_file_data_format(),
319                                                  s.config.get_native_file_header_format(),
320                                                  rate,
321                                                  (destructive
322                                                   ? Source::Flag (SndFileSource::default_writable_flags | Source::Destructive)
323                                                   : SndFileSource::default_writable_flags));
324 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
325                 // boost_debug_shared_ptr_mark_interesting (src, "Source");
326 #endif
327                 boost::shared_ptr<Source> ret (src);
328
329                 if (setup_peakfile (ret, defer_peaks)) {
330                         return boost::shared_ptr<Source>();
331                 }
332
333                 // no analysis data - this is a new file
334
335                 if (announce) {
336                         SourceCreated (ret);
337                 }
338                 return ret;
339
340         } else if (type == DataType::MIDI) {
341                 // XXX writable flags should belong to MidiSource too
342                 boost::shared_ptr<SMFSource> src (new SMFSource (s, path, SndFileSource::default_writable_flags));
343                 assert (src->writable ());
344
345                 Source::Lock lock(src->mutex());
346                 src->load_model (lock, true);
347 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
348                 // boost_debug_shared_ptr_mark_interesting (src, "Source");
349 #endif
350
351                 // no analysis data - this is a new file
352
353                 if (announce) {
354                         SourceCreated (src);
355                 }
356                 return src;
357
358         }
359
360         return boost::shared_ptr<Source> ();
361 }
362
363 boost::shared_ptr<Source>
364 SourceFactory::createForRecovery (DataType type, Session& s, const std::string& path, int chn)
365 {
366         /* this might throw failed_constructor(), which is OK */
367
368         if (type == DataType::AUDIO) {
369                 Source* src = new SndFileSource (s, path, chn);
370
371 #ifdef BOOST_SP_ENABLE_DEBUG_HOOKS
372                 // boost_debug_shared_ptr_mark_interesting (src, "Source");
373 #endif
374                 boost::shared_ptr<Source> ret (src);
375
376                 if (setup_peakfile (ret, false)) {
377                         return boost::shared_ptr<Source>();
378                 }
379
380                 // no analysis data - this is still basically a new file (we
381                 // crashed while recording.
382
383                 // always announce these files
384
385                 SourceCreated (ret);
386
387                 return ret;
388
389         } else if (type == DataType::MIDI) {
390                 error << _("Recovery attempted on a MIDI file - not implemented") << endmsg;
391         }
392
393         return boost::shared_ptr<Source> ();
394 }
395
396 boost::shared_ptr<Source>
397 SourceFactory::createFromPlaylist (DataType type, Session& s, boost::shared_ptr<Playlist> p, const PBD::ID& orig, const std::string& name,
398                                    uint32_t chn, frameoffset_t start, framecnt_t len, bool copy, bool defer_peaks)
399 {
400         if (type == DataType::AUDIO) {
401                 try {
402
403                         boost::shared_ptr<AudioPlaylist> ap = boost::dynamic_pointer_cast<AudioPlaylist>(p);
404
405                         if (ap) {
406
407                                 if (copy) {
408                                         ap.reset (new AudioPlaylist (ap, start, len, name, true));
409                                         start = 0;
410                                 }
411
412                                 Source* src = new AudioPlaylistSource (s, orig, name, ap, chn, start, len, Source::Flag (0));
413                                 boost::shared_ptr<Source> ret (src);
414
415                                 if (setup_peakfile (ret, defer_peaks)) {
416                                         return boost::shared_ptr<Source>();
417                                 }
418
419                                 ret->check_for_analysis_data_on_disk ();
420                                 SourceCreated (ret);
421                                 return ret;
422                         }
423                 }
424
425                 catch (failed_constructor& err) {
426                         /* relax - return at function scope */
427                 }
428
429         } else if (type == DataType::MIDI) {
430
431                 try {
432
433                         boost::shared_ptr<MidiPlaylist> ap = boost::dynamic_pointer_cast<MidiPlaylist>(p);
434
435                         if (ap) {
436
437                                 if (copy) {
438                                         ap.reset (new MidiPlaylist (ap, start, len, name, true));
439                                         start = 0;
440                                 }
441
442                                 Source* src = new MidiPlaylistSource (s, orig, name, ap, chn, start, len, Source::Flag (0));
443                                 boost::shared_ptr<Source> ret (src);
444
445                                 SourceCreated (ret);
446                                 return ret;
447                         }
448                 }
449
450                 catch (failed_constructor& err) {
451                         /* relax - return at function scope */
452                 }
453
454         }
455
456         return boost::shared_ptr<Source>();
457 }
458