Merge.
[dcpomatic.git] / src / tools / dcpomatic_server_cli.cc
1 /*
2     Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "lib/server.h"
21 #include <iostream>
22 #include <stdexcept>
23 #include <sstream>
24 #include <cstring>
25 #include <vector>
26 #include <unistd.h>
27 #include <errno.h>
28 #include <getopt.h>
29 #include <boost/array.hpp>
30 #include <boost/asio.hpp>
31 #include <boost/algorithm/string.hpp>
32 #include <boost/thread.hpp>
33 #include <boost/thread/mutex.hpp>
34 #include <boost/thread/condition.hpp>
35 #include "lib/config.h"
36 #include "lib/dcp_video_frame.h"
37 #include "lib/exceptions.h"
38 #include "lib/util.h"
39 #include "lib/config.h"
40 #include "lib/scaler.h"
41 #include "lib/image.h"
42 #include "lib/log.h"
43 #include "lib/version.h"
44
45 using std::cerr;
46 using std::string;
47 using std::cout;
48 using boost::shared_ptr;
49
50 static void
51 help (string n)
52 {
53         cerr << "Syntax: " << n << " [OPTION]\n"
54              << "  -v, --version      show DCP-o-matic version\n"
55              << "  -h, --help         show this help\n"
56              << "  -t, --threads      number of parallel encoding threads to use\n"
57              << "  --verbose          be verbose to stdout\n"
58              << "  --log              write a log file of activity\n";
59 }
60
61 int
62 main (int argc, char* argv[])
63 {
64         int num_threads = Config::instance()->num_local_encoding_threads ();
65         bool verbose = false;
66         bool write_log = false;
67
68         int option_index = 0;
69         while (true) {
70                 static struct option long_options[] = {
71                         { "version", no_argument, 0, 'v'},
72                         { "help", no_argument, 0, 'h'},
73                         { "threads", required_argument, 0, 't'},
74                         { "verbose", no_argument, 0, 'A'},
75                         { "log", no_argument, 0, 'B'},
76                         { 0, 0, 0, 0 }
77                 };
78
79                 int c = getopt_long (argc, argv, "vht:AB", long_options, &option_index);
80
81                 if (c == -1) {
82                         break;
83                 }
84
85                 switch (c) {
86                 case 'v':
87                         cout << "dcpomatic version " << dcpomatic_version << " " << dcpomatic_git_commit << "\n";
88                         exit (EXIT_SUCCESS);
89                 case 'h':
90                         help (argv[0]);
91                         exit (EXIT_SUCCESS);
92                 case 't':
93                         num_threads = atoi (optarg);
94                         break;
95                 case 'A':
96                         verbose = true;
97                         break;
98                 case 'B':
99                         write_log = true;
100                         break;
101                 }
102         }
103
104         Scaler::setup_scalers ();
105         shared_ptr<Log> log;
106         if (write_log) {
107                 log.reset (new FileLog ("dcpomatic_server_cli.log"));
108         } else {
109                 log.reset (new NullLog);
110         }
111         
112         Server server (log, verbose);
113         
114         try {
115                 server.run (num_threads);
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         }
123         return 0;
124 }