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 48
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)
60 , _stop_thread (false)
61 , _audio_mapping (audio_mapping)
62 , _audio_channels (audio_channels)
63 , _disable_audio (false)
65 _player_video_connection = _player->Video.connect (bind (&Butler::video, this, _1, _2));
66 _player_audio_connection = _player->Audio.connect (bind (&Butler::audio, this, _1, _2));
67 _player_text_connection = _player->Text.connect (bind (&Butler::text, this, _1, _2, _3, _4));
68 /* The butler must hear about things first, otherwise it might not sort out suspensions in time for
69 get_video() to be called in response to this signal.
71 _player_change_connection = _player->Change.connect (bind (&Butler::player_change, this, _1, _3), boost::signals2::at_front);
72 _thread = new boost::thread (bind (&Butler::thread, this));
73 #ifdef DCPOMATIC_LINUX
74 pthread_setname_np (_thread->native_handle(), "butler");
77 /* Create some threads to do work on the PlayerVideos we are creating; at present this is used to
78 multi-thread JPEG2000 decoding.
82 LOG_TIMING("start-prepare-threads %1", boost::thread::hardware_concurrency() * 2);
85 for (size_t i = 0; i < boost::thread::hardware_concurrency() * 2; ++i) {
86 _prepare_pool.create_thread (bind (&boost::asio::io_service::run, &_prepare_service));
93 boost::mutex::scoped_lock lm (_mutex);
97 _prepare_work.reset ();
98 _prepare_pool.join_all ();
99 _prepare_service.stop ();
101 _thread->interrupt ();
104 } catch (boost::thread_interrupted& e) {
110 /** Caller must hold a lock on _mutex */
112 Butler::should_run () const
114 if (_video.size() >= MAXIMUM_VIDEO_READAHEAD * 10) {
115 /* This is way too big */
116 throw ProgrammingError
117 (__FILE__, __LINE__, String::compose ("Butler video buffers reached %1 frames (audio is %2)", _video.size(), _audio.size()));
120 if (_audio.size() >= MAXIMUM_AUDIO_READAHEAD * 10) {
121 /* This is way too big */
122 throw ProgrammingError
123 (__FILE__, __LINE__, String::compose ("Butler audio buffers reached %1 frames (video is %2)", _audio.size(), _video.size()));
126 if (_video.size() >= MAXIMUM_VIDEO_READAHEAD * 2 && _log) {
127 LOG_WARNING ("Butler video buffers reached %1 frames (audio is %2)", _video.size(), _audio.size());
130 if (_audio.size() >= MAXIMUM_AUDIO_READAHEAD * 2 && _log) {
131 LOG_WARNING ("Butler audio buffers reached %1 frames (video is %2)", _audio.size(), _video.size());
134 if (_stop_thread || _finished || _died || _suspended) {
135 /* Definitely do not run */
139 if (_video.size() < MINIMUM_VIDEO_READAHEAD || (!_disable_audio && _audio.size() < MINIMUM_AUDIO_READAHEAD)) {
140 /* Definitely do run: we need data */
144 /* Run if we aren't full of video or audio */
145 return (_video.size() < MAXIMUM_VIDEO_READAHEAD) && (_audio.size() < MAXIMUM_AUDIO_READAHEAD);
153 boost::mutex::scoped_lock lm (_mutex);
155 /* Wait until we have something to do */
156 while (!should_run() && !_pending_seek_position) {
160 /* Do any seek that has been requested */
161 if (_pending_seek_position) {
163 _player->seek (*_pending_seek_position, _pending_seek_accurate);
164 _pending_seek_position = optional<DCPTime> ();
167 /* Fill _video and _audio. Don't try to carry on if a pending seek appears
168 while lm is unlocked, as in that state nothing will be added to
171 while (should_run() && !_pending_seek_position) {
173 bool const r = _player->pass ();
177 _arrived.notify_all ();
180 _arrived.notify_all ();
183 } catch (boost::thread_interrupted) {
184 /* The butler thread is being terminated */
185 boost::mutex::scoped_lock lm (_mutex);
187 _arrived.notify_all ();
190 boost::mutex::scoped_lock lm (_mutex);
192 _arrived.notify_all ();
195 pair<shared_ptr<PlayerVideo>, DCPTime>
196 Butler::get_video (Error* e)
198 boost::mutex::scoped_lock lm (_mutex);
204 return make_pair(shared_ptr<PlayerVideo>(), DCPTime());
207 /* Wait for data if we have none */
208 while (_video.empty() && !_finished && !_died) {
212 if (_video.empty()) {
216 return make_pair(shared_ptr<PlayerVideo>(), DCPTime());
219 pair<shared_ptr<PlayerVideo>, DCPTime> const r = _video.get ();
220 _summon.notify_all ();
224 optional<TextRingBuffers::Data>
225 Butler::get_closed_caption ()
227 boost::mutex::scoped_lock lm (_mutex);
228 return _closed_caption.get ();
232 Butler::seek (DCPTime position, bool accurate)
234 boost::mutex::scoped_lock lm (_mutex);
235 seek_unlocked (position, accurate);
239 Butler::seek_unlocked (DCPTime position, bool accurate)
246 _pending_seek_position = position;
247 _pending_seek_accurate = accurate;
250 boost::mutex::scoped_lock lm (_buffers_mutex);
253 _closed_caption.clear ();
256 _summon.notify_all ();
260 Butler::prepare (weak_ptr<PlayerVideo> weak_video) const
262 shared_ptr<PlayerVideo> video = weak_video.lock ();
263 /* If the weak_ptr cannot be locked the video obviously no longer requires any work */
266 LOG_TIMING("start-prepare in %1", thread_id());
272 LOG_TIMING("finish-prepare in %1", thread_id());
278 Butler::video (shared_ptr<PlayerVideo> video, DCPTime time)
280 boost::mutex::scoped_lock lm (_mutex);
282 if (_pending_seek_position) {
283 /* Don't store any video in this case */
287 _prepare_service.post (bind (&Butler::prepare, this, weak_ptr<PlayerVideo>(video)));
289 boost::mutex::scoped_lock lm2 (_buffers_mutex);
290 _video.put (video, time);
294 Butler::audio (shared_ptr<AudioBuffers> audio, DCPTime time)
297 boost::mutex::scoped_lock lm (_mutex);
298 if (_pending_seek_position || _disable_audio) {
299 /* Don't store any audio in these cases */
304 boost::mutex::scoped_lock lm2 (_buffers_mutex);
305 _audio.put (remap (audio, _audio_channels, _audio_mapping), time);
308 /** Try to get `frames' frames of audio and copy it into `out'. Silence
309 * will be filled if no audio is available.
310 * @return time of this audio, or unset if there was a buffer underrun.
313 Butler::get_audio (float* out, Frame frames)
315 optional<DCPTime> t = _audio.get (out, _audio_channels, frames);
316 _summon.notify_all ();
321 Butler::disable_audio ()
323 boost::mutex::scoped_lock lm (_mutex);
324 _disable_audio = true;
328 Butler::memory_used () const
330 /* XXX: should also look at _audio.memory_used() */
331 return _video.memory_used();
335 Butler::player_change (ChangeType type, bool frequent)
337 boost::mutex::scoped_lock lm (_mutex);
339 if (type == CHANGE_TYPE_PENDING) {
341 } else if (type == CHANGE_TYPE_DONE) {
343 if (_died || _pending_seek_position || frequent) {
345 _summon.notify_all ();
350 DCPTime next = _video.get().second;
351 if (_awaiting && _awaiting > next) {
352 /* We have recently done a player_changed seek and our buffers haven't been refilled yet,
353 so assume that we're seeking to the same place as last time.
355 seek_to = *_awaiting;
360 seek_unlocked (seek_to, true);
362 } else if (type == CHANGE_TYPE_CANCELLED) {
367 _summon.notify_all ();
371 Butler::text (PlayerText pt, TextType type, optional<DCPTextTrack> track, DCPTimePeriod period)
373 if (type != TEXT_CLOSED_CAPTION) {
377 DCPOMATIC_ASSERT (track);
379 boost::mutex::scoped_lock lm2 (_buffers_mutex);
380 _closed_caption.put (pt, *track, period);