Remove unnecessary Content class.
[libdcp.git] / src / mxf.cc
1 /*
2     Copyright (C) 2012-2014 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 "raw_convert.h"
25 #include "AS_DCP.h"
26 #include "KM_prng.h"
27 #include "KM_util.h"
28 #include "mxf.h"
29 #include "util.h"
30 #include "metadata.h"
31 #include "exceptions.h"
32 #include "dcp_assert.h"
33 #include "compose.hpp"
34 #include <libxml++/nodes/element.h>
35 #include <boost/filesystem.hpp>
36 #include <iostream>
37
38 using std::string;
39 using std::list;
40 using std::pair;
41 using boost::shared_ptr;
42 using boost::dynamic_pointer_cast;
43 using namespace dcp;
44
45 MXF::MXF (Fraction edit_rate)
46         : _edit_rate (edit_rate)
47         , _intrinsic_duration (0)
48         , _encryption_context (0)
49         , _decryption_context (0)
50 {
51         /* _intrinsic_duration must be set up up by a subclass */
52 }
53
54 MXF::MXF (boost::filesystem::path file)
55         : Asset (file)
56         , _intrinsic_duration (0)
57         , _encryption_context (0)
58         , _decryption_context (0)
59 {
60         /* _edit_rate and _intrinsic_duration must be set up up by a subclass */
61 }
62
63 MXF::~MXF ()
64 {
65         delete _encryption_context;
66         delete _decryption_context;
67 }
68
69 void
70 MXF::fill_writer_info (ASDCP::WriterInfo* writer_info, Standard standard)
71 {
72         writer_info->ProductVersion = _metadata.product_version;
73         writer_info->CompanyName = _metadata.company_name;
74         writer_info->ProductName = _metadata.product_name.c_str();
75
76         if (standard == INTEROP) {
77                 writer_info->LabelSetType = ASDCP::LS_MXF_INTEROP;
78         } else {
79                 writer_info->LabelSetType = ASDCP::LS_MXF_SMPTE;
80         }
81         unsigned int c;
82         Kumu::hex2bin (_id.c_str(), writer_info->AssetUUID, Kumu::UUID_Length, &c);
83         DCP_ASSERT (c == Kumu::UUID_Length);
84
85         if (_key) {
86                 Kumu::GenRandomUUID (writer_info->ContextID);
87                 writer_info->EncryptedEssence = true;
88
89                 unsigned int c;
90                 Kumu::hex2bin (_key_id.c_str(), writer_info->CryptographicKeyID, Kumu::UUID_Length, &c);
91                 DCP_ASSERT (c == Kumu::UUID_Length);
92         }
93 }
94
95 bool
96 MXF::equals (shared_ptr<const Asset> other, EqualityOptions opt, NoteHandler note) const
97 {
98         if (!Asset::equals (other, opt, note)) {
99                 return false;
100         }
101         
102         shared_ptr<const MXF> other_mxf = dynamic_pointer_cast<const MXF> (other);
103         if (!other_mxf) {
104                 return false;
105         }
106         
107         if (_edit_rate != other_mxf->_edit_rate) {
108                 note (DCP_ERROR, "MXF: edit rates differ");
109                 return false;
110         }
111         
112         if (_intrinsic_duration != other_mxf->_intrinsic_duration) {
113                 note (DCP_ERROR, String::compose ("MXF: intrinsic durations differ (%1 vs %2)", _intrinsic_duration, other_mxf->_intrinsic_duration));
114                 return false;
115         }
116
117         if (_file.leaf() != other_mxf->file().leaf()) {
118                 if (!opt.mxf_filenames_can_differ) {
119                         note (DCP_ERROR, "MXF: filenames differ");
120                         return false;
121                 } else {
122                         note (DCP_NOTE, "MXF: filenames differ");
123                 }
124         }
125         
126         return true;
127 }
128
129 /** Set the (private) key that will be used to encrypt or decrypt this MXF's content.
130  *  This is the top-secret key that is distributed (itself encrypted) to cinemas
131  *  via Key Delivery Messages (KDMs).
132  *  @param key Key to use.
133  */
134 void
135 MXF::set_key (Key key)
136 {
137         _key = key;
138
139         if (_key_id.empty ()) {
140                 /* No key ID so far; we now need one */
141                 _key_id = make_uuid ();
142         }
143         
144         _decryption_context = new ASDCP::AESDecContext;
145         if (ASDCP_FAILURE (_decryption_context->InitKey (_key->value ()))) {
146                 throw MiscError ("could not set up decryption context");
147         }
148
149         _encryption_context = new ASDCP::AESEncContext;
150         if (ASDCP_FAILURE (_encryption_context->InitKey (_key->value ()))) {
151                 throw MiscError ("could not set up encryption context");
152         }
153         
154         uint8_t cbc_buffer[ASDCP::CBC_BLOCK_SIZE];
155         
156         Kumu::FortunaRNG rng;
157         if (ASDCP_FAILURE (_encryption_context->SetIVec (rng.FillRandom (cbc_buffer, ASDCP::CBC_BLOCK_SIZE)))) {
158                 throw MiscError ("could not set up CBC initialization vector");
159         }
160 }
161
162 void
163 MXF::read_writer_info (ASDCP::WriterInfo const & info)
164 {
165         char buffer[64];
166         Kumu::bin2UUIDhex (info.AssetUUID, ASDCP::UUIDlen, buffer, sizeof (buffer));
167         _id = buffer;
168 }
169
170 string
171 MXF::pkl_type (Standard standard) const
172 {
173         switch (standard) {
174         case INTEROP:
175                 return String::compose ("application/x-smpte-mxf;asdcpKind=%1", asdcp_kind ());
176         case SMPTE:
177                 return "application/mxf";
178         default:
179                 DCP_ASSERT (false);
180         }
181 }