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