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