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