Some maths operations with Time.
[libdcp.git] / src / dcp.cc
index b691d1460d1ba07e28655a10faeb36bebd3f9ca8..3f04d904460a2c74e4f143d7ab6d7bf909f021dc 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 "tags.h"
+#include "metadata.h"
+#include "exceptions.h"
+#include "cpl.h"
+#include "pkl.h"
+#include "asset_map.h"
+#include "reel.h"
 
 using namespace std;
 using namespace boost;
 using namespace libdcp;
 
-/** Construct a DCP.
- *  @param d Directory to write files to.
- *  @param n Name.
- *  @param c Content type.
- *  @param fps Frames per second.
- *  @param length Length in frames.
- */
-DCP::DCP (string d, string n, ContentType c, int fps, int length)
-       : _directory (d)
-       , _name (n)
-       , _content_type (c)
+DCP::DCP (string directory, string name, ContentKind content_kind, int fps, int length)
+       : _directory (directory)
+       , _name (name)
+       , _content_kind (content_kind)
        , _fps (fps)
        , _length (length)
 {
+       
 }
 
-/** Add a sound asset.
- *  @param files Pathnames of WAV files to use in the order Left, Right, Centre, Lfe (sub), Left surround, Right surround.
- */
-void
-DCP::add_sound_asset (list<string> const & files)
-{
-       filesystem::path p;
-       p /= _directory;
-       p /= "audio.mxf";
-       _assets.push_back (shared_ptr<SoundAsset> (new SoundAsset (files, p.string(), _fps, _length)));
-}
-
-/** Add a picture asset.
- *  @param files Pathnames of JPEG2000 files, in frame order.
- */
 void
-DCP::add_picture_asset (list<string> const & files, int w, int h)
+DCP::add_reel (shared_ptr<const Reel> reel)
 {
-       filesystem::path p;
-       p /= _directory;
-       p /= "video.mxf";
-       _assets.push_back (shared_ptr<PictureAsset> (new PictureAsset (files, p.string(), _fps, _length, w, h)));
+       _reels.push_back (reel);
 }
 
-/** Write the required XML files to the directory that was
- *  passed into the constructor.
- */
 void
 DCP::write_xml () const
 {
        string cpl_uuid = make_uuid ();
        string cpl_path = write_cpl (cpl_uuid);
        int cpl_length = filesystem::file_size (cpl_path);
-       string cpl_digest = make_digest (cpl_path);
+       string cpl_digest = make_digest (cpl_path, 0);
 
        string pkl_uuid = make_uuid ();
        string pkl_path = write_pkl (pkl_uuid, cpl_uuid, cpl_digest, cpl_length);
@@ -91,10 +76,6 @@ DCP::write_xml () const
        write_assetmap (cpl_uuid, cpl_length, pkl_uuid, filesystem::file_size (pkl_path));
 }
 
-/** Write the CPL file.
- *  @param cpl_uuid UUID to use.
- *  @return CPL pathname.
- */
 string
 DCP::write_cpl (string cpl_uuid) const
 {
@@ -109,22 +90,18 @@ DCP::write_cpl (string cpl_uuid) const
            << "<CompositionPlaylist xmlns=\"http://www.smpte-ra.org/schemas/429-7/2006/CPL\">\n"
            << "  <Id>urn:uuid:" << cpl_uuid << "</Id>\n"
            << "  <AnnotationText>" << _name << "</AnnotationText>\n"
-           << "  <IssueDate>" << Tags::instance()->issue_date << "</IssueDate>\n"
-           << "  <Creator>" << Tags::instance()->creator << "</Creator>\n"
+           << "  <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 << "_" << Tags::instance()->issue_date << "</Id>\n"
-           << "    <LabelText>" << cpl_uuid << "_" << Tags::instance()->issue_date << "</LabelText>\n"
+           << "    <Id>urn:uri:" << cpl_uuid << "_" << Metadata::instance()->issue_date << "</Id>\n"
+           << "    <LabelText>" << cpl_uuid << "_" << Metadata::instance()->issue_date << "</LabelText>\n"
            << "  </ContentVersion>\n"
            << "  <RatingList/>\n"
            << "  <ReelList>\n";
 
-       cpl << "    <Reel>\n"
-           << "      <Id>urn:uuid:" << make_uuid() << "</Id>\n"
-           << "      <AssetList>\n";
-
-       for (list<shared_ptr<Asset> >::const_iterator i = _assets.begin(); i != _assets.end(); ++i) {
+       for (list<shared_ptr<const Reel> >::const_iterator i = _reels.begin(); i != _reels.end(); ++i) {
                (*i)->write_to_cpl (cpl);
        }
 
@@ -136,12 +113,6 @@ DCP::write_cpl (string cpl_uuid) const
        return p.string ();
 }
 
-/** Write the PKL file.
- *  @param pkl_uuid UUID to use.
- *  @param cpl_uuid UUID of the CPL file.
- *  @param cpl_digest SHA digest of the CPL file.
- *  @param cpl_length Length of the CPL file in bytes.
- */
 std::string
 DCP::write_pkl (string pkl_uuid, string cpl_uuid, string cpl_digest, int cpl_length) const
 {
@@ -156,12 +127,12 @@ DCP::write_pkl (string pkl_uuid, string cpl_uuid, string cpl_digest, int cpl_len
            << "<PackingList xmlns=\"http://www.smpte-ra.org/schemas/429-8/2007/PKL\">\n"
            << "  <Id>urn:uuid:" << pkl_uuid << "</Id>\n"
            << "  <AnnotationText>" << _name << "</AnnotationText>\n"
-           << "  <IssueDate>" << Tags::instance()->issue_date << "</IssueDate>\n"
-           << "  <Issuer>" << Tags::instance()->issuer << "</Issuer>\n"
-           << "  <Creator>" << Tags::instance()->creator << "</Creator>\n"
+           << "  <IssueDate>" << Metadata::instance()->issue_date << "</IssueDate>\n"
+           << "  <Issuer>" << Metadata::instance()->issuer << "</Issuer>\n"
+           << "  <Creator>" << Metadata::instance()->creator << "</Creator>\n"
            << "  <AssetList>\n";
 
-       for (list<shared_ptr<Asset> >::const_iterator i = _assets.begin(); i != _assets.end(); ++i) {
+       for (list<shared_ptr<const Reel> >::const_iterator i = _reels.begin(); i != _reels.end(); ++i) {
                (*i)->write_to_pkl (pkl);
        }
 
@@ -178,7 +149,6 @@ DCP::write_pkl (string pkl_uuid, string cpl_uuid, string cpl_digest, int cpl_len
        return p.string ();
 }
 
-/** Write the VOLINDEX file */
 void
 DCP::write_volindex () const
 {
@@ -193,12 +163,6 @@ DCP::write_volindex () const
           << "</VolumeIndex>\n";
 }
 
-/** Write the ASSETMAP file.
- *  @param cpl_uuid UUID of our CPL.
- *  @param cpl_length Length of our CPL in bytes.
- *  @param pkl_uuid UUID of our PKL.
- *  @param pkl_length Length of our PKL in bytes.
- */
 void
 DCP::write_assetmap (string cpl_uuid, int cpl_length, string pkl_uuid, int pkl_length) const
 {
@@ -210,10 +174,10 @@ DCP::write_assetmap (string cpl_uuid, int cpl_length, string pkl_uuid, int pkl_l
        am << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
           << "<AssetMap xmlns=\"http://www.smpte-ra.org/schemas/429-9/2007/AM\">\n"
           << "  <Id>urn:uuid:" << make_uuid() << "</Id>\n"
-          << "  <Creator>" << Tags::instance()->creator << "</Creator>\n"
+          << "  <Creator>" << Metadata::instance()->creator << "</Creator>\n"
           << "  <VolumeCount>1</VolumeCount>\n"
-          << "  <IssueDate>" << Tags::instance()->issue_date << "</IssueDate>\n"
-          << "  <Issuer>" << Tags::instance()->issuer << "</Issuer>\n"
+          << "  <IssueDate>" << Metadata::instance()->issue_date << "</IssueDate>\n"
+          << "  <Issuer>" << Metadata::instance()->issuer << "</Issuer>\n"
           << "  <AssetList>\n";
 
        am << "    <Asset>\n"
@@ -241,7 +205,7 @@ DCP::write_assetmap (string cpl_uuid, int cpl_length, string pkl_uuid, int pkl_l
           << "      </ChunkList>\n"
           << "    </Asset>\n";
        
-       for (list<shared_ptr<Asset> >::const_iterator i = _assets.begin(); i != _assets.end(); ++i) {
+       for (list<shared_ptr<const Reel> >::const_iterator i = _reels.begin(); i != _reels.end(); ++i) {
                (*i)->write_to_assetmap (am);
        }
 
@@ -249,35 +213,199 @@ DCP::write_assetmap (string cpl_uuid, int cpl_length, string pkl_uuid, int pkl_l
           << "</AssetMap>\n";
 }
 
-/** @param t A content type.
- *  @return A string representation suitable for use in a CPL.
- */
-string
-DCP::content_type_string (ContentType t)
+
+DCP::DCP (string directory)
+       : _directory (directory)
 {
-       switch (t) {
-       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";
+       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", "");
        }
 
-       assert (false);
+       /* 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;
+       _length = 0;
+       _fps = 0;
+
+       for (list<shared_ptr<CPLReel> >::iterator i = cpl->reels.begin(); i != cpl->reels.end(); ++i) {
+               assert (_fps == 0 || _fps == (*i)->asset_list->main_picture->frame_rate.numerator);
+               _fps = (*i)->asset_list->main_picture->frame_rate.numerator;
+               _length += (*i)->asset_list->main_picture->duration;
+
+               string n = pkl->asset_from_id ((*i)->asset_list->main_picture->id)->original_file_name;
+               if (n.empty ()) {
+                       n = (*i)->asset_list->main_picture->annotation_text;
+               }
+               
+               shared_ptr<PictureAsset> picture (new PictureAsset (
+                                                         _directory,
+                                                         n,
+                                                         _fps,
+                                                         (*i)->asset_list->main_picture->duration
+                                                         )
+                       );
+
+               shared_ptr<SoundAsset> sound;
+               
+               if ((*i)->asset_list->main_sound) {
+                       
+                       n = pkl->asset_from_id ((*i)->asset_list->main_sound->id)->original_file_name;
+                       if (n.empty ()) {
+                               n = (*i)->asset_list->main_sound->annotation_text;
+                       }
+                       
+                       sound.reset (new SoundAsset (
+                                            _directory,
+                                            n,
+                                            _fps,
+                                            (*i)->asset_list->main_sound->duration
+                                            )
+                               );
+               }
+
+               assert (files.subtitles.size() < 2);
+
+               shared_ptr<SubtitleAsset> subtitle;
+               if (!files.subtitles.empty ()) {
+                       string const l = files.subtitles.front().substr (_directory.length ());
+                       subtitle.reset (new SubtitleAsset (_directory, l));
+               }
+
+               _reels.push_back (shared_ptr<Reel> (new Reel (picture, sound, subtitle)));
+       }
 }
+
+
+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 (_reels.size() != other._reels.size()) {
+               notes.push_back ("reel counts differ");
+       }
+       
+       list<shared_ptr<const Reel> >::const_iterator a = _reels.begin ();
+       list<shared_ptr<const Reel> >::const_iterator b = other._reels.begin ();
+       
+       while (a != _reels.end ()) {
+               list<string> n = (*a)->equals (*b, opt);
+               notes.merge (n);
+               ++a;
+               ++b;
+       }
+
+       return notes;
+}
+