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.
25 #include "dcp_content_type.h"
26 #include "cinema_sound_processor.h"
27 #include "colour_conversion.h"
31 #include <dcp/colour_matrix.h>
32 #include <dcp/raw_convert.h>
33 #include <dcp/signer.h>
34 #include <dcp/certificate_chain.h>
35 #include <libcxml/cxml.h>
37 #include <boost/filesystem.hpp>
38 #include <boost/algorithm/string.hpp>
53 using boost::shared_ptr;
54 using boost::optional;
55 using boost::algorithm::trim;
56 using dcp::raw_convert;
58 Config* Config::_instance = 0;
60 /** Construct default configuration */
62 : _num_local_encoding_threads (max (2U, boost::thread::hardware_concurrency()))
63 , _server_port_base (6192)
64 , _use_any_servers (true)
66 , _cinema_sound_processor (CinemaSoundProcessor::from_id (N_("dolby_cp750")))
67 , _allow_any_dcp_frame_rate (false)
68 , _default_still_length (10)
69 , _default_container (Ratio::from_id ("185"))
70 , _default_dcp_content_type (DCPContentType::from_isdcf_name ("FTR"))
71 , _default_j2k_bandwidth (100000000)
72 , _default_audio_delay (0)
73 , _check_for_updates (false)
74 , _check_for_test_updates (false)
75 , _maximum_j2k_bandwidth (250000000)
76 , _log_types (Log::TYPE_GENERAL | Log::TYPE_WARNING | Log::TYPE_ERROR | Log::TYPE_DEBUG)
77 #ifdef DCPOMATIC_WINDOWS
78 , _win32_console (false)
81 _allowed_dcp_frame_rates.push_back (24);
82 _allowed_dcp_frame_rates.push_back (25);
83 _allowed_dcp_frame_rates.push_back (30);
84 _allowed_dcp_frame_rates.push_back (48);
85 _allowed_dcp_frame_rates.push_back (50);
86 _allowed_dcp_frame_rates.push_back (60);
88 _colour_conversions.push_back (PresetColourConversion (_("sRGB"), dcp::ColourConversion::srgb_to_xyz ()));
89 _colour_conversions.push_back (PresetColourConversion (_("Rec. 709"), dcp::ColourConversion::rec709_to_xyz ()));
97 if (!boost::filesystem::exists (file ())) {
98 /* Make a new set of signing certificates and key */
99 _signer.reset (new dcp::Signer (openssl_path ()));
100 /* And decryption keys */
101 make_decryption_keys ();
105 cxml::Document f ("Config");
106 f.read_file (file ());
109 optional<int> version = f.optional_number_child<int> ("Version");
111 _num_local_encoding_threads = f.number_child<int> ("NumLocalEncodingThreads");
112 _default_directory = f.string_child ("DefaultDirectory");
114 boost::optional<int> b = f.optional_number_child<int> ("ServerPort");
116 b = f.optional_number_child<int> ("ServerPortBase");
118 _server_port_base = b.get ();
120 boost::optional<bool> u = f.optional_bool_child ("UseAnyServers");
121 _use_any_servers = u.get_value_or (true);
123 list<cxml::NodePtr> servers = f.node_children ("Server");
124 for (list<cxml::NodePtr>::iterator i = servers.begin(); i != servers.end(); ++i) {
125 if ((*i)->node_children("HostName").size() == 1) {
126 _servers.push_back ((*i)->string_child ("HostName"));
128 _servers.push_back ((*i)->content ());
132 _tms_ip = f.string_child ("TMSIP");
133 _tms_path = f.string_child ("TMSPath");
134 _tms_user = f.string_child ("TMSUser");
135 _tms_password = f.string_child ("TMSPassword");
137 c = f.optional_string_child ("SoundProcessor");
139 _cinema_sound_processor = CinemaSoundProcessor::from_id (c.get ());
141 c = f.optional_string_child ("CinemaSoundProcessor");
143 _cinema_sound_processor = CinemaSoundProcessor::from_id (c.get ());
146 _language = f.optional_string_child ("Language");
148 c = f.optional_string_child ("DefaultContainer");
150 _default_container = Ratio::from_id (c.get ());
153 c = f.optional_string_child ("DefaultDCPContentType");
155 _default_dcp_content_type = DCPContentType::from_isdcf_name (c.get ());
158 if (f.optional_string_child ("DCPMetadataIssuer")) {
159 _dcp_issuer = f.string_child ("DCPMetadataIssuer");
160 } else if (f.optional_string_child ("DCPIssuer")) {
161 _dcp_issuer = f.string_child ("DCPIssuer");
164 if (version && version.get() >= 2) {
165 _default_isdcf_metadata = ISDCFMetadata (f.node_child ("ISDCFMetadata"));
167 _default_isdcf_metadata = ISDCFMetadata (f.node_child ("DCIMetadata"));
170 _default_still_length = f.optional_number_child<int>("DefaultStillLength").get_value_or (10);
171 _default_j2k_bandwidth = f.optional_number_child<int>("DefaultJ2KBandwidth").get_value_or (200000000);
172 _default_audio_delay = f.optional_number_child<int>("DefaultAudioDelay").get_value_or (0);
174 list<cxml::NodePtr> cc = f.node_children ("ColourConversion");
177 _colour_conversions.clear ();
181 for (list<cxml::NodePtr>::iterator i = cc.begin(); i != cc.end(); ++i) {
182 /* This is a bit of a hack; use 32 (the first Film state file version for the 2.x branch)
183 for version 2 and 10 (the current Film state version for the 1.x branch) for version 1.
185 _colour_conversions.push_back (PresetColourConversion (*i, version == 2 ? 32 : 10));
187 } catch (cxml::Error) {
188 /* Probably failed to load an old-style ColourConversion tag; just give up */
189 _colour_conversions.push_back (PresetColourConversion (_("sRGB"), dcp::ColourConversion::srgb_to_xyz ()));
190 _colour_conversions.push_back (PresetColourConversion (_("Rec. 709"), dcp::ColourConversion::rec709_to_xyz ()));
193 list<cxml::NodePtr> cin = f.node_children ("Cinema");
194 for (list<cxml::NodePtr>::iterator i = cin.begin(); i != cin.end(); ++i) {
195 /* Slightly grotty two-part construction of Cinema here so that we can use
198 shared_ptr<Cinema> cinema (new Cinema (*i));
199 cinema->read_screens (*i);
200 _cinemas.push_back (cinema);
203 _mail_server = f.string_child ("MailServer");
204 _mail_user = f.optional_string_child("MailUser").get_value_or ("");
205 _mail_password = f.optional_string_child("MailPassword").get_value_or ("");
206 _kdm_subject = f.optional_string_child ("KDMSubject").get_value_or (_("KDM delivery: $CPL_NAME"));
207 _kdm_from = f.string_child ("KDMFrom");
208 _kdm_cc = f.optional_string_child ("KDMCC").get_value_or ("");
209 _kdm_bcc = f.optional_string_child ("KDMBCC").get_value_or ("");
210 _kdm_email = f.string_child ("KDMEmail");
212 _check_for_updates = f.optional_bool_child("CheckForUpdates").get_value_or (false);
213 _check_for_test_updates = f.optional_bool_child("CheckForTestUpdates").get_value_or (false);
215 _maximum_j2k_bandwidth = f.optional_number_child<int> ("MaximumJ2KBandwidth").get_value_or (250000000);
216 _allow_any_dcp_frame_rate = f.optional_bool_child ("AllowAnyDCPFrameRate");
218 _log_types = f.optional_number_child<int> ("LogTypes").get_value_or (Log::TYPE_GENERAL | Log::TYPE_WARNING | Log::TYPE_ERROR);
219 #ifdef DCPOMATIC_WINDOWS
220 _win32_console = f.optional_bool_child ("Win32Console").get_value_or (false);
223 list<cxml::NodePtr> his = f.node_children ("History");
224 for (list<cxml::NodePtr>::const_iterator i = his.begin(); i != his.end(); ++i) {
225 _history.push_back ((*i)->content ());
228 cxml::NodePtr signer = f.optional_node_child ("Signer");
229 dcp::CertificateChain signer_chain;
231 /* Read the signing certificates and private key in from the config file */
232 list<cxml::NodePtr> certificates = signer->node_children ("Certificate");
233 for (list<cxml::NodePtr>::const_iterator i = certificates.begin(); i != certificates.end(); ++i) {
234 signer_chain.add (dcp::Certificate ((*i)->content ()));
237 _signer.reset (new dcp::Signer (signer_chain, signer->string_child ("PrivateKey")));
239 /* Make a new set of signing certificates and key */
240 _signer.reset (new dcp::Signer (openssl_path ()));
243 if (f.optional_string_child ("DecryptionCertificate")) {
244 _decryption_certificate = dcp::Certificate (f.string_child ("DecryptionCertificate"));
247 if (f.optional_string_child ("DecryptionPrivateKey")) {
248 _decryption_private_key = f.string_child ("DecryptionPrivateKey");
251 if (!f.optional_string_child ("DecryptionCertificate") || !f.optional_string_child ("DecryptionPrivateKey")) {
252 /* Generate our own decryption certificate and key if either is not present in config */
253 make_decryption_keys ();
258 Config::make_decryption_keys ()
260 boost::filesystem::path p = dcp::make_certificate_chain (openssl_path ());
261 _decryption_certificate = dcp::Certificate (dcp::file_to_string (p / "leaf.signed.pem"));
262 _decryption_private_key = dcp::file_to_string (p / "leaf.key");
263 boost::filesystem::remove_all (p);
266 /** @return Filename to write configuration to */
267 boost::filesystem::path
268 Config::file () const
270 boost::filesystem::path p;
271 p /= g_get_user_config_dir ();
272 boost::system::error_code ec;
273 boost::filesystem::create_directory (p, ec);
275 boost::filesystem::create_directory (p, ec);
280 /** @return Singleton instance */
284 if (_instance == 0) {
285 _instance = new Config;
288 } catch (exception& e) {
289 /* configuration load failed; never mind, just
290 stick with the default.
292 cerr << "dcpomatic: failed to load configuration (" << e.what() << ")\n";
294 cerr << "dcpomatic: failed to load configuration\n";
301 /** Write our configuration to disk */
303 Config::write () const
306 xmlpp::Element* root = doc.create_root_node ("Config");
308 root->add_child("Version")->add_child_text ("2");
309 root->add_child("NumLocalEncodingThreads")->add_child_text (raw_convert<string> (_num_local_encoding_threads));
310 root->add_child("DefaultDirectory")->add_child_text (_default_directory.string ());
311 root->add_child("ServerPortBase")->add_child_text (raw_convert<string> (_server_port_base));
312 root->add_child("UseAnyServers")->add_child_text (_use_any_servers ? "1" : "0");
314 for (vector<string>::const_iterator i = _servers.begin(); i != _servers.end(); ++i) {
315 root->add_child("Server")->add_child_text (*i);
318 root->add_child("TMSIP")->add_child_text (_tms_ip);
319 root->add_child("TMSPath")->add_child_text (_tms_path);
320 root->add_child("TMSUser")->add_child_text (_tms_user);
321 root->add_child("TMSPassword")->add_child_text (_tms_password);
322 if (_cinema_sound_processor) {
323 root->add_child("CinemaSoundProcessor")->add_child_text (_cinema_sound_processor->id ());
326 root->add_child("Language")->add_child_text (_language.get());
328 if (_default_container) {
329 root->add_child("DefaultContainer")->add_child_text (_default_container->id ());
331 if (_default_dcp_content_type) {
332 root->add_child("DefaultDCPContentType")->add_child_text (_default_dcp_content_type->isdcf_name ());
334 root->add_child("DCPIssuer")->add_child_text (_dcp_issuer);
336 _default_isdcf_metadata.as_xml (root->add_child ("ISDCFMetadata"));
338 root->add_child("DefaultStillLength")->add_child_text (raw_convert<string> (_default_still_length));
339 root->add_child("DefaultJ2KBandwidth")->add_child_text (raw_convert<string> (_default_j2k_bandwidth));
340 root->add_child("DefaultAudioDelay")->add_child_text (raw_convert<string> (_default_audio_delay));
342 for (vector<PresetColourConversion>::const_iterator i = _colour_conversions.begin(); i != _colour_conversions.end(); ++i) {
343 i->as_xml (root->add_child ("ColourConversion"));
346 for (list<shared_ptr<Cinema> >::const_iterator i = _cinemas.begin(); i != _cinemas.end(); ++i) {
347 (*i)->as_xml (root->add_child ("Cinema"));
350 root->add_child("MailServer")->add_child_text (_mail_server);
351 root->add_child("MailUser")->add_child_text (_mail_user);
352 root->add_child("MailPassword")->add_child_text (_mail_password);
353 root->add_child("KDMSubject")->add_child_text (_kdm_subject);
354 root->add_child("KDMFrom")->add_child_text (_kdm_from);
355 root->add_child("KDMCC")->add_child_text (_kdm_cc);
356 root->add_child("KDMBCC")->add_child_text (_kdm_bcc);
357 root->add_child("KDMEmail")->add_child_text (_kdm_email);
359 root->add_child("CheckForUpdates")->add_child_text (_check_for_updates ? "1" : "0");
360 root->add_child("CheckForTestUpdates")->add_child_text (_check_for_test_updates ? "1" : "0");
362 root->add_child("MaximumJ2KBandwidth")->add_child_text (raw_convert<string> (_maximum_j2k_bandwidth));
363 root->add_child("AllowAnyDCPFrameRate")->add_child_text (_allow_any_dcp_frame_rate ? "1" : "0");
364 root->add_child("LogTypes")->add_child_text (raw_convert<string> (_log_types));
365 #ifdef DCPOMATIC_WINDOWS
366 root->add_child("Win32Console")->add_child_text (_win32_console ? "1" : "0");
369 xmlpp::Element* signer = root->add_child ("Signer");
370 dcp::CertificateChain::List certs = _signer->certificates().root_to_leaf ();
371 for (dcp::CertificateChain::List::const_iterator i = certs.begin(); i != certs.end(); ++i) {
372 signer->add_child("Certificate")->add_child_text (i->certificate (true));
374 signer->add_child("PrivateKey")->add_child_text (_signer->key ());
376 root->add_child("DecryptionCertificate")->add_child_text (_decryption_certificate.certificate (true));
377 root->add_child("DecryptionPrivateKey")->add_child_text (_decryption_private_key);
379 for (vector<boost::filesystem::path>::const_iterator i = _history.begin(); i != _history.end(); ++i) {
380 root->add_child("History")->add_child_text (i->string ());
384 doc.write_to_file_formatted (file().string ());
385 } catch (xmlpp::exception& e) {
386 string s = e.what ();
388 throw FileError (s, file ());
392 boost::filesystem::path
393 Config::default_directory_or (boost::filesystem::path a) const
395 if (_default_directory.empty()) {
399 boost::system::error_code ec;
400 bool const e = boost::filesystem::exists (_default_directory, ec);
405 return _default_directory;
423 Config::reset_kdm_email ()
426 "Dear Projectionist\n\n"
427 "Please find attached KDMs for $CPL_NAME.\n\n"
428 "Cinema: $CINEMA_NAME\n"
429 "Screen(s): $SCREENS\n\n"
430 "The KDMs are valid from $START_TIME until $END_TIME.\n\n"
431 "Best regards,\nDCP-o-matic"
436 Config::add_to_history (boost::filesystem::path p)
438 /* Remove existing instances of this path in the history */
439 _history.erase (remove (_history.begin(), _history.end(), p), _history.end ());
441 _history.insert (_history.begin (), p);
442 if (_history.size() > HISTORY_SIZE) {
443 _history.pop_back ();