swaroop: add name to ECinema KDMs and add DKDM wrapper for ECinema.
[dcpomatic.git] / src / tools / dcpomatic_ecinema.cc
index 6a19e5c42724f3f9ca9a62d0c844ecb96939168b..a0324f281aa033f40fa620adfba197365b3f56a9 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2018 Carl Hetherington <cth@carlh.net>
+    Copyright (C) 2018-2019 Carl Hetherington <cth@carlh.net>
 
     This file is part of DCP-o-matic.
 
 */
 
 #include "lib/version.h"
+#include "lib/decrypted_ecinema_kdm.h"
+#include "lib/config.h"
+#include "lib/util.h"
+#include <dcp/key.h>
 extern "C" {
 #include <libavformat/avformat.h>
+#include <libavutil/aes_ctr.h>
 }
 #include <boost/filesystem.hpp>
 #include <boost/optional.hpp>
+#include <openssl/rand.h>
 #include <getopt.h>
 #include <sys/stat.h>
 #include <sys/types.h>
@@ -41,6 +47,7 @@ help (string n)
        cerr << "Syntax: " << n << " [OPTION] <FILE>\n"
             << "  -v, --version        show DCP-o-matic version\n"
             << "  -h, --help           show this help\n"
+            << "  -o, --output         output directory\n"
             << "\n"
             << "<FILE> is the unencrypted .mp4 file.\n";
 }
@@ -83,16 +90,15 @@ main (int argc, char* argv[])
        }
 
        if (!output) {
-               cerr << "You must specify --output or -o\n";
+               cerr << "You must specify --output-media or -o\n";
                exit (EXIT_FAILURE);
        }
 
        boost::filesystem::path input = argv[optind];
        boost::filesystem::path output_mp4 = *output / (input.filename().string() + ".ecinema");
 
-       if (mkdir(output->c_str(), 0777) < 0) {
-               cerr << "Could not create output directory `" << output->string() << "'\n";
-               exit (EXIT_FAILURE);
+       if (!boost::filesystem::is_directory(*output)) {
+               boost::filesystem::create_directory (*output);
        }
 
        av_register_all ();
@@ -147,7 +153,20 @@ main (int argc, char* argv[])
                exit (EXIT_FAILURE);
        }
 
-       if (avformat_write_header (output_fc, 0) < 0) {
+       dcp::Key key (AES_CTR_KEY_SIZE);
+       AVDictionary* options = 0;
+       av_dict_set (&options, "encryption_key", key.hex().c_str(), 0);
+       /* XXX: is this OK? */
+       av_dict_set (&options, "encryption_kid", "00000000000000000000000000000000", 0);
+       av_dict_set (&options, "encryption_scheme", "cenc-aes-ctr", 0);
+
+       string id = dcp::make_uuid ();
+       if (av_dict_set(&output_fc->metadata, SWAROOP_ID_TAG, id.c_str(), 0) < 0) {
+               cerr << "Could not write ID to output.\n";
+               exit (EXIT_FAILURE);
+       }
+
+       if (avformat_write_header (output_fc, &options) < 0) {
                cerr << "Could not write header to output.\n";
                exit (EXIT_FAILURE);
        }
@@ -170,4 +189,8 @@ main (int argc, char* argv[])
 
        avformat_free_context (input_fc);
        avformat_free_context (output_fc);
+
+       DecryptedECinemaKDM decrypted_kdm (id, output_mp4.filename().string(), key);
+       EncryptedECinemaKDM encrypted_kdm = decrypted_kdm.encrypt (Config::instance()->decryption_chain()->leaf());
+       cout << encrypted_kdm.as_xml() << "\n";
 }