Add stub sign language window.
[dcpomatic.git] / src / wx / film_viewer.cc
1 /*
2     Copyright (C) 2012-2021 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
22 /** @file  src/film_viewer.cc
23  *  @brief A wx widget to view a preview of a Film.
24  */
25
26
27 #include "closed_captions_dialog.h"
28 #include "film_viewer.h"
29 #include "gl_video_view.h"
30 #include "nag_dialog.h"
31 #include "playhead_to_frame_dialog.h"
32 #include "playhead_to_timecode_dialog.h"
33 #include "sign_language_dialog.h"
34 #include "simple_video_view.h"
35 #include "wx_util.h"
36 #include "lib/butler.h"
37 #include "lib/compose.hpp"
38 #include "lib/config.h"
39 #include "lib/dcpomatic_log.h"
40 #include "lib/examine_content_job.h"
41 #include "lib/exceptions.h"
42 #include "lib/film.h"
43 #include "lib/filter.h"
44 #include "lib/image.h"
45 #include "lib/job_manager.h"
46 #include "lib/log.h"
47 #include "lib/player.h"
48 #include "lib/player_video.h"
49 #include "lib/ratio.h"
50 #include "lib/text_content.h"
51 #include "lib/timer.h"
52 #include "lib/util.h"
53 #include "lib/video_content.h"
54 #include "lib/video_decoder.h"
55 #include <dcp/exceptions.h>
56 #include <dcp/warnings.h>
57 extern "C" {
58 #include <libavutil/pixfmt.h>
59 }
60 LIBDCP_DISABLE_WARNINGS
61 #include <wx/tglbtn.h>
62 LIBDCP_ENABLE_WARNINGS
63 #include <iomanip>
64
65
66 using std::bad_alloc;
67 using std::dynamic_pointer_cast;
68 using std::make_shared;
69 using std::max;
70 using std::shared_ptr;
71 using std::string;
72 using std::vector;
73 using boost::optional;
74 #if BOOST_VERSION >= 106100
75 using namespace boost::placeholders;
76 #endif
77 using dcp::Size;
78 using namespace dcpomatic;
79
80
81 static
82 int
83 rtaudio_callback (void* out, void *, unsigned int frames, double, RtAudioStreamStatus, void* data)
84 {
85         return reinterpret_cast<FilmViewer*>(data)->audio_callback (out, frames);
86 }
87
88
89 FilmViewer::FilmViewer (wxWindow* p)
90         : _audio (DCPOMATIC_RTAUDIO_API)
91         , _closed_captions_dialog (new ClosedCaptionsDialog(p, this))
92         , _sign_language_dialog(new SignLanguageDialog(p))
93 {
94 #if wxCHECK_VERSION(3, 1, 0)
95         switch (Config::instance()->video_view_type()) {
96         case Config::VIDEO_VIEW_OPENGL:
97                 _video_view = std::make_shared<GLVideoView>(this, p);
98                 break;
99         case Config::VIDEO_VIEW_SIMPLE:
100                 _video_view = std::make_shared<SimpleVideoView>(this, p);
101                 break;
102         }
103 #else
104         _video_view = std::make_shared<SimpleVideoView>(this, p);
105 #endif
106
107         _video_view->Sized.connect (boost::bind(&FilmViewer::video_view_sized, this));
108         _video_view->TooManyDropped.connect (boost::bind(boost::ref(TooManyDropped)));
109
110         set_film (shared_ptr<Film>());
111
112         _config_changed_connection = Config::instance()->Changed.connect(bind(&FilmViewer::config_changed, this, _1));
113         config_changed (Config::SOUND_OUTPUT);
114 }
115
116
117 FilmViewer::~FilmViewer ()
118 {
119         stop ();
120 }
121
122
123 /** Ask for ::idle_handler() to be called next time we are idle */
124 void
125 FilmViewer::request_idle_display_next_frame ()
126 {
127         if (_idle_get) {
128                 return;
129         }
130
131         _idle_get = true;
132         DCPOMATIC_ASSERT (signal_manager);
133         signal_manager->when_idle (boost::bind(&FilmViewer::idle_handler, this));
134 }
135
136
137 void
138 FilmViewer::idle_handler ()
139 {
140         if (!_idle_get) {
141                 return;
142         }
143
144         if (_video_view->display_next_frame(true) == VideoView::AGAIN) {
145                 /* get() could not complete quickly so we'll try again later */
146                 signal_manager->when_idle (boost::bind(&FilmViewer::idle_handler, this));
147         } else {
148                 _idle_get = false;
149         }
150 }
151
152
153 void
154 FilmViewer::set_film (shared_ptr<Film> film)
155 {
156         if (_film == film) {
157                 return;
158         }
159
160         _film = film;
161
162         _video_view->clear ();
163         _closed_captions_dialog->clear ();
164
165         destroy_butler();
166
167         if (!_film) {
168                 _player = boost::none;
169                 resume();
170                 _video_view->update ();
171                 return;
172         }
173
174         try {
175                 _player.emplace(_film, _optimise_for_j2k ? Image::Alignment::COMPACT : Image::Alignment::PADDED);
176                 _player->set_fast ();
177                 if (_dcp_decode_reduction) {
178                         _player->set_dcp_decode_reduction (_dcp_decode_reduction);
179                 }
180         } catch (bad_alloc &) {
181                 error_dialog (_video_view->get(), _("There is not enough free memory to do that."));
182                 _film.reset ();
183                 resume();
184                 return;
185         }
186
187         _player->set_always_burn_open_subtitles ();
188         _player->set_play_referenced ();
189
190         _film->Change.connect (boost::bind (&FilmViewer::film_change, this, _1, _2));
191         _film->LengthChange.connect (boost::bind(&FilmViewer::film_length_change, this));
192         _player->Change.connect (boost::bind (&FilmViewer::player_change, this, _1, _2, _3));
193
194         film_change (ChangeType::DONE, Film::Property::VIDEO_FRAME_RATE);
195         film_change (ChangeType::DONE, Film::Property::THREE_D);
196         film_length_change ();
197
198         /* Keep about 1 second's worth of history samples */
199         _latency_history_count = _film->audio_frame_rate() / _audio_block_size;
200
201         _closed_captions_dialog->update_tracks (_film);
202
203         create_butler();
204
205         calculate_sizes ();
206         slow_refresh ();
207 }
208
209
210 void
211 FilmViewer::destroy_butler()
212 {
213         suspend ();
214         _butler.reset ();
215 }
216
217
218 void
219 FilmViewer::destroy_and_maybe_create_butler()
220 {
221         destroy_butler();
222
223         if (!_film) {
224                 resume ();
225                 return;
226         }
227
228         create_butler();
229 }
230
231
232 void
233 FilmViewer::create_butler()
234 {
235 #if wxCHECK_VERSION(3, 1, 0)
236         auto const j2k_gl_optimised = dynamic_pointer_cast<GLVideoView>(_video_view) && _optimise_for_j2k;
237 #else
238         auto const j2k_gl_optimised = false;
239 #endif
240
241         DCPOMATIC_ASSERT(_player);
242
243         _butler = std::make_shared<Butler>(
244                 _film,
245                 *_player,
246                 Config::instance()->audio_mapping(_audio_channels),
247                 _audio_channels,
248                 boost::bind(&PlayerVideo::force, AV_PIX_FMT_RGB24),
249                 VideoRange::FULL,
250                 j2k_gl_optimised ? Image::Alignment::COMPACT : Image::Alignment::PADDED,
251                 true,
252                 j2k_gl_optimised,
253                 (Config::instance()->sound() && _audio.isStreamOpen()) ? Butler::Audio::ENABLED : Butler::Audio::DISABLED
254                 );
255
256         _closed_captions_dialog->set_butler (_butler);
257
258         resume ();
259 }
260
261
262 void
263 FilmViewer::set_outline_content (bool o)
264 {
265         _outline_content = o;
266         _video_view->update ();
267 }
268
269
270 void
271 FilmViewer::set_outline_subtitles (optional<dcpomatic::Rect<double>> rect)
272 {
273         _outline_subtitles = rect;
274         _video_view->update ();
275 }
276
277
278 void
279 FilmViewer::set_eyes (Eyes e)
280 {
281         _video_view->set_eyes (e);
282         slow_refresh ();
283 }
284
285
286 void
287 FilmViewer::video_view_sized ()
288 {
289         calculate_sizes ();
290         if (!quick_refresh()) {
291                 slow_refresh ();
292         }
293 }
294
295
296 void
297 FilmViewer::calculate_sizes ()
298 {
299         if (!_film || !_player) {
300                 return;
301         }
302
303         auto const container = _film->container ();
304
305         auto const scale = dpi_scale_factor (_video_view->get());
306         int const video_view_width = std::round(_video_view->get()->GetSize().x * scale);
307         int const video_view_height = std::round(_video_view->get()->GetSize().y * scale);
308
309         auto const view_ratio = float(video_view_width) / video_view_height;
310         auto const film_ratio = container ? container->ratio () : 1.78;
311
312         dcp::Size out_size;
313         if (view_ratio < film_ratio) {
314                 /* panel is less widscreen than the film; clamp width */
315                 out_size.width = video_view_width;
316                 out_size.height = lrintf (out_size.width / film_ratio);
317         } else {
318                 /* panel is more widescreen than the film; clamp height */
319                 out_size.height = video_view_height;
320                 out_size.width = lrintf (out_size.height * film_ratio);
321         }
322
323         /* Catch silly values */
324         out_size.width = max (64, out_size.width);
325         out_size.height = max (64, out_size.height);
326
327         /* Make sure the video container sizes are always a multiple of 2 so that
328          * we don't get gaps with subsampled sources (e.g. YUV420)
329          */
330         if (out_size.width % 2) {
331                 out_size.width++;
332         }
333         if (out_size.height % 2) {
334                 out_size.height++;
335         }
336
337         _player->set_video_container_size (out_size);
338 }
339
340
341 void
342 FilmViewer::suspend ()
343 {
344         ++_suspended;
345         if (_audio.isStreamRunning()) {
346                 _audio.abortStream();
347         }
348 }
349
350
351 void
352 FilmViewer::start_audio_stream_if_open ()
353 {
354         if (_audio.isStreamOpen()) {
355                 _audio.setStreamTime (_video_view->position().seconds());
356                 try {
357                         _audio.startStream ();
358                 } catch (RtAudioError& e) {
359                         _audio_channels = 0;
360                         error_dialog (
361                                 _video_view->get(),
362                                 _("There was a problem starting audio playback.  Please try another audio output device in Preferences."), std_to_wx(e.what())
363                                 );
364                 }
365         }
366 }
367
368
369 void
370 FilmViewer::resume ()
371 {
372         DCPOMATIC_ASSERT (_suspended > 0);
373         --_suspended;
374         if (_playing && !_suspended) {
375                 start_audio_stream_if_open ();
376                 _video_view->start ();
377         }
378 }
379
380
381 void
382 FilmViewer::start ()
383 {
384         if (!_film) {
385                 return;
386         }
387
388         auto v = PlaybackPermitted ();
389         if (v && !*v) {
390                 /* Computer says no */
391                 return;
392         }
393
394         /* We are about to set up the audio stream from the position of the video view.
395            If there is `lazy' seek in progress we need to wait for it to go through so that
396            _video_view->position() gives us a sensible answer.
397          */
398         while (_idle_get) {
399                 idle_handler ();
400         }
401
402         /* Take the video view's idea of position as our `playhead' and start the
403            audio stream (which is the timing reference) there.
404          */
405         start_audio_stream_if_open ();
406
407         _playing = true;
408         /* Calling start() below may directly result in Stopped being emitted, and if that
409          * happens we want it to come after the Started signal, so do that first.
410          */
411         Started ();
412         _video_view->start ();
413 }
414
415
416 bool
417 FilmViewer::stop ()
418 {
419         if (_audio.isStreamRunning()) {
420                 /* stop stream and discard any remaining queued samples */
421                 _audio.abortStream ();
422         }
423
424         if (!_playing) {
425                 return false;
426         }
427
428         _playing = false;
429         _video_view->stop ();
430         Stopped ();
431
432         _video_view->rethrow ();
433         return true;
434 }
435
436
437 void
438 FilmViewer::player_change (ChangeType type, int property, bool frequent)
439 {
440         if (type != ChangeType::DONE || frequent) {
441                 return;
442         }
443
444         if (_coalesce_player_changes) {
445                 _pending_player_changes.push_back (property);
446                 return;
447         }
448
449         player_change ({property});
450 }
451
452
453 void
454 FilmViewer::player_change (vector<int> properties)
455 {
456         calculate_sizes ();
457
458         bool try_quick_refresh = false;
459         bool update_ccap_tracks = false;
460
461         for (auto i: properties) {
462                 if (
463                         i == VideoContentProperty::CROP ||
464                         i == VideoContentProperty::CUSTOM_RATIO ||
465                         i == VideoContentProperty::CUSTOM_SIZE ||
466                         i == VideoContentProperty::FADE_IN ||
467                         i == VideoContentProperty::FADE_OUT ||
468                         i == VideoContentProperty::COLOUR_CONVERSION ||
469                         i == PlayerProperty::VIDEO_CONTAINER_SIZE ||
470                         i == PlayerProperty::FILM_CONTAINER
471                    ) {
472                         try_quick_refresh = true;
473                 }
474
475                 if (i == TextContentProperty::USE || i == TextContentProperty::TYPE || i == TextContentProperty::DCP_TRACK) {
476                         update_ccap_tracks = true;
477                 }
478         }
479
480         if (!try_quick_refresh || !quick_refresh()) {
481                 slow_refresh ();
482         }
483
484         if (update_ccap_tracks) {
485                 _closed_captions_dialog->update_tracks (_film);
486         }
487 }
488
489
490 void
491 FilmViewer::film_change (ChangeType type, Film::Property p)
492 {
493         if (type != ChangeType::DONE) {
494                 return;
495         }
496
497         if (p == Film::Property::AUDIO_CHANNELS) {
498                 destroy_and_maybe_create_butler();
499         } else if (p == Film::Property::VIDEO_FRAME_RATE) {
500                 _video_view->set_video_frame_rate (_film->video_frame_rate());
501         } else if (p == Film::Property::THREE_D) {
502                 _video_view->set_three_d (_film->three_d());
503         } else if (p == Film::Property::CONTENT) {
504                 _closed_captions_dialog->update_tracks (_film);
505         }
506 }
507
508
509 void
510 FilmViewer::film_length_change ()
511 {
512         _video_view->set_length (_film->length());
513 }
514
515
516 /** Re-get the current frame slowly by seeking */
517 void
518 FilmViewer::slow_refresh ()
519 {
520         seek (_video_view->position(), true);
521 }
522
523
524 /** Try to re-get the current frame quickly by resetting the metadata
525  *  in the PlayerVideo that we used last time.
526  *  @return true if this was possible, false if not.
527  */
528 bool
529 FilmViewer::quick_refresh ()
530 {
531         if (!_video_view || !_film || !_player) {
532                 return true;
533         }
534         return _video_view->reset_metadata (_film, _player->video_container_size());
535 }
536
537
538 void
539 FilmViewer::seek (shared_ptr<Content> content, ContentTime t, bool accurate)
540 {
541         DCPOMATIC_ASSERT(_player);
542         auto dt = _player->content_time_to_dcp (content, t);
543         if (dt) {
544                 seek (*dt, accurate);
545         }
546 }
547
548
549 void
550 FilmViewer::set_coalesce_player_changes (bool c)
551 {
552         _coalesce_player_changes = c;
553
554         if (!c) {
555                 player_change (_pending_player_changes);
556                 _pending_player_changes.clear ();
557         }
558 }
559
560
561 void
562 FilmViewer::seek (DCPTime t, bool accurate)
563 {
564         if (!_butler) {
565                 return;
566         }
567
568         if (t < DCPTime()) {
569                 t = DCPTime ();
570         }
571
572         if (t >= _film->length()) {
573                 t = _film->length() - one_video_frame();
574         }
575
576         suspend ();
577
578         _closed_captions_dialog->clear ();
579         _butler->seek (t, accurate);
580
581         if (!_playing) {
582                 /* We're not playing, so let the GUI thread get on and
583                    come back later to get the next frame after the seek.
584                 */
585                 request_idle_display_next_frame ();
586         } else {
587                 /* We're going to start playing again straight away
588                    so wait for the seek to finish.
589                 */
590                 while (_video_view->display_next_frame(false) == VideoView::AGAIN) {}
591         }
592
593         resume ();
594 }
595
596
597 void
598 FilmViewer::config_changed (Config::Property p)
599 {
600         if (p == Config::AUDIO_MAPPING) {
601                 destroy_and_maybe_create_butler();
602                 return;
603         }
604
605         if (p != Config::SOUND && p != Config::SOUND_OUTPUT) {
606                 return;
607         }
608
609         if (_audio.isStreamOpen ()) {
610                 _audio.closeStream ();
611         }
612
613         if (Config::instance()->sound() && _audio.getDeviceCount() > 0) {
614                 unsigned int st = 0;
615                 if (Config::instance()->sound_output()) {
616                         while (st < _audio.getDeviceCount()) {
617                                 try {
618                                         if (_audio.getDeviceInfo(st).name == Config::instance()->sound_output().get()) {
619                                                 break;
620                                         }
621                                 } catch (RtAudioError&) {
622                                         /* Something went wrong with that device so we don't want to use it anyway */
623                                 }
624                                 ++st;
625                         }
626                         if (st == _audio.getDeviceCount()) {
627                                 st = _audio.getDefaultOutputDevice();
628                         }
629                 } else {
630                         st = _audio.getDefaultOutputDevice();
631                 }
632
633                 try {
634                         _audio_channels = _audio.getDeviceInfo(st).outputChannels;
635                         RtAudio::StreamParameters sp;
636                         sp.deviceId = st;
637                         sp.nChannels = _audio_channels;
638                         sp.firstChannel = 0;
639                         _audio.openStream (&sp, 0, RTAUDIO_FLOAT32, 48000, &_audio_block_size, &rtaudio_callback, this);
640                 } catch (RtAudioError& e) {
641                         _audio_channels = 0;
642                         error_dialog (
643                                 _video_view->get(),
644                                 _("Could not set up audio output.  There will be no audio during the preview."), std_to_wx(e.what())
645                                 );
646                 }
647                 destroy_and_maybe_create_butler();
648
649         } else {
650                 _audio_channels = 0;
651                 destroy_and_maybe_create_butler();
652         }
653 }
654
655
656 DCPTime
657 FilmViewer::uncorrected_time () const
658 {
659         if (_audio.isStreamRunning()) {
660                 return DCPTime::from_seconds (const_cast<RtAudio*>(&_audio)->getStreamTime());
661         }
662
663         return _video_view->position();
664 }
665
666
667 optional<DCPTime>
668 FilmViewer::audio_time () const
669 {
670         if (!_audio.isStreamRunning()) {
671                 return {};
672         }
673
674         return DCPTime::from_seconds (const_cast<RtAudio*>(&_audio)->getStreamTime ()) -
675                 DCPTime::from_frames (average_latency(), _film->audio_frame_rate());
676 }
677
678
679 DCPTime
680 FilmViewer::time () const
681 {
682         return audio_time().get_value_or(_video_view->position());
683 }
684
685
686 int
687 FilmViewer::audio_callback (void* out_p, unsigned int frames)
688 {
689         while (true) {
690                 auto t = _butler->get_audio (Butler::Behaviour::NON_BLOCKING, reinterpret_cast<float*> (out_p), frames);
691                 if (!t || DCPTime(uncorrected_time() - *t) < one_video_frame()) {
692                         /* There was an underrun or this audio is on time; carry on */
693                         break;
694                 }
695                 /* The audio we just got was (very) late; drop it and get some more. */
696         }
697
698         boost::mutex::scoped_lock lm (_latency_history_mutex, boost::try_to_lock);
699         if (lm) {
700                 _latency_history.push_back (_audio.getStreamLatency ());
701                 if (_latency_history.size() > static_cast<size_t> (_latency_history_count)) {
702                         _latency_history.pop_front ();
703                 }
704         }
705
706         return 0;
707 }
708
709
710 Frame
711 FilmViewer::average_latency () const
712 {
713         boost::mutex::scoped_lock lm (_latency_history_mutex);
714         if (_latency_history.empty()) {
715                 return 0;
716         }
717
718         Frame total = 0;
719         for (auto i: _latency_history) {
720                 total += i;
721         }
722
723         return total / _latency_history.size();
724 }
725
726
727 void
728 FilmViewer::set_dcp_decode_reduction (optional<int> reduction)
729 {
730         _dcp_decode_reduction = reduction;
731         if (_player) {
732                 _player->set_dcp_decode_reduction (reduction);
733         }
734 }
735
736
737 optional<int>
738 FilmViewer::dcp_decode_reduction () const
739 {
740         return _dcp_decode_reduction;
741 }
742
743
744 optional<ContentTime>
745 FilmViewer::position_in_content (shared_ptr<const Content> content) const
746 {
747         DCPOMATIC_ASSERT(_player);
748         return _player->dcp_to_content_time (content, position());
749 }
750
751
752 DCPTime
753 FilmViewer::one_video_frame () const
754 {
755         return DCPTime::from_frames (1, _film ? _film->video_frame_rate() : 24);
756 }
757
758
759 /** Open a dialog box showing our film's closed captions */
760 void
761 FilmViewer::show_closed_captions ()
762 {
763         _closed_captions_dialog->Show();
764 }
765
766
767 void
768 FilmViewer::show_sign_language()
769 {
770         _sign_language_dialog->Show();
771 }
772
773
774 void
775 FilmViewer::seek_by (DCPTime by, bool accurate)
776 {
777         seek (_video_view->position() + by, accurate);
778 }
779
780
781 void
782 FilmViewer::set_pad_black (bool p)
783 {
784         _pad_black = p;
785 }
786
787
788 /** Called when a player has finished the current film.
789  *  May be called from a non-UI thread.
790  */
791 void
792 FilmViewer::finished ()
793 {
794         emit (boost::bind(&FilmViewer::ui_finished, this));
795 }
796
797
798 /** Called by finished() in the UI thread */
799 void
800 FilmViewer::ui_finished ()
801 {
802         stop ();
803         Finished ();
804 }
805
806
807 int
808 FilmViewer::dropped () const
809 {
810         return _video_view->dropped ();
811 }
812
813
814 int
815 FilmViewer::errored () const
816 {
817         return _video_view->errored ();
818 }
819
820
821 int
822 FilmViewer::gets () const
823 {
824         return _video_view->gets ();
825 }
826
827
828 void
829 FilmViewer::image_changed (shared_ptr<PlayerVideo> pv)
830 {
831         emit (boost::bind(boost::ref(ImageChanged), pv));
832 }
833
834
835 void
836 FilmViewer::set_optimise_for_j2k (bool o)
837 {
838         _optimise_for_j2k = o;
839         _video_view->set_optimise_for_j2k (o);
840 }
841
842
843 void
844 FilmViewer::set_crop_guess (dcpomatic::Rect<float> crop)
845 {
846         if (crop != _crop_guess) {
847                 _crop_guess = crop;
848                 _video_view->update ();
849         }
850 }
851
852
853 void
854 FilmViewer::unset_crop_guess ()
855 {
856         _crop_guess = boost::none;
857         _video_view->update ();
858 }
859