Misc fixes.
[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         _issue_date = f.string_child ("IssueDate");
78         _creator = f.optional_string_child ("Creator").get_value_or ("");
79         _content_title_text = f.string_child ("ContentTitleText");
80         _content_kind = content_kind_from_string (f.string_child ("ContentKind"));
81         shared_ptr<cxml::Node> content_version = f.optional_node_child ("ContentVersion");
82         if (content_version) {
83                 _content_version_id = content_version->optional_string_child ("Id").get_value_or ("");
84                 _content_version_label_text = content_version->string_child ("LabelText");
85                 content_version->done ();
86         }
87         f.ignore_child ("RatingList");
88         _reels = type_grand_children<Reel> (f, "ReelList", "Reel");
89
90         f.ignore_child ("Issuer");
91         f.ignore_child ("Signer");
92         f.ignore_child ("Signature");
93
94         f.done ();
95 }
96
97 /** Add a reel to this CPL.
98  *  @param reel Reel to add.
99  */
100 void
101 CPL::add (boost::shared_ptr<Reel> reel)
102 {
103         _reels.push_back (reel);
104 }
105
106 void
107 CPL::write_xml (boost::filesystem::path file, Standard standard, XMLMetadata metadata, shared_ptr<const Signer> signer) const
108 {
109         xmlpp::Document doc;
110         xmlpp::Element* root;
111         if (standard == INTEROP) {
112                 root = doc.create_root_node ("CompositionPlaylist", "http://www.digicine.com/PROTO-ASDCP-CPL-20040511#");
113         } else {
114                 root = doc.create_root_node ("CompositionPlaylist", "http://www.smpte-ra.org/schemas/429-7/2006/CPL");
115         }
116
117         if (signer) {
118                 root->set_namespace_declaration ("http://www.w3.org/2000/09/xmldsig#", "dsig");
119         }
120         
121         root->add_child("Id")->add_child_text ("urn:uuid:" + _id);
122         root->add_child("AnnotationText")->add_child_text (_annotation_text);
123         root->add_child("IssueDate")->add_child_text (metadata.issue_date);
124         root->add_child("Issuer")->add_child_text (metadata.issuer);
125         root->add_child("Creator")->add_child_text (metadata.creator);
126         root->add_child("ContentTitleText")->add_child_text (_content_title_text);
127         root->add_child("ContentKind")->add_child_text (content_kind_to_string (_content_kind));
128         {
129                 xmlpp::Node* cv = root->add_child ("ContentVersion");
130                 cv->add_child ("Id")->add_child_text (_content_version_id);
131                 cv->add_child ("LabelText")->add_child_text (_content_version_label_text);
132         }
133         root->add_child("RatingList");
134
135         xmlpp::Element* reel_list = root->add_child ("ReelList");
136         
137         for (list<shared_ptr<Reel> >::const_iterator i = _reels.begin(); i != _reels.end(); ++i) {
138                 (*i)->write_to_cpl (reel_list, standard);
139         }
140
141         if (signer) {
142                 signer->sign (root, standard);
143         }
144
145         /* This must not be the _formatted version otherwise signature digests will be wrong */
146         doc.write_to_file (file.string (), "UTF-8");
147
148         set_file (file);
149 }
150
151 list<shared_ptr<const Content> >
152 CPL::content () const
153 {
154         list<shared_ptr<const Content> > c;
155         for (list<shared_ptr<Reel> >::const_iterator i = _reels.begin(); i != _reels.end(); ++i) {
156                 if ((*i)->main_picture ()) {
157                         c.push_back ((*i)->main_picture()->mxf ());
158                 }
159                 if ((*i)->main_sound ()) {
160                         c.push_back ((*i)->main_sound()->mxf ());
161                 }
162                 if ((*i)->main_subtitle ()) {
163                         c.push_back ((*i)->main_subtitle()->subtitle_content ());
164                 }
165         }
166
167         return c;
168 }
169         
170 bool
171 CPL::equals (CPL const & other, EqualityOptions opt, boost::function<void (NoteType, string)> note) const
172 {
173         if (_annotation_text != other._annotation_text && !opt.cpl_annotation_texts_can_differ) {
174                 stringstream s;
175                 s << "annotation texts differ: " << _annotation_text << " vs " << other._annotation_text << "\n";
176                 note (ERROR, s.str ());
177                 return false;
178         }
179
180         if (_content_kind != other._content_kind) {
181                 note (ERROR, "content kinds differ");
182                 return false;
183         }
184
185         if (_reels.size() != other._reels.size()) {
186                 note (ERROR, String::compose ("reel counts differ (%1 vs %2)", _reels.size(), other._reels.size()));
187                 return false;
188         }
189         
190         list<shared_ptr<Reel> >::const_iterator a = _reels.begin ();
191         list<shared_ptr<Reel> >::const_iterator b = other._reels.begin ();
192         
193         while (a != _reels.end ()) {
194                 if (!(*a)->equals (*b, opt, note)) {
195                         return false;
196                 }
197                 ++a;
198                 ++b;
199         }
200
201         return true;
202 }
203
204 /** @return true if we have any encrypted content */
205 bool
206 CPL::encrypted () const
207 {
208         for (list<shared_ptr<Reel> >::const_iterator i = _reels.begin(); i != _reels.end(); ++i) {
209                 if ((*i)->encrypted ()) {
210                         return true;
211                 }
212         }
213
214         return false;
215 }
216
217 void
218 CPL::add (KDM const & kdm)
219 {
220         for (list<shared_ptr<Reel> >::const_iterator i = _reels.begin(); i != _reels.end(); ++i) {
221                 (*i)->add (kdm);
222         }
223 }
224
225 /** Set a private key for every MXF referenced by this CPL.  This will allow the data
226  *  to be decrypted or encrypted.
227  *  @param key Key to use.
228  */
229 void
230 CPL::set_mxf_keys (Key key)
231 {
232         for (list<shared_ptr<Reel> >::const_iterator i = _reels.begin(); i != _reels.end(); ++i) {
233                 (*i)->set_mxf_keys (key);
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 }