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 alignment Same as above for the `alignment' value.
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,
70 Image::Alignment alignment,
72 bool prepare_only_proxy
76 , _prepare_work (new boost::asio::io_service::work(_prepare_service))
77 , _pending_seek_accurate (false)
81 , _stop_thread (false)
82 , _audio_mapping (audio_mapping)
83 , _audio_channels (audio_channels)
84 , _disable_audio (false)
85 , _pixel_format (pixel_format)
86 , _video_range (video_range)
87 , _alignment (alignment)
89 , _prepare_only_proxy (prepare_only_proxy)
91 _player_video_connection = _player->Video.connect (bind (&Butler::video, this, _1, _2));
92 _player_audio_connection = _player->Audio.connect (bind (&Butler::audio, this, _1, _2, _3));
93 _player_text_connection = _player->Text.connect (bind (&Butler::text, this, _1, _2, _3, _4));
94 /* The butler must hear about things first, otherwise it might not sort out suspensions in time for
95 get_video() to be called in response to this signal.
97 _player_change_connection = _player->Change.connect (bind (&Butler::player_change, this, _1, _2), boost::signals2::at_front);
98 _thread = boost::thread (bind(&Butler::thread, this));
99 #ifdef DCPOMATIC_LINUX
100 pthread_setname_np (_thread.native_handle(), "butler");
103 /* Create some threads to do work on the PlayerVideos we are creating; at present this is used to
104 multi-thread JPEG2000 decoding.
107 LOG_TIMING("start-prepare-threads %1", boost::thread::hardware_concurrency() * 2);
109 for (size_t i = 0; i < boost::thread::hardware_concurrency() * 2; ++i) {
110 _prepare_pool.create_thread (bind (&boost::asio::io_service::run, &_prepare_service));
117 boost::this_thread::disable_interruption dis;
120 boost::mutex::scoped_lock lm (_mutex);
124 _prepare_work.reset ();
125 _prepare_pool.join_all ();
126 _prepare_service.stop ();
128 _thread.interrupt ();
134 /** Caller must hold a lock on _mutex */
136 Butler::should_run () const
138 if (_video.size() >= MAXIMUM_VIDEO_READAHEAD * 10) {
139 /* This is way too big */
140 optional<DCPTime> pos = _audio.peek();
142 throw ProgrammingError
143 (__FILE__, __LINE__, String::compose ("Butler video buffers reached %1 frames (audio is %2 at %3)", _video.size(), _audio.size(), pos->get()));
145 throw ProgrammingError
146 (__FILE__, __LINE__, String::compose ("Butler video buffers reached %1 frames (audio is %2)", _video.size(), _audio.size()));
150 if (_audio.size() >= MAXIMUM_AUDIO_READAHEAD * 10) {
151 /* This is way too big */
152 auto pos = _audio.peek();
154 throw ProgrammingError
155 (__FILE__, __LINE__, String::compose ("Butler audio buffers reached %1 frames at %2 (video is %3)", _audio.size(), pos->get(), _video.size()));
157 throw ProgrammingError
158 (__FILE__, __LINE__, String::compose ("Butler audio buffers reached %1 frames (video is %3)", _audio.size(), _video.size()));
162 if (_video.size() >= MAXIMUM_VIDEO_READAHEAD * 2) {
163 LOG_WARNING ("Butler video buffers reached %1 frames (audio is %2)", _video.size(), _audio.size());
166 if (_audio.size() >= MAXIMUM_AUDIO_READAHEAD * 2) {
167 LOG_WARNING ("Butler audio buffers reached %1 frames (video is %2)", _audio.size(), _video.size());
170 if (_stop_thread || _finished || _died || _suspended) {
171 /* Definitely do not run */
175 if (_video.size() < MINIMUM_VIDEO_READAHEAD || (!_disable_audio && _audio.size() < MINIMUM_AUDIO_READAHEAD)) {
176 /* Definitely do run: we need data */
180 /* Run if we aren't full of video or audio */
181 return (_video.size() < MAXIMUM_VIDEO_READAHEAD) && (_audio.size() < MAXIMUM_AUDIO_READAHEAD);
189 start_of_thread ("Butler");
192 boost::mutex::scoped_lock lm (_mutex);
194 /* Wait until we have something to do */
195 while (!should_run() && !_pending_seek_position) {
199 /* Do any seek that has been requested */
200 if (_pending_seek_position) {
202 _player->seek (*_pending_seek_position, _pending_seek_accurate);
203 _pending_seek_position = optional<DCPTime> ();
206 /* Fill _video and _audio. Don't try to carry on if a pending seek appears
207 while lm is unlocked, as in that state nothing will be added to
210 while (should_run() && !_pending_seek_position) {
212 bool const r = _player->pass ();
216 _arrived.notify_all ();
219 _arrived.notify_all ();
222 } catch (boost::thread_interrupted) {
223 /* The butler thread is being terminated */
224 boost::mutex::scoped_lock lm (_mutex);
226 _arrived.notify_all ();
227 } catch (std::exception& e) {
229 boost::mutex::scoped_lock lm (_mutex);
231 _died_message = e.what ();
232 _arrived.notify_all ();
235 boost::mutex::scoped_lock lm (_mutex);
237 _arrived.notify_all ();
241 /** @param behaviour BLOCKING if we should block until video is available. If behaviour is NON_BLOCKING
242 * and no video is immediately available the method will return a 0 PlayerVideo and the error AGAIN.
243 * @param e if non-0 this is filled with an error code (if an error occurs) or is untouched if no error occurs.
245 pair<shared_ptr<PlayerVideo>, DCPTime>
246 Butler::get_video (Behaviour behaviour, Error* e)
248 boost::mutex::scoped_lock lm (_mutex);
250 auto setup_error = [this](Error* e, Error::Code fallback) {
253 e->code = Error::Code::DIED;
254 e->message = _died_message;
255 } else if (_finished) {
256 e->code = Error::Code::FINISHED;
263 if (_video.empty() && (_finished || _died || (_suspended && behaviour == Behaviour::NON_BLOCKING))) {
264 setup_error (e, Error::Code::AGAIN);
265 return make_pair(shared_ptr<PlayerVideo>(), DCPTime());
268 /* Wait for data if we have none */
269 while (_video.empty() && !_finished && !_died) {
273 if (_video.empty()) {
274 setup_error (e, Error::Code::NONE);
275 return make_pair(shared_ptr<PlayerVideo>(), DCPTime());
278 auto const r = _video.get ();
279 _summon.notify_all ();
284 optional<TextRingBuffers::Data>
285 Butler::get_closed_caption ()
287 boost::mutex::scoped_lock lm (_mutex);
288 return _closed_caption.get ();
293 Butler::seek (DCPTime position, bool accurate)
295 boost::mutex::scoped_lock lm (_mutex);
296 _awaiting = optional<DCPTime>();
297 seek_unlocked (position, accurate);
302 Butler::seek_unlocked (DCPTime position, bool accurate)
309 _pending_seek_position = position;
310 _pending_seek_accurate = accurate;
314 _closed_caption.clear ();
316 _summon.notify_all ();
321 Butler::prepare (weak_ptr<PlayerVideo> weak_video)
324 auto video = weak_video.lock ();
325 /* If the weak_ptr cannot be locked the video obviously no longer requires any work */
327 LOG_TIMING("start-prepare in %1", thread_id());
328 video->prepare (_pixel_format, _video_range, _alignment, _fast, _prepare_only_proxy);
329 LOG_TIMING("finish-prepare in %1", thread_id());
332 catch (std::exception& e)
335 boost::mutex::scoped_lock lm (_mutex);
337 _died_message = e.what ();
342 boost::mutex::scoped_lock lm (_mutex);
348 Butler::video (shared_ptr<PlayerVideo> video, DCPTime time)
350 boost::mutex::scoped_lock lm (_mutex);
352 if (_pending_seek_position) {
353 /* Don't store any video in this case */
357 _prepare_service.post (bind(&Butler::prepare, this, weak_ptr<PlayerVideo>(video)));
359 _video.put (video, time);
364 Butler::audio (shared_ptr<AudioBuffers> audio, DCPTime time, int frame_rate)
366 boost::mutex::scoped_lock lm (_mutex);
367 if (_pending_seek_position || _disable_audio) {
368 /* Don't store any audio in these cases */
372 _audio.put (remap(audio, _audio_channels, _audio_mapping), time, frame_rate);
376 /** Try to get `frames' frames of audio and copy it into `out'.
377 * @param behaviour BLOCKING if we should block until audio is available. If behaviour is NON_BLOCKING
378 * and no audio is immediately available the buffer will be filled with silence and boost::none
380 * @return time of this audio, or unset if blocking was false and no data was available.
383 Butler::get_audio (Behaviour behaviour, float* out, Frame frames)
385 boost::mutex::scoped_lock lm (_mutex);
387 while (behaviour == Behaviour::BLOCKING && !_finished && !_died && _audio.size() < frames) {
391 auto t = _audio.get (out, _audio_channels, frames);
392 _summon.notify_all ();
398 Butler::disable_audio ()
400 boost::mutex::scoped_lock lm (_mutex);
401 _disable_audio = true;
406 Butler::memory_used () const
408 /* XXX: should also look at _audio.memory_used() */
409 return _video.memory_used();
414 Butler::player_change (ChangeType type, int property)
416 if (property == VideoContentProperty::CROP) {
417 if (type == ChangeType::DONE) {
418 auto film = _film.lock();
420 _video.reset_metadata (film, _player->video_container_size());
426 boost::mutex::scoped_lock lm (_mutex);
428 if (type == ChangeType::PENDING) {
430 } else if (type == ChangeType::DONE) {
432 if (_died || _pending_seek_position) {
434 _summon.notify_all ();
439 auto next = _video.get().second;
440 if (_awaiting && _awaiting > next) {
441 /* We have recently done a player_changed seek and our buffers haven't been refilled yet,
442 so assume that we're seeking to the same place as last time.
444 seek_to = *_awaiting;
449 seek_unlocked (seek_to, true);
451 } else if (type == ChangeType::CANCELLED) {
456 _summon.notify_all ();
461 Butler::text (PlayerText pt, TextType type, optional<DCPTextTrack> track, DCPTimePeriod period)
463 if (type != TextType::CLOSED_CAPTION) {
467 DCPOMATIC_ASSERT (track);
469 _closed_caption.put (pt, *track, period);
474 Butler::Error::summary () const
478 case Error::Code::NONE:
479 return "No error registered";
480 case Error::Code::AGAIN:
481 return "Butler not ready";
482 case Error::Code::DIED:
483 return String::compose("Butler died (%1)", message);
484 case Error::Code::FINISHED:
485 return "Butler finished";