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