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