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