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