Tidy up KDM generation from CPLs a bit.
[libdcp.git] / src / cpl.cc
1 /*
2     Copyright (C) 2012-2014 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 #include "raw_convert.h"
21 #include "cpl.h"
22 #include "util.h"
23 #include "mono_picture_mxf.h"
24 #include "stereo_picture_mxf.h"
25 #include "sound_mxf.h"
26 #include "subtitle_content.h"
27 #include "reel.h"
28 #include "metadata.h"
29 #include "signer.h"
30 #include "exceptions.h"
31 #include "xml.h"
32 #include "compose.hpp"
33 #include "reel_picture_asset.h"
34 #include "reel_sound_asset.h"
35 #include "reel_subtitle_asset.h"
36 #include "local_time.h"
37 #include <libxml/parser.h>
38
39 using std::string;
40 using std::stringstream;
41 using std::ostream;
42 using std::list;
43 using std::pair;
44 using std::make_pair;
45 using boost::shared_ptr;
46 using boost::optional;
47 using namespace dcp;
48
49 CPL::CPL (string annotation_text, ContentKind content_kind)
50         : _annotation_text (annotation_text)
51         /* default _content_title_text to _annotation_text */
52         , _content_title_text (annotation_text)
53         , _content_kind (content_kind)
54         , _content_version_id ("urn:uuid:" + make_uuid ())
55 {
56         /* default _content_version_id to and _content_version_label to
57            a random ID and the current time.
58         */
59         _content_version_id = "urn:uuid:" + make_uuid() + LocalTime().as_string ();
60         _content_version_label_text = _content_version_id;
61 }
62
63 /** Construct a CPL object from a XML file */
64 CPL::CPL (boost::filesystem::path file)
65         : Asset (file)
66         , _content_kind (FEATURE)
67 {
68         cxml::Document f ("CompositionPlaylist");
69         f.read_file (file);
70
71         _id = f.string_child ("Id");
72         if (_id.length() > 9) {
73                 _id = _id.substr (9);
74         }
75         _annotation_text = f.optional_string_child ("AnnotationText").get_value_or ("");
76         _metadata.issuer = f.optional_string_child ("Issuer").get_value_or ("");
77         _metadata.creator = f.optional_string_child ("Creator").get_value_or ("");
78         _metadata.issue_date = f.string_child ("IssueDate");
79         _content_title_text = f.string_child ("ContentTitleText");
80         _content_kind = content_kind_from_string (f.string_child ("ContentKind"));
81         shared_ptr<cxml::Node> content_version = f.optional_node_child ("ContentVersion");
82         if (content_version) {
83                 _content_version_id = content_version->optional_string_child ("Id").get_value_or ("");
84                 _content_version_label_text = content_version->string_child ("LabelText");
85                 content_version->done ();
86         }
87         f.ignore_child ("RatingList");
88         _reels = type_grand_children<Reel> (f, "ReelList", "Reel");
89
90         f.ignore_child ("Issuer");
91         f.ignore_child ("Signer");
92         f.ignore_child ("Signature");
93
94         f.done ();
95 }
96
97 /** Add a reel to this CPL.
98  *  @param reel Reel to add.
99  */
100 void
101 CPL::add (boost::shared_ptr<Reel> reel)
102 {
103         _reels.push_back (reel);
104 }
105
106 /** Write an CompositonPlaylist XML file.
107  *  @param file Filename to write.
108  *  @param standard INTEROP or SMPTE.
109  *  @param signer Signer to sign the CPL, or 0 to add no signature.
110  */
111 void
112 CPL::write_xml (boost::filesystem::path file, Standard standard, shared_ptr<const Signer> signer) const
113 {
114         xmlpp::Document doc;
115         xmlpp::Element* root;
116         if (standard == INTEROP) {
117                 root = doc.create_root_node ("CompositionPlaylist", "http://www.digicine.com/PROTO-ASDCP-CPL-20040511#");
118         } else {
119                 root = doc.create_root_node ("CompositionPlaylist", "http://www.smpte-ra.org/schemas/429-7/2006/CPL");
120         }
121
122         if (signer) {
123                 root->set_namespace_declaration ("http://www.w3.org/2000/09/xmldsig#", "dsig");
124         }
125         
126         root->add_child("Id")->add_child_text ("urn:uuid:" + _id);
127         root->add_child("AnnotationText")->add_child_text (_annotation_text);
128         root->add_child("IssueDate")->add_child_text (_metadata.issue_date);
129         root->add_child("Issuer")->add_child_text (_metadata.issuer);
130         root->add_child("Creator")->add_child_text (_metadata.creator);
131         root->add_child("ContentTitleText")->add_child_text (_content_title_text);
132         root->add_child("ContentKind")->add_child_text (content_kind_to_string (_content_kind));
133         {
134                 xmlpp::Node* cv = root->add_child ("ContentVersion");
135                 cv->add_child ("Id")->add_child_text (_content_version_id);
136                 cv->add_child ("LabelText")->add_child_text (_content_version_label_text);
137         }
138         root->add_child("RatingList");
139
140         xmlpp::Element* reel_list = root->add_child ("ReelList");
141         
142         for (list<shared_ptr<Reel> >::const_iterator i = _reels.begin(); i != _reels.end(); ++i) {
143                 (*i)->write_to_cpl (reel_list, standard);
144         }
145
146         if (signer) {
147                 signer->sign (root, standard);
148         }
149
150         /* This must not be the _formatted version otherwise signature digests will be wrong */
151         doc.write_to_file (file.string (), "UTF-8");
152
153         set_file (file);
154 }
155
156 list<shared_ptr<const ReelAsset> >
157 CPL::reel_assets () const
158 {
159         list<shared_ptr<const ReelAsset> > c;
160
161         for (list<shared_ptr<Reel> >::const_iterator i = _reels.begin(); i != _reels.end(); ++i) {
162                 if ((*i)->main_picture ()) {
163                         c.push_back ((*i)->main_picture());
164                 }
165                 if ((*i)->main_sound ()) {
166                         c.push_back ((*i)->main_sound());
167                 }
168                 if ((*i)->main_subtitle ()) {
169                         c.push_back ((*i)->main_subtitle());
170                 }
171         }
172
173         return c;
174 }
175         
176 bool
177 CPL::equals (CPL const & other, EqualityOptions opt, boost::function<void (NoteType, string)> note) const
178 {
179         if (_annotation_text != other._annotation_text && !opt.cpl_annotation_texts_can_differ) {
180                 stringstream s;
181                 s << "annotation texts differ: " << _annotation_text << " vs " << other._annotation_text << "\n";
182                 note (DCP_ERROR, s.str ());
183                 return false;
184         }
185
186         if (_content_kind != other._content_kind) {
187                 note (DCP_ERROR, "content kinds differ");
188                 return false;
189         }
190
191         if (_reels.size() != other._reels.size()) {
192                 note (DCP_ERROR, String::compose ("reel counts differ (%1 vs %2)", _reels.size(), other._reels.size()));
193                 return false;
194         }
195         
196         list<shared_ptr<Reel> >::const_iterator a = _reels.begin ();
197         list<shared_ptr<Reel> >::const_iterator b = other._reels.begin ();
198         
199         while (a != _reels.end ()) {
200                 if (!(*a)->equals (*b, opt, note)) {
201                         return false;
202                 }
203                 ++a;
204                 ++b;
205         }
206
207         return true;
208 }
209
210 /** @return true if we have any encrypted content */
211 bool
212 CPL::encrypted () const
213 {
214         for (list<shared_ptr<Reel> >::const_iterator i = _reels.begin(); i != _reels.end(); ++i) {
215                 if ((*i)->encrypted ()) {
216                         return true;
217                 }
218         }
219
220         return false;
221 }
222
223 /** Add a KDM to this CPL.  If the KDM is for any of this CPLs assets it will be used
224  *  to decrypt those assets.
225  *  @param kdm KDM.
226  */
227 void
228 CPL::add (DecryptedKDM const & kdm)
229 {
230         for (list<shared_ptr<Reel> >::const_iterator i = _reels.begin(); i != _reels.end(); ++i) {
231                 (*i)->add (kdm);
232         }
233 }
234
235 /** Set a private key for every MXF referenced by this CPL.  This will allow the data
236  *  to be decrypted or encrypted.
237  *  @param key Key to use.
238  */
239 void
240 CPL::set_mxf_keys (Key key)
241 {
242         for (list<shared_ptr<Reel> >::const_iterator i = _reels.begin(); i != _reels.end(); ++i) {
243                 (*i)->set_mxf_keys (key);
244         }
245 }
246
247 void
248 CPL::resolve_refs (list<shared_ptr<Object> > objects)
249 {
250         for (list<shared_ptr<Reel> >::const_iterator i = _reels.begin(); i != _reels.end(); ++i) {
251                 (*i)->resolve_refs (objects);
252         }
253 }
254
255 string
256 CPL::pkl_type (Standard standard) const
257 {
258         switch (standard) {
259         case INTEROP:
260                 return "text/xml;asdcpKind=CPL";
261         case SMPTE:
262                 return "text/xml";
263         default:
264                 assert (false);
265         }
266 }
267