cc783ede79033e14f9676b49446ec287bfed2cf6
[dcpomatic.git] / src / tools / dcpomatic_server.cc
1 /*
2     Copyright (C) 2012-2015 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 "wx/wx_util.h"
21 #include "wx/wx_signal_manager.h"
22 #include "lib/util.h"
23 #include "lib/encoded_log_entry.h"
24 #include "lib/server.h"
25 #include "lib/config.h"
26 #include "lib/log.h"
27 #include "lib/raw_convert.h"
28 #include "lib/signaller.h"
29 #include "lib/cross.h"
30 #include <wx/taskbar.h>
31 #include <wx/icon.h>
32 #include <boost/thread.hpp>
33 #include <boost/foreach.hpp>
34 #include <boost/optional.hpp>
35 #include <iostream>
36
37 using std::cout;
38 using std::string;
39 using std::exception;
40 using std::list;
41 using std::fixed;
42 using std::setprecision;
43 using boost::shared_ptr;
44 using boost::thread;
45 using boost::bind;
46 using boost::optional;
47 using boost::dynamic_pointer_cast;
48
49 enum {
50         ID_status = 1,
51         ID_quit,
52         ID_timer
53 };
54
55 static int const log_lines = 32;
56
57 class ServerLog : public Log, public Signaller
58 {
59 public:
60
61         string get () const {
62                 string a;
63                 BOOST_FOREACH (string const & i, _log) {
64                         a += i + "\n";
65                 }
66                 return a;
67         }
68
69         string head_and_tail (int) const {
70                 /* Not necessary */
71                 return "";
72         }
73
74         float fps () const {
75                 boost::mutex::scoped_lock lm (_state_mutex);
76                 return _fps;
77         }
78
79         boost::signals2::signal<void(string)> Appended;
80         boost::signals2::signal<void(int)> Removed;
81
82 private:
83         void do_log (shared_ptr<const LogEntry> entry)
84         {
85                 time_t const s = entry->seconds ();
86                 struct tm* local = localtime (&s);
87                 if (
88                         !_last_time ||
89                         local->tm_yday != _last_time->tm_yday ||
90                         local->tm_year != _last_time->tm_year ||
91                         local->tm_hour != _last_time->tm_hour ||
92                         local->tm_min != _last_time->tm_min
93                         ) {
94                         char buffer[64];
95                         strftime (buffer, 64, "%c", local);
96                         append (buffer);
97                 }
98
99                 append (entry->message ());
100                 if (_log.size() > log_lines) {
101                         emit (boost::bind (boost::ref (Removed), _log.front().length()));
102                         _log.pop_front ();
103                 }
104                 _last_time = *local;
105
106                 shared_ptr<const EncodedLogEntry> encoded = dynamic_pointer_cast<const EncodedLogEntry> (entry);
107                 if (encoded) {
108                         _history.push_back (encoded->seconds ());
109                         if (_history.size() > 48) {
110                                 _history.pop_front ();
111                         }
112                         if (_history.size() > 2) {
113                                 boost::mutex::scoped_lock lm (_state_mutex);
114                                 _fps = _history.size() / (_history.back() - _history.front());
115                         }
116                 }
117         }
118
119         void append (string s)
120         {
121                 _log.push_back (s);
122                 emit (boost::bind (boost::ref (Appended), s));
123         }
124
125         list<string> _log;
126         optional<struct tm> _last_time;
127         std::list<double> _history;
128
129         mutable boost::mutex _state_mutex;
130         float _fps;
131 };
132
133 static shared_ptr<ServerLog> server_log;
134
135 class StatusDialog : public wxDialog
136 {
137 public:
138         StatusDialog ()
139                 : wxDialog (
140                         0, wxID_ANY, _("DCP-o-matic encode server"),
141                         wxDefaultPosition, wxDefaultSize,
142                         wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER
143                         )
144         {
145                 wxFlexGridSizer* state_sizer = new wxFlexGridSizer (2, DCPOMATIC_SIZER_GAP, DCPOMATIC_SIZER_GAP);
146
147                 add_label_to_sizer (state_sizer, this, _("Frames per second"), true);
148                 _fps = new wxStaticText (this, wxID_ANY, wxT(""));
149                 state_sizer->Add (_fps);
150
151                 wxFlexGridSizer* log_sizer = new wxFlexGridSizer (1, DCPOMATIC_SIZER_GAP, DCPOMATIC_SIZER_GAP);
152                 log_sizer->AddGrowableCol (0, 1);
153
154                 wxClientDC dc (this);
155                 wxSize size = dc.GetTextExtent (wxT ("This is the length of the file label it should be quite long"));
156                 int const height = (size.GetHeight() + 2) * log_lines;
157                 SetSize (700, height + DCPOMATIC_SIZER_GAP * 2);
158
159                 _text = new wxTextCtrl (
160                         this, wxID_ANY, std_to_wx (server_log->get()), wxDefaultPosition, wxSize (-1, height),
161                         wxTE_READONLY | wxTE_MULTILINE
162                         );
163
164                 log_sizer->Add (_text, 1, wxEXPAND);
165
166                 wxBoxSizer* overall_sizer = new wxBoxSizer (wxVERTICAL);
167                 overall_sizer->Add (state_sizer, 0, wxALL, DCPOMATIC_SIZER_GAP);
168                 overall_sizer->Add (log_sizer, 1, wxEXPAND | wxALL, DCPOMATIC_SIZER_GAP);
169                 SetSizer (overall_sizer);
170                 overall_sizer->Layout ();
171
172                 Bind (wxEVT_TIMER, boost::bind (&StatusDialog::update_state, this));
173                 _timer.reset (new wxTimer (this));
174                 _timer->Start (1000);
175
176                 server_log->Appended.connect (bind (&StatusDialog::appended, this, _1));
177                 server_log->Removed.connect (bind (&StatusDialog::removed, this, _1));
178         }
179
180 private:
181         void appended (string s)
182         {
183                 (*_text) << s << "\n";
184         }
185
186         void removed (int n)
187         {
188                 _text->Remove (0, n + 1);
189         }
190
191         void update_state ()
192         {
193                 SafeStringStream s;
194                 s << fixed << setprecision(1) << server_log->fps ();
195                 _fps->SetLabel (std_to_wx (s.str()));
196         }
197
198         wxTextCtrl* _text;
199         wxStaticText* _fps;
200         boost::shared_ptr<wxTimer> _timer;
201 };
202
203 class TaskBarIcon : public wxTaskBarIcon
204 {
205 public:
206         TaskBarIcon ()
207                 : _status (0)
208         {
209 #ifdef DCPOMATIC_WINDOWS
210                 wxIcon icon (std_to_wx ("taskbar_icon"));
211 #else
212                 wxInitAllImageHandlers();
213                 wxBitmap bitmap (wxString::Format (wxT ("%s/dcpomatic2_server_small.png"), std_to_wx (shared_path().string())), wxBITMAP_TYPE_PNG);
214                 wxIcon icon;
215                 icon.CopyFromBitmap (bitmap);
216 #endif
217
218                 SetIcon (icon, std_to_wx ("DCP-o-matic encode server"));
219
220                 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&TaskBarIcon::status, this), ID_status);
221                 Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&TaskBarIcon::quit, this), ID_quit);
222         }
223
224         wxMenu* CreatePopupMenu ()
225         {
226                 wxMenu* menu = new wxMenu;
227                 menu->Append (ID_status, std_to_wx ("Status..."));
228                 menu->Append (ID_quit, std_to_wx ("Quit"));
229                 return menu;
230         }
231
232 private:
233         void status ()
234         {
235                 if (!_status) {
236                         _status = new StatusDialog ();
237                 }
238                 _status->Show ();
239         }
240
241         void quit ()
242         {
243                 wxTheApp->ExitMainLoop ();
244         }
245
246         StatusDialog* _status;
247 };
248
249 class App : public wxApp, public ExceptionStore
250 {
251 public:
252         App ()
253                 : wxApp ()
254                 , _thread (0)
255                 , _icon (0)
256         {}
257
258 private:
259
260         bool OnInit ()
261         {
262                 if (!wxApp::OnInit ()) {
263                         return false;
264                 }
265
266                 server_log.reset (new ServerLog);
267
268                 dcpomatic_setup_path_encoding ();
269                 dcpomatic_setup_i18n ();
270                 dcpomatic_setup ();
271                 Config::drop ();
272
273                 signal_manager = new wxSignalManager (this);
274                 Bind (wxEVT_IDLE, boost::bind (&App::idle, this));
275
276                 _icon = new TaskBarIcon;
277                 _thread = new thread (bind (&App::main_thread, this));
278
279                 Bind (wxEVT_TIMER, boost::bind (&App::check, this));
280                 _timer.reset (new wxTimer (this));
281                 _timer->Start (1000);
282
283                 return true;
284         }
285
286         int OnExit ()
287         {
288                 delete _icon;
289                 return wxApp::OnExit ();
290         }
291
292         void main_thread ()
293         try {
294                 Server server (server_log, false);
295                 server.run (Config::instance()->num_local_encoding_threads ());
296         } catch (...) {
297                 store_current ();
298         }
299
300         void check ()
301         {
302                 try {
303                         rethrow ();
304                 } catch (exception& e) {
305                         error_dialog (0, std_to_wx (e.what ()));
306                         wxTheApp->ExitMainLoop ();
307                 } catch (...) {
308                         error_dialog (0, _("An unknown error has occurred with the DCP-o-matic server."));
309                         wxTheApp->ExitMainLoop ();
310                 }
311         }
312
313         void idle ()
314         {
315                 signal_manager->ui_idle ();
316         }
317
318         boost::thread* _thread;
319         TaskBarIcon* _icon;
320         shared_ptr<wxTimer> _timer;
321 };
322
323 IMPLEMENT_APP (App)