2 Copyright (C) 2012-2019 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"
29 #include "closed_captions_dialog.h"
30 #include "gl_video_view.h"
31 #include "simple_video_view.h"
33 #include "lib/ratio.h"
35 #include "lib/job_manager.h"
36 #include "lib/image.h"
37 #include "lib/exceptions.h"
38 #include "lib/examine_content_job.h"
39 #include "lib/filter.h"
40 #include "lib/player.h"
41 #include "lib/player_video.h"
42 #include "lib/video_content.h"
43 #include "lib/video_decoder.h"
44 #include "lib/timer.h"
45 #include "lib/butler.h"
47 #include "lib/config.h"
48 #include "lib/compose.hpp"
49 #include "lib/dcpomatic_log.h"
50 #include "lib/text_content.h"
52 #include <libavutil/pixfmt.h>
54 #include <dcp/exceptions.h>
55 #include <wx/tglbtn.h>
68 using boost::shared_ptr;
69 using boost::dynamic_pointer_cast;
70 using boost::weak_ptr;
71 using boost::optional;
73 using namespace dcpomatic;
77 rtaudio_callback (void* out, void *, unsigned int frames, double, RtAudioStreamStatus, void* data)
79 return reinterpret_cast<FilmViewer*>(data)->audio_callback (out, frames);
82 FilmViewer::FilmViewer (wxWindow* p)
83 : _coalesce_player_changes (false)
84 , _audio (DCPOMATIC_RTAUDIO_API)
86 , _audio_block_size (1024)
89 , _latency_history_count (0)
90 , _closed_captions_dialog (new ClosedCaptionsDialog(p, this))
91 , _outline_content (false)
93 #ifdef DCPOMATIC_VARIANT_SWAROOP
94 , _background_image (false)
98 switch (Config::instance()->video_view_type()) {
99 case Config::VIDEO_VIEW_OPENGL:
100 _video_view = new GLVideoView (this, p);
102 case Config::VIDEO_VIEW_SIMPLE:
103 _video_view = new SimpleVideoView (this, p);
107 _video_view->Sized.connect (boost::bind(&FilmViewer::video_view_sized, this));
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);
115 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));
134 FilmViewer::idle_handler ()
140 if (_video_view->display_next_frame(true)) {
143 /* get() could not complete quickly so we'll try again later */
144 signal_manager->when_idle (boost::bind(&FilmViewer::idle_handler, this));
149 FilmViewer::set_film (shared_ptr<Film> film)
157 _video_view->clear ();
158 _closed_captions_dialog->clear ();
163 _video_view->update ();
168 _player.reset (new Player(_film));
169 _player->set_fast ();
170 if (_dcp_decode_reduction) {
171 _player->set_dcp_decode_reduction (_dcp_decode_reduction);
173 } catch (bad_alloc &) {
174 error_dialog (_video_view->get(), _("There is not enough free memory to do that."));
179 _player->set_always_burn_open_subtitles ();
180 _player->set_play_referenced ();
182 _film->Change.connect (boost::bind (&FilmViewer::film_change, this, _1, _2));
183 _film->ContentChange.connect (boost::bind(&FilmViewer::content_change, this, _1, _3));
184 _film->LengthChange.connect (boost::bind(&FilmViewer::film_length_change, this));
185 _player->Change.connect (boost::bind (&FilmViewer::player_change, this, _1, _2, _3));
187 film_change (CHANGE_TYPE_DONE, Film::VIDEO_FRAME_RATE);
188 film_change (CHANGE_TYPE_DONE, Film::THREE_D);
189 film_length_change ();
191 /* Keep about 1 second's worth of history samples */
192 _latency_history_count = _film->audio_frame_rate() / _audio_block_size;
194 _closed_captions_dialog->update_tracks (_film);
203 FilmViewer::recreate_butler ()
216 Config::instance()->audio_mapping(_audio_channels),
218 bind(&PlayerVideo::force, _1, AV_PIX_FMT_RGB24),
224 if (!Config::instance()->sound() && !_audio.isStreamOpen()) {
225 _butler->disable_audio ();
228 _closed_captions_dialog->set_butler (_butler);
234 FilmViewer::set_outline_content (bool o)
236 _outline_content = o;
237 _video_view->update ();
242 FilmViewer::set_outline_subtitles (optional<dcpomatic::Rect<double> > rect)
244 _outline_subtitles = rect;
245 _video_view->update ();
250 FilmViewer::set_eyes (Eyes e)
252 _video_view->set_eyes (e);
257 FilmViewer::video_view_sized ()
260 if (!quick_refresh()) {
266 FilmViewer::calculate_sizes ()
268 if (!_film || !_player) {
272 Ratio const * container = _film->container ();
274 float const view_ratio = float(_video_view->get()->GetSize().x) / _video_view->get()->GetSize().y;
275 float const film_ratio = container ? container->ratio () : 1.78;
277 if (view_ratio < film_ratio) {
278 /* panel is less widscreen than the film; clamp width */
279 _out_size.width = _video_view->get()->GetSize().x;
280 _out_size.height = lrintf (_out_size.width / film_ratio);
282 /* panel is more widescreen than the film; clamp height */
283 _out_size.height = _video_view->get()->GetSize().y;
284 _out_size.width = lrintf (_out_size.height * film_ratio);
287 /* Catch silly values */
288 _out_size.width = max (64, _out_size.width);
289 _out_size.height = max (64, _out_size.height);
291 _player->set_video_container_size (_out_size);
295 FilmViewer::suspend ()
298 if (_audio.isStreamRunning()) {
299 _audio.abortStream();
304 FilmViewer::resume ()
306 DCPOMATIC_ASSERT (_suspended > 0);
308 if (_playing && !_suspended) {
309 if (_audio.isStreamOpen()) {
310 _audio.setStreamTime (_video_view->position().seconds());
311 _audio.startStream ();
313 _video_view->start ();
324 optional<bool> v = PlaybackPermitted ();
326 /* Computer says no */
330 /* We are about to set up the audio stream from the position of the video view.
331 If there is `lazy' seek in progress we need to wait for it to go through so that
332 _video_view->position() gives us a sensible answer.
338 /* Take the video view's idea of position as our `playhead' and start the
339 audio stream (which is the timing reference) there.
341 if (_audio.isStreamOpen()) {
342 _audio.setStreamTime (_video_view->position().seconds());
343 _audio.startStream ();
347 _video_view->start ();
348 Started (position());
354 if (_audio.isStreamRunning()) {
355 /* stop stream and discard any remaining queued samples */
356 _audio.abortStream ();
364 _video_view->stop ();
365 Stopped (position());
367 _video_view->rethrow ();
372 FilmViewer::player_change (ChangeType type, int property, bool frequent)
374 if (type != CHANGE_TYPE_DONE || frequent) {
378 if (_coalesce_player_changes) {
379 _pending_player_changes.push_back (property);
384 bool refreshed = false;
386 property == VideoContentProperty::CROP ||
387 property == VideoContentProperty::SCALE ||
388 property == VideoContentProperty::FADE_IN ||
389 property == VideoContentProperty::FADE_OUT ||
390 property == VideoContentProperty::COLOUR_CONVERSION ||
391 property == PlayerProperty::VIDEO_CONTAINER_SIZE ||
392 property == PlayerProperty::FILM_CONTAINER
394 refreshed = quick_refresh ();
403 FilmViewer::film_change (ChangeType type, Film::Property p)
405 if (type != CHANGE_TYPE_DONE) {
409 if (p == Film::AUDIO_CHANNELS) {
411 } else if (p == Film::VIDEO_FRAME_RATE) {
412 _video_view->set_video_frame_rate (_film->video_frame_rate());
413 } else if (p == Film::THREE_D) {
414 _video_view->set_three_d (_film->three_d());
415 } else if (p == Film::CONTENT) {
416 _closed_captions_dialog->update_tracks (_film);
421 FilmViewer::film_length_change ()
423 _video_view->set_length (_film->length());
426 /** Re-get the current frame slowly by seeking */
428 FilmViewer::slow_refresh ()
430 seek (_video_view->position(), true);
433 /** Try to re-get the current frame quickly by resetting the metadata
434 * in the PlayerVideo that we used last time.
435 * @return true if this was possible, false if not.
438 FilmViewer::quick_refresh ()
440 if (!_video_view || !_film || !_player) {
443 return _video_view->refresh_metadata (_film, _player->video_container_size(), _film->frame_size());
447 FilmViewer::seek (shared_ptr<Content> content, ContentTime t, bool accurate)
449 optional<DCPTime> dt = _player->content_time_to_dcp (content, t);
451 seek (*dt, accurate);
456 FilmViewer::set_coalesce_player_changes (bool c)
458 _coalesce_player_changes = c;
461 BOOST_FOREACH (int i, _pending_player_changes) {
462 player_change (CHANGE_TYPE_DONE, i, false);
464 _pending_player_changes.clear ();
469 FilmViewer::seek (DCPTime t, bool accurate)
475 if (t < DCPTime ()) {
479 if (t >= _film->length ()) {
480 t = _film->length() - one_video_frame();
485 _closed_captions_dialog->clear ();
486 _butler->seek (t, accurate);
489 /* We're not playing, so let the GUI thread get on and
490 come back later to get the next frame after the seek.
492 request_idle_display_next_frame ();
494 /* We're going to start playing again straight away
495 so wait for the seek to finish.
497 while (!_video_view->display_next_frame(false)) {}
504 FilmViewer::config_changed (Config::Property p)
506 #ifdef DCPOMATIC_VARIANT_SWAROOP
507 if (p == Config::PLAYER_BACKGROUND_IMAGE) {
508 _video_view->update ();
513 if (p == Config::AUDIO_MAPPING) {
518 if (p != Config::SOUND && p != Config::SOUND_OUTPUT) {
522 if (_audio.isStreamOpen ()) {
523 _audio.closeStream ();
526 if (Config::instance()->sound() && _audio.getDeviceCount() > 0) {
528 if (Config::instance()->sound_output()) {
529 while (st < _audio.getDeviceCount()) {
530 if (_audio.getDeviceInfo(st).name == Config::instance()->sound_output().get()) {
535 if (st == _audio.getDeviceCount()) {
536 st = _audio.getDefaultOutputDevice();
539 st = _audio.getDefaultOutputDevice();
542 _audio_channels = _audio.getDeviceInfo(st).outputChannels;
544 RtAudio::StreamParameters sp;
546 sp.nChannels = _audio_channels;
549 _audio.openStream (&sp, 0, RTAUDIO_FLOAT32, 48000, &_audio_block_size, &rtaudio_callback, this);
550 #ifdef DCPOMATIC_USE_RTERROR
551 } catch (RtError& e) {
553 } catch (RtAudioError& e) {
557 _("Could not set up audio output. There will be no audio during the preview."), std_to_wx(e.what())
569 FilmViewer::uncorrected_time () const
571 if (_audio.isStreamRunning ()) {
572 return DCPTime::from_seconds (const_cast<RtAudio*>(&_audio)->getStreamTime());
575 return _video_view->position();
579 FilmViewer::audio_time () const
581 if (!_audio.isStreamRunning()) {
582 return optional<DCPTime>();
585 return DCPTime::from_seconds (const_cast<RtAudio*>(&_audio)->getStreamTime ()) -
586 DCPTime::from_frames (average_latency(), _film->audio_frame_rate());
590 FilmViewer::time () const
592 return audio_time().get_value_or(_video_view->position());
596 FilmViewer::audio_callback (void* out_p, unsigned int frames)
599 optional<DCPTime> t = _butler->get_audio (reinterpret_cast<float*> (out_p), frames);
600 if (!t || DCPTime(uncorrected_time() - *t) < one_video_frame()) {
601 /* There was an underrun or this audio is on time; carry on */
604 /* The audio we just got was (very) late; drop it and get some more. */
607 boost::mutex::scoped_lock lm (_latency_history_mutex, boost::try_to_lock);
609 _latency_history.push_back (_audio.getStreamLatency ());
610 if (_latency_history.size() > static_cast<size_t> (_latency_history_count)) {
611 _latency_history.pop_front ();
619 FilmViewer::average_latency () const
621 boost::mutex::scoped_lock lm (_latency_history_mutex);
622 if (_latency_history.empty()) {
627 BOOST_FOREACH (Frame i, _latency_history) {
631 return total / _latency_history.size();
635 FilmViewer::set_dcp_decode_reduction (optional<int> reduction)
637 _dcp_decode_reduction = reduction;
639 _player->set_dcp_decode_reduction (reduction);
644 FilmViewer::dcp_decode_reduction () const
646 return _dcp_decode_reduction;
650 FilmViewer::one_video_frame () const
652 return DCPTime::from_frames (1, _film ? _film->video_frame_rate() : 24);
655 /** Open a dialog box showing our film's closed captions */
657 FilmViewer::show_closed_captions ()
659 _closed_captions_dialog->Show();
663 FilmViewer::seek_by (DCPTime by, bool accurate)
665 seek (_video_view->position() + by, accurate);
669 FilmViewer::set_pad_black (bool p)
674 /** Called when a player has finished the current film.
675 * May be called from a non-UI thread.
678 FilmViewer::finished ()
680 emit (boost::bind(&FilmViewer::ui_finished, this));
683 /** Called by finished() in the UI thread */
685 FilmViewer::ui_finished ()
692 FilmViewer::dropped () const
694 return _video_view->dropped ();
699 FilmViewer::errored () const
701 return _video_view->errored ();
706 FilmViewer::gets () const
708 return _video_view->gets ();
713 FilmViewer::content_change (ChangeType type, int property)
715 if (type != CHANGE_TYPE_DONE) {
719 if (property == TextContentProperty::USE || property == TextContentProperty::TYPE || property == TextContentProperty::DCP_TRACK) {
720 _closed_captions_dialog->update_tracks (_film);