Include tidying src/lib/a-j*.h
[dcpomatic.git] / src / tools / dcpomatic_cli.cc
1 /*
2     Copyright (C) 2012-2015 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/film.h"
21 #include "lib/filter.h"
22 #include "lib/transcode_job.h"
23 #include "lib/job_manager.h"
24 #include "lib/util.h"
25 #include "lib/version.h"
26 #include "lib/cross.h"
27 #include "lib/config.h"
28 #include "lib/log.h"
29 #include "lib/signal_manager.h"
30 #include "lib/server_finder.h"
31 #include "lib/json_server.h"
32 #include "lib/ratio.h"
33 #include "lib/video_content.h"
34 #include "lib/audio_content.h"
35 #include <dcp/version.h>
36 #include <boost/foreach.hpp>
37 #include <getopt.h>
38 #include <iostream>
39 #include <iomanip>
40
41 using std::string;
42 using std::cerr;
43 using std::cout;
44 using std::vector;
45 using std::pair;
46 using std::list;
47 using boost::shared_ptr;
48 using boost::optional;
49 using boost::dynamic_pointer_cast;
50
51 static void
52 help (string n)
53 {
54         cerr << "Syntax: " << n << " [OPTION] <FILM>\n"
55              << "  -v, --version      show DCP-o-matic version\n"
56              << "  -h, --help         show this help\n"
57              << "  -f, --flags        show flags passed to C++ compiler on build\n"
58              << "  -n, --no-progress  do not print progress to stdout\n"
59              << "  -r, --no-remote    do not use any remote servers\n"
60              << "  -j, --json <port>  run a JSON server on the specified port\n"
61              << "  -k, --keep-going   keep running even when the job is complete\n"
62              << "      --dump         just dump a summary of the film's settings; don't encode\n"
63              << "\n"
64              << "<FILM> is the film directory.\n";
65 }
66
67 static void
68 print_dump (shared_ptr<Film> film)
69 {
70         cout << film->dcp_name (true) << "\n"
71              << film->container()->nickname() << " at " << ((film->resolution() == RESOLUTION_2K) ? "2K" : "4K") << "\n"
72              << (film->j2k_bandwidth() / 1000000) << "Mbit/s" << "\n"
73              << "Output " << film->video_frame_rate() << "fps " << (film->three_d() ? "3D" : "2D") << " " << (film->audio_frame_rate() / 1000) << "kHz\n"
74              << (film->interop() ? "Inter-Op" : "SMPTE") << " " << (film->encrypted() ? "encrypted" : "unencrypted") << "\n";
75
76         BOOST_FOREACH (shared_ptr<Content> c, film->content ()) {
77                 cout << "\n"
78                      << c->path(0) << "\n"
79                      << "\tat " << c->position().seconds ()
80                      << " length " << c->full_length().seconds ()
81                      << " start trim " << c->trim_start().seconds ()
82                      << " end trim " << c->trim_end().seconds () << "\n";
83
84                 shared_ptr<VideoContent> video = dynamic_pointer_cast<VideoContent> (c);
85                 if (video) {
86                         cout << "\t" << video->video_size().width << "x" << video->video_size().height << "\n"
87                              << "\t" << video->video_frame_rate() << "fps\n"
88                              << "\tcrop left " << video->left_crop()
89                              << " right " << video->right_crop()
90                              << " top " << video->top_crop()
91                              << " bottom " << video->bottom_crop() << "\n"
92                              << "\tscale " << video->scale().name() << "\n";
93                         if (video->colour_conversion()) {
94                                 if (video->colour_conversion().get().preset()) {
95                                         cout << "\tcolour conversion "
96                                              << PresetColourConversion::all()[video->colour_conversion().get().preset().get()].name
97                                              << "\n";
98                                 } else {
99                                         cout << "\tcustom colour conversion\n";
100                                 }
101                         } else {
102                                 cout << "\tno colour conversion\n";
103                         }
104
105                 }
106
107                 shared_ptr<AudioContent> audio = dynamic_pointer_cast<AudioContent> (c);
108                 if (audio) {
109                         cout << "\t" << audio->audio_delay() << " delay\n"
110                              << "\t" << audio->audio_gain() << " gain\n";
111                 }
112         }
113 }
114
115 int
116 main (int argc, char* argv[])
117 {
118         string film_dir;
119         bool progress = true;
120         bool no_remote = false;
121         optional<int> json_port;
122         bool keep_going = false;
123         bool dump = false;
124
125         int option_index = 0;
126         while (true) {
127                 static struct option long_options[] = {
128                         { "version", no_argument, 0, 'v'},
129                         { "help", no_argument, 0, 'h'},
130                         { "flags", no_argument, 0, 'f'},
131                         { "no-progress", no_argument, 0, 'n'},
132                         { "no-remote", no_argument, 0, 'r'},
133                         { "json", required_argument, 0, 'j'},
134                         { "keep-going", no_argument, 0, 'k' },
135                         /* Just using A, B, C ... from here on */
136                         { "dump", no_argument, 0, 'A' },
137                         { 0, 0, 0, 0 }
138                 };
139
140                 int c = getopt_long (argc, argv, "vhfnrj:kA", long_options, &option_index);
141
142                 if (c == -1) {
143                         break;
144                 }
145
146                 switch (c) {
147                 case 'v':
148                         cout << "dcpomatic version " << dcpomatic_version << " " << dcpomatic_git_commit << "\n";
149                         exit (EXIT_SUCCESS);
150                 case 'h':
151                         help (argv[0]);
152                         exit (EXIT_SUCCESS);
153                 case 'f':
154                         cout << dcpomatic_cxx_flags << "\n";
155                         exit (EXIT_SUCCESS);
156                 case 'n':
157                         progress = false;
158                         break;
159                 case 'r':
160                         no_remote = true;
161                         break;
162                 case 'j':
163                         json_port = atoi (optarg);
164                         break;
165                 case 'k':
166                         keep_going = true;
167                         break;
168                 case 'A':
169                         dump = true;
170                         break;
171                 }
172         }
173
174         if (optind >= argc) {
175                 help (argv[0]);
176                 exit (EXIT_FAILURE);
177         }
178
179         film_dir = argv[optind];
180
181         dcpomatic_setup_path_encoding ();
182         dcpomatic_setup ();
183         signal_manager = new SignalManager ();
184
185         if (no_remote) {
186                 ServerFinder::instance()->disable ();
187         }
188
189         if (json_port) {
190                 new JSONServer (json_port.get ());
191         }
192
193         shared_ptr<Film> film;
194         try {
195                 film.reset (new Film (film_dir));
196                 film->read_metadata ();
197         } catch (std::exception& e) {
198                 cerr << argv[0] << ": error reading film `" << film_dir << "' (" << e.what() << ")\n";
199                 exit (EXIT_FAILURE);
200         }
201
202         if (dump) {
203                 print_dump (film);
204                 exit (EXIT_SUCCESS);
205         }
206
207         ContentList content = film->content ();
208         for (ContentList::const_iterator i = content.begin(); i != content.end(); ++i) {
209                 vector<boost::filesystem::path> paths = (*i)->paths ();
210                 for (vector<boost::filesystem::path>::const_iterator j = paths.begin(); j != paths.end(); ++j) {
211                         if (!boost::filesystem::exists (*j)) {
212                                 cerr << argv[0] << ": content file " << *j << " not found.\n";
213                                 exit (EXIT_FAILURE);
214                         }
215                 }
216         }
217
218         if (progress) {
219                 cout << "\nMaking DCP for " << film->name() << "\n";
220         }
221
222         film->make_dcp ();
223
224         bool should_stop = false;
225         bool first = true;
226         bool error = false;
227         while (!should_stop) {
228
229                 dcpomatic_sleep (5);
230
231                 list<shared_ptr<Job> > jobs = JobManager::instance()->get ();
232
233                 if (!first && progress) {
234                         cout << "\033[" << jobs.size() << "A";
235                         cout.flush ();
236                 }
237
238                 first = false;
239
240                 int unfinished = 0;
241                 int finished_in_error = 0;
242
243                 for (list<shared_ptr<Job> >::iterator i = jobs.begin(); i != jobs.end(); ++i) {
244                         if (progress) {
245                                 cout << (*i)->name() << ": ";
246
247                                 if ((*i)->progress ()) {
248                                         cout << (*i)->status() << "                         \n";
249                                 } else {
250                                         cout << ": Running           \n";
251                                 }
252                         }
253
254                         if (!(*i)->finished ()) {
255                                 ++unfinished;
256                         }
257
258                         if ((*i)->finished_in_error ()) {
259                                 ++finished_in_error;
260                                 error = true;
261                         }
262
263                         if (!progress && (*i)->finished_in_error ()) {
264                                 /* We won't see this error if we haven't been showing progress,
265                                    so show it now.
266                                 */
267                                 cout << (*i)->status() << "\n";
268                         }
269                 }
270
271                 if (unfinished == 0 || finished_in_error != 0) {
272                         should_stop = true;
273                 }
274         }
275
276         if (keep_going) {
277                 while (true) {
278                         dcpomatic_sleep (3600);
279                 }
280         }
281
282         /* This is just to stop valgrind reporting leaks due to JobManager
283            indirectly holding onto codecs.
284         */
285         JobManager::drop ();
286
287         ServerFinder::drop ();
288
289         return error ? EXIT_FAILURE : EXIT_SUCCESS;
290 }