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