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/>.
25 #include "compose.hpp"
26 #include <boost/weak_ptr.hpp>
27 #include <boost/shared_ptr.hpp>
32 using boost::weak_ptr;
33 using boost::shared_ptr;
35 using boost::optional;
37 /** Minimum video readahead in frames */
38 #define MINIMUM_VIDEO_READAHEAD 10
39 /** Maximum video readahead in frames; should never be reached unless there are bugs in Player */
40 #define MAXIMUM_VIDEO_READAHEAD 240
41 /** Minimum audio readahead in frames */
42 #define MINIMUM_AUDIO_READAHEAD (48000*5)
43 /** Minimum audio readahead in frames; should never be reached unless there are bugs in Player */
44 #define MAXIMUM_AUDIO_READAHEAD (48000*60)
46 #define LOG_WARNING(...) _film.lock()->log()->log (String::compose (__VA_ARGS__), LogEntry::TYPE_WARNING);
48 Butler::Butler (weak_ptr<const Film> film, shared_ptr<Player> player, AudioMapping audio_mapping, int audio_channels)
51 , _pending_seek_accurate (false)
54 , _stop_thread (false)
55 , _audio_mapping (audio_mapping)
56 , _audio_channels (audio_channels)
57 , _disable_audio (false)
59 _player_video_connection = _player->Video.connect (bind (&Butler::video, this, _1, _2));
60 _player_audio_connection = _player->Audio.connect (bind (&Butler::audio, this, _1));
61 _player_changed_connection = _player->Changed.connect (bind (&Butler::player_changed, this));
62 _thread = new boost::thread (bind (&Butler::thread, this));
68 boost::mutex::scoped_lock lm (_mutex);
72 _thread->interrupt ();
75 } catch (boost::thread_interrupted& e) {
81 /** Caller must hold a lock on _mutex */
83 Butler::should_run () const
85 if (_video.size() >= MAXIMUM_VIDEO_READAHEAD) {
86 LOG_WARNING ("Butler video buffers reached %1 frames", _video.size());
89 if (_audio.size() >= MAXIMUM_AUDIO_READAHEAD) {
90 LOG_WARNING ("Butler audio buffers reached %1 frames", _audio.size());
93 return (_video.size() < MINIMUM_VIDEO_READAHEAD || (!_disable_audio && _audio.size() < MINIMUM_AUDIO_READAHEAD))
94 && (_video.size() < MAXIMUM_VIDEO_READAHEAD)
95 && (_audio.size() < MAXIMUM_AUDIO_READAHEAD)
106 boost::mutex::scoped_lock lm (_mutex);
108 /* Wait until we have something to do */
109 while (!should_run() && !_pending_seek_position) {
113 /* Do any seek that has been requested */
114 if (_pending_seek_position) {
116 _player->seek (*_pending_seek_position, _pending_seek_accurate);
117 _pending_seek_position = optional<DCPTime> ();
120 /* Fill _video and _audio. Don't try to carry on if a pending seek appears
121 while lm is unlocked, as in that state nothing will be added to
124 while (should_run() && !_pending_seek_position) {
126 bool const r = _player->pass ();
130 _arrived.notify_all ();
133 _arrived.notify_all ();
136 } catch (boost::thread_interrupted) {
137 /* The butler thread is being terminated */
138 boost::mutex::scoped_lock lm (_mutex);
140 _arrived.notify_all ();
143 boost::mutex::scoped_lock lm (_mutex);
145 _arrived.notify_all ();
148 pair<shared_ptr<PlayerVideo>, DCPTime>
151 boost::mutex::scoped_lock lm (_mutex);
153 /* Wait for data if we have none */
154 while (_video.empty() && !_finished && !_died) {
158 if (_video.empty()) {
159 return make_pair (shared_ptr<PlayerVideo>(), DCPTime());
162 pair<shared_ptr<PlayerVideo>, DCPTime> const r = _video.get ();
163 _summon.notify_all ();
168 Butler::seek (DCPTime position, bool accurate)
170 boost::mutex::scoped_lock lm (_mutex);
178 _pending_seek_position = position;
179 _pending_seek_accurate = accurate;
180 _summon.notify_all ();
184 Butler::video (shared_ptr<PlayerVideo> video, DCPTime time)
187 boost::mutex::scoped_lock lm (_mutex);
188 if (_pending_seek_position) {
189 /* Don't store any video while a seek is pending */
194 _video.put (video, time);
198 Butler::audio (shared_ptr<AudioBuffers> audio)
201 boost::mutex::scoped_lock lm (_mutex);
202 if (_pending_seek_position || _disable_audio) {
203 /* Don't store any audio while a seek is pending, or if audio is disabled */
208 _audio.put (remap (audio, _audio_channels, _audio_mapping));
212 Butler::player_changed ()
217 boost::mutex::scoped_lock lm (_mutex);
218 t = _video.earliest ();
230 Butler::get_audio (float* out, Frame frames)
232 _audio.get (out, _audio_channels, frames);
233 _summon.notify_all ();
237 Butler::disable_audio ()
239 boost::mutex::scoped_lock lm (_mutex);
240 _disable_audio = true;