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(...) _log->log (String::compose(__VA_ARGS__), LogEntry::TYPE_WARNING);
48 Butler::Butler (shared_ptr<Player> player, shared_ptr<Log> log, AudioMapping audio_mapping, int audio_channels)
51 , _prepare_work (new boost::asio::io_service::work (_prepare_service))
52 , _pending_seek_accurate (false)
55 , _stop_thread (false)
56 , _audio_mapping (audio_mapping)
57 , _audio_channels (audio_channels)
58 , _disable_audio (false)
60 _player_video_connection = _player->Video.connect (bind (&Butler::video, this, _1, _2));
61 _player_audio_connection = _player->Audio.connect (bind (&Butler::audio, this, _1));
62 _thread = new boost::thread (bind (&Butler::thread, this));
64 /* Create some threads to do work on the PlayerVideos we are creating; at present this is used to
65 multi-thread JPEG2000 decoding.
67 for (size_t i = 0; i < boost::thread::hardware_concurrency(); ++i) {
68 _prepare_pool.create_thread (bind (&boost::asio::io_service::run, &_prepare_service));
75 boost::mutex::scoped_lock lm (_mutex);
79 _prepare_work.reset ();
80 _prepare_pool.join_all ();
81 _prepare_service.stop ();
83 _thread->interrupt ();
86 } catch (boost::thread_interrupted& e) {
92 /** Caller must hold a lock on _mutex */
94 Butler::should_run () const
96 if (_video.size() >= MAXIMUM_VIDEO_READAHEAD) {
97 LOG_WARNING ("Butler video buffers reached %1 frames", _video.size());
100 if (_audio.size() >= MAXIMUM_AUDIO_READAHEAD) {
101 LOG_WARNING ("Butler audio buffers reached %1 frames", _audio.size());
104 return (_video.size() < MINIMUM_VIDEO_READAHEAD || (!_disable_audio && _audio.size() < MINIMUM_AUDIO_READAHEAD))
105 && (_video.size() < MAXIMUM_VIDEO_READAHEAD)
106 && (_audio.size() < MAXIMUM_AUDIO_READAHEAD)
117 boost::mutex::scoped_lock lm (_mutex);
119 /* Wait until we have something to do */
120 while (!should_run() && !_pending_seek_position) {
124 /* Do any seek that has been requested */
125 if (_pending_seek_position) {
127 _player->seek (*_pending_seek_position, _pending_seek_accurate);
128 _pending_seek_position = optional<DCPTime> ();
131 /* Fill _video and _audio. Don't try to carry on if a pending seek appears
132 while lm is unlocked, as in that state nothing will be added to
135 while (should_run() && !_pending_seek_position) {
137 bool const r = _player->pass ();
141 _arrived.notify_all ();
144 _arrived.notify_all ();
147 } catch (boost::thread_interrupted) {
148 /* The butler thread is being terminated */
149 boost::mutex::scoped_lock lm (_mutex);
151 _arrived.notify_all ();
154 boost::mutex::scoped_lock lm (_mutex);
156 _arrived.notify_all ();
159 pair<shared_ptr<PlayerVideo>, DCPTime>
162 boost::mutex::scoped_lock lm (_mutex);
164 /* Wait for data if we have none */
165 while (_video.empty() && !_finished && !_died) {
169 if (_video.empty()) {
170 return make_pair (shared_ptr<PlayerVideo>(), DCPTime());
173 pair<shared_ptr<PlayerVideo>, DCPTime> const r = _video.get ();
174 _summon.notify_all ();
179 Butler::seek (DCPTime position, bool accurate)
181 boost::mutex::scoped_lock lm (_mutex);
189 _pending_seek_position = position;
190 _pending_seek_accurate = accurate;
191 _summon.notify_all ();
195 Butler::prepare (weak_ptr<PlayerVideo> weak_video) const
197 shared_ptr<PlayerVideo> video = weak_video.lock ();
198 /* If the weak_ptr cannot be locked the video obviously no longer requires any work */
205 Butler::video (shared_ptr<PlayerVideo> video, DCPTime time)
208 boost::mutex::scoped_lock lm (_mutex);
209 if (_pending_seek_position) {
210 /* Don't store any video while a seek is pending */
215 _prepare_service.post (bind (&Butler::prepare, this, weak_ptr<PlayerVideo>(video)));
216 _video.put (video, time);
220 Butler::audio (shared_ptr<AudioBuffers> audio)
223 boost::mutex::scoped_lock lm (_mutex);
224 if (_pending_seek_position || _disable_audio) {
225 /* Don't store any audio while a seek is pending, or if audio is disabled */
230 _audio.put (remap (audio, _audio_channels, _audio_mapping));
233 /** Try to get `frames' frames of audio and copy it into `out'. Silence
234 * will be filled if no audio is available.
235 * @return true if there was a buffer underrun, otherwise false.
238 Butler::get_audio (float* out, Frame frames)
240 bool const underrun = _audio.get (out, _audio_channels, frames);
241 _summon.notify_all ();
246 Butler::disable_audio ()
248 boost::mutex::scoped_lock lm (_mutex);
249 _disable_audio = true;