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 auto check_video_much_too_big = [this](VideoType type) {
140 if (_video[type].size() >= MAXIMUM_VIDEO_READAHEAD * 10) {
141 /* This is way too big */
142 auto pos = _audio.peek();
144 throw ProgrammingError
145 (__FILE__, __LINE__, String::compose("Butler video (%1) buffers reached %2 frames (audio is %3 at %4)", video_type_to_string(type), _video[type].size(), _audio.size(), pos->get()));
147 throw ProgrammingError
148 (__FILE__, __LINE__, String::compose("Butler video (%1) buffers reached %2 frames (audio is %3)", video_type_to_string(type), _video[type].size(), _audio.size()));
153 check_video_much_too_big(VideoType::MAIN);
154 check_video_much_too_big(VideoType::SIGN_LANGUAGE);
156 if (_audio.size() >= MAXIMUM_AUDIO_READAHEAD * 10) {
157 /* This is way too big */
158 auto pos = _audio.peek();
160 throw ProgrammingError
161 (__FILE__, __LINE__, String::compose ("Butler audio buffers reached %1 frames at %2 (main video is %3)", _audio.size(), pos->get(), _video[VideoType::MAIN].size()));
163 throw ProgrammingError
164 (__FILE__, __LINE__, String::compose ("Butler audio buffers reached %1 frames (main video is %3)", _audio.size(), _video[VideoType::MAIN].size()));
168 auto check_video_too_big = [this](VideoType type) {
169 if (_video[type].size() >= MAXIMUM_VIDEO_READAHEAD * 2) {
170 LOG_WARNING ("Butler video buffers reached %1 frames (audio is %2)", _video[type].size(), _audio.size());
174 check_video_too_big(VideoType::MAIN);
175 check_video_too_big(VideoType::SIGN_LANGUAGE);
177 if (_audio.size() >= MAXIMUM_AUDIO_READAHEAD * 2) {
178 LOG_WARNING ("Butler audio buffers reached %1 frames (main video is %2)", _audio.size(), _video[VideoType::MAIN].size());
181 if (_stop_thread || _finished || _died || _suspended) {
182 /* Definitely do not run */
186 if (_video[VideoType::MAIN].size() < MINIMUM_VIDEO_READAHEAD
187 || (_player.have_sign_language() && _video[VideoType::SIGN_LANGUAGE].size() < MINIMUM_VIDEO_READAHEAD)
188 || (!_disable_audio && _audio.size() < MINIMUM_AUDIO_READAHEAD)
190 /* Definitely do run: we need data */
194 /* Run if we aren't full of video or audio */
195 return _video[VideoType::MAIN].size() < MAXIMUM_VIDEO_READAHEAD
196 && _video[VideoType::SIGN_LANGUAGE].size() < MAXIMUM_VIDEO_READAHEAD
197 && (_audio.size() < MAXIMUM_AUDIO_READAHEAD);
205 start_of_thread ("Butler");
208 boost::mutex::scoped_lock lm (_mutex);
210 /* Wait until we have something to do */
211 while (!should_run() && !_pending_seek_position) {
215 /* Do any seek that has been requested */
216 if (_pending_seek_position) {
218 _player.seek(*_pending_seek_position, _pending_seek_accurate);
219 _pending_seek_position = optional<DCPTime> ();
222 /* Fill _video and _audio. Don't try to carry on if a pending seek appears
223 while lm is unlocked, as in that state nothing will be added to
226 while (should_run() && !_pending_seek_position) {
228 bool const r = _player.pass();
232 _arrived.notify_all ();
235 _arrived.notify_all ();
238 } catch (boost::thread_interrupted) {
239 /* The butler thread is being terminated */
240 boost::mutex::scoped_lock lm (_mutex);
242 _arrived.notify_all ();
243 } catch (std::exception& e) {
245 boost::mutex::scoped_lock lm (_mutex);
247 _died_message = e.what ();
248 _arrived.notify_all ();
251 boost::mutex::scoped_lock lm (_mutex);
253 _arrived.notify_all ();
257 /** @param behaviour BLOCKING if we should block until video is available. If behaviour is NON_BLOCKING
258 * and no video is immediately available the method will return a 0 PlayerVideo and the error AGAIN.
259 * @param e if non-0 this is filled with an error code (if an error occurs) or is untouched if no error occurs.
261 pair<shared_ptr<PlayerVideo>, DCPTime>
262 Butler::get_video(VideoType type, Behaviour behaviour, Error* e)
264 boost::mutex::scoped_lock lm (_mutex);
266 auto setup_error = [this](Error* e, Error::Code fallback) {
269 e->code = Error::Code::DIED;
270 e->message = _died_message;
271 } else if (_finished) {
272 e->code = Error::Code::FINISHED;
279 if (_video[type].empty() && (_finished || _died || (_suspended && behaviour == Behaviour::NON_BLOCKING))) {
280 setup_error (e, Error::Code::AGAIN);
284 /* Wait for data if we have none */
285 while (_video[type].empty() && !_finished && !_died) {
289 if (_video[type].empty()) {
290 setup_error (e, Error::Code::NONE);
294 auto const r = _video[type].get();
295 _summon.notify_all ();
300 optional<TextRingBuffers::Data>
301 Butler::get_closed_caption ()
303 boost::mutex::scoped_lock lm (_mutex);
304 return _closed_caption.get ();
309 Butler::seek (DCPTime position, bool accurate)
311 boost::mutex::scoped_lock lm (_mutex);
312 _awaiting = optional<DCPTime>();
313 seek_unlocked (position, accurate);
318 Butler::seek_unlocked (DCPTime position, bool accurate)
325 _pending_seek_position = position;
326 _pending_seek_accurate = accurate;
328 _video[VideoType::MAIN].clear();
329 _video[VideoType::SIGN_LANGUAGE].clear();
331 _closed_caption.clear ();
333 _summon.notify_all ();
338 Butler::prepare (weak_ptr<PlayerVideo> weak_video)
341 auto video = weak_video.lock ();
342 /* If the weak_ptr cannot be locked the video obviously no longer requires any work */
344 LOG_TIMING("start-prepare in %1", thread_id());
345 video->prepare (_pixel_format, _video_range, _alignment, _fast, _prepare_only_proxy);
346 LOG_TIMING("finish-prepare in %1", thread_id());
349 catch (std::exception& e)
352 boost::mutex::scoped_lock lm (_mutex);
354 _died_message = e.what ();
359 boost::mutex::scoped_lock lm (_mutex);
365 Butler::video (shared_ptr<PlayerVideo> video, DCPTime time)
367 boost::mutex::scoped_lock lm (_mutex);
369 if (_pending_seek_position) {
370 /* Don't store any video in this case */
374 _prepare_service.post (bind(&Butler::prepare, this, weak_ptr<PlayerVideo>(video)));
376 _video[video->type()].put(video, time);
381 Butler::audio (shared_ptr<AudioBuffers> audio, DCPTime time, int frame_rate)
383 boost::mutex::scoped_lock lm (_mutex);
384 if (_pending_seek_position || _disable_audio) {
385 /* Don't store any audio in these cases */
389 _audio.put (remap(audio, _audio_channels, _audio_mapping), time, frame_rate);
393 /** Try to get `frames' frames of audio and copy it into `out'.
394 * @param behaviour BLOCKING if we should block until audio is available. If behaviour is NON_BLOCKING
395 * and no audio is immediately available the buffer will be filled with silence and boost::none
397 * @return time of this audio, or unset if blocking was false and no data was available.
400 Butler::get_audio (Behaviour behaviour, float* out, Frame frames)
402 boost::mutex::scoped_lock lm (_mutex);
404 while (behaviour == Behaviour::BLOCKING && !_finished && !_died && _audio.size() < frames) {
408 auto t = _audio.get (out, _audio_channels, frames);
409 _summon.notify_all ();
415 Butler::memory_used () const
417 /* XXX: should also look at _audio and _video[VideoType::SIGN_LANGUAGE] */
418 return _video[VideoType::MAIN].memory_used();
423 Butler::player_change (ChangeType type, int property)
425 if (property == VideoContentProperty::CROP) {
426 if (type == ChangeType::DONE) {
427 auto film = _film.lock();
429 _video[VideoType::MAIN].reset_metadata(film, _player.video_container_size(VideoType::MAIN));
430 _video[VideoType::SIGN_LANGUAGE].reset_metadata(film, _player.video_container_size(VideoType::SIGN_LANGUAGE));
436 boost::mutex::scoped_lock lm (_mutex);
438 if (type == ChangeType::PENDING) {
440 } else if (type == ChangeType::DONE) {
442 if (_died || _pending_seek_position) {
444 _summon.notify_all ();
449 auto next = _video[VideoType::MAIN].get().second;
450 if (_awaiting && _awaiting > next) {
451 /* We have recently done a player_changed seek and our buffers haven't been refilled yet,
452 so assume that we're seeking to the same place as last time.
454 seek_to = *_awaiting;
459 seek_unlocked (seek_to, true);
461 } else if (type == ChangeType::CANCELLED) {
466 _summon.notify_all ();
471 Butler::text (PlayerText pt, TextType type, optional<DCPTextTrack> track, DCPTimePeriod period)
473 if (type != TextType::CLOSED_CAPTION) {
477 DCPOMATIC_ASSERT (track);
479 _closed_caption.put (pt, *track, period);
484 Butler::Error::summary () const
488 case Error::Code::NONE:
489 return "No error registered";
490 case Error::Code::AGAIN:
491 return "Butler not ready";
492 case Error::Code::DIED:
493 return String::compose("Butler died (%1)", message);
494 case Error::Code::FINISHED:
495 return "Butler finished";