Tidy and fix logging.
[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::shared_ptr<Player> player,
41                 AudioMapping map,
42                 int audio_channels,
43                 boost::function<AVPixelFormat (AVPixelFormat)> pixel_format,
44                 bool aligned,
45                 bool fast
46                 );
47
48         ~Butler ();
49
50         void seek (DCPTime position, bool accurate);
51
52         enum Error {
53                 NONE,
54                 AGAIN
55         };
56
57         std::pair<boost::shared_ptr<PlayerVideo>, DCPTime> get_video (Error* e = 0);
58         boost::optional<DCPTime> get_audio (float* out, Frame frames);
59         boost::optional<TextRingBuffers::Data> get_closed_caption ();
60
61         void disable_audio ();
62
63         std::pair<size_t, std::string> memory_used () const;
64
65 private:
66         void thread ();
67         void video (boost::shared_ptr<PlayerVideo> video, DCPTime time);
68         void audio (boost::shared_ptr<AudioBuffers> audio, DCPTime time);
69         void text (PlayerText pt, TextType type, boost::optional<DCPTextTrack> track, DCPTimePeriod period);
70         bool should_run () const;
71         void prepare (boost::weak_ptr<PlayerVideo> video) const;
72         void player_change (ChangeType type, bool frequent);
73         void seek_unlocked (DCPTime position, bool accurate);
74
75         boost::shared_ptr<Player> _player;
76         boost::thread* _thread;
77
78         /** mutex to protect _video, _audio and _closed_caption for when we are clearing them and they all need to be
79             cleared together without any data being inserted in the interim;
80             XXX: is this necessary now that all butler output data is timestamped? Perhaps the locked clear-out
81             is only required if we guarantee that get_video() and get_audio() calls are in sync.
82         */
83         boost::mutex _buffers_mutex;
84         VideoRingBuffers _video;
85         AudioRingBuffers _audio;
86         TextRingBuffers _closed_caption;
87
88         boost::thread_group _prepare_pool;
89         boost::asio::io_service _prepare_service;
90         boost::shared_ptr<boost::asio::io_service::work> _prepare_work;
91
92         /** mutex to protect _pending_seek_position, _pending_seek_acurate, _finished, _died, _stop_thread */
93         boost::mutex _mutex;
94         boost::condition _summon;
95         boost::condition _arrived;
96         boost::optional<DCPTime> _pending_seek_position;
97         bool _pending_seek_accurate;
98         int _suspended;
99         bool _finished;
100         bool _died;
101         bool _stop_thread;
102
103         AudioMapping _audio_mapping;
104         int _audio_channels;
105
106         bool _disable_audio;
107
108         boost::function<AVPixelFormat (AVPixelFormat)> _pixel_format;
109         bool _aligned;
110         bool _fast;
111
112         /** If we are waiting to be refilled following a seek, this is the time we were
113             seeking to.
114         */
115         boost::optional<DCPTime> _awaiting;
116
117         boost::signals2::scoped_connection _player_video_connection;
118         boost::signals2::scoped_connection _player_audio_connection;
119         boost::signals2::scoped_connection _player_text_connection;
120         boost::signals2::scoped_connection _player_change_connection;
121 };