Tidying.
[libdcp.git] / src / dcp.h
1 /*
2     Copyright (C) 2012-2021 Carl Hetherington <cth@carlh.net>
3
4     This file is part of libdcp.
5
6     libdcp is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     libdcp is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with libdcp.  If not, see <http://www.gnu.org/licenses/>.
18
19     In addition, as a special exception, the copyright holders give
20     permission to link the code of portions of this program with the
21     OpenSSL library under certain conditions as described in each
22     individual source file, and distribute linked combinations
23     including the two.
24
25     You must obey the GNU General Public License in all respects
26     for all of the code used other than OpenSSL.  If you modify
27     file(s) with this exception, you may extend this exception to your
28     version of the file(s), but you are not obligated to do so.  If you
29     do not wish to do so, delete this exception statement from your
30     version.  If you delete this exception statement from all source
31     files in the program, then also delete it here.
32 */
33
34
35 /** @file  src/dcp.h
36  *  @brief DCP class
37  */
38
39
40 #ifndef LIBDCP_DCP_H
41 #define LIBDCP_DCP_H
42
43
44 #include "compose.hpp"
45 #include "types.h"
46 #include "util.h"
47 #include "certificate.h"
48 #include "metadata.h"
49 #include "name_format.h"
50 #include "verify.h"
51 #include "version.h"
52 #include <boost/signals2.hpp>
53 #include <memory>
54 #include <string>
55 #include <vector>
56
57
58 namespace xmlpp {
59         class Document;
60         class Element;
61 }
62
63
64 /** @brief Namespace for everything in libdcp */
65 namespace dcp
66 {
67
68
69 class PKL;
70 class Content;
71 class Reel;
72 class CPL;
73 class CertificateChain;
74 class DecryptedKDM;
75 class Asset;
76 class ReadError;
77
78
79 /** @class DCP
80  *  @brief A class to create or read a DCP
81  */
82 class DCP
83 {
84 public:
85         /** Construct a DCP.  You can pass an existing DCP's directory
86          *  as the parameter; alternatively, directory will be created
87          *  if it does not exist.  Note that if you pass an existing DCP
88          *  into this constructor it will not be read until you call ::read().
89          *
90          *  @param directory Directory containing the DCP's files.
91          */
92         explicit DCP (boost::filesystem::path directory);
93
94         DCP (DCP const&) = delete;
95         DCP& operator= (DCP const&) = delete;
96
97         /** Read a DCP.  This method does not do any deep checking of the DCP's validity, but
98          *  if it comes across any bad things it will do one of two things.
99          *
100          *  Errors that are so serious that they prevent the method from working will result
101          *  in an exception being thrown.  For example, a missing ASSETMAP means that the DCP
102          *  can't be read without a lot of guesswork, so this will throw.
103          *
104          *  Errors that are not fatal will be added to notes, if it's non-null.  For example,
105          *  if the DCP contains a mixture of Interop and SMPTE elements this will result
106          *  in a note being added to the vector.
107          *
108          *  For more thorough checking of a DCP's contents, see dcp::verify().
109          *
110          *  @param notes List of notes that will be added to if non-0.
111          *  @param ignore_incorrect_picture_mxf_type true to try loading MXF files marked as monoscopic
112          *  as stereoscopic if the monoscopic load fails; fixes problems some 3D DCPs that (I think)
113          *  have an incorrect descriptor in their MXF.
114          */
115         void read (std::vector<VerificationNote>* notes = nullptr, bool ignore_incorrect_picture_mxf_type = false);
116
117         /** Compare this DCP with another, according to various options.
118          *  @param other DCP to compare this one to.
119          *  @param options Options to define what "equality" means.
120          *  @param note Functor to handle notes made by the equality operation.
121          *  @return true if the DCPs are equal according to EqualityOptions, otherwise false.
122          */
123         bool equals (DCP const & other, EqualityOptions options, NoteHandler note) const;
124
125         void add (std::shared_ptr<CPL> cpl);
126
127         std::vector<std::shared_ptr<CPL>> cpls () const;
128
129         /** @param ignore_unresolved true to silently ignore unresolved assets, otherwise
130          *  an exception is thrown if they are found.
131          *  @return All assets (including CPLs).
132          */
133         std::vector<std::shared_ptr<Asset>> assets (bool ignore_unresolved = false) const;
134
135         bool any_encrypted () const;
136         bool all_encrypted () const;
137
138         /** Add a KDM to decrypt this DCP.  This method must be called after DCP::read()
139          *  or the KDM you specify will be ignored.
140          *  @param kdm KDM to use.
141          */
142         void add (DecryptedKDM const &);
143
144         /** Write all the XML files for this DCP
145          *  @param standand INTEROP or SMPTE
146          *  @param issuer Value for the PKL and AssetMap <Issuer> tags
147          *  @param creator Value for the PKL and AssetMap <Creator> tags
148          *  @param issue_date Value for the CPL <IssueDate> tags
149          *  @param annotation_text Value for the CPL <AnnotationText> tags
150          *  @param signer Signer to use
151          *  @param name_format Name format to use for the CPL and PKL filenames
152          */
153         void write_xml (
154                 Standard standard,
155                 std::string issuer = String::compose("libdcp %1", dcp::version),
156                 std::string creator = String::compose("libdcp %1", dcp::version),
157                 std::string issue_date = LocalTime().as_string(),
158                 std::string annotation_text = String::compose("Created by libdcp %1", dcp::version),
159                 std::shared_ptr<const CertificateChain> signer = std::shared_ptr<const CertificateChain>(),
160                 NameFormat name_format = NameFormat("%t")
161         );
162
163         void resolve_refs (std::vector<std::shared_ptr<Asset>> assets);
164
165         /** @return Standard of a DCP that was read in */
166         boost::optional<Standard> standard () const {
167                 return _standard;
168         }
169
170         boost::filesystem::path directory () const {
171                 return _directory;
172         }
173
174         /** @return PKLs if this DCP was read from an existing one, or if write_xml() has been called on it.
175          *  If neither is true, this method returns an empty vector.
176          */
177         std::vector<std::shared_ptr<PKL>> pkls () const {
178                 return _pkls;
179         }
180
181         boost::optional<boost::filesystem::path> asset_map_path () {
182                 return _asset_map;
183         }
184
185         static std::vector<boost::filesystem::path> directories_from_files (std::vector<boost::filesystem::path> files);
186
187 private:
188
189         void write_volindex (Standard standard) const;
190
191         /** Write the ASSETMAP file.
192          *  @param pkl_uuid UUID of our PKL.
193          *  @param pkl_path Pathname of our PKL file.
194          */
195         void write_assetmap (
196                 Standard standard, std::string pkl_uuid, boost::filesystem::path pkl_path,
197                 std::string issuer, std::string creator, std::string issue_date, std::string annotation_text
198                 ) const;
199
200         /** The directory that we are writing to */
201         boost::filesystem::path _directory;
202         /** The CPLs that make up this DCP */
203         std::vector<std::shared_ptr<CPL>> _cpls;
204         /** The PKLs that make up this DCP */
205         std::vector<std::shared_ptr<PKL>> _pkls;
206         /** File that the ASSETMAP was read from or last written to */
207         mutable boost::optional<boost::filesystem::path> _asset_map;
208
209         /** Standard of DCP that was read in */
210         boost::optional<Standard> _standard;
211 };
212
213
214 }
215
216
217 #endif