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