lots and lots of work to correctly deduce AU IO configurations and related issues
[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 #include <glibmm/thread.h>
22
23 #include <pbd/error.h>
24 #include <pbd/convert.h>
25 #include <pbd/pthread_utils.h>
26 #include <pbd/stacktrace.h>
27
28 #include <ardour/source_factory.h>
29 #include <ardour/sndfilesource.h>
30 #include <ardour/silentfilesource.h>
31 #include <ardour/configuration.h>
32
33 #ifdef  HAVE_COREAUDIO
34 #define USE_COREAUDIO_FOR_FILES
35 #endif
36
37 #ifdef USE_COREAUDIO_FOR_FILES
38 #include <ardour/coreaudiosource.h>
39 #endif
40
41
42 #include "i18n.h"
43
44 using namespace ARDOUR;
45 using namespace std;
46 using namespace PBD;
47 using namespace sigc;
48
49 sigc::signal<void,boost::shared_ptr<Source> > SourceFactory::SourceCreated;
50 Glib::Cond* SourceFactory::PeaksToBuild;
51 Glib::StaticMutex SourceFactory::peak_building_lock = GLIBMM_STATIC_MUTEX_INIT;
52 std::list<boost::weak_ptr<AudioSource> > SourceFactory::files_with_peaks;
53
54 static void 
55 peak_thread_work ()
56 {
57         PBD::ThreadCreated (pthread_self(), string ("peakbuilder-") + to_string (pthread_self(), std::dec));
58
59         while (true) {
60
61                 SourceFactory::peak_building_lock.lock ();
62                 
63           wait:
64                 if (SourceFactory::files_with_peaks.empty()) {
65                         SourceFactory::PeaksToBuild->wait (SourceFactory::peak_building_lock);
66                 }
67
68                 if (SourceFactory::files_with_peaks.empty()) {
69                         goto wait;
70                 }
71
72                 boost::shared_ptr<AudioSource> as (SourceFactory::files_with_peaks.front().lock());
73                 SourceFactory::files_with_peaks.pop_front ();
74                 SourceFactory::peak_building_lock.unlock ();
75                 
76                 if (!as) {
77                         continue;
78                 }
79
80                 as->setup_peakfile ();
81         }
82 }
83
84 void
85 SourceFactory::init ()
86 {
87         PeaksToBuild = new Glib::Cond();
88
89         for (int n = 0; n < 2; ++n) {
90                 Glib::Thread::create (sigc::ptr_fun (::peak_thread_work), false);
91         }
92 }
93
94 int
95 SourceFactory::setup_peakfile (boost::shared_ptr<Source> s, bool async)
96 {
97         boost::shared_ptr<AudioSource> as (boost::dynamic_pointer_cast<AudioSource> (s));
98
99         if (as) {
100
101                 if (async) {
102
103                         Glib::Mutex::Lock lm (peak_building_lock);
104                         files_with_peaks.push_back (boost::weak_ptr<AudioSource> (as));
105                         PeaksToBuild->broadcast ();
106
107                 } else {
108
109                         if (as->setup_peakfile ()) {
110                                 error << string_compose("SourceFactory: could not set up peakfile for %1", as->name()) << endmsg;
111                                 return -1;
112                         }
113                 }
114         }
115
116         return 0;
117 }
118
119 boost::shared_ptr<Source>
120 SourceFactory::createSilent (Session& s, const XMLNode& node, nframes_t nframes, float sr)
121 {
122         boost::shared_ptr<Source> ret (new SilentFileSource (s, node, nframes, sr));
123         // no analysis data - the file is non-existent
124         SourceCreated (ret);
125         return ret;
126 }
127
128 boost::shared_ptr<Source>
129 SourceFactory::create (Session& s, const XMLNode& node, bool defer_peaks)
130 {
131         try {
132
133                 boost::shared_ptr<Source> ret (new SndFileSource (s, node));
134                 if (setup_peakfile (ret, defer_peaks)) {
135                         return boost::shared_ptr<Source>();
136                 }
137                 ret->check_for_analysis_data_on_disk ();
138                 SourceCreated (ret);
139                 return ret;
140         } 
141
142
143         catch (failed_constructor& err) {       
144
145 #ifdef USE_COREAUDIO_FOR_FILES
146
147                 /* this is allowed to throw */
148
149                 boost::shared_ptr<Source> ret (new CoreAudioSource (s, node));
150                 if (setup_peakfile (ret, defer_peaks)) {
151                         return boost::shared_ptr<Source>();
152                 }
153                 ret->check_for_analysis_data_on_disk ();
154                 SourceCreated (ret);
155                 return ret;
156 #else
157                 throw; // rethrow 
158 #endif
159
160         }
161
162         return boost::shared_ptr<Source>(); // keep stupid gcc happy
163 }
164
165 boost::shared_ptr<Source>
166 SourceFactory::createReadable (Session& s, string path, int chn, AudioFileSource::Flag flags, bool announce, bool defer_peaks)
167 {
168         if (!(flags & Destructive)) {
169
170                 try {
171
172                         boost::shared_ptr<Source> ret (new SndFileSource (s, path, chn, flags));
173                         if (setup_peakfile (ret, defer_peaks)) {
174                                 return boost::shared_ptr<Source>();
175                         }
176                         ret->check_for_analysis_data_on_disk ();
177                         if (announce) {
178                                 SourceCreated (ret);
179                         }
180                         return ret;
181                 }
182                 
183                 catch (failed_constructor& err) {
184
185                         /* this is allowed to throw */
186
187 #ifdef USE_COREAUDIO_FOR_FILES
188
189                         boost::shared_ptr<Source> ret (new CoreAudioSource (s, path, chn, flags));
190                         if (setup_peakfile (ret, defer_peaks)) {
191                                 return boost::shared_ptr<Source>();
192                         }
193                         ret->check_for_analysis_data_on_disk ();
194                         if (announce) {
195                                 SourceCreated (ret);
196                         }
197                         return ret;
198
199 #else
200                         throw; // rethrow
201 #endif
202                 }
203
204         } else {
205
206         }
207
208         return boost::shared_ptr<Source>();
209 }
210
211 boost::shared_ptr<Source>
212 SourceFactory::createWritable (Session& s, std::string path, bool destructive, nframes_t rate, bool announce, bool defer_peaks)
213 {
214         /* this might throw failed_constructor(), which is OK */
215
216         boost::shared_ptr<Source> ret (new SndFileSource 
217                                        (s, path, 
218                                         Config->get_native_file_data_format(),
219                                         Config->get_native_file_header_format(),
220                                         rate,
221                                         (destructive ? AudioFileSource::Flag (SndFileSource::default_writable_flags | AudioFileSource::Destructive) :
222                                          SndFileSource::default_writable_flags)));      
223
224         if (setup_peakfile (ret, defer_peaks)) {
225                 return boost::shared_ptr<Source>();
226         }
227
228         // no analysis data - this is a new file
229
230         if (announce) {
231                 SourceCreated (ret);
232         }
233         return ret;
234 }