Some comments.
[libdcp.git] / src / dcp.cc
index 99cc517a7c8c274c0a79301b3595b94b5a05f0e7..c0438ebde646b7f680e4d5b3638daf23ebf74ccf 100644 (file)
@@ -32,6 +32,7 @@
 #include "asset.h"
 #include "sound_asset.h"
 #include "picture_asset.h"
+#include "subtitle_asset.h"
 #include "util.h"
 #include "metadata.h"
 #include "exceptions.h"
@@ -237,89 +238,41 @@ DCP::write_assetmap (string cpl_uuid, int cpl_length, string pkl_uuid, int pkl_l
 DCP::DCP (string directory)
        : _directory (directory)
 {
-       string cpl_file;
-       string pkl_file;
-       string asset_map_file;
+       Files files;
+       scan (files, directory);
 
-       for (filesystem::directory_iterator i = filesystem::directory_iterator(directory); i != filesystem::directory_iterator(); ++i) {
-               
-               string const t = i->path().string ();
-
-               if (ends_with (t, ".mxf")) {
-                       continue;
-               }
-
-               xmlpp::DomParser* p = new xmlpp::DomParser;
-
-               try {
-                       p->parse_file (t);
-               } catch (std::exception& e) {
-                       delete p;
-                       continue;
-               }
-               
-               if (!p) {
-                       delete p;
-                       continue;
-               }
-
-               string const root = p->get_document()->get_root_node()->get_name ();
-               delete p;
-               
-               if (root == "CompositionPlaylist") {
-                       if (cpl_file.empty ()) {
-                               cpl_file = t;
-                       } else {
-                               throw DCPReadError ("duplicate CPLs found");
-                       }
-               } else if (root == "PackingList") {
-                       if (pkl_file.empty ()) {
-                               pkl_file = t;
-                       } else {
-                               throw DCPReadError ("duplicate PKLs found");
-                       }
-               } else if (root == "AssetMap") {
-                       if (asset_map_file.empty ()) {
-                               asset_map_file = t;
-                       } else {
-                               throw DCPReadError ("duplicate AssetMaps found");
-                       }
-                       asset_map_file = t;
-               }
-       }
-
-       if (cpl_file.empty ()) {
+       if (files.cpl.empty ()) {
                throw FileError ("no CPL file found", "");
        }
 
-       if (pkl_file.empty ()) {
+       if (files.pkl.empty ()) {
                throw FileError ("no PKL file found", "");
        }
 
-       if (asset_map_file.empty ()) {
+       if (files.asset_map.empty ()) {
                throw FileError ("no AssetMap file found", "");
        }
 
        /* Read the XML */
        shared_ptr<CPL> cpl;
        try {
-               cpl.reset (new CPL (cpl_file));
+               cpl.reset (new CPL (files.cpl));
        } catch (FileError& e) {
-               throw FileError ("could not load CPL file", cpl_file);
+               throw FileError ("could not load CPL file", files.cpl);
        }
 
        shared_ptr<PKL> pkl;
        try {
-               pkl.reset (new PKL (pkl_file));
+               pkl.reset (new PKL (files.pkl));
        } catch (FileError& e) {
-               throw FileError ("could not load PKL file", pkl_file);
+               throw FileError ("could not load PKL file", files.pkl);
        }
 
        shared_ptr<AssetMap> asset_map;
        try {
-               asset_map.reset (new AssetMap (asset_map_file));
+               asset_map.reset (new AssetMap (files.asset_map));
        } catch (FileError& e) {
-               throw FileError ("could not load AssetMap file", asset_map_file);
+               throw FileError ("could not load AssetMap file", files.asset_map);
        }
 
        /* Cross-check */
@@ -336,9 +289,9 @@ DCP::DCP (string directory)
        _fps = cpl_assets->main_picture->frame_rate.numerator;
        _length = cpl_assets->main_picture->duration;
 
-       string n = cpl_assets->main_picture->annotation_text;
+       string n = pkl->asset_from_id(cpl_assets->main_picture->id)->original_file_name;
        if (n.empty ()) {
-               n = pkl->asset_from_id(cpl_assets->main_picture->id)->original_file_name;
+               n = cpl_assets->main_picture->annotation_text;
        }
 
        _assets.push_back (shared_ptr<PictureAsset> (
@@ -346,18 +299,17 @@ DCP::DCP (string directory)
                                           _directory,
                                           n,
                                           _fps,
-                                          _length,
-                                          cpl_assets->main_picture->screen_aspect_ratio.numerator,
-                                          cpl_assets->main_picture->screen_aspect_ratio.denominator
+                                          _length
                                           )
                                   ));
 
-       n = cpl_assets->main_sound->annotation_text;
-       if (n.empty ()) {
+       if (cpl_assets->main_sound) {
+
                n = pkl->asset_from_id(cpl_assets->main_sound->id)->original_file_name;
-       }
+               if (n.empty ()) {
+                       n = cpl_assets->main_sound->annotation_text;
+               }
        
-       if (cpl_assets->main_sound) {
                _assets.push_back (shared_ptr<SoundAsset> (
                                           new SoundAsset (
                                                   _directory,
@@ -367,8 +319,73 @@ DCP::DCP (string directory)
                                                   )
                                           ));
        }
+
+       for (list<string>::iterator i = files.subtitles.begin(); i != files.subtitles.end(); ++i) {
+               string const l = i->substr (_directory.length ());
+               _assets.push_back (shared_ptr<SubtitleAsset> (new SubtitleAsset (_directory, l)));
+       }
+}
+
+
+void
+DCP::scan (Files& files, string directory) const
+{
+       for (filesystem::directory_iterator i = filesystem::directory_iterator(directory); i != filesystem::directory_iterator(); ++i) {
+               
+               string const t = i->path().string ();
+
+               if (filesystem::is_directory (*i)) {
+                       scan (files, t);
+                       continue;
+               }
+
+               if (ends_with (t, ".mxf") || ends_with (t, ".ttf")) {
+                       continue;
+               }
+
+               xmlpp::DomParser* p = new xmlpp::DomParser;
+
+               try {
+                       p->parse_file (t);
+               } catch (std::exception& e) {
+                       delete p;
+                       continue;
+               }
+               
+               if (!p) {
+                       delete p;
+                       continue;
+               }
+
+               string const root = p->get_document()->get_root_node()->get_name ();
+               delete p;
+               
+               if (root == "CompositionPlaylist") {
+                       if (files.cpl.empty ()) {
+                               files.cpl = t;
+                       } else {
+                               throw DCPReadError ("duplicate CPLs found");
+                       }
+               } else if (root == "PackingList") {
+                       if (files.pkl.empty ()) {
+                               files.pkl = t;
+                       } else {
+                               throw DCPReadError ("duplicate PKLs found");
+                       }
+               } else if (root == "AssetMap") {
+                       if (files.asset_map.empty ()) {
+                               files.asset_map = t;
+                       } else {
+                               throw DCPReadError ("duplicate AssetMaps found");
+                       }
+                       files.asset_map = t;
+               } else if (root == "DCSubtitle") {
+                       files.subtitles.push_back (t);
+               }
+       }
 }
 
+
 list<string>
 DCP::equals (DCP const & other, EqualityOptions opt) const
 {
@@ -418,3 +435,29 @@ DCP::picture_asset () const
 
        return shared_ptr<const PictureAsset> ();
 }
+
+shared_ptr<const SoundAsset>
+DCP::sound_asset () const
+{
+       for (list<shared_ptr<Asset> >::const_iterator i = _assets.begin(); i != _assets.end(); ++i) {
+               shared_ptr<SoundAsset> s = dynamic_pointer_cast<SoundAsset> (*i);
+               if (s) {
+                       return s;
+               }
+       }
+
+       return shared_ptr<const SoundAsset> ();
+}
+
+shared_ptr<const SubtitleAsset>
+DCP::subtitle_asset () const
+{
+       for (list<shared_ptr<Asset> >::const_iterator i = _assets.begin(); i != _assets.end(); ++i) {
+               shared_ptr<SubtitleAsset> s = dynamic_pointer_cast<SubtitleAsset> (*i);
+               if (s) {
+                       return s;
+               }
+       }
+
+       return shared_ptr<const SubtitleAsset> ();
+}