2 Copyright (C) 2012-2016 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
21 /** @file src/film_viewer.cc
22 * @brief A wx widget to view a preview of a Film.
25 #include "film_viewer.h"
26 #include "playhead_to_timecode_dialog.h"
27 #include "playhead_to_frame_dialog.h"
30 #include "lib/ratio.h"
32 #include "lib/job_manager.h"
33 #include "lib/image.h"
34 #include "lib/exceptions.h"
35 #include "lib/examine_content_job.h"
36 #include "lib/filter.h"
37 #include "lib/player.h"
38 #include "lib/player_video.h"
39 #include "lib/video_content.h"
40 #include "lib/video_decoder.h"
41 #include "lib/timer.h"
42 #include "lib/butler.h"
44 #include "lib/config.h"
46 #include <libavutil/pixfmt.h>
48 #include <dcp/exceptions.h>
49 #include <wx/tglbtn.h>
62 using boost::shared_ptr;
63 using boost::dynamic_pointer_cast;
64 using boost::weak_ptr;
65 using boost::optional;
70 rtaudio_callback (void* out, void *, unsigned int frames, double, RtAudioStreamStatus, void* data)
72 return reinterpret_cast<FilmViewer*>(data)->audio_callback (out, frames);
75 FilmViewer::FilmViewer (wxWindow* p)
77 , _panel (new wxPanel (this))
78 , _outline_content (new wxCheckBox (this, wxID_ANY, _("Outline content")))
79 , _left_eye (new wxRadioButton (this, wxID_ANY, _("Left eye"), wxDefaultPosition, wxDefaultSize, wxRB_GROUP))
80 , _right_eye (new wxRadioButton (this, wxID_ANY, _("Right eye")))
81 , _jump_to_selected (new wxCheckBox (this, wxID_ANY, _("Jump to selected content")))
82 , _slider (new wxSlider (this, wxID_ANY, 0, 0, 4096))
83 , _back_button (new wxButton (this, wxID_ANY, wxT("<")))
84 , _forward_button (new wxButton (this, wxID_ANY, wxT(">")))
85 , _frame_number (new wxStaticText (this, wxID_ANY, wxT("")))
86 , _timecode (new wxStaticText (this, wxID_ANY, wxT("")))
87 , _play_button (new wxToggleButton (this, wxID_ANY, _("Play")))
88 , _coalesce_player_changes (false)
89 , _pending_player_change (false)
90 , _last_seek_accurate (true)
91 , _audio (DCPOMATIC_RTAUDIO_API)
93 , _audio_block_size (1024)
95 , _latency_history_count (0)
98 _panel->SetDoubleBuffered (true);
101 _panel->SetBackgroundStyle (wxBG_STYLE_PAINT);
103 _v_sizer = new wxBoxSizer (wxVERTICAL);
106 _v_sizer->Add (_panel, 1, wxEXPAND);
108 wxBoxSizer* view_options = new wxBoxSizer (wxHORIZONTAL);
109 view_options->Add (_outline_content, 0, wxRIGHT, DCPOMATIC_SIZER_GAP);
110 view_options->Add (_left_eye, 0, wxLEFT | wxRIGHT, DCPOMATIC_SIZER_GAP);
111 view_options->Add (_right_eye, 0, wxLEFT | wxRIGHT, DCPOMATIC_SIZER_GAP);
112 view_options->Add (_jump_to_selected, 0, wxLEFT | wxRIGHT, DCPOMATIC_SIZER_GAP);
113 _v_sizer->Add (view_options, 0, wxALL, DCPOMATIC_SIZER_GAP);
115 wxBoxSizer* h_sizer = new wxBoxSizer (wxHORIZONTAL);
117 wxBoxSizer* time_sizer = new wxBoxSizer (wxVERTICAL);
118 time_sizer->Add (_frame_number, 0, wxEXPAND);
119 time_sizer->Add (_timecode, 0, wxEXPAND);
121 h_sizer->Add (_back_button, 0, wxALL, 2);
122 h_sizer->Add (time_sizer, 0, wxEXPAND);
123 h_sizer->Add (_forward_button, 0, wxALL, 2);
124 h_sizer->Add (_play_button, 0, wxEXPAND);
125 h_sizer->Add (_slider, 1, wxEXPAND);
127 _v_sizer->Add (h_sizer, 0, wxEXPAND | wxALL, 6);
129 _frame_number->SetMinSize (wxSize (84, -1));
130 _back_button->SetMinSize (wxSize (32, -1));
131 _forward_button->SetMinSize (wxSize (32, -1));
133 _panel->Bind (wxEVT_PAINT, boost::bind (&FilmViewer::paint_panel, this));
134 _panel->Bind (wxEVT_SIZE, boost::bind (&FilmViewer::panel_sized, this, _1));
135 _outline_content->Bind (wxEVT_CHECKBOX, boost::bind (&FilmViewer::refresh_panel, this));
136 _left_eye->Bind (wxEVT_RADIOBUTTON, boost::bind (&FilmViewer::refresh, this));
137 _right_eye->Bind (wxEVT_RADIOBUTTON, boost::bind (&FilmViewer::refresh, this));
138 _slider->Bind (wxEVT_SCROLL_THUMBTRACK, boost::bind (&FilmViewer::slider_moved, this));
139 _slider->Bind (wxEVT_SCROLL_PAGEUP, boost::bind (&FilmViewer::slider_moved, this));
140 _slider->Bind (wxEVT_SCROLL_PAGEDOWN, boost::bind (&FilmViewer::slider_moved, this));
141 _play_button->Bind (wxEVT_TOGGLEBUTTON, boost::bind (&FilmViewer::play_clicked, this));
142 _timer.Bind (wxEVT_TIMER, boost::bind (&FilmViewer::timer, this));
143 _back_button->Bind (wxEVT_LEFT_DOWN, boost::bind (&FilmViewer::back_clicked, this, _1));
144 _forward_button->Bind (wxEVT_LEFT_DOWN, boost::bind (&FilmViewer::forward_clicked, this, _1));
145 _frame_number->Bind (wxEVT_LEFT_DOWN, boost::bind (&FilmViewer::frame_number_clicked, this));
146 _timecode->Bind (wxEVT_LEFT_DOWN, boost::bind (&FilmViewer::timecode_clicked, this));
147 _jump_to_selected->Bind (wxEVT_CHECKBOX, boost::bind (&FilmViewer::jump_to_selected_clicked, this));
149 _jump_to_selected->SetValue (Config::instance()->jump_to_selected ());
151 set_film (shared_ptr<Film> ());
153 JobManager::instance()->ActiveJobsChanged.connect (
154 bind (&FilmViewer::active_jobs_changed, this, _2)
157 setup_sensitivity ();
159 _config_changed_connection = Config::instance()->Changed.connect (bind (&FilmViewer::config_changed, this, _1));
160 config_changed (Config::SOUND_OUTPUT);
163 FilmViewer::~FilmViewer ()
169 FilmViewer::set_film (shared_ptr<Film> film)
179 update_position_slider ();
180 update_position_label ();
187 _player.reset (new Player (_film, _film->playlist ()));
188 _player->set_fast ();
189 } catch (bad_alloc) {
190 error_dialog (this, _("There is not enough free memory to do that."));
195 /* Always burn in subtitles, even if content is set not to, otherwise we won't see them
198 _player->set_always_burn_subtitles (true);
199 _player->set_play_referenced ();
201 _film->Changed.connect (boost::bind (&FilmViewer::film_changed, this, _1));
202 _player->Changed.connect (boost::bind (&FilmViewer::player_changed, this, _1));
204 /* Keep about 1 second's worth of history samples */
205 _latency_history_count = _film->audio_frame_rate() / _audio_block_size;
212 setup_sensitivity ();
216 FilmViewer::recreate_butler ()
218 bool const was_running = stop ();
225 AudioMapping map = AudioMapping (_film->audio_channels(), _audio_channels);
227 if (_audio_channels != 2 || _film->audio_channels() < 3) {
228 for (int i = 0; i < min (_film->audio_channels(), _audio_channels); ++i) {
232 /* Special case: stereo output, at least 3 channel input, map L+R to L/R and
233 C to both, all 3dB down.
235 map.set (0, 0, 1 / sqrt(2)); // L -> L
236 map.set (1, 1, 1 / sqrt(2)); // R -> R
237 map.set (2, 0, 1 / sqrt(2)); // C -> L
238 map.set (2, 1, 1 / sqrt(2)); // C -> R
241 _butler.reset (new Butler (_film, _player, map, _audio_channels));
249 FilmViewer::refresh_panel ()
258 pair<shared_ptr<PlayerVideo>, DCPTime> video;
260 video = _butler->get_video ();
263 ((_left_eye->GetValue() && video.first->eyes() == EYES_RIGHT) || (_right_eye->GetValue() && video.first->eyes() == EYES_LEFT))
272 /* In an ideal world, what we would do here is:
274 * 1. convert to XYZ exactly as we do in the DCP creation path.
275 * 2. convert back to RGB for the preview display, compensating
276 * for the monitor etc. etc.
278 * but this is inefficient if the source is RGB. Since we don't
279 * (currently) care too much about the precise accuracy of the preview's
280 * colour mapping (and we care more about its speed) we try to short-
281 * circuit this "ideal" situation in some cases.
283 * The content's specified colour conversion indicates the colourspace
284 * which the content is in (according to the user).
286 * PlayerVideo::image (bound to PlayerVideo::always_rgb) will take the source
287 * image and convert it (from whatever the user has said it is) to RGB.
290 _frame = video.first->image (
291 bind (&Log::dcp_log, _film->log().get(), _1, _2),
292 bind (&PlayerVideo::always_rgb, _1),
296 ImageChanged (video.first);
298 _video_position = video.second;
299 _inter_position = video.first->inter_position ();
300 _inter_size = video.first->inter_size ();
312 if (_audio.isStreamRunning ()) {
314 update_position_label ();
315 update_position_slider ();
316 DCPTime const next = _video_position + DCPTime::from_frames (1, _film->video_frame_rate ());
318 if (next >= _film->length()) {
322 _timer.Start (max ((next.seconds() - time().seconds()) * 1000, 0.0), wxTIMER_ONE_SHOT);
331 FilmViewer::paint_panel ()
333 wxPaintDC dc (_panel);
335 if (!_frame || !_film || !_out_size.width || !_out_size.height) {
340 wxImage frame (_out_size.width, _out_size.height, _frame->data()[0], true);
341 wxBitmap frame_bitmap (frame);
342 dc.DrawBitmap (frame_bitmap, 0, 0);
344 if (_out_size.width < _panel_size.width) {
345 wxPen p (GetBackgroundColour ());
346 wxBrush b (GetBackgroundColour ());
349 dc.DrawRectangle (_out_size.width, 0, _panel_size.width - _out_size.width, _panel_size.height);
352 if (_out_size.height < _panel_size.height) {
353 wxPen p (GetBackgroundColour ());
354 wxBrush b (GetBackgroundColour ());
357 dc.DrawRectangle (0, _out_size.height, _panel_size.width, _panel_size.height - _out_size.height);
360 if (_outline_content->GetValue ()) {
361 wxPen p (wxColour (255, 0, 0), 2);
363 dc.SetBrush (*wxTRANSPARENT_BRUSH);
364 dc.DrawRectangle (_inter_position.x, _inter_position.y, _inter_size.width, _inter_size.height);
369 FilmViewer::slider_moved ()
375 DCPTime t (_slider->GetValue() * _film->length().get() / 4096);
376 /* Ensure that we hit the end of the film at the end of the slider */
377 if (t >= _film->length ()) {
378 t = _film->length() - DCPTime::from_frames (1, _film->video_frame_rate ());
381 update_position_label ();
385 FilmViewer::panel_sized (wxSizeEvent& ev)
387 _panel_size.width = ev.GetSize().GetWidth();
388 _panel_size.height = ev.GetSize().GetHeight();
392 update_position_label ();
393 update_position_slider ();
397 FilmViewer::calculate_sizes ()
403 Ratio const * container = _film->container ();
405 float const panel_ratio = _panel_size.ratio ();
406 float const film_ratio = container ? container->ratio () : 1.78;
408 if (panel_ratio < film_ratio) {
409 /* panel is less widscreen than the film; clamp width */
410 _out_size.width = _panel_size.width;
411 _out_size.height = lrintf (_out_size.width / film_ratio);
413 /* panel is more widescreen than the film; clamp height */
414 _out_size.height = _panel_size.height;
415 _out_size.width = lrintf (_out_size.height * film_ratio);
418 /* Catch silly values */
419 _out_size.width = max (64, _out_size.width);
420 _out_size.height = max (64, _out_size.height);
422 _player->set_video_container_size (_out_size);
426 FilmViewer::play_clicked ()
432 FilmViewer::check_play_state ()
434 if (!_film || _film->video_frame_rate() == 0) {
438 if (_play_button->GetValue()) {
448 if (_audio.isStreamOpen()) {
449 _audio.setStreamTime (_video_position.seconds());
450 _audio.startStream ();
454 _timer.Start (0, wxTIMER_ONE_SHOT);
460 if (_audio.isStreamRunning()) {
461 /* stop stream and discard any remainig queued samples */
462 _audio.abortStream ();
470 _play_button->SetValue (false);
475 FilmViewer::update_position_slider ()
478 _slider->SetValue (0);
482 DCPTime const len = _film->length ();
485 int const new_slider_position = 4096 * _video_position.get() / len.get();
486 if (new_slider_position != _slider->GetValue()) {
487 _slider->SetValue (new_slider_position);
493 FilmViewer::update_position_label ()
496 _frame_number->SetLabel ("0");
497 _timecode->SetLabel ("0:0:0.0");
501 double const fps = _film->video_frame_rate ();
502 /* Count frame number from 1 ... not sure if this is the best idea */
503 _frame_number->SetLabel (wxString::Format (wxT("%ld"), lrint (_video_position.seconds() * fps) + 1));
504 _timecode->SetLabel (time_to_timecode (_video_position, fps));
508 FilmViewer::active_jobs_changed (optional<string> j)
510 /* examine content is the only job which stops the viewer working */
511 bool const a = !j || *j != "examine_content";
513 _play_button->Enable (a);
517 FilmViewer::nudge_amount (wxMouseEvent& ev)
519 DCPTime amount = DCPTime::from_frames (1, _film->video_frame_rate ());
521 if (ev.ShiftDown() && !ev.ControlDown()) {
522 amount = DCPTime::from_seconds (1);
523 } else if (!ev.ShiftDown() && ev.ControlDown()) {
524 amount = DCPTime::from_seconds (10);
525 } else if (ev.ShiftDown() && ev.ControlDown()) {
526 amount = DCPTime::from_seconds (60);
533 FilmViewer::go_to (DCPTime t)
535 if (t < DCPTime ()) {
539 if (t >= _film->length ()) {
540 t = _film->length ();
544 update_position_label ();
545 update_position_slider ();
549 FilmViewer::back_clicked (wxMouseEvent& ev)
551 go_to (_video_position - nudge_amount (ev));
556 FilmViewer::forward_clicked (wxMouseEvent& ev)
558 go_to (_video_position + nudge_amount (ev));
563 FilmViewer::player_changed (bool frequent)
569 if (_coalesce_player_changes) {
570 _pending_player_change = true;
576 update_position_label ();
577 update_position_slider ();
581 FilmViewer::setup_sensitivity ()
583 bool const c = _film && !_film->content().empty ();
586 _back_button->Enable (c);
587 _forward_button->Enable (c);
588 _play_button->Enable (c);
589 _outline_content->Enable (c);
590 _frame_number->Enable (c);
591 _timecode->Enable (c);
592 _jump_to_selected->Enable (c);
594 _left_eye->Enable (c && _film->three_d ());
595 _right_eye->Enable (c && _film->three_d ());
599 FilmViewer::film_changed (Film::Property p)
601 if (p == Film::CONTENT || p == Film::THREE_D) {
602 setup_sensitivity ();
606 /** Re-get the current frame */
608 FilmViewer::refresh ()
610 seek (_video_position, _last_seek_accurate);
614 FilmViewer::set_position (DCPTime p)
618 update_position_label ();
619 update_position_slider ();
623 FilmViewer::set_coalesce_player_changes (bool c)
625 _coalesce_player_changes = c;
628 _pending_player_change = false;
630 if (_pending_player_change) {
631 player_changed (false);
637 FilmViewer::timecode_clicked ()
639 PlayheadToTimecodeDialog* dialog = new PlayheadToTimecodeDialog (this, _film->video_frame_rate ());
640 if (dialog->ShowModal() == wxID_OK) {
641 go_to (dialog->get ());
647 FilmViewer::frame_number_clicked ()
649 PlayheadToFrameDialog* dialog = new PlayheadToFrameDialog (this, _film->video_frame_rate ());
650 if (dialog->ShowModal() == wxID_OK) {
651 go_to (dialog->get ());
657 FilmViewer::jump_to_selected_clicked ()
659 Config::instance()->set_jump_to_selected (_jump_to_selected->GetValue ());
663 FilmViewer::seek (DCPTime t, bool accurate)
669 bool const was_running = stop ();
671 _butler->seek (t, accurate);
672 _last_seek_accurate = accurate;
681 FilmViewer::config_changed (Config::Property p)
683 if (p != Config::SOUND_OUTPUT) {
687 if (_audio.isStreamOpen ()) {
688 _audio.closeStream ();
692 if (Config::instance()->sound_output()) {
693 while (st < _audio.getDeviceCount()) {
694 if (_audio.getDeviceInfo(st).name == Config::instance()->sound_output().get()) {
699 if (st == _audio.getDeviceCount()) {
700 st = _audio.getDefaultOutputDevice();
703 st = _audio.getDefaultOutputDevice();
706 _audio_channels = _audio.getDeviceInfo(st).outputChannels;
709 RtAudio::StreamParameters sp;
711 sp.nChannels = _audio_channels;
714 _audio.openStream (&sp, 0, RTAUDIO_FLOAT32, 48000, &_audio_block_size, &rtaudio_callback, this);
715 #ifdef DCPOMATIC_USE_RTERROR
716 } catch (RtError& e) {
718 } catch (RtAudioError& e) {
720 error_dialog (this, wxString::Format (_("Could not set up audio output (%s). There will be no audio during the preview."), e.what()));
725 FilmViewer::time () const
727 if (_audio.isStreamRunning ()) {
728 return DCPTime::from_seconds (const_cast<RtAudio*>(&_audio)->getStreamTime ()) -
729 DCPTime::from_frames (average_latency(), _film->audio_frame_rate());
732 return _video_position;
736 FilmViewer::audio_callback (void* out_p, unsigned int frames)
738 _butler->get_audio (reinterpret_cast<float*> (out_p), frames);
740 boost::mutex::scoped_lock lm (_latency_history_mutex, boost::try_to_lock);
742 _latency_history.push_back (_audio.getStreamLatency ());
743 if (_latency_history.size() > static_cast<size_t> (_latency_history_count)) {
744 _latency_history.pop_front ();
752 FilmViewer::average_latency () const
754 boost::mutex::scoped_lock lm (_latency_history_mutex);
755 if (_latency_history.empty()) {
760 BOOST_FOREACH (Frame i, _latency_history) {
764 return total / _latency_history.size();