2 Copyright (C) 2016-2021 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 "dcpomatic_log.h"
28 #include "compose.hpp"
29 #include "exceptions.h"
30 #include "video_content.h"
38 using std::shared_ptr;
40 using boost::optional;
42 using namespace dcpomatic;
43 #if BOOST_VERSION >= 106100
44 using namespace boost::placeholders;
48 /** Minimum video readahead in frames */
49 #define MINIMUM_VIDEO_READAHEAD 10
50 /** Maximum video readahead in frames; should never be exceeded (by much) unless there are bugs in Player */
51 #define MAXIMUM_VIDEO_READAHEAD 48
52 /** Minimum audio readahead in frames */
53 #define MINIMUM_AUDIO_READAHEAD (48000 * MINIMUM_VIDEO_READAHEAD / 24)
54 /** Maximum audio readahead in frames; should never be exceeded (by much) unless there are bugs in Player */
55 #define MAXIMUM_AUDIO_READAHEAD (48000 * MAXIMUM_VIDEO_READAHEAD / 24)
58 /** @param pixel_format Pixel format functor that will be used when calling ::image on PlayerVideos coming out of this
59 * butler. This will be used (where possible) to prepare the PlayerVideos so that calling image() on them is quick.
60 * @param aligned Same as above for the `aligned' flag.
61 * @param fast Same as above for the `fast' flag.
64 weak_ptr<const Film> film,
65 shared_ptr<Player> player,
66 AudioMapping audio_mapping,
68 function<AVPixelFormat (AVPixelFormat)> pixel_format,
69 VideoRange video_range,
75 , _prepare_work (new boost::asio::io_service::work(_prepare_service))
76 , _pending_seek_accurate (false)
80 , _stop_thread (false)
81 , _audio_mapping (audio_mapping)
82 , _audio_channels (audio_channels)
83 , _disable_audio (false)
84 , _pixel_format (pixel_format)
85 , _video_range (video_range)
89 _player_video_connection = _player->Video.connect (bind (&Butler::video, this, _1, _2));
90 _player_audio_connection = _player->Audio.connect (bind (&Butler::audio, this, _1, _2, _3));
91 _player_text_connection = _player->Text.connect (bind (&Butler::text, this, _1, _2, _3, _4));
92 /* The butler must hear about things first, otherwise it might not sort out suspensions in time for
93 get_video() to be called in response to this signal.
95 _player_change_connection = _player->Change.connect (bind (&Butler::player_change, this, _1, _2), boost::signals2::at_front);
96 _thread = boost::thread (bind(&Butler::thread, this));
97 #ifdef DCPOMATIC_LINUX
98 pthread_setname_np (_thread.native_handle(), "butler");
101 /* Create some threads to do work on the PlayerVideos we are creating; at present this is used to
102 multi-thread JPEG2000 decoding.
105 LOG_TIMING("start-prepare-threads %1", boost::thread::hardware_concurrency() * 2);
107 for (size_t i = 0; i < boost::thread::hardware_concurrency() * 2; ++i) {
108 _prepare_pool.create_thread (bind (&boost::asio::io_service::run, &_prepare_service));
115 boost::this_thread::disable_interruption dis;
118 boost::mutex::scoped_lock lm (_mutex);
122 _prepare_work.reset ();
123 _prepare_pool.join_all ();
124 _prepare_service.stop ();
126 _thread.interrupt ();
132 /** Caller must hold a lock on _mutex */
134 Butler::should_run () const
136 if (_video.size() >= MAXIMUM_VIDEO_READAHEAD * 10) {
137 /* This is way too big */
138 optional<DCPTime> pos = _audio.peek();
140 throw ProgrammingError
141 (__FILE__, __LINE__, String::compose ("Butler video buffers reached %1 frames (audio is %2 at %3)", _video.size(), _audio.size(), pos->get()));
143 throw ProgrammingError
144 (__FILE__, __LINE__, String::compose ("Butler video buffers reached %1 frames (audio is %2)", _video.size(), _audio.size()));
148 if (_audio.size() >= MAXIMUM_AUDIO_READAHEAD * 10) {
149 /* This is way too big */
150 optional<DCPTime> pos = _audio.peek();
152 throw ProgrammingError
153 (__FILE__, __LINE__, String::compose ("Butler audio buffers reached %1 frames at %2 (video is %3)", _audio.size(), pos->get(), _video.size()));
155 throw ProgrammingError
156 (__FILE__, __LINE__, String::compose ("Butler audio buffers reached %1 frames (video is %3)", _audio.size(), _video.size()));
160 if (_video.size() >= MAXIMUM_VIDEO_READAHEAD * 2) {
161 LOG_WARNING ("Butler video buffers reached %1 frames (audio is %2)", _video.size(), _audio.size());
164 if (_audio.size() >= MAXIMUM_AUDIO_READAHEAD * 2) {
165 LOG_WARNING ("Butler audio buffers reached %1 frames (video is %2)", _audio.size(), _video.size());
168 if (_stop_thread || _finished || _died || _suspended) {
169 /* Definitely do not run */
173 if (_video.size() < MINIMUM_VIDEO_READAHEAD || (!_disable_audio && _audio.size() < MINIMUM_AUDIO_READAHEAD)) {
174 /* Definitely do run: we need data */
178 /* Run if we aren't full of video or audio */
179 return (_video.size() < MAXIMUM_VIDEO_READAHEAD) && (_audio.size() < MAXIMUM_AUDIO_READAHEAD);
187 start_of_thread ("Butler");
190 boost::mutex::scoped_lock lm (_mutex);
192 /* Wait until we have something to do */
193 while (!should_run() && !_pending_seek_position) {
197 /* Do any seek that has been requested */
198 if (_pending_seek_position) {
200 _player->seek (*_pending_seek_position, _pending_seek_accurate);
201 _pending_seek_position = optional<DCPTime> ();
204 /* Fill _video and _audio. Don't try to carry on if a pending seek appears
205 while lm is unlocked, as in that state nothing will be added to
208 while (should_run() && !_pending_seek_position) {
210 bool const r = _player->pass ();
214 _arrived.notify_all ();
217 _arrived.notify_all ();
220 } catch (boost::thread_interrupted) {
221 /* The butler thread is being terminated */
222 boost::mutex::scoped_lock lm (_mutex);
224 _arrived.notify_all ();
225 } catch (std::exception& e) {
227 boost::mutex::scoped_lock lm (_mutex);
229 _died_message = e.what ();
230 _arrived.notify_all ();
233 boost::mutex::scoped_lock lm (_mutex);
235 _arrived.notify_all ();
239 /** @param blocking true if we should block until video is available. If blocking is false
240 * and no video is immediately available the method will return a 0 PlayerVideo and the error AGAIN.
241 * @param e if non-0 this is filled with an error code (if an error occurs) or is untouched if no error occurs.
243 pair<shared_ptr<PlayerVideo>, DCPTime>
244 Butler::get_video (bool blocking, Error* e)
246 boost::mutex::scoped_lock lm (_mutex);
248 auto setup_error = [this](Error* e, Error::Code fallback) {
251 e->code = Error::DIED;
252 e->message = _died_message;
253 } else if (_finished) {
254 e->code = Error::FINISHED;
261 if (_video.empty() && (_finished || _died || (_suspended && !blocking))) {
262 setup_error (e, Error::AGAIN);
263 return make_pair(shared_ptr<PlayerVideo>(), DCPTime());
266 /* Wait for data if we have none */
267 while (_video.empty() && !_finished && !_died) {
271 if (_video.empty()) {
272 setup_error (e, Error::NONE);
273 return make_pair(shared_ptr<PlayerVideo>(), DCPTime());
276 auto const r = _video.get ();
277 _summon.notify_all ();
282 optional<TextRingBuffers::Data>
283 Butler::get_closed_caption ()
285 boost::mutex::scoped_lock lm (_mutex);
286 return _closed_caption.get ();
291 Butler::seek (DCPTime position, bool accurate)
293 boost::mutex::scoped_lock lm (_mutex);
294 _awaiting = optional<DCPTime>();
295 seek_unlocked (position, accurate);
300 Butler::seek_unlocked (DCPTime position, bool accurate)
307 _pending_seek_position = position;
308 _pending_seek_accurate = accurate;
312 _closed_caption.clear ();
314 _summon.notify_all ();
319 Butler::prepare (weak_ptr<PlayerVideo> weak_video)
322 auto video = weak_video.lock ();
323 /* If the weak_ptr cannot be locked the video obviously no longer requires any work */
325 LOG_TIMING("start-prepare in %1", thread_id());
326 video->prepare (_pixel_format, _video_range, _aligned, _fast);
327 LOG_TIMING("finish-prepare in %1", thread_id());
330 catch (std::exception& e)
333 boost::mutex::scoped_lock lm (_mutex);
335 _died_message = e.what ();
340 boost::mutex::scoped_lock lm (_mutex);
346 Butler::video (shared_ptr<PlayerVideo> video, DCPTime time)
348 boost::mutex::scoped_lock lm (_mutex);
350 if (_pending_seek_position) {
351 /* Don't store any video in this case */
355 _prepare_service.post (bind(&Butler::prepare, this, weak_ptr<PlayerVideo>(video)));
357 _video.put (video, time);
362 Butler::audio (shared_ptr<AudioBuffers> audio, DCPTime time, int frame_rate)
364 boost::mutex::scoped_lock lm (_mutex);
365 if (_pending_seek_position || _disable_audio) {
366 /* Don't store any audio in these cases */
370 _audio.put (remap(audio, _audio_channels, _audio_mapping), time, frame_rate);
374 /** Try to get `frames' frames of audio and copy it into `out'. Silence
375 * will be filled if no audio is available.
376 * @return time of this audio, or unset if there was a buffer underrun.
379 Butler::get_audio (float* out, Frame frames)
381 auto t = _audio.get (out, _audio_channels, frames);
382 _summon.notify_all ();
388 Butler::disable_audio ()
390 boost::mutex::scoped_lock lm (_mutex);
391 _disable_audio = true;
396 Butler::memory_used () const
398 /* XXX: should also look at _audio.memory_used() */
399 return _video.memory_used();
404 Butler::player_change (ChangeType type, int property)
406 if (property == VideoContentProperty::CROP) {
407 if (type == ChangeType::DONE) {
408 auto film = _film.lock();
410 _video.reset_metadata (film, _player->video_container_size());
416 boost::mutex::scoped_lock lm (_mutex);
418 if (type == ChangeType::PENDING) {
420 } else if (type == ChangeType::DONE) {
422 if (_died || _pending_seek_position) {
424 _summon.notify_all ();
429 auto next = _video.get().second;
430 if (_awaiting && _awaiting > next) {
431 /* We have recently done a player_changed seek and our buffers haven't been refilled yet,
432 so assume that we're seeking to the same place as last time.
434 seek_to = *_awaiting;
439 seek_unlocked (seek_to, true);
441 } else if (type == ChangeType::CANCELLED) {
446 _summon.notify_all ();
451 Butler::text (PlayerText pt, TextType type, optional<DCPTextTrack> track, DCPTimePeriod period)
453 if (type != TextType::CLOSED_CAPTION) {
457 DCPOMATIC_ASSERT (track);
459 _closed_caption.put (pt, *track, period);
464 Butler::Error::summary () const
469 return "No error registered";
471 return "Butler not ready";
473 return String::compose("Butler died (%1)", message);
474 case Error::FINISHED:
475 return "Butler finished";