Setup grok LD_LIBRARY_PATH when required (#2657).
[dcpomatic.git] / src / tools / dcpomatic_server_cli.cc
1 /*
2     Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "lib/config.h"
22 #include "lib/dcp_video.h"
23 #include "lib/exceptions.h"
24 #include "lib/util.h"
25 #include "lib/config.h"
26 #include "lib/image.h"
27 #include "lib/file_log.h"
28 #ifdef DCPOMATIC_GROK
29 #include "lib/grok/context.h"
30 #endif
31 #include "lib/null_log.h"
32 #include "lib/version.h"
33 #include "lib/encode_server.h"
34 #include "lib/dcpomatic_log.h"
35 #include <boost/array.hpp>
36 #include <boost/asio.hpp>
37 #include <boost/algorithm/string.hpp>
38 #include <boost/thread.hpp>
39 #include <boost/thread/mutex.hpp>
40 #include <boost/thread/condition.hpp>
41 #include <unistd.h>
42 #include <errno.h>
43 #include <getopt.h>
44 #include <iostream>
45 #include <stdexcept>
46 #include <cstring>
47 #include <vector>
48
49 using std::cerr;
50 using std::string;
51 using std::cout;
52 using std::shared_ptr;
53
54 static void
55 help (string n)
56 {
57         cerr << "Syntax: " << n << " [OPTION]\n"
58              << "  -v, --version      show DCP-o-matic version\n"
59              << "  -h, --help         show this help\n"
60              << "  -t, --threads      number of parallel encoding threads to use\n"
61              << "  --verbose          be verbose to stdout\n"
62              << "  --log              write a log file of activity\n";
63 }
64
65 int
66 main (int argc, char* argv[])
67 {
68         dcpomatic_setup_path_encoding ();
69         dcpomatic_setup ();
70
71         int num_threads = Config::instance()->server_encoding_threads ();
72         bool verbose = false;
73         bool write_log = false;
74
75         int option_index = 0;
76         while (true) {
77                 static struct option long_options[] = {
78                         { "version", no_argument, 0, 'v'},
79                         { "help", no_argument, 0, 'h'},
80                         { "threads", required_argument, 0, 't'},
81                         { "verbose", no_argument, 0, 'A'},
82                         { "log", no_argument, 0, 'B'},
83                         { 0, 0, 0, 0 }
84                 };
85
86                 int c = getopt_long (argc, argv, "vht:AB", long_options, &option_index);
87
88                 if (c == -1) {
89                         break;
90                 }
91
92                 switch (c) {
93                 case 'v':
94                         cout << "dcpomatic version " << dcpomatic_version << " " << dcpomatic_git_commit << "\n";
95                         exit (EXIT_SUCCESS);
96                 case 'h':
97                         help (argv[0]);
98                         exit (EXIT_SUCCESS);
99                 case 't':
100                         num_threads = atoi (optarg);
101                         break;
102                 case 'A':
103                         verbose = true;
104                         break;
105                 case 'B':
106                         write_log = true;
107                         break;
108                 }
109         }
110
111         if (write_log) {
112                 dcpomatic_log.reset (new FileLog("dcpomatic_server_cli.log"));
113         }
114
115 #ifdef DCPOMATIC_GROK
116         setMessengerLogger(new grk_plugin::GrokLogger("[GROK] "));
117         setup_grok_library_path();
118 #endif
119
120         EncodeServer server (verbose, num_threads);
121
122         try {
123                 server.run ();
124         } catch (boost::system::system_error& e) {
125                 if (e.code() == boost::system::errc::address_in_use) {
126                         cerr << argv[0] << ": address already in use.  Is another DCP-o-matic server instance already running?\n";
127                         exit (EXIT_FAILURE);
128                 }
129                 cerr << argv[0] << ": " << e.what() << "\n";
130         } catch (std::exception& e) {
131                 cerr << argv[0] << ": failed to start server; " << e.what() << "\n";
132         }
133         return 0;
134 }