headless: Use direct x-thread wakeup instead of sleep/spin
[ardour.git] / headless / load_session.cc
index a9037214cc33f3fa6874e0c8f7f75ff024cbe7f4..47fd5151b19f3d084a1a7d947b1eb88b31ce54b3 100644 (file)
@@ -2,33 +2,42 @@
 #include <cstdlib>
 #include <getopt.h>
 
+#ifndef PLATFORM_WINDOWS
+#include <signal.h>
+#endif
+
+#include <glibmm.h>
+
+#include "pbd/convert.h"
+#include "pbd/crossthread.h"
 #include "pbd/failed_constructor.h"
 #include "pbd/error.h"
 #include "pbd/debug.h"
 
 #include "ardour/ardour.h"
 #include "ardour/audioengine.h"
+#include "ardour/revision.h"
 #include "ardour/session.h"
 
+#include "control_protocol/control_protocol.h"
+
 #include "misc.h"
 
 using namespace std;
 using namespace ARDOUR;
 using namespace PBD;
 
-#ifdef PLATFORM_WINDOWS
-#include <windows.h>
-#define sleep(X) Sleep((X) * 1000)
-#endif
-
 static const char* localedir = LOCALEDIR;
 
-TestReceiver test_receiver;
+static string backend_client_name;
+static string backend_name = "JACK";
+static CrossThreadChannel xthread (true);
+static TestReceiver test_receiver;
 
 /** @param dir Session directory.
  *  @param state Session state file, without .ardour suffix.
  */
-Session *
+static Session *
 load_session (string dir, string state)
 {
        SessionEvent::create_per_thread_pool ("test", 512);
@@ -40,13 +49,11 @@ load_session (string dir, string state)
 
        AudioEngine* engine = AudioEngine::create ();
 
-       if (!engine->set_default_backend ()) {
-               std::cerr << "Cannot create Audio/MIDI engine\n";
+       if (!engine->set_backend (backend_name, backend_client_name, "")) {
+               std::cerr << "Cannot set Audio/MIDI engine backend\n";
                ::exit (1);
        }
 
-       init_post_engine ();
-
        if (engine->start () != 0) {
                std::cerr << "Cannot start Audio/MIDI engine\n";
                ::exit (1);
@@ -57,15 +64,50 @@ load_session (string dir, string state)
        return session;
 }
 
-string session_name = "";
-string backend_client_name = "ardour";
-string backend_session_uuid;
-bool just_version = false;
-bool use_vst = true;
-bool try_hw_optimization = true;
-bool no_connect_ports = false;
+static void
+access_action (const std::string& action_group, const std::string& action_item)
+{
+       if (action_group == "Common" && action_item == "Quit") {
+               xthread.deliver ('x');
+       }
+}
+
+static void
+engine_halted (const char* reason)
+{
+       cerr << "The audio backend has been shutdown";
+       if (reason && strlen (reason) > 0) {
+               cerr << ": " << reason;
+       } else {
+               cerr << ".";
+       }
+       cerr << endl;
+       xthread.deliver ('x');
+}
 
-void
+#ifndef PLATFORM_WINDOWS
+static void wearedone (int) {
+       cerr << "caught signal - terminating." << endl;
+       xthread.deliver ('x');
+}
+#endif
+
+static void
+print_version ()
+{
+       cout
+               << PROGRAM_NAME
+               << VERSIONSTRING
+               << " (built using "
+               << ARDOUR::revision
+#ifdef __GNUC__
+               << " and GCC version " << __VERSION__
+#endif
+               << ')'
+               << endl;
+}
+
+static void
 print_help ()
 {
        cout << "Usage: hardour [OPTIONS]... DIR SNAPSHOT_NAME\n\n"
@@ -86,11 +128,12 @@ print_help ()
 
 int main (int argc, char* argv[])
 {
-       const char *optstring = "vhdD:c:VOU:P";
+       const char *optstring = "vhBdD:c:VOU:P";
 
        const struct option longopts[] = {
                { "version", 0, 0, 'v' },
                { "help", 0, 0, 'h' },
+               { "bypass-plugins", 1, 0, 'B' },
                { "disable-plugins", 1, 0, 'd' },
                { "debug", 1, 0, 'D' },
                { "name", 1, 0, 'c' },
@@ -104,6 +147,11 @@ int main (int argc, char* argv[])
        int option_index = 0;
        int c = 0;
 
+       bool use_vst = true;
+       bool try_hw_optimization = true;
+
+       backend_client_name = PBD::downcase (std::string(PROGRAM_NAME));
+
        while (1) {
                c = getopt_long (argc, argv, optstring, longopts, &option_index);
 
@@ -116,7 +164,8 @@ int main (int argc, char* argv[])
                        break;
 
                case 'v':
-                       just_version = true;
+                       print_version ();
+                       ::exit (0);
                        break;
 
                case 'h':
@@ -128,6 +177,10 @@ int main (int argc, char* argv[])
                        backend_client_name = optarg;
                        break;
 
+               case 'B':
+                       ARDOUR::Session::set_bypass_all_loaded_plugins (true);
+                       break;
+
                case 'd':
                        ARDOUR::Session::set_disable_all_loaded_plugins (true);
                        break;
@@ -143,7 +196,7 @@ int main (int argc, char* argv[])
                        break;
 
                case 'P':
-                       no_connect_ports = true;
+                       ARDOUR::Port::set_connecting_blocked (true);
                        break;
 
                case 'V':
@@ -152,10 +205,6 @@ int main (int argc, char* argv[])
 #endif /* WINDOWS_VST_SUPPORT */
                        break;
 
-               case 'U':
-                       backend_session_uuid = optarg;
-                        break;
-
                default:
                        print_help ();
                        ::exit (1);
@@ -167,13 +216,13 @@ int main (int argc, char* argv[])
                ::exit (1);
        }
 
-       if (!ARDOUR::init (false, true, localedir)) {
+       if (!ARDOUR::init (use_vst, try_hw_optimization, localedir)) {
                cerr << "Ardour failed to initialize\n" << endl;
                ::exit (1);
        }
 
        Session* s = 0;
-       
+
        try {
                s = load_session (argv[optind], argv[optind+1]);
        } catch (failed_constructor& e) {
@@ -190,9 +239,19 @@ int main (int argc, char* argv[])
                exit (EXIT_FAILURE);
        }
 
+       PBD::ScopedConnectionList con;
+       BasicUI::AccessAction.connect_same_thread (con, boost::bind (&access_action, _1, _2));
+       AudioEngine::instance()->Halted.connect_same_thread (con, boost::bind (&engine_halted, _1));
+
+#ifndef PLATFORM_WINDOWS
+       signal(SIGINT, wearedone);
+       signal(SIGTERM, wearedone);
+#endif
+
        s->request_transport_speed (1.0);
-       
-       sleep (-1);
+
+       char msg;
+       do {} while (0 == xthread.receive (msg, true));
 
        AudioEngine::instance()->remove_session ();
        delete s;