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