Stop player analysing audio even if it is configured automatically to happen.
[dcpomatic.git] / src / tools / dcpomatic_player.cc
1 /*
2     Copyright (C) 2017 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 "lib/cross.h"
22 #include "lib/config.h"
23 #include "lib/util.h"
24 #include "lib/update_checker.h"
25 #include "lib/compose.hpp"
26 #include "lib/dcp_content.h"
27 #include "lib/job_manager.h"
28 #include "lib/job.h"
29 #include "lib/video_content.h"
30 #include "lib/subtitle_content.h"
31 #include "wx/wx_signal_manager.h"
32 #include "wx/wx_util.h"
33 #include "wx/about_dialog.h"
34 #include "wx/report_problem_dialog.h"
35 #include "wx/film_viewer.h"
36 #include "wx/player_information.h"
37 #include "wx/update_dialog.h"
38 #include "wx/player_config_dialog.h"
39 #include <wx/wx.h>
40 #include <wx/stdpaths.h>
41 #include <wx/splash.h>
42 #include <wx/cmdline.h>
43 #include <wx/preferences.h>
44 #ifdef __WXOSX__
45 #include <ApplicationServices/ApplicationServices.h>
46 #endif
47 #include <boost/bind.hpp>
48 #include <iostream>
49
50 #ifdef check
51 #undef check
52 #endif
53
54 using std::string;
55 using std::cout;
56 using std::exception;
57 using boost::shared_ptr;
58 using boost::optional;
59
60 enum {
61         ID_file_open = 1,
62         ID_file_add_ov,
63         ID_file_add_kdm,
64         ID_view_scale_appropriate,
65         ID_view_scale_full,
66         ID_view_scale_half,
67         ID_view_scale_quarter,
68         ID_help_report_a_problem,
69         ID_tools_check_for_updates,
70 };
71
72 class DOMFrame : public wxFrame
73 {
74 public:
75         DOMFrame ()
76                 : wxFrame (0, -1, _("DCP-o-matic Player"))
77                 , _update_news_requested (false)
78                 , _info (0)
79                 , _config_dialog (0)
80                 , _viewer (0)
81         {
82
83 #if defined(DCPOMATIC_WINDOWS)
84                 maybe_open_console ();
85                 cout << "DCP-o-matic Player is starting." << "\n";
86 #endif
87
88                 wxMenuBar* bar = new wxMenuBar;
89                 setup_menu (bar);
90                 SetMenuBar (bar);
91
92 #ifdef DCPOMATIC_WINDOWS
93                 SetIcon (wxIcon (std_to_wx ("id")));
94 #endif
95
96                 _config_changed_connection = Config::instance()->Changed.connect (boost::bind (&DOMFrame::config_changed, this));
97
98                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_open, this), ID_file_open);
99                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_add_ov, this), ID_file_add_ov);
100                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_add_kdm, this), ID_file_add_kdm);
101                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::file_exit, this), wxID_EXIT);
102                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::edit_preferences, this), wxID_PREFERENCES);
103                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::set_decode_reduction, this, optional<int>()), ID_view_scale_appropriate);
104                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::set_decode_reduction, this, optional<int>(0)), ID_view_scale_full);
105                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::set_decode_reduction, this, optional<int>(1)), ID_view_scale_half);
106                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::set_decode_reduction, this, optional<int>(2)), ID_view_scale_quarter);
107                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::help_about, this), wxID_ABOUT);
108                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::help_report_a_problem, this), ID_help_report_a_problem);
109                 Bind (wxEVT_MENU, boost::bind (&DOMFrame::tools_check_for_updates, this), ID_tools_check_for_updates);
110
111                 /* Use a panel as the only child of the Frame so that we avoid
112                    the dark-grey background on Windows.
113                 */
114                 wxPanel* overall_panel = new wxPanel (this, wxID_ANY);
115
116                 _viewer = new FilmViewer (overall_panel, false, false);
117                 _info = new PlayerInformation (overall_panel, _viewer);
118                 wxSizer* main_sizer = new wxBoxSizer (wxVERTICAL);
119                 main_sizer->Add (_viewer, 1, wxEXPAND | wxALL, 6);
120                 main_sizer->Add (_info, 0, wxEXPAND | wxALL, 6);
121                 overall_panel->SetSizer (main_sizer);
122
123                 UpdateChecker::instance()->StateChanged.connect (boost::bind (&DOMFrame::update_checker_state_changed, this));
124         }
125
126         void set_decode_reduction (optional<int> reduction)
127         {
128                 _viewer->set_dcp_decode_reduction (reduction);
129         }
130
131         void load_dcp (boost::filesystem::path dir)
132         {
133                 _film.reset (new Film (optional<boost::filesystem::path>()));
134                 shared_ptr<DCPContent> dcp (new DCPContent (_film, dir));
135                 _film->examine_and_add_content (dcp, true);
136
137                 JobManager* jm = JobManager::instance ();
138
139                 while (jm->work_to_do() || signal_manager->ui_idle()) {
140                         /* XXX: progress dialog */
141                         dcpomatic_sleep (1);
142                 }
143
144                 DCPOMATIC_ASSERT (!jm->get().empty());
145
146                 shared_ptr<Job> last = jm->get().back();
147                 if (last->finished_in_error()) {
148                         error_dialog (this, std_to_wx (last->error_summary()) + ".\n");
149                         return;
150                 }
151
152                 if (dcp->subtitle) {
153                         dcp->subtitle->set_use (true);
154                 }
155
156                 _viewer->set_film (_film);
157                 _info->triggered_update ();
158         }
159
160 private:
161
162         void setup_menu (wxMenuBar* m)
163         {
164                 wxMenu* file = new wxMenu;
165                 file->Append (ID_file_open, _("&Open...\tCtrl-O"));
166                 file->Append (ID_file_add_ov, _("&Add OV..."));
167                 file->Append (ID_file_add_kdm, _("&Add KDM..."));
168
169 #ifdef __WXOSX__
170                 file->Append (wxID_EXIT, _("&Exit"));
171 #else
172                 file->Append (wxID_EXIT, _("&Quit"));
173 #endif
174
175 #ifdef __WXOSX__
176                 file->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
177 #else
178                 wxMenu* edit = new wxMenu;
179                 edit->Append (wxID_PREFERENCES, _("&Preferences...\tCtrl-P"));
180 #endif
181
182                 wxMenu* view = new wxMenu;
183                 view->AppendRadioItem (ID_view_scale_appropriate, _("Set decode resolution to match display"));
184                 view->AppendRadioItem (ID_view_scale_full, _("Decode at full resolution"));
185                 view->AppendRadioItem (ID_view_scale_half, _("Decode at half resolution"));
186                 view->AppendRadioItem (ID_view_scale_quarter, _("Decode at quarter resolution"));
187
188                 wxMenu* tools = new wxMenu;
189                 tools->Append (ID_tools_check_for_updates, _("Check for updates"));
190
191                 wxMenu* help = new wxMenu;
192 #ifdef __WXOSX__
193                 help->Append (wxID_ABOUT, _("About DCP-o-matic"));
194 #else
195                 help->Append (wxID_ABOUT, _("About"));
196 #endif
197                 help->Append (ID_help_report_a_problem, _("Report a problem..."));
198
199                 m->Append (file, _("&File"));
200 #ifndef __WXOSX__
201                 m->Append (edit, _("&Edit"));
202 #endif
203                 m->Append (view, _("&View"));
204                 m->Append (tools, _("&Tools"));
205                 m->Append (help, _("&Help"));
206         }
207
208         void file_open ()
209         {
210                 wxDirDialog* c = new wxDirDialog (
211                         this,
212                         _("Select DCP to open"),
213                         wxStandardPaths::Get().GetDocumentsDir(),
214                         wxDEFAULT_DIALOG_STYLE | wxDD_DIR_MUST_EXIST
215                         );
216
217                 int r;
218                 while (true) {
219                         r = c->ShowModal ();
220                         if (r == wxID_OK && c->GetPath() == wxStandardPaths::Get().GetDocumentsDir()) {
221                                 error_dialog (this, _("You did not select a folder.  Make sure that you select a folder before clicking Open."));
222                         } else {
223                                 break;
224                         }
225                 }
226
227                 if (r == wxID_OK) {
228                         load_dcp (wx_to_std (c->GetPath ()));
229                 }
230
231                 c->Destroy ();
232         }
233
234         void file_add_ov ()
235         {
236                 wxDirDialog* c = new wxDirDialog (
237                         this,
238                         _("Select DCP to open as OV"),
239                         wxStandardPaths::Get().GetDocumentsDir(),
240                         wxDEFAULT_DIALOG_STYLE | wxDD_DIR_MUST_EXIST
241                         );
242
243                 int r;
244                 while (true) {
245                         r = c->ShowModal ();
246                         if (r == wxID_OK && c->GetPath() == wxStandardPaths::Get().GetDocumentsDir()) {
247                                 error_dialog (this, _("You did not select a folder.  Make sure that you select a folder before clicking Open."));
248                         } else {
249                                 break;
250                         }
251                 }
252
253                 if (r == wxID_OK) {
254                         shared_ptr<DCPContent> dcp = boost::dynamic_pointer_cast<DCPContent>(_film->content().front());
255                         DCPOMATIC_ASSERT (dcp);
256                         dcp->add_ov (wx_to_std(c->GetPath()));
257                         dcp->examine (shared_ptr<Job>());
258                 }
259
260                 c->Destroy ();
261                 _info->triggered_update ();
262         }
263
264         void file_add_kdm ()
265         {
266                 wxFileDialog* d = new wxFileDialog (this, _("Select KDM"));
267
268                 if (d->ShowModal() == wxID_OK) {
269                         shared_ptr<DCPContent> dcp = boost::dynamic_pointer_cast<DCPContent>(_film->content().front());
270                         DCPOMATIC_ASSERT (dcp);
271                         try {
272                                 dcp->add_kdm (dcp::EncryptedKDM (dcp::file_to_string (wx_to_std (d->GetPath ()), MAX_KDM_SIZE)));
273                         } catch (exception& e) {
274                                 error_dialog (this, wxString::Format (_("Could not load KDM (%s)"), e.what ()));
275                                 d->Destroy ();
276                                 return;
277                         }
278
279                         dcp->examine (shared_ptr<Job>());
280                 }
281
282                 d->Destroy ();
283                 _info->triggered_update ();
284         }
285
286         void file_exit ()
287         {
288                 Close ();
289         }
290
291         void edit_preferences ()
292         {
293                 if (!_config_dialog) {
294                         _config_dialog = create_player_config_dialog ();
295                 }
296                 _config_dialog->Show (this);
297         }
298
299         void tools_check_for_updates ()
300         {
301                 UpdateChecker::instance()->run ();
302                 _update_news_requested = true;
303         }
304
305         void help_about ()
306         {
307                 AboutDialog* d = new AboutDialog (this);
308                 d->ShowModal ();
309                 d->Destroy ();
310         }
311
312         void help_report_a_problem ()
313         {
314                 ReportProblemDialog* d = new ReportProblemDialog (this);
315                 if (d->ShowModal () == wxID_OK) {
316                         d->report ();
317                 }
318                 d->Destroy ();
319         }
320
321         void update_checker_state_changed ()
322         {
323                 UpdateChecker* uc = UpdateChecker::instance ();
324
325                 bool const announce =
326                         _update_news_requested ||
327                         (uc->stable() && Config::instance()->check_for_updates()) ||
328                         (uc->test() && Config::instance()->check_for_updates() && Config::instance()->check_for_test_updates());
329
330                 _update_news_requested = false;
331
332                 if (!announce) {
333                         return;
334                 }
335
336                 if (uc->state() == UpdateChecker::YES) {
337                         UpdateDialog* dialog = new UpdateDialog (this, uc->stable (), uc->test ());
338                         dialog->ShowModal ();
339                         dialog->Destroy ();
340                 } else if (uc->state() == UpdateChecker::FAILED) {
341                         error_dialog (this, _("The DCP-o-matic download server could not be contacted."));
342                 } else {
343                         error_dialog (this, _("There are no new versions of DCP-o-matic available."));
344                 }
345
346                 _update_news_requested = false;
347         }
348
349         void config_changed ()
350         {
351                 /* Instantly save any config changes when using the player GUI */
352                 try {
353                         Config::instance()->write_config();
354                 } catch (exception& e) {
355                         error_dialog (
356                                 this,
357                                 wxString::Format (
358                                         _("Could not write to config file at %s.  Your changes have not been saved."),
359                                         std_to_wx (Config::instance()->cinemas_file().string()).data()
360                                         )
361                                 );
362                 }
363         }
364
365         bool _update_news_requested;
366         PlayerInformation* _info;
367         wxPreferencesEditor* _config_dialog;
368         FilmViewer* _viewer;
369         boost::shared_ptr<Film> _film;
370         boost::signals2::scoped_connection _config_changed_connection;
371 };
372
373 static const wxCmdLineEntryDesc command_line_description[] = {
374         { wxCMD_LINE_PARAM, 0, 0, "DCP to load or create", wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
375         { wxCMD_LINE_NONE, "", "", "", wxCmdLineParamType (0), 0 }
376 };
377
378 /** @class App
379  *  @brief The magic App class for wxWidgets.
380  */
381 class App : public wxApp
382 {
383 public:
384         App ()
385                 : wxApp ()
386                 , _frame (0)
387         {}
388
389 private:
390
391         bool OnInit ()
392         try
393         {
394                 wxInitAllImageHandlers ();
395
396                 Config::FailedToLoad.connect (boost::bind (&App::config_failed_to_load, this));
397
398                 wxSplashScreen* splash = 0;
399                 try {
400                         if (!Config::have_existing ("config.xml")) {
401                                 wxBitmap bitmap;
402                                 boost::filesystem::path p = shared_path () / "splash.png";
403                                 if (bitmap.LoadFile (std_to_wx (p.string ()), wxBITMAP_TYPE_PNG)) {
404                                         splash = new wxSplashScreen (bitmap, wxSPLASH_CENTRE_ON_SCREEN | wxSPLASH_NO_TIMEOUT, 0, 0, -1);
405                                         wxYield ();
406                                 }
407                         }
408                 } catch (boost::filesystem::filesystem_error& e) {
409                         /* Maybe we couldn't find the splash image; never mind */
410                 }
411
412                 SetAppName (_("DCP-o-matic Player"));
413
414                 if (!wxApp::OnInit()) {
415                         return false;
416                 }
417
418 #ifdef DCPOMATIC_LINUX
419                 unsetenv ("UBUNTU_MENUPROXY");
420 #endif
421
422 #ifdef __WXOSX__
423                 ProcessSerialNumber serial;
424                 GetCurrentProcess (&serial);
425                 TransformProcessType (&serial, kProcessTransformToForegroundApplication);
426 #endif
427
428                 dcpomatic_setup_path_encoding ();
429
430                 /* Enable i18n; this will create a Config object
431                    to look for a force-configured language.  This Config
432                    object will be wrong, however, because dcpomatic_setup
433                    hasn't yet been called and there aren't any filters etc.
434                    set up yet.
435                 */
436                 dcpomatic_setup_i18n ();
437
438                 /* Set things up, including filters etc.
439                    which will now be internationalised correctly.
440                 */
441                 dcpomatic_setup ();
442
443                 /* Force the configuration to be re-loaded correctly next
444                    time it is needed.
445                 */
446                 Config::drop ();
447
448                 _frame = new DOMFrame ();
449                 SetTopWindow (_frame);
450                 _frame->Maximize ();
451                 if (splash) {
452                         splash->Destroy ();
453                 }
454                 _frame->Show ();
455
456                 signal_manager = new wxSignalManager (this);
457
458                 if (!_dcp_to_load.empty() && boost::filesystem::is_directory (_dcp_to_load)) {
459                         try {
460                                 _frame->load_dcp (_dcp_to_load);
461                         } catch (exception& e) {
462                                 error_dialog (0, std_to_wx (String::compose (wx_to_std (_("Could not load DCP %1 (%2)")), _dcp_to_load, e.what())));
463                         }
464                 }
465
466                 Bind (wxEVT_IDLE, boost::bind (&App::idle, this));
467
468                 if (Config::instance()->check_for_updates ()) {
469                         UpdateChecker::instance()->run ();
470                 }
471
472                 return true;
473         }
474         catch (exception& e)
475         {
476                 error_dialog (0, wxString::Format ("DCP-o-matic Player could not start: %s", e.what ()));
477                 return true;
478         }
479
480         void OnInitCmdLine (wxCmdLineParser& parser)
481         {
482                 parser.SetDesc (command_line_description);
483                 parser.SetSwitchChars (wxT ("-"));
484         }
485
486         bool OnCmdLineParsed (wxCmdLineParser& parser)
487         {
488                 if (parser.GetParamCount() > 0) {
489                         _dcp_to_load = wx_to_std (parser.GetParam (0));
490                 }
491
492                 return true;
493         }
494
495         void report_exception ()
496         {
497                 try {
498                         throw;
499                 } catch (FileError& e) {
500                         error_dialog (
501                                 0,
502                                 wxString::Format (
503                                         _("An exception occurred: %s (%s)\n\n") + REPORT_PROBLEM,
504                                         std_to_wx (e.what()),
505                                         std_to_wx (e.file().string().c_str ())
506                                         )
507                                 );
508                 } catch (exception& e) {
509                         error_dialog (
510                                 0,
511                                 wxString::Format (
512                                         _("An exception occurred: %s.\n\n") + REPORT_PROBLEM,
513                                         std_to_wx (e.what ())
514                                         )
515                                 );
516                 } catch (...) {
517                         error_dialog (0, _("An unknown exception occurred.") + "  " + REPORT_PROBLEM);
518                 }
519         }
520
521         /* An unhandled exception has occurred inside the main event loop */
522         bool OnExceptionInMainLoop ()
523         {
524                 report_exception ();
525                 /* This will terminate the program */
526                 return false;
527         }
528
529         void OnUnhandledException ()
530         {
531                 report_exception ();
532         }
533
534         void idle ()
535         {
536                 signal_manager->ui_idle ();
537         }
538
539         void config_failed_to_load ()
540         {
541                 message_dialog (_frame, _("The existing configuration failed to load.  Default values will be used instead.  These may take a short time to create."));
542         }
543
544         DOMFrame* _frame;
545         string _dcp_to_load;
546 };
547
548 IMPLEMENT_APP (App)