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