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