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