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)
92 #ifdef DCPOMATIC_VARIANT_SWAROOP
93 , _background_image (false)
97 switch (Config::instance()->video_view_type()) {
98 case Config::VIDEO_VIEW_OPENGL:
99 _video_view = new GLVideoView (this, p);
101 case Config::VIDEO_VIEW_SIMPLE:
102 _video_view = new SimpleVideoView (this, p);
106 _video_view->Sized.connect (boost::bind(&FilmViewer::video_view_sized, this));
108 set_film (shared_ptr<Film> ());
110 _config_changed_connection = Config::instance()->Changed.connect (bind (&FilmViewer::config_changed, this, _1));
111 config_changed (Config::SOUND_OUTPUT);
114 FilmViewer::~FilmViewer ()
119 /** Ask for ::get() to be called next time we are idle */
121 FilmViewer::request_idle_display_next_frame ()
128 DCPOMATIC_ASSERT (signal_manager);
129 signal_manager->when_idle (boost::bind(&FilmViewer::idle_handler, this));
133 FilmViewer::idle_handler ()
139 if (_video_view->display_next_frame(true)) {
142 /* get() could not complete quickly so we'll try again later */
143 signal_manager->when_idle (boost::bind(&FilmViewer::idle_handler, this));
148 FilmViewer::set_film (shared_ptr<Film> film)
156 _video_view->clear ();
157 _closed_captions_dialog->clear ();
162 _video_view->update ();
167 _player.reset (new Player (_film, _film->playlist ()));
168 _player->set_fast ();
169 if (_dcp_decode_reduction) {
170 _player->set_dcp_decode_reduction (_dcp_decode_reduction);
172 } catch (bad_alloc &) {
173 error_dialog (_video_view->get(), _("There is not enough free memory to do that."));
178 _player->set_always_burn_open_subtitles ();
179 _player->set_play_referenced ();
181 _film->Change.connect (boost::bind (&FilmViewer::film_change, this, _1, _2));
182 _film->LengthChange.connect (boost::bind(&FilmViewer::film_length_change, this));
183 _player->Change.connect (boost::bind (&FilmViewer::player_change, this, _1, _2, _3));
185 film_change (CHANGE_TYPE_DONE, Film::VIDEO_FRAME_RATE);
186 film_change (CHANGE_TYPE_DONE, Film::THREE_D);
187 film_length_change ();
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::set_outline_content (bool o)
232 _outline_content = o;
233 _video_view->update ();
237 FilmViewer::set_eyes (Eyes e)
239 _video_view->set_eyes (e);
244 FilmViewer::video_view_sized ()
247 if (!quick_refresh()) {
253 FilmViewer::calculate_sizes ()
255 if (!_film || !_player) {
259 Ratio const * container = _film->container ();
261 float const view_ratio = float(_video_view->get()->GetSize().x) / _video_view->get()->GetSize().y;
262 float const film_ratio = container ? container->ratio () : 1.78;
264 if (view_ratio < film_ratio) {
265 /* panel is less widscreen than the film; clamp width */
266 _out_size.width = _video_view->get()->GetSize().x;
267 _out_size.height = lrintf (_out_size.width / film_ratio);
269 /* panel is more widescreen than the film; clamp height */
270 _out_size.height = _video_view->get()->GetSize().y;
271 _out_size.width = lrintf (_out_size.height * film_ratio);
274 /* Catch silly values */
275 _out_size.width = max (64, _out_size.width);
276 _out_size.height = max (64, _out_size.height);
278 _player->set_video_container_size (_out_size);
282 FilmViewer::suspend ()
285 if (_audio.isStreamRunning()) {
286 _audio.abortStream();
291 FilmViewer::resume ()
293 DCPOMATIC_ASSERT (_suspended > 0);
295 if (_playing && !_suspended) {
296 if (_audio.isStreamOpen()) {
297 _audio.setStreamTime (_video_view->position().seconds());
298 _audio.startStream ();
300 _video_view->start ();
311 optional<bool> v = PlaybackPermitted ();
313 /* Computer says no */
317 /* We are about to set up the audio stream from the position of the video view.
318 If there is `lazy' seek in progress we need to wait for it to go through so that
319 _video_view->position() gives us a sensible answer.
325 /* Take the video view's idea of position as our `playhead' and start the
326 audio stream (which is the timing reference) there.
328 if (_audio.isStreamOpen()) {
329 _audio.setStreamTime (_video_view->position().seconds());
330 _audio.startStream ();
334 _video_view->start ();
335 Started (position());
341 if (_audio.isStreamRunning()) {
342 /* stop stream and discard any remaining queued samples */
343 _audio.abortStream ();
351 _video_view->stop ();
352 Stopped (position());
354 _video_view->rethrow ();
359 FilmViewer::player_change (ChangeType type, int property, bool frequent)
361 if (type != CHANGE_TYPE_DONE || frequent) {
365 if (_coalesce_player_changes) {
366 _pending_player_changes.push_back (property);
371 bool refreshed = false;
373 property == VideoContentProperty::CROP ||
374 property == VideoContentProperty::SCALE ||
375 property == VideoContentProperty::FADE_IN ||
376 property == VideoContentProperty::FADE_OUT ||
377 property == VideoContentProperty::COLOUR_CONVERSION ||
378 property == PlayerProperty::VIDEO_CONTAINER_SIZE ||
379 property == PlayerProperty::FILM_CONTAINER
381 refreshed = quick_refresh ();
390 FilmViewer::film_change (ChangeType type, Film::Property p)
392 if (type != CHANGE_TYPE_DONE) {
396 if (p == Film::AUDIO_CHANNELS) {
398 } else if (p == Film::VIDEO_FRAME_RATE) {
399 _video_view->set_video_frame_rate (_film->video_frame_rate());
400 } else if (p == Film::THREE_D) {
401 _video_view->set_three_d (_film->three_d());
406 FilmViewer::film_length_change ()
408 _video_view->set_length (_film->length());
411 /** Re-get the current frame slowly by seeking */
413 FilmViewer::slow_refresh ()
415 seek (_video_view->position(), true);
418 /** Try to re-get the current frame quickly by resetting the metadata
419 * in the PlayerVideo that we used last time.
420 * @return true if this was possible, false if not.
423 FilmViewer::quick_refresh ()
425 if (!_video_view || !_film) {
428 return _video_view->refresh_metadata (_film, _player->video_container_size(), _film->frame_size());
432 FilmViewer::seek (shared_ptr<Content> content, ContentTime t, bool accurate)
434 optional<DCPTime> dt = _player->content_time_to_dcp (content, t);
436 seek (*dt, accurate);
441 FilmViewer::set_coalesce_player_changes (bool c)
443 _coalesce_player_changes = c;
446 BOOST_FOREACH (int i, _pending_player_changes) {
447 player_change (CHANGE_TYPE_DONE, i, false);
449 _pending_player_changes.clear ();
454 FilmViewer::seek (DCPTime t, bool accurate)
460 if (t < DCPTime ()) {
464 if (t >= _film->length ()) {
465 t = _film->length ();
470 _closed_captions_dialog->clear ();
471 _butler->seek (t, accurate);
474 /* We're not playing, so let the GUI thread get on and
475 come back later to get the next frame after the seek.
477 request_idle_display_next_frame ();
479 /* We're going to start playing again straight away
480 so wait for the seek to finish.
482 while (!_video_view->display_next_frame(false)) {}
489 FilmViewer::config_changed (Config::Property p)
491 #ifdef DCPOMATIC_VARIANT_SWAROOP
492 if (p == Config::PLAYER_BACKGROUND_IMAGE) {
493 _video_view->update ();
498 if (p == Config::AUDIO_MAPPING) {
503 if (p != Config::SOUND && p != Config::SOUND_OUTPUT) {
507 if (_audio.isStreamOpen ()) {
508 _audio.closeStream ();
511 if (Config::instance()->sound() && _audio.getDeviceCount() > 0) {
513 if (Config::instance()->sound_output()) {
514 while (st < _audio.getDeviceCount()) {
515 if (_audio.getDeviceInfo(st).name == Config::instance()->sound_output().get()) {
520 if (st == _audio.getDeviceCount()) {
521 st = _audio.getDefaultOutputDevice();
524 st = _audio.getDefaultOutputDevice();
527 _audio_channels = _audio.getDeviceInfo(st).outputChannels;
529 RtAudio::StreamParameters sp;
531 sp.nChannels = _audio_channels;
534 _audio.openStream (&sp, 0, RTAUDIO_FLOAT32, 48000, &_audio_block_size, &rtaudio_callback, this);
535 #ifdef DCPOMATIC_USE_RTERROR
536 } catch (RtError& e) {
538 } catch (RtAudioError& e) {
542 _("Could not set up audio output. There will be no audio during the preview."), std_to_wx(e.what())
554 FilmViewer::uncorrected_time () const
556 if (_audio.isStreamRunning ()) {
557 return DCPTime::from_seconds (const_cast<RtAudio*>(&_audio)->getStreamTime());
560 return _video_view->position();
564 FilmViewer::audio_time () const
566 if (!_audio.isStreamRunning()) {
567 return optional<DCPTime>();
570 return DCPTime::from_seconds (const_cast<RtAudio*>(&_audio)->getStreamTime ()) -
571 DCPTime::from_frames (average_latency(), _film->audio_frame_rate());
575 FilmViewer::time () const
577 return audio_time().get_value_or(_video_view->position());
581 FilmViewer::audio_callback (void* out_p, unsigned int frames)
584 optional<DCPTime> t = _butler->get_audio (reinterpret_cast<float*> (out_p), frames);
585 if (!t || DCPTime(uncorrected_time() - *t) < one_video_frame()) {
586 /* There was an underrun or this audio is on time; carry on */
589 /* The audio we just got was (very) late; drop it and get some more. */
592 boost::mutex::scoped_lock lm (_latency_history_mutex, boost::try_to_lock);
594 _latency_history.push_back (_audio.getStreamLatency ());
595 if (_latency_history.size() > static_cast<size_t> (_latency_history_count)) {
596 _latency_history.pop_front ();
604 FilmViewer::average_latency () const
606 boost::mutex::scoped_lock lm (_latency_history_mutex);
607 if (_latency_history.empty()) {
612 BOOST_FOREACH (Frame i, _latency_history) {
616 return total / _latency_history.size();
620 FilmViewer::set_dcp_decode_reduction (optional<int> reduction)
622 _dcp_decode_reduction = reduction;
624 _player->set_dcp_decode_reduction (reduction);
629 FilmViewer::dcp_decode_reduction () const
631 return _dcp_decode_reduction;
635 FilmViewer::one_video_frame () const
637 return DCPTime::from_frames (1, _film ? _film->video_frame_rate() : 24);
640 /** Open a dialog box showing our film's closed captions */
642 FilmViewer::show_closed_captions ()
644 _closed_captions_dialog->Show();
648 FilmViewer::seek_by (DCPTime by, bool accurate)
650 seek (_video_view->position() + by, accurate);
654 FilmViewer::set_pad_black (bool p)
659 /** Called when a player has finished the current film.
660 * May be called from a non-UI thread.
663 FilmViewer::finished ()
665 emit (boost::bind(&FilmViewer::ui_finished, this));
668 /** Called by finished() in the UI thread */
670 FilmViewer::ui_finished ()
677 FilmViewer::dropped () const
679 return _video_view->dropped ();
683 FilmViewer::gets () const
685 return _video_view->gets ();