Don't seek during timeline drags.
[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 "text_ring_buffers.h"
24 #include "audio_mapping.h"
25 #include "exception_store.h"
26 #include <boost/shared_ptr.hpp>
27 #include <boost/weak_ptr.hpp>
28 #include <boost/thread.hpp>
29 #include <boost/thread/condition.hpp>
30 #include <boost/signals2.hpp>
31 #include <boost/asio.hpp>
32
33 class Player;
34 class PlayerVideo;
35 class Log;
36
37 class Butler : public ExceptionStore, public boost::noncopyable
38 {
39 public:
40         Butler (boost::shared_ptr<Player> player, boost::shared_ptr<Log> log, AudioMapping map, int audio_channels);
41         ~Butler ();
42
43         void seek (DCPTime position, bool accurate);
44         std::pair<boost::shared_ptr<PlayerVideo>, DCPTime> get_video ();
45         boost::optional<DCPTime> get_audio (float* out, Frame frames);
46         boost::optional<std::pair<PlayerText, DCPTimePeriod> > get_closed_caption ();
47
48         void disable_audio ();
49
50         std::pair<size_t, std::string> memory_used () const;
51
52 private:
53         void thread ();
54         void video (boost::shared_ptr<PlayerVideo> video, DCPTime time);
55         void audio (boost::shared_ptr<AudioBuffers> audio, DCPTime time);
56         void text (PlayerText pt, TextType type, DCPTimePeriod period);
57         bool should_run () const;
58         void prepare (boost::weak_ptr<PlayerVideo> video) const;
59         void player_changed (bool frequent);
60         void seek_unlocked (DCPTime position, bool accurate);
61
62         boost::shared_ptr<Player> _player;
63         boost::shared_ptr<Log> _log;
64         boost::thread* _thread;
65
66         /** mutex to protect _video, _audio and _closed_caption for when we are clearing them and they all need to be
67             cleared together without any data being inserted in the interim;
68             XXX: is this necessary now that all butler output data is timestamped? Perhaps the locked clear-out
69             is only required if we guarantee that get_video() and get_audio() calls are in sync.
70         */
71         boost::mutex _buffers_mutex;
72         VideoRingBuffers _video;
73         AudioRingBuffers _audio;
74         TextRingBuffers _closed_caption;
75
76         boost::thread_group _prepare_pool;
77         boost::asio::io_service _prepare_service;
78         boost::shared_ptr<boost::asio::io_service::work> _prepare_work;
79
80         /** mutex to protect _pending_seek_position, _pending_seek_acurate, _finished, _died, _stop_thread */
81         boost::mutex _mutex;
82         boost::condition _summon;
83         boost::condition _arrived;
84         boost::optional<DCPTime> _pending_seek_position;
85         bool _pending_seek_accurate;
86         bool _finished;
87         bool _died;
88         bool _stop_thread;
89
90         AudioMapping _audio_mapping;
91         int _audio_channels;
92
93         bool _disable_audio;
94
95         /** If we are waiting to be refilled following a seek, this is the time we were
96             seeking to.
97         */
98         boost::optional<DCPTime> _awaiting;
99
100         boost::signals2::scoped_connection _player_video_connection;
101         boost::signals2::scoped_connection _player_audio_connection;
102         boost::signals2::scoped_connection _player_text_connection;
103         boost::signals2::scoped_connection _player_changed_connection;
104 };