2 Copyright (C) 2012-2016 Carl Hetherington <cth@carlh.net>
4 This file is part of libdcp.
6 libdcp is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 libdcp is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with libdcp. If not, see <http://www.gnu.org/licenses/>.
19 In addition, as a special exception, the copyright holders give
20 permission to link the code of portions of this program with the
21 OpenSSL library under certain conditions as described in each
22 individual source file, and distribute linked combinations
25 You must obey the GNU General Public License in all respects
26 for all of the code used other than OpenSSL. If you modify
27 file(s) with this exception, you may extend this exception to your
28 version of the file(s), but you are not obligated to do so. If you
29 do not wish to do so, delete this exception statement from your
30 version. If you delete this exception statement from all source
31 files in the program, then also delete it here.
38 #include "certificate_chain.h"
40 #include "reel_picture_asset.h"
41 #include "reel_sound_asset.h"
42 #include "reel_subtitle_asset.h"
43 #include "reel_closed_caption_asset.h"
44 #include "reel_atmos_asset.h"
45 #include "local_time.h"
46 #include "dcp_assert.h"
47 #include "compose.hpp"
48 #include <libxml/parser.h>
49 #include <libxml++/libxml++.h>
50 #include <boost/foreach.hpp>
57 using boost::shared_ptr;
58 using boost::optional;
59 using boost::dynamic_pointer_cast;
62 static string const cpl_interop_ns = "http://www.digicine.com/PROTO-ASDCP-CPL-20040511#";
63 static string const cpl_smpte_ns = "http://www.smpte-ra.org/schemas/429-7/2006/CPL";
65 CPL::CPL (string annotation_text, ContentKind content_kind)
66 /* default _content_title_text to annotation_text */
67 : _content_title_text (annotation_text)
68 , _content_kind (content_kind)
70 _metadata.annotation_text = annotation_text;
71 /* default _content_version_id to a random ID and _content_version_label to
72 a random ID and the current time.
74 string const uuid = make_uuid();
75 _content_version_id = "urn:uuid:" + uuid;
76 _content_version_label_text = uuid + LocalTime().as_string ();
79 /** Construct a CPL object from a XML file */
80 CPL::CPL (boost::filesystem::path file)
82 , _content_kind (FEATURE)
84 cxml::Document f ("CompositionPlaylist");
87 if (f.namespace_uri() == cpl_interop_ns) {
89 } else if (f.namespace_uri() == cpl_smpte_ns) {
92 boost::throw_exception (XMLError ("Unrecognised CPL namespace " + f.namespace_uri()));
95 _id = remove_urn_uuid (f.string_child ("Id"));
96 _metadata.annotation_text = f.optional_string_child ("AnnotationText").get_value_or ("");
97 _metadata.issuer = f.optional_string_child ("Issuer").get_value_or ("");
98 _metadata.creator = f.optional_string_child ("Creator").get_value_or ("");
99 _metadata.issue_date = f.string_child ("IssueDate");
100 _content_title_text = f.string_child ("ContentTitleText");
101 _content_kind = content_kind_from_string (f.string_child ("ContentKind"));
102 shared_ptr<cxml::Node> content_version = f.optional_node_child ("ContentVersion");
103 if (content_version) {
104 _content_version_id = content_version->optional_string_child ("Id").get_value_or ("");
105 _content_version_label_text = content_version->string_child ("LabelText");
106 content_version->done ();
108 f.ignore_child ("RatingList");
109 _reels = type_grand_children<Reel> (f, "ReelList", "Reel");
111 f.ignore_child ("Issuer");
112 f.ignore_child ("Signer");
113 f.ignore_child ("Signature");
118 /** Add a reel to this CPL.
119 * @param reel Reel to add.
122 CPL::add (boost::shared_ptr<Reel> reel)
124 _reels.push_back (reel);
127 /** Write an CompositonPlaylist XML file.
128 * @param file Filename to write.
129 * @param standard INTEROP or SMPTE.
130 * @param signer Signer to sign the CPL, or 0 to add no signature.
133 CPL::write_xml (boost::filesystem::path file, Standard standard, shared_ptr<const CertificateChain> signer) const
136 xmlpp::Element* root;
137 if (standard == INTEROP) {
138 root = doc.create_root_node ("CompositionPlaylist", cpl_interop_ns);
140 root = doc.create_root_node ("CompositionPlaylist", cpl_smpte_ns);
143 root->add_child("Id")->add_child_text ("urn:uuid:" + _id);
144 root->add_child("AnnotationText")->add_child_text (_metadata.annotation_text);
145 root->add_child("IssueDate")->add_child_text (_metadata.issue_date);
146 root->add_child("Issuer")->add_child_text (_metadata.issuer);
147 root->add_child("Creator")->add_child_text (_metadata.creator);
148 root->add_child("ContentTitleText")->add_child_text (_content_title_text);
149 root->add_child("ContentKind")->add_child_text (content_kind_to_string (_content_kind));
151 xmlpp::Node* cv = root->add_child ("ContentVersion");
152 cv->add_child ("Id")->add_child_text (_content_version_id);
153 cv->add_child ("LabelText")->add_child_text (_content_version_label_text);
155 root->add_child("RatingList");
157 xmlpp::Element* reel_list = root->add_child ("ReelList");
159 BOOST_FOREACH (shared_ptr<Reel> i, _reels) {
160 i->write_to_cpl (reel_list, standard);
166 signer->sign (root, standard);
169 doc.write_to_file_formatted (file.string(), "UTF-8");
174 list<shared_ptr<ReelAsset> >
177 list<shared_ptr<ReelAsset> > c;
179 BOOST_FOREACH (shared_ptr<Reel> i, _reels) {
180 if (i->main_picture ()) {
181 c.push_back (i->main_picture());
183 if (i->main_sound ()) {
184 c.push_back (i->main_sound());
186 if (i->main_subtitle ()) {
187 c.push_back (i->main_subtitle());
189 BOOST_FOREACH (shared_ptr<ReelClosedCaptionAsset> j, i->closed_captions()) {
193 c.push_back (i->atmos());
200 list<shared_ptr<const ReelAsset> >
201 CPL::reel_assets () const
203 list<shared_ptr<const ReelAsset> > c;
205 BOOST_FOREACH (shared_ptr<Reel> i, _reels) {
206 if (i->main_picture ()) {
207 c.push_back (i->main_picture());
209 if (i->main_sound ()) {
210 c.push_back (i->main_sound());
212 if (i->main_subtitle ()) {
213 c.push_back (i->main_subtitle());
215 BOOST_FOREACH (shared_ptr<ReelClosedCaptionAsset> j, i->closed_captions()) {
219 c.push_back (i->atmos());
227 CPL::equals (shared_ptr<const Asset> other, EqualityOptions opt, NoteHandler note) const
229 shared_ptr<const CPL> other_cpl = dynamic_pointer_cast<const CPL> (other);
234 if (_metadata.annotation_text != other_cpl->_metadata.annotation_text && !opt.cpl_annotation_texts_can_differ) {
235 string const s = "CPL: annotation texts differ: " + _metadata.annotation_text + " vs " + other_cpl->_metadata.annotation_text + "\n";
240 if (_content_kind != other_cpl->_content_kind) {
241 note (DCP_ERROR, "CPL: content kinds differ");
245 if (_reels.size() != other_cpl->_reels.size()) {
246 note (DCP_ERROR, String::compose ("CPL: reel counts differ (%1 vs %2)", _reels.size(), other_cpl->_reels.size()));
250 list<shared_ptr<Reel> >::const_iterator a = _reels.begin ();
251 list<shared_ptr<Reel> >::const_iterator b = other_cpl->_reels.begin ();
253 while (a != _reels.end ()) {
254 if (!(*a)->equals (*b, opt, note)) {
264 /** @return true if we have any encrypted content */
266 CPL::encrypted () const
268 BOOST_FOREACH (shared_ptr<Reel> i, _reels) {
269 if (i->encrypted ()) {
277 /** Add a KDM to this CPL. If the KDM is for any of this CPLs assets it will be used
278 * to decrypt those assets.
282 CPL::add (DecryptedKDM const & kdm)
284 BOOST_FOREACH (shared_ptr<Reel> i, _reels) {
290 CPL::resolve_refs (list<shared_ptr<Asset> > assets)
292 BOOST_FOREACH (shared_ptr<Reel> i, _reels) {
293 i->resolve_refs (assets);
298 CPL::pkl_type (Standard standard) const
300 return static_pkl_type (standard);
304 CPL::static_pkl_type (Standard standard)
308 return "text/xml;asdcpKind=CPL";
317 CPL::duration () const
320 BOOST_FOREACH (shared_ptr<Reel> i, _reels) {