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/>.
23 #include "compose.hpp"
25 #include "dcpomatic_log.h"
26 #include "exceptions.h"
30 #include "video_content.h"
37 using std::shared_ptr;
41 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,
66 AudioMapping audio_mapping,
68 function<AVPixelFormat (AVPixelFormat)> pixel_format,
69 VideoRange video_range,
70 Image::Alignment alignment,
72 bool prepare_only_proxy,
77 , _prepare_work (new boost::asio::io_service::work(_prepare_service))
78 , _pending_seek_accurate (false)
82 , _stop_thread (false)
83 , _audio_mapping (audio_mapping)
84 , _audio_channels (audio_channels)
85 , _disable_audio (audio == Audio::DISABLED)
86 , _pixel_format (pixel_format)
87 , _video_range (video_range)
88 , _alignment (alignment)
90 , _prepare_only_proxy (prepare_only_proxy)
92 _player_video_connection = _player.Video.connect(bind(&Butler::video, this, _1, _2));
93 _player_audio_connection = _player.Audio.connect(bind(&Butler::audio, this, _1, _2, _3));
94 _player_text_connection = _player.Text.connect(bind(&Butler::text, this, _1, _2, _3, _4));
95 /* The butler must hear about things first, otherwise it might not sort out suspensions in time for
96 get_video() to be called in response to this signal.
98 _player_change_connection = _player.Change.connect(bind(&Butler::player_change, this, _1, _2), boost::signals2::at_front);
99 _thread = boost::thread (bind(&Butler::thread, this));
100 #ifdef DCPOMATIC_LINUX
101 pthread_setname_np (_thread.native_handle(), "butler");
104 /* Create some threads to do work on the PlayerVideos we are creating; at present this is used to
105 multi-thread JPEG2000 decoding.
108 LOG_TIMING("start-prepare-threads %1", boost::thread::hardware_concurrency() * 2);
110 for (size_t i = 0; i < boost::thread::hardware_concurrency() * 2; ++i) {
111 _prepare_pool.create_thread (bind (&boost::asio::io_service::run, &_prepare_service));
118 boost::this_thread::disable_interruption dis;
121 boost::mutex::scoped_lock lm (_mutex);
125 _prepare_work.reset ();
126 _prepare_pool.join_all ();
127 _prepare_service.stop ();
129 _thread.interrupt ();
135 /** Caller must hold a lock on _mutex */
137 Butler::should_run () const
139 if (_video.size() >= MAXIMUM_VIDEO_READAHEAD * 10) {
140 /* This is way too big */
141 optional<DCPTime> pos = _audio.peek();
143 throw ProgrammingError
144 (__FILE__, __LINE__, String::compose ("Butler video buffers reached %1 frames (audio is %2 at %3)", _video.size(), _audio.size(), pos->get()));
146 throw ProgrammingError
147 (__FILE__, __LINE__, String::compose ("Butler video buffers reached %1 frames (audio is %2)", _video.size(), _audio.size()));
151 if (_audio.size() >= MAXIMUM_AUDIO_READAHEAD * 10) {
152 /* This is way too big */
153 auto pos = _audio.peek();
155 throw ProgrammingError
156 (__FILE__, __LINE__, String::compose ("Butler audio buffers reached %1 frames at %2 (video is %3)", _audio.size(), pos->get(), _video.size()));
158 throw ProgrammingError
159 (__FILE__, __LINE__, String::compose ("Butler audio buffers reached %1 frames (video is %3)", _audio.size(), _video.size()));
163 if (_video.size() >= MAXIMUM_VIDEO_READAHEAD * 2) {
164 LOG_WARNING ("Butler video buffers reached %1 frames (audio is %2)", _video.size(), _audio.size());
167 if (_audio.size() >= MAXIMUM_AUDIO_READAHEAD * 2) {
168 LOG_WARNING ("Butler audio buffers reached %1 frames (video is %2)", _audio.size(), _video.size());
171 if (_stop_thread || _finished || _died || _suspended) {
172 /* Definitely do not run */
176 if (_video.size() < MINIMUM_VIDEO_READAHEAD || (!_disable_audio && _audio.size() < MINIMUM_AUDIO_READAHEAD)) {
177 /* Definitely do run: we need data */
181 /* Run if we aren't full of video or audio */
182 return (_video.size() < MAXIMUM_VIDEO_READAHEAD) && (_audio.size() < MAXIMUM_AUDIO_READAHEAD);
190 start_of_thread ("Butler");
193 boost::mutex::scoped_lock lm (_mutex);
195 /* Wait until we have something to do */
196 while (!should_run() && !_pending_seek_position) {
200 /* Do any seek that has been requested */
201 if (_pending_seek_position) {
203 _player.seek(*_pending_seek_position, _pending_seek_accurate);
204 _pending_seek_position = optional<DCPTime> ();
207 /* Fill _video and _audio. Don't try to carry on if a pending seek appears
208 while lm is unlocked, as in that state nothing will be added to
211 while (should_run() && !_pending_seek_position) {
213 bool const r = _player.pass();
217 _arrived.notify_all ();
220 _arrived.notify_all ();
223 } catch (boost::thread_interrupted) {
224 /* The butler thread is being terminated */
225 boost::mutex::scoped_lock lm (_mutex);
227 _arrived.notify_all ();
228 } catch (std::exception& e) {
230 boost::mutex::scoped_lock lm (_mutex);
232 _died_message = e.what ();
233 _arrived.notify_all ();
236 boost::mutex::scoped_lock lm (_mutex);
238 _arrived.notify_all ();
242 /** @param behaviour BLOCKING if we should block until video is available. If behaviour is NON_BLOCKING
243 * and no video is immediately available the method will return a 0 PlayerVideo and the error AGAIN.
244 * @param e if non-0 this is filled with an error code (if an error occurs) or is untouched if no error occurs.
246 pair<shared_ptr<PlayerVideo>, DCPTime>
247 Butler::get_video (Behaviour behaviour, Error* e)
249 boost::mutex::scoped_lock lm (_mutex);
251 auto setup_error = [this](Error* e, Error::Code fallback) {
254 e->code = Error::Code::DIED;
255 e->message = _died_message;
256 } else if (_finished) {
257 e->code = Error::Code::FINISHED;
264 if (_video.empty() && (_finished || _died || (_suspended && behaviour == Behaviour::NON_BLOCKING))) {
265 setup_error (e, Error::Code::AGAIN);
266 return make_pair(shared_ptr<PlayerVideo>(), DCPTime());
269 /* Wait for data if we have none */
270 while (_video.empty() && !_finished && !_died) {
274 if (_video.empty()) {
275 setup_error (e, Error::Code::NONE);
276 return make_pair(shared_ptr<PlayerVideo>(), DCPTime());
279 auto const r = _video.get ();
280 _summon.notify_all ();
285 optional<TextRingBuffers::Data>
286 Butler::get_closed_caption ()
288 boost::mutex::scoped_lock lm (_mutex);
289 return _closed_caption.get ();
294 Butler::seek (DCPTime position, bool accurate)
296 boost::mutex::scoped_lock lm (_mutex);
297 _awaiting = optional<DCPTime>();
298 seek_unlocked (position, accurate);
303 Butler::seek_unlocked (DCPTime position, bool accurate)
310 _pending_seek_position = position;
311 _pending_seek_accurate = accurate;
315 _closed_caption.clear ();
317 _summon.notify_all ();
322 Butler::prepare (weak_ptr<PlayerVideo> weak_video)
325 auto video = weak_video.lock ();
326 /* If the weak_ptr cannot be locked the video obviously no longer requires any work */
328 LOG_TIMING("start-prepare in %1", thread_id());
329 video->prepare (_pixel_format, _video_range, _alignment, _fast, _prepare_only_proxy);
330 LOG_TIMING("finish-prepare in %1", thread_id());
333 catch (std::exception& e)
336 boost::mutex::scoped_lock lm (_mutex);
338 _died_message = e.what ();
343 boost::mutex::scoped_lock lm (_mutex);
349 Butler::video (shared_ptr<PlayerVideo> video, DCPTime time)
351 boost::mutex::scoped_lock lm (_mutex);
353 if (_pending_seek_position) {
354 /* Don't store any video in this case */
358 _prepare_service.post (bind(&Butler::prepare, this, weak_ptr<PlayerVideo>(video)));
360 _video.put (video, time);
365 Butler::audio (shared_ptr<AudioBuffers> audio, DCPTime time, int frame_rate)
367 boost::mutex::scoped_lock lm (_mutex);
368 if (_pending_seek_position || _disable_audio) {
369 /* Don't store any audio in these cases */
373 _audio.put (remap(audio, _audio_channels, _audio_mapping), time, frame_rate);
377 /** Try to get `frames' frames of audio and copy it into `out'.
378 * @param behaviour BLOCKING if we should block until audio is available. If behaviour is NON_BLOCKING
379 * and no audio is immediately available the buffer will be filled with silence and boost::none
381 * @return time of this audio, or unset if blocking was false and no data was available.
384 Butler::get_audio (Behaviour behaviour, float* out, Frame frames)
386 boost::mutex::scoped_lock lm (_mutex);
388 while (behaviour == Behaviour::BLOCKING && !_finished && !_died && _audio.size() < frames) {
392 auto t = _audio.get (out, _audio_channels, frames);
393 _summon.notify_all ();
399 Butler::memory_used () const
401 /* XXX: should also look at _audio.memory_used() */
402 return _video.memory_used();
407 Butler::player_change (ChangeType type, int property)
409 if (property == VideoContentProperty::CROP) {
410 if (type == ChangeType::DONE) {
411 auto film = _film.lock();
413 _video.reset_metadata(film, _player.video_container_size());
419 boost::mutex::scoped_lock lm (_mutex);
421 if (type == ChangeType::PENDING) {
423 } else if (type == ChangeType::DONE) {
425 if (_died || _pending_seek_position) {
427 _summon.notify_all ();
432 auto next = _video.get().second;
433 if (_awaiting && _awaiting > next) {
434 /* We have recently done a player_changed seek and our buffers haven't been refilled yet,
435 so assume that we're seeking to the same place as last time.
437 seek_to = *_awaiting;
442 seek_unlocked (seek_to, true);
444 } else if (type == ChangeType::CANCELLED) {
449 _summon.notify_all ();
454 Butler::text (PlayerText pt, TextType type, optional<DCPTextTrack> track, DCPTimePeriod period)
456 if (type != TextType::CLOSED_CAPTION) {
460 DCPOMATIC_ASSERT (track);
462 _closed_caption.put (pt, *track, period);
467 Butler::Error::summary () const
471 case Error::Code::NONE:
472 return "No error registered";
473 case Error::Code::AGAIN:
474 return "Butler not ready";
475 case Error::Code::DIED:
476 return String::compose("Butler died (%1)", message);
477 case Error::Code::FINISHED:
478 return "Butler finished";