+ if (!boost::filesystem::exists (directory)) {
+ boost::filesystem::create_directories (directory);
+ }
+
+ _directory = boost::filesystem::canonical (_directory);
+}
+
+/** Call this instead of throwing an exception if the error can be tolerated */
+template<class T> void
+survivable_error (bool keep_going, dcp::DCP::ReadErrors* errors, T const & e)
+{
+ if (keep_going) {
+ if (errors) {
+ errors->push_back (shared_ptr<T> (new T (e)));
+ }
+ } else {
+ throw e;
+ }
+}
+
+void
+DCP::read (bool keep_going, ReadErrors* errors, bool ignore_incorrect_picture_mxf_type)
+{
+ /* Read the ASSETMAP */
+
+ boost::filesystem::path asset_map_file;
+ if (boost::filesystem::exists (_directory / "ASSETMAP")) {
+ asset_map_file = _directory / "ASSETMAP";
+ } else if (boost::filesystem::exists (_directory / "ASSETMAP.xml")) {
+ asset_map_file = _directory / "ASSETMAP.xml";
+ } else {
+ boost::throw_exception (DCPReadError (String::compose ("could not find AssetMap file in `%1'", _directory.string())));
+ }
+
+ cxml::Document asset_map ("AssetMap");
+
+ asset_map.read_file (asset_map_file);
+ if (asset_map.namespace_uri() == assetmap_interop_ns) {
+ _standard = INTEROP;
+ } else if (asset_map.namespace_uri() == assetmap_smpte_ns) {
+ _standard = SMPTE;
+ } else {
+ boost::throw_exception (XMLError ("Unrecognised Assetmap namespace " + asset_map.namespace_uri()));
+ }
+
+ list<shared_ptr<cxml::Node> > asset_nodes = asset_map.node_child("AssetList")->node_children ("Asset");
+ map<string, boost::filesystem::path> paths;
+ BOOST_FOREACH (shared_ptr<cxml::Node> i, asset_nodes) {
+ if (i->node_child("ChunkList")->node_children("Chunk").size() != 1) {
+ boost::throw_exception (XMLError ("unsupported asset chunk count"));
+ }
+ string p = i->node_child("ChunkList")->node_child("Chunk")->string_child ("Path");
+ if (starts_with (p, "file://")) {
+ p = p.substr (7);
+ }
+ paths.insert (make_pair (remove_urn_uuid (i->string_child ("Id")), p));
+ }
+
+ /* Read all the assets from the asset map */
+ /* XXX: I think we should be looking at the PKL here to decide type, not
+ the extension of the file.
+ */
+
+ /* Make a list of non-CPL assets so that we can resolve the references
+ from the CPLs.
+ */
+ list<shared_ptr<Asset> > other_assets;
+
+ for (map<string, boost::filesystem::path>::const_iterator i = paths.begin(); i != paths.end(); ++i) {
+ boost::filesystem::path path = _directory / i->second;
+
+ if (!boost::filesystem::exists (path)) {
+ survivable_error (keep_going, errors, MissingAssetError (path));
+ continue;
+ }
+
+ if (boost::filesystem::extension (path) == ".xml") {
+ xmlpp::DomParser* p = new xmlpp::DomParser;
+ try {
+ p->parse_file (path.string());
+ } catch (std::exception& e) {
+ delete p;
+ continue;
+ }
+
+ string const root = p->get_document()->get_root_node()->get_name ();
+ delete p;
+
+ if (root == "CompositionPlaylist") {
+ shared_ptr<CPL> cpl (new CPL (path));
+ if (_standard && cpl->standard() && cpl->standard().get() != _standard.get()) {
+ survivable_error (keep_going, errors, MismatchedStandardError ());
+ }
+ _cpls.push_back (cpl);
+ } else if (root == "DCSubtitle") {
+ if (_standard && _standard.get() == SMPTE) {
+ survivable_error (keep_going, errors, MismatchedStandardError ());
+ }
+ other_assets.push_back (shared_ptr<InteropSubtitleAsset> (new InteropSubtitleAsset (path)));
+ }
+ } else if (boost::filesystem::extension (path) == ".mxf") {
+
+ /* XXX: asdcplib does not appear to support discovery of read MXFs standard
+ (Interop / SMPTE)
+ */
+
+ ASDCP::EssenceType_t type;
+ if (ASDCP::EssenceType (path.string().c_str(), type) != ASDCP::RESULT_OK) {
+ throw DCPReadError ("Could not find essence type");
+ }
+ switch (type) {
+ case ASDCP::ESS_UNKNOWN:
+ case ASDCP::ESS_MPEG2_VES:
+ throw DCPReadError ("MPEG2 video essences are not supported");
+ case ASDCP::ESS_JPEG_2000:
+ try {
+ other_assets.push_back (shared_ptr<MonoPictureAsset> (new MonoPictureAsset (path)));
+ } catch (dcp::MXFFileError& e) {
+ if (ignore_incorrect_picture_mxf_type && e.number() == ASDCP::RESULT_SFORMAT) {
+ /* Tried to load it as mono but the error says it's stereo; try that instead */
+ other_assets.push_back (shared_ptr<StereoPictureAsset> (new StereoPictureAsset (path)));
+ } else {
+ throw;
+ }
+ }
+ break;
+ case ASDCP::ESS_PCM_24b_48k:
+ case ASDCP::ESS_PCM_24b_96k:
+ other_assets.push_back (shared_ptr<SoundAsset> (new SoundAsset (path)));
+ break;
+ case ASDCP::ESS_JPEG_2000_S:
+ other_assets.push_back (shared_ptr<StereoPictureAsset> (new StereoPictureAsset (path)));
+ break;
+ case ASDCP::ESS_TIMED_TEXT:
+ other_assets.push_back (shared_ptr<SMPTESubtitleAsset> (new SMPTESubtitleAsset (path)));
+ break;
+ default:
+ throw DCPReadError (String::compose ("Unknown MXF essence type %1 in %2", int(type), path.string()));
+ }
+ } else if (boost::filesystem::extension (path) == ".ttf") {
+ other_assets.push_back (shared_ptr<FontAsset> (new FontAsset (i->first, path)));
+ }
+ }
+
+ BOOST_FOREACH (shared_ptr<CPL> i, cpls ()) {
+ i->resolve_refs (other_assets);
+ }
+}
+
+void
+DCP::resolve_refs (list<shared_ptr<Asset> > assets)
+{
+ BOOST_FOREACH (shared_ptr<CPL> i, cpls ()) {
+ i->resolve_refs (assets);
+ }
+}
+
+bool
+DCP::equals (DCP const & other, EqualityOptions opt, 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;