Update GPL boilerplate and (C)
[ardour.git] / headless / load_session.cc
1 #include <cstdlib>
2 #include <getopt.h>
3 #include <iostream>
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/debug.h"
14 #include "pbd/error.h"
15 #include "pbd/failed_constructor.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 (EXIT_FAILURE);
55         }
56
57         if (engine->start () != 0) {
58                 std::cerr << "Cannot start Audio/MIDI engine\n";
59                 exit (EXIT_FAILURE);
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
90 wearedone (int)
91 {
92         cerr << "caught signal - terminating." << endl;
93         xthread.deliver ('x');
94 }
95 #endif
96
97 static void
98 print_version ()
99 {
100         cout
101             << PROGRAM_NAME
102             << VERSIONSTRING
103             << " (built using "
104             << ARDOUR::revision
105 #ifdef __GNUC__
106             << " and GCC version " << __VERSION__
107 #endif
108             << ')'
109             << endl;
110 }
111
112 static void
113 print_help ()
114 {
115         cout << "Usage: hardour [OPTIONS]... DIR SNAPSHOT_NAME\n\n"
116              << "  DIR                         Directory/Folder to load session from\n"
117              << "  SNAPSHOT_NAME               Name of session/snapshot to load (without .ardour at end\n"
118              << "  -v, --version               Show version information\n"
119              << "  -h, --help                  Print this message\n"
120              << "  -c, --name <name>           Use a specific backend client name, default is ardour\n"
121              << "  -d, --disable-plugins       Disable all plugins in an existing session\n"
122              << "  -D, --debug <options>       Set debug flags. Use \"-D list\" to see available options\n"
123              << "  -O, --no-hw-optimizations   Disable h/w specific optimizations\n"
124              << "  -P, --no-connect-ports      Do not connect any ports at startup\n"
125 #ifdef WINDOWS_VST_SUPPORT
126              << "  -V, --novst                 Do not use VST support\n"
127 #endif
128             ;
129 }
130
131 int
132 main (int argc, char* argv[])
133 {
134         const char* optstring = "vhBdD:c:VOU:P";
135
136         /* clang-format off */
137         const struct option longopts[] = {
138                 { "version",             no_argument,       0, 'v' },
139                 { "help",                no_argument,       0, 'h' },
140                 { "bypass-plugins",      no_argument,       0, 'B' },
141                 { "disable-plugins",     no_argument,       0, 'd' },
142                 { "debug",               required_argument, 0, 'D' },
143                 { "name",                required_argument, 0, 'c' },
144                 { "novst",               no_argument,       0, 'V' },
145                 { "no-hw-optimizations", no_argument,       0, 'O' },
146                 { "no-connect-ports",    no_argument,       0, 'P' },
147                 { 0, 0, 0, 0 }
148         };
149         /* clang-format on */
150
151         bool use_vst             = true;
152         bool try_hw_optimization = true;
153
154         backend_client_name = PBD::downcase (std::string (PROGRAM_NAME));
155
156         int c;
157         while ((c = getopt_long (argc, argv, optstring, longopts, (int*)0)) != EOF) {
158                 switch (c) {
159                         case 0:
160                                 break;
161
162                         case 'v':
163                                 print_version ();
164                                 exit (EXIT_SUCCESS);
165                                 break;
166
167                         case 'h':
168                                 print_help ();
169                                 exit (EXIT_SUCCESS);
170                                 break;
171
172                         case 'c':
173                                 backend_client_name = optarg;
174                                 break;
175
176                         case 'B':
177                                 ARDOUR::Session::set_bypass_all_loaded_plugins (true);
178                                 break;
179
180                         case 'd':
181                                 ARDOUR::Session::set_disable_all_loaded_plugins (true);
182                                 break;
183
184                         case 'D':
185                                 if (PBD::parse_debug_options (optarg)) {
186                                         exit (EXIT_SUCCESS);
187                                 }
188                                 break;
189
190                         case 'O':
191                                 try_hw_optimization = false;
192                                 break;
193
194                         case 'P':
195                                 ARDOUR::Port::set_connecting_blocked (true);
196                                 break;
197
198                         case 'V':
199 #ifdef WINDOWS_VST_SUPPORT
200                                 use_vst = false;
201 #endif /* WINDOWS_VST_SUPPORT */
202                                 break;
203
204                         default:
205                                 print_help ();
206                                 exit (EXIT_FAILURE);
207                 }
208         }
209
210         if (argc < 3) {
211                 print_help ();
212                 exit (EXIT_FAILURE);
213         }
214
215         if (!ARDOUR::init (use_vst, try_hw_optimization, localedir)) {
216                 cerr << "Ardour failed to initialize\n"
217                      << endl;
218                 exit (EXIT_FAILURE);
219         }
220
221         Session* s = 0;
222
223         try {
224                 s = load_session (argv[optind], argv[optind + 1]);
225         } catch (failed_constructor& e) {
226                 cerr << "failed_constructor: " << e.what () << "\n";
227                 exit (EXIT_FAILURE);
228         } catch (AudioEngine::PortRegistrationFailure& e) {
229                 cerr << "PortRegistrationFailure: " << e.what () << "\n";
230                 exit (EXIT_FAILURE);
231         } catch (exception& e) {
232                 cerr << "exception: " << e.what () << "\n";
233                 exit (EXIT_FAILURE);
234         } catch (...) {
235                 cerr << "unknown exception.\n";
236                 exit (EXIT_FAILURE);
237         }
238
239         /* allow signal propagation, callback/thread-pool setup, etc
240          * similar to to GUI "first idle"
241          */
242         Glib::usleep (1000000); // 1 sec
243
244         if (!s) {
245                 cerr << "failed_to load session\n";
246                 exit (EXIT_FAILURE);
247         }
248
249         PBD::ScopedConnectionList con;
250         BasicUI::AccessAction.connect_same_thread (con, boost::bind (&access_action, _1, _2));
251         AudioEngine::instance ()->Halted.connect_same_thread (con, boost::bind (&engine_halted, _1));
252
253 #ifndef PLATFORM_WINDOWS
254         signal (SIGINT, wearedone);
255         signal (SIGTERM, wearedone);
256 #endif
257
258         s->request_transport_speed (1.0);
259
260         char msg;
261         do {
262         } while (0 == xthread.receive (msg, true));
263
264         AudioEngine::instance ()->remove_session ();
265         delete s;
266         AudioEngine::instance ()->stop ();
267
268         AudioEngine::destroy ();
269         return 0;
270 }