Timestamp audio emissions from butler and hence discard very late
[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 "audio_mapping.h"
24 #include "exception_store.h"
25 #include <boost/shared_ptr.hpp>
26 #include <boost/weak_ptr.hpp>
27 #include <boost/thread.hpp>
28 #include <boost/thread/condition.hpp>
29 #include <boost/signals2.hpp>
30 #include <boost/asio.hpp>
31
32 class Player;
33 class PlayerVideo;
34 class Log;
35
36 class Butler : public ExceptionStore, public boost::noncopyable
37 {
38 public:
39         Butler (boost::shared_ptr<Player> player, boost::shared_ptr<Log> log, AudioMapping map, int audio_channels);
40         ~Butler ();
41
42         void seek (DCPTime position, bool accurate);
43         std::pair<boost::shared_ptr<PlayerVideo>, DCPTime> get_video ();
44         boost::optional<DCPTime> get_audio (float* out, Frame frames);
45
46         void disable_audio ();
47
48         std::pair<size_t, std::string> memory_used () const;
49
50 private:
51         void thread ();
52         void video (boost::shared_ptr<PlayerVideo> video, DCPTime time);
53         void audio (boost::shared_ptr<AudioBuffers> audio, DCPTime time);
54         bool should_run () const;
55         void prepare (boost::weak_ptr<PlayerVideo> video) const;
56         void player_changed (int);
57         void seek_unlocked (DCPTime position, bool accurate);
58
59         boost::shared_ptr<Player> _player;
60         boost::shared_ptr<Log> _log;
61         boost::thread* _thread;
62
63         /** mutex to protect _video and _audio for when we are clearing them and they both need to be
64             cleared together without any data being inserted in the interim.
65         */
66         boost::mutex _video_audio_mutex;
67         VideoRingBuffers _video;
68         AudioRingBuffers _audio;
69
70         boost::thread_group _prepare_pool;
71         boost::asio::io_service _prepare_service;
72         boost::shared_ptr<boost::asio::io_service::work> _prepare_work;
73
74         /** mutex to protect _pending_seek_position, _pending_seek_acurate, _finished, _died, _stop_thread */
75         boost::mutex _mutex;
76         boost::condition _summon;
77         boost::condition _arrived;
78         boost::optional<DCPTime> _pending_seek_position;
79         bool _pending_seek_accurate;
80         bool _finished;
81         bool _died;
82         bool _stop_thread;
83
84         AudioMapping _audio_mapping;
85         int _audio_channels;
86
87         bool _disable_audio;
88
89         /** If we are waiting to be refilled following a seek, this is the time we were
90             seeking to.
91         */
92         boost::optional<DCPTime> _awaiting;
93
94         boost::signals2::scoped_connection _player_video_connection;
95         boost::signals2::scoped_connection _player_audio_connection;
96         boost::signals2::scoped_connection _player_changed_connection;
97 };