Catch butler runaway when no audio is provided by the player.
[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(...) _film.lock()->log()->log (String::compose (__VA_ARGS__), LogEntry::TYPE_WARNING);
47
48 Butler::Butler (weak_ptr<const Film> film, shared_ptr<Player> player, AudioMapping audio_mapping, int audio_channels)
49         : _film (film)
50         , _player (player)
51         , _pending_seek_accurate (false)
52         , _finished (false)
53         , _died (false)
54         , _stop_thread (false)
55         , _audio_mapping (audio_mapping)
56         , _audio_channels (audio_channels)
57         , _disable_audio (false)
58 {
59         _player_video_connection = _player->Video.connect (bind (&Butler::video, this, _1, _2));
60         _player_audio_connection = _player->Audio.connect (bind (&Butler::audio, this, _1));
61         _player_changed_connection = _player->Changed.connect (bind (&Butler::player_changed, this));
62         _thread = new boost::thread (bind (&Butler::thread, this));
63 }
64
65 Butler::~Butler ()
66 {
67         {
68                 boost::mutex::scoped_lock lm (_mutex);
69                 _stop_thread = true;
70         }
71
72         _thread->interrupt ();
73         try {
74                 _thread->join ();
75         } catch (boost::thread_interrupted& e) {
76                 /* No problem */
77         }
78         delete _thread;
79 }
80
81 /** Caller must hold a lock on _mutex */
82 bool
83 Butler::should_run () const
84 {
85         if (_video.size() >= MAXIMUM_VIDEO_READAHEAD) {
86                 LOG_WARNING ("Butler video buffers reached %1 frames", _video.size());
87         }
88
89         if (_audio.size() >= MAXIMUM_AUDIO_READAHEAD) {
90                 LOG_WARNING ("Butler audio buffers reached %1 frames", _audio.size());
91         }
92
93         return (_video.size() < MINIMUM_VIDEO_READAHEAD || (!_disable_audio && _audio.size() < MINIMUM_AUDIO_READAHEAD))
94                 && (_video.size() < MAXIMUM_VIDEO_READAHEAD)
95                 && (_audio.size() < MAXIMUM_AUDIO_READAHEAD)
96                 && !_stop_thread
97                 && !_finished
98                 && !_died;
99 }
100
101 void
102 Butler::thread ()
103 try
104 {
105         while (true) {
106                 boost::mutex::scoped_lock lm (_mutex);
107
108                 /* Wait until we have something to do */
109                 while (!should_run() && !_pending_seek_position) {
110                         _summon.wait (lm);
111                 }
112
113                 /* Do any seek that has been requested */
114                 if (_pending_seek_position) {
115                         _finished = false;
116                         _player->seek (*_pending_seek_position, _pending_seek_accurate);
117                         _pending_seek_position = optional<DCPTime> ();
118                 }
119
120                 /* Fill _video and _audio.  Don't try to carry on if a pending seek appears
121                    while lm is unlocked, as in that state nothing will be added to
122                    _video/_audio.
123                 */
124                 while (should_run() && !_pending_seek_position) {
125                         lm.unlock ();
126                         bool const r = _player->pass ();
127                         lm.lock ();
128                         if (r) {
129                                 _finished = true;
130                                 _arrived.notify_all ();
131                                 break;
132                         }
133                         _arrived.notify_all ();
134                 }
135         }
136 } catch (boost::thread_interrupted) {
137         /* The butler thread is being terminated */
138         boost::mutex::scoped_lock lm (_mutex);
139         _finished = true;
140         _arrived.notify_all ();
141 } catch (...) {
142         store_current ();
143         boost::mutex::scoped_lock lm (_mutex);
144         _died = true;
145         _arrived.notify_all ();
146 }
147
148 pair<shared_ptr<PlayerVideo>, DCPTime>
149 Butler::get_video ()
150 {
151         boost::mutex::scoped_lock lm (_mutex);
152
153         /* Wait for data if we have none */
154         while (_video.empty() && !_finished && !_died) {
155                 _arrived.wait (lm);
156         }
157
158         if (_video.empty()) {
159                 return make_pair (shared_ptr<PlayerVideo>(), DCPTime());
160         }
161
162         pair<shared_ptr<PlayerVideo>, DCPTime> const r = _video.get ();
163         _summon.notify_all ();
164         return r;
165 }
166
167 void
168 Butler::seek (DCPTime position, bool accurate)
169 {
170         boost::mutex::scoped_lock lm (_mutex);
171         if (_died) {
172                 return;
173         }
174
175         _video.clear ();
176         _audio.clear ();
177         _finished = false;
178         _pending_seek_position = position;
179         _pending_seek_accurate = accurate;
180         _summon.notify_all ();
181 }
182
183 void
184 Butler::video (shared_ptr<PlayerVideo> video, DCPTime time)
185 {
186         {
187                 boost::mutex::scoped_lock lm (_mutex);
188                 if (_pending_seek_position) {
189                         /* Don't store any video while a seek is pending */
190                         return;
191                 }
192         }
193
194         _video.put (video, time);
195 }
196
197 void
198 Butler::audio (shared_ptr<AudioBuffers> audio)
199 {
200         {
201                 boost::mutex::scoped_lock lm (_mutex);
202                 if (_pending_seek_position || _disable_audio) {
203                         /* Don't store any audio while a seek is pending, or if audio is disabled */
204                         return;
205                 }
206         }
207
208         _audio.put (remap (audio, _audio_channels, _audio_mapping));
209 }
210
211 void
212 Butler::player_changed ()
213 {
214         optional<DCPTime> t;
215
216         {
217                 boost::mutex::scoped_lock lm (_mutex);
218                 t = _video.earliest ();
219         }
220
221         if (t) {
222                 seek (*t, true);
223         } else {
224                 _video.clear ();
225                 _audio.clear ();
226         }
227 }
228
229 void
230 Butler::get_audio (float* out, Frame frames)
231 {
232         _audio.get (out, _audio_channels, frames);
233         _summon.notify_all ();
234 }
235
236 void
237 Butler::disable_audio ()
238 {
239         boost::mutex::scoped_lock lm (_mutex);
240         _disable_audio = true;
241 }