0c2d01ecdbe8cfb4312b38c57298a4548d999ce9
[ardour.git] / headless / load_session.cc
1 /*
2  * Copyright (C) 2014-2019 Paul Davis <paul@linuxaudiosystems.com>
3  * Copyright (C) 2014-2019 Robin Gareus <robin@gareus.org>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19
20 #include <cstdlib>
21 #include <getopt.h>
22 #include <iostream>
23
24 #ifndef PLATFORM_WINDOWS
25 #include <signal.h>
26 #endif
27
28 #include <glibmm.h>
29
30 #include "pbd/convert.h"
31 #include "pbd/crossthread.h"
32 #include "pbd/debug.h"
33 #include "pbd/error.h"
34 #include "pbd/failed_constructor.h"
35
36 #include "ardour/ardour.h"
37 #include "ardour/audioengine.h"
38 #include "ardour/revision.h"
39 #include "ardour/session.h"
40
41 #include "control_protocol/control_protocol.h"
42
43 #include "misc.h"
44
45 using namespace std;
46 using namespace ARDOUR;
47 using namespace PBD;
48
49 static const char* localedir = LOCALEDIR;
50
51 static string             backend_client_name;
52 static string             backend_name = "JACK";
53 static CrossThreadChannel xthread (true);
54 static TestReceiver       test_receiver;
55
56 /** @param dir Session directory.
57  *  @param state Session state file, without .ardour suffix.
58  */
59 static Session*
60 load_session (string dir, string state)
61 {
62         SessionEvent::create_per_thread_pool ("test", 512);
63
64         test_receiver.listen_to (error);
65         test_receiver.listen_to (info);
66         test_receiver.listen_to (fatal);
67         test_receiver.listen_to (warning);
68
69         AudioEngine* engine = AudioEngine::create ();
70
71         if (!engine->set_backend (backend_name, backend_client_name, "")) {
72                 std::cerr << "Cannot set Audio/MIDI engine backend\n";
73                 exit (EXIT_FAILURE);
74         }
75
76         if (engine->start () != 0) {
77                 std::cerr << "Cannot start Audio/MIDI engine\n";
78                 exit (EXIT_FAILURE);
79         }
80
81         Session* session = new Session (*engine, dir, state);
82         engine->set_session (session);
83         return session;
84 }
85
86 static void
87 access_action (const std::string& action_group, const std::string& action_item)
88 {
89         if (action_group == "Common" && action_item == "Quit") {
90                 xthread.deliver ('x');
91         }
92 }
93
94 static void
95 engine_halted (const char* reason)
96 {
97         cerr << "The audio backend has been shutdown";
98         if (reason && strlen (reason) > 0) {
99                 cerr << ": " << reason;
100         } else {
101                 cerr << ".";
102         }
103         cerr << endl;
104         xthread.deliver ('x');
105 }
106
107 #ifndef PLATFORM_WINDOWS
108 static void
109 wearedone (int)
110 {
111         cerr << "caught signal - terminating." << endl;
112         xthread.deliver ('x');
113 }
114 #endif
115
116 static void
117 print_version ()
118 {
119         cout
120             << PROGRAM_NAME
121             << VERSIONSTRING
122             << " (built using "
123             << ARDOUR::revision
124 #ifdef __GNUC__
125             << " and GCC version " << __VERSION__
126 #endif
127             << ')'
128             << endl;
129 }
130
131 static void
132 print_help ()
133 {
134         cout << "Usage: hardour [OPTIONS]... DIR SNAPSHOT_NAME\n\n"
135              << "  DIR                         Directory/Folder to load session from\n"
136              << "  SNAPSHOT_NAME               Name of session/snapshot to load (without .ardour at end\n"
137              << "  -v, --version               Show version information\n"
138              << "  -h, --help                  Print this message\n"
139              << "  -c, --name <name>           Use a specific backend client name, default is ardour\n"
140              << "  -d, --disable-plugins       Disable all plugins in an existing session\n"
141              << "  -D, --debug <options>       Set debug flags. Use \"-D list\" to see available options\n"
142              << "  -O, --no-hw-optimizations   Disable h/w specific optimizations\n"
143              << "  -P, --no-connect-ports      Do not connect any ports at startup\n"
144 #ifdef WINDOWS_VST_SUPPORT
145              << "  -V, --novst                 Do not use VST support\n"
146 #endif
147             ;
148 }
149
150 int
151 main (int argc, char* argv[])
152 {
153         const char* optstring = "vhBdD:c:VOU:P";
154
155         /* clang-format off */
156         const struct option longopts[] = {
157                 { "version",             no_argument,       0, 'v' },
158                 { "help",                no_argument,       0, 'h' },
159                 { "bypass-plugins",      no_argument,       0, 'B' },
160                 { "disable-plugins",     no_argument,       0, 'd' },
161                 { "debug",               required_argument, 0, 'D' },
162                 { "name",                required_argument, 0, 'c' },
163                 { "novst",               no_argument,       0, 'V' },
164                 { "no-hw-optimizations", no_argument,       0, 'O' },
165                 { "no-connect-ports",    no_argument,       0, 'P' },
166                 { 0, 0, 0, 0 }
167         };
168         /* clang-format on */
169
170         bool use_vst             = true;
171         bool try_hw_optimization = true;
172
173         backend_client_name = PBD::downcase (std::string (PROGRAM_NAME));
174
175         int c;
176         while ((c = getopt_long (argc, argv, optstring, longopts, (int*)0)) != EOF) {
177                 switch (c) {
178                         case 0:
179                                 break;
180
181                         case 'v':
182                                 print_version ();
183                                 exit (EXIT_SUCCESS);
184                                 break;
185
186                         case 'h':
187                                 print_help ();
188                                 exit (EXIT_SUCCESS);
189                                 break;
190
191                         case 'c':
192                                 backend_client_name = optarg;
193                                 break;
194
195                         case 'B':
196                                 ARDOUR::Session::set_bypass_all_loaded_plugins (true);
197                                 break;
198
199                         case 'd':
200                                 ARDOUR::Session::set_disable_all_loaded_plugins (true);
201                                 break;
202
203                         case 'D':
204                                 if (PBD::parse_debug_options (optarg)) {
205                                         exit (EXIT_SUCCESS);
206                                 }
207                                 break;
208
209                         case 'O':
210                                 try_hw_optimization = false;
211                                 break;
212
213                         case 'P':
214                                 ARDOUR::Port::set_connecting_blocked (true);
215                                 break;
216
217                         case 'V':
218 #ifdef WINDOWS_VST_SUPPORT
219                                 use_vst = false;
220 #endif /* WINDOWS_VST_SUPPORT */
221                                 break;
222
223                         default:
224                                 print_help ();
225                                 exit (EXIT_FAILURE);
226                 }
227         }
228
229         if (argc < 3) {
230                 print_help ();
231                 exit (EXIT_FAILURE);
232         }
233
234         if (!ARDOUR::init (use_vst, try_hw_optimization, localedir)) {
235                 cerr << "Ardour failed to initialize\n"
236                      << endl;
237                 exit (EXIT_FAILURE);
238         }
239
240         Session* s = 0;
241
242         try {
243                 s = load_session (argv[optind], argv[optind + 1]);
244         } catch (failed_constructor& e) {
245                 cerr << "failed_constructor: " << e.what () << "\n";
246                 exit (EXIT_FAILURE);
247         } catch (AudioEngine::PortRegistrationFailure& e) {
248                 cerr << "PortRegistrationFailure: " << e.what () << "\n";
249                 exit (EXIT_FAILURE);
250         } catch (exception& e) {
251                 cerr << "exception: " << e.what () << "\n";
252                 exit (EXIT_FAILURE);
253         } catch (...) {
254                 cerr << "unknown exception.\n";
255                 exit (EXIT_FAILURE);
256         }
257
258         /* allow signal propagation, callback/thread-pool setup, etc
259          * similar to to GUI "first idle"
260          */
261         Glib::usleep (1000000); // 1 sec
262
263         if (!s) {
264                 cerr << "failed_to load session\n";
265                 exit (EXIT_FAILURE);
266         }
267
268         PBD::ScopedConnectionList con;
269         BasicUI::AccessAction.connect_same_thread (con, boost::bind (&access_action, _1, _2));
270         AudioEngine::instance ()->Halted.connect_same_thread (con, boost::bind (&engine_halted, _1));
271
272 #ifndef PLATFORM_WINDOWS
273         signal (SIGINT, wearedone);
274         signal (SIGTERM, wearedone);
275 #endif
276
277         s->request_transport_speed (1.0);
278
279         char msg;
280         do {
281         } while (0 == xthread.receive (msg, true));
282
283         AudioEngine::instance ()->remove_session ();
284         delete s;
285         AudioEngine::instance ()->stop ();
286
287         AudioEngine::destroy ();
288         return 0;
289 }