Fix video flickering when seeking near the end of the film.
[dcpomatic.git] / src / lib / butler.h
1 /*
2     Copyright (C) 2016-2017 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_ring_buffers.h"
22 #include "audio_ring_buffers.h"
23 #include "audio_mapping.h"
24 #include "exception_store.h"
25 #include <boost/shared_ptr.hpp>
26 #include <boost/weak_ptr.hpp>
27 #include <boost/thread.hpp>
28 #include <boost/thread/condition.hpp>
29 #include <boost/signals2.hpp>
30
31 class Film;
32 class Player;
33 class PlayerVideo;
34
35 class Butler : public ExceptionStore, public boost::noncopyable
36 {
37 public:
38         Butler (boost::weak_ptr<const Film> film, boost::shared_ptr<Player> player, AudioMapping map, int audio_channels);
39         ~Butler ();
40
41         void seek (DCPTime position, bool accurate);
42         std::pair<boost::shared_ptr<PlayerVideo>, DCPTime> get_video ();
43         void get_audio (float* out, Frame frames);
44
45         void disable_audio ();
46
47 private:
48         void thread ();
49         void video (boost::shared_ptr<PlayerVideo> video, DCPTime time);
50         void audio (boost::shared_ptr<AudioBuffers> audio, DCPTime time);
51         void player_changed ();
52         bool should_run () const;
53
54         boost::weak_ptr<const Film> _film;
55         boost::shared_ptr<Player> _player;
56         boost::thread* _thread;
57
58         VideoRingBuffers _video;
59         AudioRingBuffers _audio;
60
61         /** mutex to protect _pending_seek_position, _pending_seek_acurate, _finished, _died, _stop_thread */
62         boost::mutex _mutex;
63         boost::condition _summon;
64         boost::condition _arrived;
65         boost::optional<DCPTime> _pending_seek_position;
66         bool _pending_seek_accurate;
67         bool _finished;
68         bool _died;
69         bool _stop_thread;
70
71         AudioMapping _audio_mapping;
72         int _audio_channels;
73
74         bool _disable_audio;
75
76         boost::signals2::scoped_connection _player_video_connection;
77         boost::signals2::scoped_connection _player_audio_connection;
78         boost::signals2::scoped_connection _player_changed_connection;
79 };