Merge master; at least partially.
[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 <fstream>
26 #include <boost/filesystem.hpp>
27 #include <libxml++/nodes/element.h>
28 #include "AS_DCP.h"
29 #include "KM_prng.h"
30 #include "KM_util.h"
31 #include "mxf_asset.h"
32 #include "util.h"
33 #include "metadata.h"
34 #include "exceptions.h"
35
36 using std::string;
37 using std::list;
38 using boost::shared_ptr;
39 using boost::dynamic_pointer_cast;
40 using namespace libdcp;
41
42 MXFAsset::MXFAsset (string directory, string file_name)
43         : Asset (directory, file_name)
44         , _progress (0)
45         , _encrypted (false)
46         , _encryption_context (0)
47 {
48
49 }
50
51 MXFAsset::MXFAsset (string directory, string file_name, boost::signals2::signal<void (float)>* progress, int edit_rate, int intrinsic_duration, bool encrypted)
52         : Asset (directory, file_name, edit_rate, intrinsic_duration)
53         , _progress (progress)
54         , _encrypted (encrypted)
55         , _encryption_context (0)
56 {
57         if (_encrypted) {
58                 _key_id = make_uuid ();
59                 uint8_t key_buffer[ASDCP::KeyLen];
60                 Kumu::FortunaRNG rng;
61                 rng.FillRandom (key_buffer, ASDCP::KeyLen);
62                 char key_string[ASDCP::KeyLen * 4];
63                 Kumu::bin2hex (key_buffer, ASDCP::KeyLen, key_string, ASDCP::KeyLen * 4);
64                 _key_value = key_string;
65                         
66                 _encryption_context = new ASDCP::AESEncContext;
67                 if (ASDCP_FAILURE (_encryption_context->InitKey (key_buffer))) {
68                         throw MiscError ("could not set up encryption context");
69                 }
70
71                 uint8_t cbc_buffer[ASDCP::CBC_BLOCK_SIZE];
72                 
73                 if (ASDCP_FAILURE (_encryption_context->SetIVec (rng.FillRandom (cbc_buffer, ASDCP::CBC_BLOCK_SIZE)))) {
74                         throw MiscError ("could not set up CBC initialization vector");
75                 }
76         }
77 }
78
79 MXFAsset::~MXFAsset ()
80 {
81         delete _encryption_context;
82 }
83
84 void
85 MXFAsset::fill_writer_info (ASDCP::WriterInfo* writer_info, string uuid, MXFMetadata const & metadata)
86 {
87         writer_info->ProductVersion = metadata.product_version;
88         writer_info->CompanyName = metadata.company_name;
89         writer_info->ProductName = metadata.product_name.c_str();
90
91         writer_info->LabelSetType = ASDCP::LS_MXF_SMPTE;
92         unsigned int c;
93         Kumu::hex2bin (uuid.c_str(), writer_info->AssetUUID, Kumu::UUID_Length, &c);
94         assert (c == Kumu::UUID_Length);
95
96         if (_encrypted) {
97                 Kumu::GenRandomUUID (writer_info->ContextID);
98                 writer_info->EncryptedEssence = true;
99
100                 unsigned int c;
101                 Kumu::hex2bin (_key_id.c_str(), writer_info->CryptographicKeyID, Kumu::UUID_Length, &c);
102                 assert (c == Kumu::UUID_Length);
103         }
104 }
105
106 bool
107 MXFAsset::equals (shared_ptr<const Asset> other, EqualityOptions opt, boost::function<void (NoteType, string)> note) const
108 {
109         if (!Asset::equals (other, opt, note)) {
110                 return false;
111         }
112         
113         shared_ptr<const MXFAsset> other_mxf = dynamic_pointer_cast<const MXFAsset> (other);
114         if (!other_mxf) {
115                 note (ERROR, "comparing an MXF asset with a non-MXF asset");
116                 return false;
117         }
118         
119         if (_file_name != other_mxf->_file_name) {
120                 note (ERROR, "MXF names differ");
121                 if (!opt.mxf_names_can_differ) {
122                         return false;
123                 }
124         }
125         
126         return true;
127 }
128
129 void
130 MXFAsset::add_typed_key_id (xmlpp::Element* parent) const
131 {
132         xmlpp::Element* typed_key_id = parent->add_child("TypedKeyId");
133         typed_key_id->add_child("KeyType")->add_child_text(key_type ());
134         typed_key_id->add_child("KeyId")->add_child_text("urn:uuid:" + _key_id);
135 }