Add round-trip KDM test. Fix various bugs in KDM generation. Some string -> path.
[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 <boost/lexical_cast.hpp>
28 #include <libxml++/nodes/element.h>
29 #include "AS_DCP.h"
30 #include "KM_prng.h"
31 #include "KM_util.h"
32 #include "mxf_asset.h"
33 #include "util.h"
34 #include "metadata.h"
35 #include "exceptions.h"
36 #include "kdm.h"
37
38 using std::string;
39 using std::list;
40 using std::pair;
41 using boost::shared_ptr;
42 using boost::lexical_cast;
43 using boost::dynamic_pointer_cast;
44 using namespace libdcp;
45
46 MXFAsset::MXFAsset (boost::filesystem::path directory, string file_name)
47         : Asset (directory, file_name)
48         , _progress (0)
49         , _encryption_context (0)
50         , _decryption_context (0)
51 {
52
53 }
54
55 MXFAsset::MXFAsset (boost::filesystem::path directory, string file_name, boost::signals2::signal<void (float)>* progress, int edit_rate, int intrinsic_duration) 
56         : Asset (directory, file_name, edit_rate, intrinsic_duration)
57         , _progress (progress)
58         , _encryption_context (0)
59         , _decryption_context (0)
60 {
61         
62 }
63
64 MXFAsset::~MXFAsset ()
65 {
66         delete _encryption_context;
67         delete _decryption_context;
68 }
69
70 void
71 MXFAsset::fill_writer_info (ASDCP::WriterInfo* writer_info, string uuid, bool interop, MXFMetadata const & metadata)
72 {
73         writer_info->ProductVersion = metadata.product_version;
74         writer_info->CompanyName = metadata.company_name;
75         writer_info->ProductName = metadata.product_name.c_str();
76
77         if (interop) {
78                 writer_info->LabelSetType = ASDCP::LS_MXF_INTEROP;
79         } else {
80                 writer_info->LabelSetType = ASDCP::LS_MXF_SMPTE;
81         }
82         unsigned int c;
83         Kumu::hex2bin (uuid.c_str(), writer_info->AssetUUID, Kumu::UUID_Length, &c);
84         assert (c == Kumu::UUID_Length);
85
86         if (_key) {
87                 Kumu::GenRandomUUID (writer_info->ContextID);
88                 writer_info->EncryptedEssence = true;
89
90                 unsigned int c;
91                 Kumu::hex2bin (_key_id.c_str(), writer_info->CryptographicKeyID, Kumu::UUID_Length, &c);
92                 assert (c == Kumu::UUID_Length);
93         }
94 }
95
96 bool
97 MXFAsset::equals (shared_ptr<const Asset> other, EqualityOptions opt, boost::function<void (NoteType, string)> note) const
98 {
99         if (!Asset::equals (other, opt, note)) {
100                 return false;
101         }
102         
103         shared_ptr<const MXFAsset> other_mxf = dynamic_pointer_cast<const MXFAsset> (other);
104         if (!other_mxf) {
105                 note (ERROR, "comparing an MXF asset with a non-MXF asset");
106                 return false;
107         }
108         
109         if (_file_name != other_mxf->_file_name) {
110                 note (ERROR, "MXF names differ");
111                 if (!opt.mxf_names_can_differ) {
112                         return false;
113                 }
114         }
115         
116         return true;
117 }
118
119 void
120 MXFAsset::write_to_cpl (xmlpp::Element* node, bool interop) const
121 {
122         pair<string, string> const attr = cpl_node_attribute (interop);
123         xmlpp::Element* a = node->add_child (cpl_node_name ());
124         if (!attr.first.empty ()) {
125                 a->set_attribute (attr.first, attr.second);
126         }
127         a->add_child ("Id")->add_child_text ("urn:uuid:" + _uuid);
128         a->add_child ("AnnotationText")->add_child_text (_file_name);
129         a->add_child ("EditRate")->add_child_text (lexical_cast<string> (_edit_rate) + " 1");
130         a->add_child ("IntrinsicDuration")->add_child_text (lexical_cast<string> (_intrinsic_duration));
131         a->add_child ("EntryPoint")->add_child_text (lexical_cast<string> (_entry_point));
132         a->add_child ("Duration")->add_child_text (lexical_cast<string> (_duration));
133         if (!_key_id.empty ()) {
134                 a->add_child("KeyId")->add_child_text ("urn:uuid:" + _key_id);
135         }
136 }
137
138 void
139 MXFAsset::set_key (Key key)
140 {
141         _key = key;
142
143         if (_key_id.empty ()) {
144                 /* No key ID so far; we now need one */
145                 _key_id = make_uuid ();
146         }
147         
148         _decryption_context = new ASDCP::AESDecContext;
149         if (ASDCP_FAILURE (_decryption_context->InitKey (_key->value ()))) {
150                 throw MiscError ("could not set up decryption context");
151         }
152
153         _encryption_context = new ASDCP::AESEncContext;
154         if (ASDCP_FAILURE (_encryption_context->InitKey (_key->value ()))) {
155                 throw MiscError ("could not set up encryption context");
156         }
157         
158         uint8_t cbc_buffer[ASDCP::CBC_BLOCK_SIZE];
159         
160         Kumu::FortunaRNG rng;
161         if (ASDCP_FAILURE (_encryption_context->SetIVec (rng.FillRandom (cbc_buffer, ASDCP::CBC_BLOCK_SIZE)))) {
162                 throw MiscError ("could not set up CBC initialization vector");
163         }
164 }