Calm a busy-wait in dcpomatic_create.
[dcpomatic.git] / src / tools / dcpomatic_create.cc
1 /*
2     Copyright (C) 2013-2016 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/version.h"
22 #include "lib/film.h"
23 #include "lib/util.h"
24 #include "lib/content_factory.h"
25 #include "lib/job_manager.h"
26 #include "lib/signal_manager.h"
27 #include "lib/job.h"
28 #include "lib/dcp_content_type.h"
29 #include "lib/ratio.h"
30 #include "lib/image_content.h"
31 #include "lib/video_content.h"
32 #include "lib/cross.h"
33 #include <libxml++/libxml++.h>
34 #include <boost/filesystem.hpp>
35 #include <getopt.h>
36 #include <string>
37 #include <iostream>
38 #include <cstdlib>
39 #include <stdexcept>
40
41 using std::string;
42 using std::cout;
43 using std::cerr;
44 using std::list;
45 using std::exception;
46 using boost::shared_ptr;
47 using boost::dynamic_pointer_cast;
48 using boost::optional;
49
50 static void
51 syntax (string n)
52 {
53         cerr << "Syntax: " << n << " [OPTION] <CONTENT> [<CONTENT> ...]\n"
54              << "  -v, --version                 show DCP-o-matic version\n"
55              << "  -h, --help                    show this help\n"
56              << "  -n, --name <name>             film name\n"
57              << "  -t, --template <name>         template name\n"
58              << "  -c, --dcp-content-type <type> FTR, SHR, TLR, TST, XSN, RTG, TSR, POL, PSA or ADV\n"
59              << "      --container-ratio <ratio> 119, 133, 137, 138, 166, 178, 185 or 239\n"
60              << "      --content-ratio <ratio>   119, 133, 137, 138, 166, 178, 185 or 239\n"
61              << "  -s, --still-length <n>        number of seconds that still content should last\n"
62              << "      --standard <standard>     SMPTE or interop (default SMPTE)\n"
63              << "      --no-use-isdcf-name       do not use an ISDCF name; use the specified name unmodified\n"
64              << "      --no-sign                 do not sign the DCP\n"
65              << "  -o, --output <dir>            output directory\n";
66 }
67
68 static void
69 help (string n)
70 {
71         cerr << "Create a film directory (ready for making a DCP) or metadata file from some content files.\n"
72              << "A film directory will be created if -o or --output is specified, otherwise a metadata file\n"
73              << "will be written to stdout.\n";
74
75         syntax (n);
76 }
77
78 class SimpleSignalManager : public SignalManager
79 {
80 public:
81         /* Do nothing in this method so that UI events happen in our thread
82            when we call SignalManager::ui_idle().
83         */
84         void wake_ui () {}
85 };
86
87 int
88 main (int argc, char* argv[])
89 {
90         dcpomatic_setup_path_encoding ();
91         dcpomatic_setup ();
92
93         string name;
94         optional<string> template_name;
95         DCPContentType const * dcp_content_type = DCPContentType::from_isdcf_name ("TST");
96         Ratio const * container_ratio = 0;
97         Ratio const * content_ratio = 0;
98         int still_length = 10;
99         dcp::Standard standard = dcp::SMPTE;
100         boost::filesystem::path output;
101         bool sign = true;
102         bool use_isdcf_name = true;
103
104         int option_index = 0;
105         while (true) {
106                 static struct option long_options[] = {
107                         { "version", no_argument, 0, 'v'},
108                         { "help", no_argument, 0, 'h'},
109                         { "name", required_argument, 0, 'n'},
110                         { "template", required_argument, 0, 'f'},
111                         { "dcp-content-type", required_argument, 0, 'c'},
112                         { "container-ratio", required_argument, 0, 'A'},
113                         { "content-ratio", required_argument, 0, 'B'},
114                         { "still-length", required_argument, 0, 's'},
115                         { "standard", required_argument, 0, 'C'},
116                         { "no-use-isdcf-name", no_argument, 0, 'D'},
117                         { "no-sign", no_argument, 0, 'E'},
118                         { "output", required_argument, 0, 'o'},
119                         { 0, 0, 0, 0}
120                 };
121
122                 int c = getopt_long (argc, argv, "vhn:f:c:A:B:C:s:o:DE", long_options, &option_index);
123                 if (c == -1) {
124                         break;
125                 }
126
127                 switch (c) {
128                 case 'v':
129                         cout << "dcpomatic version " << dcpomatic_version << " " << dcpomatic_git_commit << "\n";
130                         exit (EXIT_SUCCESS);
131                 case 'h':
132                         help (argv[0]);
133                         exit (EXIT_SUCCESS);
134                 case 'n':
135                         name = optarg;
136                         break;
137                 case 't':
138                         template_name = optarg;
139                         break;
140                 case 'c':
141                         dcp_content_type = DCPContentType::from_isdcf_name (optarg);
142                         if (dcp_content_type == 0) {
143                                 cerr << "Bad DCP content type.\n";
144                                 syntax (argv[0]);
145                                 exit (EXIT_FAILURE);
146                         }
147                         break;
148                 case 'A':
149                         container_ratio = Ratio::from_id (optarg);
150                         if (container_ratio == 0) {
151                                 cerr << "Bad container ratio.\n";
152                                 syntax (argv[0]);
153                                 exit (EXIT_FAILURE);
154                         }
155                         break;
156                 case 'B':
157                         content_ratio = Ratio::from_id (optarg);
158                         if (content_ratio == 0) {
159                                 cerr << "Bad content ratio " << optarg << ".\n";
160                                 syntax (argv[0]);
161                                 exit (EXIT_FAILURE);
162                         }
163                         break;
164                 case 'C':
165                         if (strcmp (optarg, "interop") == 0) {
166                                 standard = dcp::INTEROP;
167                         } else if (strcmp (optarg, "SMPTE") != 0) {
168                                 cerr << "Bad standard " << optarg << ".\n";
169                                 syntax (argv[0]);
170                                 exit (EXIT_FAILURE);
171                         }
172                         break;
173                 case 'D':
174                         use_isdcf_name = false;
175                         break;
176                 case 'E':
177                         sign = false;
178                         break;
179                 case 's':
180                         still_length = atoi (optarg);
181                         break;
182                 case 'o':
183                         output = optarg;
184                         break;
185                 case '?':
186                         syntax (argv[0]);
187                         exit (EXIT_FAILURE);
188                 }
189         }
190
191         if (optind > argc) {
192                 help (argv[0]);
193                 exit (EXIT_FAILURE);
194         }
195
196         if (!content_ratio) {
197                 cerr << argv[0] << ": missing required option --content-ratio.\n";
198                 exit (EXIT_FAILURE);
199         }
200
201         if (!container_ratio) {
202                 container_ratio = content_ratio;
203         }
204
205         if (optind == argc) {
206                 cerr << argv[0] << ": no content specified.\n";
207                 exit (EXIT_FAILURE);
208         }
209
210         signal_manager = new SimpleSignalManager ();
211
212         if (name.empty ()) {
213                 name = boost::filesystem::path (argv[optind]).leaf().string ();
214         }
215
216         try {
217                 shared_ptr<Film> film (new Film (output));
218                 if (template_name) {
219                         film->use_template (template_name.get());
220                 }
221                 film->set_name (name);
222
223                 film->set_container (container_ratio);
224                 film->set_dcp_content_type (dcp_content_type);
225                 film->set_interop (standard == dcp::INTEROP);
226                 film->set_use_isdcf_name (use_isdcf_name);
227                 film->set_signed (sign);
228
229                 for (int i = optind; i < argc; ++i) {
230                         shared_ptr<Content> c = content_factory (film, boost::filesystem::canonical (argv[i]));
231                         if (c->video) {
232                                 c->video->set_scale (VideoContentScale (content_ratio));
233                         }
234                         film->examine_and_add_content (c);
235                 }
236
237                 JobManager* jm = JobManager::instance ();
238
239                 while (jm->work_to_do ()) {
240                         dcpomatic_sleep (1);
241                 }
242
243                 while (signal_manager->ui_idle() > 0) {}
244
245                 ContentList content = film->content ();
246                 for (ContentList::iterator i = content.begin(); i != content.end(); ++i) {
247                         shared_ptr<ImageContent> ic = dynamic_pointer_cast<ImageContent> (*i);
248                         if (ic) {
249                                 ic->video->set_length (still_length * 24);
250                         }
251                 }
252
253                 if (jm->errors ()) {
254                         list<shared_ptr<Job> > jobs = jm->get ();
255                         for (list<shared_ptr<Job> >::iterator i = jobs.begin(); i != jobs.end(); ++i) {
256                                 if ((*i)->finished_in_error ()) {
257                                         cerr << (*i)->error_summary () << "\n"
258                                              << (*i)->error_details () << "\n";
259                                 }
260                         }
261                         exit (EXIT_FAILURE);
262                 }
263
264                 if (!output.empty ()) {
265                         film->write_metadata ();
266                 } else {
267                         film->metadata()->write_to_stream_formatted (cout, "UTF-8");
268                 }
269         } catch (exception& e) {
270                 cerr << argv[0] << ": " << e.what() << "\n";
271                 exit (EXIT_FAILURE);
272         }
273
274         return 0;
275 }