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