Fix various problems with the closed caption viewer not being updated properly.
[dcpomatic.git] / src / wx / film_viewer.cc
1 /*
2     Copyright (C) 2012-2019 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 /** @file  src/film_viewer.cc
22  *  @brief A wx widget to view a preview of a Film.
23  */
24
25 #include "film_viewer.h"
26 #include "playhead_to_timecode_dialog.h"
27 #include "playhead_to_frame_dialog.h"
28 #include "wx_util.h"
29 #include "closed_captions_dialog.h"
30 #include "lib/film.h"
31 #include "lib/ratio.h"
32 #include "lib/util.h"
33 #include "lib/job_manager.h"
34 #include "lib/image.h"
35 #include "lib/exceptions.h"
36 #include "lib/examine_content_job.h"
37 #include "lib/filter.h"
38 #include "lib/player.h"
39 #include "lib/player_video.h"
40 #include "lib/video_content.h"
41 #include "lib/video_decoder.h"
42 #include "lib/timer.h"
43 #include "lib/butler.h"
44 #include "lib/log.h"
45 #include "lib/config.h"
46 #include "lib/compose.hpp"
47 #include "lib/dcpomatic_log.h"
48 #include "lib/text_content.h"
49 extern "C" {
50 #include <libavutil/pixfmt.h>
51 }
52 #include <dcp/exceptions.h>
53 #include <wx/tglbtn.h>
54 #include <iostream>
55 #include <iomanip>
56
57 using std::string;
58 using std::pair;
59 using std::min;
60 using std::max;
61 using std::cout;
62 using std::list;
63 using std::bad_alloc;
64 using std::make_pair;
65 using std::exception;
66 using boost::shared_ptr;
67 using boost::dynamic_pointer_cast;
68 using boost::weak_ptr;
69 using boost::optional;
70 using dcp::Size;
71
72 static
73 int
74 rtaudio_callback (void* out, void *, unsigned int frames, double, RtAudioStreamStatus, void* data)
75 {
76         return reinterpret_cast<FilmViewer*>(data)->audio_callback (out, frames);
77 }
78
79 FilmViewer::FilmViewer (wxWindow* p)
80         : _panel (new wxPanel (p))
81         , _coalesce_player_changes (false)
82         , _audio (DCPOMATIC_RTAUDIO_API)
83         , _audio_channels (0)
84         , _audio_block_size (1024)
85         , _playing (false)
86         , _latency_history_count (0)
87         , _dropped (0)
88         , _closed_captions_dialog (new ClosedCaptionsDialog(p, this))
89         , _outline_content (false)
90         , _eyes (EYES_LEFT)
91         , _pad_black (false)
92 #ifdef DCPOMATIC_VARIANT_SWAROOP
93         , _in_watermark (false)
94         , _background_image (false)
95 #endif
96 {
97 #ifndef __WXOSX__
98         _panel->SetDoubleBuffered (true);
99 #endif
100
101         _panel->SetBackgroundStyle (wxBG_STYLE_PAINT);
102         _panel->SetBackgroundColour (*wxBLACK);
103
104         _panel->Bind (wxEVT_PAINT, boost::bind (&FilmViewer::paint_panel, this));
105         _panel->Bind (wxEVT_SIZE,  boost::bind (&FilmViewer::panel_sized, this, _1));
106         _timer.Bind  (wxEVT_TIMER, boost::bind (&FilmViewer::timer, this));
107
108         set_film (shared_ptr<Film> ());
109
110         _config_changed_connection = Config::instance()->Changed.connect (bind (&FilmViewer::config_changed, this, _1));
111         config_changed (Config::SOUND_OUTPUT);
112 }
113
114 FilmViewer::~FilmViewer ()
115 {
116         stop ();
117 }
118
119 void
120 FilmViewer::set_film (shared_ptr<Film> film)
121 {
122         if (_film == film) {
123                 return;
124         }
125
126         _film = film;
127         _video_position = DCPTime ();
128         _player_video.first.reset ();
129         _player_video.second = DCPTime ();
130
131         _frame.reset ();
132         _closed_captions_dialog->clear ();
133
134         if (!_film) {
135                 _player.reset ();
136                 recreate_butler ();
137                 _frame.reset ();
138                 refresh_panel ();
139                 return;
140         }
141
142         try {
143                 _player.reset (new Player (_film, _film->playlist ()));
144                 _player->set_fast ();
145                 if (_dcp_decode_reduction) {
146                         _player->set_dcp_decode_reduction (_dcp_decode_reduction);
147                 }
148         } catch (bad_alloc &) {
149                 error_dialog (_panel, _("There is not enough free memory to do that."));
150                 _film.reset ();
151                 return;
152         }
153
154         _player->set_always_burn_open_subtitles ();
155         _player->set_play_referenced ();
156
157         _film->Change.connect (boost::bind (&FilmViewer::film_change, this, _1, _2));
158         _film->ContentChange.connect (boost::bind(&FilmViewer::content_change, this, _1, _3));
159         _player->Change.connect (boost::bind (&FilmViewer::player_change, this, _1, _2, _3));
160
161         /* Keep about 1 second's worth of history samples */
162         _latency_history_count = _film->audio_frame_rate() / _audio_block_size;
163
164         recreate_butler ();
165
166         calculate_sizes ();
167         slow_refresh ();
168 }
169
170 void
171 FilmViewer::recreate_butler ()
172 {
173         bool const was_running = stop ();
174         _butler.reset ();
175
176         if (!_film) {
177                 return;
178         }
179
180         AudioMapping map = AudioMapping (_film->audio_channels(), _audio_channels);
181
182         if (_audio_channels != 2 || _film->audio_channels() < 3) {
183                 for (int i = 0; i < min (_film->audio_channels(), _audio_channels); ++i) {
184                         map.set (i, i, 1);
185                 }
186         } else {
187                 /* Special case: stereo output, at least 3 channel input.
188                    Map so that Lt = L(-3dB) + Ls(-3dB) + C(-6dB) + Lfe(-10dB)
189                                Rt = R(-3dB) + Rs(-3dB) + C(-6dB) + Lfe(-10dB)
190                 */
191                 if (_film->audio_channels() > 0) {
192                         map.set (dcp::LEFT,   0, 1 / sqrt(2)); // L -> Lt
193                 }
194                 if (_film->audio_channels() > 1) {
195                         map.set (dcp::RIGHT,  1, 1 / sqrt(2)); // R -> Rt
196                 }
197                 if (_film->audio_channels() > 2) {
198                         map.set (dcp::CENTRE, 0, 1 / 2.0); // C -> Lt
199                         map.set (dcp::CENTRE, 1, 1 / 2.0); // C -> Rt
200                 }
201                 if (_film->audio_channels() > 3) {
202                         map.set (dcp::LFE,    0, 1 / sqrt(10)); // Lfe -> Lt
203                         map.set (dcp::LFE,    1, 1 / sqrt(10)); // Lfe -> Rt
204                 }
205                 if (_film->audio_channels() > 4) {
206                         map.set (dcp::LS,     0, 1 / sqrt(2)); // Ls -> Lt
207                 }
208                 if (_film->audio_channels() > 5) {
209                         map.set (dcp::RS,     1, 1 / sqrt(2)); // Rs -> Rt
210                 }
211         }
212
213         _butler.reset (new Butler(_player, map, _audio_channels, bind(&PlayerVideo::force, _1, AV_PIX_FMT_RGB24), false, true));
214         if (!Config::instance()->sound() && !_audio.isStreamOpen()) {
215                 _butler->disable_audio ();
216         }
217
218         _closed_captions_dialog->set_butler (_butler);
219
220         if (was_running) {
221                 start ();
222         }
223 }
224
225 void
226 FilmViewer::refresh_panel ()
227 {
228         _panel->Refresh ();
229         _panel->Update ();
230 }
231
232 void
233 FilmViewer::get ()
234 {
235         DCPOMATIC_ASSERT (_butler);
236
237         do {
238                 Butler::Error e;
239                 _player_video = _butler->get_video (&e);
240                 if (!_player_video.first && e == Butler::AGAIN) {
241                         signal_manager->when_idle (boost::bind(&FilmViewer::get, this));
242                         return;
243                 }
244         } while (
245                 _player_video.first &&
246                 _film->three_d() &&
247                 _eyes != _player_video.first->eyes() &&
248                 _player_video.first->eyes() != EYES_BOTH
249                 );
250
251         try {
252                 _butler->rethrow ();
253         } catch (DecodeError& e) {
254                 error_dialog (_panel, e.what());
255         }
256
257         display_player_video ();
258 }
259
260 void
261 FilmViewer::display_player_video ()
262 {
263         if (!_player_video.first) {
264                 _frame.reset ();
265                 refresh_panel ();
266                 return;
267         }
268
269         if (_playing && (time() - _player_video.second) > one_video_frame()) {
270                 /* Too late; just drop this frame before we try to get its image (which will be the time-consuming
271                    part if this frame is J2K).
272                 */
273                 _video_position = _player_video.second;
274                 ++_dropped;
275                 return;
276         }
277
278         /* In an ideal world, what we would do here is:
279          *
280          * 1. convert to XYZ exactly as we do in the DCP creation path.
281          * 2. convert back to RGB for the preview display, compensating
282          *    for the monitor etc. etc.
283          *
284          * but this is inefficient if the source is RGB.  Since we don't
285          * (currently) care too much about the precise accuracy of the preview's
286          * colour mapping (and we care more about its speed) we try to short-
287          * circuit this "ideal" situation in some cases.
288          *
289          * The content's specified colour conversion indicates the colourspace
290          * which the content is in (according to the user).
291          *
292          * PlayerVideo::image (bound to PlayerVideo::force) will take the source
293          * image and convert it (from whatever the user has said it is) to RGB.
294          */
295
296         _frame = _player_video.first->image (bind(&PlayerVideo::force, _1, AV_PIX_FMT_RGB24), false, true);
297
298         ImageChanged (_player_video.first);
299
300         _video_position = _player_video.second;
301         _inter_position = _player_video.first->inter_position ();
302         _inter_size = _player_video.first->inter_size ();
303
304         refresh_panel ();
305
306         _closed_captions_dialog->update (time());
307 }
308
309 void
310 FilmViewer::timer ()
311 {
312         if (!_film || !_playing) {
313                 return;
314         }
315
316         get ();
317         PositionChanged ();
318         DCPTime const next = _video_position + one_video_frame();
319
320         if (next >= _film->length()) {
321                 stop ();
322                 Finished ();
323                 return;
324         }
325
326         LOG_DEBUG_PLAYER("%1 -> %2; delay %3", next.seconds(), time().seconds(), max((next.seconds() - time().seconds()) * 1000, 1.0));
327         _timer.Start (max ((next.seconds() - time().seconds()) * 1000, 1.0), wxTIMER_ONE_SHOT);
328
329         if (_butler) {
330                 _butler->rethrow ();
331         }
332 }
333
334 bool
335 #ifdef DCPOMATIC_VARIANT_SWAROOP
336 FilmViewer::maybe_draw_background_image (wxPaintDC& dc)
337 {
338         optional<boost::filesystem::path> bg = Config::instance()->player_background_image();
339         if (bg) {
340                 wxImage image (std_to_wx(bg->string()));
341                 wxBitmap bitmap (image);
342                 dc.DrawBitmap (bitmap, max(0, (_panel_size.width - image.GetSize().GetWidth()) / 2), max(0, (_panel_size.height - image.GetSize().GetHeight()) / 2));
343                 return true;
344         }
345
346         return false;
347 }
348 #else
349 FilmViewer::maybe_draw_background_image (wxPaintDC &)
350 {
351         return false;
352 }
353 #endif
354
355 void
356 FilmViewer::paint_panel ()
357 {
358         wxPaintDC dc (_panel);
359
360 #ifdef DCPOMATIC_VARIANT_SWAROOP
361         if (_background_image) {
362                 dc.Clear ();
363                 maybe_draw_background_image (dc);
364                 return;
365         }
366 #endif
367
368         if (!_out_size.width || !_out_size.height || !_film || !_frame || _out_size != _frame->size()) {
369                 dc.Clear ();
370         } else {
371
372                 wxImage frame (_out_size.width, _out_size.height, _frame->data()[0], true);
373                 wxBitmap frame_bitmap (frame);
374                 dc.DrawBitmap (frame_bitmap, 0, max(0, (_panel_size.height - _out_size.height) / 2));
375
376 #ifdef DCPOMATIC_VARIANT_SWAROOP
377                 DCPTime const period = DCPTime::from_seconds(Config::instance()->player_watermark_period() * 60);
378                 int64_t n = _video_position.get() / period.get();
379                 DCPTime from(n * period.get());
380                 DCPTime to = from + DCPTime::from_seconds(Config::instance()->player_watermark_duration() / 1000.0);
381                 if (from <= _video_position && _video_position <= to) {
382                         if (!_in_watermark) {
383                                 _in_watermark = true;
384                                 _watermark_x = rand() % _panel_size.width;
385                                 _watermark_y = rand() % _panel_size.height;
386                         }
387                         dc.SetTextForeground(*wxWHITE);
388                         string wm = Config::instance()->player_watermark_theatre();
389                         boost::posix_time::ptime t = boost::posix_time::second_clock::local_time();
390                         wm += "\n" + boost::posix_time::to_iso_extended_string(t);
391                         dc.DrawText(std_to_wx(wm), _watermark_x, _watermark_y);
392                 } else {
393                         _in_watermark = false;
394                 }
395 #endif
396         }
397
398         if (_out_size.width < _panel_size.width) {
399                 /* XXX: these colours are right for GNOME; may need adjusting for other OS */
400                 wxPen   p (_pad_black ? wxColour(0, 0, 0) : wxColour(240, 240, 240));
401                 wxBrush b (_pad_black ? wxColour(0, 0, 0) : wxColour(240, 240, 240));
402                 dc.SetPen (p);
403                 dc.SetBrush (b);
404                 dc.DrawRectangle (_out_size.width, 0, _panel_size.width - _out_size.width, _panel_size.height);
405         }
406
407         if (_out_size.height < _panel_size.height) {
408                 wxPen   p (_pad_black ? wxColour(0, 0, 0) : wxColour(240, 240, 240));
409                 wxBrush b (_pad_black ? wxColour(0, 0, 0) : wxColour(240, 240, 240));
410                 dc.SetPen (p);
411                 dc.SetBrush (b);
412                 int const gap = (_panel_size.height - _out_size.height) / 2;
413                 dc.DrawRectangle (0, 0, _panel_size.width, gap);
414                 dc.DrawRectangle (0, gap + _out_size.height + 1, _panel_size.width, gap + 1);
415         }
416
417         if (_outline_content) {
418                 wxPen p (wxColour (255, 0, 0), 2);
419                 dc.SetPen (p);
420                 dc.SetBrush (*wxTRANSPARENT_BRUSH);
421                 dc.DrawRectangle (_inter_position.x, _inter_position.y + (_panel_size.height - _out_size.height) / 2, _inter_size.width, _inter_size.height);
422         }
423 }
424
425 void
426 FilmViewer::set_outline_content (bool o)
427 {
428         _outline_content = o;
429         refresh_panel ();
430 }
431
432 void
433 FilmViewer::set_eyes (Eyes e)
434 {
435         _eyes = e;
436         slow_refresh ();
437 }
438
439 void
440 FilmViewer::panel_sized (wxSizeEvent& ev)
441 {
442         _panel_size.width = ev.GetSize().GetWidth();
443         _panel_size.height = ev.GetSize().GetHeight();
444
445         calculate_sizes ();
446         if (!quick_refresh()) {
447                 slow_refresh ();
448         }
449         PositionChanged ();
450 }
451
452 void
453 FilmViewer::calculate_sizes ()
454 {
455         if (!_film || !_player) {
456                 return;
457         }
458
459         Ratio const * container = _film->container ();
460
461         float const panel_ratio = _panel_size.ratio ();
462         float const film_ratio = container ? container->ratio () : 1.78;
463
464         if (panel_ratio < film_ratio) {
465                 /* panel is less widscreen than the film; clamp width */
466                 _out_size.width = _panel_size.width;
467                 _out_size.height = lrintf (_out_size.width / film_ratio);
468         } else {
469                 /* panel is more widescreen than the film; clamp height */
470                 _out_size.height = _panel_size.height;
471                 _out_size.width = lrintf (_out_size.height * film_ratio);
472         }
473
474         /* Catch silly values */
475         _out_size.width = max (64, _out_size.width);
476         _out_size.height = max (64, _out_size.height);
477
478         _player->set_video_container_size (_out_size);
479 }
480
481 void
482 FilmViewer::start ()
483 {
484         if (!_film) {
485                 return;
486         }
487
488         optional<bool> v = PlaybackPermitted ();
489         if (v && !*v) {
490                 /* Computer says no */
491                 return;
492         }
493
494         if (_audio.isStreamOpen()) {
495                 _audio.setStreamTime (_video_position.seconds());
496                 _audio.startStream ();
497         }
498
499         _playing = true;
500         _dropped = 0;
501         timer ();
502         Started (position());
503 }
504
505 bool
506 FilmViewer::stop ()
507 {
508         if (_audio.isStreamRunning()) {
509                 /* stop stream and discard any remaining queued samples */
510                 _audio.abortStream ();
511         }
512
513         if (!_playing) {
514                 return false;
515         }
516
517         _playing = false;
518         Stopped (position());
519         return true;
520 }
521
522 void
523 FilmViewer::player_change (ChangeType type, int property, bool frequent)
524 {
525         if (type != CHANGE_TYPE_DONE || frequent) {
526                 return;
527         }
528
529         if (_coalesce_player_changes) {
530                 _pending_player_changes.push_back (property);
531                 return;
532         }
533
534         calculate_sizes ();
535         bool refreshed = false;
536         if (
537                 property == VideoContentProperty::CROP ||
538                 property == VideoContentProperty::SCALE ||
539                 property == VideoContentProperty::FADE_IN ||
540                 property == VideoContentProperty::FADE_OUT ||
541                 property == VideoContentProperty::COLOUR_CONVERSION ||
542                 property == PlayerProperty::VIDEO_CONTAINER_SIZE ||
543                 property == PlayerProperty::FILM_CONTAINER
544                 ) {
545                 refreshed = quick_refresh ();
546         }
547
548         if (!refreshed) {
549                 slow_refresh ();
550         }
551         PositionChanged ();
552 }
553
554 void
555 FilmViewer::film_change (ChangeType type, Film::Property p)
556 {
557         if (type == CHANGE_TYPE_DONE && p == Film::AUDIO_CHANNELS) {
558                 recreate_butler ();
559         } else if (p == Film::CONTENT) {
560                 _closed_captions_dialog->update_tracks (_film);
561         }
562 }
563
564 /** Re-get the current frame slowly by seeking */
565 void
566 FilmViewer::slow_refresh ()
567 {
568         seek (_video_position, true);
569 }
570
571 /** Try to re-get the current frame quickly by resetting the metadata
572  *  in the PlayerVideo that we used last time.
573  *  @return true if this was possible, false if not.
574  */
575 bool
576 FilmViewer::quick_refresh ()
577 {
578         if (!_player_video.first) {
579                 return false;
580         }
581
582         if (!_player_video.first->reset_metadata (_film, _player->video_container_size(), _film->frame_size())) {
583                 return false;
584         }
585
586         display_player_video ();
587         return true;
588 }
589
590 void
591 FilmViewer::seek (shared_ptr<Content> content, ContentTime t, bool accurate)
592 {
593         optional<DCPTime> dt = _player->content_time_to_dcp (content, t);
594         if (dt) {
595                 seek (*dt, accurate);
596         }
597 }
598
599 void
600 FilmViewer::set_coalesce_player_changes (bool c)
601 {
602         _coalesce_player_changes = c;
603
604         if (!c) {
605                 BOOST_FOREACH (int i, _pending_player_changes) {
606                         player_change (CHANGE_TYPE_DONE, i, false);
607                 }
608                 _pending_player_changes.clear ();
609         }
610 }
611
612 void
613 FilmViewer::seek (DCPTime t, bool accurate)
614 {
615         if (!_butler) {
616                 return;
617         }
618
619         if (t < DCPTime ()) {
620                 t = DCPTime ();
621         }
622
623         if (t >= _film->length ()) {
624                 t = _film->length ();
625         }
626
627         bool const was_running = stop ();
628
629         _closed_captions_dialog->clear ();
630         _butler->seek (t, accurate);
631         get ();
632
633         if (was_running) {
634                 start ();
635         }
636
637         PositionChanged ();
638 }
639
640 void
641 FilmViewer::config_changed (Config::Property p)
642 {
643 #ifdef DCPOMATIC_VARIANT_SWAROOP
644         if (p == Config::PLAYER_BACKGROUND_IMAGE) {
645                 refresh_panel ();
646                 return;
647         }
648 #endif
649
650         if (p != Config::SOUND && p != Config::SOUND_OUTPUT) {
651                 return;
652         }
653
654         if (_audio.isStreamOpen ()) {
655                 _audio.closeStream ();
656         }
657
658         if (Config::instance()->sound() && _audio.getDeviceCount() > 0) {
659                 unsigned int st = 0;
660                 if (Config::instance()->sound_output()) {
661                         while (st < _audio.getDeviceCount()) {
662                                 if (_audio.getDeviceInfo(st).name == Config::instance()->sound_output().get()) {
663                                         break;
664                                 }
665                                 ++st;
666                         }
667                         if (st == _audio.getDeviceCount()) {
668                                 st = _audio.getDefaultOutputDevice();
669                         }
670                 } else {
671                         st = _audio.getDefaultOutputDevice();
672                 }
673
674                 _audio_channels = _audio.getDeviceInfo(st).outputChannels;
675
676                 RtAudio::StreamParameters sp;
677                 sp.deviceId = st;
678                 sp.nChannels = _audio_channels;
679                 sp.firstChannel = 0;
680                 try {
681                         _audio.openStream (&sp, 0, RTAUDIO_FLOAT32, 48000, &_audio_block_size, &rtaudio_callback, this);
682 #ifdef DCPOMATIC_USE_RTERROR
683                 } catch (RtError& e) {
684 #else
685                 } catch (RtAudioError& e) {
686 #endif
687                         error_dialog (
688                                 _panel,
689                                 _("Could not set up audio output.  There will be no audio during the preview."), std_to_wx(e.what())
690                                 );
691                 }
692                 recreate_butler ();
693
694         } else {
695                 _audio_channels = 0;
696                 recreate_butler ();
697         }
698 }
699
700 DCPTime
701 FilmViewer::uncorrected_time () const
702 {
703         if (_audio.isStreamRunning ()) {
704                 return DCPTime::from_seconds (const_cast<RtAudio*>(&_audio)->getStreamTime());
705         }
706
707         return _video_position;
708 }
709
710 DCPTime
711 FilmViewer::time () const
712 {
713         if (_audio.isStreamRunning ()) {
714                 return DCPTime::from_seconds (const_cast<RtAudio*>(&_audio)->getStreamTime ()) -
715                         DCPTime::from_frames (average_latency(), _film->audio_frame_rate());
716         }
717
718         return _video_position;
719 }
720
721 int
722 FilmViewer::audio_callback (void* out_p, unsigned int frames)
723 {
724         while (true) {
725                 optional<DCPTime> t = _butler->get_audio (reinterpret_cast<float*> (out_p), frames);
726                 if (!t || DCPTime(uncorrected_time() - *t) < one_video_frame()) {
727                         /* There was an underrun or this audio is on time; carry on */
728                         break;
729                 }
730                 /* The audio we just got was (very) late; drop it and get some more. */
731         }
732
733         boost::mutex::scoped_lock lm (_latency_history_mutex, boost::try_to_lock);
734         if (lm) {
735                 _latency_history.push_back (_audio.getStreamLatency ());
736                 if (_latency_history.size() > static_cast<size_t> (_latency_history_count)) {
737                         _latency_history.pop_front ();
738                 }
739         }
740
741         return 0;
742 }
743
744 Frame
745 FilmViewer::average_latency () const
746 {
747         boost::mutex::scoped_lock lm (_latency_history_mutex);
748         if (_latency_history.empty()) {
749                 return 0;
750         }
751
752         Frame total = 0;
753         BOOST_FOREACH (Frame i, _latency_history) {
754                 total += i;
755         }
756
757         return total / _latency_history.size();
758 }
759
760 void
761 FilmViewer::set_dcp_decode_reduction (optional<int> reduction)
762 {
763         _dcp_decode_reduction = reduction;
764         if (_player) {
765                 _player->set_dcp_decode_reduction (reduction);
766         }
767 }
768
769 optional<int>
770 FilmViewer::dcp_decode_reduction () const
771 {
772         return _dcp_decode_reduction;
773 }
774
775 DCPTime
776 FilmViewer::one_video_frame () const
777 {
778         return DCPTime::from_frames (1, _film->video_frame_rate());
779 }
780
781 /** Open a dialog box showing our film's closed captions */
782 void
783 FilmViewer::show_closed_captions ()
784 {
785         _closed_captions_dialog->Show();
786 }
787
788 void
789 FilmViewer::seek_by (DCPTime by, bool accurate)
790 {
791         seek (_video_position + by, accurate);
792 }
793
794 void
795 FilmViewer::set_pad_black (bool p)
796 {
797         _pad_black = p;
798 }
799
800 void
801 FilmViewer::content_change (ChangeType type, int property)
802 {
803         if (type != CHANGE_TYPE_DONE) {
804                 return;
805         }
806
807         if (property == TextContentProperty::USE || property == TextContentProperty::TYPE || property == TextContentProperty::DCP_TRACK) {
808                 _closed_captions_dialog->update_tracks (_film);
809         }
810 }