2 Copyright (C) 2012-2021 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/>.
22 /** @file src/film_viewer.cc
23 * @brief A wx widget to view a preview of a Film.
27 #include "closed_captions_dialog.h"
28 #include "film_viewer.h"
29 #include "gl_video_view.h"
30 #include "nag_dialog.h"
31 #include "playhead_to_frame_dialog.h"
32 #include "playhead_to_timecode_dialog.h"
33 #include "simple_video_view.h"
36 #include "lib/ratio.h"
38 #include "lib/job_manager.h"
39 #include "lib/image.h"
40 #include "lib/exceptions.h"
41 #include "lib/examine_content_job.h"
42 #include "lib/filter.h"
43 #include "lib/player.h"
44 #include "lib/player_video.h"
45 #include "lib/video_content.h"
46 #include "lib/video_decoder.h"
47 #include "lib/timer.h"
48 #include "lib/butler.h"
50 #include "lib/config.h"
51 #include "lib/compose.hpp"
52 #include "lib/dcpomatic_log.h"
53 #include "lib/text_content.h"
55 #include <libavutil/pixfmt.h>
57 #include <dcp/exceptions.h>
58 #include <wx/tglbtn.h>
71 using std::shared_ptr;
72 using std::dynamic_pointer_cast;
75 using boost::optional;
76 #if BOOST_VERSION >= 106100
77 using namespace boost::placeholders;
80 using namespace dcpomatic;
84 rtaudio_callback (void* out, void *, unsigned int frames, double, RtAudioStreamStatus, void* data)
86 return reinterpret_cast<FilmViewer*>(data)->audio_callback (out, frames);
89 FilmViewer::FilmViewer (wxWindow* p)
90 : _coalesce_player_changes (false)
91 , _audio (DCPOMATIC_RTAUDIO_API)
93 , _audio_block_size (1024)
96 , _latency_history_count (0)
97 , _closed_captions_dialog (new ClosedCaptionsDialog(p, this))
98 , _outline_content (false)
102 switch (Config::instance()->video_view_type()) {
103 case Config::VIDEO_VIEW_OPENGL:
104 _video_view = new GLVideoView (this, p);
106 case Config::VIDEO_VIEW_SIMPLE:
107 _video_view = new SimpleVideoView (this, p);
111 _video_view->Sized.connect (boost::bind(&FilmViewer::video_view_sized, this));
112 _video_view->TooManyDropped.connect (boost::bind(&FilmViewer::too_many_frames_dropped, this));
114 set_film (shared_ptr<Film> ());
116 _config_changed_connection = Config::instance()->Changed.connect (bind (&FilmViewer::config_changed, this, _1));
117 config_changed (Config::SOUND_OUTPUT);
120 FilmViewer::~FilmViewer ()
125 /** Ask for ::get() to be called next time we are idle */
127 FilmViewer::request_idle_display_next_frame ()
134 DCPOMATIC_ASSERT (signal_manager);
135 signal_manager->when_idle (boost::bind(&FilmViewer::idle_handler, this));
139 FilmViewer::idle_handler ()
145 if (_video_view->display_next_frame(true) == VideoView::AGAIN) {
146 /* get() could not complete quickly so we'll try again later */
147 signal_manager->when_idle (boost::bind(&FilmViewer::idle_handler, this));
154 FilmViewer::set_film (shared_ptr<Film> film)
162 _video_view->clear ();
163 _closed_captions_dialog->clear ();
168 _video_view->update ();
173 _player.reset (new Player(_film));
174 _player->set_fast ();
175 if (_dcp_decode_reduction) {
176 _player->set_dcp_decode_reduction (_dcp_decode_reduction);
178 } catch (bad_alloc &) {
179 error_dialog (_video_view->get(), _("There is not enough free memory to do that."));
184 _player->set_always_burn_open_subtitles ();
185 _player->set_play_referenced ();
187 _film->Change.connect (boost::bind (&FilmViewer::film_change, this, _1, _2));
188 _film->LengthChange.connect (boost::bind(&FilmViewer::film_length_change, this));
189 _player->Change.connect (boost::bind (&FilmViewer::player_change, this, _1, _2, _3));
191 film_change (ChangeType::DONE, Film::Property::VIDEO_FRAME_RATE);
192 film_change (ChangeType::DONE, Film::Property::THREE_D);
193 film_length_change ();
195 /* Keep about 1 second's worth of history samples */
196 _latency_history_count = _film->audio_frame_rate() / _audio_block_size;
198 _closed_captions_dialog->update_tracks (_film);
207 FilmViewer::recreate_butler ()
221 Config::instance()->audio_mapping(_audio_channels),
223 bind(&PlayerVideo::force, _1, AV_PIX_FMT_RGB24),
230 if (!Config::instance()->sound() && !_audio.isStreamOpen()) {
231 _butler->disable_audio ();
234 _closed_captions_dialog->set_butler (_butler);
240 FilmViewer::set_outline_content (bool o)
242 _outline_content = o;
243 _video_view->update ();
248 FilmViewer::set_outline_subtitles (optional<dcpomatic::Rect<double> > rect)
250 _outline_subtitles = rect;
251 _video_view->update ();
256 FilmViewer::set_eyes (Eyes e)
258 _video_view->set_eyes (e);
263 FilmViewer::video_view_sized ()
266 if (!quick_refresh()) {
272 FilmViewer::calculate_sizes ()
274 if (!_film || !_player) {
278 Ratio const * container = _film->container ();
280 float const view_ratio = float(_video_view->get()->GetSize().x) / _video_view->get()->GetSize().y;
281 float const film_ratio = container ? container->ratio () : 1.78;
283 if (view_ratio < film_ratio) {
284 /* panel is less widscreen than the film; clamp width */
285 _out_size.width = _video_view->get()->GetSize().x;
286 _out_size.height = lrintf (_out_size.width / film_ratio);
288 /* panel is more widescreen than the film; clamp height */
289 _out_size.height = _video_view->get()->GetSize().y;
290 _out_size.width = lrintf (_out_size.height * film_ratio);
293 /* Catch silly values */
294 _out_size.width = max (64, _out_size.width);
295 _out_size.height = max (64, _out_size.height);
297 _player->set_video_container_size (_out_size);
301 FilmViewer::suspend ()
304 if (_audio.isStreamRunning()) {
305 _audio.abortStream();
310 FilmViewer::resume ()
312 DCPOMATIC_ASSERT (_suspended > 0);
314 if (_playing && !_suspended) {
315 if (_audio.isStreamOpen()) {
316 _audio.setStreamTime (_video_view->position().seconds());
317 _audio.startStream ();
319 _video_view->start ();
330 optional<bool> v = PlaybackPermitted ();
332 /* Computer says no */
336 /* We are about to set up the audio stream from the position of the video view.
337 If there is `lazy' seek in progress we need to wait for it to go through so that
338 _video_view->position() gives us a sensible answer.
344 /* Take the video view's idea of position as our `playhead' and start the
345 audio stream (which is the timing reference) there.
347 if (_audio.isStreamOpen()) {
348 _audio.setStreamTime (_video_view->position().seconds());
349 _audio.startStream ();
353 /* Calling start() below may directly result in Stopped being emitted, and if that
354 * happens we want it to come after the Started signal, so do that first.
356 Started (position());
357 _video_view->start ();
363 if (_audio.isStreamRunning()) {
364 /* stop stream and discard any remaining queued samples */
365 _audio.abortStream ();
373 _video_view->stop ();
374 Stopped (position());
376 _video_view->rethrow ();
381 FilmViewer::player_change (ChangeType type, int property, bool frequent)
383 if (type != ChangeType::DONE || frequent) {
387 if (_coalesce_player_changes) {
388 _pending_player_changes.push_back (property);
392 player_change ({property});
396 FilmViewer::player_change (vector<int> properties)
400 bool try_quick_refresh = false;
401 bool update_ccap_tracks = false;
403 for (auto i: properties) {
405 i == VideoContentProperty::CROP ||
406 i == VideoContentProperty::SCALE ||
407 i == VideoContentProperty::FADE_IN ||
408 i == VideoContentProperty::FADE_OUT ||
409 i == VideoContentProperty::COLOUR_CONVERSION ||
410 i == PlayerProperty::VIDEO_CONTAINER_SIZE ||
411 i == PlayerProperty::FILM_CONTAINER
413 try_quick_refresh = true;
416 if (i == TextContentProperty::USE || i == TextContentProperty::TYPE || i == TextContentProperty::DCP_TRACK) {
417 update_ccap_tracks = true;
421 if (!try_quick_refresh || !quick_refresh()) {
425 if (update_ccap_tracks) {
426 _closed_captions_dialog->update_tracks (_film);
431 FilmViewer::film_change (ChangeType type, Film::Property p)
433 if (type != ChangeType::DONE) {
437 if (p == Film::Property::AUDIO_CHANNELS) {
439 } else if (p == Film::Property::VIDEO_FRAME_RATE) {
440 _video_view->set_video_frame_rate (_film->video_frame_rate());
441 } else if (p == Film::Property::THREE_D) {
442 _video_view->set_three_d (_film->three_d());
443 } else if (p == Film::Property::CONTENT) {
444 _closed_captions_dialog->update_tracks (_film);
449 FilmViewer::film_length_change ()
451 _video_view->set_length (_film->length());
454 /** Re-get the current frame slowly by seeking */
456 FilmViewer::slow_refresh ()
458 seek (_video_view->position(), true);
461 /** Try to re-get the current frame quickly by resetting the metadata
462 * in the PlayerVideo that we used last time.
463 * @return true if this was possible, false if not.
466 FilmViewer::quick_refresh ()
468 if (!_video_view || !_film || !_player) {
471 return _video_view->reset_metadata (_film, _player->video_container_size());
475 FilmViewer::seek (shared_ptr<Content> content, ContentTime t, bool accurate)
477 optional<DCPTime> dt = _player->content_time_to_dcp (content, t);
479 seek (*dt, accurate);
484 FilmViewer::set_coalesce_player_changes (bool c)
486 _coalesce_player_changes = c;
489 player_change (_pending_player_changes);
490 _pending_player_changes.clear ();
495 FilmViewer::seek (DCPTime t, bool accurate)
501 if (t < DCPTime ()) {
505 if (t >= _film->length ()) {
506 t = _film->length() - one_video_frame();
511 _closed_captions_dialog->clear ();
512 _butler->seek (t, accurate);
515 /* We're not playing, so let the GUI thread get on and
516 come back later to get the next frame after the seek.
518 request_idle_display_next_frame ();
520 /* We're going to start playing again straight away
521 so wait for the seek to finish.
523 while (_video_view->display_next_frame(false) == VideoView::AGAIN) {}
530 FilmViewer::config_changed (Config::Property p)
532 if (p == Config::AUDIO_MAPPING) {
537 if (p != Config::SOUND && p != Config::SOUND_OUTPUT) {
541 if (_audio.isStreamOpen ()) {
542 _audio.closeStream ();
545 if (Config::instance()->sound() && _audio.getDeviceCount() > 0) {
547 if (Config::instance()->sound_output()) {
548 while (st < _audio.getDeviceCount()) {
550 if (_audio.getDeviceInfo(st).name == Config::instance()->sound_output().get()) {
553 } catch (RtAudioError&) {
554 /* Something went wrong with that device so we don't want to use it anyway */
558 if (st == _audio.getDeviceCount()) {
559 st = _audio.getDefaultOutputDevice();
562 st = _audio.getDefaultOutputDevice();
566 _audio_channels = _audio.getDeviceInfo(st).outputChannels;
567 RtAudio::StreamParameters sp;
569 sp.nChannels = _audio_channels;
571 _audio.openStream (&sp, 0, RTAUDIO_FLOAT32, 48000, &_audio_block_size, &rtaudio_callback, this);
572 } catch (RtAudioError& e) {
576 _("Could not set up audio output. There will be no audio during the preview."), std_to_wx(e.what())
588 FilmViewer::uncorrected_time () const
590 if (_audio.isStreamRunning ()) {
591 return DCPTime::from_seconds (const_cast<RtAudio*>(&_audio)->getStreamTime());
594 return _video_view->position();
598 FilmViewer::audio_time () const
600 if (!_audio.isStreamRunning()) {
601 return optional<DCPTime>();
604 return DCPTime::from_seconds (const_cast<RtAudio*>(&_audio)->getStreamTime ()) -
605 DCPTime::from_frames (average_latency(), _film->audio_frame_rate());
609 FilmViewer::time () const
611 return audio_time().get_value_or(_video_view->position());
615 FilmViewer::audio_callback (void* out_p, unsigned int frames)
618 optional<DCPTime> t = _butler->get_audio (reinterpret_cast<float*> (out_p), frames);
619 if (!t || DCPTime(uncorrected_time() - *t) < one_video_frame()) {
620 /* There was an underrun or this audio is on time; carry on */
623 /* The audio we just got was (very) late; drop it and get some more. */
626 boost::mutex::scoped_lock lm (_latency_history_mutex, boost::try_to_lock);
628 _latency_history.push_back (_audio.getStreamLatency ());
629 if (_latency_history.size() > static_cast<size_t> (_latency_history_count)) {
630 _latency_history.pop_front ();
638 FilmViewer::average_latency () const
640 boost::mutex::scoped_lock lm (_latency_history_mutex);
641 if (_latency_history.empty()) {
646 for (auto i: _latency_history) {
650 return total / _latency_history.size();
654 FilmViewer::set_dcp_decode_reduction (optional<int> reduction)
656 _dcp_decode_reduction = reduction;
658 _player->set_dcp_decode_reduction (reduction);
663 FilmViewer::dcp_decode_reduction () const
665 return _dcp_decode_reduction;
669 FilmViewer::one_video_frame () const
671 return DCPTime::from_frames (1, _film ? _film->video_frame_rate() : 24);
674 /** Open a dialog box showing our film's closed captions */
676 FilmViewer::show_closed_captions ()
678 _closed_captions_dialog->Show();
682 FilmViewer::seek_by (DCPTime by, bool accurate)
684 seek (_video_view->position() + by, accurate);
688 FilmViewer::set_pad_black (bool p)
693 /** Called when a player has finished the current film.
694 * May be called from a non-UI thread.
697 FilmViewer::finished ()
699 emit (boost::bind(&FilmViewer::ui_finished, this));
702 /** Called by finished() in the UI thread */
704 FilmViewer::ui_finished ()
711 FilmViewer::dropped () const
713 return _video_view->dropped ();
718 FilmViewer::errored () const
720 return _video_view->errored ();
725 FilmViewer::gets () const
727 return _video_view->gets ();
732 FilmViewer::image_changed (shared_ptr<PlayerVideo> pv)
734 emit (boost::bind(boost::ref(ImageChanged), pv));
739 FilmViewer::too_many_frames_dropped ()
741 if (!Config::instance()->nagged(Config::NAG_TOO_MANY_DROPPED_FRAMES)) {
745 bool shown = NagDialog::maybe_nag (
747 Config::NAG_TOO_MANY_DROPPED_FRAMES,
748 _("The player is dropping a lot of frames, so playback may not be accurate.\n\n"
749 "<b>This does not necessarily mean that the DCP you are playing is defective!</b>\n\n"
750 "You may be able to improve player performance by:\n"
751 "• choosing 'decode at half resolution' or 'decode at quarter resolution' from the View menu\n"
752 "• using a more powerful computer.\n"