Tidy up to use one list of servers.
[dcpomatic.git] / src / tools / dcpomatic_cli.cc
1 /*
2     Copyright (C) 2012-2017 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/film.h"
22 #include "lib/filter.h"
23 #include "lib/transcode_job.h"
24 #include "lib/job_manager.h"
25 #include "lib/util.h"
26 #include "lib/version.h"
27 #include "lib/cross.h"
28 #include "lib/config.h"
29 #include "lib/log.h"
30 #include "lib/signal_manager.h"
31 #include "lib/encode_server_finder.h"
32 #include "lib/json_server.h"
33 #include "lib/ratio.h"
34 #include "lib/video_content.h"
35 #include "lib/audio_content.h"
36 #include <dcp/version.h>
37 #include <boost/foreach.hpp>
38 #include <getopt.h>
39 #include <iostream>
40 #include <iomanip>
41
42 using std::string;
43 using std::cerr;
44 using std::cout;
45 using std::vector;
46 using std::pair;
47 using std::setw;
48 using std::list;
49 using boost::shared_ptr;
50 using boost::optional;
51 using boost::dynamic_pointer_cast;
52
53 static void
54 help (string n)
55 {
56         cerr << "Syntax: " << n << " [OPTION] [<FILM>]\n"
57              << "  -v, --version        show DCP-o-matic version\n"
58              << "  -h, --help           show this help\n"
59              << "  -f, --flags          show flags passed to C++ compiler on build\n"
60              << "  -n, --no-progress    do not print progress to stdout\n"
61              << "  -r, --no-remote      do not use any remote servers\n"
62              << "  -t, --threads        specify number of local encoding threads (overriding configuration)\n"
63              << "  -j, --json <port>    run a JSON server on the specified port\n"
64              << "  -k, --keep-going     keep running even when the job is complete\n"
65              << "  -s, --servers <file> specify servers to use in a text file\n"
66              << "  -l, --list-servers   just display a list of encoding servers that DCP-o-matic is configured to use; don't encode\n"
67              << "  -d, --dcp-path       echo DCP's path to stdout on successful completion (implies -n)\n"
68              << "      --dump           just dump a summary of the film's settings; don't encode\n"
69              << "\n"
70              << "<FILM> is the film directory.\n";
71 }
72
73 static void
74 print_dump (shared_ptr<Film> film)
75 {
76         cout << film->dcp_name (true) << "\n"
77              << film->container()->container_nickname() << " at " << ((film->resolution() == RESOLUTION_2K) ? "2K" : "4K") << "\n"
78              << (film->j2k_bandwidth() / 1000000) << "Mbit/s" << "\n"
79              << "Output " << film->video_frame_rate() << "fps " << (film->three_d() ? "3D" : "2D") << " " << (film->audio_frame_rate() / 1000) << "kHz\n"
80              << (film->interop() ? "Inter-Op" : "SMPTE") << " " << (film->encrypted() ? "encrypted" : "unencrypted") << "\n";
81
82         BOOST_FOREACH (shared_ptr<Content> c, film->content ()) {
83                 cout << "\n"
84                      << c->path(0) << "\n"
85                      << "\tat " << c->position().seconds ()
86                      << " length " << c->full_length().seconds ()
87                      << " start trim " << c->trim_start().seconds ()
88                      << " end trim " << c->trim_end().seconds () << "\n";
89
90                 if (c->video) {
91                         cout << "\t" << c->video->size().width << "x" << c->video->size().height << "\n"
92                              << "\t" << c->active_video_frame_rate() << "fps\n"
93                              << "\tcrop left " << c->video->left_crop()
94                              << " right " << c->video->right_crop()
95                              << " top " << c->video->top_crop()
96                              << " bottom " << c->video->bottom_crop() << "\n"
97                              << "\tscale " << c->video->scale().name() << "\n";
98                         if (c->video->colour_conversion()) {
99                                 if (c->video->colour_conversion().get().preset()) {
100                                         cout << "\tcolour conversion "
101                                              << PresetColourConversion::all()[c->video->colour_conversion().get().preset().get()].name
102                                              << "\n";
103                                 } else {
104                                         cout << "\tcustom colour conversion\n";
105                                 }
106                         } else {
107                                 cout << "\tno colour conversion\n";
108                         }
109
110                 }
111
112                 if (c->audio) {
113                         cout << "\t" << c->audio->delay() << " delay\n"
114                              << "\t" << c->audio->gain() << " gain\n";
115                 }
116         }
117 }
118
119 static void
120 list_servers ()
121 {
122         while (true) {
123                 int N = 0;
124                 list<EncodeServerDescription> servers = EncodeServerFinder::instance()->servers();
125
126                 /* This is a bit fiddly because we want to list configured servers that are down as well
127                    as all those (configured and found by broadcast) that are up.
128                 */
129
130                 if (servers.empty() && Config::instance()->servers().empty()) {
131                         cout << "No encoding servers found or configured.\n";
132                         ++N;
133                 } else {
134                         cout << std::left << setw(24) << "Host" << " Status Threads\n";
135                         ++N;
136
137                         /* Report the state of configured servers */
138                         BOOST_FOREACH (string i, Config::instance()->servers()) {
139                                 cout << std::left << setw(24) << i << " ";
140
141                                 /* See if this server is on the active list; if so, remove it and note
142                                    the number of threads it is offering.
143                                 */
144                                 optional<int> threads;
145                                 list<EncodeServerDescription>::iterator j = servers.begin ();
146                                 while (j != servers.end ()) {
147                                         if (i == j->host_name() && j->current_link_version()) {
148                                                 threads = j->threads();
149                                                 list<EncodeServerDescription>::iterator tmp = j;
150                                                 ++tmp;
151                                                 servers.erase (j);
152                                                 j = tmp;
153                                         } else {
154                                                 ++j;
155                                         }
156                                 }
157                                 if (static_cast<bool>(threads)) {
158                                         cout << "UP     " << threads.get() << "\n";
159                                 } else {
160                                         cout << "DOWN\n";
161                                 }
162                                 ++N;
163                         }
164
165                         /* Now report any left that have been found by broadcast */
166                         BOOST_FOREACH (EncodeServerDescription const & i, servers) {
167                                 if (i.current_link_version()) {
168                                         cout << std::left << setw(24) << i.host_name() << " UP     " << i.threads() << "\n";
169                                 } else {
170                                         cout << std::left << setw(24) << i.host_name() << " bad version\n";
171                                 }
172                                 ++N;
173                         }
174                 }
175
176                 dcpomatic_sleep (1);
177
178                 for (int i = 0; i < N; ++i) {
179                         cout << "\033[1A\033[2K";
180                 }
181         }
182 }
183
184
185 int
186 main (int argc, char* argv[])
187 {
188         boost::filesystem::path film_dir;
189         bool progress = true;
190         bool no_remote = false;
191         optional<int> threads;
192         optional<int> json_port;
193         bool keep_going = false;
194         bool dump = false;
195         optional<boost::filesystem::path> servers;
196         bool list_servers_ = false;
197         bool dcp_path = false;
198
199         int option_index = 0;
200         while (true) {
201                 static struct option long_options[] = {
202                         { "version", no_argument, 0, 'v'},
203                         { "help", no_argument, 0, 'h'},
204                         { "flags", no_argument, 0, 'f'},
205                         { "no-progress", no_argument, 0, 'n'},
206                         { "no-remote", no_argument, 0, 'r'},
207                         { "threads", required_argument, 0, 't'},
208                         { "json", required_argument, 0, 'j'},
209                         { "keep-going", no_argument, 0, 'k' },
210                         { "servers", required_argument, 0, 's' },
211                         { "list-servers", no_argument, 0, 'l' },
212                         { "dcp-path", no_argument, 0, 'd' },
213                         /* Just using A, B, C ... from here on */
214                         { "dump", no_argument, 0, 'A' },
215                         { 0, 0, 0, 0 }
216                 };
217
218                 int c = getopt_long (argc, argv, "vhfnrt:j:kAs:ld", long_options, &option_index);
219
220                 if (c == -1) {
221                         break;
222                 }
223
224                 switch (c) {
225                 case 'v':
226                         cout << "dcpomatic version " << dcpomatic_version << " " << dcpomatic_git_commit << "\n";
227                         exit (EXIT_SUCCESS);
228                 case 'h':
229                         help (argv[0]);
230                         exit (EXIT_SUCCESS);
231                 case 'f':
232                         cout << dcpomatic_cxx_flags << "\n";
233                         exit (EXIT_SUCCESS);
234                 case 'n':
235                         progress = false;
236                         break;
237                 case 'r':
238                         no_remote = true;
239                         break;
240                 case 't':
241                         threads = atoi (optarg);
242                         break;
243                 case 'j':
244                         json_port = atoi (optarg);
245                         break;
246                 case 'k':
247                         keep_going = true;
248                         break;
249                 case 'A':
250                         dump = true;
251                         break;
252                 case 's':
253                         servers = optarg;
254                         break;
255                 case 'l':
256                         list_servers_ = true;
257                         break;
258                 case 'd':
259                         dcp_path = true;
260                         progress = false;
261                         break;
262                 }
263         }
264
265         if (servers) {
266                 FILE* f = fopen_boost (*servers, "r");
267                 if (!f) {
268                         cerr << "Could not open servers list file " << *servers << "\n";
269                         exit (EXIT_FAILURE);
270                 }
271                 vector<string> servers;
272                 while (!feof (f)) {
273                         char buffer[128];
274                         if (fscanf (f, "%s.127", buffer) == 1) {
275                                 servers.push_back (buffer);
276                         }
277                 }
278                 fclose (f);
279                 Config::instance()->set_servers (servers);
280         }
281
282         if (list_servers_) {
283                 list_servers ();
284                 exit (EXIT_SUCCESS);
285         }
286
287         if (optind >= argc) {
288                 help (argv[0]);
289                 exit (EXIT_FAILURE);
290         }
291
292         film_dir = argv[optind];
293
294         dcpomatic_setup_path_encoding ();
295         dcpomatic_setup ();
296         signal_manager = new SignalManager ();
297
298         if (no_remote) {
299                 EncodeServerFinder::instance()->stop ();
300         }
301
302         if (json_port) {
303                 new JSONServer (json_port.get ());
304         }
305
306         if (threads) {
307                 Config::instance()->set_master_encoding_threads (threads.get ());
308         }
309
310         shared_ptr<Film> film;
311         try {
312                 film.reset (new Film (film_dir));
313                 film->read_metadata ();
314         } catch (std::exception& e) {
315                 cerr << argv[0] << ": error reading film `" << film_dir.string() << "' (" << e.what() << ")\n";
316                 exit (EXIT_FAILURE);
317         }
318
319         if (dump) {
320                 print_dump (film);
321                 exit (EXIT_SUCCESS);
322         }
323
324         ContentList content = film->content ();
325         for (ContentList::const_iterator i = content.begin(); i != content.end(); ++i) {
326                 vector<boost::filesystem::path> paths = (*i)->paths ();
327                 for (vector<boost::filesystem::path>::const_iterator j = paths.begin(); j != paths.end(); ++j) {
328                         if (!boost::filesystem::exists (*j)) {
329                                 cerr << argv[0] << ": content file " << *j << " not found.\n";
330                                 exit (EXIT_FAILURE);
331                         }
332                 }
333         }
334
335         if (progress) {
336                 cout << "\nMaking DCP for " << film->name() << "\n";
337         }
338
339         film->make_dcp ();
340
341         bool should_stop = false;
342         bool first = true;
343         bool error = false;
344         while (!should_stop) {
345
346                 dcpomatic_sleep (5);
347
348                 list<shared_ptr<Job> > jobs = JobManager::instance()->get ();
349
350                 if (!first && progress) {
351                         for (size_t i = 0; i < jobs.size(); ++i) {
352                                 cout << "\033[1A\033[2K";
353                         }
354                         cout.flush ();
355                 }
356
357                 first = false;
358
359                 int unfinished = 0;
360                 int finished_in_error = 0;
361
362                 BOOST_FOREACH (shared_ptr<Job> i, jobs) {
363                         if (progress) {
364                                 cout << i->name();
365                                 if (!i->sub_name().empty()) {
366                                         cout << "; " << i->sub_name();
367                                 }
368                                 cout << ": ";
369
370                                 if (i->progress ()) {
371                                         cout << i->status() << "                            \n";
372                                 } else {
373                                         cout << ": Running           \n";
374                                 }
375                         }
376
377                         if (!i->finished ()) {
378                                 ++unfinished;
379                         }
380
381                         if (i->finished_in_error ()) {
382                                 ++finished_in_error;
383                                 error = true;
384                         }
385
386                         if (!progress && i->finished_in_error ()) {
387                                 /* We won't see this error if we haven't been showing progress,
388                                    so show it now.
389                                 */
390                                 cout << i->status() << "\n";
391                         }
392                 }
393
394                 if (unfinished == 0 || finished_in_error != 0) {
395                         should_stop = true;
396                 }
397         }
398
399         if (keep_going) {
400                 while (true) {
401                         dcpomatic_sleep (3600);
402                 }
403         }
404
405         /* This is just to stop valgrind reporting leaks due to JobManager
406            indirectly holding onto codecs.
407         */
408         JobManager::drop ();
409
410         EncodeServerFinder::drop ();
411
412         if (dcp_path && !error) {
413                 cout << film->dir (film->dcp_name (false)).string() << "\n";
414         }
415
416         return error ? EXIT_FAILURE : EXIT_SUCCESS;
417 }