2 Copyright (C) 2016-2020 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/>.
25 #include "dcpomatic_log.h"
27 #include "compose.hpp"
28 #include "exceptions.h"
29 #include "video_content.h"
37 using std::shared_ptr;
39 using boost::optional;
40 using boost::function;
41 using namespace dcpomatic;
42 #if BOOST_VERSION >= 106100
43 using namespace boost::placeholders;
46 /** Minimum video readahead in frames */
47 #define MINIMUM_VIDEO_READAHEAD 10
48 /** Maximum video readahead in frames; should never be exceeded (by much) unless there are bugs in Player */
49 #define MAXIMUM_VIDEO_READAHEAD 48
50 /** Minimum audio readahead in frames */
51 #define MINIMUM_AUDIO_READAHEAD (48000 * MINIMUM_VIDEO_READAHEAD / 24)
52 /** Maximum audio readahead in frames; should never be exceeded (by much) unless there are bugs in Player */
53 #define MAXIMUM_AUDIO_READAHEAD (48000 * MAXIMUM_VIDEO_READAHEAD / 24)
55 /** @param pixel_format Pixel format functor that will be used when calling ::image on PlayerVideos coming out of this
56 * butler. This will be used (where possible) to prepare the PlayerVideos so that calling image() on them is quick.
57 * @param aligned Same as above for the `aligned' flag.
58 * @param fast Same as above for the `fast' flag.
61 weak_ptr<const Film> film,
62 shared_ptr<Player> player,
63 AudioMapping audio_mapping,
65 function<AVPixelFormat (AVPixelFormat)> pixel_format,
66 VideoRange video_range,
72 , _prepare_work (new boost::asio::io_service::work (_prepare_service))
73 , _pending_seek_accurate (false)
77 , _stop_thread (false)
78 , _audio_mapping (audio_mapping)
79 , _audio_channels (audio_channels)
80 , _disable_audio (false)
81 , _pixel_format (pixel_format)
82 , _video_range (video_range)
86 _player_video_connection = _player->Video.connect (bind (&Butler::video, this, _1, _2));
87 _player_audio_connection = _player->Audio.connect (bind (&Butler::audio, this, _1, _2, _3));
88 _player_text_connection = _player->Text.connect (bind (&Butler::text, this, _1, _2, _3, _4));
89 /* The butler must hear about things first, otherwise it might not sort out suspensions in time for
90 get_video() to be called in response to this signal.
92 _player_change_connection = _player->Change.connect (bind (&Butler::player_change, this, _1, _2), boost::signals2::at_front);
93 _thread = boost::thread (bind(&Butler::thread, this));
94 #ifdef DCPOMATIC_LINUX
95 pthread_setname_np (_thread.native_handle(), "butler");
98 /* Create some threads to do work on the PlayerVideos we are creating; at present this is used to
99 multi-thread JPEG2000 decoding.
102 LOG_TIMING("start-prepare-threads %1", boost::thread::hardware_concurrency() * 2);
104 for (size_t i = 0; i < boost::thread::hardware_concurrency() * 2; ++i) {
105 _prepare_pool.create_thread (bind (&boost::asio::io_service::run, &_prepare_service));
111 boost::this_thread::disable_interruption dis;
114 boost::mutex::scoped_lock lm (_mutex);
118 _prepare_work.reset ();
119 _prepare_pool.join_all ();
120 _prepare_service.stop ();
122 _thread.interrupt ();
128 /** Caller must hold a lock on _mutex */
130 Butler::should_run () const
132 if (_video.size() >= MAXIMUM_VIDEO_READAHEAD * 10) {
133 /* This is way too big */
134 optional<DCPTime> pos = _audio.peek();
136 throw ProgrammingError
137 (__FILE__, __LINE__, String::compose ("Butler video buffers reached %1 frames (audio is %2 at %3)", _video.size(), _audio.size(), pos->get()));
139 throw ProgrammingError
140 (__FILE__, __LINE__, String::compose ("Butler video buffers reached %1 frames (audio is %2)", _video.size(), _audio.size()));
144 if (_audio.size() >= MAXIMUM_AUDIO_READAHEAD * 10) {
145 /* This is way too big */
146 optional<DCPTime> pos = _audio.peek();
148 throw ProgrammingError
149 (__FILE__, __LINE__, String::compose ("Butler audio buffers reached %1 frames at %2 (video is %3)", _audio.size(), pos->get(), _video.size()));
151 throw ProgrammingError
152 (__FILE__, __LINE__, String::compose ("Butler audio buffers reached %1 frames (video is %3)", _audio.size(), _video.size()));
156 if (_video.size() >= MAXIMUM_VIDEO_READAHEAD * 2) {
157 LOG_WARNING ("Butler video buffers reached %1 frames (audio is %2)", _video.size(), _audio.size());
160 if (_audio.size() >= MAXIMUM_AUDIO_READAHEAD * 2) {
161 LOG_WARNING ("Butler audio buffers reached %1 frames (video is %2)", _audio.size(), _video.size());
164 if (_stop_thread || _finished || _died || _suspended) {
165 /* Definitely do not run */
169 if (_video.size() < MINIMUM_VIDEO_READAHEAD || (!_disable_audio && _audio.size() < MINIMUM_AUDIO_READAHEAD)) {
170 /* Definitely do run: we need data */
174 /* Run if we aren't full of video or audio */
175 return (_video.size() < MAXIMUM_VIDEO_READAHEAD) && (_audio.size() < MAXIMUM_AUDIO_READAHEAD);
183 boost::mutex::scoped_lock lm (_mutex);
185 /* Wait until we have something to do */
186 while (!should_run() && !_pending_seek_position) {
190 /* Do any seek that has been requested */
191 if (_pending_seek_position) {
193 _player->seek (*_pending_seek_position, _pending_seek_accurate);
194 _pending_seek_position = optional<DCPTime> ();
197 /* Fill _video and _audio. Don't try to carry on if a pending seek appears
198 while lm is unlocked, as in that state nothing will be added to
201 while (should_run() && !_pending_seek_position) {
203 bool const r = _player->pass ();
207 _arrived.notify_all ();
210 _arrived.notify_all ();
213 } catch (boost::thread_interrupted) {
214 /* The butler thread is being terminated */
215 boost::mutex::scoped_lock lm (_mutex);
217 _arrived.notify_all ();
218 } catch (std::exception& e) {
220 boost::mutex::scoped_lock lm (_mutex);
222 _died_message = e.what ();
223 _arrived.notify_all ();
226 boost::mutex::scoped_lock lm (_mutex);
228 _arrived.notify_all ();
231 /** @param blocking true if we should block until video is available. If blocking is false
232 * and no video is immediately available the method will return a 0 PlayerVideo and the error AGAIN.
233 * @param e if non-0 this is filled with an error code (if an error occurs) or is untouched if no error occurs.
235 pair<shared_ptr<PlayerVideo>, DCPTime>
236 Butler::get_video (bool blocking, Error* e)
238 boost::mutex::scoped_lock lm (_mutex);
240 auto setup_error = [this](Error* e, Error::Code fallback) {
243 e->code = Error::DIED;
244 e->message = _died_message;
245 } else if (_finished) {
246 e->code = Error::FINISHED;
253 if (_video.empty() && (_finished || _died || (_suspended && !blocking))) {
254 setup_error (e, Error::AGAIN);
255 return make_pair(shared_ptr<PlayerVideo>(), DCPTime());
258 /* Wait for data if we have none */
259 while (_video.empty() && !_finished && !_died) {
263 if (_video.empty()) {
264 setup_error (e, Error::NONE);
265 return make_pair(shared_ptr<PlayerVideo>(), DCPTime());
268 auto const r = _video.get ();
269 _summon.notify_all ();
273 optional<TextRingBuffers::Data>
274 Butler::get_closed_caption ()
276 boost::mutex::scoped_lock lm (_mutex);
277 return _closed_caption.get ();
281 Butler::seek (DCPTime position, bool accurate)
283 boost::mutex::scoped_lock lm (_mutex);
284 _awaiting = optional<DCPTime>();
285 seek_unlocked (position, accurate);
289 Butler::seek_unlocked (DCPTime position, bool accurate)
296 _pending_seek_position = position;
297 _pending_seek_accurate = accurate;
301 _closed_caption.clear ();
303 _summon.notify_all ();
307 Butler::prepare (weak_ptr<PlayerVideo> weak_video)
310 shared_ptr<PlayerVideo> video = weak_video.lock ();
311 /* If the weak_ptr cannot be locked the video obviously no longer requires any work */
313 LOG_TIMING("start-prepare in %1", thread_id());
314 video->prepare (_pixel_format, _video_range, _aligned, _fast);
315 LOG_TIMING("finish-prepare in %1", thread_id());
318 catch (std::exception& e)
321 boost::mutex::scoped_lock lm (_mutex);
323 _died_message = e.what ();
328 boost::mutex::scoped_lock lm (_mutex);
333 Butler::video (shared_ptr<PlayerVideo> video, DCPTime time)
335 boost::mutex::scoped_lock lm (_mutex);
337 if (_pending_seek_position) {
338 /* Don't store any video in this case */
342 _prepare_service.post (bind (&Butler::prepare, this, weak_ptr<PlayerVideo>(video)));
344 _video.put (video, time);
348 Butler::audio (shared_ptr<AudioBuffers> audio, DCPTime time, int frame_rate)
350 boost::mutex::scoped_lock lm (_mutex);
351 if (_pending_seek_position || _disable_audio) {
352 /* Don't store any audio in these cases */
356 _audio.put (remap (audio, _audio_channels, _audio_mapping), time, frame_rate);
359 /** Try to get `frames' frames of audio and copy it into `out'. Silence
360 * will be filled if no audio is available.
361 * @return time of this audio, or unset if there was a buffer underrun.
364 Butler::get_audio (float* out, Frame frames)
366 optional<DCPTime> t = _audio.get (out, _audio_channels, frames);
367 _summon.notify_all ();
372 Butler::disable_audio ()
374 boost::mutex::scoped_lock lm (_mutex);
375 _disable_audio = true;
379 Butler::memory_used () const
381 /* XXX: should also look at _audio.memory_used() */
382 return _video.memory_used();
386 Butler::player_change (ChangeType type, int property)
388 if (property == VideoContentProperty::CROP) {
389 if (type == ChangeType::DONE) {
390 auto film = _film.lock();
392 _video.reset_metadata (film, _player->video_container_size());
398 boost::mutex::scoped_lock lm (_mutex);
400 if (type == ChangeType::PENDING) {
402 } else if (type == ChangeType::DONE) {
404 if (_died || _pending_seek_position) {
406 _summon.notify_all ();
411 auto next = _video.get().second;
412 if (_awaiting && _awaiting > next) {
413 /* We have recently done a player_changed seek and our buffers haven't been refilled yet,
414 so assume that we're seeking to the same place as last time.
416 seek_to = *_awaiting;
421 seek_unlocked (seek_to, true);
423 } else if (type == ChangeType::CANCELLED) {
428 _summon.notify_all ();
432 Butler::text (PlayerText pt, TextType type, optional<DCPTextTrack> track, DCPTimePeriod period)
434 if (type != TextType::CLOSED_CAPTION) {
438 DCPOMATIC_ASSERT (track);
440 _closed_caption.put (pt, *track, period);
444 Butler::Error::summary () const
449 return "No error registered";
451 return "Butler not ready";
453 return String::compose("Butler died (%1)", message);
454 case Error::FINISHED:
455 return "Butler finished";