Supporters update.
[dcpomatic.git] / src / lib / butler.h
1 /*
2     Copyright (C) 2016-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 #ifndef DCPOMATIC_BUTLER_H
23 #define DCPOMATIC_BUTLER_H
24
25
26 #include "audio_mapping.h"
27 #include "audio_ring_buffers.h"
28 #include "change_signaller.h"
29 #include "exception_store.h"
30 #include "text_ring_buffers.h"
31 #include "text_type.h"
32 #include "video_ring_buffers.h"
33 #include <boost/asio.hpp>
34 #include <boost/signals2.hpp>
35 #include <boost/thread.hpp>
36 #include <boost/thread/condition.hpp>
37
38
39 class Player;
40 class PlayerVideo;
41
42
43 class Butler : public ExceptionStore
44 {
45 public:
46         enum class Audio
47         {
48                 ENABLED,
49                 DISABLED
50         };
51
52         Butler (
53                 std::weak_ptr<const Film> film,
54                 Player& player,
55                 AudioMapping map,
56                 int audio_channels,
57                 std::function<AVPixelFormat (AVPixelFormat)> pixel_format,
58                 VideoRange video_range,
59                 Image::Alignment alignment,
60                 bool fast,
61                 bool prepare_only_proxy,
62                 Audio audio
63                 );
64
65         ~Butler ();
66
67         Butler (Butler const&) = delete;
68         Butler& operator= (Butler const&) = delete;
69
70         void seek (dcpomatic::DCPTime position, bool accurate);
71
72         class Error {
73         public:
74                 enum class Code {
75                         NONE,
76                         AGAIN,
77                         DIED,
78                         FINISHED
79                 };
80
81                 Code code = Code::NONE;
82                 std::string message;
83
84                 std::string summary () const;
85         };
86
87         enum class Behaviour {
88                 BLOCKING,
89                 NON_BLOCKING
90         };
91
92         std::pair<std::shared_ptr<PlayerVideo>, dcpomatic::DCPTime> get_video (Behaviour behaviour, Error* e = nullptr);
93         boost::optional<dcpomatic::DCPTime> get_audio (Behaviour behaviour, float* out, Frame frames);
94         boost::optional<TextRingBuffers::Data> get_closed_caption ();
95
96         std::pair<size_t, std::string> memory_used () const;
97
98 private:
99         void thread ();
100         void video (std::shared_ptr<PlayerVideo> video, dcpomatic::DCPTime time);
101         void audio (std::shared_ptr<AudioBuffers> audio, dcpomatic::DCPTime time, int frame_rate);
102         void text (PlayerText pt, TextType type, boost::optional<DCPTextTrack> track, dcpomatic::DCPTimePeriod period);
103         bool should_run () const;
104         void prepare (std::weak_ptr<PlayerVideo> video);
105         void player_change (ChangeType type, int property);
106         void seek_unlocked (dcpomatic::DCPTime position, bool accurate);
107
108         std::weak_ptr<const Film> _film;
109         Player& _player;
110         boost::thread _thread;
111
112         VideoRingBuffers _video;
113         AudioRingBuffers _audio;
114         TextRingBuffers _closed_caption;
115
116         boost::thread_group _prepare_pool;
117         boost::asio::io_service _prepare_service;
118         std::shared_ptr<boost::asio::io_service::work> _prepare_work;
119
120         /** mutex to protect _pending_seek_position, _pending_seek_accurate, _finished, _died, _stop_thread */
121         boost::mutex _mutex;
122         boost::condition _summon;
123         boost::condition _arrived;
124         boost::optional<dcpomatic::DCPTime> _pending_seek_position;
125         bool _pending_seek_accurate;
126         int _suspended;
127         bool _finished;
128         bool _died;
129         std::string _died_message;
130         bool _stop_thread;
131
132         AudioMapping _audio_mapping;
133         int _audio_channels;
134
135         bool _disable_audio;
136
137         std::function<AVPixelFormat (AVPixelFormat)> _pixel_format;
138         VideoRange _video_range;
139         Image::Alignment _alignment;
140         bool _fast;
141
142         /** true to ask PlayerVideo::prepare to only prepare the ImageProxy and not also
143          *  the final image.  We want to do this when the viewer is intending to call
144          *  PlayerVideo::raw_image() and do the things in PlayerVideo::make_image() itself:
145          *  this is the case for the GLVideoView which can do scale, pixfmt conversion etc.
146          *  in the shader.
147          */
148         bool _prepare_only_proxy = false;
149
150         /** If we are waiting to be refilled following a seek, this is the time we were
151             seeking to.
152         */
153         boost::optional<dcpomatic::DCPTime> _awaiting;
154
155         boost::signals2::scoped_connection _player_video_connection;
156         boost::signals2::scoped_connection _player_audio_connection;
157         boost::signals2::scoped_connection _player_text_connection;
158         boost::signals2::scoped_connection _player_change_connection;
159 };
160
161
162 #endif
163