Very basic closed caption viewer.
[dcpomatic.git] / src / wx / film_viewer.cc
1 /*
2     Copyright (C) 2012-2017 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 /** @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_view.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 extern "C" {
47 #include <libavutil/pixfmt.h>
48 }
49 #include <dcp/exceptions.h>
50 #include <wx/tglbtn.h>
51 #include <iostream>
52 #include <iomanip>
53
54 using std::string;
55 using std::pair;
56 using std::min;
57 using std::max;
58 using std::cout;
59 using std::list;
60 using std::bad_alloc;
61 using std::make_pair;
62 using std::exception;
63 using boost::shared_ptr;
64 using boost::dynamic_pointer_cast;
65 using boost::weak_ptr;
66 using boost::optional;
67 using dcp::Size;
68
69 static
70 int
71 rtaudio_callback (void* out, void *, unsigned int frames, double, RtAudioStreamStatus, void* data)
72 {
73         return reinterpret_cast<FilmViewer*>(data)->audio_callback (out, frames);
74 }
75
76 FilmViewer::FilmViewer (wxWindow* p, bool outline_content, bool jump_to_selected)
77         : wxPanel (p)
78         , _panel (new wxPanel (this))
79         , _outline_content (0)
80         , _eye (0)
81         , _jump_to_selected (0)
82         , _slider (new wxSlider (this, wxID_ANY, 0, 0, 4096))
83         , _rewind_button (new wxButton (this, wxID_ANY, wxT("|<")))
84         , _back_button (new wxButton (this, wxID_ANY, wxT("<")))
85         , _forward_button (new wxButton (this, wxID_ANY, wxT(">")))
86         , _frame_number (new wxStaticText (this, wxID_ANY, wxT("")))
87         , _timecode (new wxStaticText (this, wxID_ANY, wxT("")))
88         , _play_button (new wxToggleButton (this, wxID_ANY, _("Play")))
89         , _coalesce_player_changes (false)
90         , _slider_being_moved (false)
91         , _was_running_before_slider (false)
92         , _audio (DCPOMATIC_RTAUDIO_API)
93         , _audio_channels (0)
94         , _audio_block_size (1024)
95         , _playing (false)
96         , _latency_history_count (0)
97         , _dropped (0)
98         , _closed_captions_dialog (new ClosedCaptionsDialog(GetParent()))
99 {
100 #ifndef __WXOSX__
101         _panel->SetDoubleBuffered (true);
102 #endif
103
104         _panel->SetBackgroundStyle (wxBG_STYLE_PAINT);
105
106         _v_sizer = new wxBoxSizer (wxVERTICAL);
107         SetSizer (_v_sizer);
108
109         _v_sizer->Add (_panel, 1, wxEXPAND);
110
111         wxBoxSizer* view_options = new wxBoxSizer (wxHORIZONTAL);
112         if (outline_content) {
113                 _outline_content = new wxCheckBox (this, wxID_ANY, _("Outline content"));
114                 view_options->Add (_outline_content, 0, wxRIGHT | wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_GAP);
115         }
116
117         _eye = new wxChoice (this, wxID_ANY);
118         _eye->Append (_("Left"));
119         _eye->Append (_("Right"));
120         _eye->SetSelection (0);
121         view_options->Add (_eye, 0, wxLEFT | wxRIGHT | wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_GAP);
122
123         if (jump_to_selected) {
124                 _jump_to_selected = new wxCheckBox (this, wxID_ANY, _("Jump to selected content"));
125                 view_options->Add (_jump_to_selected, 0, wxLEFT | wxRIGHT | wxALIGN_CENTER_VERTICAL, DCPOMATIC_SIZER_GAP);
126         }
127
128         _v_sizer->Add (view_options, 0, wxALL, DCPOMATIC_SIZER_GAP);
129
130         wxBoxSizer* h_sizer = new wxBoxSizer (wxHORIZONTAL);
131
132         wxBoxSizer* time_sizer = new wxBoxSizer (wxVERTICAL);
133         time_sizer->Add (_frame_number, 0, wxEXPAND);
134         time_sizer->Add (_timecode, 0, wxEXPAND);
135
136         h_sizer->Add (_rewind_button, 0, wxALL, 2);
137         h_sizer->Add (_back_button, 0, wxALL, 2);
138         h_sizer->Add (time_sizer, 0, wxEXPAND);
139         h_sizer->Add (_forward_button, 0, wxALL, 2);
140         h_sizer->Add (_play_button, 0, wxEXPAND);
141         h_sizer->Add (_slider, 1, wxEXPAND);
142
143         _v_sizer->Add (h_sizer, 0, wxEXPAND | wxALL, 6);
144
145         _frame_number->SetMinSize (wxSize (84, -1));
146         _rewind_button->SetMinSize (wxSize (32, -1));
147         _back_button->SetMinSize (wxSize (32, -1));
148         _forward_button->SetMinSize (wxSize (32, -1));
149
150         _panel->Bind            (wxEVT_PAINT,               boost::bind (&FilmViewer::paint_panel,     this));
151         _panel->Bind            (wxEVT_SIZE,                boost::bind (&FilmViewer::panel_sized,     this, _1));
152         if (_outline_content) {
153                 _outline_content->Bind  (wxEVT_CHECKBOX,    boost::bind (&FilmViewer::refresh_panel,   this));
154         }
155         _eye->Bind              (wxEVT_CHOICE,              boost::bind (&FilmViewer::slow_refresh,    this));
156         _slider->Bind           (wxEVT_SCROLL_THUMBTRACK,   boost::bind (&FilmViewer::slider_moved,    this, false));
157         _slider->Bind           (wxEVT_SCROLL_PAGEUP,       boost::bind (&FilmViewer::slider_moved,    this, true));
158         _slider->Bind           (wxEVT_SCROLL_PAGEDOWN,     boost::bind (&FilmViewer::slider_moved,    this, true));
159         _slider->Bind           (wxEVT_SCROLL_THUMBRELEASE, boost::bind (&FilmViewer::slider_released, this));
160         _play_button->Bind      (wxEVT_TOGGLEBUTTON,        boost::bind (&FilmViewer::play_clicked,    this));
161         _timer.Bind             (wxEVT_TIMER,               boost::bind (&FilmViewer::timer,           this));
162         _rewind_button->Bind    (wxEVT_LEFT_DOWN,           boost::bind (&FilmViewer::rewind_clicked,  this, _1));
163         _back_button->Bind      (wxEVT_LEFT_DOWN,           boost::bind (&FilmViewer::back_clicked,    this, _1));
164         _forward_button->Bind   (wxEVT_LEFT_DOWN,           boost::bind (&FilmViewer::forward_clicked, this, _1));
165         _frame_number->Bind     (wxEVT_LEFT_DOWN,           boost::bind (&FilmViewer::frame_number_clicked, this));
166         _timecode->Bind         (wxEVT_LEFT_DOWN,           boost::bind (&FilmViewer::timecode_clicked, this));
167         if (_jump_to_selected) {
168                 _jump_to_selected->Bind (wxEVT_CHECKBOX, boost::bind (&FilmViewer::jump_to_selected_clicked, this));
169                 _jump_to_selected->SetValue (Config::instance()->jump_to_selected ());
170         }
171
172         set_film (shared_ptr<Film> ());
173
174         JobManager::instance()->ActiveJobsChanged.connect (
175                 bind (&FilmViewer::active_jobs_changed, this, _2)
176                 );
177
178         setup_sensitivity ();
179
180         _config_changed_connection = Config::instance()->Changed.connect (bind (&FilmViewer::config_changed, this, _1));
181         config_changed (Config::SOUND_OUTPUT);
182 }
183
184 FilmViewer::~FilmViewer ()
185 {
186         stop ();
187 }
188
189 void
190 FilmViewer::set_film (shared_ptr<Film> film)
191 {
192         if (_film == film) {
193                 return;
194         }
195
196         _film = film;
197
198         _frame.reset ();
199         _closed_captions_dialog->clear ();
200
201         update_position_slider ();
202         update_position_label ();
203
204         if (!_film) {
205                 _player.reset ();
206                 recreate_butler ();
207                 _frame.reset ();
208                 refresh_panel ();
209                 return;
210         }
211
212         try {
213                 _player.reset (new Player (_film, _film->playlist ()));
214                 _player->set_fast ();
215                 if (_dcp_decode_reduction) {
216                         _player->set_dcp_decode_reduction (_dcp_decode_reduction);
217                 }
218         } catch (bad_alloc) {
219                 error_dialog (this, _("There is not enough free memory to do that."));
220                 _film.reset ();
221                 return;
222         }
223
224         _player->set_always_burn_open_captions ();
225         _player->set_play_referenced ();
226
227         _film->Changed.connect (boost::bind (&FilmViewer::film_changed, this, _1));
228         _player->Changed.connect (boost::bind (&FilmViewer::player_changed, this, _1, _2));
229         _player->Caption.connect (boost::bind (&FilmViewer::caption, this, _1, _2, _3));
230
231         /* Keep about 1 second's worth of history samples */
232         _latency_history_count = _film->audio_frame_rate() / _audio_block_size;
233
234         recreate_butler ();
235
236         calculate_sizes ();
237         slow_refresh ();
238
239         setup_sensitivity ();
240 }
241
242 void
243 FilmViewer::recreate_butler ()
244 {
245         bool const was_running = stop ();
246         _butler.reset ();
247
248         if (!_film) {
249                 return;
250         }
251
252         AudioMapping map = AudioMapping (_film->audio_channels(), _audio_channels);
253
254         if (_audio_channels != 2 || _film->audio_channels() < 3) {
255                 for (int i = 0; i < min (_film->audio_channels(), _audio_channels); ++i) {
256                         map.set (i, i, 1);
257                 }
258         } else {
259                 /* Special case: stereo output, at least 3 channel input.
260                    Map so that Lt = L(-3dB) + Ls(-3dB) + C(-6dB) + Lfe(-10dB)
261                                Rt = R(-3dB) + Rs(-3dB) + C(-6dB) + Lfe(-10dB)
262                 */
263                 map.set (dcp::LEFT,   0, 1 / sqrt(2)); // L -> Lt
264                 map.set (dcp::RIGHT,  1, 1 / sqrt(2)); // R -> Rt
265                 map.set (dcp::CENTRE, 0, 1 / 2.0); // C -> Lt
266                 map.set (dcp::CENTRE, 1, 1 / 2.0); // C -> Rt
267                 map.set (dcp::LFE,    0, 1 / sqrt(10)); // Lfe -> Lt
268                 map.set (dcp::LFE,    1, 1 / sqrt(10)); // Lfe -> Rt
269                 map.set (dcp::LS,     0, 1 / sqrt(2)); // Ls -> Lt
270                 map.set (dcp::RS,     1, 1 / sqrt(2)); // Rs -> Rt
271         }
272
273         _butler.reset (new Butler (_player, _film->log(), map, _audio_channels));
274         if (!Config::instance()->sound() && !_audio.isStreamOpen()) {
275                 _butler->disable_audio ();
276         }
277
278         if (was_running) {
279                 start ();
280         }
281 }
282
283 void
284 FilmViewer::refresh_panel ()
285 {
286         _panel->Refresh ();
287         _panel->Update ();
288 }
289
290 void
291 FilmViewer::get ()
292 {
293         DCPOMATIC_ASSERT (_butler);
294
295         do {
296                 _player_video = _butler->get_video ();
297         } while (
298                 _film->three_d() &&
299                 ((_eye->GetSelection() == 0 && _player_video.first->eyes() == EYES_RIGHT) || (_eye->GetSelection() == 1 && _player_video.first->eyes() == EYES_LEFT))
300                 );
301
302         _butler->rethrow ();
303
304         display_player_video ();
305 }
306
307 void
308 FilmViewer::display_player_video ()
309 {
310         if (!_player_video.first) {
311                 _frame.reset ();
312                 refresh_panel ();
313                 return;
314         }
315
316         if (_playing && (time() - _player_video.second) > one_video_frame()) {
317                 /* Too late; just drop this frame before we try to get its image (which will be the time-consuming
318                    part if this frame is J2K).
319                 */
320                 _video_position = _player_video.second;
321                 ++_dropped;
322                 return;
323         }
324
325         /* In an ideal world, what we would do here is:
326          *
327          * 1. convert to XYZ exactly as we do in the DCP creation path.
328          * 2. convert back to RGB for the preview display, compensating
329          *    for the monitor etc. etc.
330          *
331          * but this is inefficient if the source is RGB.  Since we don't
332          * (currently) care too much about the precise accuracy of the preview's
333          * colour mapping (and we care more about its speed) we try to short-
334          * circuit this "ideal" situation in some cases.
335          *
336          * The content's specified colour conversion indicates the colourspace
337          * which the content is in (according to the user).
338          *
339          * PlayerVideo::image (bound to PlayerVideo::always_rgb) will take the source
340          * image and convert it (from whatever the user has said it is) to RGB.
341          */
342
343         _frame = _player_video.first->image (
344                 bind (&Log::dcp_log, _film->log().get(), _1, _2),
345                 bind (&PlayerVideo::always_rgb, _1),
346                 false, true
347                 );
348
349         ImageChanged (_player_video.first);
350
351         _video_position = _player_video.second;
352         _inter_position = _player_video.first->inter_position ();
353         _inter_size = _player_video.first->inter_size ();
354
355         refresh_panel ();
356
357         _closed_captions_dialog->refresh (time());
358 }
359
360 void
361 FilmViewer::timer ()
362 {
363         if (!_film || !_playing) {
364                 return;
365         }
366
367         get ();
368         update_position_label ();
369         update_position_slider ();
370         DCPTime const next = _video_position + one_video_frame();
371
372         if (next >= _film->length()) {
373                 stop ();
374         }
375
376         _timer.Start (max ((next.seconds() - time().seconds()) * 1000, 1.0), wxTIMER_ONE_SHOT);
377
378         if (_butler) {
379                 _butler->rethrow ();
380         }
381 }
382
383 void
384 FilmViewer::paint_panel ()
385 {
386         wxPaintDC dc (_panel);
387
388         if (!_frame || !_film || !_out_size.width || !_out_size.height || _out_size != _frame->size()) {
389                 dc.Clear ();
390                 return;
391         }
392
393         wxImage frame (_out_size.width, _out_size.height, _frame->data()[0], true);
394         wxBitmap frame_bitmap (frame);
395         dc.DrawBitmap (frame_bitmap, 0, 0);
396
397         if (_out_size.width < _panel_size.width) {
398                 wxPen p (GetBackgroundColour ());
399                 wxBrush b (GetBackgroundColour ());
400                 dc.SetPen (p);
401                 dc.SetBrush (b);
402                 dc.DrawRectangle (_out_size.width, 0, _panel_size.width - _out_size.width, _panel_size.height);
403         }
404
405         if (_out_size.height < _panel_size.height) {
406                 wxPen p (GetBackgroundColour ());
407                 wxBrush b (GetBackgroundColour ());
408                 dc.SetPen (p);
409                 dc.SetBrush (b);
410                 dc.DrawRectangle (0, _out_size.height, _panel_size.width, _panel_size.height - _out_size.height);
411         }
412
413         if (_outline_content && _outline_content->GetValue ()) {
414                 wxPen p (wxColour (255, 0, 0), 2);
415                 dc.SetPen (p);
416                 dc.SetBrush (*wxTRANSPARENT_BRUSH);
417                 dc.DrawRectangle (_inter_position.x, _inter_position.y, _inter_size.width, _inter_size.height);
418         }
419 }
420
421 /** @param page true if this was a PAGEUP/PAGEDOWN event for which we won't receive a THUMBRELEASE */
422 void
423 FilmViewer::slider_moved (bool page)
424 {
425         if (!_film) {
426                 return;
427         }
428
429         if (!page && !_slider_being_moved) {
430                 /* This is the first event of a drag; stop playback for the duration of the drag */
431                 _was_running_before_slider = stop ();
432                 _slider_being_moved = true;
433         }
434
435         DCPTime t (_slider->GetValue() * _film->length().get() / 4096);
436         t = t.round (_film->video_frame_rate());
437         /* Ensure that we hit the end of the film at the end of the slider */
438         if (t >= _film->length ()) {
439                 t = _film->length() - one_video_frame();
440         }
441         seek (t, false);
442         update_position_label ();
443 }
444
445 void
446 FilmViewer::slider_released ()
447 {
448         if (_was_running_before_slider) {
449                 /* Restart after a drag */
450                 start ();
451         }
452         _slider_being_moved = false;
453 }
454
455 void
456 FilmViewer::panel_sized (wxSizeEvent& ev)
457 {
458         _panel_size.width = ev.GetSize().GetWidth();
459         _panel_size.height = ev.GetSize().GetHeight();
460
461         calculate_sizes ();
462         if (!quick_refresh()) {
463                 slow_refresh ();
464         }
465         update_position_label ();
466         update_position_slider ();
467 }
468
469 void
470 FilmViewer::calculate_sizes ()
471 {
472         if (!_film || !_player) {
473                 return;
474         }
475
476         Ratio const * container = _film->container ();
477
478         float const panel_ratio = _panel_size.ratio ();
479         float const film_ratio = container ? container->ratio () : 1.78;
480
481         if (panel_ratio < film_ratio) {
482                 /* panel is less widscreen than the film; clamp width */
483                 _out_size.width = _panel_size.width;
484                 _out_size.height = lrintf (_out_size.width / film_ratio);
485         } else {
486                 /* panel is more widescreen than the film; clamp height */
487                 _out_size.height = _panel_size.height;
488                 _out_size.width = lrintf (_out_size.height * film_ratio);
489         }
490
491         /* Catch silly values */
492         _out_size.width = max (64, _out_size.width);
493         _out_size.height = max (64, _out_size.height);
494
495         _player->set_video_container_size (_out_size);
496 }
497
498 void
499 FilmViewer::play_clicked ()
500 {
501         check_play_state ();
502 }
503
504 void
505 FilmViewer::check_play_state ()
506 {
507         if (!_film || _film->video_frame_rate() == 0) {
508                 return;
509         }
510
511         if (_play_button->GetValue()) {
512                 start ();
513         } else {
514                 stop ();
515         }
516 }
517
518 void
519 FilmViewer::start ()
520 {
521         if (!_film) {
522                 return;
523         }
524
525         if (_audio.isStreamOpen()) {
526                 _audio.setStreamTime (_video_position.seconds());
527                 _audio.startStream ();
528         }
529
530         _playing = true;
531         _dropped = 0;
532         timer ();
533         _play_button->SetValue (true);
534 }
535
536 bool
537 FilmViewer::stop ()
538 {
539         if (_audio.isStreamRunning()) {
540                 /* stop stream and discard any remaining queued samples */
541                 _audio.abortStream ();
542         }
543
544         if (!_playing) {
545                 return false;
546         }
547
548         _playing = false;
549         _play_button->SetValue (false);
550         return true;
551 }
552
553 void
554 FilmViewer::update_position_slider ()
555 {
556         if (!_film) {
557                 _slider->SetValue (0);
558                 return;
559         }
560
561         DCPTime const len = _film->length ();
562
563         if (len.get ()) {
564                 int const new_slider_position = 4096 * _video_position.get() / len.get();
565                 if (new_slider_position != _slider->GetValue()) {
566                         _slider->SetValue (new_slider_position);
567                 }
568         }
569 }
570
571 void
572 FilmViewer::update_position_label ()
573 {
574         if (!_film) {
575                 _frame_number->SetLabel ("0");
576                 _timecode->SetLabel ("0:0:0.0");
577                 return;
578         }
579
580         double const fps = _film->video_frame_rate ();
581         /* Count frame number from 1 ... not sure if this is the best idea */
582         _frame_number->SetLabel (wxString::Format (wxT("%ld"), lrint (_video_position.seconds() * fps) + 1));
583         _timecode->SetLabel (time_to_timecode (_video_position, fps));
584 }
585
586 void
587 FilmViewer::active_jobs_changed (optional<string> j)
588 {
589         /* examine content is the only job which stops the viewer working */
590         bool const a = !j || *j != "examine_content";
591         _slider->Enable (a);
592         _play_button->Enable (a);
593 }
594
595 DCPTime
596 FilmViewer::nudge_amount (wxKeyboardState& ev)
597 {
598         DCPTime amount = one_video_frame ();
599
600         if (ev.ShiftDown() && !ev.ControlDown()) {
601                 amount = DCPTime::from_seconds (1);
602         } else if (!ev.ShiftDown() && ev.ControlDown()) {
603                 amount = DCPTime::from_seconds (10);
604         } else if (ev.ShiftDown() && ev.ControlDown()) {
605                 amount = DCPTime::from_seconds (60);
606         }
607
608         return amount;
609 }
610
611 void
612 FilmViewer::go_to (DCPTime t)
613 {
614         if (t < DCPTime ()) {
615                 t = DCPTime ();
616         }
617
618         if (t >= _film->length ()) {
619                 t = _film->length ();
620         }
621
622         seek (t, true);
623         update_position_label ();
624         update_position_slider ();
625 }
626
627 void
628 FilmViewer::rewind_clicked (wxMouseEvent& ev)
629 {
630         go_to(DCPTime());
631         ev.Skip();
632 }
633
634 void
635 FilmViewer::back_frame ()
636 {
637         if (!_film) {
638                 return;
639         }
640
641         go_to (_video_position - one_video_frame());
642 }
643
644 void
645 FilmViewer::forward_frame ()
646 {
647         if (!_film) {
648                 return;
649         }
650
651         go_to (_video_position + one_video_frame());
652 }
653
654 void
655 FilmViewer::back_clicked (wxKeyboardState& ev)
656 {
657         go_to (_video_position - nudge_amount (ev));
658 }
659
660 void
661 FilmViewer::forward_clicked (wxKeyboardState& ev)
662 {
663         go_to (_video_position + nudge_amount (ev));
664 }
665
666 void
667 FilmViewer::player_changed (int property, bool frequent)
668 {
669         if (frequent) {
670                 return;
671         }
672
673         if (_coalesce_player_changes) {
674                 _pending_player_changes.push_back (property);
675                 return;
676         }
677
678         calculate_sizes ();
679         bool refreshed = false;
680         if (
681                 property == VideoContentProperty::CROP ||
682                 property == VideoContentProperty::SCALE ||
683                 property == VideoContentProperty::FADE_IN ||
684                 property == VideoContentProperty::FADE_OUT ||
685                 property == VideoContentProperty::COLOUR_CONVERSION ||
686                 property == PlayerProperty::VIDEO_CONTAINER_SIZE ||
687                 property == PlayerProperty::FILM_CONTAINER
688                 ) {
689                 refreshed = quick_refresh ();
690         }
691
692         if (!refreshed) {
693                 slow_refresh ();
694         }
695         update_position_label ();
696         update_position_slider ();
697 }
698
699 void
700 FilmViewer::setup_sensitivity ()
701 {
702         bool const c = _film && !_film->content().empty ();
703
704         _slider->Enable (c);
705         _rewind_button->Enable (c);
706         _back_button->Enable (c);
707         _forward_button->Enable (c);
708         _play_button->Enable (c);
709         if (_outline_content) {
710                 _outline_content->Enable (c);
711         }
712         _frame_number->Enable (c);
713         _timecode->Enable (c);
714         if (_jump_to_selected) {
715                 _jump_to_selected->Enable (c);
716         }
717
718         _eye->Enable (c && _film->three_d ());
719 }
720
721 void
722 FilmViewer::film_changed (Film::Property p)
723 {
724         if (p == Film::CONTENT || p == Film::THREE_D) {
725                 setup_sensitivity ();
726         } else if (p == Film::AUDIO_CHANNELS) {
727                 recreate_butler ();
728         }
729 }
730
731 /** Re-get the current frame slowly by seeking */
732 void
733 FilmViewer::slow_refresh ()
734 {
735         seek (_video_position, true);
736 }
737
738 /** Try to re-get the current frame quickly by resetting the metadata
739  *  in the PlayerVideo that we used last time.
740  *  @return true if this was possible, false if not.
741  */
742 bool
743 FilmViewer::quick_refresh ()
744 {
745         if (!_player_video.first) {
746                 return false;
747         }
748
749         if (!_player_video.first->reset_metadata (_player->video_container_size(), _film->frame_size())) {
750                 return false;
751         }
752
753         display_player_video ();
754         return true;
755 }
756
757 void
758 FilmViewer::set_position (DCPTime p)
759 {
760         _video_position = p;
761         seek (p, true);
762         update_position_label ();
763         update_position_slider ();
764 }
765
766 void
767 FilmViewer::set_position (shared_ptr<Content> content, ContentTime t)
768 {
769         set_position (_player->content_time_to_dcp (content, t));
770 }
771
772 void
773 FilmViewer::set_coalesce_player_changes (bool c)
774 {
775         _coalesce_player_changes = c;
776
777         if (!c) {
778                 BOOST_FOREACH (int i, _pending_player_changes) {
779                         player_changed (i, false);
780                 }
781                 _pending_player_changes.clear ();
782         }
783 }
784
785 void
786 FilmViewer::timecode_clicked ()
787 {
788         PlayheadToTimecodeDialog* dialog = new PlayheadToTimecodeDialog (this, _film->video_frame_rate ());
789         if (dialog->ShowModal() == wxID_OK) {
790                 go_to (dialog->get ());
791         }
792         dialog->Destroy ();
793 }
794
795 void
796 FilmViewer::frame_number_clicked ()
797 {
798         PlayheadToFrameDialog* dialog = new PlayheadToFrameDialog (this, _film->video_frame_rate ());
799         if (dialog->ShowModal() == wxID_OK) {
800                 go_to (dialog->get ());
801         }
802         dialog->Destroy ();
803 }
804
805 void
806 FilmViewer::jump_to_selected_clicked ()
807 {
808         Config::instance()->set_jump_to_selected (_jump_to_selected->GetValue ());
809 }
810
811 void
812 FilmViewer::seek (DCPTime t, bool accurate)
813 {
814         if (!_butler) {
815                 return;
816         }
817
818         bool const was_running = stop ();
819
820         _closed_captions_dialog->clear ();
821         _butler->seek (t, accurate);
822         get ();
823
824         if (was_running) {
825                 start ();
826         }
827 }
828
829 void
830 FilmViewer::config_changed (Config::Property p)
831 {
832         if (p != Config::SOUND && p != Config::SOUND_OUTPUT) {
833                 return;
834         }
835
836         if (_audio.isStreamOpen ()) {
837                 _audio.closeStream ();
838         }
839
840         if (Config::instance()->sound() && _audio.getDeviceCount() > 0) {
841                 unsigned int st = 0;
842                 if (Config::instance()->sound_output()) {
843                         while (st < _audio.getDeviceCount()) {
844                                 if (_audio.getDeviceInfo(st).name == Config::instance()->sound_output().get()) {
845                                         break;
846                                 }
847                                 ++st;
848                         }
849                         if (st == _audio.getDeviceCount()) {
850                                 st = _audio.getDefaultOutputDevice();
851                         }
852                 } else {
853                         st = _audio.getDefaultOutputDevice();
854                 }
855
856                 _audio_channels = _audio.getDeviceInfo(st).outputChannels;
857
858                 RtAudio::StreamParameters sp;
859                 sp.deviceId = st;
860                 sp.nChannels = _audio_channels;
861                 sp.firstChannel = 0;
862                 try {
863                         _audio.openStream (&sp, 0, RTAUDIO_FLOAT32, 48000, &_audio_block_size, &rtaudio_callback, this);
864 #ifdef DCPOMATIC_USE_RTERROR
865                 } catch (RtError& e) {
866 #else
867                 } catch (RtAudioError& e) {
868 #endif
869                         error_dialog (
870                                 this,
871                                 _("Could not set up audio output.  There will be no audio during the preview."), std_to_wx(e.what())
872                                 );
873                 }
874                 recreate_butler ();
875
876         } else {
877                 _audio_channels = 0;
878                 recreate_butler ();
879         }
880 }
881
882 DCPTime
883 FilmViewer::time () const
884 {
885         if (_audio.isStreamRunning ()) {
886                 return DCPTime::from_seconds (const_cast<RtAudio*>(&_audio)->getStreamTime ()) -
887                         DCPTime::from_frames (average_latency(), _film->audio_frame_rate());
888         }
889
890         return _video_position;
891 }
892
893 int
894 FilmViewer::audio_callback (void* out_p, unsigned int frames)
895 {
896         _butler->get_audio (reinterpret_cast<float*> (out_p), frames);
897
898         boost::mutex::scoped_lock lm (_latency_history_mutex, boost::try_to_lock);
899         if (lm) {
900                 _latency_history.push_back (_audio.getStreamLatency ());
901                 if (_latency_history.size() > static_cast<size_t> (_latency_history_count)) {
902                         _latency_history.pop_front ();
903                 }
904         }
905
906         return 0;
907 }
908
909 Frame
910 FilmViewer::average_latency () const
911 {
912         boost::mutex::scoped_lock lm (_latency_history_mutex);
913         if (_latency_history.empty()) {
914                 return 0;
915         }
916
917         Frame total = 0;
918         BOOST_FOREACH (Frame i, _latency_history) {
919                 total += i;
920         }
921
922         return total / _latency_history.size();
923 }
924
925 void
926 FilmViewer::set_dcp_decode_reduction (optional<int> reduction)
927 {
928         _dcp_decode_reduction = reduction;
929         if (_player) {
930                 _player->set_dcp_decode_reduction (reduction);
931         }
932 }
933
934 optional<int>
935 FilmViewer::dcp_decode_reduction () const
936 {
937         return _dcp_decode_reduction;
938 }
939
940 DCPTime
941 FilmViewer::one_video_frame () const
942 {
943         return DCPTime::from_frames (1, _film->video_frame_rate());
944 }
945
946 void
947 FilmViewer::show_closed_captions ()
948 {
949         _closed_captions_dialog->Show();
950 }
951
952 void
953 FilmViewer::caption (PlayerCaption c, CaptionType t, DCPTimePeriod p)
954 {
955         if (t == CAPTION_CLOSED) {
956                 _closed_captions_dialog->caption (c, p);
957         }
958 }