Merge master.
[dcpomatic.git] / src / lib / film.cc
index f0441c9e0b038733b86a7797cc32b3706cc5a028..fb3423bb42c1c35707798ca0c4f23c363e3e5c86 100644 (file)
 #include <boost/algorithm/string.hpp>
 #include <boost/lexical_cast.hpp>
 #include <boost/date_time.hpp>
+#include <libxml++/libxml++.h>
+#include <libdcp/crypt_chain.h>
+#include <libdcp/certificates.h>
+#include "cinema.h"
 #include "film.h"
 #include "format.h"
 #include "job.h"
@@ -65,6 +69,7 @@ using std::ofstream;
 using std::setfill;
 using std::min;
 using std::make_pair;
+using std::list;
 using std::cout;
 using boost::shared_ptr;
 using boost::lexical_cast;
@@ -99,6 +104,7 @@ Film::Film (string d, bool must_exist)
        , _with_subtitles (false)
        , _subtitle_offset (0)
        , _subtitle_scale (1)
+       , _encrypted (false)
        , _colour_lut (0)
        , _j2k_bandwidth (200000000)
        , _frames_per_second (0)
@@ -169,6 +175,7 @@ Film::Film (Film const & o)
        , _with_subtitles    (o._with_subtitles)
        , _subtitle_offset   (o._subtitle_offset)
        , _subtitle_scale    (o._subtitle_scale)
+       , _encrypted         (o._encrypted)
        , _colour_lut        (o._colour_lut)
        , _j2k_bandwidth     (o._j2k_bandwidth)
        , _audio_language    (o._audio_language)
@@ -434,6 +441,7 @@ Film::write_metadata () const
        f << "with_subtitles " << _with_subtitles << "\n";
        f << "subtitle_offset " << _subtitle_offset << "\n";
        f << "subtitle_scale " << _subtitle_scale << "\n";
+       f << "encrypted " << _encrypted << "\n";
        f << "colour_lut " << _colour_lut << "\n";
        f << "j2k_bandwidth " << _j2k_bandwidth << "\n";
        f << "audio_language " << _audio_language << "\n";
@@ -563,6 +571,8 @@ Film::read_metadata ()
                        _subtitle_offset = atoi (v.c_str ());
                } else if (k == "subtitle_scale") {
                        _subtitle_scale = atof (v.c_str ());
+               } else if (k == "encrypted") {
+                       _encrypted = (v == "1");
                } else if (k == "colour_lut") {
                        _colour_lut = atoi (v.c_str ());
                } else if (k == "j2k_bandwidth") {
@@ -1232,6 +1242,16 @@ Film::set_subtitle_scale (float s)
        signal_changed (SUBTITLE_SCALE);
 }
 
+void
+Film::set_encrypted (bool e)
+{
+       {
+               boost::mutex::scoped_lock lm (_state_mutex);
+               _encrypted = e;
+       }
+       signal_changed (ENCRYPTED);
+}
+
 void
 Film::set_colour_lut (int i)
 {
@@ -1431,3 +1451,69 @@ Film::audio_stream () const
 
        return _external_audio_stream;
 }
+
+void
+Film::make_kdms (
+       list<shared_ptr<Screen> > screens,
+       boost::posix_time::ptime from,
+       boost::posix_time::ptime until,
+       string directory
+       ) const
+{
+       string const cd = Config::instance()->crypt_chain_directory ();
+       if (boost::filesystem::is_empty (cd)) {
+               libdcp::make_crypt_chain (cd);
+       }
+
+       libdcp::CertificateChain chain;
+
+       {
+               boost::filesystem::path p (cd);
+               p /= "ca.self-signed.pem";
+               chain.add (shared_ptr<libdcp::Certificate> (new libdcp::Certificate (p.string ())));
+       }
+
+       {
+               boost::filesystem::path p (cd);
+               p /= "intermediate.signed.pem";
+               chain.add (shared_ptr<libdcp::Certificate> (new libdcp::Certificate (p.string ())));
+       }
+
+       {
+               boost::filesystem::path p (cd);
+               p /= "leaf.signed.pem";
+               chain.add (shared_ptr<libdcp::Certificate> (new libdcp::Certificate (p.string ())));
+       }
+
+       boost::filesystem::path signer_key (cd);
+       signer_key /= "leaf.key";
+
+       /* Find the DCP to make the KDM for */
+       string const dir = this->directory ();
+       list<string> dcps;
+       for (boost::filesystem::directory_iterator i = boost::filesystem::directory_iterator(dir); i != boost::filesystem::directory_iterator(); ++i) {
+               if (boost::filesystem::is_directory (*i) && i->path().leaf() != "j2c" && i->path().leaf() != "wavs") {
+                       dcps.push_back (i->path().string());
+               }
+       }
+
+       if (dcps.empty()) {
+               throw KDMError ("Could not find DCP to make KDM for");
+       } else if (dcps.size() > 1) {
+               throw KDMError ("More than one possible DCP to make KDM for");
+       }
+
+       for (list<shared_ptr<Screen> >::iterator i = screens.begin(); i != screens.end(); ++i) {
+
+               libdcp::DCP dcp (dcps.front ());
+               dcp.read ();
+               
+               /* XXX: single CPL only */
+               shared_ptr<xmlpp::Document> kdm = dcp.cpls().front()->make_kdm (chain, signer_key.string(), (*i)->certificate, from, until);
+
+               boost::filesystem::path out = directory;
+               out /= "kdm.xml";
+               kdm->write_to_file_formatted (out.string());
+       }
+}
+