Add only-servers-encode option for debugging / optimisation / testing of servers.
[dcpomatic.git] / src / lib / config.cc
1 /*
2     Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
3
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.
8
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.
13
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.
17
18 */
19
20 #include "config.h"
21 #include "server.h"
22 #include "filter.h"
23 #include "ratio.h"
24 #include "types.h"
25 #include "dcp_content_type.h"
26 #include "cinema_sound_processor.h"
27 #include "colour_conversion.h"
28 #include "cinema.h"
29 #include "util.h"
30 #include "cross.h"
31 #include "raw_convert.h"
32 #include <dcp/colour_matrix.h>
33 #include <dcp/certificate_chain.h>
34 #include <libcxml/cxml.h>
35 #include <glib.h>
36 #include <boost/filesystem.hpp>
37 #include <boost/algorithm/string.hpp>
38 #include <boost/foreach.hpp>
39 #include <cstdlib>
40 #include <fstream>
41
42 #include "i18n.h"
43
44 using std::vector;
45 using std::cout;
46 using std::ifstream;
47 using std::string;
48 using std::list;
49 using std::max;
50 using std::remove;
51 using std::exception;
52 using std::cerr;
53 using boost::shared_ptr;
54 using boost::optional;
55 using boost::algorithm::trim;
56
57 Config* Config::_instance = 0;
58
59 /** Construct default configuration */
60 Config::Config ()
61 {
62         set_defaults ();
63 }
64
65 void
66 Config::set_defaults ()
67 {
68         _num_local_encoding_threads = max (2U, boost::thread::hardware_concurrency ());
69         _server_port_base = 6192;
70         _use_any_servers = true;
71         _servers.clear ();
72         _only_servers_encode = false;
73         _tms_protocol = PROTOCOL_SCP;
74         _tms_ip = "";
75         _tms_path = ".";
76         _tms_user = "";
77         _tms_password = "";
78         _cinema_sound_processor = CinemaSoundProcessor::from_id (N_("dolby_cp750"));
79         _allow_any_dcp_frame_rate = false;
80         _language = optional<string> ();
81         _default_still_length = 10;
82         _default_container = Ratio::from_id ("185");
83         _default_dcp_content_type = DCPContentType::from_isdcf_name ("FTR");
84         _default_j2k_bandwidth = 100000000;
85         _default_audio_delay = 0;
86         _mail_server = "";
87         _mail_port = 25;
88         _mail_user = "";
89         _mail_password = "";
90         _kdm_subject = _("KDM delivery");
91         _kdm_from = "";
92         _kdm_cc = "";
93         _kdm_bcc = "";
94         _check_for_updates = false;
95         _check_for_test_updates = false;
96         _maximum_j2k_bandwidth = 250000000;
97         _log_types = Log::TYPE_GENERAL | Log::TYPE_WARNING | Log::TYPE_ERROR;
98 #ifdef DCPOMATIC_WINDOWS
99         _win32_console = false;
100 #endif
101
102         _allowed_dcp_frame_rates.clear ();
103         _allowed_dcp_frame_rates.push_back (24);
104         _allowed_dcp_frame_rates.push_back (25);
105         _allowed_dcp_frame_rates.push_back (30);
106         _allowed_dcp_frame_rates.push_back (48);
107         _allowed_dcp_frame_rates.push_back (50);
108         _allowed_dcp_frame_rates.push_back (60);
109
110         set_kdm_email_to_default ();
111 }
112
113 void
114 Config::restore_defaults ()
115 {
116         Config::instance()->set_defaults ();
117         Config::instance()->changed ();
118 }
119
120 void
121 Config::read ()
122 {
123         if (!have_existing ()) {
124                 /* Make a new set of signing certificates and key */
125                 _signer_chain.reset (new dcp::CertificateChain (openssl_path ()));
126                 /* And similar for decryption of KDMs */
127                 _decryption_chain.reset (new dcp::CertificateChain (openssl_path ()));
128                 write ();
129                 return;
130         }
131
132         cxml::Document f ("Config");
133         f.read_file (file ());
134         optional<string> c;
135
136         optional<int> version = f.optional_number_child<int> ("Version");
137
138         _num_local_encoding_threads = f.number_child<int> ("NumLocalEncodingThreads");
139         _default_directory = f.string_child ("DefaultDirectory");
140
141         boost::optional<int> b = f.optional_number_child<int> ("ServerPort");
142         if (!b) {
143                 b = f.optional_number_child<int> ("ServerPortBase");
144         }
145         _server_port_base = b.get ();
146
147         boost::optional<bool> u = f.optional_bool_child ("UseAnyServers");
148         _use_any_servers = u.get_value_or (true);
149
150         list<cxml::NodePtr> servers = f.node_children ("Server");
151         for (list<cxml::NodePtr>::iterator i = servers.begin(); i != servers.end(); ++i) {
152                 if ((*i)->node_children("HostName").size() == 1) {
153                         _servers.push_back ((*i)->string_child ("HostName"));
154                 } else {
155                         _servers.push_back ((*i)->content ());
156                 }
157         }
158
159         _only_servers_encode = f.optional_bool_child ("OnlyServersEncode").get_value_or (false);
160         _tms_protocol = static_cast<Protocol> (f.optional_number_child<int> ("TMSProtocol").get_value_or (static_cast<int> (PROTOCOL_SCP)));
161         _tms_ip = f.string_child ("TMSIP");
162         _tms_path = f.string_child ("TMSPath");
163         _tms_user = f.string_child ("TMSUser");
164         _tms_password = f.string_child ("TMSPassword");
165
166         c = f.optional_string_child ("SoundProcessor");
167         if (c) {
168                 _cinema_sound_processor = CinemaSoundProcessor::from_id (c.get ());
169         }
170         c = f.optional_string_child ("CinemaSoundProcessor");
171         if (c) {
172                 _cinema_sound_processor = CinemaSoundProcessor::from_id (c.get ());
173         }
174
175         _language = f.optional_string_child ("Language");
176
177         c = f.optional_string_child ("DefaultContainer");
178         if (c) {
179                 _default_container = Ratio::from_id (c.get ());
180         }
181
182         c = f.optional_string_child ("DefaultDCPContentType");
183         if (c) {
184                 _default_dcp_content_type = DCPContentType::from_isdcf_name (c.get ());
185         }
186
187         if (f.optional_string_child ("DCPMetadataIssuer")) {
188                 _dcp_issuer = f.string_child ("DCPMetadataIssuer");
189         } else if (f.optional_string_child ("DCPIssuer")) {
190                 _dcp_issuer = f.string_child ("DCPIssuer");
191         }
192
193         _dcp_creator = f.optional_string_child ("DCPCreator").get_value_or ("");
194
195         if (version && version.get() >= 2) {
196                 _default_isdcf_metadata = ISDCFMetadata (f.node_child ("ISDCFMetadata"));
197         } else {
198                 _default_isdcf_metadata = ISDCFMetadata (f.node_child ("DCIMetadata"));
199         }
200
201         _default_still_length = f.optional_number_child<int>("DefaultStillLength").get_value_or (10);
202         _default_j2k_bandwidth = f.optional_number_child<int>("DefaultJ2KBandwidth").get_value_or (200000000);
203         _default_audio_delay = f.optional_number_child<int>("DefaultAudioDelay").get_value_or (0);
204
205         list<cxml::NodePtr> cin = f.node_children ("Cinema");
206         for (list<cxml::NodePtr>::iterator i = cin.begin(); i != cin.end(); ++i) {
207                 /* Slightly grotty two-part construction of Cinema here so that we can use
208                    shared_from_this.
209                 */
210                 shared_ptr<Cinema> cinema (new Cinema (*i));
211                 cinema->read_screens (*i);
212                 _cinemas.push_back (cinema);
213         }
214
215         _mail_server = f.string_child ("MailServer");
216         _mail_port = f.optional_number_child<int> ("MailPort").get_value_or (25);
217         _mail_user = f.optional_string_child("MailUser").get_value_or ("");
218         _mail_password = f.optional_string_child("MailPassword").get_value_or ("");
219         _kdm_subject = f.optional_string_child ("KDMSubject").get_value_or (_("KDM delivery: $CPL_NAME"));
220         _kdm_from = f.string_child ("KDMFrom");
221         _kdm_cc = f.optional_string_child ("KDMCC").get_value_or ("");
222         _kdm_bcc = f.optional_string_child ("KDMBCC").get_value_or ("");
223         _kdm_email = f.string_child ("KDMEmail");
224
225         _check_for_updates = f.optional_bool_child("CheckForUpdates").get_value_or (false);
226         _check_for_test_updates = f.optional_bool_child("CheckForTestUpdates").get_value_or (false);
227
228         _maximum_j2k_bandwidth = f.optional_number_child<int> ("MaximumJ2KBandwidth").get_value_or (250000000);
229         _allow_any_dcp_frame_rate = f.optional_bool_child ("AllowAnyDCPFrameRate");
230
231         _log_types = f.optional_number_child<int> ("LogTypes").get_value_or (Log::TYPE_GENERAL | Log::TYPE_WARNING | Log::TYPE_ERROR);
232 #ifdef DCPOMATIC_WINDOWS
233         _win32_console = f.optional_bool_child ("Win32Console").get_value_or (false);
234 #endif
235
236         list<cxml::NodePtr> his = f.node_children ("History");
237         for (list<cxml::NodePtr>::const_iterator i = his.begin(); i != his.end(); ++i) {
238                 _history.push_back ((*i)->content ());
239         }
240
241         cxml::NodePtr signer = f.optional_node_child ("Signer");
242         if (signer) {
243                 shared_ptr<dcp::CertificateChain> c (new dcp::CertificateChain ());
244                 /* Read the signing certificates and private key in from the config file */
245                 BOOST_FOREACH (cxml::NodePtr i, signer->node_children ("Certificate")) {
246                         c->add (dcp::Certificate (i->content ()));
247                 }
248                 c->set_key (signer->string_child ("PrivateKey"));
249                 _signer_chain = c;
250         } else {
251                 /* Make a new set of signing certificates and key */
252                 _signer_chain.reset (new dcp::CertificateChain (openssl_path ()));
253         }
254
255         cxml::NodePtr decryption = f.optional_node_child ("Decryption");
256         if (decryption) {
257                 shared_ptr<dcp::CertificateChain> c (new dcp::CertificateChain ());
258                 BOOST_FOREACH (cxml::NodePtr i, decryption->node_children ("Certificate")) {
259                         c->add (dcp::Certificate (i->content ()));
260                 }
261                 c->set_key (signer->string_child ("PrivateKey"));
262                 _decryption_chain = c;
263         } else {
264                 _decryption_chain.reset (new dcp::CertificateChain (openssl_path ()));
265         }
266 }
267
268 /** @return Filename to write configuration to */
269 boost::filesystem::path
270 Config::file ()
271 {
272         boost::filesystem::path p;
273 #ifdef DCPOMATIC_OSX
274         p /= g_get_home_dir ();
275         p /= "Library";
276         p /= "Preferences";
277         p /= "com.dcpomatic";
278         p /= "2";
279 #else
280         p /= g_get_user_config_dir ();
281         p /= "dcpomatic2";
282 #endif
283         boost::system::error_code ec;
284         boost::filesystem::create_directories (p, ec);
285         p /= "config.xml";
286         return p;
287 }
288
289 /** @return Singleton instance */
290 Config *
291 Config::instance ()
292 {
293         if (_instance == 0) {
294                 _instance = new Config;
295                 try {
296                         _instance->read ();
297                 } catch (exception& e) {
298                         /* configuration load failed; never mind, just
299                            stick with the default.
300                         */
301                         cerr << "dcpomatic: warning: configuration did not load (" << e.what() << "); using defaults\n";
302                 } catch (...) {
303                         cerr << "dcpomatic: warning: configuration did not load; using defaults\n";
304                 }
305         }
306
307         return _instance;
308 }
309
310 /** Write our configuration to disk */
311 void
312 Config::write () const
313 {
314         xmlpp::Document doc;
315         xmlpp::Element* root = doc.create_root_node ("Config");
316
317         root->add_child("Version")->add_child_text ("2");
318         root->add_child("NumLocalEncodingThreads")->add_child_text (raw_convert<string> (_num_local_encoding_threads));
319         root->add_child("DefaultDirectory")->add_child_text (_default_directory.string ());
320         root->add_child("ServerPortBase")->add_child_text (raw_convert<string> (_server_port_base));
321         root->add_child("UseAnyServers")->add_child_text (_use_any_servers ? "1" : "0");
322
323         for (vector<string>::const_iterator i = _servers.begin(); i != _servers.end(); ++i) {
324                 root->add_child("Server")->add_child_text (*i);
325         }
326
327         root->add_child("OnlyServersEncode")->add_child_text (_only_servers_encode ? "1" : "0");
328         root->add_child("TMSProtocol")->add_child_text (raw_convert<string> (_tms_protocol));
329         root->add_child("TMSIP")->add_child_text (_tms_ip);
330         root->add_child("TMSPath")->add_child_text (_tms_path);
331         root->add_child("TMSUser")->add_child_text (_tms_user);
332         root->add_child("TMSPassword")->add_child_text (_tms_password);
333         if (_cinema_sound_processor) {
334                 root->add_child("CinemaSoundProcessor")->add_child_text (_cinema_sound_processor->id ());
335         }
336         if (_language) {
337                 root->add_child("Language")->add_child_text (_language.get());
338         }
339         if (_default_container) {
340                 root->add_child("DefaultContainer")->add_child_text (_default_container->id ());
341         }
342         if (_default_dcp_content_type) {
343                 root->add_child("DefaultDCPContentType")->add_child_text (_default_dcp_content_type->isdcf_name ());
344         }
345         root->add_child("DCPIssuer")->add_child_text (_dcp_issuer);
346         root->add_child("DCPCreator")->add_child_text (_dcp_creator);
347
348         _default_isdcf_metadata.as_xml (root->add_child ("ISDCFMetadata"));
349
350         root->add_child("DefaultStillLength")->add_child_text (raw_convert<string> (_default_still_length));
351         root->add_child("DefaultJ2KBandwidth")->add_child_text (raw_convert<string> (_default_j2k_bandwidth));
352         root->add_child("DefaultAudioDelay")->add_child_text (raw_convert<string> (_default_audio_delay));
353
354         for (list<shared_ptr<Cinema> >::const_iterator i = _cinemas.begin(); i != _cinemas.end(); ++i) {
355                 (*i)->as_xml (root->add_child ("Cinema"));
356         }
357
358         root->add_child("MailServer")->add_child_text (_mail_server);
359         root->add_child("MailPort")->add_child_text (raw_convert<string> (_mail_port));
360         root->add_child("MailUser")->add_child_text (_mail_user);
361         root->add_child("MailPassword")->add_child_text (_mail_password);
362         root->add_child("KDMSubject")->add_child_text (_kdm_subject);
363         root->add_child("KDMFrom")->add_child_text (_kdm_from);
364         root->add_child("KDMCC")->add_child_text (_kdm_cc);
365         root->add_child("KDMBCC")->add_child_text (_kdm_bcc);
366         root->add_child("KDMEmail")->add_child_text (_kdm_email);
367
368         root->add_child("CheckForUpdates")->add_child_text (_check_for_updates ? "1" : "0");
369         root->add_child("CheckForTestUpdates")->add_child_text (_check_for_test_updates ? "1" : "0");
370
371         root->add_child("MaximumJ2KBandwidth")->add_child_text (raw_convert<string> (_maximum_j2k_bandwidth));
372         root->add_child("AllowAnyDCPFrameRate")->add_child_text (_allow_any_dcp_frame_rate ? "1" : "0");
373         root->add_child("LogTypes")->add_child_text (raw_convert<string> (_log_types));
374 #ifdef DCPOMATIC_WINDOWS
375         root->add_child("Win32Console")->add_child_text (_win32_console ? "1" : "0");
376 #endif
377
378         xmlpp::Element* signer = root->add_child ("Signer");
379         BOOST_FOREACH (dcp::Certificate const & i, _signer_chain->root_to_leaf ()) {
380                 signer->add_child("Certificate")->add_child_text (i.certificate (true));
381         }
382         signer->add_child("PrivateKey")->add_child_text (_signer_chain->key().get ());
383
384         xmlpp::Element* decryption = root->add_child ("Decryption");
385         BOOST_FOREACH (dcp::Certificate const & i, _decryption_chain->root_to_leaf ()) {
386                 decryption->add_child("Certificate")->add_child_text (i.certificate (true));
387         }
388         decryption->add_child("PrivateKey")->add_child_text (_decryption_chain->key().get ());
389
390         for (vector<boost::filesystem::path>::const_iterator i = _history.begin(); i != _history.end(); ++i) {
391                 root->add_child("History")->add_child_text (i->string ());
392         }
393
394         try {
395                 doc.write_to_file_formatted (file().string ());
396         } catch (xmlpp::exception& e) {
397                 string s = e.what ();
398                 trim (s);
399                 throw FileError (s, file ());
400         }
401 }
402
403 boost::filesystem::path
404 Config::default_directory_or (boost::filesystem::path a) const
405 {
406         if (_default_directory.empty()) {
407                 return a;
408         }
409
410         boost::system::error_code ec;
411         bool const e = boost::filesystem::exists (_default_directory, ec);
412         if (ec || !e) {
413                 return a;
414         }
415
416         return _default_directory;
417 }
418
419 void
420 Config::drop ()
421 {
422         delete _instance;
423         _instance = 0;
424 }
425
426 void
427 Config::changed (Property what)
428 {
429         Changed (what);
430 }
431
432 void
433 Config::set_kdm_email_to_default ()
434 {
435         _kdm_email = _(
436                 "Dear Projectionist\n\n"
437                 "Please find attached KDMs for $CPL_NAME.\n\n"
438                 "Cinema: $CINEMA_NAME\n"
439                 "Screen(s): $SCREENS\n\n"
440                 "The KDMs are valid from $START_TIME until $END_TIME.\n\n"
441                 "Best regards,\nDCP-o-matic"
442                 );
443 }
444
445 void
446 Config::reset_kdm_email ()
447 {
448         set_kdm_email_to_default ();
449         changed ();
450 }
451
452 void
453 Config::add_to_history (boost::filesystem::path p)
454 {
455         /* Remove existing instances of this path in the history */
456         _history.erase (remove (_history.begin(), _history.end(), p), _history.end ());
457
458         _history.insert (_history.begin (), p);
459         if (_history.size() > HISTORY_SIZE) {
460                 _history.pop_back ();
461         }
462
463         changed ();
464 }
465
466 bool
467 Config::have_existing ()
468 {
469         return boost::filesystem::exists (file ());
470 }