fully implement and deploy explicit x-thread signal connection syntax (testing comes...
[ardour.git] / libs / ardour / session_export.cc
1 /*
2     Copyright (C) 1999-2008 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 */
19
20
21 #include "pbd/error.h"
22 #include <glibmm/thread.h>
23
24 #include "ardour/audio_diskstream.h"
25 #include "ardour/audioengine.h"
26 #include "ardour/butler.h"
27 #include "ardour/export_failed.h"
28 #include "ardour/export_file_io.h"
29 #include "ardour/export_handler.h"
30 #include "ardour/export_status.h"
31 #include "ardour/export_utilities.h"
32 #include "ardour/route.h"
33 #include "ardour/session.h"
34
35 #include "i18n.h"
36
37 using namespace std;
38 using namespace ARDOUR;
39 using namespace PBD;
40
41 boost::shared_ptr<ExportHandler>
42 Session::get_export_handler ()
43 {
44         if (!export_handler) {
45                 export_handler.reset (new ExportHandler (*this));
46         }
47
48         return export_handler;
49 }
50
51 boost::shared_ptr<ExportStatus>
52 Session::get_export_status ()
53 {
54         if (!export_status) {
55                 export_status.reset (new ExportStatus ());
56         }
57
58         return export_status;
59 }
60
61
62 int
63 Session::pre_export ()
64 {
65         get_export_status (); // Init export_status
66
67         _butler->wait_until_finished ();
68
69         /* take everyone out of awrite to avoid disasters */
70
71         {
72                 boost::shared_ptr<RouteList> r = routes.reader ();
73
74                 for (RouteList::iterator i = r->begin(); i != r->end(); ++i) {
75                         (*i)->protect_automation ();
76                 }
77         }
78
79         /* make sure we are actually rolling */
80
81         if (get_record_enabled()) {
82                 disable_record (false);
83         }
84
85         /* no slaving */
86
87         post_export_sync = config.get_external_sync ();
88         post_export_position = _transport_frame;
89
90         config.set_external_sync (false);
91
92         _exporting = true;
93         export_status->running = true;
94         export_status->Aborting.connect_same_thread (*this, boost::bind (&Session::stop_audio_export, this));
95         export_status->Finished.connect_same_thread (*this, boost::bind (&Session::finalize_audio_export, this));
96
97         return 0;
98 }
99
100 int
101 Session::start_audio_export (nframes_t position, bool realtime)
102 {
103         if (!_exporting) {
104                 pre_export ();
105         }
106
107         /* get everyone to the right position */
108
109         {
110                 boost::shared_ptr<DiskstreamList> dsl = diskstreams.reader();
111
112                 for (DiskstreamList::iterator i = dsl->begin(); i != dsl->end(); ++i) {
113                         if ((*i)-> seek (position, true)) {
114                                 error << string_compose (_("%1: cannot seek to %2 for export"),
115                                                   (*i)->name(), position)
116                                       << endmsg;
117                                 return -1;
118                         }
119                 }
120         }
121
122         /* we just did the core part of a locate() call above, but
123            for the sake of any GUI, put the _transport_frame in
124            the right place too.
125         */
126
127         _transport_frame = position;
128
129         _exporting_realtime = realtime;
130         export_status->stop = false;
131
132         /* get transport ready. note how this is calling butler functions
133            from a non-butler thread. we waited for the butler to stop
134            what it was doing earlier in Session::pre_export() and nothing
135            since then has re-awakened it.
136          */
137
138         set_transport_speed (1.0, false);
139         butler_transport_work ();
140         g_atomic_int_set (&_butler->should_do_transport_work, 0);
141         post_transport ();
142
143         /* we are ready to go ... */
144
145         if (!_engine.connected()) {
146                 return -1;
147         }
148
149         if (realtime) {
150                 last_process_function = process_function;
151                 process_function = &Session::process_export;
152         } else {
153                 _engine.Freewheel.connect_same_thread (export_freewheel_connection, boost::bind (&Session::process_export_fw, this, _1));
154                 return _engine.freewheel (true);
155         }
156
157         return 0;
158 }
159
160 void
161 Session::process_export (nframes_t nframes)
162 {
163         try {
164
165                 if (export_status->stop) {
166                         stop_audio_export ();
167                         return;
168                 }
169
170                 if (!_exporting_realtime) {
171                         /* make sure we've caught up with disk i/o, since
172                         we're running faster than realtime c/o JACK.
173                         */
174
175                         _butler->wait_until_finished ();
176                 }
177
178                 /* do the usual stuff */
179
180                 process_without_events (nframes);
181
182                 /* handle export - XXX what about error handling? */
183
184                 ProcessExport (nframes);
185
186         } catch (ExportFailed e) {
187                 export_status->abort (true);
188         }
189 }
190
191 int
192 Session::process_export_fw (nframes_t nframes)
193 {
194         process_export (nframes);
195         return 0;
196 }
197
198 int
199 Session::stop_audio_export ()
200 {
201         if (_exporting_realtime) {
202                 process_function = last_process_function;
203         } else {
204                 export_freewheel_connection.disconnect();
205         }
206
207         /* can't use stop_transport() here because we need
208            an immediate halt and don't require all the declick
209            stuff that stop_transport() implements.
210         */
211
212         realtime_stop (true, true);
213         _butler->schedule_transport_work ();
214
215         if (!export_status->aborted()) {
216                 ExportReadFinished ();
217         } else {
218                 finalize_audio_export ();
219         }
220
221         return 0;
222
223 }
224
225 void
226 Session::finalize_audio_export ()
227 {
228         _exporting = false;
229         export_status->running = false;
230
231         if (!_exporting_realtime) {
232                 _engine.freewheel (false);
233                 _exporting_realtime = false;
234         }
235
236         /* Clean up */
237
238         /* BOOST SIGNAL are these necessary? 
239            ProcessExport.clear();
240            ExportReadFinished.clear();
241         */
242
243         export_freewheel_connection.disconnect();
244         export_handler.reset();
245         export_status.reset();
246
247         /* restart slaving */
248
249         if (post_export_sync) {
250                 config.set_external_sync (true);
251         } else {
252                 locate (post_export_position, false, false, false);
253         }
254 }