A few more untested bits.
[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 "AS_DCP.h"
28 #include "KM_prng.h"
29 #include "KM_util.h"
30 #include "mxf_asset.h"
31 #include "util.h"
32 #include "metadata.h"
33 #include "exceptions.h"
34
35 using std::string;
36 using std::list;
37 using boost::shared_ptr;
38 using boost::dynamic_pointer_cast;
39 using namespace libdcp;
40
41 MXFAsset::MXFAsset (string directory, string file_name, boost::signals2::signal<void (float)>* progress, int fps, int entry_point, int length, bool encrypted)
42         : Asset (directory, file_name)
43         , _progress (progress)
44         , _fps (fps)
45         , _entry_point (entry_point)
46         , _length (length)
47         , _encrypted (encrypted)
48         , _encryption_context (0)
49 {
50         if (_encrypted) {
51                 _key_id = make_uuid ();
52                 uint8_t key_buffer[ASDCP::KeyLen];
53                 Kumu::FortunaRNG rng;
54                 rng.FillRandom (key_buffer, ASDCP::KeyLen);
55                 char key_string[ASDCP::KeyLen * 4];
56                 Kumu::bin2hex (key_buffer, ASDCP::KeyLen, key_string, ASDCP::KeyLen * 4);
57                 _key_value = key_string;
58                         
59                 _encryption_context = new ASDCP::AESEncContext;
60                 if (ASDCP_FAILURE (_encryption_context->InitKey (key_buffer))) {
61                         throw MiscError ("could not set up encryption context");
62                 }
63
64                 uint8_t cbc_buffer[ASDCP::CBC_BLOCK_SIZE];
65                 
66                 if (ASDCP_FAILURE (_encryption_context->SetIVec (rng.FillRandom (cbc_buffer, ASDCP::CBC_BLOCK_SIZE)))) {
67                         throw MiscError ("could not set up CBC initialization vector");
68                 }
69         }
70 }
71
72 MXFAsset::~MXFAsset ()
73 {
74         delete _encryption_context;
75 }
76
77 void
78 MXFAsset::fill_writer_info (ASDCP::WriterInfo* writer_info) const
79 {
80         writer_info->ProductVersion = Metadata::instance()->product_version;
81         writer_info->CompanyName = Metadata::instance()->company_name;
82         writer_info->ProductName = Metadata::instance()->product_name.c_str();
83
84         writer_info->LabelSetType = ASDCP::LS_MXF_SMPTE;
85         unsigned int c;
86         Kumu::hex2bin (_uuid.c_str(), writer_info->AssetUUID, Kumu::UUID_Length, &c);
87         assert (c == Kumu::UUID_Length);
88
89         if (_encrypted) {
90                 Kumu::GenRandomUUID (writer_info->ContextID);
91                 writer_info->EncryptedEssence = true;
92
93                 unsigned int c;
94                 Kumu::hex2bin (_key_id.c_str(), writer_info->CryptographicKeyID, Kumu::UUID_Length, &c);
95                 assert (c == Kumu::UUID_Length);
96         }
97 }
98
99 bool
100 MXFAsset::equals (shared_ptr<const Asset> other, EqualityOptions, list<string>& notes) const
101 {
102         shared_ptr<const MXFAsset> other_mxf = dynamic_pointer_cast<const MXFAsset> (other);
103         if (!other_mxf) {
104                 notes.push_back ("comparing an MXF asset with a non-MXF asset");
105                 return false;
106         }
107         
108         if (_file_name != other_mxf->_file_name) {
109                 notes.push_back ("MXF names differ");
110                 return false;
111         }
112
113         if (_fps != other_mxf->_fps) {
114                 notes.push_back ("MXF frames per second differ");
115                 return false;
116         }
117         
118         if (_length != other_mxf->_length) {
119                 notes.push_back ("MXF lengths differ");
120                 return false;
121         }
122
123         return true;
124 }
125
126 int
127 MXFAsset::length () const
128 {
129         return _length;
130 }