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