Initial butler work.
[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.connect (bind (&VideoRingBuffers::put, &_video, _1, _2));
44         _thread = new boost::thread (bind (&Butler::thread, this));
45 }
46
47 Butler::~Butler ()
48 {
49         _thread->interrupt ();
50         try {
51                 _thread->join ();
52         } catch (boost::thread_interrupted& e) {
53                 /* No problem */
54         }
55         delete _thread;
56 }
57
58 void
59 Butler::thread ()
60 {
61         while (true) {
62                 boost::mutex::scoped_lock lm (_mutex);
63
64                 while (_video.size() > VIDEO_READAHEAD && !_pending_seek_position) {
65                         _summon.wait (lm);
66                 }
67
68                 if (_pending_seek_position) {
69                         _player->seek (*_pending_seek_position, _pending_seek_accurate);
70                         _pending_seek_position = optional<DCPTime> ();
71                 }
72
73                 while (_video.size() < VIDEO_READAHEAD) {
74                         _arrived.notify_all ();
75                         if (_player->pass ()) {
76                                 _finished = true;
77                                 break;
78                         }
79                 }
80         }
81 }
82
83 pair<shared_ptr<PlayerVideo>, DCPTime>
84 Butler::get_video ()
85 {
86         boost::mutex::scoped_lock lm (_mutex);
87         while (_video.size() == 0 && !_finished) {
88                 _arrived.wait (lm);
89         }
90
91         if (_finished) {
92                 return make_pair (shared_ptr<PlayerVideo>(), DCPTime());
93         }
94
95         return _video.get ();
96 }
97
98 void
99 Butler::seek (DCPTime position, bool accurate)
100 {
101         _video.clear ();
102
103         {
104                 boost::mutex::scoped_lock lm (_mutex);
105                 _finished = false;
106                 _pending_seek_position = position;
107                 _pending_seek_accurate = accurate;
108         }
109
110         _summon.notify_all ();
111 }