+ seek (_video_position, _last_seek_accurate);
+}
+
+void
+FilmViewer::set_position (DCPTime p)
+{
+ _video_position = p;
+ seek (p, true);
+ update_position_label ();
+ update_position_slider ();
+}
+
+void
+FilmViewer::set_coalesce_player_changes (bool c)
+{
+ _coalesce_player_changes = c;
+
+ if (c) {
+ _pending_player_change = false;
+ } else {
+ if (_pending_player_change) {
+ player_changed (false);
+ }
+ }
+}
+
+void
+FilmViewer::timecode_clicked ()
+{
+ PlayheadToTimecodeDialog* dialog = new PlayheadToTimecodeDialog (this, _film->video_frame_rate ());
+ if (dialog->ShowModal() == wxID_OK) {
+ go_to (dialog->get ());
+ }
+ dialog->Destroy ();
+}
+
+void
+FilmViewer::frame_number_clicked ()
+{
+ PlayheadToFrameDialog* dialog = new PlayheadToFrameDialog (this, _film->video_frame_rate ());
+ if (dialog->ShowModal() == wxID_OK) {
+ go_to (dialog->get ());
+ }
+ dialog->Destroy ();
+}
+
+void
+FilmViewer::jump_to_selected_clicked ()
+{
+ Config::instance()->set_jump_to_selected (_jump_to_selected->GetValue ());
+}
+
+void
+FilmViewer::seek (DCPTime t, bool accurate)
+{
+ if (!_butler) {
+ return;
+ }
+
+ bool const was_running = stop ();
+
+ _butler->seek (t, accurate);
+ _last_seek_accurate = accurate;
+ get ();
+
+ if (was_running) {
+ start ();
+ }
+}
+
+void
+FilmViewer::config_changed (Config::Property p)
+{
+ if (p != Config::SOUND && p != Config::SOUND_OUTPUT) {
+ return;
+ }
+
+ if (_audio.isStreamOpen ()) {
+ _audio.closeStream ();
+ }
+
+ if (Config::instance()->sound()) {
+ unsigned int st = 0;
+ if (Config::instance()->sound_output()) {
+ while (st < _audio.getDeviceCount()) {
+ if (_audio.getDeviceInfo(st).name == Config::instance()->sound_output().get()) {
+ break;
+ }
+ ++st;
+ }
+ if (st == _audio.getDeviceCount()) {
+ st = _audio.getDefaultOutputDevice();
+ }
+ } else {
+ st = _audio.getDefaultOutputDevice();
+ }
+
+ _audio_channels = _audio.getDeviceInfo(st).outputChannels;
+
+ recreate_butler ();
+
+ RtAudio::StreamParameters sp;
+ sp.deviceId = st;
+ sp.nChannels = _audio_channels;
+ sp.firstChannel = 0;
+ try {
+ _audio.openStream (&sp, 0, RTAUDIO_FLOAT32, 48000, &_audio_block_size, &rtaudio_callback, this);
+#ifdef DCPOMATIC_USE_RTERROR
+ } catch (RtError& e) {
+#else
+ } catch (RtAudioError& e) {
+#endif
+ error_dialog (
+ this,
+ wxString::Format (_("Could not set up audio output (%s). There will be no audio during the preview."), e.what())
+ );
+ }
+
+ } else {
+ _audio_channels = 0;
+ recreate_butler ();
+ }
+}
+
+DCPTime
+FilmViewer::time () const
+{
+ if (_audio.isStreamRunning ()) {
+ return DCPTime::from_seconds (const_cast<RtAudio*>(&_audio)->getStreamTime ()) -
+ DCPTime::from_frames (average_latency(), _film->audio_frame_rate());
+ }
+
+ return _video_position;
+}
+
+int
+FilmViewer::audio_callback (void* out_p, unsigned int frames)
+{
+ _butler->get_audio (reinterpret_cast<float*> (out_p), frames);
+
+ boost::mutex::scoped_lock lm (_latency_history_mutex, boost::try_to_lock);
+ if (lm) {
+ _latency_history.push_back (_audio.getStreamLatency ());
+ if (_latency_history.size() > static_cast<size_t> (_latency_history_count)) {
+ _latency_history.pop_front ();
+ }
+ }
+
+ return 0;
+}
+
+Frame
+FilmViewer::average_latency () const
+{
+ boost::mutex::scoped_lock lm (_latency_history_mutex);
+ if (_latency_history.empty()) {
+ return 0;
+ }
+
+ Frame total = 0;
+ BOOST_FOREACH (Frame i, _latency_history) {
+ total += i;
+ }
+
+ return total / _latency_history.size();
+}
+
+void
+FilmViewer::set_dcp_decode_reduction (optional<int> reduction)
+{
+ _dcp_decode_reduction = reduction;
+ if (_player) {
+ _player->set_dcp_decode_reduction (reduction);
+ }
+}
+
+DCPTime
+FilmViewer::one_video_frame () const
+{
+ return DCPTime::from_frames (1, _film->video_frame_rate());