Missing conditional wakeup.
[dcpomatic.git] / src / lib / butler.cc
1 /*
2     Copyright (C) 2016-2017 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
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.
10
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.
15
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/>.
18
19 */
20
21 #include "butler.h"
22 #include "player.h"
23 #include "util.h"
24 #include "log.h"
25 #include "compose.hpp"
26 #include <boost/weak_ptr.hpp>
27 #include <boost/shared_ptr.hpp>
28
29 using std::cout;
30 using std::pair;
31 using std::make_pair;
32 using boost::weak_ptr;
33 using boost::shared_ptr;
34 using boost::bind;
35 using boost::optional;
36
37 /** Minimum video readahead in frames */
38 #define MINIMUM_VIDEO_READAHEAD 10
39 /** Maximum video readahead in frames; should never be reached unless there are bugs in Player */
40 #define MAXIMUM_VIDEO_READAHEAD 240
41 /** Minimum audio readahead in frames */
42 #define MINIMUM_AUDIO_READAHEAD (48000*5)
43 /** Minimum audio readahead in frames; should never be reached unless there are bugs in Player */
44 #define MAXIMUM_AUDIO_READAHEAD (48000*60)
45
46 #define LOG_WARNING(...) _log->log (String::compose(__VA_ARGS__), LogEntry::TYPE_WARNING);
47
48 Butler::Butler (shared_ptr<Player> player, shared_ptr<Log> log, AudioMapping audio_mapping, int audio_channels)
49         : _player (player)
50         , _log (log)
51         , _prepare_work (new boost::asio::io_service::work (_prepare_service))
52         , _pending_seek_accurate (false)
53         , _finished (false)
54         , _died (false)
55         , _stop_thread (false)
56         , _audio_mapping (audio_mapping)
57         , _audio_channels (audio_channels)
58         , _disable_audio (false)
59 {
60         _player_video_connection = _player->Video.connect (bind (&Butler::video, this, _1, _2));
61         _player_audio_connection = _player->Audio.connect (bind (&Butler::audio, this, _1));
62         _player_changed_connection = _player->Changed.connect (bind (&Butler::player_changed, this));
63         _thread = new boost::thread (bind (&Butler::thread, this));
64
65         /* Create some threads to do work on the PlayerVideos we are creating; at present this is used to
66            multi-thread JPEG2000 decoding.
67         */
68         for (size_t i = 0; i < boost::thread::hardware_concurrency(); ++i) {
69                 _prepare_pool.create_thread (bind (&boost::asio::io_service::run, &_prepare_service));
70         }
71 }
72
73 Butler::~Butler ()
74 {
75         {
76                 boost::mutex::scoped_lock lm (_mutex);
77                 _stop_thread = true;
78         }
79
80         _prepare_work.reset ();
81         _prepare_pool.join_all ();
82         _prepare_service.stop ();
83
84         _thread->interrupt ();
85         try {
86                 _thread->join ();
87         } catch (boost::thread_interrupted& e) {
88                 /* No problem */
89         }
90         delete _thread;
91 }
92
93 /** Caller must hold a lock on _mutex */
94 bool
95 Butler::should_run () const
96 {
97         if (_video.size() >= MAXIMUM_VIDEO_READAHEAD) {
98                 LOG_WARNING ("Butler video buffers reached %1 frames", _video.size());
99         }
100
101         if (_audio.size() >= MAXIMUM_AUDIO_READAHEAD) {
102                 LOG_WARNING ("Butler audio buffers reached %1 frames", _audio.size());
103         }
104
105         return (_video.size() < MINIMUM_VIDEO_READAHEAD || (!_disable_audio && _audio.size() < MINIMUM_AUDIO_READAHEAD))
106                 && (_video.size() < MAXIMUM_VIDEO_READAHEAD)
107                 && (_audio.size() < MAXIMUM_AUDIO_READAHEAD)
108                 && !_stop_thread
109                 && !_finished
110                 && !_died;
111 }
112
113 void
114 Butler::thread ()
115 try
116 {
117         while (true) {
118                 boost::mutex::scoped_lock lm (_mutex);
119
120                 /* Wait until we have something to do */
121                 while (!should_run() && !_pending_seek_position) {
122                         _summon.wait (lm);
123                 }
124
125                 /* Do any seek that has been requested */
126                 if (_pending_seek_position) {
127                         _finished = false;
128                         _player->seek (*_pending_seek_position, _pending_seek_accurate);
129                         _pending_seek_position = optional<DCPTime> ();
130                 }
131
132                 /* Fill _video and _audio.  Don't try to carry on if a pending seek appears
133                    while lm is unlocked, as in that state nothing will be added to
134                    _video/_audio.
135                 */
136                 while (should_run() && !_pending_seek_position) {
137                         lm.unlock ();
138                         bool const r = _player->pass ();
139                         lm.lock ();
140                         if (r) {
141                                 _finished = true;
142                                 _arrived.notify_all ();
143                                 break;
144                         }
145                         _arrived.notify_all ();
146                 }
147         }
148 } catch (boost::thread_interrupted) {
149         /* The butler thread is being terminated */
150         boost::mutex::scoped_lock lm (_mutex);
151         _finished = true;
152         _arrived.notify_all ();
153 } catch (...) {
154         store_current ();
155         boost::mutex::scoped_lock lm (_mutex);
156         _died = true;
157         _arrived.notify_all ();
158 }
159
160 pair<shared_ptr<PlayerVideo>, DCPTime>
161 Butler::get_video ()
162 {
163         boost::mutex::scoped_lock lm (_mutex);
164
165         /* Wait for data if we have none */
166         while (_video.empty() && !_finished && !_died) {
167                 _arrived.wait (lm);
168         }
169
170         if (_video.empty()) {
171                 return make_pair (shared_ptr<PlayerVideo>(), DCPTime());
172         }
173
174         pair<shared_ptr<PlayerVideo>, DCPTime> const r = _video.get ();
175         _summon.notify_all ();
176         return r;
177 }
178
179 void
180 Butler::seek (DCPTime position, bool accurate)
181 {
182         boost::mutex::scoped_lock lm (_mutex);
183         if (_died) {
184                 return;
185         }
186
187         _video.clear ();
188         _audio.clear ();
189         _finished = false;
190         _pending_seek_position = position;
191         _pending_seek_accurate = accurate;
192         _summon.notify_all ();
193 }
194
195 void
196 Butler::prepare (weak_ptr<PlayerVideo> weak_video) const
197 {
198         shared_ptr<PlayerVideo> video = weak_video.lock ();
199         /* If the weak_ptr cannot be locked the video obviously no longer requires any work */
200         if (video) {
201                 video->prepare ();
202         }
203 }
204
205 void
206 Butler::video (shared_ptr<PlayerVideo> video, DCPTime time)
207 {
208         {
209                 boost::mutex::scoped_lock lm (_mutex);
210                 if (_pending_seek_position) {
211                         /* Don't store any video while a seek is pending */
212                         return;
213                 }
214         }
215
216         _prepare_service.post (bind (&Butler::prepare, this, weak_ptr<PlayerVideo>(video)));
217         _video.put (video, time);
218 }
219
220 void
221 Butler::audio (shared_ptr<AudioBuffers> audio)
222 {
223         {
224                 boost::mutex::scoped_lock lm (_mutex);
225                 if (_pending_seek_position || _disable_audio) {
226                         /* Don't store any audio while a seek is pending, or if audio is disabled */
227                         return;
228                 }
229         }
230
231         _audio.put (remap (audio, _audio_channels, _audio_mapping));
232 }
233
234 void
235 Butler::player_changed ()
236 {
237         _video.clear ();
238         _audio.clear ();
239         _summon.notify_all ();
240 }
241
242 /** Try to get `frames' frames of audio and copy it into `out'.  Silence
243  *  will be filled if no audio is available.
244  *  @return true if there was a buffer underrun, otherwise false.
245  */
246 bool
247 Butler::get_audio (float* out, Frame frames)
248 {
249         bool const underrun = _audio.get (out, _audio_channels, frames);
250         _summon.notify_all ();
251         return underrun;
252 }
253
254 void
255 Butler::disable_audio ()
256 {
257         boost::mutex::scoped_lock lm (_mutex);
258         _disable_audio = true;
259 }