Set up seek position correctly when a seek skips over a reel in
[dcpomatic.git] / src / lib / config.cc
index 6c44e5153c5fb96e9f8ce3849b590a12d4df538f..503ca9c5f30f14e26ad209a62151b2ff81362202 100644 (file)
@@ -1,5 +1,5 @@
 /*
-    Copyright (C) 2012-2017 Carl Hetherington <cth@carlh.net>
+    Copyright (C) 2012-2018 Carl Hetherington <cth@carlh.net>
 
     This file is part of DCP-o-matic.
 
@@ -31,6 +31,7 @@
 #include "cross.h"
 #include "film.h"
 #include "dkdm_wrapper.h"
+#include "compose.hpp"
 #include <dcp/raw_convert.h>
 #include <dcp/name_format.h>
 #include <dcp/certificate_chain.h>
@@ -64,6 +65,8 @@ using dcp::raw_convert;
 
 Config* Config::_instance = 0;
 boost::signals2::signal<void ()> Config::FailedToLoad;
+boost::signals2::signal<void (string)> Config::Warning;
+boost::optional<boost::filesystem::path> Config::test_path;
 
 /** Construct default configuration */
 Config::Config ()
@@ -98,6 +101,7 @@ Config::set_defaults ()
        _default_j2k_bandwidth = 100000000;
        _default_audio_delay = 0;
        _default_interop = true;
+       _default_upload_after_make_dcp = false;
        _mail_server = "";
        _mail_port = 25;
        _mail_user = "";
@@ -125,8 +129,22 @@ Config::set_defaults ()
        for (int i = 0; i < NAG_COUNT; ++i) {
                _nagged[i] = false;
        }
-       _preview_sound = false;
-       _preview_sound_output = optional<string> ();
+       _sound = true;
+       _sound_output = optional<string> ();
+       _last_kdm_write_type = KDM_WRITE_FLAT;
+       _last_dkdm_write_type = DKDM_WRITE_INTERNAL;
+
+       /* I think the scaling factor here should be the ratio of the longest frame
+          encode time to the shortest; if the thread count is T, longest time is L
+          and the shortest time S we could encode L/S frames per thread whilst waiting
+          for the L frame to encode so we might have to store LT/S frames.
+
+          However we don't want to use too much memory, so keep it a bit lower than we'd
+          perhaps like.  A J2K frame is typically about 1Mb so 3 here will mean we could
+          use about 240Mb with 72 encoding threads.
+       */
+       _frames_in_memory_multiplier = 3;
+       _decode_reduction = optional<int>();
 
        _allowed_dcp_frame_rates.clear ();
        _allowed_dcp_frame_rates.push_back (24);
@@ -167,7 +185,7 @@ Config::read ()
 try
 {
        cxml::Document f ("Config");
-       f.read_file (config_file());
+       f.read_file (config_file ());
 
        optional<int> version = f.optional_number_child<int> ("Version");
 
@@ -225,16 +243,17 @@ try
                _default_container = Ratio::from_id (c.get ());
        }
 
-       c = f.optional_string_child ("DefaultScaleTo");
-       if (c) {
-               _default_scale_to = Ratio::from_id (c.get ());
+       if (_default_container && !_default_container->used_for_container()) {
+               Warning (_("Your default container is not valid and has been changed to Flat (1.85:1)"));
+               _default_container = Ratio::from_id ("185");
        }
 
-       c = f.optional_string_child ("DefaultDCPContentType");
+       c = f.optional_string_child ("DefaultScaleTo");
        if (c) {
-               _default_dcp_content_type = DCPContentType::from_isdcf_name (c.get ());
+               _default_scale_to = Ratio::from_id (c.get ());
        }
 
+       _default_dcp_content_type = DCPContentType::from_isdcf_name(f.optional_string_child("DefaultDCPContentType").get_value_or("FTR"));
        _default_dcp_audio_channels = f.optional_number_child<int>("DefaultDCPAudioChannels").get_value_or (6);
 
        if (f.optional_string_child ("DCPMetadataIssuer")) {
@@ -243,6 +262,7 @@ try
                _dcp_issuer = f.string_child ("DCPIssuer");
        }
 
+       _default_upload_after_make_dcp = f.optional_bool_child("DefaultUploadAfterMakeDCP").get_value_or (false);
        _dcp_creator = f.optional_string_child ("DCPCreator").get_value_or ("");
 
        if (version && version.get() >= 2) {
@@ -287,11 +307,14 @@ try
        _win32_console = f.optional_bool_child ("Win32Console").get_value_or (false);
 #endif
 
-       list<cxml::NodePtr> his = f.node_children ("History");
        BOOST_FOREACH (cxml::ConstNodePtr i, f.node_children("History")) {
                _history.push_back (i->content ());
        }
 
+       BOOST_FOREACH (cxml::ConstNodePtr i, f.node_children("PlayerHistory")) {
+               _player_history.push_back (i->content ());
+       }
+
        cxml::NodePtr signer = f.optional_node_child ("Signer");
        if (signer) {
                shared_ptr<dcp::CertificateChain> c (new dcp::CertificateChain ());
@@ -342,11 +365,31 @@ try
                        _nagged[id] = raw_convert<int>(i->content());
                }
        }
-       _preview_sound = f.optional_bool_child("PreviewSound").get_value_or (false);
-       _preview_sound_output = f.optional_string_child("PreviewSoundOutput");
+       /* The variable was renamed but not the XML tag */
+       _sound = f.optional_bool_child("PreviewSound").get_value_or (true);
+       _sound_output = f.optional_string_child("PreviewSoundOutput");
        if (f.optional_string_child("CoverSheet")) {
                _cover_sheet = f.optional_string_child("CoverSheet").get();
        }
+       _last_player_load_directory = f.optional_string_child("LastPlayerLoadDirectory");
+       if (f.optional_string_child("LastKDMWriteType")) {
+               if (f.optional_string_child("LastKDMWriteType").get() == "flat") {
+                       _last_kdm_write_type = KDM_WRITE_FLAT;
+               } else if (f.optional_string_child("LastKDMWriteType").get() == "folder") {
+                       _last_kdm_write_type = KDM_WRITE_FOLDER;
+               } else if (f.optional_string_child("LastKDMWriteType").get() == "zip") {
+                       _last_kdm_write_type = KDM_WRITE_ZIP;
+               }
+       }
+       if (f.optional_string_child("LastDKDMWriteType")) {
+               if (f.optional_string_child("LastDKDMWriteType").get() == "internal") {
+                       _last_dkdm_write_type = DKDM_WRITE_INTERNAL;
+               } else if (f.optional_string_child("LastDKDMWriteType").get() == "file") {
+                       _last_dkdm_write_type = DKDM_WRITE_FILE;
+               }
+       }
+       _frames_in_memory_multiplier = f.optional_number_child<int>("FramesInMemoryMultiplier").get_value_or(3);
+       _decode_reduction = f.optional_number_child<int>("DecodeReduction");
 
        /* Replace any cinemas from config.xml with those from the configured file */
        if (boost::filesystem::exists (_cinemas_file)) {
@@ -360,8 +403,13 @@ catch (...) {
 
                /* Make a copy of the configuration */
                try {
-                       boost::filesystem::copy_file (path ("config.xml", false), path ("config.xml.backup", false));
-                       boost::filesystem::copy_file (path ("cinemas.xml", false), path ("cinemas.xml.backup", false));
+                       int n = 1;
+                       while (n < 100 && boost::filesystem::exists(path(String::compose("config.xml.%1", n)))) {
+                               ++n;
+                       }
+
+                       boost::filesystem::copy_file(path("config.xml", false), path(String::compose("config.xml.%1", n), false));
+                       boost::filesystem::copy_file(path("cinemas.xml", false), path(String::compose("cinemas.xml.%1", n), false));
                } catch (...) {}
 
                /* We have a config file but it didn't load */
@@ -380,16 +428,20 @@ boost::filesystem::path
 Config::path (string file, bool create_directories)
 {
        boost::filesystem::path p;
+       if (test_path) {
+               p = test_path.get();
+       } else {
 #ifdef DCPOMATIC_OSX
-       p /= g_get_home_dir ();
-       p /= "Library";
-       p /= "Preferences";
-       p /= "com.dcpomatic";
-       p /= "2";
+               p /= g_get_home_dir ();
+               p /= "Library";
+               p /= "Preferences";
+               p /= "com.dcpomatic";
+               p /= "2";
 #else
-       p /= g_get_user_config_dir ();
-       p /= "dcpomatic2";
+               p /= g_get_user_config_dir ();
+               p /= "dcpomatic2";
 #endif
+       }
        boost::system::error_code ec;
        if (create_directories) {
                boost::filesystem::create_directories (p, ec);
@@ -474,9 +526,9 @@ Config::write_config () const
                root->add_child("Language")->add_child_text (_language.get());
        }
        if (_default_container) {
-               /* [XML:opt] DefaultContainer ID of default container to use when creating new films (<code>119</code>, <code>133</code>,
-                  <code>138</code>, <code>143</code>, <code>166</code>, <code>178</code>, <code>185</code>, <code>235</code>,
-                  <code>239</code> or <code>full-frame</code>).
+               /* [XML:opt] DefaultContainer ID of default container
+                * to use when creating new films (<code>185</code>,<code>239</code> or
+                * <code>190</code>).
                */
                root->add_child("DefaultContainer")->add_child_text (_default_container->id ());
        }
@@ -498,6 +550,7 @@ Config::write_config () const
        root->add_child("DCPIssuer")->add_child_text (_dcp_issuer);
        /* [XML] DCPIssuer Creator text to write into CPL files. */
        root->add_child("DCPCreator")->add_child_text (_dcp_creator);
+       root->add_child("DefaultUploadAfterMakeDCP")->add_child_text (_default_upload_after_make_dcp ? "1" : "0");
 
        /* [XML] ISDCFMetadata Default ISDCF metadata to use for new films; child tags are <code>&lt;ContentVersion&gt;</code>,
           <code>&lt;AudioLanguage&gt;</code>, <code>&lt;SubtitleLanguage&gt;</code>, <code>&lt;Territory&gt;</code>,
@@ -588,6 +641,10 @@ Config::write_config () const
                root->add_child("History")->add_child_text (i.string ());
        }
 
+       BOOST_FOREACH (boost::filesystem::path i, _player_history) {
+               root->add_child("PlayerHistory")->add_child_text (i.string ());
+       }
+
        /* [XML] DKDMGroup A group of DKDMs, each with a <code>Name</code> attribute, containing other <code>&lt;DKDMGroup&gt;</code>
           or <code>&lt;DKDM&gt;</code> tags.
        */
@@ -616,14 +673,49 @@ Config::write_config () const
                e->set_attribute ("Id", raw_convert<string>(i));
                e->add_child_text (_nagged[i] ? "1" : "0");
        }
-       /* [XML] PreviewSound 1 to use sound in the GUI preview, otherwise 0 */
-       root->add_child("PreviewSound")->add_child_text (_preview_sound ? "1" : "0");
-       if (_preview_sound_output) {
+       /* [XML] PreviewSound 1 to use sound in the GUI preview and player, otherwise 0 */
+       root->add_child("PreviewSound")->add_child_text (_sound ? "1" : "0");
+       if (_sound_output) {
                /* [XML:opt] PreviewSoundOutput Name of the audio output to use */
-               root->add_child("PreviewSoundOutput")->add_child_text (_preview_sound_output.get());
+               root->add_child("PreviewSoundOutput")->add_child_text (_sound_output.get());
        }
        /* [XML] CoverSheet Text of the cover sheet to write when making DCPs */
        root->add_child("CoverSheet")->add_child_text (_cover_sheet);
+       if (_last_player_load_directory) {
+               root->add_child("LastPlayerLoadDirectory")->add_child_text(_last_player_load_directory->string());
+       }
+       if (_last_kdm_write_type) {
+               switch (_last_kdm_write_type.get()) {
+               case KDM_WRITE_FLAT:
+                       root->add_child("LastKDMWriteType")->add_child_text("flat");
+                       break;
+               case KDM_WRITE_FOLDER:
+                       root->add_child("LastKDMWriteType")->add_child_text("folder");
+                       break;
+               case KDM_WRITE_ZIP:
+                       root->add_child("LastKDMWriteType")->add_child_text("zip");
+                       break;
+               }
+       }
+       if (_last_dkdm_write_type) {
+               switch (_last_dkdm_write_type.get()) {
+               case DKDM_WRITE_INTERNAL:
+                       root->add_child("LastDKDMWriteType")->add_child_text("internal");
+                       break;
+               case DKDM_WRITE_FILE:
+                       root->add_child("LastDKDMWriteType")->add_child_text("file");
+                       break;
+               }
+       }
+       /* [XML] FramesInMemoryMultiplier value to multiply the encoding threads count by to get the maximum number of
+          frames to be held in memory at once.
+       */
+       root->add_child("FramesInMemoryMultiplier")->add_child_text(raw_convert<string>(_frames_in_memory_multiplier));
+
+       /* [XML] DecodeReduction power of 2 to reduce DCP images by before decoding in the player */
+       if (_decode_reduction) {
+               root->add_child("DecodeReduction")->add_child_text(raw_convert<string>(_decode_reduction.get()));
+       }
 
        try {
                doc.write_to_file_formatted(config_file().string());
@@ -734,13 +826,25 @@ Config::set_cover_sheet_to_default ()
 
 void
 Config::add_to_history (boost::filesystem::path p)
+{
+       add_to_history_internal (_history, p);
+}
+
+void
+Config::add_to_player_history (boost::filesystem::path p)
+{
+       add_to_history_internal (_player_history, p);
+}
+
+void
+Config::add_to_history_internal (vector<boost::filesystem::path>& h, boost::filesystem::path p)
 {
        /* Remove existing instances of this path in the history */
-       _history.erase (remove (_history.begin(), _history.end(), p), _history.end ());
+       h.erase (remove (h.begin(), h.end(), p), h.end ());
 
-       _history.insert (_history.begin (), p);
-       if (_history.size() > HISTORY_SIZE) {
-               _history.pop_back ();
+       h.insert (h.begin (), p);
+       if (h.size() > HISTORY_SIZE) {
+               h.pop_back ();
        }
 
        changed ();
@@ -832,6 +936,12 @@ Config::config_file ()
 {
        cxml::Document f ("Config");
        boost::filesystem::path main = path("config.xml", false);
+       if (!boost::filesystem::exists (main)) {
+               /* It doesn't exist, so there can't be any links; just return it */
+               return main;
+       }
+
+       /* See if there's a link */
        f.read_file (main);
        optional<string> link = f.optional_string_child("Link");
        if (link) {