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