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