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