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/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 std::runtime_error;
46 using boost::shared_ptr;
47 using boost::optional;
49 using boost::dynamic_pointer_cast;
50 using namespace dcpomatic;
55 cerr << "Syntax: " << program_name << " [OPTION] <FILM|CPL-ID|DKDM>\n"
56 " -h, --help show this help\n"
57 " -o, --output output file or directory\n"
58 " -K, --filename-format filename format for KDMs\n"
59 " -Z, --container-name-format filename format for ZIP containers\n"
60 " -f, --valid-from valid from time (in local time zone of the cinema) (e.g. \"2013-09-28 01:41:51\") or \"now\"\n"
61 " -t, --valid-to valid to time (in local time zone of the cinema) (e.g. \"2014-09-28 01:41:51\")\n"
62 " -d, --valid-duration valid duration (e.g. \"1 day\", \"4 hours\", \"2 weeks\")\n"
63 " -F, --formulation modified-transitional-1, multiple-modified-transitional-1, dci-any or dci-specific [default modified-transitional-1]\n"
64 " -a, --disable-forensic-marking-picture disable forensic marking of pictures essences\n"
65 " -a, --disable-forensic-marking-audio disable forensic marking of audio essences (optionally above a given channel, e.g 12)\n"
66 " -z, --zip ZIP each cinema's KDMs into its own file\n"
67 " -v, --verbose be verbose\n"
68 " -c, --cinema specify a cinema, either by name or email address\n"
69 " -S, --screen screen description\n"
70 " -C, --certificate file containing projector certificate\n"
71 " -T, --trusted-device file containing a trusted device's certificate\n"
72 " --list-cinemas list known cinemas from the DCP-o-matic settings\n"
73 " --list-dkdm-cpls list CPLs for which DCP-o-matic has DKDMs\n\n"
74 "CPL-ID must be the ID of a CPL that is mentioned in DCP-o-matic's DKDM list.\n\n"
76 "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"
77 "(Fred's Cinema must have been set up in DCP-o-matic's KDM window)\n\n"
78 "\t" << program_name << " -c \"Fred's Cinema\" -f now -d \"2 weeks\" -z my_great_movie\n\n";
84 cerr << program_name << ": " << m << "\n";
88 static boost::posix_time::ptime
89 time_from_string (string t)
92 return boost::posix_time::second_clock::local_time ();
95 return boost::posix_time::time_from_string (t);
98 static boost::posix_time::time_duration
99 duration_from_string (string d)
102 char unit_buf[64] = "\0";
103 sscanf (d.c_str(), "%d %63s", &N, unit_buf);
104 string const unit (unit_buf);
107 cerr << "Could not understand duration \"" << d << "\"\n";
111 if (unit == "year" || unit == "years") {
112 return boost::posix_time::time_duration (N * 24 * 365, 0, 0, 0);
113 } else if (unit == "week" || unit == "weeks") {
114 return boost::posix_time::time_duration (N * 24 * 7, 0, 0, 0);
115 } else if (unit == "day" || unit == "days") {
116 return boost::posix_time::time_duration (N * 24, 0, 0, 0);
117 } else if (unit == "hour" || unit == "hours") {
118 return boost::posix_time::time_duration (N, 0, 0, 0);
121 cerr << "Could not understand duration \"" << d << "\"\n";
133 list<shared_ptr<ScreenKDM> > screen_kdms,
135 boost::filesystem::path output,
136 dcp::NameFormat container_name_format,
137 dcp::NameFormat filename_format,
138 dcp::NameFormat::Map values,
143 int const N = CinemaKDMs::write_zip_files (
144 CinemaKDMs::collect (screen_kdms),
146 container_name_format,
149 bind (&always_overwrite)
153 cout << "Wrote " << N << " ZIP files to " << output << "\n";
156 int const N = ScreenKDM::write_files (
157 screen_kdms, output, filename_format, values,
158 bind (&always_overwrite)
162 cout << "Wrote " << N << " KDM files to " << output << "\n";
168 find_cinema (string cinema_name)
170 list<shared_ptr<Cinema> > cinemas = Config::instance()->cinemas ();
171 list<shared_ptr<Cinema> >::const_iterator i = cinemas.begin();
173 i != cinemas.end() &&
174 (*i)->name != cinema_name &&
175 find ((*i)->emails.begin(), (*i)->emails.end(), cinema_name) == (*i)->emails.end()) {
180 if (i == cinemas.end ()) {
181 cerr << program_name << ": could not find cinema \"" << cinema_name << "\"\n";
190 list<shared_ptr<Screen> > screens,
191 boost::filesystem::path film_dir,
193 boost::filesystem::path output,
194 dcp::NameFormat container_name_format,
195 dcp::NameFormat filename_format,
196 boost::posix_time::ptime valid_from,
197 boost::posix_time::ptime valid_to,
198 dcp::Formulation formulation,
199 bool disable_forensic_marking_picture,
200 optional<int> disable_forensic_marking_audio,
204 shared_ptr<Film> film;
206 film.reset (new Film (film_dir));
207 film->read_metadata ();
209 cout << "Read film " << film->name () << "\n";
211 } catch (std::exception& e) {
212 cerr << program_name << ": error reading film `" << film_dir.string() << "' (" << e.what() << ")\n";
216 /* XXX: allow specification of this */
217 vector<CPLSummary> cpls = film->cpls ();
219 error ("no CPLs found in film");
220 } else if (cpls.size() > 1) {
221 error ("more than one CPL found in film");
224 boost::filesystem::path cpl = cpls.front().cpl_file;
226 dcp::NameFormat::Map values;
227 values['f'] = film->name();
228 values['b'] = dcp::LocalTime(valid_from).date() + " " + dcp::LocalTime(valid_from).time_of_day(true, false);
229 values['e'] = dcp::LocalTime(valid_to).date() + " " + dcp::LocalTime(valid_to).time_of_day(true, false);
232 list<shared_ptr<ScreenKDM> > screen_kdms = film->make_kdms (
233 screens, cpl, valid_from, valid_to, formulation, disable_forensic_marking_picture, disable_forensic_marking_audio
236 write_files (screen_kdms, zip, output, container_name_format, filename_format, values, verbose);
237 } catch (FileError& e) {
238 cerr << program_name << ": " << e.what() << " (" << e.file().string() << ")\n";
240 } catch (KDMError& e) {
241 cerr << program_name << ": " << e.what() << "\n";
243 } catch (runtime_error& e) {
244 cerr << program_name << ": " << e.what() << "\n";
249 optional<dcp::EncryptedKDM>
250 sub_find_dkdm (shared_ptr<DKDMGroup> group, string cpl_id)
252 BOOST_FOREACH (shared_ptr<DKDMBase> i, group->children()) {
253 shared_ptr<DKDMGroup> g = dynamic_pointer_cast<DKDMGroup>(i);
255 optional<dcp::EncryptedKDM> dkdm = sub_find_dkdm (g, cpl_id);
260 shared_ptr<DKDM> d = dynamic_pointer_cast<DKDM>(i);
262 if (d->dkdm().cpl_id() == cpl_id) {
268 return optional<dcp::EncryptedKDM>();
271 optional<dcp::EncryptedKDM>
272 find_dkdm (string cpl_id)
274 return sub_find_dkdm (Config::instance()->dkdms(), cpl_id);
279 dcp::DecryptedKDM dkdm,
280 dcp::Certificate target,
281 vector<string> trusted_devices,
282 dcp::LocalTime valid_from,
283 dcp::LocalTime valid_to,
284 dcp::Formulation formulation,
285 bool disable_forensic_marking_picture,
286 optional<int> disable_forensic_marking_audio
289 /* Signer for new KDM */
290 shared_ptr<const dcp::CertificateChain> signer = Config::instance()->signer_chain ();
291 if (!signer->valid ()) {
292 error ("signing certificate chain is invalid.");
295 /* Make a new empty KDM and add the keys from the DKDM to it */
296 dcp::DecryptedKDM kdm (
299 dkdm.annotation_text().get_value_or(""),
300 dkdm.content_title_text(),
301 dcp::LocalTime().as_string()
304 BOOST_FOREACH (dcp::DecryptedKDMKey const & j, dkdm.keys()) {
308 return kdm.encrypt (signer, target, trusted_devices, formulation, disable_forensic_marking_picture, disable_forensic_marking_audio);
313 list<shared_ptr<Screen> > screens,
314 dcp::DecryptedKDM dkdm,
316 boost::filesystem::path output,
317 dcp::NameFormat container_name_format,
318 dcp::NameFormat filename_format,
319 boost::posix_time::ptime valid_from,
320 boost::posix_time::ptime valid_to,
321 dcp::Formulation formulation,
322 bool disable_forensic_marking_picture,
323 optional<int> disable_forensic_marking_audio,
327 dcp::NameFormat::Map values;
328 values['f'] = dkdm.annotation_text().get_value_or("");
329 values['b'] = dcp::LocalTime(valid_from).date() + " " + dcp::LocalTime(valid_from).time_of_day(true, false);
330 values['e'] = dcp::LocalTime(valid_to).date() + " " + dcp::LocalTime(valid_to).time_of_day(true, false);
333 list<shared_ptr<ScreenKDM> > screen_kdms;
334 BOOST_FOREACH (shared_ptr<Screen> i, screens) {
339 screen_kdms.push_back (
340 shared_ptr<ScreenKDM>(
346 i->trusted_device_thumbprints(),
347 dcp::LocalTime(valid_from, i->cinema->utc_offset_hour(), i->cinema->utc_offset_minute()),
348 dcp::LocalTime(valid_to, i->cinema->utc_offset_hour(), i->cinema->utc_offset_minute()),
350 disable_forensic_marking_picture,
351 disable_forensic_marking_audio
357 write_files (screen_kdms, zip, output, container_name_format, filename_format, values, verbose);
358 } catch (FileError& e) {
359 cerr << program_name << ": " << e.what() << " (" << e.file().string() << ")\n";
361 } catch (KDMError& e) {
362 cerr << program_name << ": " << e.what() << "\n";
368 dump_dkdm_group (shared_ptr<DKDMGroup> group, int indent)
371 for (int i = 0; i < indent; ++i) {
374 cout << group->name() << "\n";
376 BOOST_FOREACH (shared_ptr<DKDMBase> i, group->children()) {
377 shared_ptr<DKDMGroup> g = dynamic_pointer_cast<DKDMGroup>(i);
379 dump_dkdm_group (g, indent + 2);
381 for (int j = 0; j < indent; ++j) {
384 shared_ptr<DKDM> d = dynamic_pointer_cast<DKDM>(i);
386 cout << d->dkdm().cpl_id() << "\n";
391 int main (int argc, char* argv[])
393 boost::filesystem::path output = ".";
394 dcp::NameFormat container_name_format = Config::instance()->kdm_container_name_format();
395 dcp::NameFormat filename_format = Config::instance()->kdm_filename_format();
396 optional<string> cinema_name;
397 shared_ptr<Cinema> cinema;
398 string screen_description = "";
399 list<shared_ptr<Screen> > screens;
400 optional<dcp::EncryptedKDM> dkdm;
401 optional<boost::posix_time::ptime> valid_from;
402 optional<boost::posix_time::ptime> valid_to;
404 bool list_cinemas = false;
405 bool list_dkdm_cpls = false;
406 optional<string> duration_string;
407 bool verbose = false;
408 dcp::Formulation formulation = dcp::MODIFIED_TRANSITIONAL_1;
409 bool disable_forensic_marking_picture = false;
410 optional<int> disable_forensic_marking_audio;
412 program_name = argv[0];
414 int option_index = 0;
416 static struct option long_options[] = {
417 { "help", no_argument, 0, 'h'},
418 { "output", required_argument, 0, 'o'},
419 { "filename-format", required_argument, 0, 'K'},
420 { "container-name-format", required_argument, 0, 'Z'},
421 { "valid-from", required_argument, 0, 'f'},
422 { "valid-to", required_argument, 0, 't'},
423 { "valid-duration", required_argument, 0, 'd'},
424 { "formulation", required_argument, 0, 'F' },
425 { "disable-forensic-marking-picture", no_argument, 0, 'p' },
426 { "disable-forensic-marking-audio", optional_argument, 0, 'a' },
427 { "zip", no_argument, 0, 'z' },
428 { "verbose", no_argument, 0, 'v' },
429 { "cinema", required_argument, 0, 'c' },
430 { "screen", required_argument, 0, 'S' },
431 { "certificate", required_argument, 0, 'C' },
432 { "trusted-device", required_argument, 0, 'T' },
433 { "list-cinemas", no_argument, 0, 'B' },
434 { "list-dkdm-cpls", no_argument, 0, 'D' },
438 int c = getopt_long (argc, argv, "ho:K:Z:f:t:d:F:pa::zvc:S:C:T:BD", long_options, &option_index);
452 filename_format = dcp::NameFormat (optarg);
455 container_name_format = dcp::NameFormat (optarg);
458 valid_from = time_from_string (optarg);
461 valid_to = time_from_string (optarg);
464 duration_string = optarg;
467 if (string (optarg) == "modified-transitional-1") {
468 formulation = dcp::MODIFIED_TRANSITIONAL_1;
469 } else if (string (optarg) == "multiple-modified-transitional-1") {
470 formulation = dcp::MULTIPLE_MODIFIED_TRANSITIONAL_1;
471 } else if (string (optarg) == "dci-any") {
472 formulation = dcp::DCI_ANY;
473 } else if (string (optarg) == "dci-specific") {
474 formulation = dcp::DCI_SPECIFIC;
476 error ("unrecognised KDM formulation " + string (optarg));
480 disable_forensic_marking_picture = true;
483 disable_forensic_marking_audio = 0;
484 if (optarg == 0 && argv[optind] != 0 && argv[optind][0] != '-') {
485 disable_forensic_marking_audio = atoi (argv[optind++]);
487 disable_forensic_marking_audio = atoi (optarg);
497 /* This could be a cinema to search for in the configured list or the name of a cinema being
498 built up on-the-fly in the option. Cater for both possilibities here by storing the name
499 (for lookup) and by creating a Cinema which the next Screen will be added to.
501 cinema_name = optarg;
502 cinema = shared_ptr<Cinema> (new Cinema (optarg, list<string>(), "", 0, 0));
505 screen_description = optarg;
509 /* Make a new screen and add it to the current cinema */
510 dcp::CertificateChain chain (dcp::file_to_string(optarg));
511 shared_ptr<Screen> screen (new Screen (screen_description, chain.leaf(), vector<TrustedDevice>()));
513 cinema->add_screen (screen);
515 screens.push_back (screen);
519 /* A trusted device ends up in the last screen we made */
520 if (!screens.empty ()) {
521 screens.back()->trusted_devices.push_back(TrustedDevice(dcp::Certificate(dcp::file_to_string(optarg))));
528 list_dkdm_cpls = true;
534 list<boost::shared_ptr<Cinema> > cinemas = Config::instance()->cinemas ();
535 for (list<boost::shared_ptr<Cinema> >::const_iterator i = cinemas.begin(); i != cinemas.end(); ++i) {
536 cout << (*i)->name << " (" << Emailer::address_list ((*i)->emails) << ")\n";
541 if (list_dkdm_cpls) {
542 dump_dkdm_group (Config::instance()->dkdms(), 0);
546 if (!duration_string && !valid_to) {
547 error ("you must specify a --valid-duration or --valid-to");
551 error ("you must specify --valid-from");
555 if (optind >= argc) {
560 if (screens.empty()) {
562 error ("you must specify either a cinema or one or more screens using certificate files");
565 screens = find_cinema (*cinema_name)->screens ();
568 if (duration_string) {
569 valid_to = valid_from.get() + duration_from_string (*duration_string);
572 dcpomatic_setup_path_encoding ();
576 cout << "Making KDMs valid from " << valid_from.get() << " to " << valid_to.get() << "\n";
579 string const thing = argv[optind];
580 if (boost::filesystem::is_directory(thing) && boost::filesystem::is_regular_file(boost::filesystem::path(thing) / "metadata.xml")) {
586 container_name_format,
591 disable_forensic_marking_picture,
592 disable_forensic_marking_audio,
596 if (boost::filesystem::is_regular_file(thing)) {
597 dkdm = dcp::EncryptedKDM (dcp::file_to_string (thing));
599 dkdm = find_dkdm (thing);
603 error ("could not find film or CPL ID corresponding to " + thing);
608 dcp::DecryptedKDM (*dkdm, Config::instance()->decryption_chain()->key().get()),
611 container_name_format,
616 disable_forensic_marking_picture,
617 disable_forensic_marking_audio,