b7100d62a543f783041cad3704bca4e80eaf44e8
[dcpomatic.git] / src / tools / dcpomatic_server.cc
1 /*
2     Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "wx/wx_util.h"
22 #include "wx/wx_signal_manager.h"
23 #include "wx/static_text.h"
24 #include "lib/util.h"
25 #include "lib/encoded_log_entry.h"
26 #include "lib/encode_server.h"
27 #include "lib/config.h"
28 #ifdef DCPOMATIC_GROK
29 #include "lib/grok/context.h"
30 #endif
31 #include "lib/log.h"
32 #include "lib/signaller.h"
33 #include "lib/cross.h"
34 #include "lib/dcpomatic_log.h"
35 #include <dcp/warnings.h>
36 LIBDCP_DISABLE_WARNINGS
37 #include <wx/icon.h>
38 #include <wx/splash.h>
39 #include <wx/taskbar.h>
40 LIBDCP_ENABLE_WARNINGS
41 #include <boost/thread.hpp>
42 #include <boost/optional.hpp>
43 #include <iostream>
44
45
46 using std::cout;
47 using std::string;
48 using std::exception;
49 using std::list;
50 using std::fixed;
51 using std::setprecision;
52 using std::shared_ptr;
53 using boost::thread;
54 using boost::bind;
55 using boost::optional;
56 using std::dynamic_pointer_cast;
57 #if BOOST_VERSION >= 106100
58 using namespace boost::placeholders;
59 #endif
60
61
62 enum {
63         ID_status = 1,
64         ID_quit,
65         ID_timer
66 };
67
68 static unsigned int const log_lines = 32;
69
70
71 class ServerLog : public Log, public Signaller
72 {
73 public:
74         ServerLog ()
75                 : _fps (0)
76         {}
77
78         string get () const {
79                 string a;
80                 for (auto const& i: _log) {
81                         a += i + "\n";
82                 }
83                 return a;
84         }
85
86         float fps () const {
87                 boost::mutex::scoped_lock lm (_state_mutex);
88                 return _fps;
89         }
90
91         boost::signals2::signal<void(string)> Appended;
92         boost::signals2::signal<void(int)> Removed;
93
94 private:
95         void do_log (shared_ptr<const LogEntry> entry) override
96         {
97                 time_t const s = entry->seconds ();
98                 struct tm* local = localtime (&s);
99                 if (
100                         !_last_time ||
101                         local->tm_yday != _last_time->tm_yday ||
102                         local->tm_year != _last_time->tm_year ||
103                         local->tm_hour != _last_time->tm_hour ||
104                         local->tm_min != _last_time->tm_min
105                         ) {
106                         char buffer[64];
107                         strftime (buffer, 64, "%c", local);
108                         append (buffer);
109                 }
110
111                 append (entry->message ());
112                 if (_log.size() > log_lines) {
113                         emit (boost::bind (boost::ref (Removed), _log.front().length()));
114                         _log.pop_front ();
115                 }
116                 _last_time = *local;
117
118                 auto encoded = dynamic_pointer_cast<const EncodedLogEntry> (entry);
119                 if (encoded) {
120                         _history.push_back (encoded->seconds ());
121                         if (_history.size() > 48) {
122                                 _history.pop_front ();
123                         }
124                         if (_history.size() > 2) {
125                                 boost::mutex::scoped_lock lm (_state_mutex);
126                                 _fps = _history.size() / (_history.back() - _history.front());
127                         }
128                 }
129         }
130
131         void append (string s)
132         {
133                 _log.push_back (s);
134                 emit (boost::bind(boost::ref(Appended), s));
135         }
136
137         list<string> _log;
138         optional<struct tm> _last_time;
139         std::list<double> _history;
140
141         mutable boost::mutex _state_mutex;
142         float _fps;
143 };
144
145
146 static shared_ptr<ServerLog> server_log;
147
148
149 class StatusDialog : public wxDialog
150 {
151 public:
152         StatusDialog ()
153                 : wxDialog (
154                         nullptr, wxID_ANY, _("DCP-o-matic Encode Server"),
155                         wxDefaultPosition, wxDefaultSize,
156 #ifdef DCPOMATIC_OSX
157                         wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER | wxSTAY_ON_TOP
158 #else
159                         wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER
160 #endif
161                         )
162         {
163                 auto state_sizer = new wxFlexGridSizer (2, DCPOMATIC_SIZER_GAP, DCPOMATIC_SIZER_GAP);
164
165                 add_label_to_sizer (state_sizer, this, _("Frames per second"), true);
166                 _fps = new StaticText (this, wxT(""));
167                 state_sizer->Add (_fps);
168
169                 auto log_sizer = new wxFlexGridSizer (1, DCPOMATIC_SIZER_GAP, DCPOMATIC_SIZER_GAP);
170                 log_sizer->AddGrowableCol (0, 1);
171
172                 wxClientDC dc (this);
173                 wxSize size = dc.GetTextExtent (wxT ("This is the length of the file label it should be quite long"));
174                 int const height = (size.GetHeight() + 2) * log_lines;
175                 SetSize (700, height + DCPOMATIC_SIZER_GAP * 2);
176
177                 _text = new wxTextCtrl (
178                         this, wxID_ANY, std_to_wx (server_log->get()), wxDefaultPosition, wxSize (-1, height),
179                         wxTE_READONLY | wxTE_MULTILINE
180                         );
181
182                 log_sizer->Add (_text, 1, wxEXPAND);
183
184                 wxBoxSizer* overall_sizer = new wxBoxSizer (wxVERTICAL);
185                 overall_sizer->Add (state_sizer, 0, wxALL, DCPOMATIC_SIZER_GAP);
186                 overall_sizer->Add (log_sizer, 1, wxEXPAND | wxALL, DCPOMATIC_SIZER_GAP);
187                 SetSizer (overall_sizer);
188                 overall_sizer->Layout ();
189
190                 Bind (wxEVT_TIMER, boost::bind (&StatusDialog::update_state, this));
191                 _timer.reset (new wxTimer (this));
192                 _timer->Start (1000);
193
194                 server_log->Appended.connect (bind (&StatusDialog::appended, this, _1));
195                 server_log->Removed.connect (bind (&StatusDialog::removed, this, _1));
196         }
197
198 private:
199         void appended (string s)
200         {
201                 (*_text) << s << "\n";
202         }
203
204         void removed (int n)
205         {
206                 _text->Remove (0, n + 1);
207         }
208
209         void update_state ()
210         {
211                 _fps->SetLabel (wxString::Format ("%.1f", server_log->fps()));
212         }
213
214         wxTextCtrl* _text;
215         wxStaticText* _fps;
216         std::shared_ptr<wxTimer> _timer;
217 };
218
219
220 static StatusDialog* status_dialog = nullptr;
221
222
223 class TaskBarIcon : public wxTaskBarIcon
224 {
225 public:
226         TaskBarIcon ()
227         {
228                 set_icon ();
229
230                 Bind (wxEVT_MENU, boost::bind (&TaskBarIcon::status, this), ID_status);
231                 Bind (wxEVT_MENU, boost::bind (&TaskBarIcon::quit, this), ID_quit);
232         }
233
234         wxMenu* CreatePopupMenu () override
235         {
236                 auto menu = new wxMenu;
237                 menu->Append (ID_status, std_to_wx ("Status..."));
238                 menu->Append (ID_quit, std_to_wx ("Quit"));
239                 return menu;
240         }
241
242         void set_icon ()
243         {
244 #ifdef DCPOMATIC_WINDOWS
245                 wxIcon icon (std_to_wx("id"));
246 #else
247                 string const colour = gui_is_dark() ? "white" : "black";
248                 wxBitmap bitmap (
249                         bitmap_path(String::compose("dcpomatic_small_%1.png", colour)),
250                         wxBITMAP_TYPE_PNG
251                         );
252                 wxIcon icon;
253                 icon.CopyFromBitmap (bitmap);
254 #endif
255
256                 SetIcon (icon, std_to_wx ("DCP-o-matic Encode Server"));
257         }
258
259 private:
260         void status ()
261         {
262                 status_dialog->Show ();
263         }
264
265         void quit ()
266         {
267                 wxTheApp->ExitMainLoop ();
268         }
269 };
270
271
272 class App : public wxApp, public ExceptionStore
273 {
274 public:
275         App ()
276                 : wxApp ()
277         {}
278
279 private:
280
281         bool OnInit () override
282         {
283                 if (!wxApp::OnInit()) {
284                         return false;
285                 }
286
287                 wxInitAllImageHandlers ();
288
289                 server_log.reset (new ServerLog);
290                 server_log->set_types (LogEntry::TYPE_GENERAL | LogEntry::TYPE_WARNING | LogEntry::TYPE_ERROR);
291                 dcpomatic_log = server_log;
292
293                 Config::FailedToLoad.connect(boost::bind(&App::config_failed_to_load, this, _1));
294                 Config::Warning.connect (boost::bind (&App::config_warning, this, _1));
295
296                 auto splash = maybe_show_splash ();
297
298                 dcpomatic_setup_path_encoding ();
299                 dcpomatic_setup_i18n ();
300                 dcpomatic_setup ();
301                 Config::drop ();
302
303                 signal_manager = new wxSignalManager (this);
304                 Bind (wxEVT_IDLE, boost::bind (&App::idle, this));
305
306                 /* Bad things happen (on Linux at least) if the config is reloaded by main_thread;
307                    it seems like there's a race which results in the locked_sstream mutex being
308                    locked before it is initialised.  Calling Config::instance() here loads the config
309                    again in this thread, which seems to work around the problem.
310                 */
311                 Config::instance();
312
313                 status_dialog = new StatusDialog ();
314 #ifdef DCPOMATIC_LINUX
315                 status_dialog->Show ();
316 #else
317                 _icon = new TaskBarIcon;
318                 status_dialog->Bind (wxEVT_SYS_COLOUR_CHANGED, boost::bind(&TaskBarIcon::set_icon, _icon));
319 #endif
320                 _thread = thread (bind (&App::main_thread, this));
321
322                 Bind (wxEVT_TIMER, boost::bind (&App::check, this));
323                 _timer.reset (new wxTimer (this));
324                 _timer->Start (1000);
325
326                 if (splash) {
327                         splash->Destroy ();
328                 }
329
330                 SetExitOnFrameDelete (false);
331
332 #ifdef DCPOMATIC_GROK
333                 grk_plugin::setMessengerLogger(new grk_plugin::GrokLogger("[GROK] "));
334 #endif
335
336                 return true;
337         }
338
339         int OnExit () override
340         {
341                 delete _icon;
342                 return wxApp::OnExit ();
343         }
344
345         void main_thread ()
346         try {
347                 EncodeServer server (false, Config::instance()->server_encoding_threads());
348                 server.run ();
349         } catch (...) {
350                 store_current ();
351         }
352
353         void check ()
354         {
355                 try {
356                         rethrow ();
357                 } catch (exception& e) {
358                         error_dialog (nullptr, std_to_wx(e.what()));
359                         wxTheApp->ExitMainLoop ();
360                 } catch (...) {
361                         error_dialog (nullptr, _("An unknown error has occurred with the DCP-o-matic server."));
362                         wxTheApp->ExitMainLoop ();
363                 }
364         }
365
366         void idle ()
367         {
368                 signal_manager->ui_idle ();
369         }
370
371         void config_failed_to_load(Config::LoadFailure what)
372         {
373                 report_config_load_failure(nullptr, what);
374         }
375
376         void config_warning (string m)
377         {
378                 message_dialog (nullptr, std_to_wx(m));
379         }
380
381         boost::thread _thread;
382         TaskBarIcon* _icon = nullptr;
383         shared_ptr<wxTimer> _timer;
384 };
385
386 IMPLEMENT_APP (App)