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