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