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