Attempt to prevent a deadlock if audio is full and video empty (or vice versa).
[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         _thread = new boost::thread (bind (&Butler::thread, this));
63
64         /* Create some threads to do work on the PlayerVideos we are creating; at present this is used to
65            multi-thread JPEG2000 decoding.
66         */
67         for (size_t i = 0; i < boost::thread::hardware_concurrency(); ++i) {
68                 _prepare_pool.create_thread (bind (&boost::asio::io_service::run, &_prepare_service));
69         }
70 }
71
72 Butler::~Butler ()
73 {
74         {
75                 boost::mutex::scoped_lock lm (_mutex);
76                 _stop_thread = true;
77         }
78
79         _prepare_work.reset ();
80         _prepare_pool.join_all ();
81         _prepare_service.stop ();
82
83         _thread->interrupt ();
84         try {
85                 _thread->join ();
86         } catch (boost::thread_interrupted& e) {
87                 /* No problem */
88         }
89         delete _thread;
90 }
91
92 /** Caller must hold a lock on _mutex */
93 bool
94 Butler::should_run () const
95 {
96         if (_video.size() >= MAXIMUM_VIDEO_READAHEAD) {
97                 LOG_WARNING ("Butler video buffers reached %1 frames", _video.size());
98         }
99
100         if (_audio.size() >= MAXIMUM_AUDIO_READAHEAD) {
101                 LOG_WARNING ("Butler audio buffers reached %1 frames", _audio.size());
102         }
103
104         if (_stop_thread || _finished || _died) {
105                 /* Definitely do not run */
106                 return false;
107         }
108
109         if (_video.size() < MINIMUM_VIDEO_READAHEAD || (!_disable_audio && _audio.size() < MINIMUM_AUDIO_READAHEAD)) {
110                 /* Definitely do run: we need data */
111                 return true;
112         }
113
114         /* Run if we aren't full of video or audio */
115         return (_video.size() < MAXIMUM_VIDEO_READAHEAD) && (_audio.size() < MAXIMUM_AUDIO_READAHEAD);
116 }
117
118 void
119 Butler::thread ()
120 try
121 {
122         while (true) {
123                 boost::mutex::scoped_lock lm (_mutex);
124
125                 /* Wait until we have something to do */
126                 while (!should_run() && !_pending_seek_position) {
127                         _summon.wait (lm);
128                 }
129
130                 /* Do any seek that has been requested */
131                 if (_pending_seek_position) {
132                         _finished = false;
133                         _player->seek (*_pending_seek_position, _pending_seek_accurate);
134                         _pending_seek_position = optional<DCPTime> ();
135                 }
136
137                 /* Fill _video and _audio.  Don't try to carry on if a pending seek appears
138                    while lm is unlocked, as in that state nothing will be added to
139                    _video/_audio.
140                 */
141                 while (should_run() && !_pending_seek_position) {
142                         lm.unlock ();
143                         bool const r = _player->pass ();
144                         lm.lock ();
145                         if (r) {
146                                 _finished = true;
147                                 _arrived.notify_all ();
148                                 break;
149                         }
150                         _arrived.notify_all ();
151                 }
152         }
153 } catch (boost::thread_interrupted) {
154         /* The butler thread is being terminated */
155         boost::mutex::scoped_lock lm (_mutex);
156         _finished = true;
157         _arrived.notify_all ();
158 } catch (...) {
159         store_current ();
160         boost::mutex::scoped_lock lm (_mutex);
161         _died = true;
162         _arrived.notify_all ();
163 }
164
165 pair<shared_ptr<PlayerVideo>, DCPTime>
166 Butler::get_video ()
167 {
168         boost::mutex::scoped_lock lm (_mutex);
169
170         /* Wait for data if we have none */
171         while (_video.empty() && !_finished && !_died) {
172                 _arrived.wait (lm);
173         }
174
175         if (_video.empty()) {
176                 return make_pair (shared_ptr<PlayerVideo>(), DCPTime());
177         }
178
179         pair<shared_ptr<PlayerVideo>, DCPTime> const r = _video.get ();
180         _summon.notify_all ();
181         return r;
182 }
183
184 void
185 Butler::seek (DCPTime position, bool accurate)
186 {
187         boost::mutex::scoped_lock lm (_mutex);
188         if (_died) {
189                 return;
190         }
191
192         _video.clear ();
193         _audio.clear ();
194         _finished = false;
195         _pending_seek_position = position;
196         _pending_seek_accurate = accurate;
197         _summon.notify_all ();
198 }
199
200 void
201 Butler::prepare (weak_ptr<PlayerVideo> weak_video) const
202 {
203         shared_ptr<PlayerVideo> video = weak_video.lock ();
204         /* If the weak_ptr cannot be locked the video obviously no longer requires any work */
205         if (video) {
206                 video->prepare ();
207         }
208 }
209
210 void
211 Butler::video (shared_ptr<PlayerVideo> video, DCPTime time)
212 {
213         {
214                 boost::mutex::scoped_lock lm (_mutex);
215                 if (_pending_seek_position) {
216                         /* Don't store any video while a seek is pending */
217                         return;
218                 }
219         }
220
221         _prepare_service.post (bind (&Butler::prepare, this, weak_ptr<PlayerVideo>(video)));
222         _video.put (video, time);
223 }
224
225 void
226 Butler::audio (shared_ptr<AudioBuffers> audio)
227 {
228         {
229                 boost::mutex::scoped_lock lm (_mutex);
230                 if (_pending_seek_position || _disable_audio) {
231                         /* Don't store any audio while a seek is pending, or if audio is disabled */
232                         return;
233                 }
234         }
235
236         _audio.put (remap (audio, _audio_channels, _audio_mapping));
237 }
238
239 /** Try to get `frames' frames of audio and copy it into `out'.  Silence
240  *  will be filled if no audio is available.
241  *  @return true if there was a buffer underrun, otherwise false.
242  */
243 bool
244 Butler::get_audio (float* out, Frame frames)
245 {
246         bool const underrun = _audio.get (out, _audio_channels, frames);
247         _summon.notify_all ();
248         return underrun;
249 }
250
251 void
252 Butler::disable_audio ()
253 {
254         boost::mutex::scoped_lock lm (_mutex);
255         _disable_audio = true;
256 }