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