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