No-op; white space.
[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         , _audio_mapping (audio_mapping)
45         , _audio_channels (audio_channels)
46         , _stop_thread (false)
47 {
48         _player_video_connection = _player->Video.connect (bind (&Butler::video, this, _1, _2));
49         _player_audio_connection = _player->Audio.connect (bind (&Butler::audio, this, _1, _2));
50         _player_changed_connection = _player->Changed.connect (bind (&Butler::player_changed, this));
51         _thread = new boost::thread (bind (&Butler::thread, this));
52 }
53
54 Butler::~Butler ()
55 {
56         _stop_thread = true;
57         _thread->interrupt ();
58         try {
59                 _thread->join ();
60         } catch (boost::thread_interrupted& e) {
61                 /* No problem */
62         }
63         delete _thread;
64 }
65
66 bool
67 Butler::should_run () const
68 {
69         return (_video.size() < VIDEO_READAHEAD || _audio.size() < AUDIO_READAHEAD) && !_stop_thread && !_finished;
70 }
71
72 void
73 Butler::thread ()
74 try
75 {
76         while (true) {
77                 boost::mutex::scoped_lock lm (_mutex);
78
79                 /* Wait until we have something to do */
80                 while (!should_run() && !_pending_seek_position) {
81                         _summon.wait (lm);
82                 }
83
84                 /* Do any seek that has been requested */
85                 if (_pending_seek_position) {
86                         _player->seek (*_pending_seek_position, _pending_seek_accurate);
87                         _pending_seek_position = optional<DCPTime> ();
88                 }
89
90                 /* Fill _video and _audio.  Don't try to carry on if a pending seek appears
91                    while lm is unlocked, as in that state nothing will be added to
92                    _video/_audio.
93                 */
94                 while (should_run() && !_pending_seek_position) {
95                         lm.unlock ();
96                         if (_player->pass ()) {
97                                 _finished = true;
98                                 _arrived.notify_all ();
99                                 break;
100                         }
101                         lm.lock ();
102                         _arrived.notify_all ();
103                 }
104         }
105 } catch (boost::thread_interrupted) {
106         /* The butler thread is being terminated */
107 } catch (...) {
108         store_current ();
109 }
110
111 pair<shared_ptr<PlayerVideo>, DCPTime>
112 Butler::get_video ()
113 {
114         boost::mutex::scoped_lock lm (_mutex);
115
116         /* Wait for data if we have none */
117         while (_video.empty() && !_finished) {
118                 _arrived.wait (lm);
119         }
120
121         if (_video.empty() && _finished) {
122                 return make_pair (shared_ptr<PlayerVideo>(), DCPTime());
123         }
124
125         pair<shared_ptr<PlayerVideo>, DCPTime> const r = _video.get ();
126         _summon.notify_all ();
127         return r;
128 }
129
130 void
131 Butler::seek (DCPTime position, bool accurate)
132 {
133         boost::mutex::scoped_lock lm (_mutex);
134         _video.clear ();
135         _audio.clear ();
136         _finished = false;
137         _pending_seek_position = position;
138         _pending_seek_accurate = accurate;
139         _summon.notify_all ();
140 }
141
142 void
143 Butler::video (shared_ptr<PlayerVideo> video, DCPTime time)
144 {
145         {
146                 boost::mutex::scoped_lock lm (_mutex);
147                 if (_pending_seek_position) {
148                         /* Don't store any video while a seek is pending */
149                         return;
150                 }
151         }
152
153         _video.put (video, time);
154 }
155
156 void
157 Butler::audio (shared_ptr<AudioBuffers> audio, DCPTime time)
158 {
159         {
160                 boost::mutex::scoped_lock lm (_mutex);
161                 if (_pending_seek_position) {
162                         /* Don't store any audio while a seek is pending */
163                         return;
164                 }
165         }
166
167         _audio.put (audio, time);
168 }
169
170 void
171 Butler::player_changed ()
172 {
173         optional<DCPTime> t;
174
175         {
176                 boost::mutex::scoped_lock lm (_mutex);
177                 t = _video.earliest ();
178         }
179
180         if (t) {
181                 seek (*t, true);
182         } else {
183                 _video.clear ();
184                 _audio.clear ();
185         }
186 }
187
188 void
189 Butler::get_audio (float* out, Frame frames)
190 {
191         _audio.get (out, _audio_channels, frames);
192         _summon.notify_all ();
193 }