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