2 Copyright (C) 2013-2017 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
21 /** @file src/tools/dcpomatic_kdm_cli.cc
22 * @brief Command-line program to generate KDMs.
26 #include "lib/cinema.h"
27 #include "lib/screen_kdm.h"
28 #include "lib/cinema_kdms.h"
29 #include "lib/config.h"
30 #include "lib/exceptions.h"
31 #include "lib/emailer.h"
32 #include "lib/dkdm_wrapper.h"
33 #include "lib/screen.h"
34 #include <dcp/certificate.h>
35 #include <dcp/decrypted_kdm.h>
36 #include <dcp/encrypted_kdm.h>
45 using boost::shared_ptr;
46 using boost::optional;
48 using boost::dynamic_pointer_cast;
53 cerr << "Syntax: " << program_name << " [OPTION] <FILM|CPL-ID|DKDM>\n"
54 " -h, --help show this help\n"
55 " -o, --output output file or directory\n"
56 " -K, --filename-format filename format for KDMs\n"
57 " -Z, --container-name-format filename format for ZIP containers\n"
58 " -f, --valid-from valid from time (in local time zone of the cinema) (e.g. \"2013-09-28 01:41:51\") or \"now\"\n"
59 " -t, --valid-to valid to time (in local time zone of the cinema) (e.g. \"2014-09-28 01:41:51\")\n"
60 " -d, --valid-duration valid duration (e.g. \"1 day\", \"4 hours\", \"2 weeks\")\n"
61 " -F, --formulation modified-transitional-1, multiple-modified-transitional-1, dci-any or dci-specific [default modified-transitional-1]\n"
62 " -a, --disable-forensic-marking-picture disable forensic of pictures essences\n"
63 " -a, --disable-forensic-marking-audio disable forensic of audio essences (optionally above a given channel, e.g 12)\n"
64 " -z, --zip ZIP each cinema's KDMs into its own file\n"
65 " -v, --verbose be verbose\n"
66 " -c, --cinema specify a cinema, either by name or email address\n"
67 " -S, --screen screen description\n"
68 " -C, --certificate file containing projector certificate\n"
69 " -T, --trusted-device file containing a trusted device's certificate\n"
70 " --list-cinemas list known cinemas from the DCP-o-matic settings\n"
71 " --list-dkdm-cpls list CPLs for which DCP-o-matic has DKDMs\n\n"
72 "CPL-ID must be the ID of a CPL that is mentioned in DCP-o-matic's DKDM list.\n\n"
74 "Create KDMs for my_great_movie to play in all of Fred's Cinema's screens for the next two weeks and zip them up.\n"
75 "(Fred's Cinema must have been set up in DCP-o-matic's KDM window)\n\n"
76 "\tdcpomatic2_kdm_cli -c \"Fred's Cinema\" -f now -d \"2 weeks\" -z my_great_movie\n\n";
82 cerr << program_name << ": " << m << "\n";
86 static boost::posix_time::ptime
87 time_from_string (string t)
90 return boost::posix_time::second_clock::local_time ();
93 return boost::posix_time::time_from_string (t);
96 static boost::posix_time::time_duration
97 duration_from_string (string d)
100 char unit_buf[64] = "\0";
101 sscanf (d.c_str(), "%d %63s", &N, unit_buf);
102 string const unit (unit_buf);
105 cerr << "Could not understand duration \"" << d << "\"\n";
109 if (unit == "year" || unit == "years") {
110 return boost::posix_time::time_duration (N * 24 * 365, 0, 0, 0);
111 } else if (unit == "week" || unit == "weeks") {
112 return boost::posix_time::time_duration (N * 24 * 7, 0, 0, 0);
113 } else if (unit == "day" || unit == "days") {
114 return boost::posix_time::time_duration (N * 24, 0, 0, 0);
115 } else if (unit == "hour" || unit == "hours") {
116 return boost::posix_time::time_duration (N, 0, 0, 0);
119 cerr << "Could not understand duration \"" << d << "\"\n";
131 list<ScreenKDM> screen_kdms,
133 boost::filesystem::path output,
134 dcp::NameFormat container_name_format,
135 dcp::NameFormat filename_format,
136 dcp::NameFormat::Map values,
141 int const N = CinemaKDMs::write_zip_files (
142 CinemaKDMs::collect (screen_kdms),
144 container_name_format,
147 bind (&always_overwrite)
151 cout << "Wrote " << N << " ZIP files to " << output << "\n";
154 int const N = ScreenKDM::write_files (
155 screen_kdms, output, filename_format, values,
156 bind (&always_overwrite)
160 cout << "Wrote " << N << " KDM files to " << output << "\n";
166 find_cinema (string cinema_name)
168 list<shared_ptr<Cinema> > cinemas = Config::instance()->cinemas ();
169 list<shared_ptr<Cinema> >::const_iterator i = cinemas.begin();
171 i != cinemas.end() &&
172 (*i)->name != cinema_name &&
173 find ((*i)->emails.begin(), (*i)->emails.end(), cinema_name) == (*i)->emails.end()) {
178 if (i == cinemas.end ()) {
179 cerr << program_name << ": could not find cinema \"" << cinema_name << "\"\n";
188 list<shared_ptr<Screen> > screens,
189 boost::filesystem::path film_dir,
191 boost::filesystem::path output,
192 dcp::NameFormat container_name_format,
193 dcp::NameFormat filename_format,
194 boost::posix_time::ptime valid_from,
195 boost::posix_time::ptime valid_to,
196 dcp::Formulation formulation,
197 bool disable_forensic_marking_picture,
198 optional<int> disable_forensic_marking_audio,
202 shared_ptr<Film> film;
204 film.reset (new Film (film_dir));
205 film->read_metadata ();
207 cout << "Read film " << film->name () << "\n";
209 } catch (std::exception& e) {
210 cerr << program_name << ": error reading film `" << film_dir.string() << "' (" << e.what() << ")\n";
214 /* XXX: allow specification of this */
215 vector<CPLSummary> cpls = film->cpls ();
217 error ("no CPLs found in film");
218 } else if (cpls.size() > 1) {
219 error ("more than one CPL found in film");
222 boost::filesystem::path cpl = cpls.front().cpl_file;
224 dcp::NameFormat::Map values;
225 values['f'] = film->name();
226 values['b'] = dcp::LocalTime(valid_from).date() + " " + dcp::LocalTime(valid_from).time_of_day(true, false);
227 values['e'] = dcp::LocalTime(valid_to).date() + " " + dcp::LocalTime(valid_to).time_of_day(true, false);
230 list<ScreenKDM> screen_kdms = film->make_kdms (
231 screens, cpl, valid_from, valid_to, formulation, disable_forensic_marking_picture, disable_forensic_marking_audio
234 write_files (screen_kdms, zip, output, container_name_format, filename_format, values, verbose);
235 } catch (FileError& e) {
236 cerr << program_name << ": " << e.what() << " (" << e.file().string() << ")\n";
238 } catch (KDMError& e) {
239 cerr << program_name << ": " << e.what() << "\n";
244 optional<dcp::EncryptedKDM>
245 sub_find_dkdm (shared_ptr<DKDMGroup> group, string cpl_id)
247 BOOST_FOREACH (shared_ptr<DKDMBase> i, group->children()) {
248 shared_ptr<DKDMGroup> g = dynamic_pointer_cast<DKDMGroup>(i);
250 optional<dcp::EncryptedKDM> dkdm = sub_find_dkdm (g, cpl_id);
255 shared_ptr<DKDM> d = dynamic_pointer_cast<DKDM>(i);
257 if (d->dkdm().cpl_id() == cpl_id) {
263 return optional<dcp::EncryptedKDM>();
266 optional<dcp::EncryptedKDM>
267 find_dkdm (string cpl_id)
269 return sub_find_dkdm (Config::instance()->dkdms(), cpl_id);
274 dcp::DecryptedKDM dkdm,
275 dcp::Certificate target,
276 vector<string> trusted_devices,
277 dcp::LocalTime valid_from,
278 dcp::LocalTime valid_to,
279 dcp::Formulation formulation,
280 bool disable_forensic_marking_picture,
281 optional<int> disable_forensic_marking_audio
284 /* Signer for new KDM */
285 shared_ptr<const dcp::CertificateChain> signer = Config::instance()->signer_chain ();
286 if (!signer->valid ()) {
287 error ("signing certificate chain is invalid.");
290 /* Make a new empty KDM and add the keys from the DKDM to it */
291 dcp::DecryptedKDM kdm (
294 dkdm.annotation_text().get_value_or(""),
295 dkdm.content_title_text(),
296 dcp::LocalTime().as_string()
299 BOOST_FOREACH (dcp::DecryptedKDMKey const & j, dkdm.keys()) {
303 return kdm.encrypt (signer, target, trusted_devices, formulation, disable_forensic_marking_picture, disable_forensic_marking_audio);
308 list<shared_ptr<Screen> > screens,
309 dcp::DecryptedKDM dkdm,
311 boost::filesystem::path output,
312 dcp::NameFormat container_name_format,
313 dcp::NameFormat filename_format,
314 boost::posix_time::ptime valid_from,
315 boost::posix_time::ptime valid_to,
316 dcp::Formulation formulation,
317 bool disable_forensic_marking_picture,
318 optional<int> disable_forensic_marking_audio,
322 dcp::NameFormat::Map values;
323 values['f'] = dkdm.annotation_text().get_value_or("");
324 values['b'] = dcp::LocalTime(valid_from).date() + " " + dcp::LocalTime(valid_from).time_of_day(true, false);
325 values['e'] = dcp::LocalTime(valid_to).date() + " " + dcp::LocalTime(valid_to).time_of_day(true, false);
328 list<ScreenKDM> screen_kdms;
329 BOOST_FOREACH (shared_ptr<Screen> i, screens) {
334 screen_kdms.push_back (
340 i->trusted_device_thumbprints(),
341 dcp::LocalTime(valid_from, i->cinema->utc_offset_hour(), i->cinema->utc_offset_minute()),
342 dcp::LocalTime(valid_to, i->cinema->utc_offset_hour(), i->cinema->utc_offset_minute()),
344 disable_forensic_marking_picture,
345 disable_forensic_marking_audio
350 write_files (screen_kdms, zip, output, container_name_format, filename_format, values, verbose);
351 } catch (FileError& e) {
352 cerr << program_name << ": " << e.what() << " (" << e.file().string() << ")\n";
354 } catch (KDMError& e) {
355 cerr << program_name << ": " << e.what() << "\n";
361 dump_dkdm_group (shared_ptr<DKDMGroup> group, int indent)
364 for (int i = 0; i < indent; ++i) {
367 cout << group->name() << "\n";
369 BOOST_FOREACH (shared_ptr<DKDMBase> i, group->children()) {
370 shared_ptr<DKDMGroup> g = dynamic_pointer_cast<DKDMGroup>(i);
372 dump_dkdm_group (g, indent + 2);
374 for (int j = 0; j < indent; ++j) {
377 shared_ptr<DKDM> d = dynamic_pointer_cast<DKDM>(i);
379 cout << d->dkdm().cpl_id() << "\n";
384 int main (int argc, char* argv[])
386 boost::filesystem::path output = ".";
387 dcp::NameFormat container_name_format = Config::instance()->kdm_container_name_format();
388 dcp::NameFormat filename_format = Config::instance()->kdm_filename_format();
389 optional<string> cinema_name;
390 shared_ptr<Cinema> cinema;
391 string screen_description = "";
392 list<shared_ptr<Screen> > screens;
393 optional<dcp::EncryptedKDM> dkdm;
394 optional<boost::posix_time::ptime> valid_from;
395 optional<boost::posix_time::ptime> valid_to;
397 bool list_cinemas = false;
398 bool list_dkdm_cpls = false;
399 optional<string> duration_string;
400 bool verbose = false;
401 dcp::Formulation formulation = dcp::MODIFIED_TRANSITIONAL_1;
402 bool disable_forensic_marking_picture = false;
403 optional<int> disable_forensic_marking_audio;
405 program_name = argv[0];
407 int option_index = 0;
409 static struct option long_options[] = {
410 { "help", no_argument, 0, 'h'},
411 { "output", required_argument, 0, 'o'},
412 { "filename-format", required_argument, 0, 'K'},
413 { "container-name-format", required_argument, 0, 'Z'},
414 { "valid-from", required_argument, 0, 'f'},
415 { "valid-to", required_argument, 0, 't'},
416 { "valid-duration", required_argument, 0, 'd'},
417 { "formulation", required_argument, 0, 'F' },
418 { "disable-forensic-marking-picture", no_argument, 0, 'p' },
419 { "disable-forensic-marking-audio", optional_argument, 0, 'a' },
420 { "zip", no_argument, 0, 'z' },
421 { "verbose", no_argument, 0, 'v' },
422 { "cinema", required_argument, 0, 'c' },
423 { "screen", required_argument, 0, 'S' },
424 { "certificate", required_argument, 0, 'C' },
425 { "trusted-device", required_argument, 0, 'T' },
426 { "list-cinemas", no_argument, 0, 'B' },
427 { "list-dkdm-cpls", no_argument, 0, 'D' },
431 int c = getopt_long (argc, argv, "ho:K:Z:f:t:d:F:pa::zvc:S:C:T:BD", long_options, &option_index);
445 filename_format = dcp::NameFormat (optarg);
448 container_name_format = dcp::NameFormat (optarg);
451 valid_from = time_from_string (optarg);
454 valid_to = time_from_string (optarg);
457 duration_string = optarg;
460 if (string (optarg) == "modified-transitional-1") {
461 formulation = dcp::MODIFIED_TRANSITIONAL_1;
462 } else if (string (optarg) == "multiple-modified-transitional-1") {
463 formulation = dcp::MULTIPLE_MODIFIED_TRANSITIONAL_1;
464 } else if (string (optarg) == "dci-any") {
465 formulation = dcp::DCI_ANY;
466 } else if (string (optarg) == "dci-specific") {
467 formulation = dcp::DCI_SPECIFIC;
469 error ("unrecognised KDM formulation " + string (optarg));
473 disable_forensic_marking_picture = true;
476 disable_forensic_marking_audio = 0;
477 if (optarg == 0 && argv[optind] != 0 && argv[optind][0] != '-') {
478 disable_forensic_marking_audio = atoi (argv[optind++]);
480 disable_forensic_marking_audio = atoi (optarg);
490 /* This could be a cinema to search for in the configured list or the name of a cinema being
491 built up on-the-fly in the option. Cater for both possilibities here by storing the name
492 (for lookup) and by creating a Cinema which the next Screen will be added to.
494 cinema_name = optarg;
495 cinema = shared_ptr<Cinema> (new Cinema (optarg, list<string>(), "", 0, 0));
498 screen_description = optarg;
502 /* Make a new screen and add it to the current cinema */
503 shared_ptr<Screen> screen (new Screen (screen_description, dcp::Certificate (dcp::file_to_string (optarg)), vector<TrustedDevice>()));
505 cinema->add_screen (screen);
507 screens.push_back (screen);
511 /* A trusted device ends up in the last screen we made */
512 if (!screens.empty ()) {
513 screens.back()->trusted_devices.push_back(TrustedDevice(dcp::Certificate(dcp::file_to_string(optarg))));
520 list_dkdm_cpls = true;
526 list<boost::shared_ptr<Cinema> > cinemas = Config::instance()->cinemas ();
527 for (list<boost::shared_ptr<Cinema> >::const_iterator i = cinemas.begin(); i != cinemas.end(); ++i) {
528 cout << (*i)->name << " (" << Emailer::address_list ((*i)->emails) << ")\n";
533 if (list_dkdm_cpls) {
534 dump_dkdm_group (Config::instance()->dkdms(), 0);
538 if (!duration_string && !valid_to) {
539 error ("you must specify a --valid-duration or --valid-to");
543 error ("you must specify --valid-from");
547 if (optind >= argc) {
552 if (screens.empty()) {
554 error ("you must specify either a cinema or one or more screens using certificate files");
557 screens = find_cinema (*cinema_name)->screens ();
560 if (duration_string) {
561 valid_to = valid_from.get() + duration_from_string (*duration_string);
564 dcpomatic_setup_path_encoding ();
568 cout << "Making KDMs valid from " << valid_from.get() << " to " << valid_to.get() << "\n";
571 string const thing = argv[optind];
572 if (boost::filesystem::is_directory(thing) && boost::filesystem::is_regular_file(boost::filesystem::path(thing) / "metadata.xml")) {
578 container_name_format,
583 disable_forensic_marking_picture,
584 disable_forensic_marking_audio,
588 if (boost::filesystem::is_regular_file(thing)) {
589 dkdm = dcp::EncryptedKDM (dcp::file_to_string (thing));
591 dkdm = find_dkdm (thing);
595 error ("could not find film or CPL ID corresponding to " + thing);
600 dcp::DecryptedKDM (*dkdm, Config::instance()->decryption_chain()->key().get()),
603 container_name_format,
608 disable_forensic_marking_picture,
609 disable_forensic_marking_audio,