Add comment.
[libdcp.git] / src / dcp.cc
index 8c74ea8e593e3ba2f4e1597249622a6e6e65f249..b6014941b823c03b3f37eb257e452a6473791342 100644 (file)
 #include "picture_asset.h"
 #include "util.h"
 #include "metadata.h"
+#include "exceptions.h"
+#include "cpl.h"
+#include "pkl.h"
+#include "asset_map.h"
 
 using namespace std;
 using namespace boost;
 using namespace libdcp;
 
-DCP::DCP (string directory, string name, ContentType content_type, int fps, int length)
+DCP::DCP (string directory, string name, ContentKind content_kind, int fps, int length)
        : _directory (directory)
        , _name (name)
-       , _content_type (content_type)
+       , _content_kind (content_kind)
        , _fps (fps)
        , _length (length)
 {
@@ -50,37 +54,25 @@ DCP::DCP (string directory, string name, ContentType content_type, int fps, int
 void
 DCP::add_sound_asset (vector<string> const & files)
 {
-       filesystem::path p;
-       p /= _directory;
-       p /= "audio.mxf";
-       _assets.push_back (shared_ptr<SoundAsset> (new SoundAsset (files, p.string(), &Progress, _fps, _length)));
+       _assets.push_back (shared_ptr<SoundAsset> (new SoundAsset (files, _directory, "audio.mxf", &Progress, _fps, _length)));
 }
 
 void
 DCP::add_sound_asset (sigc::slot<string, Channel> get_path, int channels)
 {
-       filesystem::path p;
-       p /= _directory;
-       p /= "audio.mxf";
-       _assets.push_back (shared_ptr<SoundAsset> (new SoundAsset (get_path, p.string(), &Progress, _fps, _length, channels)));
+       _assets.push_back (shared_ptr<SoundAsset> (new SoundAsset (get_path, _directory, "audio.mxf", &Progress, _fps, _length, channels)));
 }
 
 void
 DCP::add_picture_asset (vector<string> const & files, int width, int height)
 {
-       filesystem::path p;
-       p /= _directory;
-       p /= "video.mxf";
-       _assets.push_back (shared_ptr<PictureAsset> (new PictureAsset (files, p.string(), &Progress, _fps, _length, width, height)));
+       _assets.push_back (shared_ptr<PictureAsset> (new PictureAsset (files, _directory, "video.mxf", &Progress, _fps, _length, width, height)));
 }
 
 void
 DCP::add_picture_asset (sigc::slot<string, int> get_path, int width, int height)
 {
-       filesystem::path p;
-       p /= _directory;
-       p /= "video.mxf";
-       _assets.push_back (shared_ptr<PictureAsset> (new PictureAsset (get_path, p.string(), &Progress, _fps, _length, width, height)));
+       _assets.push_back (shared_ptr<PictureAsset> (new PictureAsset (get_path, _directory, "video.mxf", &Progress, _fps, _length, width, height)));
 }
 
 void
@@ -115,7 +107,7 @@ DCP::write_cpl (string cpl_uuid) const
            << "  <IssueDate>" << Metadata::instance()->issue_date << "</IssueDate>\n"
            << "  <Creator>" << Metadata::instance()->creator << "</Creator>\n"
            << "  <ContentTitleText>" << _name << "</ContentTitleText>\n"
-           << "  <ContentKind>" << content_type_string (_content_type) << "</ContentKind>\n"
+           << "  <ContentKind>" << content_kind_to_string (_content_kind) << "</ContentKind>\n"
            << "  <ContentVersion>\n"
            << "    <Id>urn:uri:" << cpl_uuid << "_" << Metadata::instance()->issue_date << "</Id>\n"
            << "    <LabelText>" << cpl_uuid << "_" << Metadata::instance()->issue_date << "</LabelText>\n"
@@ -243,148 +235,150 @@ DCP::write_assetmap (string cpl_uuid, int cpl_length, string pkl_uuid, int pkl_l
 DCP::DCP (string directory)
        : _directory (directory)
 {
-       string cpl;
-       string pkl;
-       string asset_map;
+       string cpl_file;
+       string pkl_file;
+       string asset_map_file;
 
        for (filesystem::directory_iterator i = filesystem::directory_iterator(directory); i != filesystem::directory_iterator(); ++i) {
-               string const t = i->path()->string ();
+               string const t = i->path().string ();
                if (ends_with (t, "_cpl.xml")) {
-                       if (cpl.empty ()) {
-                               cpl = t;
+                       if (cpl_file.empty ()) {
+                               cpl_file = t;
                        } else {
                                throw DCPReadError ("duplicate CPLs found");
                        }
                } else if (ends_with (t, "_pkl.xml")) {
-                       if (pkl.empty ()) {
-                               pkl = t;
+                       if (pkl_file.empty ()) {
+                               pkl_file = t;
                        } else {
                                throw DCPReadError ("duplicate PKLs found");
                        }
                } else if (ends_with (t, "ASSETMAP.xml")) {
-                       if (asset_map.empty ()) {
-                               asset_map = t;
+                       if (asset_map_file.empty ()) {
+                               asset_map_file = t;
                        } else {
                                throw DCPReadError ("duplicate AssetMaps found");
                        }
                }
        }
 
-       load_cpl (cpl);
-       load_pkl (pkl);
-       load_asset_map (asset_map);
-}
+       if (cpl_file.empty ()) {
+               throw FileError ("no CPL file found", "");
+       }
 
-void
-DCP::load_cpl (string file)
-{
-       xmlpp::DOMParser parser;
-       parser.parser_file (file);
-       if (!parser) {
-               throw DCPReadError ("could not create parser for CPL");
+       if (pkl_file.empty ()) {
+               throw FileError ("no PKL file found", "");
        }
 
-       xmlpp::Element* root = parser.get_document()->get_root_node ();
-       dcp_read_assert (root->get_name() == "CompositionPlaylist", "unrecognised CPL format");
-
-       xmlpp::Node::NodeList children = root->get_children ();
-       for (xmlpp::Node::NodeList::iterator i = children.begin(); i != children.end(); ++i) {
-               bool taken = false;
-               xml_maybe (*i, taken, _cpl_id, "Id");
-               xml_maybe (*i, taken, _annotation_text, "AnnotationText");
-               xml_maybe (*i, taken, _issue_date, "IssueDate");
-               xml_maybe (*i, taken, _creator, "Creator");
-               xml_maybe (*i, taken, _content_title_text, "ContentTitleText");
-               xml_maybe (*i, taken, _content_kind, "ContentKind");
-
-               if ((*i)->get_name() == "ContentVersion") {
-                       taken = true;
-                       load_cpl_content_version (*i);
-               }
+       if (asset_map_file.empty ()) {
+               throw FileError ("no AssetMap file found", "");
+       }
 
-               if ((*i)->get_name() == "RatingList") {
-                       taken = true;
-               }
+       /* Read the XML */
+       shared_ptr<CPL> cpl;
+       try {
+               cpl.reset (new CPL (cpl_file));
+       } catch (FileError& e) {
+               throw FileError ("could not load CPL file", cpl_file);
+       }
 
-               if ((*i)->get_name() == "ReelList") {
-                       taken = true;
-                       load_cpl_reel_list (*i);
-               }
+       shared_ptr<PKL> pkl;
+       try {
+               pkl.reset (new PKL (pkl_file));
+       } catch (FileError& e) {
+               throw FileError ("could not load PKL file", pkl_file);
+       }
 
-               xml_assert_taken (*i, taken);
+       shared_ptr<AssetMap> asset_map;
+       try {
+               asset_map.reset (new AssetMap (asset_map_file));
+       } catch (FileError& e) {
+               throw FileError ("could not load AssetMap file", asset_map_file);
        }
-}
 
-void
-DCP::load_cpl_content_version (xmlpp::Node const * node)
-{
-       xmlpp::Node::NodeList children = node->get_children ();
-       for (xmlpp::Node::NodeList::iterator i = children.begin(); i != children.end(); ++i) {
-               bool taken = false;
-               xml_maybe (*i, taken, _content_version_id, "Id");
-               xml_maybe (*i, taken, _content_version_label_text, "LabelText");
-               xml_assert_taken (*i, taken);
+       /* Cross-check */
+       /* XXX */
+
+       /* Now cherry-pick the required bits into our own data structure */
+       
+       _name = cpl->annotation_text;
+       _content_kind = cpl->content_kind;
+
+       shared_ptr<CPLAssetList> cpl_assets = cpl->reels.front()->asset_list;
+
+       /* XXX */
+       _fps = cpl_assets->main_picture->frame_rate.numerator;
+       _length = cpl_assets->main_picture->duration;
+
+       _assets.push_back (shared_ptr<PictureAsset> (
+                                  new PictureAsset (
+                                          _directory,
+                                          cpl_assets->main_picture->annotation_text,
+                                          _fps,
+                                          _length,
+                                          cpl_assets->main_picture->screen_aspect_ratio.numerator,
+                                          cpl_assets->main_picture->screen_aspect_ratio.denominator
+                                          )
+                                  ));
+
+       if (cpl_assets->main_sound) {
+               _assets.push_back (shared_ptr<SoundAsset> (
+                                          new SoundAsset (
+                                                  _directory,
+                                                  cpl_assets->main_sound->annotation_text,
+                                                  _fps,
+                                                  _length
+                                                  )
+                                          ));
        }
 }
 
-void
-DCP::load_cpl_reel_list (xmlpp::Node const * node)
+list<string>
+DCP::equals (DCP const & other, EqualityOptions opt) const
 {
-       xmlpp::Node::NodeList children = node->get_children ();
-       bool had_reel = false;
-       for (xmlpp::Node::NodeList::iterator i = children.begin(); i != children.end(); ++i) {
-               bool taken = false;
-               if ((*i)->get_name() == "Reel") {
-                       if (!had_reel) {
-                               load_cpl_reel (*i);
-                               had_reel = true;
-                       } else {
-                               throw DCPReadError ("multiple reels not supported");
-                       }
+       list<string> notes;
+       
+       if (opt.flags & LIBDCP_METADATA) {
+               if (_name != other._name) {
+                       notes.push_back ("names differ");
+               }
+               if (_content_kind != other._content_kind) {
+                       notes.push_back ("content kinds differ");
+               }
+               if (_fps != other._fps) {
+                       notes.push_back ("frames per second differ");
+               }
+               if (_length != other._length) {
+                       notes.push_back ("lengths differ");
                }
-               xml_assert_taken (*i, taken);
        }
-}
 
-void
-DCP::load_cpl_reel (xmlpp::Node const * node)
-{
-       xmlpp::Node::NodeList children = node->get_children ();
-       for (xmlpp::Node::NodeList::iterator i = children.begin(); i != children.end(); ++i) {
-               bool taken = false;
-               xml_taken (*i, taken, _reel_id, "Id");
-               if ((*i)->name() == "AssetList") {
-                       taken = true;
-                       load_cpl_asset_list (*i);
-               }
-               xml_assert_taken (*i, taken);
+       if (_assets.size() != other._assets.size()) {
+               notes.push_back ("asset counts differ");
+       }
+       
+       list<shared_ptr<Asset> >::const_iterator a = _assets.begin ();
+       list<shared_ptr<Asset> >::const_iterator b = other._assets.begin ();
+       
+       while (a != _assets.end ()) {
+               list<string> n = (*a)->equals (*b, opt);
+               notes.merge (n);
+               ++a;
+               ++b;
        }
+
+       return notes;
 }
 
-void
-DCP::load_cpl_asset_list (xmlpp::Node const * node)
+shared_ptr<const PictureAsset>
+DCP::picture_asset () const
 {
-       xmlpp::Node::NodeList children = node->get_children ();
-       for (xmlpp::Node::NodeList::iterator i = children.begin(); i != children.end(); ++i) {
-               bool taken = false;
-               if ((*i)->name() == "MainPicture") {
-                       taken = true;
-                       load_cpl_main_picture (*i);
-               } else if ((*i)->name() == "MainSound") {
-                       taken = true;
-                       load_cpl_main_sound (*i);
+       for (list<shared_ptr<Asset> >::const_iterator i = _assets.begin(); i != _assets.end(); ++i) {
+               shared_ptr<PictureAsset> p = dynamic_pointer_cast<PictureAsset> (*i);
+               if (p) {
+                       return p;
                }
-               xml_assert_taken (*i, taken);
        }
-}
 
-void
-DCP::load_cpl_main_picture (xmlpp::Node const * node)
-{
-       xmlpp::Node::NodeList children = node->get_children ();
-       for (xmlpp::Node::NodeList::iterator i = children.begin(); i != children.end(); ++i) {
-               bool taken = false;
-               xml_maybe (*i, taken, _video_id, "Id");
-               
-       }
+       return shared_ptr<const PictureAsset> ();
 }