From 4f902db0ad994910a34ca845225635ceefcac96e Mon Sep 17 00:00:00 2001 From: Carl Hetherington Date: Mon, 31 Dec 2012 18:16:58 +0000 Subject: Initial hacks. --- examples/make_dcp.cc | 4 +- run-tests.sh | 14 +++-- src/dcp_time.cc | 11 ++-- src/mxf_asset.cc | 41 ++++++++++++++- src/mxf_asset.h | 15 +++++- src/picture_asset.cc | 23 +++++---- src/picture_asset.h | 12 +++-- src/sound_asset.cc | 12 ++--- src/sound_asset.h | 8 ++- .../81fb54df-e1bf-4647-8788-ea7ba154375b_cpl.xml | 39 -------------- test/ref/DCP/ASSETMAP.xml | 55 -------------------- test/ref/DCP/VOLINDEX.xml | 4 -- test/ref/DCP/audio.mxf | Bin 305326 -> 0 bytes .../df0e4141-13c3-4a7a-bef8-b5a04fcbc4bb_pkl.xml | 30 ----------- test/ref/DCP/video.mxf | Bin 26080 -> 0 bytes test/tests.cc | 57 +++++++++++++++++++-- 16 files changed, 159 insertions(+), 166 deletions(-) delete mode 100644 test/ref/DCP/81fb54df-e1bf-4647-8788-ea7ba154375b_cpl.xml delete mode 100644 test/ref/DCP/ASSETMAP.xml delete mode 100644 test/ref/DCP/VOLINDEX.xml delete mode 100644 test/ref/DCP/audio.mxf delete mode 100644 test/ref/DCP/df0e4141-13c3-4a7a-bef8-b5a04fcbc4bb_pkl.xml delete mode 100644 test/ref/DCP/video.mxf diff --git a/examples/make_dcp.cc b/examples/make_dcp.cc index e1d8353b..22490d71 100644 --- a/examples/make_dcp.cc +++ b/examples/make_dcp.cc @@ -73,7 +73,7 @@ main () for 2K projectors. */ boost::shared_ptr picture_asset ( - new libdcp::MonoPictureAsset (video_frame, "My Film DCP", "video.mxf", 0, 24, 48, 1998, 1080) + new libdcp::MonoPictureAsset (video_frame, "My Film DCP", "video.mxf", 0, 24, 48, 1998, 1080, false) ); /* Now we will create a `sound asset', which is made up of a WAV file for each channel of audio. Here we're using @@ -93,7 +93,7 @@ main () /* Now we can create the sound asset using these files */ boost::shared_ptr sound_asset ( - new libdcp::SoundAsset (sound_files, "My Film DCP", "audio.mxf", 0, 24, 48) + new libdcp::SoundAsset (sound_files, "My Film DCP", "audio.mxf", 0, 24, 48, false) ); /* Now that we have the assets, we can create a Reel to put them in and add it to the CPL */ diff --git a/run-tests.sh b/run-tests.sh index 3c92dae0..e08dc86d 100755 --- a/run-tests.sh +++ b/run-tests.sh @@ -1,8 +1,9 @@ #!/bin/bash # -# Runs our test suite, which builds a DCP. -# The output is compared against the one +# Runs our test suite, which (amongst other things) +# builds a couple of DCPs. +# The outputs are compared against the ones # in test/ref/DCP, and an error is given # if anything is different. # @@ -13,7 +14,14 @@ if [ "$1" == "--debug" ]; then else LD_LIBRARY_PATH=build/src:build/asdcplib/src build/test/tests fi -diff -ur test/ref/DCP build/test/foo + +diff -ur test/ref/DCP/foo build/test/foo +if [ "$?" != "0" ]; then + echo "FAIL: files differ" + exit 1 +fi + +diff -ur test/ref/DCP/bar build/test/bar if [ "$?" != "0" ]; then echo "FAIL: files differ" exit 1 diff --git a/src/dcp_time.cc b/src/dcp_time.cc index c9cd751a..c5a7affd 100644 --- a/src/dcp_time.cc +++ b/src/dcp_time.cc @@ -39,9 +39,14 @@ Time::Time (int frame, int frames_per_second) , s (0) , t (0) { - float sec_float = float (frame) / frames_per_second; - t = (int (floor (sec_float * 1000)) % 1000) / 4; - s = floor (sec_float); + set (double (frame) / frames_per_second); +} + +void +Time::set (double ss) +{ + t = (int (round (ss * 1000)) % 1000) / 4; + s = floor (ss); if (s > 60) { m = s / 60; diff --git a/src/mxf_asset.cc b/src/mxf_asset.cc index 95412d0c..d229331b 100644 --- a/src/mxf_asset.cc +++ b/src/mxf_asset.cc @@ -25,10 +25,12 @@ #include #include #include "AS_DCP.h" +#include "KM_prng.h" #include "KM_util.h" #include "mxf_asset.h" #include "util.h" #include "metadata.h" +#include "exceptions.h" using std::string; using std::list; @@ -36,14 +38,40 @@ using boost::shared_ptr; using boost::dynamic_pointer_cast; using namespace libdcp; -MXFAsset::MXFAsset (string directory, string file_name, boost::signals2::signal* progress, int fps, int entry_point, int length) +MXFAsset::MXFAsset (string directory, string file_name, boost::signals2::signal* progress, int fps, int entry_point, int length, bool encrypted) : Asset (directory, file_name) , _progress (progress) , _fps (fps) , _entry_point (entry_point) , _length (length) + , _encrypted (encrypted) + , _encryption_context (0) { - + if (_encrypted) { + _key_id = make_uuid (); + uint8_t key_buffer[ASDCP::KeyLen]; + Kumu::FortunaRNG rng; + rng.FillRandom (key_buffer, ASDCP::KeyLen); + char key_string[ASDCP::KeyLen * 4]; + Kumu::bin2hex (key_buffer, ASDCP::KeyLen, key_string, ASDCP::KeyLen * 4); + _key_value = key_string; + + _encryption_context = new ASDCP::AESEncContext; + if (ASDCP_FAILURE (_encryption_context->InitKey (key_buffer))) { + throw MiscError ("could not set up encryption context"); + } + + uint8_t cbc_buffer[ASDCP::CBC_BLOCK_SIZE]; + + if (ASDCP_FAILURE (_encryption_context->SetIVec (rng.FillRandom (cbc_buffer, ASDCP::CBC_BLOCK_SIZE)))) { + throw MiscError ("could not set up CBC initialization vector"); + } + } +} + +MXFAsset::~MXFAsset () +{ + delete _encryption_context; } void @@ -57,6 +85,15 @@ MXFAsset::fill_writer_info (ASDCP::WriterInfo* writer_info) const unsigned int c; Kumu::hex2bin (_uuid.c_str(), writer_info->AssetUUID, Kumu::UUID_Length, &c); assert (c == Kumu::UUID_Length); + + if (_encrypted) { + Kumu::GenRandomUUID (writer_info->ContextID); + writer_info->EncryptedEssence = true; + + unsigned int c; + Kumu::hex2bin (_key_id.c_str(), writer_info->CryptographicKeyID, Kumu::UUID_Length, &c); + assert (c == Kumu::UUID_Length); + } } bool diff --git a/src/mxf_asset.h b/src/mxf_asset.h index 03f2aa6b..e45ab2d7 100644 --- a/src/mxf_asset.h +++ b/src/mxf_asset.h @@ -23,6 +23,10 @@ #include #include "asset.h" +namespace ASDCP { + class AESEncContext; +} + namespace libdcp { @@ -36,8 +40,13 @@ public: * @param fps Frames per second. * @param entry_point The entry point of this MXF; ie the first frame that should be used. * @param length Length in frames. + * @param encrypted true if the MXF should be encrypted. */ - MXFAsset (std::string directory, std::string file_name, boost::signals2::signal* progress, int fps, int entry_point, int length); + MXFAsset ( + std::string directory, std::string file_name, boost::signals2::signal* progress, int fps, int entry_point, int length, bool encrypted + ); + + ~MXFAsset (); virtual bool equals (boost::shared_ptr other, EqualityOptions opt, std::list& notes) const; @@ -56,6 +65,10 @@ protected: int _entry_point; /** Length in frames */ int _length; + bool _encrypted; + ASDCP::AESEncContext* _encryption_context; + std::string _key_value; + std::string _key_id; }; } diff --git a/src/picture_asset.cc b/src/picture_asset.cc index ef5d40d4..f2c489e7 100644 --- a/src/picture_asset.cc +++ b/src/picture_asset.cc @@ -46,8 +46,8 @@ using boost::dynamic_pointer_cast; using boost::lexical_cast; using namespace libdcp; -PictureAsset::PictureAsset (string directory, string mxf_name, boost::signals2::signal* progress, int fps, int entry_point, int length) - : MXFAsset (directory, mxf_name, progress, fps, entry_point, length) +PictureAsset::PictureAsset (string directory, string mxf_name, boost::signals2::signal* progress, int fps, int entry_point, int length, bool encrypted) + : MXFAsset (directory, mxf_name, progress, fps, entry_point, length, encrypted) , _width (0) , _height (0) { @@ -138,8 +138,9 @@ MonoPictureAsset::MonoPictureAsset ( int fps, int length, int width, - int height) - : PictureAsset (directory, mxf_name, progress, fps, 0, length) + int height, + bool encrypted) + : PictureAsset (directory, mxf_name, progress, fps, 0, length, encrypted) { _width = width; _height = height; @@ -154,8 +155,9 @@ MonoPictureAsset::MonoPictureAsset ( int fps, int length, int width, - int height) - : PictureAsset (directory, mxf_name, progress, fps, 0, length) + int height, + bool encrypted) + : PictureAsset (directory, mxf_name, progress, fps, 0, length, encrypted) { _width = width; _height = height; @@ -163,7 +165,7 @@ MonoPictureAsset::MonoPictureAsset ( } MonoPictureAsset::MonoPictureAsset (string directory, string mxf_name, int fps, int entry_point, int length) - : PictureAsset (directory, mxf_name, 0, fps, entry_point, length) + : PictureAsset (directory, mxf_name, 0, fps, entry_point, length, false) { ASDCP::JP2K::MXFReader reader; if (ASDCP_FAILURE (reader.OpenRead (path().string().c_str()))) { @@ -194,7 +196,7 @@ MonoPictureAsset::construct (boost::function get_path) ASDCP::WriterInfo writer_info; fill_writer_info (&writer_info); - + ASDCP::JP2K::MXFWriter mxf_writer; if (ASDCP_FAILURE (mxf_writer.OpenWrite (path().string().c_str(), writer_info, picture_desc))) { throw MXFFileError ("could not open MXF file for writing", path().string()); @@ -208,8 +210,7 @@ MonoPictureAsset::construct (boost::function get_path) throw FileError ("could not open JPEG2000 file for reading", path); } - /* XXX: passing 0 to WriteFrame ok? */ - if (ASDCP_FAILURE (mxf_writer.WriteFrame (frame_buffer, 0, 0))) { + if (ASDCP_FAILURE (mxf_writer.WriteFrame (frame_buffer, _encryption_context, 0))) { throw MiscError ("error in writing video MXF"); } @@ -363,7 +364,7 @@ PictureAsset::frame_buffer_equals ( StereoPictureAsset::StereoPictureAsset (string directory, string mxf_name, int fps, int entry_point, int length) - : PictureAsset (directory, mxf_name, 0, fps, entry_point, length) + : PictureAsset (directory, mxf_name, 0, fps, entry_point, length, false) { ASDCP::JP2K::MXFSReader reader; if (ASDCP_FAILURE (reader.OpenRead (path().string().c_str()))) { diff --git a/src/picture_asset.h b/src/picture_asset.h index 08eb338a..53c62223 100644 --- a/src/picture_asset.h +++ b/src/picture_asset.h @@ -34,7 +34,9 @@ class StereoPictureFrame; class PictureAsset : public MXFAsset { public: - PictureAsset (std::string directory, std::string mxf_name, boost::signals2::signal* progress, int fps, int entry_point, int length); + PictureAsset ( + std::string directory, std::string mxf_name, boost::signals2::signal* progress, int fps, int entry_point, int length, bool encrypted + ); /** Write details of this asset to a CPL stream. * @param s Stream. @@ -78,6 +80,7 @@ public: * @param length Length in frames. * @param width Width of images in pixels. * @param height Height of images in pixels. + * @param encrypted true if asset should be encrypted. */ MonoPictureAsset ( std::vector const & files, @@ -87,7 +90,8 @@ public: int fps, int length, int width, - int height + int height, + bool encrypted ); /** Construct a PictureAsset, generating the MXF from the JPEG2000 files. @@ -100,6 +104,7 @@ public: * @param length Length in frames. * @param width Width of images in pixels. * @param height Height of images in pixels. + * @param encrypted true if asset should be encrypted. */ MonoPictureAsset ( boost::function get_path, @@ -109,7 +114,8 @@ public: int fps, int length, int width, - int height + int height, + bool encrypted ); MonoPictureAsset (std::string directory, std::string mxf_name, int fps, int entry_point, int length); diff --git a/src/sound_asset.cc b/src/sound_asset.cc index e987239a..5e52da8e 100644 --- a/src/sound_asset.cc +++ b/src/sound_asset.cc @@ -42,9 +42,9 @@ using boost::lexical_cast; using namespace libdcp; SoundAsset::SoundAsset ( - vector const & files, string directory, string mxf_name, boost::signals2::signal* progress, int fps, int length + vector const & files, string directory, string mxf_name, boost::signals2::signal* progress, int fps, int length, bool encrypted ) - : MXFAsset (directory, mxf_name, progress, fps, 0, length) + : MXFAsset (directory, mxf_name, progress, fps, 0, length, encrypted) , _channels (files.size ()) , _sampling_rate (0) { @@ -56,9 +56,9 @@ SoundAsset::SoundAsset ( string directory, string mxf_name, boost::signals2::signal* progress, - int fps, int length, int channels + int fps, int length, int channels, bool encrypted ) - : MXFAsset (directory, mxf_name, progress, fps, 0, length) + : MXFAsset (directory, mxf_name, progress, fps, 0, length, encrypted) , _channels (channels) , _sampling_rate (0) { @@ -66,7 +66,7 @@ SoundAsset::SoundAsset ( } SoundAsset::SoundAsset (string directory, string mxf_name, int fps, int entry_point, int length) - : MXFAsset (directory, mxf_name, 0, fps, entry_point, length) + : MXFAsset (directory, mxf_name, 0, fps, entry_point, length, false) , _channels (0) { ASDCP::PCM::MXFReader reader; @@ -176,7 +176,7 @@ SoundAsset::construct (boost::function get_path) offset += sample_size; } - if (ASDCP_FAILURE (mxf_writer.WriteFrame (frame_buffer, 0, 0))) { + if (ASDCP_FAILURE (mxf_writer.WriteFrame (frame_buffer, _encryption_context, 0))) { throw MiscError ("could not write audio MXF frame"); } diff --git a/src/sound_asset.h b/src/sound_asset.h index 3189c067..4633b9a7 100644 --- a/src/sound_asset.h +++ b/src/sound_asset.h @@ -44,6 +44,7 @@ public: * @param progress Signal to inform of progress. * @param fps Frames per second. * @param length Length in frames. + * @param encrypted true if asset should be encrypted. */ SoundAsset ( std::vector const & files, @@ -51,7 +52,8 @@ public: std::string mxf_name, boost::signals2::signal* progress, int fps, - int length + int length, + bool encrypted ); /** Construct a SoundAsset, generating the MXF from some WAV files. @@ -63,6 +65,7 @@ public: * @param fps Frames per second. * @param length Length in frames. * @param channels Number of audio channels. + * @param encrypted true if asset should be encrypted. */ SoundAsset ( boost::function get_path, @@ -71,7 +74,8 @@ public: boost::signals2::signal* progress, int fps, int length, - int channels + int channels, + bool encrypted ); SoundAsset ( diff --git a/test/ref/DCP/81fb54df-e1bf-4647-8788-ea7ba154375b_cpl.xml b/test/ref/DCP/81fb54df-e1bf-4647-8788-ea7ba154375b_cpl.xml deleted file mode 100644 index 59d50075..00000000 --- a/test/ref/DCP/81fb54df-e1bf-4647-8788-ea7ba154375b_cpl.xml +++ /dev/null @@ -1,39 +0,0 @@ - - - urn:uuid:81fb54df-e1bf-4647-8788-ea7ba154375b - A Test DCP - 2012-07-17T04:45:18+00:00 - OpenDCP 0.0.25 - A Test DCP - feature - - urn:uri:81fb54df-e1bf-4647-8788-ea7ba154375b_2012-07-17T04:45:18+00:00 - 81fb54df-e1bf-4647-8788-ea7ba154375b_2012-07-17T04:45:18+00:00 - - - - - urn:uuid:379fa64c-ad71-46cf-bef7-b45624006610 - - - urn:uuid:d36f4bb3-c4fa-4a95-9915-6fec3110cd71 - video.mxf - 24 1 - 24 - 0 - 24 - 24 1 - 32 32 - - - urn:uuid:c38bdd62-ce03-4988-8603-195f134207c7 - audio.mxf - 24 1 - 24 - 0 - 24 - - - - - diff --git a/test/ref/DCP/ASSETMAP.xml b/test/ref/DCP/ASSETMAP.xml deleted file mode 100644 index defe18da..00000000 --- a/test/ref/DCP/ASSETMAP.xml +++ /dev/null @@ -1,55 +0,0 @@ - - - urn:uuid:b135d5cf-d180-43d8-b0b3-7373737b73bf - OpenDCP 0.0.25 - 1 - 2012-07-17T04:45:18+00:00 - OpenDCP 0.0.25 - - - urn:uuid:df0e4141-13c3-4a7a-bef8-b5a04fcbc4bb - true - - - df0e4141-13c3-4a7a-bef8-b5a04fcbc4bb_pkl.xml - 1 - 0 - 1049 - - - - - urn:uuid:81fb54df-e1bf-4647-8788-ea7ba154375b - - - 81fb54df-e1bf-4647-8788-ea7ba154375b_cpl.xml - 1 - 0 - 1526 - - - - - urn:uuid:c38bdd62-ce03-4988-8603-195f134207c7 - - - audio.mxf - 1 - 0 - 305326 - - - - - urn:uuid:d36f4bb3-c4fa-4a95-9915-6fec3110cd71 - - - video.mxf - 1 - 0 - 26080 - - - - - diff --git a/test/ref/DCP/VOLINDEX.xml b/test/ref/DCP/VOLINDEX.xml deleted file mode 100644 index f66c004a..00000000 --- a/test/ref/DCP/VOLINDEX.xml +++ /dev/null @@ -1,4 +0,0 @@ - - - 1 - diff --git a/test/ref/DCP/audio.mxf b/test/ref/DCP/audio.mxf deleted file mode 100644 index 9bc735af..00000000 Binary files a/test/ref/DCP/audio.mxf and /dev/null differ diff --git a/test/ref/DCP/df0e4141-13c3-4a7a-bef8-b5a04fcbc4bb_pkl.xml b/test/ref/DCP/df0e4141-13c3-4a7a-bef8-b5a04fcbc4bb_pkl.xml deleted file mode 100644 index 7a8ec697..00000000 --- a/test/ref/DCP/df0e4141-13c3-4a7a-bef8-b5a04fcbc4bb_pkl.xml +++ /dev/null @@ -1,30 +0,0 @@ - - - urn:uuid:df0e4141-13c3-4a7a-bef8-b5a04fcbc4bb - A Test DCP - 2012-07-17T04:45:18+00:00 - OpenDCP 0.0.25 - OpenDCP 0.0.25 - - - urn:uuid:c38bdd62-ce03-4988-8603-195f134207c7 - audio.mxf - +qImGHkt/XouNaJ1V/+7BtcB4VU= - 305326 - application/mxf - - - urn:uuid:d36f4bb3-c4fa-4a95-9915-6fec3110cd71 - video.mxf - E2vhyxdJQhEzSQZdp31w84ZZpfk= - 26080 - application/mxf - - - urn:uuid:81fb54df-e1bf-4647-8788-ea7ba154375b - TTn7vvdUQi/G+KaW1Pym/DjxULM= - 1526 - text/xml - - - diff --git a/test/ref/DCP/video.mxf b/test/ref/DCP/video.mxf deleted file mode 100644 index 645fb85a..00000000 Binary files a/test/ref/DCP/video.mxf and /dev/null differ diff --git a/test/tests.cc b/test/tests.cc index ef25b7c8..a842fa4d 100644 --- a/test/tests.cc +++ b/test/tests.cc @@ -76,7 +76,8 @@ BOOST_AUTO_TEST_CASE (dcp_test) 24, 24, 32, - 32 + 32, + false )); shared_ptr ms (new libdcp::SoundAsset ( @@ -86,7 +87,8 @@ BOOST_AUTO_TEST_CASE (dcp_test) &(d.Progress), 24, 24, - 2 + 2, + false )); cpl->add_reel (shared_ptr (new libdcp::Reel (mp, ms, shared_ptr ()))); @@ -97,12 +99,12 @@ BOOST_AUTO_TEST_CASE (dcp_test) BOOST_AUTO_TEST_CASE (error_test) { - libdcp::DCP d ("build/test/bar"); + libdcp::DCP d ("build/test/fred"); vector p; p.push_back ("frobozz"); - BOOST_CHECK_THROW (new libdcp::MonoPictureAsset (p, "build/test/bar", "video.mxf", &d.Progress, 24, 24, 32, 32), libdcp::FileError); - BOOST_CHECK_THROW (new libdcp::SoundAsset (p, "build/test/bar", "audio.mxf", &d.Progress, 24, 24), libdcp::FileError); + BOOST_CHECK_THROW (new libdcp::MonoPictureAsset (p, "build/test/fred", "video.mxf", &d.Progress, 24, 24, 32, 32, false), libdcp::FileError); + BOOST_CHECK_THROW (new libdcp::SoundAsset (p, "build/test/fred", "audio.mxf", &d.Progress, 24, 24, false), libdcp::FileError); } BOOST_AUTO_TEST_CASE (read_dcp) @@ -579,3 +581,48 @@ BOOST_AUTO_TEST_CASE (color) BOOST_CHECK_EQUAL (c.to_argb_string(), "FF0000FF"); } + +BOOST_AUTO_TEST_CASE (encryption) +{ + Kumu::libdcp_test = true; + + libdcp::Metadata* t = libdcp::Metadata::instance (); + t->issuer = "OpenDCP 0.0.25"; + t->creator = "OpenDCP 0.0.25"; + t->company_name = "OpenDCP"; + t->product_name = "OpenDCP"; + t->product_version = "0.0.25"; + t->issue_date = "2012-07-17T04:45:18+00:00"; + boost::filesystem::remove_all ("build/test/bar"); + boost::filesystem::create_directories ("build/test/bar"); + libdcp::DCP d ("build/test/bar"); + shared_ptr cpl (new libdcp::CPL ("build/test/bar", "A Test DCP", libdcp::FEATURE, 24, 24)); + + shared_ptr mp (new libdcp::MonoPictureAsset ( + j2c, + "build/test/bar", + "video.mxf", + &d.Progress, + 24, + 24, + 32, + 32, + false + )); + + shared_ptr ms (new libdcp::SoundAsset ( + wav, + "build/test/bar", + "audio.mxf", + &(d.Progress), + 24, + 24, + 2, + false + )); + + cpl->add_reel (shared_ptr (new libdcp::Reel (mp, ms, shared_ptr ()))); + d.add_cpl (cpl); + + d.write_xml (); +} -- cgit v1.2.3