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