68a15e2e0dfc2663d4a6f14d624278a05315378e
[dcpomatic.git] / src / wx / video_view.cc
1 /*
2     Copyright (C) 2019 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
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.
10
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.
15
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/>.
18
19 */
20
21 #include "video_view.h"
22 #include "wx_util.h"
23 #include "film_viewer.h"
24 #include "lib/butler.h"
25 #include "lib/dcpomatic_log.h"
26 #include <boost/optional.hpp>
27
28 using std::pair;
29 using std::shared_ptr;
30 using boost::optional;
31
32 VideoView::VideoView (FilmViewer* viewer)
33         : _viewer (viewer)
34         , _state_timer ("viewer")
35         , _video_frame_rate (0)
36         , _eyes (Eyes::LEFT)
37         , _three_d (false)
38         , _dropped (0)
39         , _errored (0)
40         , _gets (0)
41 {
42
43 }
44
45 void
46 VideoView::clear ()
47 {
48         boost::mutex::scoped_lock lm (_mutex);
49         _player_video.first.reset ();
50         _player_video.second = dcpomatic::DCPTime ();
51 }
52
53 /** Could be called from any thread.
54  *  @param non_blocking true to return false quickly if no video is available quickly.
55  *  @return FAIL if there's no frame, AGAIN if the method should be called again, or SUCCESS
56  *  if there is a frame.
57  */
58 VideoView::NextFrameResult
59 VideoView::get_next_frame (bool non_blocking)
60 {
61         if (length() == dcpomatic::DCPTime()) {
62                 return FAIL;
63         }
64
65         auto butler = _viewer->butler ();
66         if (!butler) {
67                 return FAIL;
68         }
69         add_get ();
70
71         boost::mutex::scoped_lock lm (_mutex);
72
73         do {
74                 Butler::Error e;
75                 auto pv = butler->get_video (!non_blocking, &e);
76                 if (e.code == Butler::Error::DIED) {
77                         LOG_ERROR ("Butler died with %1", e.summary());
78                 }
79                 if (!pv.first) {
80                         return e.code == Butler::Error::AGAIN ? AGAIN : FAIL;
81                 }
82                 _player_video = pv;
83         } while (
84                 _player_video.first &&
85                 _three_d &&
86                 _eyes != _player_video.first->eyes() &&
87                 _player_video.first->eyes() != Eyes::BOTH
88                 );
89
90         if (_player_video.first && _player_video.first->error()) {
91                 ++_errored;
92         }
93
94         return SUCCESS;
95 }
96
97 dcpomatic::DCPTime
98 VideoView::one_video_frame () const
99 {
100         return dcpomatic::DCPTime::from_frames (1, video_frame_rate());
101 }
102
103 /** @return Time in ms until the next frame is due, or empty if nothing is due */
104 optional<int>
105 VideoView::time_until_next_frame () const
106 {
107         if (length() == dcpomatic::DCPTime()) {
108                 /* There's no content, so this doesn't matter */
109                 return optional<int>();
110         }
111
112         auto const next = position() + one_video_frame();
113         auto const time = _viewer->audio_time().get_value_or(position());
114         if (next < time) {
115                 return 0;
116         }
117         return (next.seconds() - time.seconds()) * 1000;
118 }
119
120 void
121 VideoView::start ()
122 {
123         boost::mutex::scoped_lock lm (_mutex);
124         _dropped = 0;
125         _errored = 0;
126 }
127
128 bool
129 VideoView::reset_metadata (shared_ptr<const Film> film, dcp::Size player_video_container_size)
130 {
131         auto pv = player_video ();
132         if (!pv.first) {
133                 return false;
134         }
135
136         if (!pv.first->reset_metadata(film, player_video_container_size)) {
137                 return false;
138         }
139
140         update ();
141         return true;
142 }
143