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