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)
89 , _closed_captions_dialog (new ClosedCaptionsDialog(p, this))
90 , _outline_content (false)
93 #ifdef DCPOMATIC_VARIANT_SWAROOP
94 , _background_image (false)
99 switch (Config::instance()->video_view_type()) {
100 case Config::VIDEO_VIEW_OPENGL:
101 _video_view = new GLVideoView (this, p);
103 case Config::VIDEO_VIEW_SIMPLE:
104 _video_view = new SimpleVideoView (this, p);
108 _video_view->Sized.connect (boost::bind(&FilmViewer::video_view_sized, this));
110 set_film (shared_ptr<Film> ());
112 _config_changed_connection = Config::instance()->Changed.connect (bind (&FilmViewer::config_changed, this, _1));
113 config_changed (Config::SOUND_OUTPUT);
116 FilmViewer::~FilmViewer ()
121 /** Ask for ::get() to be called next time we are idle */
123 FilmViewer::request_idle_display_next_frame ()
130 DCPOMATIC_ASSERT (signal_manager);
131 signal_manager->when_idle (boost::bind(&FilmViewer::idle_handler, this));
135 FilmViewer::idle_handler ()
141 if (_video_view->display_next_frame(true)) {
144 /* get() could not complete quickly so we'll try again later */
145 signal_manager->when_idle (boost::bind(&FilmViewer::idle_handler, this));
150 FilmViewer::set_film (shared_ptr<Film> film)
158 _video_view->clear ();
159 _closed_captions_dialog->clear ();
169 _player.reset (new Player (_film, _film->playlist ()));
170 _player->set_fast ();
171 if (_dcp_decode_reduction) {
172 _player->set_dcp_decode_reduction (_dcp_decode_reduction);
174 } catch (bad_alloc &) {
175 error_dialog (_video_view->get(), _("There is not enough free memory to do that."));
180 _player->set_always_burn_open_subtitles ();
181 _player->set_play_referenced ();
183 _film->Change.connect (boost::bind (&FilmViewer::film_change, this, _1, _2));
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 /* Keep about 1 second's worth of history samples */
188 _latency_history_count = _film->audio_frame_rate() / _audio_block_size;
197 FilmViewer::recreate_butler ()
210 Config::instance()->audio_mapping(_audio_channels),
212 bind(&PlayerVideo::force, _1, AV_PIX_FMT_RGB24),
218 if (!Config::instance()->sound() && !_audio.isStreamOpen()) {
219 _butler->disable_audio ();
222 _closed_captions_dialog->set_film_and_butler (_film, _butler);
228 FilmViewer::refresh_view ()
230 _video_view->update ();
234 FilmViewer::set_outline_content (bool o)
236 _outline_content = o;
241 FilmViewer::set_eyes (Eyes e)
248 FilmViewer::video_view_sized ()
251 if (!quick_refresh()) {
257 FilmViewer::calculate_sizes ()
259 if (!_film || !_player) {
263 Ratio const * container = _film->container ();
265 float const view_ratio = float(_video_view->get()->GetSize().x) / _video_view->get()->GetSize().y;
266 float const film_ratio = container ? container->ratio () : 1.78;
268 if (view_ratio < film_ratio) {
269 /* panel is less widscreen than the film; clamp width */
270 _out_size.width = _video_view->get()->GetSize().x;
271 _out_size.height = lrintf (_out_size.width / film_ratio);
273 /* panel is more widescreen than the film; clamp height */
274 _out_size.height = _video_view->get()->GetSize().y;
275 _out_size.width = lrintf (_out_size.height * film_ratio);
278 /* Catch silly values */
279 _out_size.width = max (64, _out_size.width);
280 _out_size.height = max (64, _out_size.height);
282 _player->set_video_container_size (_out_size);
286 FilmViewer::suspend ()
289 if (_audio.isStreamRunning()) {
290 _audio.abortStream();
295 FilmViewer::resume ()
298 if (_playing && !_suspended) {
299 if (_audio.isStreamOpen()) {
300 _audio.setStreamTime (_video_view->position().seconds());
301 _audio.startStream ();
303 _video_view->start ();
314 optional<bool> v = PlaybackPermitted ();
316 /* Computer says no */
320 if (_audio.isStreamOpen()) {
321 _audio.setStreamTime (_video_view->position().seconds());
322 _audio.startStream ();
326 _video_view->start ();
327 Started (position());
333 if (_audio.isStreamRunning()) {
334 /* stop stream and discard any remaining queued samples */
335 _audio.abortStream ();
343 _video_view->stop ();
344 Stopped (position());
349 FilmViewer::player_change (ChangeType type, int property, bool frequent)
351 if (type != CHANGE_TYPE_DONE || frequent) {
355 if (_coalesce_player_changes) {
356 _pending_player_changes.push_back (property);
361 bool refreshed = false;
363 property == VideoContentProperty::CROP ||
364 property == VideoContentProperty::SCALE ||
365 property == VideoContentProperty::FADE_IN ||
366 property == VideoContentProperty::FADE_OUT ||
367 property == VideoContentProperty::COLOUR_CONVERSION ||
368 property == PlayerProperty::VIDEO_CONTAINER_SIZE ||
369 property == PlayerProperty::FILM_CONTAINER
371 refreshed = quick_refresh ();
380 FilmViewer::film_change (ChangeType type, Film::Property p)
382 if (type != CHANGE_TYPE_DONE) {
386 if (p == Film::AUDIO_CHANNELS) {
388 } else if (p == Film::VIDEO_FRAME_RATE) {
389 _video_view->set_video_frame_rate (_film->video_frame_rate());
394 FilmViewer::film_length_change ()
396 _video_view->set_length (_film->length());
399 /** Re-get the current frame slowly by seeking */
401 FilmViewer::slow_refresh ()
403 seek (_video_view->position(), true);
406 /** Try to re-get the current frame quickly by resetting the metadata
407 * in the PlayerVideo that we used last time.
408 * @return true if this was possible, false if not.
411 FilmViewer::quick_refresh ()
413 if (!_video_view->_player_video.first) {
417 if (!_video_view->_player_video.first->reset_metadata (_film, _player->video_container_size(), _film->frame_size())) {
421 _video_view->display_player_video ();
426 FilmViewer::seek (shared_ptr<Content> content, ContentTime t, bool accurate)
428 optional<DCPTime> dt = _player->content_time_to_dcp (content, t);
430 seek (*dt, accurate);
435 FilmViewer::set_coalesce_player_changes (bool c)
437 _coalesce_player_changes = c;
440 BOOST_FOREACH (int i, _pending_player_changes) {
441 player_change (CHANGE_TYPE_DONE, i, false);
443 _pending_player_changes.clear ();
448 FilmViewer::seek (DCPTime t, bool accurate)
454 if (t < DCPTime ()) {
458 if (t >= _film->length ()) {
459 t = _film->length ();
464 _closed_captions_dialog->clear ();
465 _butler->seek (t, accurate);
468 request_idle_display_next_frame ();
470 while (!_video_view->display_next_frame(false)) {}
477 FilmViewer::config_changed (Config::Property p)
479 #ifdef DCPOMATIC_VARIANT_SWAROOP
480 if (p == Config::PLAYER_BACKGROUND_IMAGE) {
486 if (p == Config::AUDIO_MAPPING) {
491 if (p != Config::SOUND && p != Config::SOUND_OUTPUT) {
495 if (_audio.isStreamOpen ()) {
496 _audio.closeStream ();
499 if (Config::instance()->sound() && _audio.getDeviceCount() > 0) {
501 if (Config::instance()->sound_output()) {
502 while (st < _audio.getDeviceCount()) {
503 if (_audio.getDeviceInfo(st).name == Config::instance()->sound_output().get()) {
508 if (st == _audio.getDeviceCount()) {
509 st = _audio.getDefaultOutputDevice();
512 st = _audio.getDefaultOutputDevice();
515 _audio_channels = _audio.getDeviceInfo(st).outputChannels;
517 RtAudio::StreamParameters sp;
519 sp.nChannels = _audio_channels;
522 _audio.openStream (&sp, 0, RTAUDIO_FLOAT32, 48000, &_audio_block_size, &rtaudio_callback, this);
523 #ifdef DCPOMATIC_USE_RTERROR
524 } catch (RtError& e) {
526 } catch (RtAudioError& e) {
530 _("Could not set up audio output. There will be no audio during the preview."), std_to_wx(e.what())
542 FilmViewer::uncorrected_time () const
544 if (_audio.isStreamRunning ()) {
545 return DCPTime::from_seconds (const_cast<RtAudio*>(&_audio)->getStreamTime());
548 return _video_view->position();
552 FilmViewer::audio_time () const
554 if (!_audio.isStreamRunning()) {
555 return optional<DCPTime>();
558 return DCPTime::from_seconds (const_cast<RtAudio*>(&_audio)->getStreamTime ()) -
559 DCPTime::from_frames (average_latency(), _film->audio_frame_rate());
563 FilmViewer::time () const
565 return audio_time().get_value_or(_video_view->position());
569 FilmViewer::audio_callback (void* out_p, unsigned int frames)
572 optional<DCPTime> t = _butler->get_audio (reinterpret_cast<float*> (out_p), frames);
573 if (!t || DCPTime(uncorrected_time() - *t) < one_video_frame()) {
574 /* There was an underrun or this audio is on time; carry on */
577 /* The audio we just got was (very) late; drop it and get some more. */
580 boost::mutex::scoped_lock lm (_latency_history_mutex, boost::try_to_lock);
582 _latency_history.push_back (_audio.getStreamLatency ());
583 if (_latency_history.size() > static_cast<size_t> (_latency_history_count)) {
584 _latency_history.pop_front ();
592 FilmViewer::average_latency () const
594 boost::mutex::scoped_lock lm (_latency_history_mutex);
595 if (_latency_history.empty()) {
600 BOOST_FOREACH (Frame i, _latency_history) {
604 return total / _latency_history.size();
608 FilmViewer::set_dcp_decode_reduction (optional<int> reduction)
610 _dcp_decode_reduction = reduction;
612 _player->set_dcp_decode_reduction (reduction);
617 FilmViewer::dcp_decode_reduction () const
619 return _dcp_decode_reduction;
623 FilmViewer::one_video_frame () const
625 return DCPTime::from_frames (1, _film ? _film->video_frame_rate() : 24);
628 /** Open a dialog box showing our film's closed captions */
630 FilmViewer::show_closed_captions ()
632 _closed_captions_dialog->Show();
636 FilmViewer::seek_by (DCPTime by, bool accurate)
638 seek (_video_view->position() + by, accurate);
642 FilmViewer::set_pad_black (bool p)
647 /* May be called from a non-UI thread */
649 FilmViewer::emit_finished ()
651 emit (boost::bind(boost::ref(Finished)));
655 FilmViewer::dropped () const
657 return _video_view->dropped ();