2 Copyright (C) 2013-2018 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/kdm_with_metadata.h"
28 #include "lib/config.h"
29 #include "lib/exceptions.h"
30 #include "lib/emailer.h"
31 #include "lib/dkdm_wrapper.h"
32 #include "lib/screen.h"
33 #include <dcp/certificate.h>
34 #include <dcp/decrypted_kdm.h>
35 #include <dcp/encrypted_kdm.h>
44 using std::runtime_error;
45 using boost::shared_ptr;
46 using boost::optional;
48 using boost::dynamic_pointer_cast;
49 using namespace dcpomatic;
54 cerr << "Syntax: " << program_name << " [OPTION] <FILM|CPL-ID|DKDM>\n"
55 " -h, --help show this help\n"
56 " -o, --output output file or directory\n"
57 " -K, --filename-format filename format for KDMs\n"
58 " -Z, --container-name-format filename format for ZIP containers\n"
59 " -f, --valid-from valid from time (in local time zone of the cinema) (e.g. \"2013-09-28 01:41:51\") or \"now\"\n"
60 " -t, --valid-to valid to time (in local time zone of the cinema) (e.g. \"2014-09-28 01:41:51\")\n"
61 " -d, --valid-duration valid duration (e.g. \"1 day\", \"4 hours\", \"2 weeks\")\n"
62 " -F, --formulation modified-transitional-1, multiple-modified-transitional-1, dci-any or dci-specific [default modified-transitional-1]\n"
63 " -a, --disable-forensic-marking-picture disable forensic marking of pictures essences\n"
64 " -a, --disable-forensic-marking-audio disable forensic marking of audio essences (optionally above a given channel, e.g 12)\n"
65 " -z, --zip ZIP each cinema's KDMs into its own file\n"
66 " -v, --verbose be verbose\n"
67 " -c, --cinema specify a cinema, either by name or email address\n"
68 " -S, --screen screen description\n"
69 " -C, --certificate file containing projector certificate\n"
70 " -T, --trusted-device file containing a trusted device's certificate\n"
71 " --list-cinemas list known cinemas from the DCP-o-matic settings\n"
72 " --list-dkdm-cpls list CPLs for which DCP-o-matic has DKDMs\n\n"
73 "CPL-ID must be the ID of a CPL that is mentioned in DCP-o-matic's DKDM list.\n\n"
75 "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"
76 "(Fred's Cinema must have been set up in DCP-o-matic's KDM window)\n\n"
77 "\t" << program_name << " -c \"Fred's Cinema\" -f now -d \"2 weeks\" -z my_great_movie\n\n";
83 cerr << program_name << ": " << m << "\n";
87 static boost::posix_time::ptime
88 time_from_string (string t)
91 return boost::posix_time::second_clock::local_time ();
94 return boost::posix_time::time_from_string (t);
97 static boost::posix_time::time_duration
98 duration_from_string (string d)
101 char unit_buf[64] = "\0";
102 sscanf (d.c_str(), "%d %63s", &N, unit_buf);
103 string const unit (unit_buf);
106 cerr << "Could not understand duration \"" << d << "\"\n";
110 if (unit == "year" || unit == "years") {
111 return boost::posix_time::time_duration (N * 24 * 365, 0, 0, 0);
112 } else if (unit == "week" || unit == "weeks") {
113 return boost::posix_time::time_duration (N * 24 * 7, 0, 0, 0);
114 } else if (unit == "day" || unit == "days") {
115 return boost::posix_time::time_duration (N * 24, 0, 0, 0);
116 } else if (unit == "hour" || unit == "hours") {
117 return boost::posix_time::time_duration (N, 0, 0, 0);
120 cerr << "Could not understand duration \"" << d << "\"\n";
132 list<KDMWithMetadataPtr> kdms,
134 boost::filesystem::path output,
135 dcp::NameFormat container_name_format,
136 dcp::NameFormat filename_format,
141 int const N = write_zip_files (
144 container_name_format,
146 bind (&always_overwrite)
150 cout << "Wrote " << N << " ZIP files to " << output << "\n";
153 int const N = write_files (
154 kdms, output, filename_format,
155 bind (&always_overwrite)
159 cout << "Wrote " << N << " KDM files to " << output << "\n";
165 find_cinema (string cinema_name)
167 list<shared_ptr<Cinema> > cinemas = Config::instance()->cinemas ();
168 list<shared_ptr<Cinema> >::const_iterator i = cinemas.begin();
170 i != cinemas.end() &&
171 (*i)->name != cinema_name &&
172 find ((*i)->emails.begin(), (*i)->emails.end(), cinema_name) == (*i)->emails.end()) {
177 if (i == cinemas.end ()) {
178 cerr << program_name << ": could not find cinema \"" << cinema_name << "\"\n";
187 list<shared_ptr<Screen> > screens,
188 boost::filesystem::path film_dir,
190 boost::filesystem::path output,
191 dcp::NameFormat container_name_format,
192 dcp::NameFormat filename_format,
193 boost::posix_time::ptime valid_from,
194 boost::posix_time::ptime valid_to,
195 dcp::Formulation formulation,
196 bool disable_forensic_marking_picture,
197 optional<int> disable_forensic_marking_audio,
201 shared_ptr<Film> film;
203 film.reset (new Film (film_dir));
204 film->read_metadata ();
206 cout << "Read film " << film->name () << "\n";
208 } catch (std::exception& e) {
209 cerr << program_name << ": error reading film `" << film_dir.string() << "' (" << e.what() << ")\n";
213 /* XXX: allow specification of this */
214 vector<CPLSummary> cpls = film->cpls ();
216 error ("no CPLs found in film");
217 } else if (cpls.size() > 1) {
218 error ("more than one CPL found in film");
221 boost::filesystem::path cpl = cpls.front().cpl_file;
223 dcp::NameFormat::Map values;
226 list<KDMWithMetadataPtr> kdms;
228 BOOST_FOREACH (shared_ptr<Screen> i, screens) {
231 dcp::LocalTime const begin(valid_from, i->cinema ? i->cinema->utc_offset_hour() : 0, i->cinema ? i->cinema->utc_offset_minute() : 0);
232 dcp::LocalTime const end(valid_to, i->cinema ? i->cinema->utc_offset_hour() : 0, i->cinema ? i->cinema->utc_offset_minute() : 0);
234 dcp::EncryptedKDM const kdm = film->make_kdm (
236 i->trusted_device_thumbprints(),
241 disable_forensic_marking_picture,
242 disable_forensic_marking_audio
245 dcp::NameFormat::Map name_values;
246 name_values['c'] = i->cinema->name;
247 name_values['s'] = i->name;
248 name_values['f'] = film->name();
249 name_values['b'] = dcp::LocalTime(begin).date() + " " + dcp::LocalTime(begin).time_of_day(true, false);
250 name_values['e'] = dcp::LocalTime(end).date() + " " + dcp::LocalTime(end).time_of_day(true, false);
251 name_values['i'] = kdm.cpl_id();
253 kdms.push_back (KDMWithMetadataPtr(new DCPKDMWithMetadata(name_values, i->cinema, kdm)));
257 write_files (kdms, zip, output, container_name_format, filename_format, verbose);
258 } catch (FileError& e) {
259 cerr << program_name << ": " << e.what() << " (" << e.file().string() << ")\n";
261 } catch (KDMError& e) {
262 cerr << program_name << ": " << e.what() << "\n";
264 } catch (runtime_error& e) {
265 cerr << program_name << ": " << e.what() << "\n";
270 optional<dcp::EncryptedKDM>
271 sub_find_dkdm (shared_ptr<DKDMGroup> group, string cpl_id)
273 BOOST_FOREACH (shared_ptr<DKDMBase> i, group->children()) {
274 shared_ptr<DKDMGroup> g = dynamic_pointer_cast<DKDMGroup>(i);
276 optional<dcp::EncryptedKDM> dkdm = sub_find_dkdm (g, cpl_id);
281 shared_ptr<DKDM> d = dynamic_pointer_cast<DKDM>(i);
283 if (d->dkdm().cpl_id() == cpl_id) {
289 return optional<dcp::EncryptedKDM>();
292 optional<dcp::EncryptedKDM>
293 find_dkdm (string cpl_id)
295 return sub_find_dkdm (Config::instance()->dkdms(), cpl_id);
300 dcp::DecryptedKDM dkdm,
301 dcp::Certificate target,
302 vector<string> trusted_devices,
303 dcp::LocalTime valid_from,
304 dcp::LocalTime valid_to,
305 dcp::Formulation formulation,
306 bool disable_forensic_marking_picture,
307 optional<int> disable_forensic_marking_audio
310 /* Signer for new KDM */
311 shared_ptr<const dcp::CertificateChain> signer = Config::instance()->signer_chain ();
312 if (!signer->valid ()) {
313 error ("signing certificate chain is invalid.");
316 /* Make a new empty KDM and add the keys from the DKDM to it */
317 dcp::DecryptedKDM kdm (
320 dkdm.annotation_text().get_value_or(""),
321 dkdm.content_title_text(),
322 dcp::LocalTime().as_string()
325 BOOST_FOREACH (dcp::DecryptedKDMKey const & j, dkdm.keys()) {
329 return kdm.encrypt (signer, target, trusted_devices, formulation, disable_forensic_marking_picture, disable_forensic_marking_audio);
334 list<shared_ptr<Screen> > screens,
335 dcp::DecryptedKDM dkdm,
337 boost::filesystem::path output,
338 dcp::NameFormat container_name_format,
339 dcp::NameFormat filename_format,
340 boost::posix_time::ptime valid_from,
341 boost::posix_time::ptime valid_to,
342 dcp::Formulation formulation,
343 bool disable_forensic_marking_picture,
344 optional<int> disable_forensic_marking_audio,
348 dcp::NameFormat::Map values;
351 list<KDMWithMetadataPtr> kdms;
352 BOOST_FOREACH (shared_ptr<Screen> i, screens) {
357 dcp::LocalTime begin(valid_from, i->cinema->utc_offset_hour(), i->cinema->utc_offset_minute());
358 dcp::LocalTime end(valid_to, i->cinema->utc_offset_hour(), i->cinema->utc_offset_minute());
360 dcp::EncryptedKDM const kdm = kdm_from_dkdm(
363 i->trusted_device_thumbprints(),
367 disable_forensic_marking_picture,
368 disable_forensic_marking_audio
371 dcp::NameFormat::Map name_values;
372 name_values['c'] = i->cinema->name;
373 name_values['s'] = i->name;
374 name_values['f'] = dkdm.annotation_text().get_value_or("");
375 name_values['b'] = begin.date() + " " + begin.time_of_day(true, false);
376 name_values['e'] = end.date() + " " + end.time_of_day(true, false);
377 name_values['i'] = kdm.cpl_id();
379 kdms.push_back (KDMWithMetadataPtr(new DCPKDMWithMetadata(name_values, i->cinema, kdm)));
381 write_files (kdms, zip, output, container_name_format, filename_format, verbose);
382 } catch (FileError& e) {
383 cerr << program_name << ": " << e.what() << " (" << e.file().string() << ")\n";
385 } catch (KDMError& e) {
386 cerr << program_name << ": " << e.what() << "\n";
392 dump_dkdm_group (shared_ptr<DKDMGroup> group, int indent)
395 for (int i = 0; i < indent; ++i) {
398 cout << group->name() << "\n";
400 BOOST_FOREACH (shared_ptr<DKDMBase> i, group->children()) {
401 shared_ptr<DKDMGroup> g = dynamic_pointer_cast<DKDMGroup>(i);
403 dump_dkdm_group (g, indent + 2);
405 for (int j = 0; j < indent; ++j) {
408 shared_ptr<DKDM> d = dynamic_pointer_cast<DKDM>(i);
410 cout << d->dkdm().cpl_id() << "\n";
415 int main (int argc, char* argv[])
417 boost::filesystem::path output = ".";
418 dcp::NameFormat container_name_format = Config::instance()->kdm_container_name_format();
419 dcp::NameFormat filename_format = Config::instance()->kdm_filename_format();
420 optional<string> cinema_name;
421 shared_ptr<Cinema> cinema;
422 string screen_description = "";
423 list<shared_ptr<Screen> > screens;
424 optional<dcp::EncryptedKDM> dkdm;
425 optional<boost::posix_time::ptime> valid_from;
426 optional<boost::posix_time::ptime> valid_to;
428 bool list_cinemas = false;
429 bool list_dkdm_cpls = false;
430 optional<string> duration_string;
431 bool verbose = false;
432 dcp::Formulation formulation = dcp::MODIFIED_TRANSITIONAL_1;
433 bool disable_forensic_marking_picture = false;
434 optional<int> disable_forensic_marking_audio;
436 program_name = argv[0];
438 int option_index = 0;
440 static struct option long_options[] = {
441 { "help", no_argument, 0, 'h'},
442 { "output", required_argument, 0, 'o'},
443 { "filename-format", required_argument, 0, 'K'},
444 { "container-name-format", required_argument, 0, 'Z'},
445 { "valid-from", required_argument, 0, 'f'},
446 { "valid-to", required_argument, 0, 't'},
447 { "valid-duration", required_argument, 0, 'd'},
448 { "formulation", required_argument, 0, 'F' },
449 { "disable-forensic-marking-picture", no_argument, 0, 'p' },
450 { "disable-forensic-marking-audio", optional_argument, 0, 'a' },
451 { "zip", no_argument, 0, 'z' },
452 { "verbose", no_argument, 0, 'v' },
453 { "cinema", required_argument, 0, 'c' },
454 { "screen", required_argument, 0, 'S' },
455 { "certificate", required_argument, 0, 'C' },
456 { "trusted-device", required_argument, 0, 'T' },
457 { "list-cinemas", no_argument, 0, 'B' },
458 { "list-dkdm-cpls", no_argument, 0, 'D' },
462 int c = getopt_long (argc, argv, "ho:K:Z:f:t:d:F:pa::zvc:S:C:T:BD", long_options, &option_index);
476 filename_format = dcp::NameFormat (optarg);
479 container_name_format = dcp::NameFormat (optarg);
482 valid_from = time_from_string (optarg);
485 valid_to = time_from_string (optarg);
488 duration_string = optarg;
491 if (string (optarg) == "modified-transitional-1") {
492 formulation = dcp::MODIFIED_TRANSITIONAL_1;
493 } else if (string (optarg) == "multiple-modified-transitional-1") {
494 formulation = dcp::MULTIPLE_MODIFIED_TRANSITIONAL_1;
495 } else if (string (optarg) == "dci-any") {
496 formulation = dcp::DCI_ANY;
497 } else if (string (optarg) == "dci-specific") {
498 formulation = dcp::DCI_SPECIFIC;
500 error ("unrecognised KDM formulation " + string (optarg));
504 disable_forensic_marking_picture = true;
507 disable_forensic_marking_audio = 0;
508 if (optarg == 0 && argv[optind] != 0 && argv[optind][0] != '-') {
509 disable_forensic_marking_audio = atoi (argv[optind++]);
511 disable_forensic_marking_audio = atoi (optarg);
521 /* This could be a cinema to search for in the configured list or the name of a cinema being
522 built up on-the-fly in the option. Cater for both possilibities here by storing the name
523 (for lookup) and by creating a Cinema which the next Screen will be added to.
525 cinema_name = optarg;
526 cinema = shared_ptr<Cinema> (new Cinema (optarg, list<string>(), "", 0, 0));
529 screen_description = optarg;
533 /* Make a new screen and add it to the current cinema */
534 dcp::CertificateChain chain (dcp::file_to_string(optarg));
535 shared_ptr<Screen> screen (new Screen (screen_description, "", chain.leaf(), vector<TrustedDevice>()));
537 cinema->add_screen (screen);
539 screens.push_back (screen);
543 /* A trusted device ends up in the last screen we made */
544 if (!screens.empty ()) {
545 screens.back()->trusted_devices.push_back(TrustedDevice(dcp::Certificate(dcp::file_to_string(optarg))));
552 list_dkdm_cpls = true;
558 list<boost::shared_ptr<Cinema> > cinemas = Config::instance()->cinemas ();
559 for (list<boost::shared_ptr<Cinema> >::const_iterator i = cinemas.begin(); i != cinemas.end(); ++i) {
560 cout << (*i)->name << " (" << Emailer::address_list ((*i)->emails) << ")\n";
565 if (list_dkdm_cpls) {
566 dump_dkdm_group (Config::instance()->dkdms(), 0);
570 if (!duration_string && !valid_to) {
571 error ("you must specify a --valid-duration or --valid-to");
575 error ("you must specify --valid-from");
579 if (optind >= argc) {
584 if (screens.empty()) {
586 error ("you must specify either a cinema or one or more screens using certificate files");
589 screens = find_cinema (*cinema_name)->screens ();
592 if (duration_string) {
593 valid_to = valid_from.get() + duration_from_string (*duration_string);
596 dcpomatic_setup_path_encoding ();
600 cout << "Making KDMs valid from " << valid_from.get() << " to " << valid_to.get() << "\n";
603 string const thing = argv[optind];
604 if (boost::filesystem::is_directory(thing) && boost::filesystem::is_regular_file(boost::filesystem::path(thing) / "metadata.xml")) {
610 container_name_format,
615 disable_forensic_marking_picture,
616 disable_forensic_marking_audio,
620 if (boost::filesystem::is_regular_file(thing)) {
621 dkdm = dcp::EncryptedKDM (dcp::file_to_string (thing));
623 dkdm = find_dkdm (thing);
627 error ("could not find film or CPL ID corresponding to " + thing);
632 dcp::DecryptedKDM (*dkdm, Config::instance()->decryption_chain()->key().get()),
635 container_name_format,
640 disable_forensic_marking_picture,
641 disable_forensic_marking_audio,