Fix deadlock.
[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 <boost/optional.hpp>
26
27 using std::pair;
28 using boost::shared_ptr;
29 using boost::optional;
30
31 VideoView::VideoView (FilmViewer* viewer)
32         : _viewer (viewer)
33 #ifdef DCPOMATIC_VARIANT_SWAROOP
34         , _in_watermark (false)
35 #endif
36         , _state_timer ("viewer")
37         , _video_frame_rate (0)
38         , _eyes (EYES_LEFT)
39         , _three_d (false)
40         , _dropped (0)
41         , _gets (0)
42 {
43
44 }
45
46 void
47 VideoView::clear ()
48 {
49         boost::mutex::scoped_lock lm (_mutex);
50         _player_video.first.reset ();
51         _player_video.second = dcpomatic::DCPTime ();
52 }
53
54 /** Could be called from any thread.
55  *  @param non_blocking true to return false quickly if no video is available quickly.
56  *  @return false if we gave up because it would take too long, otherwise true.
57  */
58 bool
59 VideoView::get_next_frame (bool non_blocking)
60 {
61         if (length() == dcpomatic::DCPTime()) {
62                 return true;
63         }
64
65         shared_ptr<Butler> butler = _viewer->butler ();
66         if (!butler) {
67                 return false;
68         }
69         add_get ();
70
71         boost::mutex::scoped_lock lm (_mutex);
72
73         do {
74                 Butler::Error e;
75                 _player_video = butler->get_video (!non_blocking, &e);
76                 if (!_player_video.first && e == Butler::AGAIN) {
77                         return false;
78                 }
79         } while (
80                 _player_video.first &&
81                 _three_d &&
82                 _eyes != _player_video.first->eyes() &&
83                 _player_video.first->eyes() != EYES_BOTH
84                 );
85
86         return true;
87 }
88
89 dcpomatic::DCPTime
90 VideoView::one_video_frame () const
91 {
92         return dcpomatic::DCPTime::from_frames (1, video_frame_rate());
93 }
94
95 /** @return Time in ms until the next frame is due, or empty if nothing is due */
96 optional<int>
97 VideoView::time_until_next_frame () const
98 {
99         if (length() == dcpomatic::DCPTime()) {
100                 /* There's no content, so this doesn't matter */
101                 return optional<int>();
102         }
103
104         dcpomatic::DCPTime const next = position() + one_video_frame();
105         dcpomatic::DCPTime const time = _viewer->audio_time().get_value_or(position());
106         if (next < time) {
107                 return 0;
108         }
109         return (next.seconds() - time.seconds()) * 1000;
110 }
111
112 void
113 VideoView::start ()
114 {
115         boost::mutex::scoped_lock lm (_mutex);
116         _dropped = 0;
117 }
118
119 bool
120 VideoView::refresh_metadata (shared_ptr<const Film> film, dcp::Size video_container_size, dcp::Size film_frame_size)
121 {
122         pair<shared_ptr<PlayerVideo>, dcpomatic::DCPTime> pv = player_video ();
123         if (!pv.first) {
124                 return false;
125         }
126
127         if (!pv.first->reset_metadata (film, video_container_size, film_frame_size)) {
128                 return false;
129         }
130
131         update ();
132         return true;
133 }
134