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