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