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>
33 using boost::weak_ptr;
34 using boost::shared_ptr;
36 using boost::optional;
38 /** Minimum video readahead in frames */
39 #define MINIMUM_VIDEO_READAHEAD 10
40 /** Maximum video readahead in frames; should never be reached unless there are bugs in Player */
41 #define MAXIMUM_VIDEO_READAHEAD 24
42 /** Minimum audio readahead in frames */
43 #define MINIMUM_AUDIO_READAHEAD (48000 * MINIMUM_VIDEO_READAHEAD / 24)
44 /** Minimum audio readahead in frames; should never be reached unless there are bugs in Player */
45 #define MAXIMUM_AUDIO_READAHEAD (48000 * MAXIMUM_VIDEO_READAHEAD / 24)
47 #define LOG_WARNING(...) _log->log (String::compose(__VA_ARGS__), LogEntry::TYPE_WARNING);
49 Butler::Butler (shared_ptr<Player> player, shared_ptr<Log> log, AudioMapping audio_mapping, int audio_channels)
52 , _prepare_work (new boost::asio::io_service::work (_prepare_service))
53 , _pending_seek_accurate (false)
56 , _stop_thread (false)
57 , _audio_mapping (audio_mapping)
58 , _audio_channels (audio_channels)
59 , _disable_audio (false)
61 _player_video_connection = _player->Video.connect (bind (&Butler::video, this, _1, _2));
62 _player_audio_connection = _player->Audio.connect (bind (&Butler::audio, this, _1));
63 _thread = new boost::thread (bind (&Butler::thread, this));
65 /* Create some threads to do work on the PlayerVideos we are creating; at present this is used to
66 multi-thread JPEG2000 decoding.
68 for (size_t i = 0; i < boost::thread::hardware_concurrency(); ++i) {
69 _prepare_pool.create_thread (bind (&boost::asio::io_service::run, &_prepare_service));
76 boost::mutex::scoped_lock lm (_mutex);
80 _prepare_work.reset ();
81 _prepare_pool.join_all ();
82 _prepare_service.stop ();
84 _thread->interrupt ();
87 } catch (boost::thread_interrupted& e) {
93 /** Caller must hold a lock on _mutex */
95 Butler::should_run () const
97 if (_video.size() >= MAXIMUM_VIDEO_READAHEAD) {
98 LOG_WARNING ("Butler video buffers reached %1 frames", _video.size());
101 if (_audio.size() >= MAXIMUM_AUDIO_READAHEAD) {
102 LOG_WARNING ("Butler audio buffers reached %1 frames", _audio.size());
105 if (_stop_thread || _finished || _died) {
106 /* Definitely do not run */
110 if (_video.size() < MINIMUM_VIDEO_READAHEAD || (!_disable_audio && _audio.size() < MINIMUM_AUDIO_READAHEAD)) {
111 /* Definitely do run: we need data */
115 /* Run if we aren't full of video or audio */
116 return (_video.size() < MAXIMUM_VIDEO_READAHEAD) && (_audio.size() < MAXIMUM_AUDIO_READAHEAD);
124 boost::mutex::scoped_lock lm (_mutex);
126 /* Wait until we have something to do */
127 while (!should_run() && !_pending_seek_position) {
131 /* Do any seek that has been requested */
132 if (_pending_seek_position) {
134 _player->seek (*_pending_seek_position, _pending_seek_accurate);
135 _pending_seek_position = optional<DCPTime> ();
138 /* Fill _video and _audio. Don't try to carry on if a pending seek appears
139 while lm is unlocked, as in that state nothing will be added to
142 while (should_run() && !_pending_seek_position) {
144 bool const r = _player->pass ();
148 _arrived.notify_all ();
151 _arrived.notify_all ();
154 } catch (boost::thread_interrupted) {
155 /* The butler thread is being terminated */
156 boost::mutex::scoped_lock lm (_mutex);
158 _arrived.notify_all ();
161 boost::mutex::scoped_lock lm (_mutex);
163 _arrived.notify_all ();
166 pair<shared_ptr<PlayerVideo>, DCPTime>
169 boost::mutex::scoped_lock lm (_mutex);
171 /* Wait for data if we have none */
172 while (_video.empty() && !_finished && !_died) {
176 if (_video.empty()) {
177 return make_pair (shared_ptr<PlayerVideo>(), DCPTime());
180 pair<shared_ptr<PlayerVideo>, DCPTime> const r = _video.get ();
181 _summon.notify_all ();
186 Butler::seek (DCPTime position, bool accurate)
188 boost::mutex::scoped_lock lm (_mutex);
196 _pending_seek_position = position;
197 _pending_seek_accurate = accurate;
198 _summon.notify_all ();
202 Butler::prepare (weak_ptr<PlayerVideo> weak_video) const
204 shared_ptr<PlayerVideo> video = weak_video.lock ();
205 /* If the weak_ptr cannot be locked the video obviously no longer requires any work */
212 Butler::video (shared_ptr<PlayerVideo> video, DCPTime time)
215 boost::mutex::scoped_lock lm (_mutex);
216 if (_pending_seek_position) {
217 /* Don't store any video while a seek is pending */
222 _prepare_service.post (bind (&Butler::prepare, this, weak_ptr<PlayerVideo>(video)));
223 _video.put (video, time);
227 Butler::audio (shared_ptr<AudioBuffers> audio)
230 boost::mutex::scoped_lock lm (_mutex);
231 if (_pending_seek_position || _disable_audio) {
232 /* Don't store any audio while a seek is pending, or if audio is disabled */
237 _audio.put (remap (audio, _audio_channels, _audio_mapping));
240 /** Try to get `frames' frames of audio and copy it into `out'. Silence
241 * will be filled if no audio is available.
242 * @return true if there was a buffer underrun, otherwise false.
245 Butler::get_audio (float* out, Frame frames)
247 bool const underrun = _audio.get (out, _audio_channels, frames);
248 _summon.notify_all ();
253 Butler::disable_audio ()
255 boost::mutex::scoped_lock lm (_mutex);
256 _disable_audio = true;
260 Butler::memory_used () const
262 /* XXX: should also look at _audio.memory_used() */
263 return _video.memory_used();