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