Hand-apply e30fd8d; resurrect JSON server code.
[dcpomatic.git] / src / tools / dcpomatic_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 <iostream>
21 #include <iomanip>
22 #include <getopt.h>
23 #include <dcp/version.h>
24 #include "lib/film.h"
25 #include "lib/filter.h"
26 #include "lib/transcode_job.h"
27 #include "lib/job_manager.h"
28 #include "lib/util.h"
29 #include "lib/scaler.h"
30 #include "lib/version.h"
31 #include "lib/cross.h"
32 #include "lib/config.h"
33 #include "lib/log.h"
34 #include "lib/ui_signaller.h"
35 #include "lib/server_finder.h"
36 #include "lib/json_server.h"
37
38 using std::string;
39 using std::cerr;
40 using std::cout;
41 using std::vector;
42 using std::pair;
43 using std::list;
44 using boost::shared_ptr;
45 using boost::optional;
46
47 static void
48 help (string n)
49 {
50         cerr << "Syntax: " << n << " [OPTION] <FILM>\n"
51              << "  -v, --version      show DCP-o-matic version\n"
52              << "  -h, --help         show this help\n"
53              << "  -f, --flags        show flags passed to C++ compiler on build\n"
54              << "  -n, --no-progress  do not print progress to stdout\n"
55              << "  -r, --no-remote    do not use any remote servers\n"
56              << "  -j, --json <port>  run a JSON server on the specified port\n"
57              << "  -k, --keep-going   keep running even when the job is complete\n"
58              << "\n"
59              << "<FILM> is the film directory.\n";
60 }
61
62 int
63 main (int argc, char* argv[])
64 {
65         string film_dir;
66         bool progress = true;
67         bool no_remote = false;
68         optional<int> json_port;
69         bool keep_going = false;
70
71         int option_index = 0;
72         while (true) {
73                 static struct option long_options[] = {
74                         { "version", no_argument, 0, 'v'},
75                         { "help", no_argument, 0, 'h'},
76                         { "flags", no_argument, 0, 'f'},
77                         { "no-progress", no_argument, 0, 'n'},
78                         { "no-remote", no_argument, 0, 'r'},
79                         { "json", required_argument, 0, 'j'},
80                         { "keep-going", no_argument, 0, 'k' },
81                         { 0, 0, 0, 0 }
82                 };
83
84                 int c = getopt_long (argc, argv, "vhfnrj:k", long_options, &option_index);
85
86                 if (c == -1) {
87                         break;
88                 }
89
90                 switch (c) {
91                 case 'v':
92                         cout << "dcpomatic version " << dcpomatic_version << " " << dcpomatic_git_commit << "\n";
93                         exit (EXIT_SUCCESS);
94                 case 'h':
95                         help (argv[0]);
96                         exit (EXIT_SUCCESS);
97                 case 'f':
98                         cout << dcpomatic_cxx_flags << "\n";
99                         exit (EXIT_SUCCESS);
100                 case 'n':
101                         progress = false;
102                         break;
103                 case 'r':
104                         no_remote = true;
105                         break;
106                 case 'j':
107                         json_port = atoi (optarg);
108                         break;
109                 case 'k':
110                         keep_going = true;
111                         break;
112                 }
113         }
114
115         if (optind >= argc) {
116                 help (argv[0]);
117                 exit (EXIT_FAILURE);
118         }
119
120         film_dir = argv[optind];
121                         
122         dcpomatic_setup ();
123         ui_signaller = new UISignaller ();
124         
125         if (no_remote) {
126                 ServerFinder::instance()->disable ();
127         }
128
129         if (json_port) {
130                 new JSONServer (json_port.get ());
131         }
132
133         cout << "DCP-o-matic " << dcpomatic_version << " git " << dcpomatic_git_commit;
134         char buf[256];
135         if (gethostname (buf, 256) == 0) {
136                 cout << " on " << buf;
137         }
138         cout << "\n";
139
140         shared_ptr<Film> film;
141         try {
142                 film.reset (new Film (film_dir));
143                 film->read_metadata ();
144         } catch (std::exception& e) {
145                 cerr << argv[0] << ": error reading film `" << film_dir << "' (" << e.what() << ")\n";
146                 exit (EXIT_FAILURE);
147         }
148
149         ContentList content = film->content ();
150         for (ContentList::const_iterator i = content.begin(); i != content.end(); ++i) {
151                 vector<boost::filesystem::path> paths = (*i)->paths ();
152                 for (vector<boost::filesystem::path>::const_iterator j = paths.begin(); j != paths.end(); ++j) {
153                         if (!boost::filesystem::exists (*j)) {
154                                 cerr << argv[0] << ": content file " << *j << " not found.\n";
155                                 exit (EXIT_FAILURE);
156                         }
157                 }
158         }
159                 
160         cout << "\nMaking DCP for " << film->name() << "\n";
161
162         film->make_dcp ();
163
164         bool should_stop = false;
165         bool first = true;
166         bool error = false;
167         while (!should_stop) {
168
169                 dcpomatic_sleep (5);
170
171                 list<shared_ptr<Job> > jobs = JobManager::instance()->get ();
172
173                 if (!first && progress) {
174                         cout << "\033[" << jobs.size() << "A";
175                         cout.flush ();
176                 }
177
178                 first = false;
179
180                 int unfinished = 0;
181                 int finished_in_error = 0;
182
183                 for (list<shared_ptr<Job> >::iterator i = jobs.begin(); i != jobs.end(); ++i) {
184                         if (progress) {
185                                 cout << (*i)->name() << ": ";
186                                 
187                                 if ((*i)->progress ()) {
188                                         cout << (*i)->status() << "                         \n";
189                                 } else {
190                                         cout << ": Running           \n";
191                                 }
192                         }
193
194                         if (!(*i)->finished ()) {
195                                 ++unfinished;
196                         }
197
198                         if ((*i)->finished_in_error ()) {
199                                 ++finished_in_error;
200                                 error = true;
201                         }
202
203                         if (!progress && (*i)->finished_in_error ()) {
204                                 /* We won't see this error if we haven't been showing progress,
205                                    so show it now.
206                                 */
207                                 cout << (*i)->status() << "\n";
208                         }
209                 }
210
211                 if (unfinished == 0 || finished_in_error != 0) {
212                         should_stop = true;
213                 }
214         }
215
216         if (keep_going) {
217                 while (true) {
218                         dcpomatic_sleep (3600);
219                 }
220         }
221
222         /* This is just to stop valgrind reporting leaks due to JobManager
223            indirectly holding onto codecs.
224         */
225         JobManager::drop ();
226
227         ServerFinder::drop ();
228         
229         return error ? EXIT_FAILURE : EXIT_SUCCESS;
230 }
231
232