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