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/screen_with_kdm.h"
27 #include "lib/config.h"
28 #include "lib/exceptions.h"
29 #include "lib/emailer.h"
30 #include "lib/dkdm_wrapper.h"
31 #include "lib/screen.h"
32 #include "lib/cinema.h"
33 #include "lib/cinema_kdms.h"
34 #include <dcp/certificate.h>
35 #include <dcp/decrypted_kdm.h>
36 #include <dcp/encrypted_kdm.h>
38 #include <boost/foreach.hpp>
46 using std::runtime_error;
47 using boost::shared_ptr;
48 using boost::optional;
50 using boost::dynamic_pointer_cast;
51 using namespace dcpomatic;
56 cerr << "Syntax: " << program_name << " [OPTION] <FILM|CPL-ID|DKDM>\n"
57 " -h, --help show this help\n"
58 " -o, --output output file or directory\n"
59 " -K, --filename-format filename format for KDMs\n"
60 " -Z, --container-name-format filename format for ZIP containers\n"
61 " -f, --valid-from valid from time (in local time zone of the cinema) (e.g. \"2013-09-28 01:41:51\") or \"now\"\n"
62 " -t, --valid-to valid to time (in local time zone of the cinema) (e.g. \"2014-09-28 01:41:51\")\n"
63 " -d, --valid-duration valid duration (e.g. \"1 day\", \"4 hours\", \"2 weeks\")\n"
64 " -F, --formulation modified-transitional-1, multiple-modified-transitional-1, dci-any or dci-specific [default modified-transitional-1]\n"
65 " -a, --disable-forensic-marking-picture disable forensic marking of pictures essences\n"
66 " -a, --disable-forensic-marking-audio disable forensic marking of audio essences (optionally above a given channel, e.g 12)\n"
67 " -z, --zip ZIP each cinema's KDMs into its own file\n"
68 " -v, --verbose be verbose\n"
69 " -c, --cinema specify a cinema, either by name or email address\n"
70 " -S, --screen screen description\n"
71 " -C, --certificate file containing projector certificate\n"
72 " -T, --trusted-device file containing a trusted device's certificate\n"
73 " --list-cinemas list known cinemas from the DCP-o-matic settings\n"
74 " --list-dkdm-cpls list CPLs for which DCP-o-matic has DKDMs\n\n"
75 "CPL-ID must be the ID of a CPL that is mentioned in DCP-o-matic's DKDM list.\n\n"
77 "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"
78 "(Fred's Cinema must have been set up in DCP-o-matic's KDM window)\n\n"
79 "\t" << program_name << " -c \"Fred's Cinema\" -f now -d \"2 weeks\" -z my_great_movie\n\n";
85 cerr << program_name << ": " << m << "\n";
89 static boost::posix_time::ptime
90 time_from_string (string t)
93 return boost::posix_time::second_clock::local_time ();
96 return boost::posix_time::time_from_string (t);
99 static boost::posix_time::time_duration
100 duration_from_string (string d)
103 char unit_buf[64] = "\0";
104 sscanf (d.c_str(), "%d %63s", &N, unit_buf);
105 string const unit (unit_buf);
108 cerr << "Could not understand duration \"" << d << "\"\n";
112 if (unit == "year" || unit == "years") {
113 return boost::posix_time::time_duration (N * 24 * 365, 0, 0, 0);
114 } else if (unit == "week" || unit == "weeks") {
115 return boost::posix_time::time_duration (N * 24 * 7, 0, 0, 0);
116 } else if (unit == "day" || unit == "days") {
117 return boost::posix_time::time_duration (N * 24, 0, 0, 0);
118 } else if (unit == "hour" || unit == "hours") {
119 return boost::posix_time::time_duration (N, 0, 0, 0);
122 cerr << "Could not understand duration \"" << d << "\"\n";
134 list<shared_ptr<ScreenWithKDM> > screen_kdms,
136 boost::filesystem::path output,
137 dcp::NameFormat container_name_format,
138 dcp::NameFormat filename_format,
139 dcp::NameFormat::Map values,
144 int const N = CinemaKDMs::write_zip_files (
145 CinemaKDMs::collect (screen_kdms),
147 container_name_format,
150 bind (&always_overwrite)
154 cout << "Wrote " << N << " ZIP files to " << output << "\n";
157 int const N = ScreenWithKDM::write_files (
158 screen_kdms, output, filename_format, values,
159 bind (&always_overwrite)
163 cout << "Wrote " << N << " KDM files to " << output << "\n";
169 find_cinema (string cinema_name)
171 list<shared_ptr<Cinema> > cinemas = Config::instance()->cinemas ();
172 list<shared_ptr<Cinema> >::const_iterator i = cinemas.begin();
174 i != cinemas.end() &&
175 (*i)->name != cinema_name &&
176 find ((*i)->emails.begin(), (*i)->emails.end(), cinema_name) == (*i)->emails.end()) {
181 if (i == cinemas.end ()) {
182 cerr << program_name << ": could not find cinema \"" << cinema_name << "\"\n";
191 list<shared_ptr<Screen> > screens,
192 boost::filesystem::path film_dir,
194 boost::filesystem::path output,
195 dcp::NameFormat container_name_format,
196 dcp::NameFormat filename_format,
197 boost::posix_time::ptime valid_from,
198 boost::posix_time::ptime valid_to,
199 dcp::Formulation formulation,
200 bool disable_forensic_marking_picture,
201 optional<int> disable_forensic_marking_audio,
205 shared_ptr<Film> film;
207 film.reset (new Film (film_dir));
208 film->read_metadata ();
210 cout << "Read film " << film->name () << "\n";
212 } catch (std::exception& e) {
213 cerr << program_name << ": error reading film `" << film_dir.string() << "' (" << e.what() << ")\n";
217 /* XXX: allow specification of this */
218 vector<CPLSummary> cpls = film->cpls ();
220 error ("no CPLs found in film");
221 } else if (cpls.size() > 1) {
222 error ("more than one CPL found in film");
225 boost::filesystem::path cpl = cpls.front().cpl_file;
227 dcp::NameFormat::Map values;
228 values['f'] = film->name();
229 values['b'] = dcp::LocalTime(valid_from).date() + " " + dcp::LocalTime(valid_from).time_of_day(true, false);
230 values['e'] = dcp::LocalTime(valid_to).date() + " " + dcp::LocalTime(valid_to).time_of_day(true, false);
233 list<shared_ptr<ScreenWithKDM> > kdms;
235 BOOST_FOREACH (shared_ptr<Screen> i, screens) {
237 dcp::EncryptedKDM const kdm = film->make_kdm (
239 i->trusted_device_thumbprints(),
241 dcp::LocalTime(valid_from, i->utc_offset_hour(), i->utc_offset_minute()),
242 dcp::LocalTime(valid_to, i->utc_offset_hour(), i->utc_offset_minute()),
244 disable_forensic_marking_picture,
245 disable_forensic_marking_audio
248 kdms.push_back (shared_ptr<ScreenWithKDM>(new DCPScreenWithKDM(i, kdm)));
252 write_files (kdms, zip, output, container_name_format, filename_format, values, verbose);
253 } catch (FileError& e) {
254 cerr << program_name << ": " << e.what() << " (" << e.file().string() << ")\n";
256 } catch (KDMError& e) {
257 cerr << program_name << ": " << e.what() << "\n";
259 } catch (runtime_error& e) {
260 cerr << program_name << ": " << e.what() << "\n";
265 optional<dcp::EncryptedKDM>
266 sub_find_dkdm (shared_ptr<DKDMGroup> group, string cpl_id)
268 BOOST_FOREACH (shared_ptr<DKDMBase> i, group->children()) {
269 shared_ptr<DKDMGroup> g = dynamic_pointer_cast<DKDMGroup>(i);
271 optional<dcp::EncryptedKDM> dkdm = sub_find_dkdm (g, cpl_id);
276 shared_ptr<DKDM> d = dynamic_pointer_cast<DKDM>(i);
278 if (d->dkdm().cpl_id() == cpl_id) {
284 return optional<dcp::EncryptedKDM>();
287 optional<dcp::EncryptedKDM>
288 find_dkdm (string cpl_id)
290 return sub_find_dkdm (Config::instance()->dkdms(), cpl_id);
295 dcp::DecryptedKDM dkdm,
296 dcp::Certificate target,
297 vector<string> trusted_devices,
298 dcp::LocalTime valid_from,
299 dcp::LocalTime valid_to,
300 dcp::Formulation formulation,
301 bool disable_forensic_marking_picture,
302 optional<int> disable_forensic_marking_audio
305 /* Signer for new KDM */
306 shared_ptr<const dcp::CertificateChain> signer = Config::instance()->signer_chain ();
307 if (!signer->valid ()) {
308 error ("signing certificate chain is invalid.");
311 /* Make a new empty KDM and add the keys from the DKDM to it */
312 dcp::DecryptedKDM kdm (
315 dkdm.annotation_text().get_value_or(""),
316 dkdm.content_title_text(),
317 dcp::LocalTime().as_string()
320 BOOST_FOREACH (dcp::DecryptedKDMKey const & j, dkdm.keys()) {
324 return kdm.encrypt (signer, target, trusted_devices, formulation, disable_forensic_marking_picture, disable_forensic_marking_audio);
329 list<shared_ptr<Screen> > screens,
330 dcp::DecryptedKDM dkdm,
332 boost::filesystem::path output,
333 dcp::NameFormat container_name_format,
334 dcp::NameFormat filename_format,
335 boost::posix_time::ptime valid_from,
336 boost::posix_time::ptime valid_to,
337 dcp::Formulation formulation,
338 bool disable_forensic_marking_picture,
339 optional<int> disable_forensic_marking_audio,
343 dcp::NameFormat::Map values;
344 values['f'] = dkdm.annotation_text().get_value_or("");
345 values['b'] = dcp::LocalTime(valid_from).date() + " " + dcp::LocalTime(valid_from).time_of_day(true, false);
346 values['e'] = dcp::LocalTime(valid_to).date() + " " + dcp::LocalTime(valid_to).time_of_day(true, false);
349 list<shared_ptr<ScreenWithKDM> > screen_kdms;
350 BOOST_FOREACH (shared_ptr<Screen> i, screens) {
355 screen_kdms.push_back (
356 shared_ptr<ScreenWithKDM>(
357 new DCPScreenWithKDM(
362 i->trusted_device_thumbprints(),
363 dcp::LocalTime(valid_from, i->utc_offset_hour(), i->utc_offset_minute()),
364 dcp::LocalTime(valid_to, i->utc_offset_hour(), i->utc_offset_minute()),
366 disable_forensic_marking_picture,
367 disable_forensic_marking_audio
373 write_files (screen_kdms, zip, output, container_name_format, filename_format, values, verbose);
374 } catch (FileError& e) {
375 cerr << program_name << ": " << e.what() << " (" << e.file().string() << ")\n";
377 } catch (KDMError& e) {
378 cerr << program_name << ": " << e.what() << "\n";
384 dump_dkdm_group (shared_ptr<DKDMGroup> group, int indent)
387 for (int i = 0; i < indent; ++i) {
390 cout << group->name() << "\n";
392 BOOST_FOREACH (shared_ptr<DKDMBase> i, group->children()) {
393 shared_ptr<DKDMGroup> g = dynamic_pointer_cast<DKDMGroup>(i);
395 dump_dkdm_group (g, indent + 2);
397 for (int j = 0; j < indent; ++j) {
400 shared_ptr<DKDM> d = dynamic_pointer_cast<DKDM>(i);
402 cout << d->dkdm().cpl_id() << "\n";
407 int main (int argc, char* argv[])
409 boost::filesystem::path output = ".";
410 dcp::NameFormat container_name_format = Config::instance()->kdm_container_name_format();
411 dcp::NameFormat filename_format = Config::instance()->kdm_filename_format();
412 optional<string> cinema_name;
413 shared_ptr<Cinema> cinema;
414 string screen_description = "";
415 list<shared_ptr<Screen> > screens;
416 optional<dcp::EncryptedKDM> dkdm;
417 optional<boost::posix_time::ptime> valid_from;
418 optional<boost::posix_time::ptime> valid_to;
420 bool list_cinemas = false;
421 bool list_dkdm_cpls = false;
422 optional<string> duration_string;
423 bool verbose = false;
424 dcp::Formulation formulation = dcp::MODIFIED_TRANSITIONAL_1;
425 bool disable_forensic_marking_picture = false;
426 optional<int> disable_forensic_marking_audio;
428 program_name = argv[0];
430 int option_index = 0;
432 static struct option long_options[] = {
433 { "help", no_argument, 0, 'h'},
434 { "output", required_argument, 0, 'o'},
435 { "filename-format", required_argument, 0, 'K'},
436 { "container-name-format", required_argument, 0, 'Z'},
437 { "valid-from", required_argument, 0, 'f'},
438 { "valid-to", required_argument, 0, 't'},
439 { "valid-duration", required_argument, 0, 'd'},
440 { "formulation", required_argument, 0, 'F' },
441 { "disable-forensic-marking-picture", no_argument, 0, 'p' },
442 { "disable-forensic-marking-audio", optional_argument, 0, 'a' },
443 { "zip", no_argument, 0, 'z' },
444 { "verbose", no_argument, 0, 'v' },
445 { "cinema", required_argument, 0, 'c' },
446 { "screen", required_argument, 0, 'S' },
447 { "certificate", required_argument, 0, 'C' },
448 { "trusted-device", required_argument, 0, 'T' },
449 { "list-cinemas", no_argument, 0, 'B' },
450 { "list-dkdm-cpls", no_argument, 0, 'D' },
454 int c = getopt_long (argc, argv, "ho:K:Z:f:t:d:F:pa::zvc:S:C:T:BD", long_options, &option_index);
468 filename_format = dcp::NameFormat (optarg);
471 container_name_format = dcp::NameFormat (optarg);
474 valid_from = time_from_string (optarg);
477 valid_to = time_from_string (optarg);
480 duration_string = optarg;
483 if (string (optarg) == "modified-transitional-1") {
484 formulation = dcp::MODIFIED_TRANSITIONAL_1;
485 } else if (string (optarg) == "multiple-modified-transitional-1") {
486 formulation = dcp::MULTIPLE_MODIFIED_TRANSITIONAL_1;
487 } else if (string (optarg) == "dci-any") {
488 formulation = dcp::DCI_ANY;
489 } else if (string (optarg) == "dci-specific") {
490 formulation = dcp::DCI_SPECIFIC;
492 error ("unrecognised KDM formulation " + string (optarg));
496 disable_forensic_marking_picture = true;
499 disable_forensic_marking_audio = 0;
500 if (optarg == 0 && argv[optind] != 0 && argv[optind][0] != '-') {
501 disable_forensic_marking_audio = atoi (argv[optind++]);
503 disable_forensic_marking_audio = atoi (optarg);
513 /* This could be a cinema to search for in the configured list or the name of a cinema being
514 built up on-the-fly in the option. Cater for both possilibities here by storing the name
515 (for lookup) and by creating a Cinema which the next Screen will be added to.
517 cinema_name = optarg;
518 cinema = shared_ptr<Cinema> (new Cinema (optarg, list<string>(), "", 0, 0));
521 screen_description = optarg;
525 /* Make a new screen and add it to the current cinema */
526 dcp::CertificateChain chain (dcp::file_to_string(optarg));
527 shared_ptr<Screen> screen (new Screen (screen_description, "", chain.leaf(), vector<TrustedDevice>()));
529 cinema->add_screen (screen);
531 screens.push_back (screen);
535 /* A trusted device ends up in the last screen we made */
536 if (!screens.empty ()) {
537 screens.back()->trusted_devices.push_back(TrustedDevice(dcp::Certificate(dcp::file_to_string(optarg))));
544 list_dkdm_cpls = true;
550 BOOST_FOREACH(shared_ptr<Cinema> i, Config::instance()->cinemas()) {
551 cout << i->name << " (" << Emailer::address_list(i->emails) << ")\n";
556 if (list_dkdm_cpls) {
557 dump_dkdm_group (Config::instance()->dkdms(), 0);
561 if (!duration_string && !valid_to) {
562 error ("you must specify a --valid-duration or --valid-to");
566 error ("you must specify --valid-from");
570 if (optind >= argc) {
575 if (screens.empty()) {
577 error ("you must specify either a cinema or one or more screens using certificate files");
580 screens = find_cinema (*cinema_name)->screens ();
583 if (duration_string) {
584 valid_to = valid_from.get() + duration_from_string (*duration_string);
587 dcpomatic_setup_path_encoding ();
591 cout << "Making KDMs valid from " << valid_from.get() << " to " << valid_to.get() << "\n";
594 string const thing = argv[optind];
595 if (boost::filesystem::is_directory(thing) && boost::filesystem::is_regular_file(boost::filesystem::path(thing) / "metadata.xml")) {
601 container_name_format,
606 disable_forensic_marking_picture,
607 disable_forensic_marking_audio,
611 if (boost::filesystem::is_regular_file(thing)) {
612 dkdm = dcp::EncryptedKDM (dcp::file_to_string (thing));
614 dkdm = find_dkdm (thing);
618 error ("could not find film or CPL ID corresponding to " + thing);
623 dcp::DecryptedKDM (*dkdm, Config::instance()->decryption_chain()->key().get()),
626 container_name_format,
631 disable_forensic_marking_picture,
632 disable_forensic_marking_audio,