Various probably quite untidy progress on KDMs.
[libdcp.git] / src / dcp.h
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/dcp.h
21  *  @brief A class to create a DCP.
22  */
23
24 #ifndef LIBDCP_DCP_H
25 #define LIBDCP_DCP_H
26
27 #include <string>
28 #include <vector>
29 #include <boost/shared_ptr.hpp>
30 #include <boost/signals2.hpp>
31 #include "types.h"
32 #include "certificates.h"
33
34 namespace xmlpp {
35         class Document;
36         class Element;
37 }
38
39 /** @brief Namespace for everything in libdcp */
40 namespace libdcp
41 {
42
43 class Asset;    
44 class PictureAsset;
45 class SoundAsset;
46 class SubtitleAsset;
47 class Reel;
48 class AssetMap;
49
50 class CPL
51 {
52 public:
53         CPL (std::string directory, std::string name, ContentKind content_kind, int length, int frames_per_second);
54         CPL (std::string directory, std::string file, boost::shared_ptr<const AssetMap> asset_map, bool require_mxfs = true);
55
56         void add_reel (boost::shared_ptr<const Reel> reel);
57         
58         /** @return the length in frames */
59         int length () const {
60                 return _length;
61         }
62
63         /** @return the type of the content, used by media servers
64          *  to categorise things (e.g. feature, trailer, etc.)
65          */
66         ContentKind content_kind () const {
67                 return _content_kind;
68         }
69
70         std::list<boost::shared_ptr<const Reel> > reels () const {
71                 return _reels;
72         }
73
74         /** @return the CPL's name, as will be presented on projector
75          *  media servers and theatre management systems.
76          */
77         std::string name () const {
78                 return _name;
79         }
80
81         /** @return the number of frames per second */
82         int frames_per_second () const {
83                 return _fps;
84         }
85
86         std::list<boost::shared_ptr<const Asset> > assets () const;
87         
88         bool equals (CPL const & other, EqualityOptions options, std::list<std::string>& notes) const;
89         
90         void write_xml (bool, CertificateChain const &, std::string const &) const;
91         void write_to_assetmap (std::ostream& s) const;
92         void write_to_pkl (xmlpp::Element* p) const;
93
94         boost::shared_ptr<xmlpp::Document> make_kdm (CertificateChain const &, std::string const &, boost::shared_ptr<const Certificate>) const;
95         
96 private:
97         std::string _directory;
98         /** the name of the DCP */
99         std::string _name;
100         /** the content kind of the CPL */
101         ContentKind _content_kind;
102         /** length in frames */
103         mutable int _length;
104         /** frames per second */
105         int _fps;
106         /** reels */
107         std::list<boost::shared_ptr<const Reel> > _reels;
108
109         std::string _uuid;
110         mutable std::string _digest;
111 };
112
113 /** @class DCP dcp.h libdcp/dcp.h
114  *  @brief A class to create or read a DCP.
115  */
116         
117 class DCP
118 {
119 public:
120         /** Construct a DCP.  You can pass an existing DCP's directory
121          *  as the parameter, or a non-existant folder to create a new
122          *  DCP in.
123          *
124          *  @param directory Directory containing the DCP's files.
125          */
126         DCP (std::string directory);
127
128         /** Read an existing DCP's data.
129          *
130          *  The DCP's XML metadata will be examined, and you can then look at the contents
131          *  of the DCP.
132          *
133          *  @param require_mxfs true to throw an exception if MXF files are missing; setting to false
134          *  can be useful for testing, but normally it should be set to true.
135          */
136         void read (bool require_mxfs = true);
137
138         /** Write the required XML files to the directory that was
139          *  passed into the constructor.
140          */
141         void write_xml () const;
142
143         /** Compare this DCP with another, according to various options.
144          *  @param other DCP to compare this one to.
145          *  @param options Options to define just what "equality" means.
146          *  @param notes Filled in with notes about differences.
147          *  @return true if the DCPs are equal according to EqualityOptions, otherwise false.
148          */
149         bool equals (DCP const & other, EqualityOptions options, std::list<std::string>& notes) const;
150
151         void add_cpl (boost::shared_ptr<CPL> cpl);
152
153         std::list<boost::shared_ptr<const CPL> > cpls () const {
154                 return _cpls;
155         }
156
157         void set_encrypted (bool e) {
158                 _encrypted = e;
159         }
160
161         void set_certificates (CertificateChain const & c) {
162                 _certificates = c;
163         }
164
165         CertificateChain certificates () const {
166                 return _certificates;
167         }
168
169         void set_signer_key (std::string const & s) {
170                 _signer_key = s;
171         }
172
173         std::string signer_key () const {
174                 return _signer_key;
175         }
176
177         /** Emitted with a parameter between 0 and 1 to indicate progress
178          *  for long jobs.
179          */
180         boost::signals2::signal<void (float)> Progress;
181
182 private:
183
184         /** Write the PKL file.
185          *  @param pkl_uuid UUID to use.
186          */
187         std::string write_pkl (std::string pkl_uuid) const;
188         
189         /** Write the VOLINDEX file */
190         void write_volindex () const;
191
192         /** Write the ASSETMAP file.
193          *  @param pkl_uuid UUID of our PKL.
194          *  @param pkl_length Length of our PKL in bytes.
195          */
196         void write_assetmap (std::string pkl_uuid, int pkl_length) const;
197
198         std::list<boost::shared_ptr<const Asset> > assets () const;
199
200         struct Files {
201                 std::list<std::string> cpls;
202                 std::string pkl;
203                 std::string asset_map;
204         };
205
206         /** the directory that we are writing to */
207         std::string _directory;
208         std::list<boost::shared_ptr<const CPL> > _cpls;
209
210         bool _encrypted;
211         CertificateChain _certificates;
212         std::string _signer_key;
213 };
214
215 }
216
217 #endif