Various probably quite untidy progress on KDMs.
[libdcp.git] / src / mxf_asset.cc
1 /*
2     Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 /** @file  src/asset.cc
21  *  @brief Parent class for assets of DCPs made up of MXF files.
22  */
23
24 #include <iostream>
25 #include <fstream>
26 #include <boost/filesystem.hpp>
27 #include <libxml++/nodes/element.h>
28 #include "AS_DCP.h"
29 #include "KM_prng.h"
30 #include "KM_util.h"
31 #include "mxf_asset.h"
32 #include "util.h"
33 #include "metadata.h"
34 #include "exceptions.h"
35
36 using std::string;
37 using std::list;
38 using boost::shared_ptr;
39 using boost::dynamic_pointer_cast;
40 using namespace libdcp;
41
42 MXFAsset::MXFAsset (string directory, string file_name, boost::signals2::signal<void (float)>* progress, int fps, int entry_point, int length, bool encrypted)
43         : Asset (directory, file_name)
44         , _progress (progress)
45         , _fps (fps)
46         , _entry_point (entry_point)
47         , _length (length)
48         , _encrypted (encrypted)
49         , _encryption_context (0)
50 {
51         if (_encrypted) {
52                 _key_id = make_uuid ();
53                 uint8_t key_buffer[ASDCP::KeyLen];
54                 Kumu::FortunaRNG rng;
55                 rng.FillRandom (key_buffer, ASDCP::KeyLen);
56                 char key_string[ASDCP::KeyLen * 4];
57                 Kumu::bin2hex (key_buffer, ASDCP::KeyLen, key_string, ASDCP::KeyLen * 4);
58                 _key_value = key_string;
59                         
60                 _encryption_context = new ASDCP::AESEncContext;
61                 if (ASDCP_FAILURE (_encryption_context->InitKey (key_buffer))) {
62                         throw MiscError ("could not set up encryption context");
63                 }
64
65                 uint8_t cbc_buffer[ASDCP::CBC_BLOCK_SIZE];
66                 
67                 if (ASDCP_FAILURE (_encryption_context->SetIVec (rng.FillRandom (cbc_buffer, ASDCP::CBC_BLOCK_SIZE)))) {
68                         throw MiscError ("could not set up CBC initialization vector");
69                 }
70         }
71 }
72
73 MXFAsset::~MXFAsset ()
74 {
75         delete _encryption_context;
76 }
77
78 void
79 MXFAsset::fill_writer_info (ASDCP::WriterInfo* writer_info) const
80 {
81         writer_info->ProductVersion = Metadata::instance()->product_version;
82         writer_info->CompanyName = Metadata::instance()->company_name;
83         writer_info->ProductName = Metadata::instance()->product_name.c_str();
84
85         writer_info->LabelSetType = ASDCP::LS_MXF_SMPTE;
86         unsigned int c;
87         Kumu::hex2bin (_uuid.c_str(), writer_info->AssetUUID, Kumu::UUID_Length, &c);
88         assert (c == Kumu::UUID_Length);
89
90         if (_encrypted) {
91                 Kumu::GenRandomUUID (writer_info->ContextID);
92                 writer_info->EncryptedEssence = true;
93
94                 unsigned int c;
95                 Kumu::hex2bin (_key_id.c_str(), writer_info->CryptographicKeyID, Kumu::UUID_Length, &c);
96                 assert (c == Kumu::UUID_Length);
97         }
98 }
99
100 bool
101 MXFAsset::equals (shared_ptr<const Asset> other, EqualityOptions, list<string>& notes) const
102 {
103         shared_ptr<const MXFAsset> other_mxf = dynamic_pointer_cast<const MXFAsset> (other);
104         if (!other_mxf) {
105                 notes.push_back ("comparing an MXF asset with a non-MXF asset");
106                 return false;
107         }
108         
109         if (_file_name != other_mxf->_file_name) {
110                 notes.push_back ("MXF names differ");
111                 return false;
112         }
113
114         if (_fps != other_mxf->_fps) {
115                 notes.push_back ("MXF frames per second differ");
116                 return false;
117         }
118         
119         if (_length != other_mxf->_length) {
120                 notes.push_back ("MXF lengths differ");
121                 return false;
122         }
123
124         return true;
125 }
126
127 int
128 MXFAsset::length () const
129 {
130         return _length;
131 }
132
133 void
134 MXFAsset::add_typed_key_id (xmlpp::Element* parent) const
135 {
136         xmlpp::Element* typed_key_id = parent->add_child("TypedKeyId");
137         typed_key_id->add_child("KeyType")->add_child_text(key_type ());
138         typed_key_id->add_child("KeyId")->add_child_text("urn:uuid:" + _key_id);
139 }