headless: Use direct x-thread wakeup instead of sleep/spin
[ardour.git] / headless / load_session.cc
1 #include <iostream>
2 #include <cstdlib>
3 #include <getopt.h>
4
5 #ifndef PLATFORM_WINDOWS
6 #include <signal.h>
7 #endif
8
9 #include <glibmm.h>
10
11 #include "pbd/convert.h"
12 #include "pbd/crossthread.h"
13 #include "pbd/failed_constructor.h"
14 #include "pbd/error.h"
15 #include "pbd/debug.h"
16
17 #include "ardour/ardour.h"
18 #include "ardour/audioengine.h"
19 #include "ardour/revision.h"
20 #include "ardour/session.h"
21
22 #include "control_protocol/control_protocol.h"
23
24 #include "misc.h"
25
26 using namespace std;
27 using namespace ARDOUR;
28 using namespace PBD;
29
30 static const char* localedir = LOCALEDIR;
31
32 static string backend_client_name;
33 static string backend_name = "JACK";
34 static CrossThreadChannel xthread (true);
35 static TestReceiver test_receiver;
36
37 /** @param dir Session directory.
38  *  @param state Session state file, without .ardour suffix.
39  */
40 static Session *
41 load_session (string dir, string state)
42 {
43         SessionEvent::create_per_thread_pool ("test", 512);
44
45         test_receiver.listen_to (error);
46         test_receiver.listen_to (info);
47         test_receiver.listen_to (fatal);
48         test_receiver.listen_to (warning);
49
50         AudioEngine* engine = AudioEngine::create ();
51
52         if (!engine->set_backend (backend_name, backend_client_name, "")) {
53                 std::cerr << "Cannot set Audio/MIDI engine backend\n";
54                 ::exit (1);
55         }
56
57         if (engine->start () != 0) {
58                 std::cerr << "Cannot start Audio/MIDI engine\n";
59                 ::exit (1);
60         }
61
62         Session* session = new Session (*engine, dir, state);
63         engine->set_session (session);
64         return session;
65 }
66
67 static void
68 access_action (const std::string& action_group, const std::string& action_item)
69 {
70         if (action_group == "Common" && action_item == "Quit") {
71                 xthread.deliver ('x');
72         }
73 }
74
75 static void
76 engine_halted (const char* reason)
77 {
78         cerr << "The audio backend has been shutdown";
79         if (reason && strlen (reason) > 0) {
80                 cerr << ": " << reason;
81         } else {
82                 cerr << ".";
83         }
84         cerr << endl;
85         xthread.deliver ('x');
86 }
87
88 #ifndef PLATFORM_WINDOWS
89 static void wearedone (int) {
90         cerr << "caught signal - terminating." << endl;
91         xthread.deliver ('x');
92 }
93 #endif
94
95 static void
96 print_version ()
97 {
98         cout
99                 << PROGRAM_NAME
100                 << VERSIONSTRING
101                 << " (built using "
102                 << ARDOUR::revision
103 #ifdef __GNUC__
104                 << " and GCC version " << __VERSION__
105 #endif
106                 << ')'
107                 << endl;
108 }
109
110 static void
111 print_help ()
112 {
113         cout << "Usage: hardour [OPTIONS]... DIR SNAPSHOT_NAME\n\n"
114              << "  DIR                         Directory/Folder to load session from\n"
115              << "  SNAPSHOT_NAME               Name of session/snapshot to load (without .ardour at end\n"
116              << "  -v, --version               Show version information\n"
117              << "  -h, --help                  Print this message\n"
118              << "  -c, --name <name>           Use a specific backend client name, default is ardour\n"
119              << "  -d, --disable-plugins       Disable all plugins in an existing session\n"
120              << "  -D, --debug <options>       Set debug flags. Use \"-D list\" to see available options\n"
121              << "  -O, --no-hw-optimizations   Disable h/w specific optimizations\n"
122              << "  -P, --no-connect-ports      Do not connect any ports at startup\n"
123 #ifdef WINDOWS_VST_SUPPORT
124              << "  -V, --novst                 Do not use VST support\n"
125 #endif
126                 ;
127 }
128
129 int main (int argc, char* argv[])
130 {
131         const char *optstring = "vhBdD:c:VOU:P";
132
133         const struct option longopts[] = {
134                 { "version", 0, 0, 'v' },
135                 { "help", 0, 0, 'h' },
136                 { "bypass-plugins", 1, 0, 'B' },
137                 { "disable-plugins", 1, 0, 'd' },
138                 { "debug", 1, 0, 'D' },
139                 { "name", 1, 0, 'c' },
140                 { "novst", 0, 0, 'V' },
141                 { "no-hw-optimizations", 0, 0, 'O' },
142                 { "uuid", 1, 0, 'U' },
143                 { "no-connect-ports", 0, 0, 'P' },
144                 { 0, 0, 0, 0 }
145         };
146
147         int option_index = 0;
148         int c = 0;
149
150         bool use_vst = true;
151         bool try_hw_optimization = true;
152
153         backend_client_name = PBD::downcase (std::string(PROGRAM_NAME));
154
155         while (1) {
156                 c = getopt_long (argc, argv, optstring, longopts, &option_index);
157
158                 if (c == -1) {
159                         break;
160                 }
161
162                 switch (c) {
163                 case 0:
164                         break;
165
166                 case 'v':
167                         print_version ();
168                         ::exit (0);
169                         break;
170
171                 case 'h':
172                         print_help ();
173                         exit (0);
174                         break;
175
176                 case 'c':
177                         backend_client_name = optarg;
178                         break;
179
180                 case 'B':
181                         ARDOUR::Session::set_bypass_all_loaded_plugins (true);
182                         break;
183
184                 case 'd':
185                         ARDOUR::Session::set_disable_all_loaded_plugins (true);
186                         break;
187
188                 case 'D':
189                         if (PBD::parse_debug_options (optarg)) {
190                                 ::exit (1);
191                         }
192                         break;
193
194                 case 'O':
195                         try_hw_optimization = false;
196                         break;
197
198                 case 'P':
199                         ARDOUR::Port::set_connecting_blocked (true);
200                         break;
201
202                 case 'V':
203 #ifdef WINDOWS_VST_SUPPORT
204                         use_vst = false;
205 #endif /* WINDOWS_VST_SUPPORT */
206                         break;
207
208                 default:
209                         print_help ();
210                         ::exit (1);
211                 }
212         }
213
214         if (argc < 3) {
215                 print_help ();
216                 ::exit (1);
217         }
218
219         if (!ARDOUR::init (use_vst, try_hw_optimization, localedir)) {
220                 cerr << "Ardour failed to initialize\n" << endl;
221                 ::exit (1);
222         }
223
224         Session* s = 0;
225
226         try {
227                 s = load_session (argv[optind], argv[optind+1]);
228         } catch (failed_constructor& e) {
229                 cerr << "failed_constructor: " << e.what() << "\n";
230                 exit (EXIT_FAILURE);
231         } catch (AudioEngine::PortRegistrationFailure& e) {
232                 cerr << "PortRegistrationFailure: " << e.what() << "\n";
233                 exit (EXIT_FAILURE);
234         } catch (exception& e) {
235                 cerr << "exception: " << e.what() << "\n";
236                 exit (EXIT_FAILURE);
237         } catch (...) {
238                 cerr << "unknown exception.\n";
239                 exit (EXIT_FAILURE);
240         }
241
242         PBD::ScopedConnectionList con;
243         BasicUI::AccessAction.connect_same_thread (con, boost::bind (&access_action, _1, _2));
244         AudioEngine::instance()->Halted.connect_same_thread (con, boost::bind (&engine_halted, _1));
245
246 #ifndef PLATFORM_WINDOWS
247         signal(SIGINT, wearedone);
248         signal(SIGTERM, wearedone);
249 #endif
250
251         s->request_transport_speed (1.0);
252
253         char msg;
254         do {} while (0 == xthread.receive (msg, true));
255
256         AudioEngine::instance()->remove_session ();
257         delete s;
258         AudioEngine::instance()->stop ();
259
260         AudioEngine::destroy ();
261
262         return 0;
263 }