2 Copyright (C) 2012-2014 Carl Hetherington <cth@carlh.net>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include <boost/filesystem.hpp>
24 #include <boost/algorithm/string.hpp>
25 #include <dcp/colour_matrix.h>
26 #include <dcp/raw_convert.h>
27 #include <dcp/signer.h>
28 #include <dcp/certificate_chain.h>
29 #include <libcxml/cxml.h>
35 #include "dcp_content_type.h"
36 #include "cinema_sound_processor.h"
37 #include "colour_conversion.h"
53 using boost::shared_ptr;
54 using boost::optional;
55 using boost::algorithm::is_any_of;
56 using boost::algorithm::split;
57 using dcp::raw_convert;
59 Config* Config::_instance = 0;
61 /** Construct default configuration */
63 : _num_local_encoding_threads (max (2U, boost::thread::hardware_concurrency()))
64 , _server_port_base (6192)
65 , _use_any_servers (true)
67 , _cinema_sound_processor (CinemaSoundProcessor::from_id (N_("dolby_cp750")))
68 , _allow_any_dcp_frame_rate (false)
69 , _default_still_length (10)
70 , _default_scale (VideoContentScale (Ratio::from_id ("185")))
71 , _default_container (Ratio::from_id ("185"))
72 , _default_dcp_content_type (DCPContentType::from_isdcf_name ("TST"))
73 , _default_j2k_bandwidth (100000000)
74 , _default_audio_delay (0)
75 , _check_for_updates (false)
76 , _check_for_test_updates (false)
77 , _maximum_j2k_bandwidth (250000000)
78 , _log_types (Log::TYPE_GENERAL | Log::TYPE_WARNING | Log::TYPE_ERROR)
79 #ifdef DCPOMATIC_WINDOWS
80 , _win32_console (false)
83 _allowed_dcp_frame_rates.push_back (24);
84 _allowed_dcp_frame_rates.push_back (25);
85 _allowed_dcp_frame_rates.push_back (30);
86 _allowed_dcp_frame_rates.push_back (48);
87 _allowed_dcp_frame_rates.push_back (50);
88 _allowed_dcp_frame_rates.push_back (60);
90 _colour_conversions.push_back (PresetColourConversion (_("sRGB"), 2.4, true, dcp::colour_matrix::srgb_to_xyz, 2.6));
91 _colour_conversions.push_back (PresetColourConversion (_("sRGB non-linearised"), 2.4, false, dcp::colour_matrix::srgb_to_xyz, 2.6));
92 _colour_conversions.push_back (PresetColourConversion (_("Rec. 709"), 2.2, false, dcp::colour_matrix::rec709_to_xyz, 2.6));
100 if (!boost::filesystem::exists (file ())) {
101 /* Make a new set of signing certificates and key */
102 _signer.reset (new dcp::Signer (openssl_path ()));
103 /* And decryption keys */
104 make_decryption_keys ();
108 cxml::Document f ("Config");
109 f.read_file (file ());
112 optional<int> version = f.optional_number_child<int> ("Version");
114 _num_local_encoding_threads = f.number_child<int> ("NumLocalEncodingThreads");
115 _default_directory = f.string_child ("DefaultDirectory");
117 boost::optional<int> b = f.optional_number_child<int> ("ServerPort");
119 b = f.optional_number_child<int> ("ServerPortBase");
121 _server_port_base = b.get ();
123 boost::optional<bool> u = f.optional_bool_child ("UseAnyServers");
124 _use_any_servers = u.get_value_or (true);
126 list<cxml::NodePtr> servers = f.node_children ("Server");
127 for (list<cxml::NodePtr>::iterator i = servers.begin(); i != servers.end(); ++i) {
128 if ((*i)->node_children("HostName").size() == 1) {
129 _servers.push_back ((*i)->string_child ("HostName"));
131 _servers.push_back ((*i)->content ());
135 _tms_ip = f.string_child ("TMSIP");
136 _tms_path = f.string_child ("TMSPath");
137 _tms_user = f.string_child ("TMSUser");
138 _tms_password = f.string_child ("TMSPassword");
140 c = f.optional_string_child ("SoundProcessor");
142 _cinema_sound_processor = CinemaSoundProcessor::from_id (c.get ());
144 c = f.optional_string_child ("CinemaSoundProcessor");
146 _cinema_sound_processor = CinemaSoundProcessor::from_id (c.get ());
149 _language = f.optional_string_child ("Language");
151 c = f.optional_string_child ("DefaultScale");
153 _default_scale = VideoContentScale::from_id (c.get ());
156 c = f.optional_string_child ("DefaultContainer");
158 _default_container = Ratio::from_id (c.get ());
161 c = f.optional_string_child ("DefaultDCPContentType");
163 _default_dcp_content_type = DCPContentType::from_isdcf_name (c.get ());
166 if (f.optional_string_child ("DCPMetadataIssuer")) {
167 _dcp_issuer = f.string_child ("DCPMetadataIssuer");
168 } else if (f.optional_string_child ("DCPIssuer")) {
169 _dcp_issuer = f.string_child ("DCPIssuer");
172 if (version && version.get() >= 2) {
173 _default_isdcf_metadata = ISDCFMetadata (f.node_child ("ISDCFMetadata"));
175 _default_isdcf_metadata = ISDCFMetadata (f.node_child ("DCIMetadata"));
178 _default_still_length = f.optional_number_child<int>("DefaultStillLength").get_value_or (10);
179 _default_j2k_bandwidth = f.optional_number_child<int>("DefaultJ2KBandwidth").get_value_or (200000000);
180 _default_audio_delay = f.optional_number_child<int>("DefaultAudioDelay").get_value_or (0);
182 list<cxml::NodePtr> cc = f.node_children ("ColourConversion");
185 _colour_conversions.clear ();
188 for (list<cxml::NodePtr>::iterator i = cc.begin(); i != cc.end(); ++i) {
189 _colour_conversions.push_back (PresetColourConversion (*i));
193 /* Loading version 0 (before Rec. 709 was added as a preset).
196 _colour_conversions.push_back (PresetColourConversion (_("Rec. 709"), 2.2, false, dcp::colour_matrix::rec709_to_xyz, 2.6));
199 list<cxml::NodePtr> cin = f.node_children ("Cinema");
200 for (list<cxml::NodePtr>::iterator i = cin.begin(); i != cin.end(); ++i) {
201 /* Slightly grotty two-part construction of Cinema here so that we can use
204 shared_ptr<Cinema> cinema (new Cinema (*i));
205 cinema->read_screens (*i);
206 _cinemas.push_back (cinema);
209 _mail_server = f.string_child ("MailServer");
210 _mail_user = f.optional_string_child("MailUser").get_value_or ("");
211 _mail_password = f.optional_string_child("MailPassword").get_value_or ("");
212 _kdm_subject = f.optional_string_child ("KDMSubject").get_value_or (_("KDM delivery: $CPL_NAME"));
213 _kdm_from = f.string_child ("KDMFrom");
214 _kdm_cc = f.optional_string_child ("KDMCC").get_value_or ("");
215 _kdm_bcc = f.optional_string_child ("KDMBCC").get_value_or ("");
216 _kdm_email = f.string_child ("KDMEmail");
218 _check_for_updates = f.optional_bool_child("CheckForUpdates").get_value_or (false);
219 _check_for_test_updates = f.optional_bool_child("CheckForTestUpdates").get_value_or (false);
221 _maximum_j2k_bandwidth = f.optional_number_child<int> ("MaximumJ2KBandwidth").get_value_or (250000000);
222 _allow_any_dcp_frame_rate = f.optional_bool_child ("AllowAnyDCPFrameRate");
224 _log_types = f.optional_number_child<int> ("LogTypes").get_value_or (Log::TYPE_GENERAL | Log::TYPE_WARNING | Log::TYPE_ERROR);
225 #ifdef DCPOMATIC_WINDOWS
226 _win32_console = f.optional_bool_child ("Win32Console").get_value_or (false);
229 list<cxml::NodePtr> his = f.node_children ("History");
230 for (list<cxml::NodePtr>::const_iterator i = his.begin(); i != his.end(); ++i) {
231 _history.push_back ((*i)->content ());
234 cxml::NodePtr signer = f.optional_node_child ("Signer");
235 dcp::CertificateChain signer_chain;
237 /* Read the signing certificates and private key in from the config file */
238 list<cxml::NodePtr> certificates = signer->node_children ("Certificate");
239 for (list<cxml::NodePtr>::const_iterator i = certificates.begin(); i != certificates.end(); ++i) {
240 signer_chain.add (dcp::Certificate ((*i)->content ()));
243 _signer.reset (new dcp::Signer (signer_chain, signer->string_child ("PrivateKey")));
245 /* Make a new set of signing certificates and key */
246 _signer.reset (new dcp::Signer (openssl_path ()));
249 if (f.optional_string_child ("DecryptionCertificate")) {
250 _decryption_certificate = dcp::Certificate (f.string_child ("DecryptionCertificate"));
253 if (f.optional_string_child ("DecryptionPrivateKey")) {
254 _decryption_private_key = f.string_child ("DecryptionPrivateKey");
257 if (!f.optional_string_child ("DecryptionCertificate") || !f.optional_string_child ("DecryptionPrivateKey")) {
258 /* Generate our own decryption certificate and key if either is not present in config */
259 make_decryption_keys ();
264 Config::make_decryption_keys ()
266 boost::filesystem::path p = dcp::make_certificate_chain (openssl_path ());
267 _decryption_certificate = dcp::Certificate (dcp::file_to_string (p / "leaf.signed.pem"));
268 _decryption_private_key = dcp::file_to_string (p / "leaf.key");
269 boost::filesystem::remove_all (p);
272 /** @return Filename to write configuration to */
273 boost::filesystem::path
274 Config::file () const
276 boost::filesystem::path p;
277 p /= g_get_user_config_dir ();
278 boost::system::error_code ec;
279 boost::filesystem::create_directory (p, ec);
281 boost::filesystem::create_directory (p, ec);
286 /** @return Singleton instance */
290 if (_instance == 0) {
291 _instance = new Config;
294 } catch (exception& e) {
295 /* configuration load failed; never mind, just
296 stick with the default.
298 cerr << "dcpomatic: failed to load configuration (" << e.what() << ")\n";
300 cerr << "dcpomatic: failed to load configuration\n";
307 /** Write our configuration to disk */
309 Config::write () const
312 xmlpp::Element* root = doc.create_root_node ("Config");
314 root->add_child("Version")->add_child_text ("2");
315 root->add_child("NumLocalEncodingThreads")->add_child_text (raw_convert<string> (_num_local_encoding_threads));
316 root->add_child("DefaultDirectory")->add_child_text (_default_directory.string ());
317 root->add_child("ServerPortBase")->add_child_text (raw_convert<string> (_server_port_base));
318 root->add_child("UseAnyServers")->add_child_text (_use_any_servers ? "1" : "0");
320 for (vector<string>::const_iterator i = _servers.begin(); i != _servers.end(); ++i) {
321 root->add_child("Server")->add_child_text (*i);
324 root->add_child("TMSIP")->add_child_text (_tms_ip);
325 root->add_child("TMSPath")->add_child_text (_tms_path);
326 root->add_child("TMSUser")->add_child_text (_tms_user);
327 root->add_child("TMSPassword")->add_child_text (_tms_password);
328 if (_cinema_sound_processor) {
329 root->add_child("CinemaSoundProcessor")->add_child_text (_cinema_sound_processor->id ());
332 root->add_child("Language")->add_child_text (_language.get());
334 root->add_child("DefaultScale")->add_child_text (_default_scale.id ());
335 if (_default_container) {
336 root->add_child("DefaultContainer")->add_child_text (_default_container->id ());
338 if (_default_dcp_content_type) {
339 root->add_child("DefaultDCPContentType")->add_child_text (_default_dcp_content_type->isdcf_name ());
341 root->add_child("DCPIssuer")->add_child_text (_dcp_issuer);
343 _default_isdcf_metadata.as_xml (root->add_child ("ISDCFMetadata"));
345 root->add_child("DefaultStillLength")->add_child_text (raw_convert<string> (_default_still_length));
346 root->add_child("DefaultJ2KBandwidth")->add_child_text (raw_convert<string> (_default_j2k_bandwidth));
347 root->add_child("DefaultAudioDelay")->add_child_text (raw_convert<string> (_default_audio_delay));
349 for (vector<PresetColourConversion>::const_iterator i = _colour_conversions.begin(); i != _colour_conversions.end(); ++i) {
350 i->as_xml (root->add_child ("ColourConversion"));
353 for (list<shared_ptr<Cinema> >::const_iterator i = _cinemas.begin(); i != _cinemas.end(); ++i) {
354 (*i)->as_xml (root->add_child ("Cinema"));
357 root->add_child("MailServer")->add_child_text (_mail_server);
358 root->add_child("MailUser")->add_child_text (_mail_user);
359 root->add_child("MailPassword")->add_child_text (_mail_password);
360 root->add_child("KDMSubject")->add_child_text (_kdm_subject);
361 root->add_child("KDMFrom")->add_child_text (_kdm_from);
362 root->add_child("KDMCC")->add_child_text (_kdm_cc);
363 root->add_child("KDMBCC")->add_child_text (_kdm_bcc);
364 root->add_child("KDMEmail")->add_child_text (_kdm_email);
366 root->add_child("CheckForUpdates")->add_child_text (_check_for_updates ? "1" : "0");
367 root->add_child("CheckForTestUpdates")->add_child_text (_check_for_test_updates ? "1" : "0");
369 root->add_child("MaximumJ2KBandwidth")->add_child_text (raw_convert<string> (_maximum_j2k_bandwidth));
370 root->add_child("AllowAnyDCPFrameRate")->add_child_text (_allow_any_dcp_frame_rate ? "1" : "0");
371 root->add_child("LogTypes")->add_child_text (raw_convert<string> (_log_types));
372 #ifdef DCPOMATIC_WINDOWS
373 root->add_child("Win32Console")->add_child_text (_win32_console ? "1" : "0");
376 xmlpp::Element* signer = root->add_child ("Signer");
377 dcp::CertificateChain::List certs = _signer->certificates().root_to_leaf ();
378 for (dcp::CertificateChain::List::const_iterator i = certs.begin(); i != certs.end(); ++i) {
379 signer->add_child("Certificate")->add_child_text (i->certificate (true));
381 signer->add_child("PrivateKey")->add_child_text (_signer->key ());
383 root->add_child("DecryptionCertificate")->add_child_text (_decryption_certificate.certificate (true));
384 root->add_child("DecryptionPrivateKey")->add_child_text (_decryption_private_key);
386 for (vector<boost::filesystem::path>::const_iterator i = _history.begin(); i != _history.end(); ++i) {
387 root->add_child("History")->add_child_text (i->string ());
390 doc.write_to_file_formatted (file().string ());
393 boost::filesystem::path
394 Config::default_directory_or (boost::filesystem::path a) const
396 if (_default_directory.empty()) {
400 boost::system::error_code ec;
401 bool const e = boost::filesystem::exists (_default_directory, ec);
406 return _default_directory;
424 Config::reset_kdm_email ()
427 "Dear Projectionist\n\n"
428 "Please find attached KDMs for $CPL_NAME.\n\n"
429 "Cinema: $CINEMA_NAME\n"
430 "Screen(s): $SCREENS\n\n"
431 "The KDMs are valid from $START_TIME until $END_TIME.\n\n"
432 "Best regards,\nDCP-o-matic"
437 Config::add_to_history (boost::filesystem::path p)
439 /* Remove existing instances of this path in the history */
440 _history.erase (remove (_history.begin(), _history.end(), p), _history.end ());
442 _history.insert (_history.begin (), p);
443 if (_history.size() > HISTORY_SIZE) {
444 _history.pop_back ();