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>
32 using std::shared_ptr;
33 using boost::optional;
36 static constexpr int TOO_MANY_DROPPED_FRAMES = 20;
37 static constexpr int TOO_MANY_DROPPED_PERIOD = 5.0;
40 VideoView::VideoView (FilmViewer* viewer)
42 , _state_timer ("viewer")
51 boost::mutex::scoped_lock lm (_mutex);
52 _player_video.first.reset ();
53 _player_video.second = dcpomatic::DCPTime ();
57 /** Could be called from any thread.
58 * @param non_blocking true to return false quickly if no video is available quickly.
59 * @return FAIL if there's no frame, AGAIN if the method should be called again, or SUCCESS
60 * if there is a frame.
62 VideoView::NextFrameResult
63 VideoView::get_next_frame (bool non_blocking)
65 if (length() == dcpomatic::DCPTime()) {
69 auto butler = _viewer->butler ();
75 boost::mutex::scoped_lock lm (_mutex);
79 auto pv = butler->get_video (!non_blocking, &e);
80 if (e.code == Butler::Error::DIED) {
81 LOG_ERROR ("Butler died with %1", e.summary());
84 return e.code == Butler::Error::AGAIN ? AGAIN : FAIL;
88 _player_video.first &&
90 _eyes != _player_video.first->eyes() &&
91 _player_video.first->eyes() != Eyes::BOTH
94 if (_player_video.first && _player_video.first->error()) {
103 VideoView::one_video_frame () const
105 return dcpomatic::DCPTime::from_frames (1, video_frame_rate());
109 /** @return Time in ms until the next frame is due, or empty if nothing is due */
111 VideoView::time_until_next_frame () const
113 if (length() == dcpomatic::DCPTime()) {
114 /* There's no content, so this doesn't matter */
115 return optional<int>();
118 auto const next = position() + one_video_frame();
119 auto const time = _viewer->audio_time().get_value_or(position());
123 return (next.seconds() - time.seconds()) * 1000;
130 boost::mutex::scoped_lock lm (_mutex);
133 gettimeofday(&_dropped_check_period_start, nullptr);
138 VideoView::reset_metadata (shared_ptr<const Film> film, dcp::Size player_video_container_size)
140 auto pv = player_video ();
145 if (!pv.first->reset_metadata(film, player_video_container_size)) {
155 VideoView::add_dropped ()
157 bool too_many = false;
160 boost::mutex::scoped_lock lm (_mutex);
162 if (_dropped > TOO_MANY_DROPPED_FRAMES) {
164 gettimeofday (&now, nullptr);
165 double const elapsed = seconds(now) - seconds(_dropped_check_period_start);
166 too_many = elapsed < TOO_MANY_DROPPED_PERIOD;
168 _dropped_check_period_start = now;
173 emit (boost::bind(boost::ref(TooManyDropped)));