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