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