Some comments.
[libdcp.git] / src / dcp.cc
index ca2a2d143e933fb188957f4e7b8f0a8967dfe5bb..c0438ebde646b7f680e4d5b3638daf23ebf74ccf 100644 (file)
 
 */
 
+/** @file  src/dcp.cc
+ *  @brief A class to create a DCP.
+ */
+
 #include <sstream>
 #include <fstream>
 #include <iomanip>
 #include <cassert>
+#include <iostream>
 #include <boost/filesystem.hpp>
+#include <libxml++/libxml++.h>
 #include "dcp.h"
 #include "asset.h"
 #include "sound_asset.h"
 #include "picture_asset.h"
+#include "subtitle_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)
 {
@@ -44,21 +55,27 @@ DCP::DCP (string directory, string name, ContentType content_type, int fps, int
 }
 
 void
-DCP::add_sound_asset (list<string> const & files)
+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_picture_asset (list<string> const & files, int w, int h)
+DCP::add_sound_asset (sigc::slot<string, Channel> get_path, int channels)
 {
-       filesystem::path p;
-       p /= _directory;
-       p /= "video.mxf";
-       _assets.push_back (shared_ptr<PictureAsset> (new PictureAsset (files, p.string(), &Progress, _fps, _length, w, h)));
+       _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)
+{
+       _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)
+{
+       _assets.push_back (shared_ptr<PictureAsset> (new PictureAsset (get_path, _directory, "video.mxf", &Progress, _fps, _length, width, height)));
 }
 
 void
@@ -93,7 +110,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"
@@ -217,32 +234,230 @@ DCP::write_assetmap (string cpl_uuid, int cpl_length, string pkl_uuid, int pkl_l
           << "</AssetMap>\n";
 }
 
-string
-DCP::content_type_string (ContentType type)
-{
-       switch (type) {
-       case FEATURE:
-               return "feature";
-       case SHORT:
-               return "short";
-       case TRAILER:
-               return "trailer";
-       case TEST:
-               return "test";
-       case TRANSITIONAL:
-               return "transitional";
-       case RATING:
-               return "rating";
-       case TEASER:
-               return "teaser";
-       case POLICY:
-               return "policy";
-       case PUBLIC_SERVICE_ANNOUNCEMENT:
-               return "psa";
-       case ADVERTISEMENT:
-               return "advertisement";
-       }
-
-       assert (false);
+
+DCP::DCP (string directory)
+       : _directory (directory)
+{
+       Files files;
+       scan (files, directory);
+
+       if (files.cpl.empty ()) {
+               throw FileError ("no CPL file found", "");
+       }
+
+       if (files.pkl.empty ()) {
+               throw FileError ("no PKL file found", "");
+       }
+
+       if (files.asset_map.empty ()) {
+               throw FileError ("no AssetMap file found", "");
+       }
+
+       /* Read the XML */
+       shared_ptr<CPL> cpl;
+       try {
+               cpl.reset (new CPL (files.cpl));
+       } catch (FileError& e) {
+               throw FileError ("could not load CPL file", files.cpl);
+       }
+
+       shared_ptr<PKL> pkl;
+       try {
+               pkl.reset (new PKL (files.pkl));
+       } catch (FileError& e) {
+               throw FileError ("could not load PKL file", files.pkl);
+       }
+
+       shared_ptr<AssetMap> asset_map;
+       try {
+               asset_map.reset (new AssetMap (files.asset_map));
+       } catch (FileError& e) {
+               throw FileError ("could not load AssetMap file", files.asset_map);
+       }
+
+       /* 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;
+
+       string n = pkl->asset_from_id(cpl_assets->main_picture->id)->original_file_name;
+       if (n.empty ()) {
+               n = cpl_assets->main_picture->annotation_text;
+       }
+
+       _assets.push_back (shared_ptr<PictureAsset> (
+                                  new PictureAsset (
+                                          _directory,
+                                          n,
+                                          _fps,
+                                          _length
+                                          )
+                                  ));
+
+       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;
+               }
+       
+               _assets.push_back (shared_ptr<SoundAsset> (
+                                          new SoundAsset (
+                                                  _directory,
+                                                  n,
+                                                  _fps,
+                                                  _length
+                                                  )
+                                          ));
+       }
+
+       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
+{
+       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");
+               }
+       }
+
+       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;
+}
+
+shared_ptr<const PictureAsset>
+DCP::picture_asset () const
+{
+       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;
+               }
+       }
+
+       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> ();
+}