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>
65 using std::dynamic_pointer_cast;
69 using std::make_shared;
73 using std::shared_ptr;
77 using boost::optional;
78 #if BOOST_VERSION >= 106100
79 using namespace boost::placeholders;
82 using namespace dcpomatic;
87 rtaudio_callback (void* out, void *, unsigned int frames, double, RtAudioStreamStatus, void* data)
89 return reinterpret_cast<FilmViewer*>(data)->audio_callback (out, frames);
93 FilmViewer::FilmViewer (wxWindow* p)
94 : _audio (DCPOMATIC_RTAUDIO_API)
95 , _closed_captions_dialog (new ClosedCaptionsDialog(p, this))
97 switch (Config::instance()->video_view_type()) {
98 case Config::VIDEO_VIEW_OPENGL:
99 _video_view = std::make_shared<GLVideoView>(this, p);
101 case Config::VIDEO_VIEW_SIMPLE:
102 _video_view = std::make_shared<SimpleVideoView>(this, p);
106 _video_view->Sized.connect (boost::bind(&FilmViewer::video_view_sized, this));
107 _video_view->TooManyDropped.connect (boost::bind(boost::ref(TooManyDropped)));
109 set_film (shared_ptr<Film>());
111 _config_changed_connection = Config::instance()->Changed.connect(bind(&FilmViewer::config_changed, this, _1));
112 config_changed (Config::SOUND_OUTPUT);
116 FilmViewer::~FilmViewer ()
122 /** Ask for ::get() to be called next time we are idle */
124 FilmViewer::request_idle_display_next_frame ()
131 DCPOMATIC_ASSERT (signal_manager);
132 signal_manager->when_idle (boost::bind(&FilmViewer::idle_handler, this));
137 FilmViewer::idle_handler ()
143 if (_video_view->display_next_frame(true) == VideoView::AGAIN) {
144 /* get() could not complete quickly so we'll try again later */
145 signal_manager->when_idle (boost::bind(&FilmViewer::idle_handler, this));
153 FilmViewer::set_film (shared_ptr<Film> film)
161 _video_view->clear ();
162 _closed_captions_dialog->clear ();
167 _video_view->update ();
172 _player = make_shared<Player>(_film);
173 _player->set_fast ();
174 if (_dcp_decode_reduction) {
175 _player->set_dcp_decode_reduction (_dcp_decode_reduction);
177 } catch (bad_alloc &) {
178 error_dialog (_video_view->get(), _("There is not enough free memory to do that."));
183 _player->set_always_burn_open_subtitles ();
184 _player->set_play_referenced ();
186 _film->Change.connect (boost::bind (&FilmViewer::film_change, this, _1, _2));
187 _film->LengthChange.connect (boost::bind(&FilmViewer::film_length_change, this));
188 _player->Change.connect (boost::bind (&FilmViewer::player_change, this, _1, _2, _3));
190 film_change (ChangeType::DONE, Film::Property::VIDEO_FRAME_RATE);
191 film_change (ChangeType::DONE, Film::Property::THREE_D);
192 film_length_change ();
194 /* Keep about 1 second's worth of history samples */
195 _latency_history_count = _film->audio_frame_rate() / _audio_block_size;
197 _closed_captions_dialog->update_tracks (_film);
207 FilmViewer::recreate_butler ()
217 _butler = std::make_shared<Butler>(
220 Config::instance()->audio_mapping(_audio_channels),
222 bind(&PlayerVideo::force, _1, AV_PIX_FMT_RGB24),
228 if (!Config::instance()->sound() && !_audio.isStreamOpen()) {
229 _butler->disable_audio ();
232 if (dynamic_pointer_cast<GLVideoView>(_video_view) && _optimise_for_j2k) {
233 _butler->set_prepare_only_proxy (true);
236 _closed_captions_dialog->set_butler (_butler);
243 FilmViewer::set_outline_content (bool o)
245 _outline_content = o;
246 _video_view->update ();
251 FilmViewer::set_outline_subtitles (optional<dcpomatic::Rect<double>> rect)
253 _outline_subtitles = rect;
254 _video_view->update ();
259 FilmViewer::set_eyes (Eyes e)
261 _video_view->set_eyes (e);
267 FilmViewer::video_view_sized ()
270 if (!quick_refresh()) {
277 FilmViewer::calculate_sizes ()
279 if (!_film || !_player) {
283 auto const container = _film->container ();
285 auto const view_ratio = float(_video_view->get()->GetSize().x) / _video_view->get()->GetSize().y;
286 auto const film_ratio = container ? container->ratio () : 1.78;
289 if (view_ratio < film_ratio) {
290 /* panel is less widscreen than the film; clamp width */
291 out_size.width = _video_view->get()->GetSize().x;
292 out_size.height = lrintf (out_size.width / film_ratio);
294 /* panel is more widescreen than the film; clamp height */
295 out_size.height = _video_view->get()->GetSize().y;
296 out_size.width = lrintf (out_size.height * film_ratio);
299 /* Catch silly values */
300 out_size.width = max (64, out_size.width);
301 out_size.height = max (64, out_size.height);
303 _player->set_video_container_size (out_size);
308 FilmViewer::suspend ()
311 if (_audio.isStreamRunning()) {
312 _audio.abortStream();
318 FilmViewer::resume ()
320 DCPOMATIC_ASSERT (_suspended > 0);
322 if (_playing && !_suspended) {
323 if (_audio.isStreamOpen()) {
324 _audio.setStreamTime (_video_view->position().seconds());
325 _audio.startStream ();
327 _video_view->start ();
339 auto v = PlaybackPermitted ();
341 /* Computer says no */
345 /* We are about to set up the audio stream from the position of the video view.
346 If there is `lazy' seek in progress we need to wait for it to go through so that
347 _video_view->position() gives us a sensible answer.
353 /* Take the video view's idea of position as our `playhead' and start the
354 audio stream (which is the timing reference) there.
356 if (_audio.isStreamOpen()) {
357 _audio.setStreamTime (_video_view->position().seconds());
359 _audio.startStream ();
360 } catch (RtAudioError& e) {
364 _("There was a problem starting audio playback. Please try another audio output device in Preferences."), std_to_wx(e.what())
370 /* Calling start() below may directly result in Stopped being emitted, and if that
371 * happens we want it to come after the Started signal, so do that first.
373 Started (position());
374 _video_view->start ();
381 if (_audio.isStreamRunning()) {
382 /* stop stream and discard any remaining queued samples */
383 _audio.abortStream ();
391 _video_view->stop ();
392 Stopped (position());
394 _video_view->rethrow ();
400 FilmViewer::player_change (ChangeType type, int property, bool frequent)
402 if (type != ChangeType::DONE || frequent) {
406 if (_coalesce_player_changes) {
407 _pending_player_changes.push_back (property);
411 player_change ({property});
416 FilmViewer::player_change (vector<int> properties)
420 bool try_quick_refresh = false;
421 bool update_ccap_tracks = false;
423 for (auto i: properties) {
425 i == VideoContentProperty::CROP ||
426 i == VideoContentProperty::CUSTOM_RATIO ||
427 i == VideoContentProperty::CUSTOM_SIZE ||
428 i == VideoContentProperty::FADE_IN ||
429 i == VideoContentProperty::FADE_OUT ||
430 i == VideoContentProperty::COLOUR_CONVERSION ||
431 i == PlayerProperty::VIDEO_CONTAINER_SIZE ||
432 i == PlayerProperty::FILM_CONTAINER
434 try_quick_refresh = true;
437 if (i == TextContentProperty::USE || i == TextContentProperty::TYPE || i == TextContentProperty::DCP_TRACK) {
438 update_ccap_tracks = true;
442 if (!try_quick_refresh || !quick_refresh()) {
446 if (update_ccap_tracks) {
447 _closed_captions_dialog->update_tracks (_film);
453 FilmViewer::film_change (ChangeType type, Film::Property p)
455 if (type != ChangeType::DONE) {
459 if (p == Film::Property::AUDIO_CHANNELS) {
461 } else if (p == Film::Property::VIDEO_FRAME_RATE) {
462 _video_view->set_video_frame_rate (_film->video_frame_rate());
463 } else if (p == Film::Property::THREE_D) {
464 _video_view->set_three_d (_film->three_d());
465 } else if (p == Film::Property::CONTENT) {
466 _closed_captions_dialog->update_tracks (_film);
472 FilmViewer::film_length_change ()
474 _video_view->set_length (_film->length());
478 /** Re-get the current frame slowly by seeking */
480 FilmViewer::slow_refresh ()
482 seek (_video_view->position(), true);
486 /** Try to re-get the current frame quickly by resetting the metadata
487 * in the PlayerVideo that we used last time.
488 * @return true if this was possible, false if not.
491 FilmViewer::quick_refresh ()
493 if (!_video_view || !_film || !_player) {
496 return _video_view->reset_metadata (_film, _player->video_container_size());
501 FilmViewer::seek (shared_ptr<Content> content, ContentTime t, bool accurate)
503 auto dt = _player->content_time_to_dcp (content, t);
505 seek (*dt, accurate);
511 FilmViewer::set_coalesce_player_changes (bool c)
513 _coalesce_player_changes = c;
516 player_change (_pending_player_changes);
517 _pending_player_changes.clear ();
523 FilmViewer::seek (DCPTime t, bool accurate)
533 if (t >= _film->length()) {
534 t = _film->length() - one_video_frame();
539 _closed_captions_dialog->clear ();
540 _butler->seek (t, accurate);
543 /* We're not playing, so let the GUI thread get on and
544 come back later to get the next frame after the seek.
546 request_idle_display_next_frame ();
548 /* We're going to start playing again straight away
549 so wait for the seek to finish.
551 while (_video_view->display_next_frame(false) == VideoView::AGAIN) {}
559 FilmViewer::config_changed (Config::Property p)
561 if (p == Config::AUDIO_MAPPING) {
566 if (p != Config::SOUND && p != Config::SOUND_OUTPUT) {
570 if (_audio.isStreamOpen ()) {
571 _audio.closeStream ();
574 if (Config::instance()->sound() && _audio.getDeviceCount() > 0) {
576 if (Config::instance()->sound_output()) {
577 while (st < _audio.getDeviceCount()) {
579 if (_audio.getDeviceInfo(st).name == Config::instance()->sound_output().get()) {
582 } catch (RtAudioError&) {
583 /* Something went wrong with that device so we don't want to use it anyway */
587 if (st == _audio.getDeviceCount()) {
588 st = _audio.getDefaultOutputDevice();
591 st = _audio.getDefaultOutputDevice();
595 _audio_channels = _audio.getDeviceInfo(st).outputChannels;
596 RtAudio::StreamParameters sp;
598 sp.nChannels = _audio_channels;
600 _audio.openStream (&sp, 0, RTAUDIO_FLOAT32, 48000, &_audio_block_size, &rtaudio_callback, this);
601 } catch (RtAudioError& e) {
605 _("Could not set up audio output. There will be no audio during the preview."), std_to_wx(e.what())
618 FilmViewer::uncorrected_time () const
620 if (_audio.isStreamRunning()) {
621 return DCPTime::from_seconds (const_cast<RtAudio*>(&_audio)->getStreamTime());
624 return _video_view->position();
629 FilmViewer::audio_time () const
631 if (!_audio.isStreamRunning()) {
635 return DCPTime::from_seconds (const_cast<RtAudio*>(&_audio)->getStreamTime ()) -
636 DCPTime::from_frames (average_latency(), _film->audio_frame_rate());
641 FilmViewer::time () const
643 return audio_time().get_value_or(_video_view->position());
648 FilmViewer::audio_callback (void* out_p, unsigned int frames)
651 auto t = _butler->get_audio (reinterpret_cast<float*> (out_p), frames);
652 if (!t || DCPTime(uncorrected_time() - *t) < one_video_frame()) {
653 /* There was an underrun or this audio is on time; carry on */
656 /* The audio we just got was (very) late; drop it and get some more. */
659 boost::mutex::scoped_lock lm (_latency_history_mutex, boost::try_to_lock);
661 _latency_history.push_back (_audio.getStreamLatency ());
662 if (_latency_history.size() > static_cast<size_t> (_latency_history_count)) {
663 _latency_history.pop_front ();
672 FilmViewer::average_latency () const
674 boost::mutex::scoped_lock lm (_latency_history_mutex);
675 if (_latency_history.empty()) {
680 for (auto i: _latency_history) {
684 return total / _latency_history.size();
689 FilmViewer::set_dcp_decode_reduction (optional<int> reduction)
691 _dcp_decode_reduction = reduction;
693 _player->set_dcp_decode_reduction (reduction);
699 FilmViewer::dcp_decode_reduction () const
701 return _dcp_decode_reduction;
706 FilmViewer::one_video_frame () const
708 return DCPTime::from_frames (1, _film ? _film->video_frame_rate() : 24);
712 /** Open a dialog box showing our film's closed captions */
714 FilmViewer::show_closed_captions ()
716 _closed_captions_dialog->Show();
721 FilmViewer::seek_by (DCPTime by, bool accurate)
723 seek (_video_view->position() + by, accurate);
728 FilmViewer::set_pad_black (bool p)
734 /** Called when a player has finished the current film.
735 * May be called from a non-UI thread.
738 FilmViewer::finished ()
740 emit (boost::bind(&FilmViewer::ui_finished, this));
744 /** Called by finished() in the UI thread */
746 FilmViewer::ui_finished ()
754 FilmViewer::dropped () const
756 return _video_view->dropped ();
761 FilmViewer::errored () const
763 return _video_view->errored ();
768 FilmViewer::gets () const
770 return _video_view->gets ();
775 FilmViewer::image_changed (shared_ptr<PlayerVideo> pv)
777 emit (boost::bind(boost::ref(ImageChanged), pv));
782 FilmViewer::set_optimise_for_j2k (bool o)
784 _optimise_for_j2k = o;
785 _video_view->set_optimise_for_j2k (o);