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