2 Copyright (C) 2016-2017 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
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.
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.
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/>.
23 #include <boost/weak_ptr.hpp>
24 #include <boost/shared_ptr.hpp>
29 using boost::weak_ptr;
30 using boost::shared_ptr;
32 using boost::optional;
34 /** Video readahead in frames */
35 #define VIDEO_READAHEAD 10
36 /** Audio readahead in frames */
37 #define AUDIO_READAHEAD (48000*5)
39 Butler::Butler (weak_ptr<const Film> film, shared_ptr<Player> player, AudioMapping audio_mapping, int audio_channels)
42 , _pending_seek_accurate (false)
45 , _audio_mapping (audio_mapping)
46 , _audio_channels (audio_channels)
47 , _stop_thread (false)
48 , _disable_audio (false)
50 _player_video_connection = _player->Video.connect (bind (&Butler::video, this, _1, _2));
51 _player_audio_connection = _player->Audio.connect (bind (&Butler::audio, this, _1, _2));
52 _player_changed_connection = _player->Changed.connect (bind (&Butler::player_changed, this));
53 _thread = new boost::thread (bind (&Butler::thread, this));
59 _thread->interrupt ();
62 } catch (boost::thread_interrupted& e) {
69 Butler::should_run () const
71 return (_video.size() < VIDEO_READAHEAD || (!_disable_audio && _audio.size() < AUDIO_READAHEAD)) && !_stop_thread && !_finished && !_died;
79 boost::mutex::scoped_lock lm (_mutex);
81 /* Wait until we have something to do */
82 while (!should_run() && !_pending_seek_position) {
86 /* Do any seek that has been requested */
87 if (_pending_seek_position) {
88 _player->seek (*_pending_seek_position, _pending_seek_accurate);
89 _pending_seek_position = optional<DCPTime> ();
92 /* Fill _video and _audio. Don't try to carry on if a pending seek appears
93 while lm is unlocked, as in that state nothing will be added to
96 while (should_run() && !_pending_seek_position) {
98 bool const r = _player->pass ();
102 _arrived.notify_all ();
105 _arrived.notify_all ();
108 } catch (boost::thread_interrupted) {
109 /* The butler thread is being terminated */
110 boost::mutex::scoped_lock lm (_mutex);
112 _arrived.notify_all ();
115 boost::mutex::scoped_lock lm (_mutex);
117 _arrived.notify_all ();
120 pair<shared_ptr<PlayerVideo>, DCPTime>
123 boost::mutex::scoped_lock lm (_mutex);
125 /* Wait for data if we have none */
126 while (_video.empty() && !_finished && !_died) {
130 if (_video.empty()) {
131 return make_pair (shared_ptr<PlayerVideo>(), DCPTime());
134 pair<shared_ptr<PlayerVideo>, DCPTime> const r = _video.get ();
135 _summon.notify_all ();
140 Butler::seek (DCPTime position, bool accurate)
142 boost::mutex::scoped_lock lm (_mutex);
150 _pending_seek_position = position;
151 _pending_seek_accurate = accurate;
152 _summon.notify_all ();
156 Butler::video (shared_ptr<PlayerVideo> video, DCPTime time)
159 boost::mutex::scoped_lock lm (_mutex);
160 if (_pending_seek_position) {
161 /* Don't store any video while a seek is pending */
166 _video.put (video, time);
170 Butler::audio (shared_ptr<AudioBuffers> audio, DCPTime time)
173 boost::mutex::scoped_lock lm (_mutex);
174 if (_pending_seek_position || _disable_audio) {
175 /* Don't store any audio while a seek is pending, or if audio is disabled */
180 _audio.put (audio, time);
184 Butler::player_changed ()
189 boost::mutex::scoped_lock lm (_mutex);
190 t = _video.earliest ();
202 Butler::get_audio (float* out, Frame frames)
204 _audio.get (out, _audio_channels, frames);
205 _summon.notify_all ();
209 Butler::disable_audio ()
211 boost::mutex::scoped_lock lm (_mutex);
212 _disable_audio = true;