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"
51 #include <libavutil/pixfmt.h>
53 #include <dcp/exceptions.h>
54 #include <wx/tglbtn.h>
67 using boost::shared_ptr;
68 using boost::dynamic_pointer_cast;
69 using boost::weak_ptr;
70 using boost::optional;
72 using namespace dcpomatic;
76 rtaudio_callback (void* out, void *, unsigned int frames, double, RtAudioStreamStatus, void* data)
78 return reinterpret_cast<FilmViewer*>(data)->audio_callback (out, frames);
81 FilmViewer::FilmViewer (wxWindow* p)
82 : _coalesce_player_changes (false)
83 , _audio (DCPOMATIC_RTAUDIO_API)
85 , _audio_block_size (1024)
88 , _latency_history_count (0)
90 , _closed_captions_dialog (new ClosedCaptionsDialog(p, this))
91 , _outline_content (false)
94 #ifdef DCPOMATIC_VARIANT_SWAROOP
95 , _background_image (false)
97 , _state_timer ("viewer")
101 switch (Config::instance()->video_view_type()) {
102 case Config::VIDEO_VIEW_OPENGL:
103 _video_view = new GLVideoView (this, p);
105 case Config::VIDEO_VIEW_SIMPLE:
106 _video_view = new SimpleVideoView (this, p);
110 _video_view->Sized.connect (boost::bind(&FilmViewer::video_view_sized, this));
112 set_film (shared_ptr<Film> ());
114 _config_changed_connection = Config::instance()->Changed.connect (bind (&FilmViewer::config_changed, this, _1));
115 config_changed (Config::SOUND_OUTPUT);
118 FilmViewer::~FilmViewer ()
123 /** Ask for ::get() to be called next time we are idle */
125 FilmViewer::request_idle_display_next_frame ()
132 DCPOMATIC_ASSERT (signal_manager);
133 signal_manager->when_idle (boost::bind(&FilmViewer::idle_handler, this));
137 FilmViewer::idle_handler ()
143 if (_video_view->display_next_frame(true)) {
146 /* get() could not complete quickly so we'll try again later */
147 signal_manager->when_idle (boost::bind(&FilmViewer::idle_handler, this));
152 FilmViewer::set_film (shared_ptr<Film> film)
160 _video_view->clear ();
161 _closed_captions_dialog->clear ();
171 _player.reset (new Player (_film, _film->playlist ()));
172 _player->set_fast ();
173 if (_dcp_decode_reduction) {
174 _player->set_dcp_decode_reduction (_dcp_decode_reduction);
176 } catch (bad_alloc &) {
177 error_dialog (_video_view->get(), _("There is not enough free memory to do that."));
182 _player->set_always_burn_open_subtitles ();
183 _player->set_play_referenced ();
185 _film->Change.connect (boost::bind (&FilmViewer::film_change, this, _1, _2));
186 _film->LengthChange.connect (boost::bind(&FilmViewer::film_length_change, this));
187 _player->Change.connect (boost::bind (&FilmViewer::player_change, this, _1, _2, _3));
189 /* Keep about 1 second's worth of history samples */
190 _latency_history_count = _film->audio_frame_rate() / _audio_block_size;
199 FilmViewer::recreate_butler ()
212 Config::instance()->audio_mapping(_audio_channels),
214 bind(&PlayerVideo::force, _1, AV_PIX_FMT_RGB24),
220 if (!Config::instance()->sound() && !_audio.isStreamOpen()) {
221 _butler->disable_audio ();
224 _closed_captions_dialog->set_film_and_butler (_film, _butler);
230 FilmViewer::refresh_view ()
232 _state_timer.set ("update-view");
233 _video_view->update ();
234 _state_timer.unset ();
238 FilmViewer::set_outline_content (bool o)
240 _outline_content = o;
245 FilmViewer::set_eyes (Eyes e)
252 FilmViewer::video_view_sized ()
255 if (!quick_refresh()) {
261 FilmViewer::calculate_sizes ()
263 if (!_film || !_player) {
267 Ratio const * container = _film->container ();
269 float const view_ratio = float(_video_view->get()->GetSize().x) / _video_view->get()->GetSize().y;
270 float const film_ratio = container ? container->ratio () : 1.78;
272 if (view_ratio < film_ratio) {
273 /* panel is less widscreen than the film; clamp width */
274 _out_size.width = _video_view->get()->GetSize().x;
275 _out_size.height = lrintf (_out_size.width / film_ratio);
277 /* panel is more widescreen than the film; clamp height */
278 _out_size.height = _video_view->get()->GetSize().y;
279 _out_size.width = lrintf (_out_size.height * film_ratio);
282 /* Catch silly values */
283 _out_size.width = max (64, _out_size.width);
284 _out_size.height = max (64, _out_size.height);
286 _player->set_video_container_size (_out_size);
290 FilmViewer::suspend ()
293 if (_audio.isStreamRunning()) {
294 _audio.abortStream();
299 FilmViewer::resume ()
302 if (_playing && !_suspended) {
303 if (_audio.isStreamOpen()) {
304 _audio.setStreamTime (_video_view->position().seconds());
305 _audio.startStream ();
307 _video_view->start ();
318 optional<bool> v = PlaybackPermitted ();
320 /* Computer says no */
324 if (_audio.isStreamOpen()) {
325 _audio.setStreamTime (_video_view->position().seconds());
326 _audio.startStream ();
331 _video_view->start ();
332 Started (position());
338 if (_audio.isStreamRunning()) {
339 /* stop stream and discard any remaining queued samples */
340 _audio.abortStream ();
348 _video_view->stop ();
349 Stopped (position());
354 FilmViewer::player_change (ChangeType type, int property, bool frequent)
356 if (type != CHANGE_TYPE_DONE || frequent) {
360 if (_coalesce_player_changes) {
361 _pending_player_changes.push_back (property);
366 bool refreshed = false;
368 property == VideoContentProperty::CROP ||
369 property == VideoContentProperty::SCALE ||
370 property == VideoContentProperty::FADE_IN ||
371 property == VideoContentProperty::FADE_OUT ||
372 property == VideoContentProperty::COLOUR_CONVERSION ||
373 property == PlayerProperty::VIDEO_CONTAINER_SIZE ||
374 property == PlayerProperty::FILM_CONTAINER
376 refreshed = quick_refresh ();
385 FilmViewer::film_change (ChangeType type, Film::Property p)
387 if (type != CHANGE_TYPE_DONE) {
391 if (p == Film::AUDIO_CHANNELS) {
393 } else if (p == Film::VIDEO_FRAME_RATE) {
394 _video_view->set_video_frame_rate (_film->video_frame_rate());
399 FilmViewer::film_length_change ()
401 _video_view->set_length (_film->length());
404 /** Re-get the current frame slowly by seeking */
406 FilmViewer::slow_refresh ()
408 seek (_video_view->position(), true);
411 /** Try to re-get the current frame quickly by resetting the metadata
412 * in the PlayerVideo that we used last time.
413 * @return true if this was possible, false if not.
416 FilmViewer::quick_refresh ()
418 if (!_video_view->_player_video.first) {
422 if (!_video_view->_player_video.first->reset_metadata (_film, _player->video_container_size(), _film->frame_size())) {
426 _video_view->display_player_video ();
431 FilmViewer::seek (shared_ptr<Content> content, ContentTime t, bool accurate)
433 optional<DCPTime> dt = _player->content_time_to_dcp (content, t);
435 seek (*dt, accurate);
440 FilmViewer::set_coalesce_player_changes (bool c)
442 _coalesce_player_changes = c;
445 BOOST_FOREACH (int i, _pending_player_changes) {
446 player_change (CHANGE_TYPE_DONE, i, false);
448 _pending_player_changes.clear ();
453 FilmViewer::seek (DCPTime t, bool accurate)
459 if (t < DCPTime ()) {
463 if (t >= _film->length ()) {
464 t = _film->length ();
469 _closed_captions_dialog->clear ();
470 _butler->seek (t, accurate);
473 request_idle_display_next_frame ();
475 while (!_video_view->display_next_frame(false)) {}
482 FilmViewer::config_changed (Config::Property p)
484 #ifdef DCPOMATIC_VARIANT_SWAROOP
485 if (p == Config::PLAYER_BACKGROUND_IMAGE) {
491 if (p == Config::AUDIO_MAPPING) {
496 if (p != Config::SOUND && p != Config::SOUND_OUTPUT) {
500 if (_audio.isStreamOpen ()) {
501 _audio.closeStream ();
504 if (Config::instance()->sound() && _audio.getDeviceCount() > 0) {
506 if (Config::instance()->sound_output()) {
507 while (st < _audio.getDeviceCount()) {
508 if (_audio.getDeviceInfo(st).name == Config::instance()->sound_output().get()) {
513 if (st == _audio.getDeviceCount()) {
514 st = _audio.getDefaultOutputDevice();
517 st = _audio.getDefaultOutputDevice();
520 _audio_channels = _audio.getDeviceInfo(st).outputChannels;
522 RtAudio::StreamParameters sp;
524 sp.nChannels = _audio_channels;
527 _audio.openStream (&sp, 0, RTAUDIO_FLOAT32, 48000, &_audio_block_size, &rtaudio_callback, this);
528 #ifdef DCPOMATIC_USE_RTERROR
529 } catch (RtError& e) {
531 } catch (RtAudioError& e) {
535 _("Could not set up audio output. There will be no audio during the preview."), std_to_wx(e.what())
547 FilmViewer::uncorrected_time () const
549 if (_audio.isStreamRunning ()) {
550 return DCPTime::from_seconds (const_cast<RtAudio*>(&_audio)->getStreamTime());
553 return _video_view->position();
557 FilmViewer::audio_time () const
559 if (!_audio.isStreamRunning()) {
560 return optional<DCPTime>();
563 return DCPTime::from_seconds (const_cast<RtAudio*>(&_audio)->getStreamTime ()) -
564 DCPTime::from_frames (average_latency(), _film->audio_frame_rate());
568 FilmViewer::time () const
570 return audio_time().get_value_or(_video_view->position());
574 FilmViewer::audio_callback (void* out_p, unsigned int frames)
577 optional<DCPTime> t = _butler->get_audio (reinterpret_cast<float*> (out_p), frames);
578 if (!t || DCPTime(uncorrected_time() - *t) < one_video_frame()) {
579 /* There was an underrun or this audio is on time; carry on */
582 /* The audio we just got was (very) late; drop it and get some more. */
585 boost::mutex::scoped_lock lm (_latency_history_mutex, boost::try_to_lock);
587 _latency_history.push_back (_audio.getStreamLatency ());
588 if (_latency_history.size() > static_cast<size_t> (_latency_history_count)) {
589 _latency_history.pop_front ();
597 FilmViewer::average_latency () const
599 boost::mutex::scoped_lock lm (_latency_history_mutex);
600 if (_latency_history.empty()) {
605 BOOST_FOREACH (Frame i, _latency_history) {
609 return total / _latency_history.size();
613 FilmViewer::set_dcp_decode_reduction (optional<int> reduction)
615 _dcp_decode_reduction = reduction;
617 _player->set_dcp_decode_reduction (reduction);
622 FilmViewer::dcp_decode_reduction () const
624 return _dcp_decode_reduction;
628 FilmViewer::one_video_frame () const
630 return DCPTime::from_frames (1, _film ? _film->video_frame_rate() : 24);
633 /** Open a dialog box showing our film's closed captions */
635 FilmViewer::show_closed_captions ()
637 _closed_captions_dialog->Show();
641 FilmViewer::seek_by (DCPTime by, bool accurate)
643 seek (_video_view->position() + by, accurate);
647 FilmViewer::set_pad_black (bool p)
652 /* May be called from a non-UI thread */
654 FilmViewer::emit_finished ()
656 emit (boost::bind(boost::ref(Finished)));