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_get ()
132 DCPOMATIC_ASSERT (signal_manager);
133 signal_manager->when_idle (boost::bind(&FilmViewer::idle_handler, this));
137 FilmViewer::idle_handler ()
143 if (_video_view->get(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)
159 _video_position = DCPTime ();
160 _video_view->clear ();
162 _video_view->set_image (shared_ptr<Image>());
163 _closed_captions_dialog->clear ();
173 _player.reset (new Player (_film, _film->playlist ()));
174 _player->set_fast ();
175 if (_dcp_decode_reduction) {
176 _player->set_dcp_decode_reduction (_dcp_decode_reduction);
178 } catch (bad_alloc &) {
179 error_dialog (_video_view->get(), _("There is not enough free memory to do that."));
184 _player->set_always_burn_open_subtitles ();
185 _player->set_play_referenced ();
187 _film->Change.connect (boost::bind (&FilmViewer::film_change, this, _1, _2));
188 _player->Change.connect (boost::bind (&FilmViewer::player_change, this, _1, _2, _3));
190 /* Keep about 1 second's worth of history samples */
191 _latency_history_count = _film->audio_frame_rate() / _audio_block_size;
200 FilmViewer::recreate_butler ()
213 Config::instance()->audio_mapping(_audio_channels),
215 bind(&PlayerVideo::force, _1, AV_PIX_FMT_RGB24),
221 if (!Config::instance()->sound() && !_audio.isStreamOpen()) {
222 _butler->disable_audio ();
225 _closed_captions_dialog->set_film_and_butler (_film, _butler);
231 FilmViewer::refresh_view ()
233 _state_timer.set ("update-view");
234 _video_view->update ();
235 _state_timer.unset ();
239 FilmViewer::set_outline_content (bool o)
241 _outline_content = o;
246 FilmViewer::set_eyes (Eyes e)
253 FilmViewer::video_view_sized ()
256 if (!quick_refresh()) {
263 FilmViewer::calculate_sizes ()
265 if (!_film || !_player) {
269 Ratio const * container = _film->container ();
271 float const view_ratio = float(_video_view->get()->GetSize().x) / _video_view->get()->GetSize().y;
272 float const film_ratio = container ? container->ratio () : 1.78;
274 if (view_ratio < film_ratio) {
275 /* panel is less widscreen than the film; clamp width */
276 _out_size.width = _video_view->get()->GetSize().x;
277 _out_size.height = lrintf (_out_size.width / film_ratio);
279 /* panel is more widescreen than the film; clamp height */
280 _out_size.height = _video_view->get()->GetSize().y;
281 _out_size.width = lrintf (_out_size.height * film_ratio);
284 /* Catch silly values */
285 _out_size.width = max (64, _out_size.width);
286 _out_size.height = max (64, _out_size.height);
288 _player->set_video_container_size (_out_size);
292 FilmViewer::suspend ()
295 if (_audio.isStreamRunning()) {
296 _audio.abortStream();
301 FilmViewer::resume ()
304 if (_playing && !_suspended) {
305 if (_audio.isStreamOpen()) {
306 _audio.setStreamTime (_video_position.seconds());
307 _audio.startStream ();
320 optional<bool> v = PlaybackPermitted ();
322 /* Computer says no */
326 if (_audio.isStreamOpen()) {
327 _audio.setStreamTime (_video_position.seconds());
328 _audio.startStream ();
333 _video_view->start ();
334 Started (position());
340 if (_audio.isStreamRunning()) {
341 /* stop stream and discard any remaining queued samples */
342 _audio.abortStream ();
350 Stopped (position());
355 FilmViewer::player_change (ChangeType type, int property, bool frequent)
357 if (type != CHANGE_TYPE_DONE || frequent) {
361 if (_coalesce_player_changes) {
362 _pending_player_changes.push_back (property);
367 bool refreshed = false;
369 property == VideoContentProperty::CROP ||
370 property == VideoContentProperty::SCALE ||
371 property == VideoContentProperty::FADE_IN ||
372 property == VideoContentProperty::FADE_OUT ||
373 property == VideoContentProperty::COLOUR_CONVERSION ||
374 property == PlayerProperty::VIDEO_CONTAINER_SIZE ||
375 property == PlayerProperty::FILM_CONTAINER
377 refreshed = quick_refresh ();
387 FilmViewer::film_change (ChangeType type, Film::Property p)
389 if (type == CHANGE_TYPE_DONE && p == Film::AUDIO_CHANNELS) {
394 /** Re-get the current frame slowly by seeking */
396 FilmViewer::slow_refresh ()
398 seek (_video_position, true);
401 /** Try to re-get the current frame quickly by resetting the metadata
402 * in the PlayerVideo that we used last time.
403 * @return true if this was possible, false if not.
406 FilmViewer::quick_refresh ()
408 if (!_video_view->_player_video.first) {
412 if (!_video_view->_player_video.first->reset_metadata (_film, _player->video_container_size(), _film->frame_size())) {
416 _video_view->display_player_video ();
421 FilmViewer::seek (shared_ptr<Content> content, ContentTime t, bool accurate)
423 optional<DCPTime> dt = _player->content_time_to_dcp (content, t);
425 seek (*dt, accurate);
430 FilmViewer::set_coalesce_player_changes (bool c)
432 _coalesce_player_changes = c;
435 BOOST_FOREACH (int i, _pending_player_changes) {
436 player_change (CHANGE_TYPE_DONE, i, false);
438 _pending_player_changes.clear ();
443 FilmViewer::seek (DCPTime t, bool accurate)
449 if (t < DCPTime ()) {
453 if (t >= _film->length ()) {
454 t = _film->length ();
459 _closed_captions_dialog->clear ();
460 _butler->seek (t, accurate);
465 /* Make sure we get a frame so that _video_position is set up before we resume */
466 while (!get(true)) {}
473 FilmViewer::config_changed (Config::Property p)
475 #ifdef DCPOMATIC_VARIANT_SWAROOP
476 if (p == Config::PLAYER_BACKGROUND_IMAGE) {
482 if (p == Config::AUDIO_MAPPING) {
487 if (p != Config::SOUND && p != Config::SOUND_OUTPUT) {
491 if (_audio.isStreamOpen ()) {
492 _audio.closeStream ();
495 if (Config::instance()->sound() && _audio.getDeviceCount() > 0) {
497 if (Config::instance()->sound_output()) {
498 while (st < _audio.getDeviceCount()) {
499 if (_audio.getDeviceInfo(st).name == Config::instance()->sound_output().get()) {
504 if (st == _audio.getDeviceCount()) {
505 st = _audio.getDefaultOutputDevice();
508 st = _audio.getDefaultOutputDevice();
511 _audio_channels = _audio.getDeviceInfo(st).outputChannels;
513 RtAudio::StreamParameters sp;
515 sp.nChannels = _audio_channels;
518 _audio.openStream (&sp, 0, RTAUDIO_FLOAT32, 48000, &_audio_block_size, &rtaudio_callback, this);
519 #ifdef DCPOMATIC_USE_RTERROR
520 } catch (RtError& e) {
522 } catch (RtAudioError& e) {
526 _("Could not set up audio output. There will be no audio during the preview."), std_to_wx(e.what())
538 FilmViewer::uncorrected_time () const
540 if (_audio.isStreamRunning ()) {
541 return DCPTime::from_seconds (const_cast<RtAudio*>(&_audio)->getStreamTime());
544 return _video_position;
548 FilmViewer::time () const
550 if (_audio.isStreamRunning ()) {
551 return DCPTime::from_seconds (const_cast<RtAudio*>(&_audio)->getStreamTime ()) -
552 DCPTime::from_frames (average_latency(), _film->audio_frame_rate());
555 return _video_position;
559 FilmViewer::audio_callback (void* out_p, unsigned int frames)
562 optional<DCPTime> t = _butler->get_audio (reinterpret_cast<float*> (out_p), frames);
563 if (!t || DCPTime(uncorrected_time() - *t) < one_video_frame()) {
564 /* There was an underrun or this audio is on time; carry on */
567 /* The audio we just got was (very) late; drop it and get some more. */
570 boost::mutex::scoped_lock lm (_latency_history_mutex, boost::try_to_lock);
572 _latency_history.push_back (_audio.getStreamLatency ());
573 if (_latency_history.size() > static_cast<size_t> (_latency_history_count)) {
574 _latency_history.pop_front ();
582 FilmViewer::average_latency () const
584 boost::mutex::scoped_lock lm (_latency_history_mutex);
585 if (_latency_history.empty()) {
590 BOOST_FOREACH (Frame i, _latency_history) {
594 return total / _latency_history.size();
598 FilmViewer::set_dcp_decode_reduction (optional<int> reduction)
600 _dcp_decode_reduction = reduction;
602 _player->set_dcp_decode_reduction (reduction);
607 FilmViewer::dcp_decode_reduction () const
609 return _dcp_decode_reduction;
613 FilmViewer::one_video_frame () const
615 return DCPTime::from_frames (1, _film ? _film->video_frame_rate() : 24);
618 /** Open a dialog box showing our film's closed captions */
620 FilmViewer::show_closed_captions ()
622 _closed_captions_dialog->Show();
626 FilmViewer::seek_by (DCPTime by, bool accurate)
628 seek (_video_position + by, accurate);
632 FilmViewer::set_pad_black (bool p)