Bitwise MXF comparison.
[libdcp.git] / src / dcp.cc
index 758e357bc1805cb9df4727e2149ea6fe9376fa67..ed63170fb049c4d7565f7462d39318e327629fc0 100644 (file)
 
 */
 
+/** @file  src/dcp.cc
+ *  @brief A class to create a DCP.
+ */
+
 #include <sstream>
 #include <fstream>
 #include <iomanip>
 #include "sound_asset.h"
 #include "picture_asset.h"
 #include "util.h"
-#include "tags.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;
 
-/** Construct a DCP.
- *  @param d Directory to write files to.
- */
-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)
 {
+       
 }
 
 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(), _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(), _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)));
 }
 
-/** 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);
@@ -81,10 +90,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
 {
@@ -99,13 +104,13 @@ 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";
@@ -126,12 +131,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
 {
@@ -146,9 +145,9 @@ 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) {
@@ -193,10 +192,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"
@@ -233,32 +232,115 @@ DCP::write_assetmap (string cpl_uuid, int cpl_length, string pkl_uuid, int pkl_l
 }
 
 
-string
-DCP::content_type_string (ContentType t)
+DCP::DCP (string directory)
+       : _directory (directory)
+{
+       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 ();
+               if (ends_with (t, "_cpl.xml")) {
+                       if (cpl_file.empty ()) {
+                               cpl_file = t;
+                       } else {
+                               throw DCPReadError ("duplicate CPLs found");
+                       }
+               } else if (ends_with (t, "_pkl.xml")) {
+                       if (pkl_file.empty ()) {
+                               pkl_file = t;
+                       } else {
+                               throw DCPReadError ("duplicate PKLs found");
+                       }
+               } else if (ends_with (t, "ASSETMAP.xml")) {
+                       if (asset_map_file.empty ()) {
+                               asset_map_file = t;
+                       } else {
+                               throw DCPReadError ("duplicate AssetMaps found");
+                       }
+               }
+       }
+
+       /* Read the XML */
+       CPL cpl (cpl_file);
+       PKL pkl (pkl_file);
+       AssetMap asset_map (asset_map_file);
+
+       /* 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_picture->annotation_text,
+                                                  _fps,
+                                                  _length
+                                                  )
+                                          ));
+       }
+}
+
+list<string>
+DCP::equals (DCP const & other, EqualityFlags flags) const
 {
-       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";
+       list<string> notes;
+       
+       if (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 (flags & LIBDCP_METADATA || flags & MXF_BITWISE) {
+               if (_assets.size() != other._assets.size()) {
+                       notes.push_back ("asset counts differ");
+               }
+       }
+
+       if (flags & MXF_BITWISE) {
+               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->get(), MXF_BITWISE);
+                       notes.merge (n);
+                       ++a;
+                       ++b;
+               }
        }
 
-       assert (false);
+       return notes;
 }
-