Distinguish master DoM encode threads count from the server count.
[dcpomatic.git] / src / tools / dcpomatic_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/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      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()->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 show_servers ()
120 {
121         while (true) {
122                 int N = 0;
123                 list<EncodeServerDescription> servers = EncodeServerFinder::instance()->servers ();
124
125                 if (Config::instance()->use_any_servers ()) {
126                         if (servers.empty ()) {
127                                 cout << "No encoding servers found.\n";
128                                 ++N;
129                         } else {
130                                 cout << std::left << setw(24) << "Host" << " Threads\n";
131                                 ++N;
132                                 BOOST_FOREACH (EncodeServerDescription const & i, servers) {
133                                         cout << std::left << setw(24) << i.host_name() << " " << i.threads() << "\n";
134                                         ++N;
135                                 }
136                         }
137                 } else {
138                         vector<string> configured_servers = Config::instance()->servers();
139                         if (configured_servers.empty()) {
140                                 cout << "No configured servers, and DCP-o-matic is not set to search for any server.\n";
141                                 ++N;
142                         } else {
143                                 cout << std::left << setw(24) << "Host" << " Status Threads\n";
144                                 ++N;
145                                 BOOST_FOREACH (string const & i, Config::instance()->servers()) {
146                                         cout << std::left << setw(24) << i << " ";
147                                         optional<int> threads;
148                                         BOOST_FOREACH (EncodeServerDescription const & j, servers) {
149                                                 if (i == j.host_name()) {
150                                                         threads = j.threads();
151                                                 }
152                                         }
153                                         if (static_cast<bool>(threads)) {
154                                                 cout << "UP     " << threads.get() << "\n";
155                                         } else {
156                                                 cout << "DOWN\n";
157                                         }
158                                         ++N;
159                                 }
160                         }
161                 }
162
163
164                 dcpomatic_sleep (1);
165
166                 for (int i = 0; i < N; ++i) {
167                         cout << "\033[1A\033[2K";
168                 }
169         }
170 }
171
172
173 int
174 main (int argc, char* argv[])
175 {
176         boost::filesystem::path film_dir;
177         bool progress = true;
178         bool no_remote = false;
179         optional<int> threads;
180         optional<int> json_port;
181         bool keep_going = false;
182         bool dump = false;
183         bool servers = false;
184         bool dcp_path = false;
185
186         int option_index = 0;
187         while (true) {
188                 static struct option long_options[] = {
189                         { "version", no_argument, 0, 'v'},
190                         { "help", no_argument, 0, 'h'},
191                         { "flags", no_argument, 0, 'f'},
192                         { "no-progress", no_argument, 0, 'n'},
193                         { "no-remote", no_argument, 0, 'r'},
194                         { "threads", required_argument, 0, 't'},
195                         { "json", required_argument, 0, 'j'},
196                         { "keep-going", no_argument, 0, 'k' },
197                         { "servers", no_argument, 0, 's' },
198                         { "dcp-path", no_argument, 0, 'd' },
199                         /* Just using A, B, C ... from here on */
200                         { "dump", no_argument, 0, 'A' },
201                         { 0, 0, 0, 0 }
202                 };
203
204                 int c = getopt_long (argc, argv, "vhfnrt:j:kAsd", long_options, &option_index);
205
206                 if (c == -1) {
207                         break;
208                 }
209
210                 switch (c) {
211                 case 'v':
212                         cout << "dcpomatic version " << dcpomatic_version << " " << dcpomatic_git_commit << "\n";
213                         exit (EXIT_SUCCESS);
214                 case 'h':
215                         help (argv[0]);
216                         exit (EXIT_SUCCESS);
217                 case 'f':
218                         cout << dcpomatic_cxx_flags << "\n";
219                         exit (EXIT_SUCCESS);
220                 case 'n':
221                         progress = false;
222                         break;
223                 case 'r':
224                         no_remote = true;
225                         break;
226                 case 't':
227                         threads = atoi (optarg);
228                         break;
229                 case 'j':
230                         json_port = atoi (optarg);
231                         break;
232                 case 'k':
233                         keep_going = true;
234                         break;
235                 case 'A':
236                         dump = true;
237                         break;
238                 case 's':
239                         servers = true;
240                         break;
241                 case 'd':
242                         dcp_path = true;
243                         progress = false;
244                         break;
245                 }
246         }
247
248         if (servers) {
249                 show_servers ();
250                 exit (EXIT_SUCCESS);
251         }
252
253         if (optind >= argc) {
254                 help (argv[0]);
255                 exit (EXIT_FAILURE);
256         }
257
258         film_dir = argv[optind];
259
260         dcpomatic_setup_path_encoding ();
261         dcpomatic_setup ();
262         signal_manager = new SignalManager ();
263
264         if (no_remote) {
265                 EncodeServerFinder::instance()->stop ();
266         }
267
268         if (json_port) {
269                 new JSONServer (json_port.get ());
270         }
271
272         if (threads) {
273                 Config::instance()->set_master_encoding_threads (threads.get ());
274         }
275
276         shared_ptr<Film> film;
277         try {
278                 film.reset (new Film (film_dir));
279                 film->read_metadata ();
280         } catch (std::exception& e) {
281                 cerr << argv[0] << ": error reading film `" << film_dir.string() << "' (" << e.what() << ")\n";
282                 exit (EXIT_FAILURE);
283         }
284
285         if (dump) {
286                 print_dump (film);
287                 exit (EXIT_SUCCESS);
288         }
289
290         ContentList content = film->content ();
291         for (ContentList::const_iterator i = content.begin(); i != content.end(); ++i) {
292                 vector<boost::filesystem::path> paths = (*i)->paths ();
293                 for (vector<boost::filesystem::path>::const_iterator j = paths.begin(); j != paths.end(); ++j) {
294                         if (!boost::filesystem::exists (*j)) {
295                                 cerr << argv[0] << ": content file " << *j << " not found.\n";
296                                 exit (EXIT_FAILURE);
297                         }
298                 }
299         }
300
301         if (progress) {
302                 cout << "\nMaking DCP for " << film->name() << "\n";
303         }
304
305         film->make_dcp ();
306
307         bool should_stop = false;
308         bool first = true;
309         bool error = false;
310         while (!should_stop) {
311
312                 dcpomatic_sleep (5);
313
314                 list<shared_ptr<Job> > jobs = JobManager::instance()->get ();
315
316                 if (!first && progress) {
317                         for (size_t i = 0; i < jobs.size(); ++i) {
318                                 cout << "\033[1A\033[2K";
319                         }
320                         cout.flush ();
321                 }
322
323                 first = false;
324
325                 int unfinished = 0;
326                 int finished_in_error = 0;
327
328                 BOOST_FOREACH (shared_ptr<Job> i, jobs) {
329                         if (progress) {
330                                 cout << i->name();
331                                 if (!i->sub_name().empty()) {
332                                         cout << "; " << i->sub_name();
333                                 }
334                                 cout << ": ";
335
336                                 if (i->progress ()) {
337                                         cout << i->status() << "                            \n";
338                                 } else {
339                                         cout << ": Running           \n";
340                                 }
341                         }
342
343                         if (!i->finished ()) {
344                                 ++unfinished;
345                         }
346
347                         if (i->finished_in_error ()) {
348                                 ++finished_in_error;
349                                 error = true;
350                         }
351
352                         if (!progress && i->finished_in_error ()) {
353                                 /* We won't see this error if we haven't been showing progress,
354                                    so show it now.
355                                 */
356                                 cout << i->status() << "\n";
357                         }
358                 }
359
360                 if (unfinished == 0 || finished_in_error != 0) {
361                         should_stop = true;
362                 }
363         }
364
365         if (keep_going) {
366                 while (true) {
367                         dcpomatic_sleep (3600);
368                 }
369         }
370
371         /* This is just to stop valgrind reporting leaks due to JobManager
372            indirectly holding onto codecs.
373         */
374         JobManager::drop ();
375
376         EncodeServerFinder::drop ();
377
378         if (dcp_path && !error) {
379                 cout << film->dir (film->dcp_name (false)).string() << "\n";
380         }
381
382         return error ? EXIT_FAILURE : EXIT_SUCCESS;
383 }