It builds again.
[libdcp.git] / src / mxf.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 "AS_DCP.h"
25 #include "KM_prng.h"
26 #include "KM_util.h"
27 #include "mxf.h"
28 #include "util.h"
29 #include "metadata.h"
30 #include "exceptions.h"
31 #include "kdm.h"
32 #include <libxml++/nodes/element.h>
33 #include <boost/filesystem.hpp>
34 #include <boost/lexical_cast.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::lexical_cast;
42 using boost::dynamic_pointer_cast;
43 using namespace dcp;
44
45 MXF::MXF (Fraction edit_rate)
46         : Content (edit_rate)
47         , _progress (0)
48         , _encryption_context (0)
49         , _decryption_context (0)
50 {
51
52 }
53
54 MXF::MXF (boost::filesystem::path file)
55         : Content (file)
56         , _progress (0)
57         , _encryption_context (0)
58         , _decryption_context (0)
59 {
60
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         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                 assert (c == Kumu::UUID_Length);
92         }
93 }
94
95 bool
96 MXF::equals (shared_ptr<const Content> other, EqualityOptions opt, boost::function<void (NoteType, string)> note) const
97 {
98         if (!Content::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                 note (ERROR, "comparing an MXF asset with a non-MXF asset");
105                 return false;
106         }
107         
108         if (_file != other_mxf->file ()) {
109                 note (ERROR, "MXF names differ");
110                 if (!opt.mxf_names_can_differ) {
111                         return false;
112                 }
113         }
114         
115         return true;
116 }
117
118 void
119 MXF::set_key (Key key)
120 {
121         _key = key;
122
123         if (_key_id.empty ()) {
124                 /* No key ID so far; we now need one */
125                 _key_id = make_uuid ();
126         }
127         
128         _decryption_context = new ASDCP::AESDecContext;
129         if (ASDCP_FAILURE (_decryption_context->InitKey (_key->value ()))) {
130                 throw MiscError ("could not set up decryption context");
131         }
132
133         _encryption_context = new ASDCP::AESEncContext;
134         if (ASDCP_FAILURE (_encryption_context->InitKey (_key->value ()))) {
135                 throw MiscError ("could not set up encryption context");
136         }
137         
138         uint8_t cbc_buffer[ASDCP::CBC_BLOCK_SIZE];
139         
140         Kumu::FortunaRNG rng;
141         if (ASDCP_FAILURE (_encryption_context->SetIVec (rng.FillRandom (cbc_buffer, ASDCP::CBC_BLOCK_SIZE)))) {
142                 throw MiscError ("could not set up CBC initialization vector");
143         }
144 }