2 Copyright (C) 2013-2021 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/>.
22 /** @file src/tools/dcpomatic_kdm_cli.cc
23 * @brief Command-line program to generate KDMs.
27 #include "lib/cinema.h"
28 #include "lib/config.h"
29 #include "lib/dkdm_wrapper.h"
30 #include "lib/emailer.h"
31 #include "lib/exceptions.h"
33 #include "lib/kdm_with_metadata.h"
34 #include "lib/screen.h"
35 #include <dcp/certificate.h>
36 #include <dcp/decrypted_kdm.h>
37 #include <dcp/encrypted_kdm.h>
44 using std::dynamic_pointer_cast;
46 using std::make_shared;
47 using std::runtime_error;
48 using std::shared_ptr;
51 using boost::optional;
53 #if BOOST_VERSION >= 106100
54 using namespace boost::placeholders;
56 using namespace dcpomatic;
62 cerr << "Syntax: " << program_name << " [OPTION] <FILM|CPL-ID|DKDM>\n"
63 " -h, --help show this help\n"
64 " -o, --output output file or directory\n"
65 " -K, --filename-format filename format for KDMs\n"
66 " -Z, --container-name-format filename format for ZIP containers\n"
67 " -f, --valid-from valid from time (in local time zone of the cinema) (e.g. \"2013-09-28 01:41:51\") or \"now\"\n"
68 " -t, --valid-to valid to time (in local time zone of the cinema) (e.g. \"2014-09-28 01:41:51\")\n"
69 " -d, --valid-duration valid duration (e.g. \"1 day\", \"4 hours\", \"2 weeks\")\n"
70 " -F, --formulation modified-transitional-1, multiple-modified-transitional-1, dci-any or dci-specific [default modified-transitional-1]\n"
71 " -a, --disable-forensic-marking-picture disable forensic marking of pictures essences\n"
72 " -a, --disable-forensic-marking-audio disable forensic marking of audio essences (optionally above a given channel, e.g 12)\n"
73 " -z, --zip ZIP each cinema's KDMs into its own file\n"
74 " -v, --verbose be verbose\n"
75 " -c, --cinema specify a cinema, either by name or email address\n"
76 " -S, --screen screen description\n"
77 " -C, --certificate file containing projector certificate\n"
78 " -T, --trusted-device file containing a trusted device's certificate\n"
79 " --list-cinemas list known cinemas from the DCP-o-matic settings\n"
80 " --list-dkdm-cpls list CPLs for which DCP-o-matic has DKDMs\n\n"
81 "CPL-ID must be the ID of a CPL that is mentioned in DCP-o-matic's DKDM list.\n\n"
83 "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"
84 "(Fred's Cinema must have been set up in DCP-o-matic's KDM window)\n\n"
85 "\t" << program_name << " -c \"Fred's Cinema\" -f now -d \"2 weeks\" -z my_great_movie\n\n";
92 cerr << program_name << ": " << m << "\n";
97 static boost::posix_time::ptime
98 time_from_string (string t)
101 return boost::posix_time::second_clock::local_time ();
104 return boost::posix_time::time_from_string (t);
108 static boost::posix_time::time_duration
109 duration_from_string (string d)
112 char unit_buf[64] = "\0";
113 sscanf (d.c_str(), "%d %63s", &N, unit_buf);
114 string const unit (unit_buf);
117 cerr << "Could not understand duration \"" << d << "\"\n";
121 if (unit == "year" || unit == "years") {
122 return boost::posix_time::time_duration (N * 24 * 365, 0, 0, 0);
123 } else if (unit == "week" || unit == "weeks") {
124 return boost::posix_time::time_duration (N * 24 * 7, 0, 0, 0);
125 } else if (unit == "day" || unit == "days") {
126 return boost::posix_time::time_duration (N * 24, 0, 0, 0);
127 } else if (unit == "hour" || unit == "hours") {
128 return boost::posix_time::time_duration (N, 0, 0, 0);
131 cerr << "Could not understand duration \"" << d << "\"\n";
145 list<KDMWithMetadataPtr> kdms,
147 boost::filesystem::path output,
148 dcp::NameFormat container_name_format,
149 dcp::NameFormat filename_format,
154 int const N = write_zip_files (
157 container_name_format,
159 bind (&always_overwrite)
163 cout << "Wrote " << N << " ZIP files to " << output << "\n";
166 int const N = write_files (
167 kdms, output, filename_format,
168 bind (&always_overwrite)
172 cout << "Wrote " << N << " KDM files to " << output << "\n";
179 find_cinema (string cinema_name)
181 auto cinemas = Config::instance()->cinemas ();
182 auto i = cinemas.begin();
184 i != cinemas.end() &&
185 (*i)->name != cinema_name &&
186 find ((*i)->emails.begin(), (*i)->emails.end(), cinema_name) == (*i)->emails.end()) {
191 if (i == cinemas.end ()) {
192 cerr << program_name << ": could not find cinema \"" << cinema_name << "\"\n";
202 list<shared_ptr<Screen>> screens,
203 boost::filesystem::path film_dir,
205 boost::filesystem::path output,
206 dcp::NameFormat container_name_format,
207 dcp::NameFormat filename_format,
208 boost::posix_time::ptime valid_from,
209 boost::posix_time::ptime valid_to,
210 dcp::Formulation formulation,
211 bool disable_forensic_marking_picture,
212 optional<int> disable_forensic_marking_audio,
216 shared_ptr<Film> film;
218 film = make_shared<Film>(film_dir);
219 film->read_metadata ();
221 cout << "Read film " << film->name () << "\n";
223 } catch (std::exception& e) {
224 cerr << program_name << ": error reading film `" << film_dir.string() << "' (" << e.what() << ")\n";
228 /* XXX: allow specification of this */
229 vector<CPLSummary> cpls = film->cpls ();
231 error ("no CPLs found in film");
232 } else if (cpls.size() > 1) {
233 error ("more than one CPL found in film");
236 auto cpl = cpls.front().cpl_file;
239 list<KDMWithMetadataPtr> kdms;
240 for (auto i: screens) {
241 auto p = kdm_for_screen (film, cpl, i, valid_from, valid_to, formulation, disable_forensic_marking_picture, disable_forensic_marking_audio);
246 write_files (kdms, zip, output, container_name_format, filename_format, verbose);
247 } catch (FileError& e) {
248 cerr << program_name << ": " << e.what() << " (" << e.file().string() << ")\n";
250 } catch (KDMError& e) {
251 cerr << program_name << ": " << e.what() << "\n";
253 } catch (runtime_error& e) {
254 cerr << program_name << ": " << e.what() << "\n";
260 optional<dcp::EncryptedKDM>
261 sub_find_dkdm (shared_ptr<DKDMGroup> group, string cpl_id)
263 for (auto i: group->children()) {
264 auto g = dynamic_pointer_cast<DKDMGroup>(i);
266 auto dkdm = sub_find_dkdm (g, cpl_id);
271 auto d = dynamic_pointer_cast<DKDM>(i);
273 if (d->dkdm().cpl_id() == cpl_id) {
279 return optional<dcp::EncryptedKDM>();
283 optional<dcp::EncryptedKDM>
284 find_dkdm (string cpl_id)
286 return sub_find_dkdm (Config::instance()->dkdms(), cpl_id);
292 dcp::DecryptedKDM dkdm,
293 dcp::Certificate target,
294 vector<string> trusted_devices,
295 dcp::LocalTime valid_from,
296 dcp::LocalTime valid_to,
297 dcp::Formulation formulation,
298 bool disable_forensic_marking_picture,
299 optional<int> disable_forensic_marking_audio
302 /* Signer for new KDM */
303 auto signer = Config::instance()->signer_chain ();
304 if (!signer->valid ()) {
305 error ("signing certificate chain is invalid.");
308 /* Make a new empty KDM and add the keys from the DKDM to it */
309 dcp::DecryptedKDM kdm (
312 dkdm.annotation_text().get_value_or(""),
313 dkdm.content_title_text(),
314 dcp::LocalTime().as_string()
317 for (auto const& j: dkdm.keys()) {
321 return kdm.encrypt (signer, target, trusted_devices, formulation, disable_forensic_marking_picture, disable_forensic_marking_audio);
327 list<shared_ptr<Screen>> screens,
328 dcp::DecryptedKDM dkdm,
330 boost::filesystem::path output,
331 dcp::NameFormat container_name_format,
332 dcp::NameFormat filename_format,
333 boost::posix_time::ptime valid_from,
334 boost::posix_time::ptime valid_to,
335 dcp::Formulation formulation,
336 bool disable_forensic_marking_picture,
337 optional<int> disable_forensic_marking_audio,
341 dcp::NameFormat::Map values;
344 list<KDMWithMetadataPtr> kdms;
345 for (auto i: screens) {
350 dcp::LocalTime begin(valid_from, i->cinema->utc_offset_hour(), i->cinema->utc_offset_minute());
351 dcp::LocalTime end(valid_to, i->cinema->utc_offset_hour(), i->cinema->utc_offset_minute());
353 auto const kdm = kdm_from_dkdm(
356 i->trusted_device_thumbprints(),
360 disable_forensic_marking_picture,
361 disable_forensic_marking_audio
364 dcp::NameFormat::Map name_values;
365 name_values['c'] = i->cinema->name;
366 name_values['s'] = i->name;
367 name_values['f'] = dkdm.annotation_text().get_value_or("");
368 name_values['b'] = begin.date() + " " + begin.time_of_day(true, false);
369 name_values['e'] = end.date() + " " + end.time_of_day(true, false);
370 name_values['i'] = kdm.cpl_id();
372 kdms.push_back (make_shared<KDMWithMetadata>(name_values, i->cinema.get(), i->cinema->emails, kdm));
374 write_files (kdms, zip, output, container_name_format, filename_format, verbose);
375 } catch (FileError& e) {
376 cerr << program_name << ": " << e.what() << " (" << e.file().string() << ")\n";
378 } catch (KDMError& e) {
379 cerr << program_name << ": " << e.what() << "\n";
386 dump_dkdm_group (shared_ptr<DKDMGroup> group, int indent)
389 for (int i = 0; i < indent; ++i) {
392 cout << group->name() << "\n";
394 for (auto i: group->children()) {
395 auto g = dynamic_pointer_cast<DKDMGroup>(i);
397 dump_dkdm_group (g, indent + 2);
399 for (int j = 0; j < indent; ++j) {
402 auto d = dynamic_pointer_cast<DKDM>(i);
404 cout << d->dkdm().cpl_id() << "\n";
410 int main (int argc, char* argv[])
412 boost::filesystem::path output = ".";
413 auto container_name_format = Config::instance()->kdm_container_name_format();
414 auto filename_format = Config::instance()->kdm_filename_format();
415 optional<string> cinema_name;
416 shared_ptr<Cinema> cinema;
417 string screen_description;
418 list<shared_ptr<Screen>> screens;
419 optional<dcp::EncryptedKDM> dkdm;
420 optional<boost::posix_time::ptime> valid_from;
421 optional<boost::posix_time::ptime> valid_to;
423 bool list_cinemas = false;
424 bool list_dkdm_cpls = false;
425 optional<string> duration_string;
426 bool verbose = false;
427 dcp::Formulation formulation = dcp::Formulation::MODIFIED_TRANSITIONAL_1;
428 bool disable_forensic_marking_picture = false;
429 optional<int> disable_forensic_marking_audio;
431 program_name = argv[0];
433 int option_index = 0;
435 static struct option long_options[] = {
436 { "help", no_argument, 0, 'h'},
437 { "output", required_argument, 0, 'o'},
438 { "filename-format", required_argument, 0, 'K'},
439 { "container-name-format", required_argument, 0, 'Z'},
440 { "valid-from", required_argument, 0, 'f'},
441 { "valid-to", required_argument, 0, 't'},
442 { "valid-duration", required_argument, 0, 'd'},
443 { "formulation", required_argument, 0, 'F' },
444 { "disable-forensic-marking-picture", no_argument, 0, 'p' },
445 { "disable-forensic-marking-audio", optional_argument, 0, 'a' },
446 { "zip", no_argument, 0, 'z' },
447 { "verbose", no_argument, 0, 'v' },
448 { "cinema", required_argument, 0, 'c' },
449 { "screen", required_argument, 0, 'S' },
450 { "certificate", required_argument, 0, 'C' },
451 { "trusted-device", required_argument, 0, 'T' },
452 { "list-cinemas", no_argument, 0, 'B' },
453 { "list-dkdm-cpls", no_argument, 0, 'D' },
457 int c = getopt_long (argc, argv, "ho:K:Z:f:t:d:F:pa::zvc:S:C:T:BD", long_options, &option_index);
471 filename_format = dcp::NameFormat (optarg);
474 container_name_format = dcp::NameFormat (optarg);
477 valid_from = time_from_string (optarg);
480 valid_to = time_from_string (optarg);
483 duration_string = optarg;
486 if (string(optarg) == "modified-transitional-1") {
487 formulation = dcp::Formulation::MODIFIED_TRANSITIONAL_1;
488 } else if (string(optarg) == "multiple-modified-transitional-1") {
489 formulation = dcp::Formulation::MULTIPLE_MODIFIED_TRANSITIONAL_1;
490 } else if (string(optarg) == "dci-any") {
491 formulation = dcp::Formulation::DCI_ANY;
492 } else if (string(optarg) == "dci-specific") {
493 formulation = dcp::Formulation::DCI_SPECIFIC;
495 error ("unrecognised KDM formulation " + string (optarg));
499 disable_forensic_marking_picture = true;
502 disable_forensic_marking_audio = 0;
503 if (optarg == 0 && argv[optind] != 0 && argv[optind][0] != '-') {
504 disable_forensic_marking_audio = atoi (argv[optind++]);
506 disable_forensic_marking_audio = atoi (optarg);
516 /* This could be a cinema to search for in the configured list or the name of a cinema being
517 built up on-the-fly in the option. Cater for both possilibities here by storing the name
518 (for lookup) and by creating a Cinema which the next Screen will be added to.
520 cinema_name = optarg;
521 cinema = make_shared<Cinema>(optarg, list<string>(), "", 0, 0);
524 screen_description = optarg;
528 /* Make a new screen and add it to the current cinema */
529 dcp::CertificateChain chain (dcp::file_to_string(optarg));
530 auto screen = make_shared<Screen>(screen_description, "", chain.leaf(), vector<TrustedDevice>());
532 cinema->add_screen (screen);
534 screens.push_back (screen);
538 /* A trusted device ends up in the last screen we made */
539 if (!screens.empty ()) {
540 screens.back()->trusted_devices.push_back(TrustedDevice(dcp::Certificate(dcp::file_to_string(optarg))));
547 list_dkdm_cpls = true;
553 auto cinemas = Config::instance()->cinemas ();
554 for (auto i: cinemas) {
555 cout << i->name << " (" << Emailer::address_list (i->emails) << ")\n";
560 if (list_dkdm_cpls) {
561 dump_dkdm_group (Config::instance()->dkdms(), 0);
565 if (!duration_string && !valid_to) {
566 error ("you must specify a --valid-duration or --valid-to");
570 error ("you must specify --valid-from");
574 if (optind >= argc) {
579 if (screens.empty()) {
581 error ("you must specify either a cinema or one or more screens using certificate files");
584 screens = find_cinema (*cinema_name)->screens ();
587 if (duration_string) {
588 valid_to = valid_from.get() + duration_from_string (*duration_string);
591 dcpomatic_setup_path_encoding ();
595 cout << "Making KDMs valid from " << valid_from.get() << " to " << valid_to.get() << "\n";
598 string const thing = argv[optind];
599 if (boost::filesystem::is_directory(thing) && boost::filesystem::is_regular_file(boost::filesystem::path(thing) / "metadata.xml")) {
605 container_name_format,
610 disable_forensic_marking_picture,
611 disable_forensic_marking_audio,
615 if (boost::filesystem::is_regular_file(thing)) {
616 dkdm = dcp::EncryptedKDM (dcp::file_to_string (thing));
618 dkdm = find_dkdm (thing);
622 error ("could not find film or CPL ID corresponding to " + thing);
627 dcp::DecryptedKDM (*dkdm, Config::instance()->decryption_chain()->key().get()),
630 container_name_format,
635 disable_forensic_marking_picture,
636 disable_forensic_marking_audio,