Add wrappers around boost::filesystem methods that handle the
[libdcp.git] / src / cpl.cc
index 0e431cfc94b46458336ca4eb0a2319faef027c45..5467fef3b1db0b38ebf859e5d4adf623e7342dd7 100644 (file)
@@ -41,6 +41,8 @@
 #include "compose.hpp"
 #include "cpl.h"
 #include "dcp_assert.h"
+#include "equality_options.h"
+#include "filesystem.h"
 #include "local_time.h"
 #include "metadata.h"
 #include "raw_convert.h"
@@ -107,7 +109,7 @@ CPL::CPL (boost::filesystem::path file)
        , _content_kind (ContentKind::FEATURE)
 {
        cxml::Document f ("CompositionPlaylist");
-       f.read_file (file);
+       f.read_file(dcp::filesystem::fix_long_path(file));
 
        if (f.namespace_uri() == cpl_interop_ns) {
                _standard = Standard::INTEROP;
@@ -182,7 +184,7 @@ CPL::set (std::vector<std::shared_ptr<Reel>> reels)
 
 
 void
-CPL::write_xml (boost::filesystem::path file, shared_ptr<const CertificateChain> signer) const
+CPL::write_xml(boost::filesystem::path file, shared_ptr<const CertificateChain> signer, bool include_mca_subdescriptors) const
 {
        xmlpp::Document doc;
        xmlpp::Element* root;
@@ -227,7 +229,7 @@ CPL::write_xml (boost::filesystem::path file, shared_ptr<const CertificateChain>
        for (auto i: _reels) {
                auto asset_list = i->write_to_cpl (reel_list, _standard);
                if (first && _standard == Standard::SMPTE) {
-                       maybe_write_composition_metadata_asset (asset_list);
+                       maybe_write_composition_metadata_asset(asset_list, include_mca_subdescriptors);
                        first = false;
                }
        }
@@ -238,7 +240,7 @@ CPL::write_xml (boost::filesystem::path file, shared_ptr<const CertificateChain>
                signer->sign (root, _standard);
        }
 
-       doc.write_to_file_formatted (file.string(), "UTF-8");
+       doc.write_to_file_formatted(dcp::filesystem::fix_long_path(file).string(), "UTF-8");
 
        set_file (file);
 }
@@ -288,7 +290,16 @@ CPL::read_composition_metadata_asset (cxml::ConstNodePtr node)
                _luminance = Luminance (lum);
        }
 
-       _main_sound_configuration = node->optional_string_child("MainSoundConfiguration");
+       if (auto msc = node->optional_string_child("MainSoundConfiguration")) {
+               try {
+                       _main_sound_configuration = MainSoundConfiguration(*msc);
+               } catch (MainSoundConfigurationError& e) {
+                       /* With Interop DCPs this node may not make any sense, but that's OK */
+                       if (_standard == dcp::Standard::SMPTE) {
+                               throw e;
+                       }
+               }
+       }
 
        auto sr = node->optional_string_child("MainSoundSampleRate");
        if (sr) {
@@ -352,12 +363,83 @@ CPL::read_composition_metadata_asset (cxml::ConstNodePtr node)
 }
 
 
+void
+CPL::write_mca_subdescriptors(xmlpp::Element* parent, shared_ptr<const SoundAsset> asset) const
+{
+       auto reader = asset->start_read ();
+       ASDCP::MXF::SoundfieldGroupLabelSubDescriptor* soundfield;
+       ASDCP::Result_t r = reader->reader()->OP1aHeader().GetMDObjectByType(
+               asdcp_smpte_dict->ul(ASDCP::MDD_SoundfieldGroupLabelSubDescriptor),
+               reinterpret_cast<ASDCP::MXF::InterchangeObject**>(&soundfield)
+               );
+       if (KM_SUCCESS(r)) {
+               auto mca_subs = parent->add_child("mca:MCASubDescriptors");
+               mca_subs->set_namespace_declaration (mca_sub_descriptors_ns, "mca");
+               mca_subs->set_namespace_declaration (smpte_395_ns, "r0");
+               mca_subs->set_namespace_declaration (smpte_335_ns, "r1");
+               auto sf = mca_subs->add_child("SoundfieldGroupLabelSubDescriptor", "r0");
+               char buffer[64];
+               soundfield->InstanceUID.EncodeString(buffer, sizeof(buffer));
+               sf->add_child("InstanceID", "r1")->add_child_text("urn:uuid:" + string(buffer));
+               soundfield->MCALabelDictionaryID.EncodeString(buffer, sizeof(buffer));
+               sf->add_child("MCALabelDictionaryID", "r1")->add_child_text("urn:smpte:ul:" + string(buffer));
+               soundfield->MCALinkID.EncodeString(buffer, sizeof(buffer));
+               sf->add_child("MCALinkID", "r1")->add_child_text("urn:uuid:" + string(buffer));
+               soundfield->MCATagSymbol.EncodeString(buffer, sizeof(buffer));
+               sf->add_child("MCATagSymbol", "r1")->add_child_text(buffer);
+               if (!soundfield->MCATagName.empty()) {
+                       soundfield->MCATagName.get().EncodeString(buffer, sizeof(buffer));
+                       sf->add_child("MCATagName", "r1")->add_child_text(buffer);
+               }
+               if (!soundfield->RFC5646SpokenLanguage.empty()) {
+                       soundfield->RFC5646SpokenLanguage.get().EncodeString(buffer, sizeof(buffer));
+                       sf->add_child("RFC5646SpokenLanguage", "r1")->add_child_text(buffer);
+               }
+
+               /* Find the MCA subdescriptors in the MXF so that we can also write them here */
+               list<ASDCP::MXF::InterchangeObject*> channels;
+               auto r = reader->reader()->OP1aHeader().GetMDObjectsByType(
+                       asdcp_smpte_dict->ul(ASDCP::MDD_AudioChannelLabelSubDescriptor),
+                       channels
+                       );
+
+               for (auto i: channels) {
+                       auto channel = reinterpret_cast<ASDCP::MXF::AudioChannelLabelSubDescriptor*>(i);
+                       auto ch = mca_subs->add_child("AudioChannelLabelSubDescriptor", "r0");
+                       channel->InstanceUID.EncodeString(buffer, sizeof(buffer));
+                       ch->add_child("InstanceID", "r1")->add_child_text("urn:uuid:" + string(buffer));
+                       channel->MCALabelDictionaryID.EncodeString(buffer, sizeof(buffer));
+                       ch->add_child("MCALabelDictionaryID", "r1")->add_child_text("urn:smpte:ul:" + string(buffer));
+                       channel->MCALinkID.EncodeString(buffer, sizeof(buffer));
+                       ch->add_child("MCALinkID", "r1")->add_child_text("urn:uuid:" + string(buffer));
+                       channel->MCATagSymbol.EncodeString(buffer, sizeof(buffer));
+                       ch->add_child("MCATagSymbol", "r1")->add_child_text(buffer);
+                       if (!channel->MCATagName.empty()) {
+                               channel->MCATagName.get().EncodeString(buffer, sizeof(buffer));
+                               ch->add_child("MCATagName", "r1")->add_child_text(buffer);
+                       }
+                       if (!channel->MCAChannelID.empty()) {
+                               ch->add_child("MCAChannelID", "r1")->add_child_text(raw_convert<string>(channel->MCAChannelID.get()));
+                       }
+                       if (!channel->RFC5646SpokenLanguage.empty()) {
+                               channel->RFC5646SpokenLanguage.get().EncodeString(buffer, sizeof(buffer));
+                               ch->add_child("RFC5646SpokenLanguage", "r1")->add_child_text(buffer);
+                       }
+                       if (!channel->SoundfieldGroupLinkID.empty()) {
+                               channel->SoundfieldGroupLinkID.get().EncodeString(buffer, sizeof(buffer));
+                               ch->add_child("SoundfieldGroupLinkID", "r1")->add_child_text("urn:uuid:" + string(buffer));
+                       }
+               }
+       }
+}
+
+
 /** Write a CompositionMetadataAsset node as a child of @param node provided
  *  the required metadata is stored in the object.  If any required metadata
  *  is missing this method will do nothing.
  */
 void
-CPL::maybe_write_composition_metadata_asset (xmlpp::Element* node) const
+CPL::maybe_write_composition_metadata_asset(xmlpp::Element* node, bool include_mca_subdescriptors) const
 {
        if (
                !_main_sound_configuration ||
@@ -421,7 +503,9 @@ CPL::maybe_write_composition_metadata_asset (xmlpp::Element* node) const
                _luminance->as_xml (meta, "meta");
        }
 
-       meta->add_child("MainSoundConfiguration", "meta")->add_child_text(*_main_sound_configuration);
+       if (_main_sound_configuration) {
+               meta->add_child("MainSoundConfiguration", "meta")->add_child_text(_main_sound_configuration->to_string());
+       }
        meta->add_child("MainSoundSampleRate", "meta")->add_child_text(raw_convert<string>(*_main_sound_sample_rate) + " 1");
 
        auto stored = meta->add_child("MainPictureStoredArea", "meta");
@@ -476,74 +560,8 @@ CPL::maybe_write_composition_metadata_asset (xmlpp::Element* node) const
 
        if (_reels.front()->main_sound()) {
                auto asset = _reels.front()->main_sound()->asset();
-               if (asset) {
-                       auto reader = asset->start_read ();
-                       ASDCP::MXF::SoundfieldGroupLabelSubDescriptor* soundfield;
-                       ASDCP::Result_t r = reader->reader()->OP1aHeader().GetMDObjectByType(
-                               asdcp_smpte_dict->ul(ASDCP::MDD_SoundfieldGroupLabelSubDescriptor),
-                               reinterpret_cast<ASDCP::MXF::InterchangeObject**>(&soundfield)
-                               );
-                       if (KM_SUCCESS(r)) {
-                               auto mca_subs = meta->add_child("mca:MCASubDescriptors");
-                               mca_subs->set_namespace_declaration (mca_sub_descriptors_ns, "mca");
-                               mca_subs->set_namespace_declaration (smpte_395_ns, "r0");
-                               mca_subs->set_namespace_declaration (smpte_335_ns, "r1");
-                               auto sf = mca_subs->add_child("SoundfieldGroupLabelSubDescriptor", "r0");
-                               char buffer[64];
-                               soundfield->InstanceUID.EncodeString(buffer, sizeof(buffer));
-                               sf->add_child("InstanceID", "r1")->add_child_text("urn:uuid:" + string(buffer));
-                               soundfield->MCALabelDictionaryID.EncodeString(buffer, sizeof(buffer));
-                               sf->add_child("MCALabelDictionaryID", "r1")->add_child_text("urn:smpte:ul:" + string(buffer));
-                               soundfield->MCALinkID.EncodeString(buffer, sizeof(buffer));
-                               sf->add_child("MCALinkID", "r1")->add_child_text("urn:uuid:" + string(buffer));
-                               soundfield->MCATagSymbol.EncodeString(buffer, sizeof(buffer));
-                               sf->add_child("MCATagSymbol", "r1")->add_child_text(buffer);
-                               if (!soundfield->MCATagName.empty()) {
-                                       soundfield->MCATagName.get().EncodeString(buffer, sizeof(buffer));
-                                       sf->add_child("MCATagName", "r1")->add_child_text(buffer);
-                               }
-                               if (!soundfield->RFC5646SpokenLanguage.empty()) {
-                                       soundfield->RFC5646SpokenLanguage.get().EncodeString(buffer, sizeof(buffer));
-                                       sf->add_child("RFC5646SpokenLanguage", "r1")->add_child_text(buffer);
-                               }
-
-                               list<ASDCP::MXF::InterchangeObject*> channels;
-                               auto r = reader->reader()->OP1aHeader().GetMDObjectsByType(
-                                       asdcp_smpte_dict->ul(ASDCP::MDD_AudioChannelLabelSubDescriptor),
-                                       channels
-                                       );
-
-                               for (auto i: channels) {
-                                       auto channel = reinterpret_cast<ASDCP::MXF::AudioChannelLabelSubDescriptor*>(i);
-                                       if (static_cast<int>(channel->MCAChannelID) > asset->channels()) {
-                                               continue;
-                                       }
-                                       auto ch = mca_subs->add_child("AudioChannelLabelSubDescriptor", "r0");
-                                       channel->InstanceUID.EncodeString(buffer, sizeof(buffer));
-                                       ch->add_child("InstanceID", "r1")->add_child_text("urn:uuid:" + string(buffer));
-                                       channel->MCALabelDictionaryID.EncodeString(buffer, sizeof(buffer));
-                                       ch->add_child("MCALabelDictionaryID", "r1")->add_child_text("urn:smpte:ul:" + string(buffer));
-                                       channel->MCALinkID.EncodeString(buffer, sizeof(buffer));
-                                       ch->add_child("MCALinkID", "r1")->add_child_text("urn:uuid:" + string(buffer));
-                                       channel->MCATagSymbol.EncodeString(buffer, sizeof(buffer));
-                                       ch->add_child("MCATagSymbol", "r1")->add_child_text(buffer);
-                                       if (!channel->MCATagName.empty()) {
-                                               channel->MCATagName.get().EncodeString(buffer, sizeof(buffer));
-                                               ch->add_child("MCATagName", "r1")->add_child_text(buffer);
-                                       }
-                                       if (!channel->MCAChannelID.empty()) {
-                                               ch->add_child("MCAChannelID", "r1")->add_child_text(raw_convert<string>(channel->MCAChannelID.get()));
-                                       }
-                                       if (!channel->RFC5646SpokenLanguage.empty()) {
-                                               channel->RFC5646SpokenLanguage.get().EncodeString(buffer, sizeof(buffer));
-                                               ch->add_child("RFC5646SpokenLanguage", "r1")->add_child_text(buffer);
-                                       }
-                                       if (!channel->SoundfieldGroupLinkID.empty()) {
-                                               channel->SoundfieldGroupLinkID.get().EncodeString(buffer, sizeof(buffer));
-                                               ch->add_child("SoundfieldGroupLinkID", "r1")->add_child_text("urn:uuid:" + string(buffer));
-                                       }
-                               }
-                       }
+               if (asset && include_mca_subdescriptors) {
+                       write_mca_subdescriptors(meta, asset);
                }
        }
 }
@@ -592,7 +610,7 @@ CPL::reel_file_assets () const
 
 
 bool
-CPL::equals (shared_ptr<const Asset> other, EqualityOptions opt, NoteHandler note) const
+CPL::equals(shared_ptr<const Asset> other, EqualityOptions const& opt, NoteHandler note) const
 {
        auto other_cpl = dynamic_pointer_cast<const CPL>(other);
        if (!other_cpl) {