2 Copyright (C) 2016-2018 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 <boost/weak_ptr.hpp>
30 #include <boost/shared_ptr.hpp>
36 using boost::weak_ptr;
37 using boost::shared_ptr;
39 using boost::optional;
40 using boost::function;
41 #if BOOST_VERSION >= 106100
42 using namespace boost::placeholders;
45 /** Minimum video readahead in frames */
46 #define MINIMUM_VIDEO_READAHEAD 10
47 /** Maximum video readahead in frames; should never be exceeded (by much) unless there are bugs in Player */
48 #define MAXIMUM_VIDEO_READAHEAD 48
49 /** Minimum audio readahead in frames */
50 #define MINIMUM_AUDIO_READAHEAD (48000 * MINIMUM_VIDEO_READAHEAD / 24)
51 /** Maximum audio readahead in frames; should never be exceeded (by much) unless there are bugs in Player */
52 #define MAXIMUM_AUDIO_READAHEAD (48000 * MAXIMUM_VIDEO_READAHEAD / 24)
54 /** @param pixel_format Pixel format functor that will be used when calling ::image on PlayerVideos coming out of this
55 * butler. This will be used (where possible) to prepare the PlayerVideos so that calling image() on them is quick.
56 * @param aligned Same as above for the `aligned' flag.
57 * @param fast Same as above for the `fast' flag.
60 shared_ptr<Player> player,
61 AudioMapping audio_mapping,
63 function<AVPixelFormat (AVPixelFormat)> pixel_format,
68 , _prepare_work (new boost::asio::io_service::work (_prepare_service))
69 , _pending_seek_accurate (false)
73 , _stop_thread (false)
74 , _audio_mapping (audio_mapping)
75 , _audio_channels (audio_channels)
76 , _disable_audio (false)
77 , _pixel_format (pixel_format)
81 _player_video_connection = _player->Video.connect (bind (&Butler::video, this, _1, _2));
82 _player_audio_connection = _player->Audio.connect (bind (&Butler::audio, this, _1, _2, _3));
83 _player_text_connection = _player->Text.connect (bind (&Butler::text, this, _1, _2, _3, _4));
84 /* The butler must hear about things first, otherwise it might not sort out suspensions in time for
85 get_video() to be called in response to this signal.
87 _player_change_connection = _player->Change.connect (bind (&Butler::player_change, this, _1, _3), boost::signals2::at_front);
88 _thread = new boost::thread (bind (&Butler::thread, this));
89 #ifdef DCPOMATIC_LINUX
90 pthread_setname_np (_thread->native_handle(), "butler");
93 /* Create some threads to do work on the PlayerVideos we are creating; at present this is used to
94 multi-thread JPEG2000 decoding.
97 LOG_TIMING("start-prepare-threads %1", boost::thread::hardware_concurrency() * 2);
99 for (size_t i = 0; i < boost::thread::hardware_concurrency() * 2; ++i) {
100 _prepare_pool.create_thread (bind (&boost::asio::io_service::run, &_prepare_service));
107 boost::mutex::scoped_lock lm (_mutex);
111 _prepare_work.reset ();
112 _prepare_pool.join_all ();
113 _prepare_service.stop ();
115 _thread->interrupt ();
118 } catch (boost::thread_interrupted& e) {
124 /** Caller must hold a lock on _mutex */
126 Butler::should_run () const
128 if (_video.size() >= MAXIMUM_VIDEO_READAHEAD * 10) {
129 /* This is way too big */
130 optional<DCPTime> pos = _audio.peek();
132 throw ProgrammingError
133 (__FILE__, __LINE__, String::compose ("Butler video buffers reached %1 frames (audio is %2 at %3)", _video.size(), _audio.size(), pos->get()));
135 throw ProgrammingError
136 (__FILE__, __LINE__, String::compose ("Butler video buffers reached %1 frames (audio is %2)", _video.size(), _audio.size()));
140 if (_audio.size() >= MAXIMUM_AUDIO_READAHEAD * 10) {
141 /* This is way too big */
142 optional<DCPTime> pos = _audio.peek();
144 throw ProgrammingError
145 (__FILE__, __LINE__, String::compose ("Butler audio buffers reached %1 frames at %2 (video is %3)", _audio.size(), pos->get(), _video.size()));
147 throw ProgrammingError
148 (__FILE__, __LINE__, String::compose ("Butler audio buffers reached %1 frames (video is %3)", _audio.size(), _video.size()));
152 if (_video.size() >= MAXIMUM_VIDEO_READAHEAD * 2) {
153 LOG_WARNING ("Butler video buffers reached %1 frames (audio is %2)", _video.size(), _audio.size());
156 if (_audio.size() >= MAXIMUM_AUDIO_READAHEAD * 2) {
157 LOG_WARNING ("Butler audio buffers reached %1 frames (video is %2)", _audio.size(), _video.size());
160 if (_stop_thread || _finished || _died || _suspended) {
161 /* Definitely do not run */
165 if (_video.size() < MINIMUM_VIDEO_READAHEAD || (!_disable_audio && _audio.size() < MINIMUM_AUDIO_READAHEAD)) {
166 /* Definitely do run: we need data */
170 /* Run if we aren't full of video or audio */
171 return (_video.size() < MAXIMUM_VIDEO_READAHEAD) && (_audio.size() < MAXIMUM_AUDIO_READAHEAD);
179 boost::mutex::scoped_lock lm (_mutex);
181 /* Wait until we have something to do */
182 while (!should_run() && !_pending_seek_position) {
186 /* Do any seek that has been requested */
187 if (_pending_seek_position) {
189 _player->seek (*_pending_seek_position, _pending_seek_accurate);
190 _pending_seek_position = optional<DCPTime> ();
193 /* Fill _video and _audio. Don't try to carry on if a pending seek appears
194 while lm is unlocked, as in that state nothing will be added to
197 while (should_run() && !_pending_seek_position) {
199 bool const r = _player->pass ();
203 _arrived.notify_all ();
206 _arrived.notify_all ();
209 } catch (boost::thread_interrupted) {
210 /* The butler thread is being terminated */
211 boost::mutex::scoped_lock lm (_mutex);
213 _arrived.notify_all ();
216 boost::mutex::scoped_lock lm (_mutex);
218 _arrived.notify_all ();
221 pair<shared_ptr<PlayerVideo>, DCPTime>
222 Butler::get_video (Error* e)
224 boost::mutex::scoped_lock lm (_mutex);
230 return make_pair(shared_ptr<PlayerVideo>(), DCPTime());
233 /* Wait for data if we have none */
234 while (_video.empty() && !_finished && !_died) {
238 if (_video.empty()) {
240 *e = _died ? DIED : NONE;
242 return make_pair(shared_ptr<PlayerVideo>(), DCPTime());
245 pair<shared_ptr<PlayerVideo>, DCPTime> const r = _video.get ();
246 _summon.notify_all ();
250 optional<TextRingBuffers::Data>
251 Butler::get_closed_caption ()
253 boost::mutex::scoped_lock lm (_mutex);
254 return _closed_caption.get ();
258 Butler::seek (DCPTime position, bool accurate)
260 boost::mutex::scoped_lock lm (_mutex);
261 _awaiting = optional<DCPTime>();
262 seek_unlocked (position, accurate);
266 Butler::seek_unlocked (DCPTime position, bool accurate)
273 _pending_seek_position = position;
274 _pending_seek_accurate = accurate;
277 boost::mutex::scoped_lock lm (_buffers_mutex);
280 _closed_caption.clear ();
283 _summon.notify_all ();
287 Butler::prepare (weak_ptr<PlayerVideo> weak_video)
290 shared_ptr<PlayerVideo> video = weak_video.lock ();
291 /* If the weak_ptr cannot be locked the video obviously no longer requires any work */
293 LOG_TIMING("start-prepare in %1", thread_id());
294 video->prepare (_pixel_format, _aligned, _fast);
295 LOG_TIMING("finish-prepare in %1", thread_id());
301 boost::mutex::scoped_lock lm (_mutex);
306 Butler::video (shared_ptr<PlayerVideo> video, DCPTime time)
308 boost::mutex::scoped_lock lm (_mutex);
310 if (_pending_seek_position) {
311 /* Don't store any video in this case */
315 _prepare_service.post (bind (&Butler::prepare, this, weak_ptr<PlayerVideo>(video)));
317 boost::mutex::scoped_lock lm2 (_buffers_mutex);
318 _video.put (video, time);
322 Butler::audio (shared_ptr<AudioBuffers> audio, DCPTime time, int frame_rate)
325 boost::mutex::scoped_lock lm (_mutex);
326 if (_pending_seek_position || _disable_audio) {
327 /* Don't store any audio in these cases */
332 boost::mutex::scoped_lock lm2 (_buffers_mutex);
333 _audio.put (remap (audio, _audio_channels, _audio_mapping), time, frame_rate);
336 /** Try to get `frames' frames of audio and copy it into `out'. Silence
337 * will be filled if no audio is available.
338 * @return time of this audio, or unset if there was a buffer underrun.
341 Butler::get_audio (float* out, Frame frames)
343 optional<DCPTime> t = _audio.get (out, _audio_channels, frames);
344 _summon.notify_all ();
349 Butler::disable_audio ()
351 boost::mutex::scoped_lock lm (_mutex);
352 _disable_audio = true;
356 Butler::memory_used () const
358 /* XXX: should also look at _audio.memory_used() */
359 return _video.memory_used();
363 Butler::player_change (ChangeType type, bool frequent)
365 boost::mutex::scoped_lock lm (_mutex);
367 if (type == CHANGE_TYPE_PENDING) {
369 } else if (type == CHANGE_TYPE_DONE) {
371 if (_died || _pending_seek_position || frequent) {
373 _summon.notify_all ();
378 DCPTime next = _video.get().second;
379 if (_awaiting && _awaiting > next) {
380 /* We have recently done a player_changed seek and our buffers haven't been refilled yet,
381 so assume that we're seeking to the same place as last time.
383 seek_to = *_awaiting;
388 seek_unlocked (seek_to, true);
390 } else if (type == CHANGE_TYPE_CANCELLED) {
395 _summon.notify_all ();
399 Butler::text (PlayerText pt, TextType type, optional<DCPTextTrack> track, DCPTimePeriod period)
401 if (type != TEXT_CLOSED_CAPTION) {
405 DCPOMATIC_ASSERT (track);
407 boost::mutex::scoped_lock lm2 (_buffers_mutex);
408 _closed_caption.put (pt, *track, period);