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/>.
26 #include "compose.hpp"
27 #include "exceptions.h"
28 #include <boost/weak_ptr.hpp>
29 #include <boost/shared_ptr.hpp>
31 #define LOG_TIMING(...) _log->log (String::compose(__VA_ARGS__), LogEntry::TYPE_TIMING);
32 #define LOG_WARNING(...) _log->log (String::compose(__VA_ARGS__), LogEntry::TYPE_WARNING);
38 using boost::weak_ptr;
39 using boost::shared_ptr;
41 using boost::optional;
43 /** Minimum video readahead in frames */
44 #define MINIMUM_VIDEO_READAHEAD 10
45 /** Maximum video readahead in frames; should never be reached unless there are bugs in Player */
46 #define MAXIMUM_VIDEO_READAHEAD 24
47 /** Minimum audio readahead in frames */
48 #define MINIMUM_AUDIO_READAHEAD (48000 * MINIMUM_VIDEO_READAHEAD / 24)
49 /** Minimum audio readahead in frames; should never be reached unless there are bugs in Player */
50 #define MAXIMUM_AUDIO_READAHEAD (48000 * MAXIMUM_VIDEO_READAHEAD / 24)
52 Butler::Butler (shared_ptr<Player> player, shared_ptr<Log> log, AudioMapping audio_mapping, int audio_channels)
55 , _prepare_work (new boost::asio::io_service::work (_prepare_service))
56 , _pending_seek_accurate (false)
59 , _stop_thread (false)
60 , _audio_mapping (audio_mapping)
61 , _audio_channels (audio_channels)
62 , _disable_audio (false)
64 _player_video_connection = _player->Video.connect (bind (&Butler::video, this, _1, _2));
65 _player_audio_connection = _player->Audio.connect (bind (&Butler::audio, this, _1));
66 _thread = new boost::thread (bind (&Butler::thread, this));
67 #ifdef DCPOMATIC_LINUX
68 pthread_setname_np (_thread->native_handle(), "butler");
71 /* Create some threads to do work on the PlayerVideos we are creating; at present this is used to
72 multi-thread JPEG2000 decoding.
75 LOG_TIMING("start-prepare-threads %1", boost::thread::hardware_concurrency());
76 for (size_t i = 0; i < boost::thread::hardware_concurrency(); ++i) {
77 _prepare_pool.create_thread (bind (&boost::asio::io_service::run, &_prepare_service));
84 boost::mutex::scoped_lock lm (_mutex);
88 _prepare_work.reset ();
89 _prepare_pool.join_all ();
90 _prepare_service.stop ();
92 _thread->interrupt ();
95 } catch (boost::thread_interrupted& e) {
101 /** Caller must hold a lock on _mutex */
103 Butler::should_run () const
105 if (_video.size() >= MAXIMUM_VIDEO_READAHEAD * 10) {
106 /* This is way too big */
107 throw ProgrammingError
108 (__FILE__, __LINE__, String::compose ("Butler video buffers reached %1 frames (audio is %2)", _video.size(), _audio.size()));
111 if (_audio.size() >= MAXIMUM_AUDIO_READAHEAD * 10) {
112 /* This is way too big */
113 throw ProgrammingError
114 (__FILE__, __LINE__, String::compose ("Butler audio buffers reached %1 frames (video is %2)", _audio.size(), _video.size()));
117 if (_video.size() >= MAXIMUM_VIDEO_READAHEAD * 2) {
118 LOG_WARNING ("Butler video buffers reached %1 frames (audio is %2)", _video.size(), _audio.size());
121 if (_audio.size() >= MAXIMUM_AUDIO_READAHEAD * 2) {
122 LOG_WARNING ("Butler audio buffers reached %1 frames (video is %2)", _audio.size(), _video.size());
125 if (_stop_thread || _finished || _died) {
126 /* Definitely do not run */
130 if (_video.size() < MINIMUM_VIDEO_READAHEAD || (!_disable_audio && _audio.size() < MINIMUM_AUDIO_READAHEAD)) {
131 /* Definitely do run: we need data */
135 /* Run if we aren't full of video or audio */
136 return (_video.size() < MAXIMUM_VIDEO_READAHEAD) && (_audio.size() < MAXIMUM_AUDIO_READAHEAD);
144 boost::mutex::scoped_lock lm (_mutex);
146 /* Wait until we have something to do */
147 while (!should_run() && !_pending_seek_position) {
151 /* Do any seek that has been requested */
152 if (_pending_seek_position) {
154 _player->seek (*_pending_seek_position, _pending_seek_accurate);
155 _pending_seek_position = optional<DCPTime> ();
158 /* Fill _video and _audio. Don't try to carry on if a pending seek appears
159 while lm is unlocked, as in that state nothing will be added to
162 while (should_run() && !_pending_seek_position) {
164 bool const r = _player->pass ();
168 _arrived.notify_all ();
171 _arrived.notify_all ();
174 } catch (boost::thread_interrupted) {
175 /* The butler thread is being terminated */
176 boost::mutex::scoped_lock lm (_mutex);
178 _arrived.notify_all ();
181 boost::mutex::scoped_lock lm (_mutex);
183 _arrived.notify_all ();
186 pair<shared_ptr<PlayerVideo>, DCPTime>
189 boost::mutex::scoped_lock lm (_mutex);
191 /* Wait for data if we have none */
192 while (_video.empty() && !_finished && !_died) {
196 if (_video.empty()) {
197 return make_pair (shared_ptr<PlayerVideo>(), DCPTime());
200 pair<shared_ptr<PlayerVideo>, DCPTime> const r = _video.get ();
201 _summon.notify_all ();
206 Butler::seek (DCPTime position, bool accurate)
208 boost::mutex::scoped_lock lm (_mutex);
216 _pending_seek_position = position;
217 _pending_seek_accurate = accurate;
218 _summon.notify_all ();
222 Butler::prepare (weak_ptr<PlayerVideo> weak_video) const
224 shared_ptr<PlayerVideo> video = weak_video.lock ();
225 /* If the weak_ptr cannot be locked the video obviously no longer requires any work */
227 LOG_TIMING("start-prepare in %1", thread_id());
229 LOG_TIMING("finish-prepare in %1", thread_id());
234 Butler::video (shared_ptr<PlayerVideo> video, DCPTime time)
236 boost::mutex::scoped_lock lm (_mutex);
237 if (_pending_seek_position) {
238 /* Don't store any video while a seek is pending */
242 _prepare_service.post (bind (&Butler::prepare, this, weak_ptr<PlayerVideo>(video)));
243 _video.put (video, time);
247 Butler::audio (shared_ptr<AudioBuffers> audio)
250 boost::mutex::scoped_lock lm (_mutex);
251 if (_pending_seek_position || _disable_audio) {
252 /* Don't store any audio while a seek is pending, or if audio is disabled */
257 _audio.put (remap (audio, _audio_channels, _audio_mapping));
260 /** Try to get `frames' frames of audio and copy it into `out'. Silence
261 * will be filled if no audio is available.
262 * @return true if there was a buffer underrun, otherwise false.
265 Butler::get_audio (float* out, Frame frames)
267 bool const underrun = _audio.get (out, _audio_channels, frames);
268 _summon.notify_all ();
273 Butler::disable_audio ()
275 boost::mutex::scoped_lock lm (_mutex);
276 _disable_audio = true;
280 Butler::memory_used () const
282 /* XXX: should also look at _audio.memory_used() */
283 return _video.memory_used();