summaryrefslogtreecommitdiff
path: root/src/dc_package.cc
blob: ef016f632d324dbddcedfa3a71de36c4b9444f23 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
    Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

/** @file  src/dcp.cc
 *  @brief DCP class.
 */

#include "raw_convert.h"
#include "dc_package.h"
#include "sound_asset.h"
#include "picture_asset.h"
#include "interop_subtitle_asset.h"
#include "smpte_subtitle_asset.h"
#include "mono_picture_asset.h"
#include "stereo_picture_asset.h"
#include "reel_subtitle_asset.h"
#include "util.h"
#include "metadata.h"
#include "exceptions.h"
#include "dc_cpl.h"
#include "certificate_chain.h"
#include "compose.hpp"
#include "AS_DCP.h"
#include "decrypted_kdm.h"
#include "decrypted_kdm_key.h"
#include "dcp_assert.h"
#include "reel_asset.h"
#include "font_asset.h"
#include "package.h"
#include <xmlsec/xmldsig.h>
#include <xmlsec/app.h>
#include <libxml++/libxml++.h>
#include <boost/filesystem.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/foreach.hpp>
#include <iostream>

using std::string;
using std::list;
using std::cout;
using std::ostream;
using std::make_pair;
using std::map;
using std::cout;
using std::cerr;
using std::exception;
using boost::shared_ptr;
using boost::dynamic_pointer_cast;
using boost::algorithm::starts_with;
using namespace dcp::dc;

Package::Package (boost::filesystem::path directory)
	: dcp::Package<CPL> (directory)
{

}

bool
Package::equals (Package const & other, dcp::EqualityOptions opt, dcp::NoteHandler note) const
{
	list<shared_ptr<CPL> > a = cpls ();
	list<shared_ptr<CPL> > b = other.cpls ();

	if (a.size() != b.size()) {
		note (DCP_ERROR, String::compose ("CPL counts differ: %1 vs %2", a.size(), b.size()));
		return false;
	}

	bool r = true;

	BOOST_FOREACH (shared_ptr<CPL> i, a) {
		list<shared_ptr<CPL> >::const_iterator j = b.begin ();
		while (j != b.end() && !(*j)->equals (i, opt, note)) {
			++j;
		}

		if (j == b.end ()) {
			r = false;
		}
	}

	return r;
}

bool
Package::encrypted () const
{
	BOOST_FOREACH (shared_ptr<CPL> i, cpls ()) {
		if (i->encrypted ()) {
			return true;
		}
	}

	return false;
}

void
Package::add_kdm (DecryptedKDM const & kdm)
{
	list<DecryptedKDMKey> keys = kdm.keys ();

	BOOST_FOREACH (shared_ptr<CPL> i, cpls ()) {
		BOOST_FOREACH (DecryptedKDMKey const & j, kdm.keys ()) {
			if (j.cpl_id() == i->id()) {
				i->add (kdm);
			}
		}
	}
}

/** Write all the XML files for this DCP.
 *  @param standand INTEROP or SMPTE.
 *  @param metadata Metadata to use for PKL and asset map files.
 *  @param signer Signer to use, or 0.
 */
void
Package::write_xml (
	Standard standard,
	dcp::XMLMetadata metadata,
	shared_ptr<const dcp::CertificateChain> signer
	)
{
	do_write_xml (standard, metadata, signer);
}

/** @return All assets (including CPLs) */
list<shared_ptr<dcp::Asset> >
Package::assets () const
{
	list<shared_ptr<dcp::Asset> > assets;
	BOOST_FOREACH (shared_ptr<CPL> i, cpls ()) {
		assets.push_back (i);
		BOOST_FOREACH (shared_ptr<const ReelAsset> j, i->reel_assets ()) {
			shared_ptr<dcp::Asset> o = j->asset_ref().asset ();
			assets.push_back (o);
			/* More Interop special-casing */
			shared_ptr<InteropSubtitleAsset> sub = dynamic_pointer_cast<InteropSubtitleAsset> (o);
			if (sub) {
				sub->add_font_assets (assets);
			}
		}
	}

	return assets;
}

shared_ptr<dcp::Asset>
Package::xml_asset_factory (boost::filesystem::path file, string root) const
{
	shared_ptr<dcp::Asset> asset;

	if (root == "CompositionPlaylist") {
		asset.reset (new CPL (file));
	} else if (root == "DCSubtitle") {
		asset.reset (new InteropSubtitleAsset (file));
	}

	return asset;
}

shared_ptr<dcp::Asset>
Package::mxf_asset_factory (boost::filesystem::path file) const
{
	shared_ptr<dcp::Asset> asset;

	ASDCP::EssenceType_t type;
	if (ASDCP::EssenceType (file.string().c_str(), type) != ASDCP::RESULT_OK) {
		throw PackageReadError ("Could not find essence type");
	}

	switch (type) {
	case ASDCP::ESS_UNKNOWN:
	case ASDCP::ESS_MPEG2_VES:
		throw PackageReadError ("MPEG2 video essences are not supported");
	case ASDCP::ESS_JPEG_2000:
		asset.reset (new MonoPictureAsset (file));
		break;
	case ASDCP::ESS_PCM_24b_48k:
	case ASDCP::ESS_PCM_24b_96k:
		asset.reset (new SoundAsset (file));
		break;
	case ASDCP::ESS_JPEG_2000_S:
		asset.reset (new StereoPictureAsset (file));
		break;
	case ASDCP::ESS_TIMED_TEXT:
		asset.reset (new SMPTESubtitleAsset (file));
		break;
	default:
		throw PackageReadError ("Unknown MXF essence type");
	}

	return asset;
}

string
Package::pkl_annotation_text () const
{
	DCP_ASSERT (!_cpls.empty ());
	return _cpls.front()->annotation_text ();
}