2 Copyright (C) 2019-2021 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/>.
22 #include "video_view.h"
24 #include "film_viewer.h"
25 #include "lib/butler.h"
26 #include "lib/dcpomatic_log.h"
27 #include <boost/optional.hpp>
31 using std::shared_ptr;
32 using boost::optional;
35 static constexpr int TOO_MANY_DROPPED_FRAMES = 20;
36 static constexpr int TOO_MANY_DROPPED_PERIOD = 5.0;
39 VideoView::VideoView (FilmViewer* viewer)
41 , _state_timer ("viewer")
50 boost::mutex::scoped_lock lm (_mutex);
51 _player_video.first.reset ();
52 _player_video.second = dcpomatic::DCPTime ();
56 /** Could be called from any thread.
57 * @param non_blocking true to return false quickly if no video is available quickly.
58 * @return FAIL if there's no frame, AGAIN if the method should be called again, or SUCCESS
59 * if there is a frame.
61 VideoView::NextFrameResult
62 VideoView::get_next_frame (bool non_blocking)
64 if (length() == dcpomatic::DCPTime()) {
68 auto butler = _viewer->butler ();
74 boost::mutex::scoped_lock lm (_mutex);
78 auto pv = butler->get_video (non_blocking ? Butler::Behaviour::NON_BLOCKING : Butler::Behaviour::BLOCKING, &e);
79 if (e.code == Butler::Error::Code::DIED) {
80 LOG_ERROR ("Butler died with %1", e.summary());
83 return e.code == Butler::Error::Code::AGAIN ? AGAIN : FAIL;
87 _player_video.first &&
89 _eyes != _player_video.first->eyes() &&
90 _player_video.first->eyes() != Eyes::BOTH
93 if (_player_video.first && _player_video.first->error()) {
102 VideoView::one_video_frame () const
104 return dcpomatic::DCPTime::from_frames (1, video_frame_rate());
108 /** @return Time in ms until the next frame is due, or empty if nothing is due */
110 VideoView::time_until_next_frame () const
112 if (length() == dcpomatic::DCPTime()) {
113 /* There's no content, so this doesn't matter */
117 auto const next = position() + one_video_frame();
118 auto const time = _viewer->audio_time().get_value_or(position());
122 return (next.seconds() - time.seconds()) * 1000;
129 boost::mutex::scoped_lock lm (_mutex);
132 gettimeofday(&_dropped_check_period_start, nullptr);
137 VideoView::reset_metadata (shared_ptr<const Film> film, dcp::Size player_video_container_size)
139 auto pv = player_video ();
144 if (!pv.first->reset_metadata(film, player_video_container_size)) {
154 VideoView::add_dropped ()
156 bool too_many = false;
159 boost::mutex::scoped_lock lm (_mutex);
161 if (_dropped > TOO_MANY_DROPPED_FRAMES) {
163 gettimeofday (&now, nullptr);
164 double const elapsed = seconds(now) - seconds(_dropped_check_period_start);
165 too_many = elapsed < TOO_MANY_DROPPED_PERIOD;
167 _dropped_check_period_start = now;
172 emit (boost::bind(boost::ref(TooManyDropped)));
178 VideoView::pad_colour () const
180 if (_viewer->pad_black()) {
181 return wxColour(0, 0, 0);
182 } else if (gui_is_dark()) {
183 return wxColour(50, 50, 50);
185 return wxColour(240, 240, 240);