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