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;
66 using std::make_shared;
68 using std::shared_ptr;
71 using boost::optional;
72 #if BOOST_VERSION >= 106100
73 using namespace boost::placeholders;
76 using namespace dcpomatic;
81 rtaudio_callback (void* out, void *, unsigned int frames, double, RtAudioStreamStatus, void* data)
83 return reinterpret_cast<FilmViewer*>(data)->audio_callback (out, frames);
87 FilmViewer::FilmViewer (wxWindow* p)
88 : _audio (DCPOMATIC_RTAUDIO_API)
89 , _closed_captions_dialog (new ClosedCaptionsDialog(p, this))
91 #if wxCHECK_VERSION(3, 1, 0)
92 switch (Config::instance()->video_view_type()) {
93 case Config::VIDEO_VIEW_OPENGL:
94 _video_view = std::make_shared<GLVideoView>(this, p);
96 case Config::VIDEO_VIEW_SIMPLE:
97 _video_view = std::make_shared<SimpleVideoView>(this, p);
101 _video_view = std::make_shared<SimpleVideoView>(this, p);
104 _video_view->Sized.connect (boost::bind(&FilmViewer::video_view_sized, this));
105 _video_view->TooManyDropped.connect (boost::bind(boost::ref(TooManyDropped)));
107 set_film (shared_ptr<Film>());
109 _config_changed_connection = Config::instance()->Changed.connect(bind(&FilmViewer::config_changed, this, _1));
110 config_changed (Config::SOUND_OUTPUT);
114 FilmViewer::~FilmViewer ()
120 /** Ask for ::get() to be called next time we are idle */
122 FilmViewer::request_idle_display_next_frame ()
129 DCPOMATIC_ASSERT (signal_manager);
130 signal_manager->when_idle (boost::bind(&FilmViewer::idle_handler, this));
135 FilmViewer::idle_handler ()
141 if (_video_view->display_next_frame(true) == VideoView::AGAIN) {
142 /* get() could not complete quickly so we'll try again later */
143 signal_manager->when_idle (boost::bind(&FilmViewer::idle_handler, this));
151 FilmViewer::set_film (shared_ptr<Film> film)
159 _video_view->clear ();
160 _closed_captions_dialog->clear ();
165 _video_view->update ();
170 _player = make_shared<Player>(_film, _optimise_for_j2k ? Image::Alignment::COMPACT : Image::Alignment::PADDED);
171 _player->set_fast ();
172 if (_dcp_decode_reduction) {
173 _player->set_dcp_decode_reduction (_dcp_decode_reduction);
175 } catch (bad_alloc &) {
176 error_dialog (_video_view->get(), _("There is not enough free memory to do that."));
181 _player->set_always_burn_open_subtitles ();
182 _player->set_play_referenced ();
184 _film->Change.connect (boost::bind (&FilmViewer::film_change, this, _1, _2));
185 _film->LengthChange.connect (boost::bind(&FilmViewer::film_length_change, this));
186 _player->Change.connect (boost::bind (&FilmViewer::player_change, this, _1, _2, _3));
188 film_change (ChangeType::DONE, Film::Property::VIDEO_FRAME_RATE);
189 film_change (ChangeType::DONE, Film::Property::THREE_D);
190 film_length_change ();
192 /* Keep about 1 second's worth of history samples */
193 _latency_history_count = _film->audio_frame_rate() / _audio_block_size;
195 _closed_captions_dialog->update_tracks (_film);
205 FilmViewer::recreate_butler ()
215 #if wxCHECK_VERSION(3, 1, 0)
216 auto const j2k_gl_optimised = dynamic_pointer_cast<GLVideoView>(_video_view) && _optimise_for_j2k;
218 auto const j2k_gl_optimised = false;
221 _butler = std::make_shared<Butler>(
224 Config::instance()->audio_mapping(_audio_channels),
226 bind(&PlayerVideo::force, _1, AV_PIX_FMT_RGB24),
228 j2k_gl_optimised ? Image::Alignment::COMPACT : Image::Alignment::PADDED,
233 if (!Config::instance()->sound() && !_audio.isStreamOpen()) {
234 _butler->disable_audio ();
237 _closed_captions_dialog->set_butler (_butler);
244 FilmViewer::set_outline_content (bool o)
246 _outline_content = o;
247 _video_view->update ();
252 FilmViewer::set_outline_subtitles (optional<dcpomatic::Rect<double>> rect)
254 _outline_subtitles = rect;
255 _video_view->update ();
260 FilmViewer::set_eyes (Eyes e)
262 _video_view->set_eyes (e);
268 FilmViewer::video_view_sized ()
271 if (!quick_refresh()) {
278 FilmViewer::calculate_sizes ()
280 if (!_film || !_player) {
284 auto const container = _film->container ();
286 auto const dpi_scale_factor = _video_view->get()->GetDPIScaleFactor();
287 int const video_view_width = std::round(_video_view->get()->GetSize().x * dpi_scale_factor);
288 int const video_view_height = std::round(_video_view->get()->GetSize().y * dpi_scale_factor);
290 auto const view_ratio = float(video_view_width) / video_view_height;
291 auto const film_ratio = container ? container->ratio () : 1.78;
294 if (view_ratio < film_ratio) {
295 /* panel is less widscreen than the film; clamp width */
296 out_size.width = video_view_width;
297 out_size.height = lrintf (out_size.width / film_ratio);
299 /* panel is more widescreen than the film; clamp height */
300 out_size.height = video_view_height;
301 out_size.width = lrintf (out_size.height * film_ratio);
304 /* Catch silly values */
305 out_size.width = max (64, out_size.width);
306 out_size.height = max (64, out_size.height);
308 _player->set_video_container_size (out_size);
313 FilmViewer::suspend ()
316 if (_audio.isStreamRunning()) {
317 _audio.abortStream();
323 FilmViewer::resume ()
325 DCPOMATIC_ASSERT (_suspended > 0);
327 if (_playing && !_suspended) {
328 if (_audio.isStreamOpen()) {
329 _audio.setStreamTime (_video_view->position().seconds());
330 _audio.startStream ();
332 _video_view->start ();
344 auto v = PlaybackPermitted ();
346 /* Computer says no */
350 /* We are about to set up the audio stream from the position of the video view.
351 If there is `lazy' seek in progress we need to wait for it to go through so that
352 _video_view->position() gives us a sensible answer.
358 /* Take the video view's idea of position as our `playhead' and start the
359 audio stream (which is the timing reference) there.
361 if (_audio.isStreamOpen()) {
362 _audio.setStreamTime (_video_view->position().seconds());
364 _audio.startStream ();
365 } catch (RtAudioError& e) {
369 _("There was a problem starting audio playback. Please try another audio output device in Preferences."), std_to_wx(e.what())
375 /* Calling start() below may directly result in Stopped being emitted, and if that
376 * happens we want it to come after the Started signal, so do that first.
379 _video_view->start ();
386 if (_audio.isStreamRunning()) {
387 /* stop stream and discard any remaining queued samples */
388 _audio.abortStream ();
396 _video_view->stop ();
399 _video_view->rethrow ();
405 FilmViewer::player_change (ChangeType type, int property, bool frequent)
407 if (type != ChangeType::DONE || frequent) {
411 if (_coalesce_player_changes) {
412 _pending_player_changes.push_back (property);
416 player_change ({property});
421 FilmViewer::player_change (vector<int> properties)
425 bool try_quick_refresh = false;
426 bool update_ccap_tracks = false;
428 for (auto i: properties) {
430 i == VideoContentProperty::CROP ||
431 i == VideoContentProperty::CUSTOM_RATIO ||
432 i == VideoContentProperty::CUSTOM_SIZE ||
433 i == VideoContentProperty::FADE_IN ||
434 i == VideoContentProperty::FADE_OUT ||
435 i == VideoContentProperty::COLOUR_CONVERSION ||
436 i == PlayerProperty::VIDEO_CONTAINER_SIZE ||
437 i == PlayerProperty::FILM_CONTAINER
439 try_quick_refresh = true;
442 if (i == TextContentProperty::USE || i == TextContentProperty::TYPE || i == TextContentProperty::DCP_TRACK) {
443 update_ccap_tracks = true;
447 if (!try_quick_refresh || !quick_refresh()) {
451 if (update_ccap_tracks) {
452 _closed_captions_dialog->update_tracks (_film);
458 FilmViewer::film_change (ChangeType type, Film::Property p)
460 if (type != ChangeType::DONE) {
464 if (p == Film::Property::AUDIO_CHANNELS) {
466 } else if (p == Film::Property::VIDEO_FRAME_RATE) {
467 _video_view->set_video_frame_rate (_film->video_frame_rate());
468 } else if (p == Film::Property::THREE_D) {
469 _video_view->set_three_d (_film->three_d());
470 } else if (p == Film::Property::CONTENT) {
471 _closed_captions_dialog->update_tracks (_film);
477 FilmViewer::film_length_change ()
479 _video_view->set_length (_film->length());
483 /** Re-get the current frame slowly by seeking */
485 FilmViewer::slow_refresh ()
487 seek (_video_view->position(), true);
491 /** Try to re-get the current frame quickly by resetting the metadata
492 * in the PlayerVideo that we used last time.
493 * @return true if this was possible, false if not.
496 FilmViewer::quick_refresh ()
498 if (!_video_view || !_film || !_player) {
501 return _video_view->reset_metadata (_film, _player->video_container_size());
506 FilmViewer::seek (shared_ptr<Content> content, ContentTime t, bool accurate)
508 auto dt = _player->content_time_to_dcp (content, t);
510 seek (*dt, accurate);
516 FilmViewer::set_coalesce_player_changes (bool c)
518 _coalesce_player_changes = c;
521 player_change (_pending_player_changes);
522 _pending_player_changes.clear ();
528 FilmViewer::seek (DCPTime t, bool accurate)
538 if (t >= _film->length()) {
539 t = _film->length() - one_video_frame();
544 _closed_captions_dialog->clear ();
545 _butler->seek (t, accurate);
548 /* We're not playing, so let the GUI thread get on and
549 come back later to get the next frame after the seek.
551 request_idle_display_next_frame ();
553 /* We're going to start playing again straight away
554 so wait for the seek to finish.
556 while (_video_view->display_next_frame(false) == VideoView::AGAIN) {}
564 FilmViewer::config_changed (Config::Property p)
566 if (p == Config::AUDIO_MAPPING) {
571 if (p != Config::SOUND && p != Config::SOUND_OUTPUT) {
575 if (_audio.isStreamOpen ()) {
576 _audio.closeStream ();
579 if (Config::instance()->sound() && _audio.getDeviceCount() > 0) {
581 if (Config::instance()->sound_output()) {
582 while (st < _audio.getDeviceCount()) {
584 if (_audio.getDeviceInfo(st).name == Config::instance()->sound_output().get()) {
587 } catch (RtAudioError&) {
588 /* Something went wrong with that device so we don't want to use it anyway */
592 if (st == _audio.getDeviceCount()) {
593 st = _audio.getDefaultOutputDevice();
596 st = _audio.getDefaultOutputDevice();
600 _audio_channels = _audio.getDeviceInfo(st).outputChannels;
601 RtAudio::StreamParameters sp;
603 sp.nChannels = _audio_channels;
605 _audio.openStream (&sp, 0, RTAUDIO_FLOAT32, 48000, &_audio_block_size, &rtaudio_callback, this);
606 } catch (RtAudioError& e) {
610 _("Could not set up audio output. There will be no audio during the preview."), std_to_wx(e.what())
623 FilmViewer::uncorrected_time () const
625 if (_audio.isStreamRunning()) {
626 return DCPTime::from_seconds (const_cast<RtAudio*>(&_audio)->getStreamTime());
629 return _video_view->position();
634 FilmViewer::audio_time () const
636 if (!_audio.isStreamRunning()) {
640 return DCPTime::from_seconds (const_cast<RtAudio*>(&_audio)->getStreamTime ()) -
641 DCPTime::from_frames (average_latency(), _film->audio_frame_rate());
646 FilmViewer::time () const
648 return audio_time().get_value_or(_video_view->position());
653 FilmViewer::audio_callback (void* out_p, unsigned int frames)
656 auto t = _butler->get_audio (Butler::Behaviour::NON_BLOCKING, reinterpret_cast<float*> (out_p), frames);
657 if (!t || DCPTime(uncorrected_time() - *t) < one_video_frame()) {
658 /* There was an underrun or this audio is on time; carry on */
661 /* The audio we just got was (very) late; drop it and get some more. */
664 boost::mutex::scoped_lock lm (_latency_history_mutex, boost::try_to_lock);
666 _latency_history.push_back (_audio.getStreamLatency ());
667 if (_latency_history.size() > static_cast<size_t> (_latency_history_count)) {
668 _latency_history.pop_front ();
677 FilmViewer::average_latency () const
679 boost::mutex::scoped_lock lm (_latency_history_mutex);
680 if (_latency_history.empty()) {
685 for (auto i: _latency_history) {
689 return total / _latency_history.size();
694 FilmViewer::set_dcp_decode_reduction (optional<int> reduction)
696 _dcp_decode_reduction = reduction;
698 _player->set_dcp_decode_reduction (reduction);
704 FilmViewer::dcp_decode_reduction () const
706 return _dcp_decode_reduction;
711 FilmViewer::one_video_frame () const
713 return DCPTime::from_frames (1, _film ? _film->video_frame_rate() : 24);
717 /** Open a dialog box showing our film's closed captions */
719 FilmViewer::show_closed_captions ()
721 _closed_captions_dialog->Show();
726 FilmViewer::seek_by (DCPTime by, bool accurate)
728 seek (_video_view->position() + by, accurate);
733 FilmViewer::set_pad_black (bool p)
739 /** Called when a player has finished the current film.
740 * May be called from a non-UI thread.
743 FilmViewer::finished ()
745 emit (boost::bind(&FilmViewer::ui_finished, this));
749 /** Called by finished() in the UI thread */
751 FilmViewer::ui_finished ()
759 FilmViewer::dropped () const
761 return _video_view->dropped ();
766 FilmViewer::errored () const
768 return _video_view->errored ();
773 FilmViewer::gets () const
775 return _video_view->gets ();
780 FilmViewer::image_changed (shared_ptr<PlayerVideo> pv)
782 emit (boost::bind(boost::ref(ImageChanged), pv));
787 FilmViewer::set_optimise_for_j2k (bool o)
789 _optimise_for_j2k = o;
790 _video_view->set_optimise_for_j2k (o);