summaryrefslogtreecommitdiff
path: root/src/lib/butler.cc
blob: e40ae0b24f958767f85f318543112957fe4818d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
    Copyright (C) 2016-2017 Carl Hetherington <cth@carlh.net>

    This file is part of DCP-o-matic.

    DCP-o-matic is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    DCP-o-matic is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.

*/

#include "butler.h"
#include "player.h"
#include <boost/weak_ptr.hpp>
#include <boost/shared_ptr.hpp>

using std::cout;
using std::pair;
using std::make_pair;
using boost::weak_ptr;
using boost::shared_ptr;
using boost::bind;
using boost::optional;

/** Video readahead in frames */
#define VIDEO_READAHEAD 10

Butler::Butler (weak_ptr<const Film> film, shared_ptr<Player> player)
	: _film (film)
	, _player (player)
	, _pending_seek_accurate (false)
	, _finished (false)
{
	_player->Video.connect (bind (&VideoRingBuffers::put, &_video, _1, _2));
	_thread = new boost::thread (bind (&Butler::thread, this));
}

Butler::~Butler ()
{
	_thread->interrupt ();
	try {
		_thread->join ();
	} catch (boost::thread_interrupted& e) {
		/* No problem */
	}
	delete _thread;
}

void
Butler::thread ()
{
	while (true) {
		boost::mutex::scoped_lock lm (_mutex);

		while (_video.size() > VIDEO_READAHEAD && !_pending_seek_position) {
			_summon.wait (lm);
		}

		if (_pending_seek_position) {
			_player->seek (*_pending_seek_position, _pending_seek_accurate);
			_pending_seek_position = optional<DCPTime> ();
		}

		while (_video.size() < VIDEO_READAHEAD) {
			_arrived.notify_all ();
			if (_player->pass ()) {
				_finished = true;
				break;
			}
		}
	}
}

pair<shared_ptr<PlayerVideo>, DCPTime>
Butler::get_video ()
{
	boost::mutex::scoped_lock lm (_mutex);
	while (_video.size() == 0 && !_finished) {
		_arrived.wait (lm);
	}

	if (_finished) {
		return make_pair (shared_ptr<PlayerVideo>(), DCPTime());
	}

	return _video.get ();
}

void
Butler::seek (DCPTime position, bool accurate)
{
	_video.clear ();

	{
		boost::mutex::scoped_lock lm (_mutex);
		_finished = false;
		_pending_seek_position = position;
		_pending_seek_accurate = accurate;
	}

	_summon.notify_all ();
}