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.
24 #include <boost/filesystem.hpp>
25 #include <boost/algorithm/string.hpp>
26 #include <dcp/colour_matrix.h>
27 #include <dcp/raw_convert.h>
28 #include <libcxml/cxml.h>
34 #include "dcp_content_type.h"
35 #include "cinema_sound_processor.h"
36 #include "colour_conversion.h"
49 using boost::shared_ptr;
50 using boost::optional;
51 using boost::algorithm::is_any_of;
52 using boost::algorithm::split;
53 using dcp::raw_convert;
55 Config* Config::_instance = 0;
57 /** Construct default configuration */
59 : _num_local_encoding_threads (max (2U, boost::thread::hardware_concurrency()))
60 , _server_port_base (6192)
61 , _use_any_servers (true)
63 , _cinema_sound_processor (CinemaSoundProcessor::from_id (N_("dolby_cp750")))
64 , _allow_any_dcp_frame_rate (false)
65 , _default_still_length (10)
66 , _default_scale (Ratio::from_id ("185"))
67 , _default_container (Ratio::from_id ("185"))
68 , _default_dcp_content_type (DCPContentType::from_isdcf_name ("TST"))
69 , _default_j2k_bandwidth (100000000)
70 , _default_audio_delay (0)
71 , _check_for_updates (false)
72 , _check_for_test_updates (false)
73 , _maximum_j2k_bandwidth (250000000)
74 , _log_types (Log::TYPE_GENERAL | Log::TYPE_WARNING | Log::TYPE_ERROR)
76 _allowed_dcp_frame_rates.push_back (24);
77 _allowed_dcp_frame_rates.push_back (25);
78 _allowed_dcp_frame_rates.push_back (30);
79 _allowed_dcp_frame_rates.push_back (48);
80 _allowed_dcp_frame_rates.push_back (50);
81 _allowed_dcp_frame_rates.push_back (60);
83 _colour_conversions.push_back (PresetColourConversion (_("sRGB"), 2.4, true, dcp::colour_matrix::srgb_to_xyz, 2.6));
84 _colour_conversions.push_back (PresetColourConversion (_("sRGB non-linearised"), 2.4, false, dcp::colour_matrix::srgb_to_xyz, 2.6));
85 _colour_conversions.push_back (PresetColourConversion (_("Rec. 709"), 2.2, false, dcp::colour_matrix::rec709_to_xyz, 2.6));
93 if (!boost::filesystem::exists (file (false))) {
97 cxml::Document f ("Config");
98 f.read_file (file (false));
101 optional<int> version = f.optional_number_child<int> ("Version");
103 _num_local_encoding_threads = f.number_child<int> ("NumLocalEncodingThreads");
104 _default_directory = f.string_child ("DefaultDirectory");
106 boost::optional<int> b = f.optional_number_child<int> ("ServerPort");
108 b = f.optional_number_child<int> ("ServerPortBase");
110 _server_port_base = b.get ();
112 boost::optional<bool> u = f.optional_bool_child ("UseAnyServers");
113 _use_any_servers = u.get_value_or (true);
115 list<cxml::NodePtr> servers = f.node_children ("Server");
116 for (list<cxml::NodePtr>::iterator i = servers.begin(); i != servers.end(); ++i) {
117 if ((*i)->node_children("HostName").size() == 1) {
118 _servers.push_back ((*i)->string_child ("HostName"));
120 _servers.push_back ((*i)->content ());
124 _tms_ip = f.string_child ("TMSIP");
125 _tms_path = f.string_child ("TMSPath");
126 _tms_user = f.string_child ("TMSUser");
127 _tms_password = f.string_child ("TMSPassword");
129 c = f.optional_string_child ("SoundProcessor");
131 _cinema_sound_processor = CinemaSoundProcessor::from_id (c.get ());
133 c = f.optional_string_child ("CinemaSoundProcessor");
135 _cinema_sound_processor = CinemaSoundProcessor::from_id (c.get ());
138 _language = f.optional_string_child ("Language");
140 c = f.optional_string_child ("DefaultScale");
142 _default_scale = Ratio::from_id (c.get ());
145 c = f.optional_string_child ("DefaultContainer");
147 _default_container = Ratio::from_id (c.get ());
150 c = f.optional_string_child ("DefaultDCPContentType");
152 _default_dcp_content_type = DCPContentType::from_isdcf_name (c.get ());
155 _dcp_metadata.issuer = f.optional_string_child ("DCPMetadataIssuer").get_value_or ("");
156 _dcp_metadata.creator = f.optional_string_child ("DCPMetadataCreator").get_value_or ("");
158 if (version && version.get() >= 2) {
159 _default_isdcf_metadata = ISDCFMetadata (f.node_child ("ISDCFMetadata"));
161 _default_isdcf_metadata = ISDCFMetadata (f.node_child ("DCIMetadata"));
164 _default_still_length = f.optional_number_child<int>("DefaultStillLength").get_value_or (10);
165 _default_j2k_bandwidth = f.optional_number_child<int>("DefaultJ2KBandwidth").get_value_or (200000000);
166 _default_audio_delay = f.optional_number_child<int>("DefaultAudioDelay").get_value_or (0);
168 list<cxml::NodePtr> cc = f.node_children ("ColourConversion");
171 _colour_conversions.clear ();
174 for (list<cxml::NodePtr>::iterator i = cc.begin(); i != cc.end(); ++i) {
175 _colour_conversions.push_back (PresetColourConversion (*i));
179 /* Loading version 0 (before Rec. 709 was added as a preset).
182 _colour_conversions.push_back (PresetColourConversion (_("Rec. 709"), 2.2, false, dcp::colour_matrix::rec709_to_xyz, 2.6));
185 list<cxml::NodePtr> cin = f.node_children ("Cinema");
186 for (list<cxml::NodePtr>::iterator i = cin.begin(); i != cin.end(); ++i) {
187 /* Slightly grotty two-part construction of Cinema here so that we can use
190 shared_ptr<Cinema> cinema (new Cinema (*i));
191 cinema->read_screens (*i);
192 _cinemas.push_back (cinema);
195 _mail_server = f.string_child ("MailServer");
196 _mail_user = f.optional_string_child("MailUser").get_value_or ("");
197 _mail_password = f.optional_string_child("MailPassword").get_value_or ("");
198 _kdm_subject = f.optional_string_child ("KDMSubject").get_value_or (_("KDM delivery: $CPL_NAME"));
199 _kdm_from = f.string_child ("KDMFrom");
200 _kdm_cc = f.optional_string_child ("KDMCC").get_value_or ("");
201 _kdm_email = f.string_child ("KDMEmail");
203 _check_for_updates = f.optional_bool_child("CheckForUpdates").get_value_or (false);
204 _check_for_test_updates = f.optional_bool_child("CheckForTestUpdates").get_value_or (false);
206 _maximum_j2k_bandwidth = f.optional_number_child<int> ("MaximumJ2KBandwidth").get_value_or (250000000);
207 _allow_any_dcp_frame_rate = f.optional_bool_child ("AllowAnyDCPFrameRate");
209 _log_types = f.optional_number_child<int> ("LogTypes").get_value_or (Log::TYPE_GENERAL | Log::TYPE_WARNING | Log::TYPE_ERROR);
212 /** @return Filename to write configuration to */
213 boost::filesystem::path
214 Config::file (bool old) const
216 boost::filesystem::path p;
217 p /= g_get_user_config_dir ();
218 boost::system::error_code ec;
219 boost::filesystem::create_directory (p, ec);
224 boost::filesystem::create_directory (p, ec);
230 boost::filesystem::path
231 Config::signer_chain_directory () const
233 boost::filesystem::path p;
234 p /= g_get_user_config_dir ();
237 boost::filesystem::create_directories (p);
241 /** @return Singleton instance */
245 if (_instance == 0) {
246 _instance = new Config;
249 } catch (exception& e) {
250 /* configuration load failed; never mind, just
251 stick with the default.
253 cerr << "dcpomatic: failed to load configuration (" << e.what() << ")\n";
255 cerr << "dcpomatic: failed to load configuration\n";
262 /** Write our configuration to disk */
264 Config::write () const
267 xmlpp::Element* root = doc.create_root_node ("Config");
269 root->add_child("Version")->add_child_text ("2");
270 root->add_child("NumLocalEncodingThreads")->add_child_text (raw_convert<string> (_num_local_encoding_threads));
271 root->add_child("DefaultDirectory")->add_child_text (_default_directory.string ());
272 root->add_child("ServerPortBase")->add_child_text (raw_convert<string> (_server_port_base));
273 root->add_child("UseAnyServers")->add_child_text (_use_any_servers ? "1" : "0");
275 for (vector<string>::const_iterator i = _servers.begin(); i != _servers.end(); ++i) {
276 root->add_child("Server")->add_child_text (*i);
279 root->add_child("TMSIP")->add_child_text (_tms_ip);
280 root->add_child("TMSPath")->add_child_text (_tms_path);
281 root->add_child("TMSUser")->add_child_text (_tms_user);
282 root->add_child("TMSPassword")->add_child_text (_tms_password);
283 if (_cinema_sound_processor) {
284 root->add_child("CinemaSoundProcessor")->add_child_text (_cinema_sound_processor->id ());
287 root->add_child("Language")->add_child_text (_language.get());
289 if (_default_scale) {
290 root->add_child("DefaultScale")->add_child_text (_default_scale->id ());
292 if (_default_container) {
293 root->add_child("DefaultContainer")->add_child_text (_default_container->id ());
295 if (_default_dcp_content_type) {
296 root->add_child("DefaultDCPContentType")->add_child_text (_default_dcp_content_type->isdcf_name ());
298 root->add_child("DCPMetadataIssuer")->add_child_text (_dcp_metadata.issuer);
299 root->add_child("DCPMetadataCreator")->add_child_text (_dcp_metadata.creator);
301 _default_isdcf_metadata.as_xml (root->add_child ("ISDCFMetadata"));
303 root->add_child("DefaultStillLength")->add_child_text (raw_convert<string> (_default_still_length));
304 root->add_child("DefaultJ2KBandwidth")->add_child_text (raw_convert<string> (_default_j2k_bandwidth));
305 root->add_child("DefaultAudioDelay")->add_child_text (raw_convert<string> (_default_audio_delay));
307 for (vector<PresetColourConversion>::const_iterator i = _colour_conversions.begin(); i != _colour_conversions.end(); ++i) {
308 i->as_xml (root->add_child ("ColourConversion"));
311 for (list<shared_ptr<Cinema> >::const_iterator i = _cinemas.begin(); i != _cinemas.end(); ++i) {
312 (*i)->as_xml (root->add_child ("Cinema"));
315 root->add_child("MailServer")->add_child_text (_mail_server);
316 root->add_child("MailUser")->add_child_text (_mail_user);
317 root->add_child("MailPassword")->add_child_text (_mail_password);
318 root->add_child("KDMSubject")->add_child_text (_kdm_subject);
319 root->add_child("KDMFrom")->add_child_text (_kdm_from);
320 root->add_child("KDMCC")->add_child_text (_kdm_cc);
321 root->add_child("KDMEmail")->add_child_text (_kdm_email);
323 root->add_child("CheckForUpdates")->add_child_text (_check_for_updates ? "1" : "0");
324 root->add_child("CheckForTestUpdates")->add_child_text (_check_for_test_updates ? "1" : "0");
326 root->add_child("MaximumJ2KBandwidth")->add_child_text (raw_convert<string> (_maximum_j2k_bandwidth));
327 root->add_child("AllowAnyDCPFrameRate")->add_child_text (_allow_any_dcp_frame_rate ? "1" : "0");
328 root->add_child("LogTypes")->add_child_text (raw_convert<string> (_log_types));
330 doc.write_to_file_formatted (file(false).string ());
333 boost::filesystem::path
334 Config::default_directory_or (boost::filesystem::path a) const
336 if (_default_directory.empty()) {
340 boost::system::error_code ec;
341 bool const e = boost::filesystem::exists (_default_directory, ec);
346 return _default_directory;
364 Config::reset_kdm_email ()
367 "Dear Projectionist\n\n"
368 "Please find attached KDMs for $CPL_NAME.\n\n"
369 "Cinema: $CINEMA_NAME\n"
370 "Screen(s): $SCREENS\n\n"
371 "The KDMs are valid from $START_TIME until $END_TIME.\n\n"
372 "Best regards,\nDCP-o-matic"