C++11 tidying.
[dcpomatic.git] / src / wx / video_view.cc
1 /*
2     Copyright (C) 2019-2021 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
22 #include "video_view.h"
23 #include "wx_util.h"
24 #include "film_viewer.h"
25 #include "lib/butler.h"
26 #include "lib/dcpomatic_log.h"
27 #include <boost/optional.hpp>
28
29 using std::pair;
30 using std::shared_ptr;
31 using boost::optional;
32
33
34 VideoView::VideoView (FilmViewer* viewer)
35         : _viewer (viewer)
36         , _state_timer ("viewer")
37 {
38
39 }
40
41
42 void
43 VideoView::clear ()
44 {
45         boost::mutex::scoped_lock lm (_mutex);
46         _player_video.first.reset ();
47         _player_video.second = dcpomatic::DCPTime ();
48 }
49
50
51 /** Could be called from any thread.
52  *  @param non_blocking true to return false quickly if no video is available quickly.
53  *  @return FAIL if there's no frame, AGAIN if the method should be called again, or SUCCESS
54  *  if there is a frame.
55  */
56 VideoView::NextFrameResult
57 VideoView::get_next_frame (bool non_blocking)
58 {
59         if (length() == dcpomatic::DCPTime()) {
60                 return FAIL;
61         }
62
63         auto butler = _viewer->butler ();
64         if (!butler) {
65                 return FAIL;
66         }
67         add_get ();
68
69         boost::mutex::scoped_lock lm (_mutex);
70
71         do {
72                 Butler::Error e;
73                 auto pv = butler->get_video (!non_blocking, &e);
74                 if (e.code == Butler::Error::DIED) {
75                         LOG_ERROR ("Butler died with %1", e.summary());
76                 }
77                 if (!pv.first) {
78                         return e.code == Butler::Error::AGAIN ? AGAIN : FAIL;
79                 }
80                 _player_video = pv;
81         } while (
82                 _player_video.first &&
83                 _three_d &&
84                 _eyes != _player_video.first->eyes() &&
85                 _player_video.first->eyes() != Eyes::BOTH
86                 );
87
88         if (_player_video.first && _player_video.first->error()) {
89                 ++_errored;
90         }
91
92         return SUCCESS;
93 }
94
95
96 dcpomatic::DCPTime
97 VideoView::one_video_frame () const
98 {
99         return dcpomatic::DCPTime::from_frames (1, video_frame_rate());
100 }
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
121 void
122 VideoView::start ()
123 {
124         boost::mutex::scoped_lock lm (_mutex);
125         _dropped = 0;
126         _errored = 0;
127 }
128
129
130 bool
131 VideoView::reset_metadata (shared_ptr<const Film> film, dcp::Size player_video_container_size)
132 {
133         auto pv = player_video ();
134         if (!pv.first) {
135                 return false;
136         }
137
138         if (!pv.first->reset_metadata(film, player_video_container_size)) {
139                 return false;
140         }
141
142         update ();
143         return true;
144 }
145