Barely-functioning GL playback with new arrangement.
[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 #include "lib/null_log.h"
29 #include "lib/version.h"
30 #include "lib/encode_server.h"
31 #include "lib/dcpomatic_log.h"
32 #include <boost/array.hpp>
33 #include <boost/asio.hpp>
34 #include <boost/algorithm/string.hpp>
35 #include <boost/thread.hpp>
36 #include <boost/thread/mutex.hpp>
37 #include <boost/thread/condition.hpp>
38 #include <unistd.h>
39 #include <errno.h>
40 #include <getopt.h>
41 #include <iostream>
42 #include <stdexcept>
43 #include <cstring>
44 #include <vector>
45
46 using std::cerr;
47 using std::string;
48 using std::cout;
49 using boost::shared_ptr;
50
51 static void
52 help (string n)
53 {
54         cerr << "Syntax: " << n << " [OPTION]\n"
55              << "  -v, --version      show DCP-o-matic version\n"
56              << "  -h, --help         show this help\n"
57              << "  -t, --threads      number of parallel encoding threads to use\n"
58              << "  --verbose          be verbose to stdout\n"
59              << "  --log              write a log file of activity\n";
60 }
61
62 int
63 main (int argc, char* argv[])
64 {
65         dcpomatic_setup_path_encoding ();
66         dcpomatic_setup ();
67
68         int num_threads = Config::instance()->server_encoding_threads ();
69         bool verbose = false;
70         bool write_log = false;
71
72         int option_index = 0;
73         while (true) {
74                 static struct option long_options[] = {
75                         { "version", no_argument, 0, 'v'},
76                         { "help", no_argument, 0, 'h'},
77                         { "threads", required_argument, 0, 't'},
78                         { "verbose", no_argument, 0, 'A'},
79                         { "log", no_argument, 0, 'B'},
80                         { 0, 0, 0, 0 }
81                 };
82
83                 int c = getopt_long (argc, argv, "vht:AB", long_options, &option_index);
84
85                 if (c == -1) {
86                         break;
87                 }
88
89                 switch (c) {
90                 case 'v':
91                         cout << "dcpomatic version " << dcpomatic_version << " " << dcpomatic_git_commit << "\n";
92                         exit (EXIT_SUCCESS);
93                 case 'h':
94                         help (argv[0]);
95                         exit (EXIT_SUCCESS);
96                 case 't':
97                         num_threads = atoi (optarg);
98                         break;
99                 case 'A':
100                         verbose = true;
101                         break;
102                 case 'B':
103                         write_log = true;
104                         break;
105                 }
106         }
107
108         if (write_log) {
109                 dcpomatic_log.reset (new FileLog("dcpomatic_server_cli.log"));
110         }
111
112         EncodeServer server (verbose, num_threads);
113
114         try {
115                 server.run ();
116         } catch (boost::system::system_error& e) {
117                 if (e.code() == boost::system::errc::address_in_use) {
118                         cerr << argv[0] << ": address already in use.  Is another DCP-o-matic server instance already running?\n";
119                         exit (EXIT_FAILURE);
120                 }
121                 cerr << argv[0] << ": " << e.what() << "\n";
122         } catch (std::exception& e) {
123                 cerr << argv[0] << ": failed to start server; " << e.what() << "\n";
124         }
125         return 0;
126 }