Very basics of colour conversion configuration.
[dcpomatic.git] / src / lib / config.cc
1 /*
2     Copyright (C) 2012 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 <sstream>
21 #include <cstdlib>
22 #include <fstream>
23 #include <glib.h>
24 #include <boost/filesystem.hpp>
25 #include <libdcp/colour_matrix.h>
26 #include <libcxml/cxml.h>
27 #include "config.h"
28 #include "server.h"
29 #include "scaler.h"
30 #include "filter.h"
31 #include "ratio.h"
32 #include "dcp_content_type.h"
33 #include "sound_processor.h"
34 #include "colour_conversion.h"
35
36 #include "i18n.h"
37
38 using std::vector;
39 using std::ifstream;
40 using std::string;
41 using std::ofstream;
42 using std::list;
43 using std::max;
44 using boost::shared_ptr;
45 using boost::lexical_cast;
46 using boost::optional;
47
48 Config* Config::_instance = 0;
49
50 /** Construct default configuration */
51 Config::Config ()
52         : _num_local_encoding_threads (max (2U, boost::thread::hardware_concurrency()))
53         , _server_port (6192)
54         , _tms_path (N_("."))
55         , _sound_processor (SoundProcessor::from_id (N_("dolby_cp750")))
56         , _default_still_length (10)
57         , _default_container (Ratio::from_id ("185"))
58         , _default_dcp_content_type (DCPContentType::from_dci_name ("TST"))
59         , _default_j2k_bandwidth (200000000)
60 {
61         _allowed_dcp_frame_rates.push_back (24);
62         _allowed_dcp_frame_rates.push_back (25);
63         _allowed_dcp_frame_rates.push_back (30);
64         _allowed_dcp_frame_rates.push_back (48);
65         _allowed_dcp_frame_rates.push_back (50);
66         _allowed_dcp_frame_rates.push_back (60);
67
68         _colour_conversions.push_back (shared_ptr<ColourConversion> (new ColourConversion (_("sRGB"), 2.4, true, libdcp::colour_matrix::xyz_to_rgb, 2.6)));
69 }
70
71 void
72 Config::read ()
73 {
74         if (!boost::filesystem::exists (file (false))) {
75                 read_old_metadata ();
76                 return;
77         }
78
79         cxml::File f (file (false), "Config");
80         optional<string> c;
81
82         _num_local_encoding_threads = f.number_child<int> ("NumLocalEncodingThreads");
83         _default_directory = f.string_child ("DefaultDirectory");
84         _server_port = f.number_child<int> ("ServerPort");
85         
86         list<shared_ptr<cxml::Node> > servers = f.node_children ("Server");
87         for (list<shared_ptr<cxml::Node> >::iterator i = servers.begin(); i != servers.end(); ++i) {
88                 _servers.push_back (shared_ptr<ServerDescription> (new ServerDescription (*i)));
89         }
90
91         _tms_ip = f.string_child ("TMSIP");
92         _tms_path = f.string_child ("TMSPath");
93         _tms_user = f.string_child ("TMSUser");
94         _tms_password = f.string_child ("TMSPassword");
95
96         c = f.optional_string_child ("SoundProcessor");
97         if (c) {
98                 _sound_processor = SoundProcessor::from_id (c.get ());
99         }
100
101         _language = f.optional_string_child ("Language");
102
103         c = f.optional_string_child ("DefaultContainer");
104         if (c) {
105                 _default_container = Ratio::from_id (c.get ());
106         }
107
108         c = f.optional_string_child ("DefaultDCPContentType");
109         if (c) {
110                 _default_dcp_content_type = DCPContentType::from_dci_name (c.get ());
111         }
112
113         _dcp_metadata.issuer = f.optional_string_child ("DCPMetadataIssuer").get_value_or ("");
114         _dcp_metadata.creator = f.optional_string_child ("DCPMetadataCreator").get_value_or ("");
115
116         _default_dci_metadata = DCIMetadata (f.node_child ("DCIMetadata"));
117         _default_still_length = f.optional_number_child<int>("DefaultStillLength").get_value_or (10);
118         _default_j2k_bandwidth = f.optional_number_child<int>("DefaultJ2KBandwidth").get_value_or (200000000);
119
120         list<shared_ptr<cxml::Node> > cc = f.node_children ("ColourConversion");
121
122         if (!cc.empty ()) {
123                 _colour_conversions.clear ();
124         }
125         
126         for (list<shared_ptr<cxml::Node> >::iterator i = cc.begin(); i != cc.end(); ++i) {
127                 _colour_conversions.push_back (shared_ptr<ColourConversion> (new ColourConversion (*i)));
128         }
129 }
130
131 void
132 Config::read_old_metadata ()
133 {
134         ifstream f (file(true).c_str ());
135         string line;
136         while (getline (f, line)) {
137                 if (line.empty ()) {
138                         continue;
139                 }
140
141                 if (line[0] == '#') {
142                         continue;
143                 }
144
145                 size_t const s = line.find (' ');
146                 if (s == string::npos) {
147                         continue;
148                 }
149                 
150                 string const k = line.substr (0, s);
151                 string const v = line.substr (s + 1);
152
153                 if (k == N_("num_local_encoding_threads")) {
154                         _num_local_encoding_threads = atoi (v.c_str ());
155                 } else if (k == N_("default_directory")) {
156                         _default_directory = v;
157                 } else if (k == N_("server_port")) {
158                         _server_port = atoi (v.c_str ());
159                 } else if (k == N_("server")) {
160                         _servers.push_back (ServerDescription::create_from_metadata (v));
161                 } else if (k == N_("tms_ip")) {
162                         _tms_ip = v;
163                 } else if (k == N_("tms_path")) {
164                         _tms_path = v;
165                 } else if (k == N_("tms_user")) {
166                         _tms_user = v;
167                 } else if (k == N_("tms_password")) {
168                         _tms_password = v;
169                 } else if (k == N_("sound_processor")) {
170                         _sound_processor = SoundProcessor::from_id (v);
171                 } else if (k == "language") {
172                         _language = v;
173                 } else if (k == "default_container") {
174                         _default_container = Ratio::from_id (v);
175                 } else if (k == "default_dcp_content_type") {
176                         _default_dcp_content_type = DCPContentType::from_dci_name (v);
177                 } else if (k == "dcp_metadata_issuer") {
178                         _dcp_metadata.issuer = v;
179                 } else if (k == "dcp_metadata_creator") {
180                         _dcp_metadata.creator = v;
181                 } else if (k == "dcp_metadata_issue_date") {
182                         _dcp_metadata.issue_date = v;
183                 }
184
185                 _default_dci_metadata.read_old_metadata (k, v);
186         }
187 }
188
189 /** @return Filename to write configuration to */
190 string
191 Config::file (bool old) const
192 {
193         boost::filesystem::path p;
194         p /= g_get_user_config_dir ();
195         boost::system::error_code ec;
196         boost::filesystem::create_directory (p, ec);
197         if (old) {
198                 p /= ".dvdomatic";
199         } else {
200                 p /= ".dcpomatic.xml";
201         }
202         return p.string ();
203 }
204
205 /** @return Singleton instance */
206 Config *
207 Config::instance ()
208 {
209         if (_instance == 0) {
210                 _instance = new Config;
211                 try {
212                         _instance->read ();
213                 } catch (...) {
214                         /* configuration load failed; never mind, just
215                            stick with the default.
216                         */
217                 }
218         }
219
220         return _instance;
221 }
222
223 /** Write our configuration to disk */
224 void
225 Config::write () const
226 {
227         xmlpp::Document doc;
228         xmlpp::Element* root = doc.create_root_node ("Config");
229
230         root->add_child("NumLocalEncodingThreads")->add_child_text (lexical_cast<string> (_num_local_encoding_threads));
231         root->add_child("DefaultDirectory")->add_child_text (_default_directory);
232         root->add_child("ServerPort")->add_child_text (lexical_cast<string> (_server_port));
233         
234         for (vector<shared_ptr<ServerDescription> >::const_iterator i = _servers.begin(); i != _servers.end(); ++i) {
235                 (*i)->as_xml (root->add_child ("Server"));
236         }
237
238         root->add_child("TMSIP")->add_child_text (_tms_ip);
239         root->add_child("TMSPath")->add_child_text (_tms_path);
240         root->add_child("TMSUser")->add_child_text (_tms_user);
241         root->add_child("TMSPassword")->add_child_text (_tms_password);
242         if (_sound_processor) {
243                 root->add_child("SoundProcessor")->add_child_text (_sound_processor->id ());
244         }
245         if (_language) {
246                 root->add_child("Language")->add_child_text (_language.get());
247         }
248         if (_default_container) {
249                 root->add_child("DefaultContainer")->add_child_text (_default_container->id ());
250         }
251         if (_default_dcp_content_type) {
252                 root->add_child("DefaultDCPContentType")->add_child_text (_default_dcp_content_type->dci_name ());
253         }
254         root->add_child("DCPMetadataIssuer")->add_child_text (_dcp_metadata.issuer);
255         root->add_child("DCPMetadataCreator")->add_child_text (_dcp_metadata.creator);
256
257         _default_dci_metadata.as_xml (root->add_child ("DCIMetadata"));
258
259         root->add_child("DefaultStillLength")->add_child_text (lexical_cast<string> (_default_still_length));
260         root->add_child("DefaultJ2KBandwidth")->add_child_text (lexical_cast<string> (_default_j2k_bandwidth));
261
262         for (vector<shared_ptr<ColourConversion> >::const_iterator i = _colour_conversions.begin(); i != _colour_conversions.end(); ++i) {
263                 (*i)->as_xml (root->add_child ("ColourConversion"));
264         }
265
266         doc.write_to_file_formatted (file (false));
267 }
268
269 string
270 Config::default_directory_or (string a) const
271 {
272         if (_default_directory.empty() || !boost::filesystem::exists (_default_directory)) {
273                 return a;
274         }
275
276         return _default_directory;
277 }
278
279 void
280 Config::drop ()
281 {
282         delete _instance;
283         _instance = 0;
284 }